-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
286 lines (239 loc) · 9.61 KB
/
MainForm.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
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
using System.Diagnostics;
using System.Text.Json;
using System.Windows.Forms;
namespace SyndicateSaveManager;
public partial class MainForm : Form
{
private FileSystemWatcher _fileWatcher;
private string _logFilePath;
private string _jsonSaveFile = "saveStates.json";
private long _mostRecentTimestamp = 0;
public MainForm()
{
InitializeComponent();
var appData = LoadDataFromJson();
PopulateTreeView(appData.SaveStates);
StartLogFileWatcher(appData.Settings.LogFilePath);
}
private void UpdateTreeViewSafe(Action action)
{
if (treeViewSaveStates.InvokeRequired)
{
treeViewSaveStates.Invoke(action);
}
else
{
action();
}
}
private void PopulateTreeView(Dictionary<string, List<SaveState>> saveStates)
{
UpdateTreeViewSafe(() =>
{
treeViewSaveStates.Nodes.Clear();
// Sort dates in descending order
var sortedDates = saveStates.Keys.OrderByDescending(date => DateTime.Parse(date)).ToList();
foreach (var date in sortedDates)
{
TreeNode dateNode = new TreeNode(date);
// Sort save states by timestamp in descending order
var sortedSaveStates = saveStates[date]
.OrderByDescending(ss => ss.Timestamp)
.ToList();
foreach (var saveState in sortedSaveStates)
{
DateTime utcDateTime = DateTimeOffset.FromUnixTimeSeconds(saveState.Timestamp).UtcDateTime;
TreeNode saveNode = new TreeNode(utcDateTime.ToLongTimeString())
{
Tag = saveState
};
dateNode.Nodes.Add(saveNode);
}
treeViewSaveStates.Nodes.Add(dateNode);
}
});
}
private AppData LoadDataFromJson()
{
if (File.Exists(_jsonSaveFile))
{
string json = File.ReadAllText(_jsonSaveFile);
var data = JsonSerializer.Deserialize<AppData>(json);
// Set log file path from settings, or use default if not present
if (string.IsNullOrEmpty(data.Settings?.LogFilePath))
{
data.Settings = new Settings { LogFilePath = GetDefaultLogFilePath() };
}
_logFilePath = data.Settings.LogFilePath;
// Set the most recent timestamp from the save states
if (data.SaveStates != null && data.SaveStates.Count > 0)
{
_mostRecentTimestamp = data.SaveStates
.SelectMany(s => s.Value)
.Max(s => s.Timestamp);
}
return data;
}
// If no file exists, return with default log path and empty save states
return new AppData
{
Settings = new Settings { LogFilePath = GetDefaultLogFilePath() },
SaveStates = new Dictionary<string, List<SaveState>>()
};
}
private string GetDefaultLogFilePath()
{
string userProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
return Path.Combine(userProfile, "AppData", "LocalLow", "VRChat", "VRChat");
}
private void StartLogFileWatcher(string logFilePath)
{
_fileWatcher = new FileSystemWatcher(logFilePath, "*.txt"); // Only watch .txt files
_fileWatcher.Changed += OnLogFileChanged;
_fileWatcher.EnableRaisingEvents = true;
}
private void OnLogFileChanged(object sender, FileSystemEventArgs e)
{
try
{
// Open the file with FileShare.ReadWrite to allow access while another process has the file open
using (FileStream fs = new FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (StreamReader sr = new StreamReader(fs))
{
string content = sr.ReadToEnd();
ProcessLogFileContent(content);
}
}
catch (IOException ex)
{
// Handle any exceptions that may occur, such as file locks or access issues
Debug.WriteLine($"Error reading log file: {ex.Message}");
}
}
private void ProcessLogFileContent(string content)
{
Debug.WriteLine($"ProcessLogFileContent");
// Split the content into lines
var lines = content.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
foreach (var lineFull in lines)
{
string line = lineFull.Trim();
// Check if the line contains "[Syndicate-Save]"
if (line.Contains("[Syndicate-Save]"))
{
Debug.WriteLine($"line: {line}");
try
{
// Example log line format:
// 2024.09.06 09:53:14 Log - [Syndicate-Save] <created_utc_long> <base64_data>
var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
// Extract the date and time
string datePart = $"{parts[0]} {parts[1]}";
DateTime logDateTime = DateTime.ParseExact(datePart, "yyyy.MM.dd HH:mm:ss", null);
DateTimeOffset logTimestampOffset = new DateTimeOffset(logDateTime, TimeSpan.Zero);
long logTimestamp = logTimestampOffset.ToUnixTimeSeconds();
// Extract the Unix timestamp and Base64 save data
long createdTimestamp = long.Parse(parts[5]); // parts[5] is the Unix timestamp of the log
string base64SaveData = parts[6]; // parts[6] is the Base64 data
// Update the most recent log timestamp
if (logTimestamp > _mostRecentTimestamp)
{
Debug.WriteLine($"new");
_mostRecentTimestamp = logTimestamp;
StoreSaveState(logTimestamp, createdTimestamp, base64SaveData);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to process log line: {ex.Message}");
}
}
}
}
private void StoreSaveState(long timestamp, long created, string base64Save)
{
string date = DateTimeOffset.FromUnixTimeSeconds(timestamp).ToString("yyyy-MM-dd");
// Load existing JSON or create new with default log file path
var data = LoadDataFromJson();
// Check if the date already exists
if (!data.SaveStates.ContainsKey(date))
{
data.SaveStates[date] = new List<SaveState>();
}
// Check for duplicate timestamps
if (!data.SaveStates[date].Any(ss => ss.Timestamp == timestamp))
{
data.SaveStates[date].Add(new SaveState { Created = created, Timestamp = timestamp, Base64 = base64Save });
// Create JsonSerializerOptions with indentation
var options = new JsonSerializerOptions
{
WriteIndented = true // Enable indentation
};
// Write back to the JSON file with indentation
File.WriteAllText(_jsonSaveFile, JsonSerializer.Serialize(data, options));
// Update the TreeView
PopulateTreeView(data.SaveStates);
}
else
{
Debug.WriteLine($"Duplicate save state ignored: Timestamp {timestamp}");
}
}
private void TreeViewSaveStates_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Tag is SaveState saveState)
{
Clipboard.SetText(saveState.Base64); // Copy the base64 data to the clipboard
UpdateStatusStrip("Copied to clipboard");
}
}
private void UpdateLogFilePath(string newPath)
{
_logFilePath = newPath;
// Load current data, update settings, and save
var data = LoadDataFromJson();
data.Settings.LogFilePath = _logFilePath;
File.WriteAllText(_jsonSaveFile, JsonSerializer.Serialize(data));
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
var settingsDialog = new SettingsDialog(_logFilePath);
if (settingsDialog.ShowDialog() == DialogResult.OK)
{
_logFilePath = settingsDialog.LogFilePath;
UpdateLogFilePath(_logFilePath);
}
}
private void UpdateStatusStrip(string message)
{
if (statusStrip.InvokeRequired)
{
statusStrip.Invoke(new Action(() => toolStripStatusLabel.Text = message));
}
else
{
toolStripStatusLabel.Text = message;
}
// Clear the message after a few seconds (e.g., 3 seconds)
Task.Delay(3000).ContinueWith(_ => UpdateStatusStrip("Ready"));
}
}
public class AppData
{
public Settings Settings { get; set; }
public Dictionary<string, List<SaveState>> SaveStates { get; set; }
}
public class Settings
{
public string LogFilePath { get; set; }
}
public class SaveState
{
public long Created { get; set; }
public long Timestamp { get; set; }
public string Base64 { get; set; }
}