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
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#NoTrayIcon
FileEncoding, UTF-8-RAW
#SingleInstance OFF
SetBatchLines, -1
SetFormat, float, 0.99

file_path = %1%
Loop {
	if (file_path == "") {
		default_path := GetDefaultPath()
		FileSelectFile, file_path, 3, %default_path%,  Please select a file of this type .mate, Material File(*.mate)
	}
	if (ErrorLevel == 1) {
		ExitApp
	}
	if (FileExist(file_path) == "") {
		Msgbox, The file does not exist
		file_path := ""
	}
	else {
		break
	}
}
file := FileOpen(file_path, "r")
if (file.Read(file.ReadChar()) != "CM3D2_MATERIAL") {
	MsgBox, This is not a CM3d2 Material
	ExitApp
}

version := file.ReadInt()
value1 := ReadString(file)
value2 := ReadString(file)
value3 := ReadString(file)
value4 := ReadString(file)

tex_data := Object()
col_data := Object()
f_data := Object()
Loop {
	type := ReadString(file)
	if (type == "end") {
		break
	}
	else if (type == "tex") {
		pos := GetMaxIndex(tex_data) + 1
		tex_data[pos, 1] := ReadString(file)
		tex_data[pos, 2] := ReadString(file)
		if (tex_data[pos, 2] == "tex2d") {
			tex_data[pos, 3] := ReadString(file)
			tex_data[pos, 4] := ReadString(file)
			tex_data[pos, 5, 1] := file.ReadFloat()
			tex_data[pos, 5, 2] := file.ReadFloat()
			tex_data[pos, 5, 3] := file.ReadFloat()
			tex_data[pos, 5, 4] := file.ReadFloat()
		}
	}
	else if (type == "col") {
		pos := GetMaxIndex(col_data) + 1
		col_data[pos, 1] := ReadString(file)
		col_data[pos, 2, 1] := file.ReadFloat()
		if (col_data[pos, 2, 1] != "null") {
			col_data[pos, 2, 2] := file.ReadFloat()
			col_data[pos, 2, 3] := file.ReadFloat()
			col_data[pos, 2, 4] := file.ReadFloat()
		}
		else {
			col_data[pos, 2, 2] := ""
			col_data[pos, 2, 3] := ""
			col_data[pos, 2, 4] := ""
		}
	}
	else if (type == "f") {
		pos := GetMaxIndex(f_data) + 1
		f_data[pos, 1] := ReadString(file)
		f_data[pos, 2] := file.ReadFloat()
	}
	else {
		MsgBox, There was unknown data(%type%)`nQuit
		ExitApp
	}
}
file.Close()

SplitPath, file_path, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
Gui, Add, Text, xm0 ym0 W450, %A_Space%%OutFileName%
Gui, Add, Text, x+0 yp+0 W100 H20, %A_Space%%A_Space%File Version
Gui, Add, Edit, x+0 yp+0 W50 H20 VVversion, %version%

Gui, Add, Text, xm0 y+0 W50 H20, %A_Space%%A_Space%Value1
Gui, Add, Edit, x+0 yp+0 W250 H20 VVvalue1, %value1%
Gui, Add, Text, x+0 yp+0 W50 H20, %A_Space%%A_Space%Value2
Gui, Add, Edit, x+0 yp+0 W250 H20 VVvalue2, %value2%

Gui, Add, Text, xm0 y+0 W50 H20, %A_Space%%A_Space%Value3
Gui, Add, Edit, x+0 yp+0 W250 H20 VVvalue3, %value3%
Gui, Add, Text, x+0 yp+0 W50 H20, %A_Space%%A_Space%Value4
Gui, Add, Edit, x+0 yp+0 W250 H20 VVvalue4, %value4%

Gui, Add, Text, xm0 y+5 W50, %A_Space%%A_Space%texture
Gui, Add, TreeView, xm0 y+0 W550 H200 -ReadOnly VVtex_data
size := GetMaxIndex(tex_data)
Loop, %size% {
	parent := TV_Add(tex_data[A_Index, 1], 0, "Expand")
	TV_Add(tex_data[A_Index, 2], parent)
	if (tex_data[A_Index, 2] == "tex2d") {
		TV_Add(tex_data[A_Index, 3], parent)
		TV_Add(tex_data[A_Index, 4], parent)
		vector := TV_Add("Color", parent)
		TV_Add(SetFloat(tex_data[A_Index, 5, 1]), vector)
		TV_Add(SetFloat(tex_data[A_Index, 5, 2]), vector)
		TV_Add(SetFloat(tex_data[A_Index, 5, 3]), vector)
		TV_Add(SetFloat(tex_data[A_Index, 5, 4]), vector)
	}
}
Gui, Add, Button, x+0 yp+0 W50 H100 GtexAdd, +
Gui, Add, Button, xp+0 y+0 W50 H100 GtexDel, ×

Gui, Add, Text, xm0 y+5 W50, %A_Space%%A_Space%color
Gui, Add, TreeView, xm0 y+0 W550 H150 -ReadOnly VVcol_data
size := GetMaxIndex(col_data)
Loop, %size% {
	parent := TV_Add(col_data[A_Index, 1], 0, "Expand")
	vector := TV_Add("Color", parent)
	TV_Add(SetFloat(col_data[A_Index, 2, 1]), vector)
	TV_Add(SetFloat(col_data[A_Index, 2, 2]), vector)
	TV_Add(SetFloat(col_data[A_Index, 2, 3]), vector)
	TV_Add(SetFloat(col_data[A_Index, 2, 4]), vector)
}
Gui, Add, Button, x+0 yp+0 W50 H75 GcolAdd, +
Gui, Add, Button, xp+0 y+0 W50 H75 GcolDel, ×

Gui, Add, Text, xm0 y+5 W50, %A_Space%%A_Space%float
Gui, Add, TreeView, xm0 y+0 W550 H100 -ReadOnly VVf_data
size := GetMaxIndex(f_data)
Loop, %size% {
	parent := TV_Add(f_data[A_Index, 1], 0, "Expand")
	TV_Add(SetFloat(f_data[A_Index, 2]), parent)
}
Gui, Add, Button, x+0 yp+0 W50 H50 GfAdd, +
Gui, Add, Button, xp+0 y+0 W50 H50 GfDel, ×

Gui, Add, Button, xm0 y+5 W600 H50 GMySubmit, Save

Gui, Show, AutoSize
return



MySubmit:
	Gui, Submit, NoHide
	
	file := FileOpen(file_path, "w")
	
	WriteString(file, "CM3D2_MATERIAL")
	file.WriteInt(Vversion)
	WriteString(file, Vvalue1)
	WriteString(file, Vvalue2)
	WriteString(file, Vvalue3)
	WriteString(file, Vvalue4)
	
	Gui, TreeView, Vtex_data
	parent_id = 0
	Loop {
		parent_id := TV_GetNext(parent_id)
		if (parent_id == 0) {
			break
		}
		WriteString(file, "tex")
		
		TV_GetText(string, parent_id)
		WriteString(file, string)
		
		child_id := TV_GetChild(parent_id)
		TV_GetText(string, child_id)
		WriteString(file, string)
		if (string == "null") {
			continue
		}
		
		child_id := TV_GetNext(child_id)
		TV_GetText(string, child_id)
		WriteString(file, string)
		
		child_id := TV_GetNext(child_id)
		TV_GetText(string, child_id)
		WriteString(file, string)
		
		child_id := TV_GetNext(child_id)
		child_id := TV_GetChild(child_id)
		TV_GetText(string, child_id)
		file.WriteFloat(string)
		Loop, 3 {
			child_id := TV_GetNext(child_id)
			TV_GetText(string, child_id)
			file.WriteFloat(string)
		}
	}
	
	Gui, TreeView, Vcol_data
	parent_id = 0
	Loop {
		parent_id := TV_GetNext(parent_id)
		if (parent_id == 0) {
			break
		}
		WriteString(file, "col")
		
		TV_GetText(string, parent_id)
		WriteString(file, string)
		
		child_id := TV_GetChild(parent_id)
		child_id := TV_GetChild(child_id)
		TV_GetText(string, child_id)
		file.WriteFloat(string)
		Loop, 3 {
			child_id := TV_GetNext(child_id)
			TV_GetText(string, child_id)
			file.WriteFloat(string)
		}
	}
	
	Gui, TreeView, Vf_data
	parent_id = 0
	Loop {
		parent_id := TV_GetNext(parent_id)
		if (parent_id == 0) {
			break
		}
		WriteString(file, "f")
		
		TV_GetText(string, parent_id)
		WriteString(file, string)
		
		child_id := TV_GetChild(parent_id)
		TV_GetText(string, child_id)
		file.WriteFloat(string)
	}
	
	WriteString(file, "end")
	file.close()
	
	SplitPath, file_path, OutFileName, OutDir, OutExtension, OutNameNoExt, OutDrive
	MsgBox, %OutFileName% Do we overwrite this file?
return

texAdd:
	Gui, TreeView, SysTreeView321
	InputBox, new_name, , Please enter new name, , , , , , , , _
	if (ErrorLevel == 0){
		parent := TV_Add(new_name, 0, "Expand")
		TV_Add("tex2d", parent)
		TV_Add("XXX_XXX", parent)
		TV_Add("Assets/texture/~*.png", parent)
		vector := TV_Add("Color Information?", parent)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
	}
return
texDel:
	Gui, TreeView, SysTreeView321
	parent := TV_GetSelection()
	if (parent == 0) {
		MsgBox, Please select an item
		return
	}
	Loop {
		if (TV_GetParent(parent) != 0) {
			parent := TV_GetParent(parent)
		}
		else {
			break
		}
	}
	TV_Delete(parent)
return
colAdd:
	Gui, TreeView, SysTreeView322
	InputBox, new_name, , Please enter a new name
	if (ErrorLevel == 0){
		parent := TV_Add(new_name, 0, "Expand")
		vector := TV_Add("Color information", parent)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
		TV_Add(1.0, vector)
	}
return
colDel:
	Gui, TreeView, SysTreeView322
	parent := TV_GetSelection()
	if (parent == 0) {
		MsgBox, Please select an item
		return
	}
	Loop {
		if (TV_GetParent(parent) != 0) {
			parent := TV_GetParent(parent)
		}
		else {
			break
		}
	}
	TV_Delete(parent)
return
fAdd:
	Gui, TreeView, SysTreeView323
	InputBox, new_name, , Please enter a new name
	if (ErrorLevel == 0){
		parent := TV_Add(new_name, 0, "Expand")
		TV_Add(1.0, parent)
	}
return
fDel:
	Gui, TreeView, SysTreeView323
	parent := TV_GetSelection()
	if (parent == 0) {
		MsgBox, Please select an item
		return
	}
	Loop {
		if (TV_GetParent(parent) != 0) {
			parent := TV_GetParent(parent)
		}
		else {
			break
		}
	}
	TV_Delete(parent)
return

GetMaxIndex(obj) {
	value := obj.MaxIndex()
	if (value == "") {
		return 0
	}
	return value
}

GetStringLength(string) {
	count = 0
	Loop, Parse, string
	{
		if (RegExMatch(A_LoopField, "^[^\x01-\x7E]$") != 0) {
			count += 3
		}
		else {
			count += 1
		}
	}
	return count
}

ReadString(file, size=-1) {
	if (size <= -1) {
		size := 0
		chars := Object()
		Loop {
			char := file.ReadUChar()
			chars[A_Index] := char
			if (char < 128) {
				break
			}
		}
		num := GetMaxIndex(chars)
		Loop, %num% {
			char := chars[A_Index]
			multi := 256 ** (A_Index - 1)
			size += char * multi
			if (1 < A_Index) {
				size -= (multi / 2) * (char + 1)
			}
		}
	}
	string := ""
	count = 0
	Loop, 9999 {
		if (size <= count) {
			break
		}
		s := file.Read(1)
		string := string . s
		count += GetStringLength(s)
		if (GetStringLength(s) == 0) {
			pos := file.Pos
			MsgBox, Failed to read the file(場所: %pos%)`nQuit
			ExitApp
		}
	}
	return string
}

WriteString(file, string) {
	len := GetStringLength(string)
	if (128 <= len) {
		temp := Mod(len, 128) + 128
		file.WriteChar(temp)
		temp := Floor(len / 128)
		file.WriteChar(temp)
	}
	else {
		file.WriteChar(len)
	}
	file.Write(string)
}

GetDefaultPath() {
	RegRead, path, HKEY_CURRENT_USER, Software\KISS\カスタムメイド3D2, InstallPath
	if (ErrorLevel == 0) {
		path = %path%GameData\
	}
	else {
		path := A_ScriptDir
	}
	return path
}

SetFloat(f) {
	f := RegExReplace(f, "(\.0)0+$", "$1")
	f := RegExReplace(f, "([1-9])0+$", "$1")
	return f
}

GuiEscape:
GuiClose:
	ExitApp
return