1- using System . Text . Json . Nodes ;
1+ using System . Text . Json ;
2+ using System . Text . Json . Nodes ;
23using System . Text . RegularExpressions ;
34
45namespace OpcodeExtractor ;
@@ -7,7 +8,7 @@ public static class OpcodeExtractorVTable
78{
89 private static Regex WhitespaceRegex = new Regex ( "\\ s" ) ;
910
10- internal unsafe static Dictionary < int , string > Extract ( JsonNode opcodeMapData , byte [ ] gameData , bool dumpAllOpcodes )
11+ internal unsafe static Dictionary < int , string > Extract ( JsonNode opcodeMapData , byte [ ] gameData , bool dumpAllOpcodes , string inputMapKey )
1112 {
1213 var signatureData = opcodeMapData [ "signature" ] ! ;
1314 string signature = "" ;
@@ -31,8 +32,8 @@ internal unsafe static Dictionary<int, string> Extract(JsonNode opcodeMapData, b
3132
3233 Dictionary < int , string > indexMap = [ ] ;
3334
34- var mapData = opcodeMapData [ "map" ] ! ;
35- if ( mapData . GetValueKind ( ) != System . Text . Json . JsonValueKind . Object )
35+ var mapData = opcodeMapData [ inputMapKey ] ! ;
36+ if ( mapData == null || mapData . GetValueKind ( ) != System . Text . Json . JsonValueKind . Object )
3637 {
3738 Console . Error . WriteLine ( "Invalid data type for \" map\" in opcodes file" ) ;
3839 return [ ] ;
@@ -44,6 +45,12 @@ internal unsafe static Dictionary<int, string> Extract(JsonNode opcodeMapData, b
4445 indexMap [ entryIndex ] = entry . Key ;
4546 }
4647
48+ if ( ! ReadJSONInt ( opcodeMapData , "switchTableOffset_offset" , out var switchTableOffset_offset ) ) return [ ] ;
49+ if ( ! ReadJSONInt ( opcodeMapData , "switchTableCount_offset" , out var switchTableCount_offset ) ) return [ ] ;
50+ if ( ! ReadJSONInt ( opcodeMapData , "defaultCaseAddr_offset" , out var defaultCaseAddr_offset ) ) return [ ] ;
51+ if ( ! ReadJSONInt ( opcodeMapData , "imageBaseOffset_offset" , out var imageBaseOffset_offset ) ) return [ ] ;
52+ if ( ! ReadJSONInt ( opcodeMapData , "switchTableDataOffset_offset" , out var switchTableDataOffset_offset ) ) return [ ] ;
53+
4754 Console . WriteLine ( $ "Scanning for opcode maps for { indexMap . Count } opcodes, dumping all: { dumpAllOpcodes } ") ;
4855
4956 var offset = matches [ 0 ] ;
@@ -54,11 +61,11 @@ internal unsafe static Dictionary<int, string> Extract(JsonNode opcodeMapData, b
5461 {
5562 byte * funcPtr = ptr + offset ;
5663
57- var switchTableOffset = * ( sbyte * ) ( funcPtr + 15 ) ;
58- var switchTableCount = * ( int * ) ( funcPtr + 17 ) ;
59- var defaultCaseAddr = offset + 23 + Common . ExtractRIPOffsetFromPtr ( funcPtr + 23 ) ;
60- var imageBaseOffset = offset + 30 + Common . ExtractRIPOffsetFromPtr ( funcPtr + 30 ) ;
61- var switchTableDataOffset = * ( int * ) ( funcPtr + 40 ) ;
64+ var switchTableOffset = * ( sbyte * ) ( funcPtr + switchTableOffset_offset ) ;
65+ var switchTableCount = * ( int * ) ( funcPtr + switchTableCount_offset ) ;
66+ var defaultCaseAddr = offset + defaultCaseAddr_offset + Common . ExtractRIPOffsetFromPtr ( funcPtr + defaultCaseAddr_offset ) ;
67+ var imageBaseOffset = offset + imageBaseOffset_offset + Common . ExtractRIPOffsetFromPtr ( funcPtr + imageBaseOffset_offset ) ;
68+ var switchTableDataOffset = * ( int * ) ( funcPtr + switchTableDataOffset_offset ) ;
6269 var switchTableDataPtr = ( int * ) ( ptr + imageBaseOffset + switchTableDataOffset ) ;
6370
6471 for ( int i = 0 ; i <= switchTableCount ; ++ i )
@@ -90,6 +97,20 @@ internal unsafe static Dictionary<int, string> Extract(JsonNode opcodeMapData, b
9097 return opcodeMap ;
9198 }
9299
100+ private static bool ReadJSONInt ( JsonNode opcodeMapData , string key , out int value )
101+ {
102+ var mapData = opcodeMapData [ key ] ! ;
103+ if ( mapData . GetValueKind ( ) != System . Text . Json . JsonValueKind . Number )
104+ {
105+ Console . Error . WriteLine ( $ "Invalid data type for \" ${ key } \" in opcodes file") ;
106+ value = 0 ;
107+ return false ;
108+ }
109+
110+ value = mapData . GetValue < int > ( ) ;
111+ return true ;
112+ }
113+
93114 private static unsafe int GetVFTableIndex ( byte * caseBodyPtr )
94115 {
95116 int index ;
0 commit comments