-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.boo
329 lines (297 loc) · 11.6 KB
/
MainForm.boo
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
namespace TURBU.RubyMarshal
import System
import System.Collections.Generic
import System.IO
import System.Linq.Enumerable
import System.Windows.Forms
import Newtonsoft.Json.Linq
import Boo.Lang.Interpreter
partial class MainForm:
private _target as int
private _mapName as string
private _mapFile as string
private _extension as string
private _results = List[of string]()
private _mapData = System.Collections.Generic.Dictionary[of int, string]()
private _filters = System.Collections.Generic.Dictionary[of string, Func[of XPEventCommand, int, bool]]()
private _activeFilter as Func[of XPEventCommand, int, bool]
public def constructor():
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent()
filters = GetFilterText()
LoadFilters(filters)
private def GetFilterText() as string:
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), 'RMXP Scanner')
Directory.CreateDirectory(path) unless Directory.Exists(path)
path = Path.Combine(path, 'filters.txt')
if File.Exists(path):
return File.ReadAllText(path)
else:
var result = DefaultFilters.Value
File.WriteAllText(path, result)
return result
private def LoadFilters(text as string):
interpreter = InteractiveInterpreter()
interpreter.References.Add(System.Reflection.Assembly.GetCallingAssembly())
var results = interpreter.Eval(`import TURBU.RubyMarshal.Reader
AddFilter as System.Action[of string, System.Func[of TURBU.RubyMarshal.XPEventCommand, int, bool]] = null`)
System.Diagnostics.Debugger.Break() if results.Errors.Count > 0
addFilter as System.Action[of string, System.Func[of TURBU.RubyMarshal.XPEventCommand, int, bool]] = self.AddFilter
interpreter.SetValue('AddFilter', addFilter)
interpreter.Pipeline.Insert(1, FilterValidator())
results = interpreter.Eval(text)
if results.Errors.Count > 0:
System.Windows.Forms.MessageBox.Show(
results.Errors.ToString(),
'Unable to load filters.txt',
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Error)
private def AddFilter(name as string, handler as Func[of XPEventCommand, int, bool]):
self.comboBox1.Items.Add(name)
self._filters.Add(name, handler)
private def chkReadFileClick(sender as object, e as System.EventArgs):
if chkReadFile.Checked:
button1.Enabled = not string.IsNullOrEmpty(self.txtRMProject.Text)
else:
button1.Enabled = not string.IsNullOrEmpty(comboBox1.Text)
private def BtnRMProjectClick(sender as object, e as System.EventArgs):
if dlgRMLocation.ShowDialog() == DialogResult.OK:
self.txtRMProject.Text = dlgRMLocation.FileName
button1.Enabled = true if chkReadFile.Checked
private def Button1Click(sender as object, e as System.EventArgs):
unless string.IsNullOrEmpty(txtRMProject.Text):
if chkReadFile.Checked:
ReadFile()
else: ScanProject()
private def ReadFile():
marshal = RubyMarshal()
marshal.AddUnique('Table', Table)
marshal.AddUnique('Tone', Tone)
marshal.AddUnique('Color', Tone)
values = marshal.Read(txtRMProject.Text)
var baseFilename = Path.GetFileName(txtRMProject.Text)
vList = values as Boo.Lang.List
if vList is not null:
self.txtOutput.Clear()
list = System.Collections.Generic.List[of string]()
tvwDisplay.Nodes.Clear()
container = JObject()
for i in range(vList.Count):
if vList[i] isa Hash:
var jHash = Hash2JSON(vList[i])
list.Add(jHash.ToString())
container.Add(i.ToString(), jHash)
elif vList[i] isa Boo.Lang.List:
var jList = List2JSON(vList[i])
list.Add(jList.ToString())
container.Add(i.ToString(), jList)
elif vList[i] is not null:
var jt = JToken.FromObject(vList[i])
list.Add(jt.ToString())
container.Add(i.ToString(), jt)
JsonToTree(baseFilename, container)
self.txtOutput.Lines = list.ToArray()
else:
values = Hash2JSON(values)
self.txtOutput.Text = values.ToString()
JsonToTree(baseFilename, values)
private def NewMarshal() as RubyMarshal:
result = RubyMarshal()
result.AddUnique('Table', Table)
result.AddUnique('Tone', Tone)
result.AddUnique('Color', Tone)
return result
private def ScanProject():
_target = self.txtItemID.Value
_results.Clear()
marshal = RubyMarshal()
_extension = Path.GetExtension(txtRMProject.Text)
values = marshal.Read(Path.Combine(Path.GetDirectoryName(txtRMProject.Text), "MapInfos$_extension"))
LoadMapData(values)
for filename in Directory.EnumerateFiles(Path.GetDirectoryName(txtRMProject.Text), "Map*$_extension"):
values = NewMarshal().Read(filename)
_mapFile = Path.GetFileNameWithoutExtension(filename)
id as int
continue unless int.TryParse(_mapFile[3:], id)
_mapName = _mapData[id]
CheckMapForMatch(values)
CheckCommonEventsForMatch(NewMarshal().Read(Path.Combine(Path.GetDirectoryName(txtRMProject.Text), "CommonEvents$_extension")))
self.txtOutput.Lines = _results.ToArray()
private def LoadMapData(data as Hash):
def ExtractMapName(value) as string:
return value if value isa string
hVal = value cast Hash
return hVal["Object"]
_mapData.Clear()
for key, value as Hash in array((item.Key, item.Value) for item in (data["Values"] as Hash)):
assert value["Class"] == "RPG::MapInfo"
props = value["Values"] as Hash
_mapData.Add(key, ExtractMapName(props["@name"]))
private def CheckCommonEventsForMatch(value as Boo.Lang.List):
for obj as Hash in value:
continue if obj is null
assert obj['Class'] == "RPG::CommonEvent"
values as Hash = obj['Values']
id as int = values["@id"]
eventCommands as Boo.Lang.List = values['@list']
validSet = GetValidSet( eventCommands.Cast[of Hash]().Select({h | XPEventCommand(h)}) ).ToArray()
first = true
for valid in validSet:
if first:
_results.Add("$(comboBox1.Text) found at Common Event $id")
first = false
if chkShowAllValues.Checked:
_results.Add(" Values: [$(join(valid.Params, ', '))]")
else: break
private def CheckMapForMatch(value as Hash):
assert value['Class'] == "RPG::Map"
values as Hash = value['Values']
events as Hash = values['@events'] as Hash
for eventID, mapEvent as Hash in array((item.Key, item.Value) for item in (events['Values'] as Hash)):
assert mapEvent['Class'] == "RPG::Event"
eventValues as Hash = mapEvent['Values']
assert eventValues['@id'] == eventID
pages as Boo.Lang.List = eventValues['@pages']
x as int = eventValues['@x']
y as int = eventValues['@y']
for pageID as int, page as Hash in enumerate(pages):
CheckPageForMatch(eventID, pageID, page, x, y)
private def CheckPageConditionsForMatch(eventID as int, pageID as int, value as Hash, x as int, y as int):
values as Hash = value['Values']
conditions as Hash = (values['@condition'] cast Hash)['Values']
var variable = conditions['@variable_id'] cast int
if variable == _target:
var check = conditions['@variable_value'] cast int
_results.Add("Variable >= $check found at \"$_mapName\" ($_mapFile), Event #$eventID ($x, $y), page $(pageID + 1)")
private def CheckPageForMatch(eventID as int, pageID as int, value as Hash, x as int, y as int):
assert value['Class'] == "RPG::Event::Page"
values as Hash = value['Values']
eventCommands as Boo.Lang.List = values['@list']
validSet = GetValidSet( eventCommands.Cast[of Hash]().Select({h | XPEventCommand(h)}) ).ToArray()
first = true
for valid in validSet:
if first:
_results.Add("$(comboBox1.Text) found at \"$_mapName\" ($_mapFile), Event #$eventID ($x, $y), page $(pageID + 1)")
first = false
if chkShowAllValues.Checked:
_results.Add(" Values: [$(join(valid.Params, ', '))]")
else: break
private def GetValidSet(eventCommands as XPEventCommand*):
return eventCommands.Where({c | _activeFilter(c, _target)})
private def List2JSON(values as Boo.Lang.List) as JArray:
result = JArray()
for elem as object in values:
if elem isa Hash:
result.Add(Hash2JSON(elem))
elif elem isa Boo.Lang.List:
result.Add(List2JSON(elem))
else: result.Add(elem)
return result
private def Hash2JSON(value as Hash) as JToken:
return JValue.CreateNull() if value is null
result = JObject()
enumerator = value.GetEnumerator()
while enumerator.MoveNext():
entry = enumerator.Entry
elem as object = entry.Value
if elem isa Hash:
elem = Hash2JSON(elem)
elif elem isa Boo.Lang.List:
elem = List2JSON(elem)
key as object = entry.Key
key = Hash2JSON(key).ToString().Replace('\r\n', '') if key isa Hash
result.Add(key.ToString(), elem)
return result
private def ComboBox1SelectedIndexChanged(sender as object, e as System.EventArgs):
_activeFilter = _filters[comboBox1.Text]
button1.Enabled = true
// The routines JsonToTree and Json2Tree are adapted from code found at
// http://stackoverflow.com/a/29260447/32914
private def JsonToTree(filename as string, obj as JObject):
try:
tvwDisplay.Nodes.Clear()
parent as TreeNode = Json2Tree(obj)
parent.Text = filename
tvwDisplay.Nodes.Add(parent)
except ex as Exception:
MessageBox.Show(ex.Message, 'ERROR')
private def Json2Tree(obj as JObject) as TreeNode:
//create the parent node
//loop through the obj. all token should be pair<key, value>
//change the display Content of the parent
//create the child node
//check if the value is of type obj recall the method
// child.Text = token.Key.ToString();
//create a new JObject using the the Token.value
o as JObject
parent = TreeNode()
for token as KeyValuePair[of string, JToken] in obj:
key as string = token.Key.ToString()
continue if key == 'Type'
child = TreeNode()
if token.Value.Type.ToString() == 'Object':
o = (token.Value cast JObject)
//recall the method
child = Json2Tree(o)
//add the child to the parentNode
parent.Nodes.Add(child)
elif token.Value.Type.ToString() == 'Array':
//if type is of array
ix as int = (-1)
// child.Text = token.Key.ToString();
//loop though the array
for itm in token.Value:
//check if value is an Array of objects
if itm.Type.ToString() == 'Object':
objTN = TreeNode()
//child.Text = token.Key.ToString();
//call back the method
ix += 1
o = (itm cast JObject)
objTN = Json2Tree(o)
objTN.Text = (((token.Key.ToString() + '[') + ix) + ']')
child.Nodes.Add(objTN)
elif itm.Type.ToString() == 'Array':
//parent.Nodes.Add(child);
//regular array string, int, etc
ix += 1
dataArray = TreeNode()
for data in itm:
dataArray.Text = (((token.Key.ToString() + '[') + ix) + ']')
dataArray.Nodes.Add(data.ToString())
child.Nodes.Add(dataArray)
else:
child.Nodes.Add(itm.ToString())
parent.Nodes.Add(child)
else:
//if token.Value is not nested
// child.Text = token.Key.ToString();
//change the value into N/A if value == null or an empty string
if token.Value.ToString() == '':
child.Nodes.Add('N/A')
else:
child.Nodes.Add(token.Value.ToString())
parent.Nodes.Add(child)
child.Text = token.Key.ToString()
return parent
public class XPEventCommand:
[Getter(Params)]
_params = []
[Getter(Code)]
_code as int
def constructor(value as Hash):
assert value["Class"] == "RPG::EventCommand"
values as Hash = value["Values"]
for param in values["@parameters"]:
if param isa Hash:
pHash = param cast Hash
if pHash["Type"] == 'IVAR':
_params.Add(pHash["Object"])
else: _params.Add(pHash)
else: _params.Add(param)
_code = values["@code"]
[STAThread]
public def Main(argv as (string)) as void:
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(false)
Application.Run(MainForm())