Skip to content

Commit 49ea5e6

Browse files
Merge pull request #10 from SuperTavor/master
Custom function dictionaries
2 parents b574a84 + 106dedf commit 49ea5e6

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

FunctionDictionary.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
3+
"sub20": "Call",
4+
"sub30": "Jump1",
5+
"sub31": "Jump2",
6+
"sub33": "JumpParameter",
7+
"sub130": "Compare",
8+
"sub501": "printf"
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Text.RegularExpressions;
6+
7+
namespace XtractQuery.Parsers
8+
{
9+
public class FunctionDictionaryParser
10+
{
11+
public static Dictionary<int, string> ParseDictionary()
12+
{
13+
string filePath = "FunctionDictionary.json";
14+
15+
if (!File.Exists(filePath))
16+
{
17+
Console.WriteLine("File not found: " + filePath);
18+
return new Dictionary<int, string>();
19+
}
20+
21+
string json = File.ReadAllText(filePath);
22+
var jsonData = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
23+
24+
var formattedDictionary = new Dictionary<int, string>();
25+
26+
foreach (var kvp in jsonData)
27+
{
28+
int subNumber = ExtractSubNumber(kvp.Key);
29+
if (subNumber != -1)
30+
{
31+
formattedDictionary[subNumber] = kvp.Value;
32+
}
33+
}
34+
35+
return formattedDictionary;
36+
}
37+
38+
private static int ExtractSubNumber(string key)
39+
{
40+
var match = Regex.Match(key, @"sub(\d+)");
41+
return match.Success && match.Groups.Count > 1
42+
? int.TryParse(match.Groups[1].Value, out int subNumber) ? subNumber : -1
43+
: -1;
44+
}
45+
46+
47+
48+
public static void MergeIntoDictionary(IDictionary<int, string> targetDictionary)
49+
{
50+
var parsedDictionary = ParseDictionary();
51+
52+
foreach (var kvp in parsedDictionary)
53+
{
54+
targetDictionary[kvp.Key] = kvp.Value;
55+
}
56+
}
57+
}
58+
}

XtractQuery/Parsers/Models/Instruction.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public Instruction(int instructionType, IList<Argument> arguments, int returnPar
2727
public string GetString(IStringReader stringReader)
2828
{
2929
var args = string.Join(", ", Arguments.Select(x => x.GetString(InstructionType, stringReader)));
30+
FunctionDictionaryParser.MergeIntoDictionary(RoutineMap);
3031
var subName = RoutineMap.ContainsKey(InstructionType) ? RoutineMap[InstructionType] : $"sub{InstructionType}";
3132
var returnParameter = Parameter.Parse(ReturnParameter);
3233

@@ -109,14 +110,9 @@ private static IEnumerable<string> GetArguments(string argumentBody)
109110
}
110111
}
111112

112-
private static IDictionary<int, string> RoutineMap = new Dictionary<int, string>
113+
public static IDictionary<int, string> RoutineMap = new Dictionary<int, string>
113114
{
114-
[20] = "Call",
115-
[30] = "Jump1",
116-
[31] = "Jump2",
117-
[33] = "JumpParameter",
118-
[130] = "Compare",
119-
[501] = "printf"
115+
120116
};
121117
}
122118
}

XtractQuery/Program.cs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using XtractQuery.Interfaces;
77
using XtractQuery.Options;
88
using XtractQuery.Parsers;
9+
using XtractQuery.Parsers.Models;
910

1011
namespace XtractQuery
1112
{
@@ -132,6 +133,7 @@ private static void ExtractFile(string type, string file)
132133

133134
private static void CreateFile(string type, string file)
134135
{
136+
FunctionDictionaryParser.MergeIntoDictionary(Instruction.RoutineMap);
135137
IParser parser;
136138
switch (type)
137139
{

XtractQuery/XtractQuery.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1516
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0-preview.4.20251.6" />
1617
</ItemGroup>
1718

0 commit comments

Comments
 (0)