-
Notifications
You must be signed in to change notification settings - Fork 5
/
brk_tg_config.pas
263 lines (222 loc) · 5.29 KB
/
brk_tg_config.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
unit brk_tg_config;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpjson
;
type
{ TTelegramPaymentsConf }
TTelegramPaymentsConf = class
private
FCurrency: String;
FDisabled: Boolean;
FPrice: Integer;
FToken: String;
published
property Token: String read FToken write FToken;
property Price: Integer read FPrice write FPrice;
property Disabled: Boolean read FDisabled write FDisabled;
property Currency: String read FCurrency write FCurrency;
end;
{ TTelegramConf }
TTelegramConf = class
private
FPayments: TTelegramPaymentsConf;
FToken: String;
FUsername: String;
FUsers: TStrings;
public
constructor Create;
destructor Destroy; override;
published
property Token: String read FToken write FToken;
property UserName: String read FUsername write FUserName;
property Payments: TTelegramPaymentsConf read FPayments write FPayments;
property Users: TStrings read FUsers write FUsers;
end;
{ TDebugInfo }
TDebugInfo = class
private
FEnabled: Boolean;
published
property Enabled: Boolean read FEnabled write FEnabled;
end;
{ TLoggerConfig }
TLoggerConf = class
private
FActive: Boolean;
FFilename: String;
published
property Active: Boolean read FActive write FActive;
property FileName: String read FFilename write FFilename;
end;
{ TDBConf }
TDBConf = class
private
FDatabase: String;
FDriver: String;
FHost: String;
FLoggerConf: TLoggerConf;
FPassword: String;
FUser: String;
public
constructor Create;
destructor Destroy; override;
published
property Driver: String read FDriver write FDriver;
property Database: String read FDatabase write FDatabase;
property User: String read FUser write FUser;
property Password: String read FPassword write FPassword;
property Host: String read FHost write FHost;
property Logger: TLoggerConf read FLoggerConf write FLoggerConf;
end;
{ TAppConf }
TAppConf = class
private
FTitle: String;
published
property Title: String read FTitle write FTitle;
end;
{ TBotConf }
TBotConf = class
private
FAdverts: TStrings;
FDebug: Boolean;
FFreeLimit: Integer;
FTelegram: TTelegramConf;
procedure SetAdverts(AValue: TStrings);
public
constructor Create;
destructor Destroy; override;
published
property Telegram: TTelegramConf read FTelegram write FTelegram;
property Adverts: TStrings read FAdverts write SetAdverts;
property Debug: Boolean read FDebug write FDebug;
property FreeLimit: Integer read FFreeLimit write FFreeLimit;
end;
procedure LoadFromJSON(AObject: TObject; const AFileName: String);
procedure SaveToJSON(AObject: TObject; const AFileName: String);
function StringToJSONObject(const AString: String): TJSONObject;
implementation
uses
jsonparser, fpjsonrtti, jsonscanner
;
var
MyCriticalSection: TRTLCriticalSection;
procedure LoadFromJSON(AObject: TObject; const AFileName: String);
var
ADeStreamer: TJSONDeStreamer;
AJSON: TStringList;
begin
if not FileExists(AFileName) then
Exit;
ADeStreamer:=TJSONDeStreamer.Create(nil);
try
AJSON:=TStringList.Create;
try
AJSON.LoadFromFile(AFileName);
try
ADeStreamer.JSONToObject(AJSON.Text, AObject);
except
end;
finally
AJSON.Free;
end;
finally
ADeStreamer.Free;
end;
end;
function StringToJSONObject(const AString: String): TJSONObject;
var
lParser: TJSONParser;
begin
Result := nil;
if AString<>EmptyStr then
begin
lParser := TJSONParser.Create(AString, DefaultOptions);
try
try
Result := lParser.Parse as TJSONObject
except
Result:=nil;
end;
finally
lParser.Free;
end;
end;
end;
procedure SaveToJSON(AObject: TObject; const AFileName: String);
var
AStreamer: TJSONStreamer;
AJSON: TStringList;
AJSONObject: TJSONObject;
begin
AStreamer:=TJSONStreamer.Create(nil);
AStreamer.Options:=[jsoTStringsAsArray];
try
AJSON:=TStringList.Create;
try
try
AJSONObject:=AStreamer.ObjectToJSON(AObject);
try
AJSON.Text:=AJSONObject.FormatJSON();
AJSON.SaveToFile(AFileName);
finally
AJSONObject.Free;
end;
except
end;
finally
AJSON.Free;
end;
finally
AStreamer.Free;
end;
end;
{ TDBConf }
constructor TDBConf.Create;
begin
inherited;
FLoggerConf:=TLoggerConf.Create;
end;
destructor TDBConf.Destroy;
begin
FLoggerConf.Free;
inherited Destroy;
end;
{ TTelegramConf }
constructor TTelegramConf.Create;
begin
FPayments:=TTelegramPaymentsConf.Create;
FUsers:=TStringList.Create;
end;
destructor TTelegramConf.Destroy;
begin
FUsers.Free;
FPayments.Free;
inherited Destroy;
end;
{ TBotConf }
procedure TBotConf.SetAdverts(AValue: TStrings);
begin
if FAdverts=AValue then Exit;
EnterCriticalsection(MyCriticalSection{%H-});
FAdverts.Assign(AValue);
LeaveCriticalsection(MyCriticalSection);
end;
constructor TBotConf.Create;
begin
FTelegram:=TTelegramConf.Create;
FAdverts:=TStringList.Create;
end;
destructor TBotConf.Destroy;
begin
FAdverts.Free;
FTelegram.Free;
inherited Destroy;
end;
initialization
InitCriticalSection(MyCriticalSection{%H-});
finalization
DoneCriticalsection(MyCriticalSection);
end.