| 1 | unit Directories;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | var
|
|---|
| 6 | DataDir: string;
|
|---|
| 7 | LocaleCode: string = '';
|
|---|
| 8 | LocaleCodeAuto: string = '';
|
|---|
| 9 |
|
|---|
| 10 | function LocalizedFilePath(const Path: string): string;
|
|---|
| 11 | procedure UnitInit;
|
|---|
| 12 | function GetSavedDir(Home: Boolean = False): string;
|
|---|
| 13 | function GetMapsDir(Home: Boolean = False): string;
|
|---|
| 14 | function GetGraphicsDir: string;
|
|---|
| 15 | function GetSoundsDir: string;
|
|---|
| 16 | function GetMusicDir: string;
|
|---|
| 17 | function GetAiDir: string;
|
|---|
| 18 | function GetAppSharePath(Path: string): string;
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | implementation
|
|---|
| 22 |
|
|---|
| 23 | uses
|
|---|
| 24 | FileUtil, LCLIntf, LCLType, LCLProc, LazUTF8, SysUtils, LazFileUtils, Forms,
|
|---|
| 25 | Translations;
|
|---|
| 26 |
|
|---|
| 27 | function GetAppSharePath(Path: string): string;
|
|---|
| 28 | {$IFDEF UNIX}
|
|---|
| 29 | var
|
|---|
| 30 | NewPath: string;
|
|---|
| 31 | {$ENDIF}
|
|---|
| 32 | begin
|
|---|
| 33 | Result := ExtractFileDir(Application.ExeName) + DirectorySeparator + Path;
|
|---|
| 34 | {$IFDEF UNIX}
|
|---|
| 35 | // If installed in Linux system then try to use different installation directory
|
|---|
| 36 | NewPath := ExtractFileDir(Application.ExeName) + DirectorySeparator + '..' +
|
|---|
| 37 | DirectorySeparator + 'share' + DirectorySeparator +
|
|---|
| 38 | ExtractFileNameOnly(Application.ExeName) + DirectorySeparator + Path;
|
|---|
| 39 |
|
|---|
| 40 | if not DirectoryExists(Result) then begin
|
|---|
| 41 | if DirectoryExists(NewPath) then begin
|
|---|
| 42 | Result := NewPath;
|
|---|
| 43 | Exit;
|
|---|
| 44 | end;
|
|---|
| 45 | end;
|
|---|
| 46 |
|
|---|
| 47 | if not FileExists(Result) then begin
|
|---|
| 48 | if FileExists(NewPath) then begin
|
|---|
| 49 | Result := NewPath;
|
|---|
| 50 | Exit;
|
|---|
| 51 | end;
|
|---|
| 52 | end;
|
|---|
| 53 | {$ENDIF}
|
|---|
| 54 | end;
|
|---|
| 55 |
|
|---|
| 56 | function GetLocale: string;
|
|---|
| 57 | var
|
|---|
| 58 | I: Integer;
|
|---|
| 59 | LanguageID: TLanguageID;
|
|---|
| 60 | begin
|
|---|
| 61 | // Win32 user may decide to override locale with LANG variable.
|
|---|
| 62 | Result := Copy(GetEnvironmentVariableUTF8('LANG'), 1, 2);
|
|---|
| 63 |
|
|---|
| 64 | if Result = '' then begin
|
|---|
| 65 | for I := 1 to ParamCount - 1 do
|
|---|
| 66 | if (ParamStrUTF8(I) = '--LANG') or (ParamStrUTF8(I) = '-l') or
|
|---|
| 67 | (ParamStrUTF8(I) = '--lang') then
|
|---|
| 68 | Result := ParamStrUTF8(I + 1);
|
|---|
| 69 | end;
|
|---|
| 70 | if Result = '' then begin
|
|---|
| 71 | LanguageID := GetLanguageID;
|
|---|
| 72 | Result := Copy(LanguageID.LanguageID, 1, 2);
|
|---|
| 73 | end;
|
|---|
| 74 | end;
|
|---|
| 75 |
|
|---|
| 76 | function LocalizedFilePath(const Path: string): string;
|
|---|
| 77 | var
|
|---|
| 78 | LocaleCodeDir: string;
|
|---|
| 79 | begin
|
|---|
| 80 | if LocaleCode = '' then begin
|
|---|
| 81 | if LocaleCodeAuto = '' then LocaleCodeAuto := GetLocale;
|
|---|
| 82 | LocaleCodeDir := LocaleCodeAuto;
|
|---|
| 83 | end else LocaleCodeDir := LocaleCode;
|
|---|
| 84 |
|
|---|
| 85 | if LocaleCode <> 'en' then begin
|
|---|
| 86 | Result := GetAppSharePath('Localization' + DirectorySeparator + LocaleCodeDir + DirectorySeparator + Path);
|
|---|
| 87 | if not DirectoryExists(Result) and not FileExists(Result) then
|
|---|
| 88 | Result := GetAppSharePath(Path);
|
|---|
| 89 | end else Result := GetAppSharePath(Path);
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | procedure CopyDir(SourceDir, DestinationDir, Filter: string);
|
|---|
| 93 | var
|
|---|
| 94 | Src, Dst: TSearchRec;
|
|---|
| 95 | begin
|
|---|
| 96 | if not DirectoryExists(DestinationDir) then ForceDirectories(DestinationDir);
|
|---|
| 97 | if FindFirst(SourceDir + DirectorySeparator + Filter, $21, Src) = 0 then
|
|---|
| 98 | repeat
|
|---|
| 99 | if (FindFirst(DestinationDir + DirectorySeparator + Src.Name, $21, Dst) <> 0) or
|
|---|
| 100 | (Dst.Time < Src.Time) then
|
|---|
| 101 | CopyFile(SourceDir + DirectorySeparator + Src.Name,
|
|---|
| 102 | DestinationDir + DirectorySeparator + Src.Name, False);
|
|---|
| 103 | FindClose(Dst);
|
|---|
| 104 | until FindNext(Src) <> 0;
|
|---|
| 105 | FindClose(Src);
|
|---|
| 106 | end;
|
|---|
| 107 |
|
|---|
| 108 | procedure CopyFiles;
|
|---|
| 109 | begin
|
|---|
| 110 | if DirectoryExists(GetSavedDir(True)) and not DirectoryExists(GetSavedDir(False)) then
|
|---|
| 111 | CopyDir(GetSavedDir(True), GetSavedDir(False), '*.*');
|
|---|
| 112 | if DirectoryExists(GetMapsDir(True)) and not DirectoryExists(GetMapsDir(False)) then
|
|---|
| 113 | CopyDir(GetMapsDir(True), GetMapsDir(False), '*.*');
|
|---|
| 114 | end;
|
|---|
| 115 |
|
|---|
| 116 | procedure UnitInit;
|
|---|
| 117 | var
|
|---|
| 118 | AppDataDir: string;
|
|---|
| 119 | begin
|
|---|
| 120 | LocaleCode := '';
|
|---|
| 121 |
|
|---|
| 122 | AppDataDir := GetAppConfigDir(False);
|
|---|
| 123 | if AppDataDir = '' then DataDir := GetAppSharePath('')
|
|---|
| 124 | else begin
|
|---|
| 125 | if not DirectoryExists(AppDataDir) then ForceDirectories(AppDataDir);
|
|---|
| 126 | DataDir := AppDataDir;
|
|---|
| 127 | end;
|
|---|
| 128 | CopyFiles;
|
|---|
| 129 | end;
|
|---|
| 130 |
|
|---|
| 131 | function GetSavedDir(Home: Boolean = False): string;
|
|---|
| 132 | begin
|
|---|
| 133 | if Home then Result := GetAppSharePath('Saved')
|
|---|
| 134 | else Result := DataDir + 'Saved';
|
|---|
| 135 | end;
|
|---|
| 136 |
|
|---|
| 137 | function GetMapsDir(Home: Boolean = False): string;
|
|---|
| 138 | begin
|
|---|
| 139 | if Home then Result := GetAppSharePath('Maps')
|
|---|
| 140 | else Result := DataDir + 'Maps';
|
|---|
| 141 | end;
|
|---|
| 142 |
|
|---|
| 143 | function GetGraphicsDir: string;
|
|---|
| 144 | begin
|
|---|
| 145 | Result := GetAppSharePath('Graphics');
|
|---|
| 146 | end;
|
|---|
| 147 |
|
|---|
| 148 | function GetSoundsDir: string;
|
|---|
| 149 | begin
|
|---|
| 150 | Result := GetAppSharePath('Sounds');
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | function GetMusicDir: string;
|
|---|
| 154 | begin
|
|---|
| 155 | Result := GetAppSharePath('Music');
|
|---|
| 156 | end;
|
|---|
| 157 |
|
|---|
| 158 | function GetAiDir: string;
|
|---|
| 159 | begin
|
|---|
| 160 | Result := GetAppSharePath('AI');
|
|---|
| 161 | end;
|
|---|
| 162 |
|
|---|
| 163 | end.
|
|---|