mirror of
https://github.com/tiennm99/time-mocker.git
synced 2026-08-07 08:25:16 +00:00
feat: cleanup
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"permissions": {
|
||||
"allow": [
|
||||
"Bash(dotnet build:*)"
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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}");
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user