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

Last change on this file was 381, checked in by chronos, 3 years ago
  • Fixed: Error if map without map picture was renamed.
  • Fixed: Copy default map files and example saved game only once if such target directories don't exist yet.
File size: 3.4 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 CopyDir(SourceDir, DestinationDir, Filter: string);
66var
67 Src, Dst: TSearchRec;
68begin
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);
79end;
80
81procedure CopyFiles;
82begin
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), '*.*');
87end;
88
89procedure UnitInit;
90var
91 AppDataDir: string;
92begin
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;
103end;
104
105function GetSavedDir(Home: Boolean = False): string;
106begin
107 if Home then Result := HomeDir + 'Saved'
108 else Result := DataDir + 'Saved';
109end;
110
111function GetMapsDir(Home: Boolean = False): string;
112begin
113 if Home then Result := HomeDir + 'Maps'
114 else Result := DataDir + 'Maps';
115end;
116
117function GetGraphicsDir: string;
118begin
119 Result := HomeDir + 'Graphics';
120end;
121
122function GetSoundsDir: string;
123begin
124 Result := HomeDir + 'Sounds';
125end;
126
127function GetAiDir: string;
128begin
129 Result := HomeDir + 'AI';
130end;
131
132end.
Note: See TracBrowser for help on using the repository browser.