-
Notifications
You must be signed in to change notification settings - Fork 8
/
SaveWatcher.cs
85 lines (77 loc) · 3.02 KB
/
SaveWatcher.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ValheimSaveShield
{
public class SaveWatcher : IDisposable
{
public FileSystemWatcher WorldWatcher { get; set; }
public FileSystemWatcher CharacterWatcher { get; set; }
public string SavePath { get; set; }
public event EventHandler<SaveWatcherLogMessageEventArgs> LogMessage;
public SaveWatcher(string path) : this(path, null) { }
public SaveWatcher(string path, EventHandler<SaveWatcherLogMessageEventArgs> logEventHandler)
{
if (logEventHandler != null)
{
LogMessage += logEventHandler;
}
SavePath = path;
WorldWatcher = new FileSystemWatcher();
if (!Directory.Exists($@"{path}\worlds_local"))
{
Directory.CreateDirectory($@"{path}\worlds_local");
}
WorldWatcher.Path = $@"{path}\worlds_local";
// Watch for changes in LastWrite times.
WorldWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.FileName;
// Only watch .db files.
WorldWatcher.Filter = "*.db";
CharacterWatcher = new FileSystemWatcher();
if (!Directory.Exists($@"{path}\characters_local"))
{
Directory.CreateDirectory($@"{path}\characters_local");
}
CharacterWatcher.Path = $@"{path}\characters_local";
// Watch for changes in LastWrite and file creation times.
CharacterWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.FileName;
// Only watch .fch files.
CharacterWatcher.Filter = "*.fch";
}
private void logMessage(string message)
{
logMessage(message, LogType.Normal);
}
private void logMessage(string message, LogType logtype)
{
OnLogMessage(new SaveWatcherLogMessageEventArgs(this, message, logtype));
}
private void OnLogMessage(SaveWatcherLogMessageEventArgs args)
{
EventHandler<SaveWatcherLogMessageEventArgs> handler = LogMessage;
if (null != handler) handler(this, args);
}
public void Dispose()
{
WorldWatcher.Dispose();
CharacterWatcher.Dispose();
}
}
public class SaveWatcherLogMessageEventArgs : LogMessageEventArgs
{
private readonly SaveWatcher _savewatcher;
public SaveWatcherLogMessageEventArgs(SaveWatcher watcher, string message, LogType logtype) : base(message, logtype)
{
_savewatcher = watcher;
}
public SaveWatcherLogMessageEventArgs(SaveWatcher watcher, string message) : this(watcher, message, LogType.Normal) { }
public SaveWatcher SaveWatcher
{
get { return _savewatcher; }
}
}
}