-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.bas
375 lines (305 loc) · 12 KB
/
import.bas
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
Attribute VB_Name = "import"
Sub indicate_import_progress(file As String)
Application.ScreenUpdating = True
set_status "Loading... " & file
Application.ScreenUpdating = False
End Sub
Sub clear_sheet(file As String)
On Error Resume Next
Worksheets(file).ListObjects("nice_table").Delete
On Error GoTo 0
End Sub
Function get_file_xml(file As String) As String
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim contents As String
contents = read_file(get_cell_path() & "\" & file)
' Workaround for a REALLY painful MSXML bug(?)
' If an attribute looks like attribute="'text"
' then it MAY! be loaded in as "text" instead of "'text".
' So just replace apostrophes directly following " by a curly quote (U+2018)
' and sanitize it out later.
contents = Replace(contents, """'", """" & ChrW(&H2018))
' For the icing on the cake: MSXML2.DOMDocument.LoadXML only works with UTF-16!
' So just use Load with a temporary file instead of converting the string
Dim temp_name As String
temp_name = FSO.GetTempName()
write_file temp_name, contents
get_file_xml = temp_name
End Function
Sub delete_temp_file(filename As String)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile filename
End Sub
Sub import_simple(file As String)
indicate_import_progress file
Dim XDoc As Object, root As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
Dim temp_doc_filename As String
temp_doc_filename = get_file_xml(file)
success = XDoc.Load(temp_doc_filename)
delete_temp_file temp_doc_filename
If Not success Then
MsgBox "Can't import " & file & "! " & XDoc.parseError.reason, vbExclamation
Exit Sub
End If
Set root = XDoc.DocumentElement
Dim row As Integer
row = 1
Dim schema() As Variant
Dim schema_max As Integer
If file = "strings.xml" Then
If Not IsNull(root.getAttribute("max_local_for")) Then
Worksheets("Controls").Range("B18").value = root.getAttribute("max_local_for")
schema_max = 5
Else
Worksheets("Controls").Range("B18").value = ""
schema_max = 4
End If
ReDim schema(schema_max)
schema(0) = "english"
schema(1) = "translation"
schema(2) = "case"
schema(3) = "explanation"
schema(4) = "max"
If schema_max = 5 Then
schema(5) = "max_local"
End If
ElseIf file = "numbers.xml" Then
schema_max = 4
ReDim schema(schema_max)
schema(0) = "value"
schema(1) = "form"
schema(2) = "english"
schema(3) = "translation"
schema(4) = "translation2"
ElseIf file = "roomnames.xml" Then
schema_max = 4
ReDim schema(schema_max)
schema(0) = "x"
schema(1) = "y"
schema(2) = "english"
schema(3) = "translation"
schema(4) = "explanation"
ElseIf file = "roomnames_special.xml" Then
schema_max = 2
ReDim schema(schema_max)
schema(0) = "english"
schema(1) = "translation"
schema(2) = "explanation"
Else
MsgBox "Can't import " & file & ", schema not handled!", vbExclamation
Exit Sub
End If
col_A = 1
col_Z = schema_max + 1
clear_sheet file
With Worksheets(file)
For i = 0 To schema_max
.Cells(1, i + 1).value = schema(i)
Next i
row = row + 1
For Each subNode In root.ChildNodes
' Format as text, don't guess/convert numbers
.Range(.Cells(row, col_A), .Cells(row, col_Z)).NumberFormat = "@"
' Comments in roomnames_special.xml are kinda special, we want to keep them.
If TypeName(subNode) <> "IXMLDOMComment" Then
For i = 0 To schema_max
attr_name = schema(i)
.Cells(row, i + 1).value = subNode.getAttribute(attr_name)
.Cells(row, i + 1).Errors(xlNumberAsText).Ignore = True
Next i
End If
row = row + 1
Next subNode
Dim table_range
Set table_range = .Range(.Cells(1, col_A), .Cells(row - 1, col_Z))
.ListObjects.Add(xlSrcRange, table_range, , xlYes).name = "nice_table"
End With
End Sub
Sub import_strings_plural(ByRef forms() As Boolean, ByRef forms_example() As Integer)
Dim file As String
file = "strings_plural.xml"
indicate_import_progress file
Dim XDoc As Object, root As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
Dim temp_doc_filename As String
temp_doc_filename = get_file_xml(file)
success = XDoc.Load(temp_doc_filename)
delete_temp_file temp_doc_filename
If Not success Then
MsgBox "Can't import " & file & "! " & XDoc.parseError.reason, vbExclamation
Exit Sub
End If
Set root = XDoc.DocumentElement
num_forms = 0
For f = 0 To 254
If forms(f) Then
num_forms = num_forms + 1
End If
Next f
Dim row As Integer
row = 1
Dim schema_max As Integer
If Not IsNull(root.getAttribute("max_local_for")) Then
schema_max = 6 + num_forms
Else
schema_max = 5 + num_forms
End If
ReDim schema(schema_max) As String
schema(0) = "english_plural"
schema(1) = "english_singular"
schema(2) = "explanation"
schema(3) = "max"
schema(4) = "var"
schema(5) = "expect"
If schema_max = 6 + num_forms Then
schema(6) = "max_local"
sch_ix = 7
Else
sch_ix = 6
End If
For f = 0 To 254
If forms(f) Then
schema(sch_ix) = "form " & f & " (ex: " & forms_example(f) & ")"
sch_ix = sch_ix + 1
End If
Next f
col_A = 1
col_Z = schema_max + 1
clear_sheet file
With Worksheets(file)
For i = 0 To schema_max
.Cells(1, i + 1).value = schema(i)
Next i
row = row + 1
For Each subNode In root.ChildNodes
' <string english_plural= english_singular= explanation= max= var= expect=>
' Format as text, don't guess/convert numbers
.Range(.Cells(row, col_A), .Cells(row, col_Z)).NumberFormat = "@"
If TypeName(subNode) <> "IXMLDOMComment" Then
For i = 0 To schema_max
attr_name = schema(i)
Dim new_value As String
new_value = ""
If attr_name Like "form *" Then
parts = Split(attr_name, " ", 3)
For Each subsubNode In subNode.ChildNodes
' <translation form= translation=>
If TypeName(subsubNode) = "IXMLDOMComment" Then
ElseIf subsubNode.getAttribute("form") = parts(1) Then
new_value = subsubNode.getAttribute("translation")
End If
Next subsubNode
Else
new_value = subNode.getAttribute(attr_name)
End If
.Cells(row, i + 1).value = new_value
.Cells(row, i + 1).Errors(xlNumberAsText).Ignore = True
Next i
End If
row = row + 1
Next subNode
Dim table_range
Set table_range = .Range(.Cells(1, col_A), .Cells(row - 1, col_Z))
.ListObjects.Add(xlSrcRange, table_range, , xlYes).name = "nice_table"
End With
End Sub
Sub import_cutscenes()
Dim file As String
file = "cutscenes.xml"
indicate_import_progress file
Dim XDoc As Object, root As Object
Set XDoc = CreateObject("MSXML2.DOMDocument")
XDoc.async = False: XDoc.validateOnParse = False
Dim temp_doc_filename As String
temp_doc_filename = get_file_xml(file)
success = XDoc.Load(temp_doc_filename)
delete_temp_file temp_doc_filename
If Not success Then
MsgBox "Can't import " & file & "! " & XDoc.parseError.reason, vbExclamation
Exit Sub
End If
Set root = XDoc.DocumentElement
Dim row As Integer
row = 1
Dim schema_max As Integer
schema_max = 13
ReDim schema(schema_max) As String
schema(0) = "id"
schema(1) = "explanation"
schema(2) = "speaker"
schema(3) = "english"
schema(4) = "translation"
schema(5) = "case"
schema(6) = "tt"
schema(7) = "wraplimit"
schema(8) = "centertext"
schema(9) = "pad"
schema(10) = "pad_left"
schema(11) = "pad_right"
schema(12) = "padtowidth"
schema(13) = "buttons"
col_A = 1
col_Z = schema_max + 1
clear_sheet file
With Worksheets(file)
For i = 0 To schema_max
.Cells(1, i + 1).value = schema(i)
Next i
row = row + 1
For Each subNode In root.ChildNodes
' <cutscene id= explanation=>
If TypeName(subNode) <> "IXMLDOMComment" Then
Dim script_id As String, script_explanation As String
script_id = subNode.getAttribute("id")
script_explanation = subNode.getAttribute("explanation")
For Each subsubNode In subNode.ChildNodes
' <dialogue speaker= english= translation= ...>
' Format as text, don't guess/convert numbers
.Range(.Cells(row, col_A), .Cells(row, col_Z)).NumberFormat = "@"
For i = 0 To schema_max
attr_name = schema(i)
If attr_name = "id" Then
.Cells(row, i + 1).value = script_id
ElseIf attr_name = "explanation" Then
.Cells(row, i + 1).value = script_explanation
ElseIf TypeName(subsubNode) <> "IXMLDOMComment" Then
.Cells(row, i + 1).value = subsubNode.getAttribute(attr_name)
End If
.Cells(row, i + 1).Errors(xlNumberAsText).Ignore = True
Next i
If TypeName(subsubNode) <> "IXMLDOMComment" Then
row = row + 1
End If
Next subsubNode
End If
Next subNode
Dim table_range
Set table_range = .Range(.Cells(1, col_A), .Cells(row - 1, col_Z))
.ListObjects.Add(xlSrcRange, table_range, , xlYes).name = "nice_table"
End With
End Sub
Sub get_used_forms(ByRef forms() As Boolean, ByRef forms_example() As Integer)
For f = 0 To 254
forms_example(f) = 0
Next f
Dim row As ListRow
For Each row In Worksheets("numbers.xml").ListObjects("nice_table").ListRows
form_str = ListRow_get(row, "form")
If form_str <> "" Then
Dim form As Integer
form = CInt(form_str)
If form >= 0 And form <= 254 Then
forms(form) = True
If forms_example(form) = 0 Then
' Yes, we don't want 0 as an example unless it's the only possible example
forms_example(form) = ListRow_get(row, "value")
End If
End If
End If
Next row
End Sub