-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
140 lines (126 loc) · 5.31 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
namespace map2idc
{
class Program
{
static void Main(string[] args)
{
string mapFile = args.Length >= 1 ? args[0] : @"D:\Dolphin\windwaker\GZLE01.map";
string[] lines = File.ReadAllLines(mapFile);
var sections = SplitSections(lines);
string idcFile = args.Length >= 2 ? args[1] : Path.ChangeExtension(mapFile, ".idc");
if (File.Exists(idcFile))
File.Delete(idcFile);
using (var sw = new StreamWriter(File.OpenWrite(idcFile)))
{
WriteMain(sw);
WriteSegments(sw, sections);
WriteFunctions(sw, sections);
}
}
private static void WriteMain(StreamWriter outFile)
{
outFile.WriteLine("static main()");
outFile.WriteLine("{");
//write functions
outFile.WriteLine("\thandleSegments();");
outFile.WriteLine("\thandleFunctions();");
outFile.WriteLine("}");
}
private static void WriteSegments(StreamWriter outFile, IDictionary<string, string[]> sections)
{
outFile.WriteLine("static handleSegments()");
outFile.WriteLine("{");
//rename segments
string[] segmentLines;
if (sections.TryGetValue("Memory map:", out segmentLines))
{
var segments = new List<string>();
foreach (string line in segmentLines)
{
var match = Regex.Match(line, @"\s+(?<name>[^ ]+)\s+(?<start>[a-f0-9]+)\s+(?<size>[a-f0-9]+)\s+(?<offset>[a-f0-9]+)", RegexOptions.Compiled);
if (match.Success)
{
outFile.WriteLine("\tRenameSeg(0x{0}, \"{1}\");", match.Groups["start"].Value, match.Groups["name"].Value);
segments.Add(match.Groups["name"].Value.Trim());
}
}
sections["segments"] = segments.ToArray();
}
outFile.WriteLine("}");
}
private static void WriteFunctions(StreamWriter outFile, IDictionary<string, string[]> sections)
{
outFile.WriteLine("static handleFunctions()");
outFile.WriteLine("{");
//rename functions
string[] segments;
if (sections.TryGetValue("segments", out segments))
{
foreach (string segment in segments)
{
string[] segmentLines;
if (sections.TryGetValue(string.Format("{0} section layout", segment), out segmentLines))
{
foreach (string line in segmentLines)
{
var match = Regex.Match(line, @"\s+(?<start>[a-f0-9]+)\s+(?<size>[a-f0-9]+)\s+(?<address>[a-f0-9]+)\s+(?<type>\d+)\s+(?<name>[^ ]+)\s*(?<module>.+)?", RegexOptions.Compiled);
if (match.Success)
{
//type 1 == segment?
//type 4 == symbol?
if (match.Groups["type"].Value == "4")
{
string name = match.Groups["name"].Value.Trim();
int flags = 0x101;
if (name[0] == '@')
flags += 0x04 + 0x20;
else
flags += 0x02 + 0x40;
outFile.WriteLine("\tMakeNameEx(0x{0}, \"{1}\", 0x{2:x});", match.Groups["address"].Value, name, flags);
if (match.Groups["module"].Success)
outFile.WriteLine("\tMakeComm(0x{0}, \"{1}\");", match.Groups["address"].Value, match.Groups["module"].Value.Trim());
}
}
}
}
}
}
outFile.WriteLine("}");
}
private static IDictionary<string, string[]> SplitSections(string[] lines)
{
var ret = new Dictionary<string, string[]>();
var currentSegmentEntries = new List<string>();
string currentSegment = "";
for (int i = 0; i < lines.Length; i++)
{
string line = lines[i];
if (string.IsNullOrEmpty(line))
{
if (currentSegmentEntries.Any())
{
ret[currentSegment] = currentSegmentEntries.ToArray();
currentSegmentEntries.Clear();
}
}
else if (line[0] != ' ')
{
currentSegment = line.Trim();
}
else
{
currentSegmentEntries.Add(line);
}
}
if (currentSegmentEntries.Any())
{
ret[currentSegment] = currentSegmentEntries.ToArray();
}
return ret;
}
}
}