mirror of
https://github.com/tiennm99/time-mocker.git
synced 2026-10-11 03:13:47 +00:00
feat: save and load options
This commit is contained in:
1 parent
78b60c8cea
commit
56cc5d36d0
3 files changed
+74
No files matched your search
@@ -1,4 +1,5 @@
|
|||||||
.idea
|
.idea
|
||||||
|
*.DotSettings.user
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,54 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using TimeMocker.UI.Core;
|
using TimeMocker.UI.Core;
|
||||||
|
|
||||||
namespace TimeMocker.UI.Forms
|
namespace TimeMocker.UI.Forms
|
||||||
{
|
{
|
||||||
|
public class PatternRuleDto
|
||||||
|
{
|
||||||
|
public string Pattern { get; set; }
|
||||||
|
public bool UseRegex { get; set; }
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AppConfig
|
||||||
|
{
|
||||||
|
private static readonly string ConfigPath = Path.Combine(
|
||||||
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||||
|
"TimeMocker",
|
||||||
|
"timemocker-config.json");
|
||||||
|
|
||||||
|
public bool AutoInjectEnabled { get; set; } = true;
|
||||||
|
public bool AutoAdvanceEnabled { get; set; } = true;
|
||||||
|
public List<PatternRuleDto> Patterns { get; set; } = new List<PatternRuleDto>();
|
||||||
|
|
||||||
|
public void Save()
|
||||||
|
{
|
||||||
|
var dir = Path.GetDirectoryName(ConfigPath);
|
||||||
|
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dir);
|
||||||
|
}
|
||||||
|
File.WriteAllText(ConfigPath, JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true }));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AppConfig Load()
|
||||||
|
{
|
||||||
|
return File.Exists(ConfigPath)
|
||||||
|
? JsonSerializer.Deserialize<AppConfig>(File.ReadAllText(ConfigPath)) ?? new AppConfig()
|
||||||
|
: new AppConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public partial class MainForm : Form
|
public partial class MainForm : Form
|
||||||
{
|
{
|
||||||
|
private readonly AppConfig _config;
|
||||||
|
|
||||||
private InjectionManager _injMgr;
|
private InjectionManager _injMgr;
|
||||||
private ProcessWatcher _watcher;
|
private ProcessWatcher _watcher;
|
||||||
|
|
||||||
@@ -50,6 +90,8 @@ namespace TimeMocker.UI.Forms
|
|||||||
|
|
||||||
public MainForm()
|
public MainForm()
|
||||||
{
|
{
|
||||||
|
_config = AppConfig.Load();
|
||||||
|
|
||||||
Text = "TimeMocker – Process Time Injection";
|
Text = "TimeMocker – Process Time Injection";
|
||||||
Size = new Size(900, 680);
|
Size = new Size(900, 680);
|
||||||
MinimumSize = new Size(750, 560);
|
MinimumSize = new Size(750, 560);
|
||||||
@@ -67,6 +109,21 @@ namespace TimeMocker.UI.Forms
|
|||||||
BeginInvoke((Action)(() => RefreshInjectedTab()));
|
BeginInvoke((Action)(() => RefreshInjectedTab()));
|
||||||
|
|
||||||
BuildUI();
|
BuildUI();
|
||||||
|
|
||||||
|
// Load config settings
|
||||||
|
chkWatcherEnabled.Checked = _config.AutoInjectEnabled;
|
||||||
|
chkAutoAdvance.Checked = _config.AutoAdvanceEnabled;
|
||||||
|
foreach (var pattern in _config.Patterns)
|
||||||
|
{
|
||||||
|
_watcher.AddRule(new PatternRule
|
||||||
|
{
|
||||||
|
Pattern = pattern.Pattern,
|
||||||
|
UseRegex = pattern.UseRegex,
|
||||||
|
Enabled = pattern.Enabled
|
||||||
|
});
|
||||||
|
dgvPatterns.Rows.Add(pattern.Pattern, pattern.UseRegex ? "Regex" : "Glob", pattern.Enabled);
|
||||||
|
}
|
||||||
|
|
||||||
RefreshProcessList();
|
RefreshProcessList();
|
||||||
UpdateTimePreview();
|
UpdateTimePreview();
|
||||||
}
|
}
|
||||||
@@ -679,6 +736,21 @@ namespace TimeMocker.UI.Forms
|
|||||||
|
|
||||||
protected override void OnFormClosing(FormClosingEventArgs e)
|
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||||
{
|
{
|
||||||
|
// Save config
|
||||||
|
_config.AutoInjectEnabled = chkWatcherEnabled.Checked;
|
||||||
|
_config.AutoAdvanceEnabled = chkAutoAdvance.Checked;
|
||||||
|
_config.Patterns.Clear();
|
||||||
|
foreach (DataGridViewRow row in dgvPatterns.Rows)
|
||||||
|
{
|
||||||
|
_config.Patterns.Add(new PatternRuleDto
|
||||||
|
{
|
||||||
|
Pattern = row.Cells[0].Value?.ToString() ?? "",
|
||||||
|
UseRegex = row.Cells[1].Value?.ToString() == "Regex",
|
||||||
|
Enabled = (bool)(row.Cells[2].Value ?? true)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
_config.Save();
|
||||||
|
|
||||||
_watcher.Stop();
|
_watcher.Stop();
|
||||||
_injMgr.Dispose();
|
_injMgr.Dispose();
|
||||||
base.OnFormClosing(e);
|
base.OnFormClosing(e);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="EasyHook" Version="2.7.7097.0"/>
|
<PackageReference Include="EasyHook" Version="2.7.7097.0"/>
|
||||||
|
<PackageReference Include="System.Text.Json" Version="8.0.5"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<!-- Ensure the hook DLL is always next to the UI exe -->
|
<!-- Ensure the hook DLL is always next to the UI exe -->
|
||||||
|
|||||||
Reference in new issue
Block a user