This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupper.pas
More file actions
422 lines (339 loc) · 10.6 KB
/
Copy pathBackupper.pas
File metadata and controls
422 lines (339 loc) · 10.6 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
unit Backupper;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, ShellApi, Vcl.ExtCtrls,
IOUtils, Types, DateUtils;
Const
fn = 'LastGeneratedFolder.txt';
CrLf = #13#10;
type
TSaveBackupper = class(TForm)
btnDo: TButton;
lblBackup: TLabel;
tmrAutoBackup: TTimer;
lblAutosave: TLabel;
btnTimerBool: TButton;
edtAutoSave: TEdit;
lblCountDown: TLabel;
tmrRefresh: TTimer;
function GetUserName: String;
function CopyDir(const fromDir, toDir: string): Boolean;
function GetLastModifiedFolderName(AFolder: String): string;
function LeerArchivox(const FileName: TFileName): String;
function FileIsEmpty(const FileName: String): Boolean;
function GenerateRandomString(const ALength: Integer;
const ACharSequence
: String =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'): String;
procedure Remove(const Dir: string);
procedure btnDoClick(Sender: TObject);
procedure tmrAutoBackupTimer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure MakeAStringlistAndSaveThat(CheckForFileOnly: Boolean);
procedure edtAutosaveChange(Sender: TObject);
procedure btnTimerBoolClick(Sender: TObject);
procedure RenameFolder(FolderOld, FolderNew: string);
procedure tmrRefreshTimer(Sender: TObject);
private
TimerInterval: Cardinal;
CountDown: Integer;
GeneratedBackupFolder: String;
BackupFolder: String;
SaveFolder: String;
DiffFolder: String;
LastModifiedSave: String;
Autosaving: Boolean;
User: String;
public
{ Public declarations }
end;
var
SaveBackupper: TSaveBackupper;
implementation
{$R *.dfm}
procedure TSaveBackupper.btnTimerBoolClick(Sender: TObject);
begin
tmrAutoBackup.Enabled := NOT tmrAutoBackup.Enabled;
if (tmrAutoBackup.Enabled) then
btnTimerBool.Caption := 'Disable autosaving'
else if (NOT tmrAutoBackup.Enabled) then
btnTimerBool.Caption := 'Enable autosaving';
tmrRefresh.Enabled := tmrAutoBackup.Enabled;
if tmrRefresh.Enabled then
CountDown := TimerInterval;
end;
function TSaveBackupper.CopyDir(const fromDir, toDir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;
procedure TSaveBackupper.edtAutosaveChange(Sender: TObject);
begin
tmrAutoBackup.Enabled := False;
tmrRefresh.Enabled := False;
btnTimerBool.Enabled := False;
if (edtAutoSave.Text = '') OR (StrToInt(edtAutoSave.Text) <= 9) then
begin
lblCountDown.Caption := 'ERROR';
btnTimerBool.Caption := 'ERROR';
exit
end;
TimerInterval := StrToInt(edtAutoSave.Text);
CountDown := TimerInterval;
if TimerInterval > 9 then
tmrAutoBackup.Interval := TimerInterval * 1000;
if btnTimerBool.Caption = 'ERROR' then
btnTimerBool.Caption := 'Enable autosaving';
if not(btnTimerBool.Caption = 'Enable autosaving') then
begin
tmrRefresh.Enabled := True;
tmrAutoBackup.Enabled := True;
end;
btnTimerBool.Enabled := True;
end;
function TSaveBackupper.GetUserName: String;
var
nSize: DWord;
begin
nSize := 1024;
SetLength(Result, nSize);
if Winapi.Windows.GetUserName(PChar(Result), nSize) then
begin
SetLength(Result, nSize - 1)
end
else
begin
RaiseLastOSError;
end
end;
procedure TSaveBackupper.FormCreate(Sender: TObject);
begin
Autosaving := False;
User := Self.GetUserName;
DiffFolder := GetLastModifiedFolderName('C:\Users\' + User +
'\Zomboid\Saves\');
LastModifiedSave := GetLastModifiedFolderName('C:\Users\' + User +
'\Zomboid\Saves\' + DiffFolder + '\');
BackupFolder := 'C:\Users\' + User + '\Zomboid\BackupSaves\';
MakeAStringlistAndSaveThat(True);
GeneratedBackupFolder := LeerArchivox(fn);
if NOT(GeneratedBackupFolder = '') then
GeneratedBackupFolder := StringReplace(GeneratedBackupFolder, #$D#$A, '',
[rfReplaceAll]);
end;
procedure TSaveBackupper.RenameFolder(FolderOld, FolderNew: string);
var
ShellInfo: TSHFileOpStruct;
begin
ShellInfo.Wnd := 0;
ShellInfo.wFunc := FO_RENAME;
ShellInfo.pFrom := PChar(FolderOld);
ShellInfo.pTo := PChar(FolderNew);
ShFileOperation(ShellInfo);
end;
procedure TSaveBackupper.FormShow(Sender: TObject);
begin
if (GeneratedBackupFolder = '') then
begin
lblBackup.Caption := 'Autosaving' + CrLf +
'you cannot replace your save, yet.';
lblBackup.left := 46;
lblBackup.Top := 53;
btnDo.Enabled := False;
btnDo.Visible := False;
end
else
begin
lblBackup.Caption := 'Do you wish to replace your save?';
lblBackup.left := 45;
lblBackup.Top := 29;
btnDo.Enabled := True;
end;
TimerInterval := (tmrAutoBackup.Interval div 1000);
edtAutoSave.Text := IntToStr(TimerInterval);
CountDown := TimerInterval;
tmrAutoBackup.Enabled := True;
if (tmrAutoBackup.Enabled) then
btnTimerBool.Caption := 'Disable autosaving'
else if (NOT tmrAutoBackup.Enabled) then
btnTimerBool.Caption := 'Enable autosaving';
end;
procedure TSaveBackupper.tmrAutoBackupTimer(Sender: TObject);
begin
tmrRefresh.Enabled := False;
tmrAutoBackup.Enabled := False;
CountDown := 0;
lblCountDown.Caption := IntToStr(CountDown);
lblBackup.Caption := 'Do you wish to replace your save?';
lblBackup.left := 45;
lblBackup.Top := 29;
btnDo.Enabled := True;
btnDo.Visible := True;
DiffFolder := GetLastModifiedFolderName('C:\Users\' + User +
'\Zomboid\Saves\');
LastModifiedSave := GetLastModifiedFolderName('C:\Users\' + User +
'\Zomboid\Saves\' + DiffFolder + '\');
SaveFolder := 'C:\Users\' + User + '\Zomboid\Saves\' + DiffFolder + '\' +
LastModifiedSave;
DateTimeToString(GeneratedBackupFolder, 'dd-mm-yy_hh-nn-ss', Now);
GeneratedBackupFolder := GeneratedBackupFolder + '-LASTSAVE-' + LastModifiedSave;
CreateDir(BackupFolder + GeneratedBackupFolder);
CopyDir(SaveFolder, BackupFolder + GeneratedBackupFolder);
MakeAStringlistAndSaveThat(False);
tmrAutoBackup.Enabled := True;
tmrRefresh.Enabled := True;
end;
procedure TSaveBackupper.tmrRefreshTimer(Sender: TObject);
begin
if tmrAutoBackup.Enabled then
dec(CountDown, 1)
else if NOT tmrAutoBackup.Enabled then
CountDown := TimerInterval;
if CountDown <= 0 then
CountDown := TimerInterval;
lblCountDown.Caption := IntToStr(CountDown);
end;
procedure TSaveBackupper.MakeAStringlistAndSaveThat(CheckForFileOnly: Boolean);
var
MyText: TStringlist;
dir: String;
begin
dir := GetCurrentDir;
MyText := TStringlist.create;
try
if NOT FileExists(fn) then
FileCreate(fn);
if CheckForFileOnly then
exit;
if (GeneratedBackupFolder = '') then
exit;
MyText.Add(GeneratedBackupFolder);
MyText.SaveToFile(GetCurrentDir + '\' + fn);
finally
MyText.Free
end;
end;
function TSaveBackupper.FileIsEmpty(const FileName: String): Boolean;
var
fad: TWin32FileAttributeData;
begin
Result := GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad)
and (fad.nFileSizeLow = 0) and (fad.nFileSizeHigh = 0);
end;
function TSaveBackupper.LeerArchivox(const FileName: TFileName): String;
var
List: TStringlist;
begin
Result := '';
if (FileExists(FileName)) then
begin
List := TStringlist.create;
List.Loadfromfile(FileName);
Result := List.Text;
List.Free;
end;
end;
procedure TSaveBackupper.btnDoClick(Sender: TObject);
begin
tmrAutoBackup.Enabled := False;
if (NOT FileExists(fn)) then
FileCreate(fn);
if (GeneratedBackupFolder = '') AND (FileExists(fn)) AND (FileIsEmpty(fn))
then
exit
else if (GeneratedBackupFolder = '') AND (FileExists(fn)) AND
(NOT FileIsEmpty(fn)) then
GeneratedBackupFolder := LeerArchivox(fn);
CopyDir('C:\Users\' + User + '\Zomboid\Saves\' +
DiffFolder + '\' + LastModifiedSave , 'C:\Users\' + User + '\Zomboid\BackupSaves\' +
LastModifiedSave + '-BackupUsed-' + GenerateRandomString(8, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'));
Remove('C:\Users\' + User + '\Zomboid\Saves\' +
DiffFolder + '\' + LastModifiedSave);
if NOT(GeneratedBackupFolder = '') then
begin
tmrAutoBackup.Enabled := False;
CopyDir('C:\Users\' + User + '\Zomboid\BackupSaves\' + GeneratedBackupFolder
+ '\' + LastModifiedSave + '\', 'C:\Users\' + User + '\Zomboid\Saves\' +
DiffFolder + '\');
tmrAutoBackup.Enabled := True;
end;
end;
procedure TSaveBackupper.Remove(const Dir: string);
var
Result: TSearchRec;
begin
if FindFirst(Dir + '\*', faAnyFile, Result) = 0 then
begin
Try
repeat
if (Result.Attr and faDirectory) = faDirectory then
begin
if (Result.Name <> '.') and (Result.Name <> '..') then
Remove(Dir + '\' + Result.Name)
end
else if not DeleteFile(Dir + '\' + Result.Name) then
RaiseLastOSError;
until FindNext(Result) <> 0;
Finally
FindClose(Result);
End;
end;
if not RemoveDir(Dir) then
RaiseLastOSError;
end;
function TSaveBackupper.GetLastModifiedFolderName(AFolder: String): string;
var
sr: TSearchRec;
aTime: Integer;
begin
Result := '';
aTime := 0;
if FindFirst(IncludeTrailingPathDelimiter(AFolder) + '*', faDirectory, sr) = 0
then
begin
repeat
if (sr.Attr and faDirectory) = faDirectory then
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if sr.Time > aTime then
begin
aTime := sr.Time;
Result := sr.Name;
end;
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end
else
begin
Result := '-1';
end;
end;
function TSaveBackupper.GenerateRandomString(const ALength: Integer;
const ACharSequence
: String =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'): String;
var
Ch, SequenceLength: Integer;
begin
SequenceLength := Length(ACharSequence);
SetLength(Result, ALength);
Randomize;
for Ch := Low(Result) to High(Result) do
Result[Ch] := ACharSequence.Chars[Random(SequenceLength)];
end;
end.