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 UnitInit;
|
---|
66 | var
|
---|
67 | AppDataDir: string;
|
---|
68 | src, dst: TSearchRec;
|
---|
69 | begin
|
---|
70 | LocaleCode := '';
|
---|
71 | HomeDir := ExtractFilePath(ParamStr(0));
|
---|
72 |
|
---|
73 | AppDataDir := GetAppConfigDir(False);
|
---|
74 | if AppDataDir = '' then
|
---|
75 | DataDir := HomeDir
|
---|
76 | else
|
---|
77 | begin
|
---|
78 | if not DirectoryExists(AppDataDir) then CreateDir(AppDataDir);
|
---|
79 | DataDir := AppDataDir;
|
---|
80 | end;
|
---|
81 | if not DirectoryExists(GetSavedDir) then CreateDir(GetSavedDir);
|
---|
82 | if not DirectoryExists(GetMapsDir) then CreateDir(GetMapsDir);
|
---|
83 |
|
---|
84 | // Copy appdata if not done yet
|
---|
85 | if FindFirst(GetSavedDir(True) + DirectorySeparator + '*.cevo', $21, src) = 0 then
|
---|
86 | repeat
|
---|
87 | if (FindFirst(GetSavedDir(True) + DirectorySeparator + src.Name, $21, dst) <> 0) or
|
---|
88 | (dst.Time < src.Time) then
|
---|
89 | CopyFile(PChar(GetSavedDir(True) + DirectorySeparator + src.Name),
|
---|
90 | PChar(GetSavedDir(True) + DirectorySeparator + src.Name), false);
|
---|
91 | FindClose(dst);
|
---|
92 | until FindNext(src) <> 0;
|
---|
93 | FindClose(src);
|
---|
94 |
|
---|
95 | // Copy appdata if not done yet
|
---|
96 | if FindFirst(GetMapsDir(True) + DirectorySeparator + '*.*', $21, src) = 0 then
|
---|
97 | repeat
|
---|
98 | if (FindFirst(GetMapsDir(True) + DirectorySeparator + src.Name, $21, dst) <> 0) or
|
---|
99 | (dst.Time < src.Time) then
|
---|
100 | CopyFile(PChar(GetMapsDir(True) + DirectorySeparator + src.Name),
|
---|
101 | PChar(GetMapsDir(True) + DirectorySeparator + src.Name), false);
|
---|
102 | FindClose(dst);
|
---|
103 | until FindNext(src) <> 0;
|
---|
104 | FindClose(src);
|
---|
105 | end;
|
---|
106 |
|
---|
107 | function GetSavedDir(Home: Boolean = False): string;
|
---|
108 | begin
|
---|
109 | if Home then Result := HomeDir + 'Saved'
|
---|
110 | else Result := DataDir + 'Saved';
|
---|
111 | end;
|
---|
112 |
|
---|
113 | function GetMapsDir(Home: Boolean = False): string;
|
---|
114 | begin
|
---|
115 | if Home then Result := HomeDir + 'Maps'
|
---|
116 | else Result := DataDir + 'Maps';
|
---|
117 | end;
|
---|
118 |
|
---|
119 | function GetGraphicsDir: string;
|
---|
120 | begin
|
---|
121 | Result := HomeDir + 'Graphics';
|
---|
122 | end;
|
---|
123 |
|
---|
124 | function GetSoundsDir: string;
|
---|
125 | begin
|
---|
126 | Result := HomeDir + 'Sounds';
|
---|
127 | end;
|
---|
128 |
|
---|
129 | function GetAiDir: string;
|
---|
130 | begin
|
---|
131 | Result := HomeDir + 'AI';
|
---|
132 | end;
|
---|
133 |
|
---|
134 | end.
|
---|