11using System . Diagnostics ;
22using System . Runtime . InteropServices ;
33using System . Runtime . Versioning ;
4- using Microsoft . Extensions . Logging ;
54using Microsoft . Win32 . SafeHandles ;
65using PatternFinder ;
76using Sims1LegacyHacks . Utilities ;
87using Windows . Win32 ;
98using Windows . Win32 . System . Memory ;
9+ using ILogger = Serilog . ILogger ;
1010
1111namespace 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}\n Win32 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+ }
0 commit comments