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