-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOptions.pas
More file actions
110 lines (94 loc) · 2.76 KB
/
Copy pathOptions.pas
File metadata and controls
110 lines (94 loc) · 2.76 KB
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
unit Options;
interface
uses
Windows,
System.SysUtils,
System.StrUtils,
IniFiles;
const
OPTIONS_FILENAME = 'sing-box-drover.ini';
SECTION_MAIN = 'sing-box-drover';
type
TTunStartMode = (tsmOn, tsmOff);
TSelectorMenuLayout = (smlAuto, smlFlat, smlNested);
TDroverOptions = record
sbDir: string;
sbConfigFile: string;
systemProxyAuto: boolean;
tunStartMode: TTunStartMode;
selectorMenuLayout: TSelectorMenuLayout;
selectorPersist: boolean;
logFile: string;
class function Load(filename: string): TDroverOptions; static;
private
class function ParseTunStartMode(s: string): TTunStartMode; static;
class function ParseSelectorMenuLayout(s: string): TSelectorMenuLayout; static;
end;
implementation
class function TDroverOptions.ParseTunStartMode(s: string): TTunStartMode;
begin
s := trim(LowerCase(s));
if MatchStr(s, ['off', '0', '']) then
exit(TTunStartMode.tsmOff)
else
exit(TTunStartMode.tsmOn);
end;
class function TDroverOptions.ParseSelectorMenuLayout(s: string): TSelectorMenuLayout;
begin
s := trim(LowerCase(s));
if s = 'flat' then
exit(TSelectorMenuLayout.smlFlat)
else if s = 'nested' then
exit(TSelectorMenuLayout.smlNested)
else
exit(TSelectorMenuLayout.smlAuto);
end;
class function TDroverOptions.Load(filename: string): TDroverOptions;
var
s, path, currentDir: string;
f: TIniFile;
begin
currentDir := IncludeTrailingPathDelimiter(ExtractFilePath(filename));
result := Default (TDroverOptions);
try
f := TIniFile.Create(filename);
try
with f do
begin
s := ReadString(SECTION_MAIN, 'sb-dir', '');
if s = '' then
s := currentDir
else
s := IncludeTrailingPathDelimiter(s);
result.sbDir := s;
s := ReadString(SECTION_MAIN, 'sb-config-file', 'config.json');
if not s.Contains(':') then
begin
for path in [currentDir + s, result.sbDir + s] do
begin
if FileExists(path) then
begin
s := path;
break;
end;
end;
end;
result.sbConfigFile := s;
result.tunStartMode := ParseTunStartMode(ReadString(SECTION_MAIN, 'tun-start-mode', ''));
result.systemProxyAuto := ReadBool(SECTION_MAIN, 'system-proxy-auto', false);
result.selectorMenuLayout := ParseSelectorMenuLayout(ReadString(SECTION_MAIN, 'selector-menu-layout', ''));
result.selectorPersist := ReadBool(SECTION_MAIN, 'selector-persist', true);
s := ReadString(SECTION_MAIN, 'log-file', '');
if (s <> '') and (not s.Contains(':')) then
begin
s := currentDir + s;
end;
result.logFile := s;
end;
finally
f.Free;
end;
except
end;
end;
end.