1 | unit Directories;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | var
|
---|
6 | HomeDir: string;
|
---|
7 | DataDir: string;
|
---|
8 | LocaleCode: string = '';
|
---|
9 | LocaleCodeAuto: string = '';
|
---|
10 |
|
---|
11 | function LocalizedFilePath(const Path: string): string;
|
---|
12 | procedure UnitInit;
|
---|
13 | function GetSavedDir(Home: Boolean = False): string;
|
---|
14 | function GetMapsDir(Home: Boolean = False): string;
|
---|
15 | function GetGraphicsDir: string;
|
---|
16 | function GetSoundsDir: string;
|
---|
17 | function GetAiDir: string;
|
---|
18 |
|
---|
19 |
|
---|
20 | implementation
|
---|
21 |
|
---|
22 | uses
|
---|
23 | FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils;
|
---|
24 |
|
---|
25 | function GetLocale: string;
|
---|
26 | var
|
---|
27 | Lang: string;
|
---|
28 | I: Integer;
|
---|
29 | T: string;
|
---|
30 | begin
|
---|
31 | // Win32 user may decide to override locale with LANG variable.
|
---|
32 | Lang := Copy(GetEnvironmentVariableUTF8('LANG'), 1, 2);
|
---|
33 |
|
---|
34 | if Lang = '' then begin
|
---|
35 | for i := 1 to Paramcount - 1 do
|
---|
36 | if (ParamStrUTF8(i) = '--LANG') or (ParamStrUTF8(i) = '-l') or
|
---|
37 | (ParamStrUTF8(i) = '--lang') then
|
---|
38 | Lang := ParamStrUTF8(i + 1);
|
---|
39 | end;
|
---|
40 | if Lang = '' then begin
|
---|
41 | T := '';
|
---|
42 | LazGetLanguageIDs(Lang, T);
|
---|
43 | Lang := Copy(Lang, 1, 2);
|
---|
44 | end;
|
---|
45 |
|
---|
46 | Result := Lang;
|
---|
47 | end;
|
---|
48 |
|
---|
49 | function LocalizedFilePath(const Path: string): string;
|
---|
50 | var
|
---|
51 | LocaleCodeDir: string;
|
---|
52 | begin
|
---|
53 | if LocaleCode = '' then begin
|
---|
54 | if LocaleCodeAuto = '' then LocaleCodeAuto := GetLocale;
|
---|
55 | LocaleCodeDir := LocaleCodeAuto;
|
---|
56 | end else LocaleCodeDir := LocaleCode;
|
---|
57 |
|
---|
58 | if LocaleCode <> 'en' then begin
|
---|
59 | Result := HomeDir + 'Localization' + DirectorySeparator + LocaleCodeDir + DirectorySeparator + Path;
|
---|
60 | if not DirectoryExists(Result) and not FileExists(Result) then
|
---|
61 | Result := HomeDir + Path;
|
---|
62 | end else Result := HomeDir + Path;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | procedure CopyDir(SourceDir, DestinationDir, Filter: string);
|
---|
66 | var
|
---|
67 | Src, Dst: TSearchRec;
|
---|
68 | begin
|
---|
69 | if not DirectoryExists(DestinationDir) then ForceDirectories(DestinationDir);
|
---|
70 | if FindFirst(SourceDir + DirectorySeparator + Filter, $21, Src) = 0 then
|
---|
71 | repeat
|
---|
72 | if (FindFirst(DestinationDir + DirectorySeparator + Src.Name, $21, Dst) <> 0) or
|
---|
73 | (Dst.Time < Src.Time) then
|
---|
74 | CopyFile(SourceDir + DirectorySeparator + Src.Name,
|
---|
75 | DestinationDir + DirectorySeparator + Src.Name, false);
|
---|
76 | FindClose(Dst);
|
---|
77 | until FindNext(Src) <> 0;
|
---|
78 | FindClose(Src);
|
---|
79 | end;
|
---|
80 |
|
---|
81 | procedure CopyFiles;
|
---|
82 | begin
|
---|
83 | if DirectoryExists(GetSavedDir(True)) and not DirectoryExists(GetSavedDir(False)) then
|
---|
84 | CopyDir(GetSavedDir(True), GetSavedDir(False), '*.*');
|
---|
85 | if DirectoryExists(GetMapsDir(True)) and not DirectoryExists(GetMapsDir(False)) then
|
---|
86 | CopyDir(GetMapsDir(True), GetMapsDir(False), '*.*');
|
---|
87 | end;
|
---|
88 |
|
---|
89 | procedure UnitInit;
|
---|
90 | var
|
---|
91 | AppDataDir: string;
|
---|
92 | begin
|
---|
93 | LocaleCode := '';
|
---|
94 | HomeDir := ExtractFilePath(ParamStr(0));
|
---|
95 |
|
---|
96 | AppDataDir := GetAppConfigDir(False);
|
---|
97 | if AppDataDir = '' then DataDir := HomeDir
|
---|
98 | else begin
|
---|
99 | if not DirectoryExists(AppDataDir) then ForceDirectories(AppDataDir);
|
---|
100 | DataDir := AppDataDir;
|
---|
101 | end;
|
---|
102 | CopyFiles;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | function GetSavedDir(Home: Boolean = False): string;
|
---|
106 | begin
|
---|
107 | if Home then Result := HomeDir + 'Saved'
|
---|
108 | else Result := DataDir + 'Saved';
|
---|
109 | end;
|
---|
110 |
|
---|
111 | function GetMapsDir(Home: Boolean = False): string;
|
---|
112 | begin
|
---|
113 | if Home then Result := HomeDir + 'Maps'
|
---|
114 | else Result := DataDir + 'Maps';
|
---|
115 | end;
|
---|
116 |
|
---|
117 | function GetGraphicsDir: string;
|
---|
118 | begin
|
---|
119 | Result := HomeDir + 'Graphics';
|
---|
120 | end;
|
---|
121 |
|
---|
122 | function GetSoundsDir: string;
|
---|
123 | begin
|
---|
124 | Result := HomeDir + 'Sounds';
|
---|
125 | end;
|
---|
126 |
|
---|
127 | function GetAiDir: string;
|
---|
128 | begin
|
---|
129 | Result := HomeDir + 'AI';
|
---|
130 | end;
|
---|
131 |
|
---|
132 | end.
|
---|