@@ -210,37 +210,22 @@ fn find_game(gui_state: &mut GuiState)
210210
211211 if let Some ( ( emu, handle) ) = emu_info
212212 {
213- let mut raw_str = [ 0 ; 22 ] ;
214- let base = match emu
215- {
216- game_data:: Emulator :: Bsnes => 0xB151E8 as * const c_void ,
217- game_data:: Emulator :: Mame =>
218- {
219- let name_offset = vec ! [ 0x11B72B48 ] ;
220- let offset = get_mame_offset ( & handle, name_offset) ;
221- ( offset + 0xD8 ) as * const c_void
222- }
223- } ;
224-
225- unsafe
226- {
227- let p_raw_str = raw_str. as_mut_ptr ( ) as * mut _ as * mut c_void ;
228- let mut count = 0 ;
229- ReadProcessMemory ( handle, base, p_raw_str, raw_str. len ( ) - 1 , & mut count) ;
230- }
213+ let mut first_module = HINSTANCE :: default ( ) ;
214+ let mut lpcb_needed = 0 ;
215+ unsafe { K32EnumProcessModules ( handle, & mut first_module, std:: mem:: size_of :: < HINSTANCE > ( ) as u32 , & mut lpcb_needed) ; }
231216
232- let terminator = raw_str. into_iter ( ) . position ( |x| x == 0 ) . unwrap ( ) ;
217+ let mut info = MODULEINFO :: default ( ) ;
218+ unsafe { K32GetModuleInformation ( handle, first_module, & mut info, std:: mem:: size_of :: < MODULEINFO > ( ) as u32 ) ; }
233219
234- let game_name = match std :: str :: from_utf8 ( & raw_str [ 0 .. terminator ] )
220+ if emu == game_data :: Emulator :: Mame
235221 {
236- Ok ( name ) => match emu
222+ if game_data :: Emulator :: get_mame_version ( info . SizeOfImage ) == 0
237223 {
238- game_data:: Emulator :: Bsnes => game_data:: Games :: bsnes_game_name ( name) ,
239- game_data:: Emulator :: Mame => game_data:: Games :: mame_game_name ( name) ,
224+ return ; //unsupported mame version. kinda bootleg way to do this
240225 }
226+ }
241227
242- Err ( e) => panic ! ( "failed to get convert game name to string: {e}" ) ,
243- } ;
228+ let game_name = get_game_name ( & handle, & info, & emu) ;
244229
245230 match game_name
246231 {
@@ -251,7 +236,12 @@ fn find_game(gui_state: &mut GuiState)
251236 gui_state. offset = match emu
252237 {
253238 game_data:: Emulator :: Bsnes => 0xB16D7C ,
254- game_data:: Emulator :: Mame => get_mame_offset ( & handle, game. mame_game_offset ( ) ) ,
239+ game_data:: Emulator :: Mame =>
240+ {
241+ let version = game_data:: Emulator :: get_mame_version ( info. SizeOfImage ) ;
242+ let offset_list = game_data:: Emulator :: mame_game_offset ( version, game) ;
243+ get_mame_offset ( & handle, info. lpBaseOfDll as u64 , offset_list)
244+ }
255245 } ;
256246 }
257247
@@ -263,31 +253,40 @@ fn find_game(gui_state: &mut GuiState)
263253 }
264254}
265255
266- fn get_mame_offset ( handle : & HANDLE , offset_list : Vec < u64 > ) -> u64
256+ fn get_game_name ( handle : & HANDLE , info : & MODULEINFO , emu : & game_data :: Emulator ) -> Option < game_data :: Games >
267257{
268- //sleep because getting the offset while mame is loading the game can fail
269- std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ;
258+ let mut raw_str = [ 0 ; 22 ] ;
270259
271- unsafe
260+ let game_name_offset = match emu
272261 {
273- let mut first_module = HINSTANCE :: default ( ) ;
274- let mut lpcb_needed = 0 ;
275- K32EnumProcessModules ( handle, & mut first_module, std:: mem:: size_of :: < HINSTANCE > ( ) as u32 , & mut lpcb_needed) ;
262+ game_data:: Emulator :: Bsnes => 0xB151E8 as * const c_void ,
263+ game_data:: Emulator :: Mame =>
264+ {
265+ let version = game_data:: Emulator :: get_mame_version ( info. SizeOfImage ) ;
266+ let name_offset = game_data:: Emulator :: get_mame_name_offset ( version) ;
276267
277- let mut info = MODULEINFO :: default ( ) ;
278- K32GetModuleInformation ( handle, first_module, & mut info, std:: mem:: size_of :: < MODULEINFO > ( ) as u32 ) ;
268+ ( info. lpBaseOfDll as u64 + name_offset as u64 ) as * const c_void
269+ }
270+ } ;
271+
272+ unsafe
273+ {
274+ let p_raw_str = raw_str. as_mut_ptr ( ) as * mut _ as * mut c_void ;
275+ let mut count = 0 ;
276+ ReadProcessMemory ( handle, game_name_offset, p_raw_str, raw_str. len ( ) - 1 , & mut count) ;
277+ }
279278
280- let mut address = info . lpBaseOfDll as u64 ;
279+ let terminator = raw_str . into_iter ( ) . position ( |x| x == 0 ) . unwrap ( ) ;
281280
282- for offset in offset_list
281+ match std:: str:: from_utf8 ( & raw_str[ 0 .. terminator] )
282+ {
283+ Ok ( name) => match emu
283284 {
284- let base = ( address + offset) as * const c_void ;
285- let p_address = & mut address as * mut _ as * mut c_void ;
286- let mut count = 0 ;
287- ReadProcessMemory ( handle, base, p_address, 8 , & mut count) ;
285+ game_data:: Emulator :: Bsnes => game_data:: Games :: bsnes_game_name ( name) ,
286+ game_data:: Emulator :: Mame => game_data:: Games :: mame_game_name ( name) ,
288287 }
289288
290- address
289+ Err ( _ ) => None ,
291290 }
292291}
293292
@@ -300,6 +299,26 @@ fn enum_processes() -> ([u32; 384], u32)
300299 ( pid_list, pid_size / 4 )
301300}
302301
302+ fn get_mame_offset ( handle : & HANDLE , dll_base : u64 , offset_list : Vec < u64 > ) -> u64
303+ {
304+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ; //sleep because getting the offset while mame is loading the game can fail
305+
306+ unsafe
307+ {
308+ let mut address = dll_base;
309+
310+ for offset in offset_list
311+ {
312+ let base = ( address + offset) as * const c_void ;
313+ let p_address = & mut address as * mut _ as * mut c_void ;
314+ let mut count = 0 ;
315+ ReadProcessMemory ( handle, base, p_address, 8 , & mut count) ;
316+ }
317+
318+ address
319+ }
320+ }
321+
303322fn update ( gui_state : & mut GuiState )
304323{
305324 if gui_state. memory_read_timer > 0
0 commit comments