Skip to content

Commit 0cb5a6f

Browse files
committed
Switch logging framework to Serilog
1 parent a7f1329 commit 0cb5a6f

5 files changed

Lines changed: 139 additions & 143 deletions

File tree

Sims1LegacyHacks/Hacks/DebugCheats.cs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Diagnostics;
22
using System.Runtime.InteropServices;
33
using System.Runtime.Versioning;
4-
using Microsoft.Extensions.Logging;
54
using Microsoft.Win32.SafeHandles;
65
using PatternFinder;
76
using Sims1LegacyHacks.Utilities;
87
using Windows.Win32;
98
using Windows.Win32.System.Memory;
9+
using ILogger = Serilog.ILogger;
1010

1111
namespace Sims1LegacyHacks.Hacks;
1212

@@ -17,7 +17,7 @@ public class DebugCheatsSettings
1717
}
1818

1919
[SupportedOSPlatform("windows5.1.2600")]
20-
public partial class DebugCheats : IHack
20+
public class DebugCheats : IHack
2121
{
2222
private readonly ILogger _logger;
2323
private readonly DebugCheatsSettings _settings;
@@ -26,7 +26,7 @@ public partial class DebugCheats : IHack
2626

2727
public DebugCheats(ILogger logger, SimsProcess simsProcess, DebugCheatsSettings settings)
2828
{
29-
_logger = logger;
29+
_logger = logger.ForContext<DebugCheats>();
3030
_settings = settings;
3131
SetupSubscriptions(simsProcess);
3232
}
@@ -35,7 +35,7 @@ private void SetupSubscriptions(SimsProcess simsProcess)
3535
{
3636
if (_settings.Enabled)
3737
{
38-
LogEnabled(_logger);
38+
_logger.Information("Debug cheats are enabled");
3939
simsProcess.SimsHooked.Subscribe(evt =>
4040
{
4141
_simsHandle = evt.SimsHandle;
@@ -49,10 +49,12 @@ private unsafe void ReadAndPatchMemory()
4949
{
5050
if (_simsProcess is not null)
5151
{
52-
LogSearchingMemoryForDebugCheats(_logger);
52+
_logger.Information("Searching Sims memory for debug cheats bytes");
5353
var addr = _simsProcess.MainModule!.BaseAddress;
5454
var maxSize = _simsProcess.VirtualMemorySize64;
55+
_logger.Information("Addr: {Addr}, MaxSize: {MaxSize}", addr, maxSize);
5556
var buff = new byte[4096];
57+
5658
fixed (byte* pBuff = buff)
5759
{
5860
while (addr < maxSize)
@@ -73,12 +75,23 @@ private unsafe void ReadAndPatchMemory()
7375
)
7476
{
7577
var err = Marshal.GetLastWin32Error();
76-
LogException(_logger, "ReadProcessMemory failed", err);
77-
return;
78+
_logger.Error(
79+
"ReadProcessMemory failed, handle: {Handle}, address: {Address}, Win32 Error: {Win32Err}",
80+
_simsHandle,
81+
addr,
82+
err
83+
);
84+
throw new DebugCheatsException(
85+
$"ReadProcessMemory failed, handle: {_simsHandle}, address: {addr}, win32Err: {err}"
86+
);
7887
}
7988
if (Pattern.Find(buff, PatternBytes, out var offset))
8089
{
81-
LogFoundOffset(_logger, addr, offset);
90+
_logger.Information(
91+
"Found debug cheats bytes at {Address} + {Offset}",
92+
addr,
93+
offset
94+
);
8295

8396
if (
8497
!PInvoke.VirtualProtectEx(
@@ -91,8 +104,10 @@ out var prevFlags
91104
)
92105
{
93106
var err = Marshal.GetLastWin32Error();
94-
LogException(_logger, "VirtualProtectEx failed", err);
95-
return;
107+
_logger.Error("VirtualProtectEx failed, Win32 Error: {Win32Err}", err);
108+
throw new DebugCheatsException(
109+
$"VirtualProtectEx failed, win32Err: {err}"
110+
);
96111
}
97112

98113
fixed (int* pPatchBytesBuff = PatchBytes)
@@ -108,8 +123,16 @@ out var prevFlags
108123
)
109124
{
110125
var err = Marshal.GetLastWin32Error();
111-
LogException(_logger, "WriteProcessMemory failed", err);
112-
return;
126+
_logger.Error(
127+
"WriteProcessMemory failed, handle: {SimsHandle}, address: {Address} + {Offset} + 4, Win32 Error: {Win32Err}",
128+
_simsHandle,
129+
addr,
130+
offset,
131+
err
132+
);
133+
throw new DebugCheatsException(
134+
$"WriteProcessMemory failed, handle: {_simsHandle}, address: {addr} + {offset} + 4, win32Err: {err}"
135+
);
113136
}
114137
}
115138

@@ -124,11 +147,17 @@ out prevFlags
124147
)
125148
{
126149
var err = Marshal.GetLastWin32Error();
127-
LogException(_logger, "VirtualProtectEx failed", err);
128-
return;
150+
_logger.Error(
151+
"{Msg} Win32 Error: {Win32Err}",
152+
"VirtualProtectEx failed",
153+
err
154+
);
155+
throw new DebugCheatsException(
156+
$"VirtualProtectEx failed, win32Err: {err}"
157+
);
129158
}
130159

131-
LogSuccess(_logger);
160+
_logger.Information("Debug cheats bytes patched successfully");
132161
if (_settings.PlaySound)
133162
{
134163
SoundPlayer.PlaySound();
@@ -148,24 +177,20 @@ public void Dispose()
148177
_simsProcess?.Dispose();
149178
}
150179

151-
[LoggerMessage(LogLevel.Information, "Debug cheats are enabled")]
152-
public static partial void LogEnabled(ILogger l);
153-
154-
[LoggerMessage(LogLevel.Information, "Searching Sims memory for debug cheats bytes")]
155-
public static partial void LogSearchingMemoryForDebugCheats(ILogger l);
156-
157-
[LoggerMessage(LogLevel.Information, "Found debug cheats bytes at {Address} + {Offset}")]
158-
public static partial void LogFoundOffset(ILogger l, nint address, long offset);
159-
160-
[LoggerMessage(LogLevel.Error, "{Msg}\nWin32 Error: {Win32Err}")]
161-
public static partial void LogException(ILogger l, string msg, int win32Err);
162-
163-
[LoggerMessage(LogLevel.Information, "Debug cheats bytes patched successfully")]
164-
public static partial void LogSuccess(ILogger l);
165-
166180
private const string SearchPattern = "807B4C007543";
167181
private static readonly int[] PatchBytes = [0xEB];
168182
private static readonly PatternFinder.Pattern.Byte[] PatternBytes = Pattern.Transform(
169183
SearchPattern
170184
);
171185
}
186+
187+
public class DebugCheatsException : Exception
188+
{
189+
public DebugCheatsException() { }
190+
191+
public DebugCheatsException(string message)
192+
: base(message) { }
193+
194+
public DebugCheatsException(string message, Exception inner)
195+
: base(message, inner) { }
196+
}

Sims1LegacyHacks/Hacks/_1080pResolutionPatch.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Diagnostics;
2-
using System.Runtime.Versioning;
3-
using Microsoft.Extensions.Logging;
1+
using System.Runtime.Versioning;
42
using Microsoft.Win32.SafeHandles;
3+
using Serilog;
54
using SharpHook.Native;
65
using SharpHook.Reactive;
76
using Sims1LegacyHacks.Utilities;
87
using Windows.Win32;
9-
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
108

119
namespace Sims1LegacyHacks.Hacks;
1210

@@ -48,7 +46,7 @@ private void SetupKeyboardHook()
4846
{
4947
if (_settings.Enabled)
5048
{
51-
LogSetupKeyboardHooks(_logger);
49+
_logger.Information("Registering CTRL+F9 and CTRL+F8");
5250
_hook.KeyReleased.Subscribe(evt =>
5351
{
5452
if (evt.RawEvent.Mask.HasCtrl() && evt.Data.KeyCode == KeyCode.VcF9)
@@ -95,7 +93,11 @@ out _previousHeight
9593
return false;
9694
}
9795

98-
LogInitialResolution(_logger, _previousWidth, _previousHeight);
96+
_logger.Information(
97+
"Initial resolution found: {Width}x{Height}",
98+
_previousWidth,
99+
_previousHeight
100+
);
99101
return true;
100102
}
101103

@@ -112,7 +114,7 @@ private void SetupSubscriptions(SimsProcess simsProcess)
112114
}
113115
catch (Exception ex)
114116
{
115-
LogException(_logger, ex);
117+
_logger.Error(ex, "Error starting up");
116118
throw;
117119
}
118120
});
@@ -139,15 +141,18 @@ private unsafe void Startup(SafeFileHandle simsHandle, nint baseAddress)
139141
}
140142

141143
_foundAddr = BitConverter.ToInt32(buff, 0);
142-
_logger.LogInformation("{FoundAddr}", _foundAddr);
144+
_logger.Information("{FoundAddr}", _foundAddr);
143145

144146
bool foundInitialResolution;
145147
do
146148
{
147149
foundInitialResolution = GetCurrentResolution();
148150
if (!foundInitialResolution)
149151
{
150-
LogInitialResolutionNotSet(_logger, _settings.InitialResolutionSearchTimeout);
152+
_logger.Debug(
153+
"InitialResolution not set, sleeping {Sleep}",
154+
_settings.InitialResolutionSearchTimeout
155+
);
151156
}
152157
Thread.Sleep(_settings.InitialResolutionSearchTimeout);
153158
} while (
@@ -167,6 +172,7 @@ private void Patch()
167172
{
168173
return;
169174
}
175+
_logger.Information("CTRL+F9 pressed, patching");
170176
MemUtils.WritePtrChains(_simsHandle, _foundAddr, WidthOffsetChains, DesiredWidth);
171177
MemUtils.WritePtrChains(_simsHandle, _foundAddr, HeightOffsetChains, DesiredHeight);
172178
MemUtils.WritePtrChains(
@@ -185,7 +191,7 @@ private void Patch()
185191

186192
private void UnPatch()
187193
{
188-
LogUnPatching(_logger);
194+
_logger.Information("CTRL+F8 pressed, un-patching");
189195
if (_simsHandle is null)
190196
{
191197
return;
@@ -257,22 +263,4 @@ public void Dispose()
257263
{
258264
Stop();
259265
}
260-
261-
[LoggerMessage(LogLevel.Debug, "InitialResolution not set, sleeping {Sleep}")]
262-
public static partial void LogInitialResolutionNotSet(ILogger l, int sleep);
263-
264-
[LoggerMessage(LogLevel.Information, "Registering CTRL+F9 and CTRL+F8")]
265-
public static partial void LogSetupKeyboardHooks(ILogger l);
266-
267-
[LoggerMessage(LogLevel.Information, "CTRL+F9 pressed, patching")]
268-
public static partial void LogPatching(ILogger l);
269-
270-
[LoggerMessage(LogLevel.Information, "CTRL+F8 pressed, un-patching")]
271-
public static partial void LogUnPatching(ILogger l);
272-
273-
[LoggerMessage(LogLevel.Information, "Initial resolution found: {Width}x{Height}")]
274-
public static partial void LogInitialResolution(ILogger l, int width, int height);
275-
276-
[LoggerMessage(LogLevel.Critical, "Exception")]
277-
public static partial void LogException(ILogger l, Exception ex);
278266
}

0 commit comments

Comments
 (0)