source: tags/1.2.0/Packages/CevoComponents/Directories.pas

Last change on this file was 199, checked in by chronos, 4 years ago
  • Fixed: LocalizedFilePath function can accept file names and also directory names. Check also existence of localized directory. Directories were not correctly verified under Windows.
File size: 3.6 KB
Line 
1unit Directories;
2
3interface
4
5var
6 HomeDir: string;
7 DataDir: string;
8 LocaleCode: string = '';
9 LocaleCodeAuto: string = '';
10
11function LocalizedFilePath(const Path: string): string;
12procedure UnitInit;
13function GetSavedDir(Home: Boolean = False): string;
14function GetMapsDir(Home: Boolean = False): string;
15function GetGraphicsDir: string;
16function GetSoundsDir: string;
17function GetAiDir: string;
18
19
20implementation
21
22uses
23 FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils;
24
25function GetLocale: string;
26var
27 Lang: string;
28 I: Integer;
29 T: string;
30begin
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;
47end;
48
49function LocalizedFilePath(const Path: string): string;
50var
51 LocaleCodeDir: string;
52begin
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;
63end;
64
65procedure UnitInit;
66var
67 AppDataDir: string;
68 src, dst: TSearchRec;
69begin
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);
105end;
106
107function GetSavedDir(Home: Boolean = False): string;
108begin
109 if Home then Result := HomeDir + 'Saved'
110 else Result := DataDir + 'Saved';
111end;
112
113function GetMapsDir(Home: Boolean = False): string;
114begin
115 if Home then Result := HomeDir + 'Maps'
116 else Result := DataDir + 'Maps';
117end;
118
119function GetGraphicsDir: string;
120begin
121 Result := HomeDir + 'Graphics';
122end;
123
124function GetSoundsDir: string;
125begin
126 Result := HomeDir + 'Sounds';
127end;
128
129function GetAiDir: string;
130begin
131 Result := HomeDir + 'AI';
132end;
133
134end.
Note: See TracBrowser for help on using the repository browser.