source: branches/zoom/Packages/CevoComponents/Directories.pas

Last change on this file was 664, checked in by chronos, 2 months ago
  • Added: Ability to play music in background in start screen and in-game. Used uos as audio library.
File size: 4.6 KB
Line 
1unit Directories;
2
3interface
4
5var
6 DataDir: string;
7 LocaleCode: string = '';
8 LocaleCodeAuto: string = '';
9
10function LocalizedFilePath(const Path: string): string;
11procedure UnitInit;
12function GetSavedDir(Home: Boolean = False): string;
13function GetMapsDir(Home: Boolean = False): string;
14function GetGraphicsDir: string;
15function GetSoundsDir: string;
16function GetMusicDir: string;
17function GetAiDir: string;
18function GetAppSharePath(Path: string): string;
19
20
21implementation
22
23uses
24 FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils, LazFileUtils, Forms;
25
26function GetAppSharePath(Path: string): string;
27{$IFDEF UNIX}
28var
29 NewPath: string;
30{$ENDIF}
31begin
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}
54end;
55
56function GetLocale: string;
57var
58 Lang: string;
59 I: Integer;
60 T: string;
61begin
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;
78end;
79
80function LocalizedFilePath(const Path: string): string;
81var
82 LocaleCodeDir: string;
83begin
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);
94end;
95
96procedure CopyDir(SourceDir, DestinationDir, Filter: string);
97var
98 Src, Dst: TSearchRec;
99begin
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);
110end;
111
112procedure CopyFiles;
113begin
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), '*.*');
118end;
119
120procedure UnitInit;
121var
122 AppDataDir: string;
123begin
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;
133end;
134
135function GetSavedDir(Home: Boolean = False): string;
136begin
137 if Home then Result := GetAppSharePath('Saved')
138 else Result := DataDir + 'Saved';
139end;
140
141function GetMapsDir(Home: Boolean = False): string;
142begin
143 if Home then Result := GetAppSharePath('Maps')
144 else Result := DataDir + 'Maps';
145end;
146
147function GetGraphicsDir: string;
148begin
149 Result := GetAppSharePath('Graphics');
150end;
151
152function GetSoundsDir: string;
153begin
154 Result := GetAppSharePath('Sounds');
155end;
156
157function GetMusicDir: string;
158begin
159 Result := GetAppSharePath('Music');
160end;
161
162function GetAiDir: string;
163begin
164 Result := GetAppSharePath('AI');
165end;
166
167end.
Note: See TracBrowser for help on using the repository browser.