-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfig.cs
More file actions
215 lines (187 loc) · 6.53 KB
/
Config.cs
File metadata and controls
215 lines (187 loc) · 6.53 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.Json;
namespace X4LogWatcher
{
public class Config : INotifyPropertyChanged
{
private static readonly int MaxRecentProfiles = 5;
private bool _isSaving; // Flag to prevent recursive saves
// Backing fields for properties
private ObservableCollection<string> _recentProfiles = new ObservableCollection<string>();
private string? _activeProfile;
private string? _lastLogFolderPath;
private string _logFileExtension = ".log";
private bool _skipSignatureErrors = true;
private bool _realTimeStamping;
private JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions { WriteIndented = true };
// Properties with notification
public ObservableCollection<string> RecentProfiles
{
get => _recentProfiles;
set => SetProperty(ref _recentProfiles, value);
}
public string? ActiveProfile
{
get => _activeProfile;
set => SetProperty(ref _activeProfile, value);
}
public string? LastLogFolderPath
{
get => _lastLogFolderPath;
set => SetProperty(ref _lastLogFolderPath, value);
}
public string LogFileExtension
{
get => _logFileExtension;
set => SetProperty(ref _logFileExtension, value);
}
public bool SkipSignatureErrors
{
get => _skipSignatureErrors;
set => SetProperty(ref _skipSignatureErrors, value);
}
public bool RealTimeStamping
{
get => _realTimeStamping;
set => SetProperty(ref _realTimeStamping, value);
}
// Constructor to initialize the ObservableCollection
public Config()
{
_recentProfiles = new ObservableCollection<string>();
// Subscribe to collection changes to auto-save when the collection is modified
_recentProfiles.CollectionChanged += (s, e) => AutoSave();
}
// Save configuration to a file
public static void SaveConfig(Config config)
{
try
{
string executablePath = AppDomain.CurrentDomain.BaseDirectory;
string executableName = Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
string configPath = Path.Combine(executablePath, $"{executableName}.cfg");
// Convert ObservableCollection to List for serialization
var configToSave = new
{
RecentProfiles = new List<string>(config.RecentProfiles),
config.ActiveProfile,
config.LastLogFolderPath,
config.LogFileExtension,
config.SkipSignatureErrors,
config.RealTimeStamping,
};
// Serialize the config to JSON and write to file
string jsonConfig = JsonSerializer.Serialize(configToSave, config._jsonSerializerOptions);
File.WriteAllText(configPath, jsonConfig);
}
catch (Exception ex)
{
Console.WriteLine($"Error saving configuration: {ex.Message}");
}
}
// Load configuration from file
public static Config LoadConfig()
{
try
{
string executablePath = AppDomain.CurrentDomain.BaseDirectory;
string executableName = Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
string configPath = Path.Combine(executablePath, $"{executableName}.cfg");
if (File.Exists(configPath))
{
string jsonConfig = File.ReadAllText(configPath);
var tempConfig = JsonSerializer.Deserialize<Config>(jsonConfig);
if (tempConfig != null)
{
var config = new Config();
// Copy values from deserialized config
if (tempConfig.RecentProfiles != null)
{
foreach (var profile in tempConfig.RecentProfiles)
{
config.RecentProfiles.Add(profile);
}
}
config.ActiveProfile = tempConfig.ActiveProfile;
config.LastLogFolderPath = tempConfig.LastLogFolderPath;
config.LogFileExtension = tempConfig.LogFileExtension ?? ".log";
if (tempConfig.GetType().GetProperty(nameof(SkipSignatureErrors)) != null)
{
config.SkipSignatureErrors = tempConfig.SkipSignatureErrors;
}
if (tempConfig.GetType().GetProperty(nameof(RealTimeStamping)) != null)
{
config.RealTimeStamping = tempConfig.RealTimeStamping;
}
return config;
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error loading configuration: {ex.Message}");
}
// Return default config if loading fails
return new Config();
}
// Add a profile to the recent list
public void AddRecentProfile(string profilePath)
{
// Set flag to prevent auto-save during this batch operation
_isSaving = true;
try
{
// Remove the profile if it already exists (Remove returns true if found and removed)
RecentProfiles.Remove(profilePath);
// Add the profile to the beginning of the list
RecentProfiles.Insert(0, profilePath);
// Trim the list if it exceeds the maximum number of recent profiles
while (RecentProfiles.Count > MaxRecentProfiles)
{
RecentProfiles.RemoveAt(RecentProfiles.Count - 1);
}
// Set this as the active profile
ActiveProfile = profilePath;
}
finally
{
// Reset flag and manually save at the end
_isSaving = false;
SaveConfig(this);
}
}
// Auto-save when any property changes
private void AutoSave()
{
if (!_isSaving)
{
SaveConfig(this);
}
}
public event PropertyChangedEventHandler? PropertyChanged;
// Improved property changed notification method with caller member name
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
// Auto-save the configuration when a property changes
AutoSave();
}
// Helper method to set property values and raise change notification
protected bool SetProperty<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value))
{
return false;
}
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
}