This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvcmi.dirs.base.pas
142 lines (106 loc) · 3.16 KB
/
vcmi.dirs.base.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
{ This file is a part of Map editor for VCMI project
Copyright (C) 2016-2017 Alexander Shishkin alexvins@users.sourceforge.net
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit vcmi.dirs.base;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, LazUTF8, LazFileUtils, LazUTF8Classes;
type
TDirs = class;
TDirsClass = class of TDirs;
{ TDirs }
TDirs = class
private
FExecutablePath: AnsiString;
FPathsConfig: TStringListUTF8;
protected
function GetUserProfilePath: AnsiString; virtual;
function GetUserCachePath: AnsiString; virtual;
function GetUserConfigPath: AnsiString; virtual;
public
constructor Create; virtual;
destructor Destroy; override;
property UserCachePath: AnsiString read GetUserCachePath;
property UserConfigPath: AnsiString read GetUserConfigPath;
procedure FillDataPaths(AList: TStringListUTF8); virtual;
procedure CreatePaths;
class function GetActualClass: TDirsClass;
end;
implementation
{$PUSH}
{$WARN SYMBOL_PLATFORM OFF}
{$WARN UNIT_PLATFORM OFF}
{$IF DEFINED (MSWINDOWS)}
uses vcmi.dirs.windows;
type
TDirsActual = TDirsWindows;
{$ELSEIF DEFINED (LINUX) OR DEFINED(BSD) AND NOT DEFINED(DARWIN)}
uses vcmi.dirs.xdg;
type
TDirsActual = TDirsXDG;
{$ELSE}
{$WARNING OS target not supported}
type
TDirsActual = TDirs;
{$ENDIF}
{$POP}
const
GAME_PATH_CONFIG = 'gamepath.txt';
{ TDirs }
function TDirs.GetUserConfigPath: AnsiString;
begin
Result := GetUserProfilePath();
end;
function TDirs.GetUserProfilePath: AnsiString;
begin
Result := FExecutablePath; //fallback to executable path
end;
function TDirs.GetUserCachePath: AnsiString;
begin
Result := GetUserProfilePath();
end;
constructor TDirs.Create;
var
s: AnsiString;
begin
FPathsConfig := TStringListUTF8.Create;
FExecutablePath := ExtractFilePath(ParamStrUTF8(0));
s := FExecutablePath + GAME_PATH_CONFIG;
if FileExistsUTF8(s) then
begin
FPathsConfig.LoadFromFile(s);
end;
FPathsConfig.Insert(0, FExecutablePath);
end;
destructor TDirs.Destroy;
begin
FPathsConfig.Free;
inherited Destroy;
end;
procedure TDirs.FillDataPaths(AList: TStringListUTF8);
begin
AList.AddStrings(FPathsConfig);
AList.Add(GetUserProfilePath());
end;
procedure TDirs.CreatePaths;
begin
if not ForceDirectoriesUTF8(UserCachePath) then
begin
raise Exception.CreateFmt('Unable to create path %s',[UserCachePath]);
end;
end;
class function TDirs.GetActualClass: TDirsClass;
begin
Result := TDirsActual;
end;
end.