-
Notifications
You must be signed in to change notification settings - Fork 8
/
SaveFile.cs
200 lines (187 loc) · 5.88 KB
/
SaveFile.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Collections;
namespace ValheimSaveShield
{
class SaveFile
{
private string filePath;
public SaveFile(string path)
{
filePath = path;
}
public string Type
{
get
{
if (new FileInfo(this.filePath).Directory.FullName.EndsWith("\\worlds_local"))
{
return "World";
}
else
{
return "Character";
}
}
}
public string Name
{
get
{
var fileName = FileName;
var parts = new ArrayList(FileName.Split('.'));
parts.RemoveAt(parts.Count - 1);
return string.Join(".", parts.ToArray()).Trim();
}
}
public string FileName
{
get
{
return new FileInfo(this.filePath).Name;
}
}
public DateTime SaveTime
{
get
{
return new FileInfo(filePath).LastWriteTime;
}
}
public string FullPath
{
get
{
return filePath;
}
set
{
this.filePath = value;
}
}
public string BackupsPath
{
get
{
return Properties.Settings.Default.BackupFolder + "\\" + this.Type.ToLower() + "s_local\\" + this.Name;
}
}
public string FullBackupPath
{
get
{
return this.BackupFolder + "\\" + this.FileName;
}
}
public string BackupFolder
{
get
{
return this.BackupsPath + "\\" + new FileInfo(filePath).LastWriteTime.Ticks;
}
}
public bool BackedUp
{
get
{
return File.Exists(this.FullBackupPath);
}
}
public bool NeedsBackedUp
{
get
{
//DateTime newBackupTime = this.BackupDueTime;
if (DateTime.Compare(SaveTime, this.BackupDueTime) >= 0)
{
return true;
}
return false;
}
}
public DateTime BackupDueTime
{
get
{
if (!Directory.Exists(this.BackupsPath))
{
Directory.CreateDirectory(this.BackupsPath);
}
string[] backups = Directory.GetDirectories(this.BackupsPath);
SaveBackup latestBackup = null;
foreach (string bdir in backups)
{
SaveBackup backup = new SaveBackup(bdir + "\\" + this.FileName);
if (latestBackup == null || backup.SaveDate.Ticks > latestBackup.SaveDate.Ticks)
{
latestBackup = backup;
}
}
DateTime latestBackupTime = new DateTime();
if (latestBackup != null)
{
latestBackupTime = latestBackup.SaveDate;
}
//Debug.WriteLine($"Latest backup for {this.FullPath} is {latestBackup.SaveDate}");
return latestBackupTime.AddMinutes(Properties.Settings.Default.BackupMinutes);
}
}
public SaveBackup PerformBackup()
{
int copyAttempts = 0;
try
{
string backupFolder = $@"{Properties.Settings.Default.BackupFolder}\{this.Type.ToLower()}s_local\{this.Name}\{File.GetLastWriteTime(this.FullPath).Ticks}";
if (!Directory.Exists(backupFolder))
{
Directory.CreateDirectory(backupFolder);
}
File.Copy(this.FullPath, this.FullBackupPath, true);
if (this.Type.Equals("World"))
{
FileInfo info = new FileInfo(this.FullPath);
string sourcefwl = info.DirectoryName + "\\" + this.Name + ".fwl";
string destfwl = this.BackupFolder + "\\" + this.Name + ".fwl";
File.Copy(sourcefwl, destfwl, true);
foreach (var ext in Properties.Settings.Default.WorldFileExtensions)
{
string sourcefile = info.DirectoryName + "\\" + this.Name + ext;
if (File.Exists(sourcefile))
{
string destfile = this.BackupFolder + "\\" + this.Name + ext;
File.Copy(sourcefile, destfile, true);
}
}
}
return new SaveBackup(this.FullBackupPath);
}
catch (IOException ex)
{
if (ex.Message.Contains("being used by another process"))
{
copyAttempts++;
if (copyAttempts < 5)
{
//logMessage("Save file in use; waiting 0.5 seconds and retrying.");
System.Threading.Thread.Sleep(500*copyAttempts);
return this.PerformBackup();
}
else
{
throw new Exception("Save file in use; multiple attempts to copy failed.");
}
}
}
catch (Exception ex)
{
throw ex;
}
return null;
}
}
}