From 5e1e89a55064b840964cbffe7bccaa537699bc18 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Fri, 27 Feb 2026 10:30:21 +0700 Subject: [PATCH] feat: cleanup --- csharp/.claude/settings.local.json | 7 +++++ csharp/TimeMocker.Hook/InjectionEntryPoint.cs | 9 ++---- csharp/TimeMocker.UI/Core/InjectionManager.cs | 29 +++++-------------- csharp/TimeMocker.UI/Core/ProcessWatcher.cs | 2 +- .../TimeMocker.UI/Core/SharedMemoryManager.cs | 1 - csharp/TimeMocker.UI/Forms/MainForm.cs | 4 +-- 6 files changed, 20 insertions(+), 32 deletions(-) create mode 100644 csharp/.claude/settings.local.json diff --git a/csharp/.claude/settings.local.json b/csharp/.claude/settings.local.json new file mode 100644 index 0000000..00fc07d --- /dev/null +++ b/csharp/.claude/settings.local.json @@ -0,0 +1,7 @@ +{ + "permissions": { + "allow": [ + "Bash(dotnet build:*)" + ] + } +} diff --git a/csharp/TimeMocker.Hook/InjectionEntryPoint.cs b/csharp/TimeMocker.Hook/InjectionEntryPoint.cs index b02c283..613f796 100644 --- a/csharp/TimeMocker.Hook/InjectionEntryPoint.cs +++ b/csharp/TimeMocker.Hook/InjectionEntryPoint.cs @@ -15,7 +15,6 @@ namespace TimeMocker.Hook public struct MockTimeInfo { public long DeltaTicks; // Offset to add to DateTime.UtcNow.Ticks (can be negative) - public int Enabled; // 1 = mock active, 0 = passthrough } // ------------------------------------------------------------------------- @@ -108,12 +107,8 @@ namespace TimeMocker.Hook private DateTime GetFakeUtc() { var info = ReadMockInfo(); - if (info.Enabled == 1) - { - // Return real UTC time plus the stored delta offset - return new DateTime(DateTime.UtcNow.Ticks + info.DeltaTicks, DateTimeKind.Utc); - } - return DateTime.UtcNow; + // Return real UTC time plus the stored delta offset + return new DateTime(DateTime.UtcNow.Ticks + info.DeltaTicks, DateTimeKind.Utc); } private void InstallHooks() diff --git a/csharp/TimeMocker.UI/Core/InjectionManager.cs b/csharp/TimeMocker.UI/Core/InjectionManager.cs index 7b4189e..b48b57d 100644 --- a/csharp/TimeMocker.UI/Core/InjectionManager.cs +++ b/csharp/TimeMocker.UI/Core/InjectionManager.cs @@ -43,8 +43,8 @@ namespace TimeMocker.UI.Core try { - // Write disabled state initially so hook passes through - entry.Shm.Write(new MockTimeInfo { Enabled = 0, DeltaTicks = 0 }); + // Write initial delta (zero = real time) before hook starts reading + entry.Shm.Write(new MockTimeInfo { DeltaTicks = 0 }); RemoteHooking.Inject( process.Id, @@ -70,32 +70,20 @@ namespace TimeMocker.UI.Core // ----------------------------------------------------------------------- // Update fake time for a process // ----------------------------------------------------------------------- - public void SetFakeTime(int processId, DateTime fakeUtc, bool enabled) + public void SetFakeTime(int processId, DateTime fakeUtc) { if (!_injected.TryGetValue(processId, out var entry)) return; - long deltaTicks; - if (enabled) - { - // Calculate delta: (desired fake time) - (current real UTC time) - deltaTicks = fakeUtc.Ticks - DateTime.UtcNow.Ticks; - } - else - { - deltaTicks = 0; - } + // Calculate delta: (desired fake time) - (current real UTC time) + long deltaTicks = fakeUtc.Ticks - DateTime.UtcNow.Ticks; - entry.Shm.Write(new MockTimeInfo - { - DeltaTicks = deltaTicks, - Enabled = enabled ? 1 : 0 - }); + entry.Shm.Write(new MockTimeInfo { DeltaTicks = deltaTicks }); } - public void SetFakeTimeAll(DateTime fakeUtc, bool enabled) + public void SetFakeTimeAll(DateTime fakeUtc) { foreach (var pid in _injected.Keys) - SetFakeTime(pid, fakeUtc, enabled); + SetFakeTime(pid, fakeUtc); } public bool IsInjected(int processId) @@ -111,7 +99,6 @@ namespace TimeMocker.UI.Core public void Eject(int processId) { if (!_injected.TryGetValue(processId, out var entry)) return; - entry.Shm.Write(new MockTimeInfo { Enabled = 0, DeltaTicks = 0 }); // disable mock first entry.Shm.Dispose(); _injected.Remove(processId); Log($"Ejected from [{processId}] {entry.ProcessName}"); diff --git a/csharp/TimeMocker.UI/Core/ProcessWatcher.cs b/csharp/TimeMocker.UI/Core/ProcessWatcher.cs index a8cd25e..25aaa61 100644 --- a/csharp/TimeMocker.UI/Core/ProcessWatcher.cs +++ b/csharp/TimeMocker.UI/Core/ProcessWatcher.cs @@ -122,7 +122,7 @@ namespace TimeMocker.UI.Core try { var entry = _injectionMgr.Inject(p); - _injectionMgr.SetFakeTime(p.Id, FakeUtc, MockEnabled); + _injectionMgr.SetFakeTime(p.Id, FakeUtc); Log($"[AutoInject] Matched rule '{rule.Pattern}' → [{p.Id}] {p.ProcessName}"); ProcessAutoInjected?.Invoke(entry); } diff --git a/csharp/TimeMocker.UI/Core/SharedMemoryManager.cs b/csharp/TimeMocker.UI/Core/SharedMemoryManager.cs index b7b0384..22e0c17 100644 --- a/csharp/TimeMocker.UI/Core/SharedMemoryManager.cs +++ b/csharp/TimeMocker.UI/Core/SharedMemoryManager.cs @@ -47,6 +47,5 @@ namespace TimeMocker.UI.Core public struct MockTimeInfo { public long DeltaTicks; // Offset to add to DateTime.UtcNow.Ticks (can be negative) - public int Enabled; // 1 = mock active, 0 = passthrough } } \ No newline at end of file diff --git a/csharp/TimeMocker.UI/Forms/MainForm.cs b/csharp/TimeMocker.UI/Forms/MainForm.cs index 180d2ab..3a0ee8e 100644 --- a/csharp/TimeMocker.UI/Forms/MainForm.cs +++ b/csharp/TimeMocker.UI/Forms/MainForm.cs @@ -359,7 +359,7 @@ namespace TimeMocker.UI.Forms var p = Process.GetProcessById(selected.Value); _injMgr.Inject(p); var dt = GetFakeTime(); - _injMgr.SetFakeTime(p.Id, dt.ToUniversalTime(), enabled: true); + _injMgr.SetFakeTime(p.Id, dt.ToUniversalTime()); RefreshInjectedTab(); AppendLog($"Manually injected into [{p.Id}] {p.ProcessName}"); } @@ -535,7 +535,7 @@ namespace TimeMocker.UI.Forms private void ApplyTime() { var dt = GetFakeTime().ToUniversalTime(); - _injMgr.SetFakeTimeAll(dt, enabled: true); + _injMgr.SetFakeTimeAll(dt); _watcher.FakeUtc = dt; _watcher.MockEnabled = true;