source: trunk/Packages/CevoComponents/Directories.pas

Last change on this file was 713, checked in by chronos, 2 weeks ago
  • Modified: Code cleanup.
File size: 4.4 KB
Line 
1unit Directories;
2
3interface
4
5var
6 DataDir: string;
7 LocaleCode: string = '';
8 LocaleCodeAuto: string = '';
9
10function LocalizedFilePath(const Path: string): string;
11procedure UnitInit;
12function GetSavedDir(Home: Boolean = False): string;
13function GetMapsDir(Home: Boolean = False): string;
14function GetGraphicsDir: string;
15function GetSoundsDir: string;
16function GetMusicDir: string;
17function GetAiDir: string;
18function GetAppSharePath(Path: string): string;
19
20
21implementation
22
23uses
24 FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils, LazFileUtils, Forms,
25 Translations;
26
27function GetAppSharePath(Path: string): string;
28{$IFDEF UNIX}
29var
30 NewPath: string;
31{$ENDIF}
32begin
33 Result := ExtractFileDir(Application.ExeName) + DirectorySeparator + Path;
34 {$IFDEF UNIX}
35 // If installed in Linux system then try to use different installation directory
36 NewPath := ExtractFileDir(Application.ExeName) + DirectorySeparator + '..' +
37 DirectorySeparator + 'share' + DirectorySeparator +
38 ExtractFileNameOnly(Application.ExeName) + DirectorySeparator + Path;
39
40 if not DirectoryExists(Result) then begin
41 if DirectoryExists(NewPath) then begin
42 Result := NewPath;
43 Exit;
44 end;
45 end;
46
47 if not FileExists(Result) then begin
48 if FileExists(NewPath) then begin
49 Result := NewPath;
50 Exit;
51 end;
52 end;
53 {$ENDIF}
54end;
55
56function GetLocale: string;
57var
58 I: Integer;
59 LanguageID: TLanguageID;
60begin
61 // Win32 user may decide to override locale with LANG variable.
62 Result := Copy(GetEnvironmentVariableUTF8('LANG'), 1, 2);
63
64 if Result = '' then begin
65 for I := 1 to ParamCount - 1 do
66 if (ParamStrUTF8(I) = '--LANG') or (ParamStrUTF8(I) = '-l') or
67 (ParamStrUTF8(I) = '--lang') then
68 Result := ParamStrUTF8(I + 1);
69 end;
70 if Result = '' then begin
71 LanguageID := GetLanguageID;
72 Result := Copy(LanguageID.LanguageID, 1, 2);
73 end;
74end;
75
76function LocalizedFilePath(const Path: string): string;
77var
78 LocaleCodeDir: string;
79begin
80 if LocaleCode = '' then begin
81 if LocaleCodeAuto = '' then LocaleCodeAuto := GetLocale;
82 LocaleCodeDir := LocaleCodeAuto;
83 end else LocaleCodeDir := LocaleCode;
84
85 if LocaleCode <> 'en' then begin
86 Result := GetAppSharePath('Localization' + DirectorySeparator + LocaleCodeDir + DirectorySeparator + Path);
87 if not DirectoryExists(Result) and not FileExists(Result) then
88 Result := GetAppSharePath(Path);
89 end else Result := GetAppSharePath(Path);
90end;
91
92procedure CopyDir(SourceDir, DestinationDir, Filter: string);
93var
94 Src, Dst: TSearchRec;
95begin
96 if not DirectoryExists(DestinationDir) then ForceDirectories(DestinationDir);
97 if FindFirst(SourceDir + DirectorySeparator + Filter, $21, Src) = 0 then
98 repeat
99 if (FindFirst(DestinationDir + DirectorySeparator + Src.Name, $21, Dst) <> 0) or
100 (Dst.Time < Src.Time) then
101 CopyFile(SourceDir + DirectorySeparator + Src.Name,
102 DestinationDir + DirectorySeparator + Src.Name, False);
103 FindClose(Dst);
104 until FindNext(Src) <> 0;
105 FindClose(Src);
106end;
107
108procedure CopyFiles;
109begin
110 if DirectoryExists(GetSavedDir(True)) and not DirectoryExists(GetSavedDir(False)) then
111 CopyDir(GetSavedDir(True), GetSavedDir(False), '*.*');
112 if DirectoryExists(GetMapsDir(True)) and not DirectoryExists(GetMapsDir(False)) then
113 CopyDir(GetMapsDir(True), GetMapsDir(False), '*.*');
114end;
115
116procedure UnitInit;
117var
118 AppDataDir: string;
119begin
120 LocaleCode := '';
121
122 AppDataDir := GetAppConfigDir(False);
123 if AppDataDir = '' then DataDir := GetAppSharePath('')
124 else begin
125 if not DirectoryExists(AppDataDir) then ForceDirectories(AppDataDir);
126 DataDir := AppDataDir;
127 end;
128 CopyFiles;
129end;
130
131function GetSavedDir(Home: Boolean = False): string;
132begin
133 if Home then Result := GetAppSharePath('Saved')
134 else Result := DataDir + 'Saved';
135end;
136
137function GetMapsDir(Home: Boolean = False): string;
138begin
139 if Home then Result := GetAppSharePath('Maps')
140 else Result := DataDir + 'Maps';
141end;
142
143function GetGraphicsDir: string;
144begin
145 Result := GetAppSharePath('Graphics');
146end;
147
148function GetSoundsDir: string;
149begin
150 Result := GetAppSharePath('Sounds');
151end;
152
153function GetMusicDir: string;
154begin
155 Result := GetAppSharePath('Music');
156end;
157
158function GetAiDir: string;
159begin
160 Result := GetAppSharePath('AI');
161end;
162
163end.
Note: See TracBrowser for help on using the repository browser.