1-- NW4C Menu Utilities(2010/10/20)
2-- Version 0.4.7
3-- (c)2010 Nintendo
4
5struct nw4c_utils
6(
7	enableSaveToScene = true,
8
9	fn outComment fs str =
10	(
11		format "# %\n" str to:fs
12	),
13
14	fn outReturn fs=
15	(
16		format "\n" to:fs
17	),
18
19	fn outValue fs key v =
20	(
21		format "%=\"%\"\n" key (v as string) to:fs
22	),
23
24	fn outVersion fs key v =
25	(
26		local str = ""
27		for i = 1 to 3 do
28		(
29			local d = (mod v 10) as integer
30			v = (v - d) / 10
31			str = (d as string) + str
32			if i != 3 do str = "." + str
33		)
34		outValue fs key str
35	),
36
37	fn outQuantValue fs key v =
38	(
39		local str = case v of
40		(
41			3: "Byte"
42			2: "Short"
43			default: "Float"
44		)
45
46		outValue fs key str
47	),
48
49	fn QuantStrToInt str =
50	(
51		local val = toLower str
52		case val of
53		(
54			"byte": 3
55			"short": 2
56			default: 1
57		)
58	),
59
60	fn loadSettingFromFile filename isDefault:false =
61	(
62		local nw4c = nw4cmax
63		if nw4c == undefined do return false
64		if filename == undefined do return false
65		local fs = openFile filename mode:"rt"
66		if fs == undefined do
67		(
68			format "File Open Error(%)\n" filename
69			return false
70		)
71		-- read header
72		if (findString (readLine fs) "NW4C_Export settings") == undefined do return false
73		local str, key, val
74		local isEOF = false
75		while isEOF == false do
76		(
77			str = readLine fs
78			-- skip comment and null line
79			if (str.count < 1) or (str[1] == "#") do continue
80			local elem = filterString str "="
81			if (classof elem != Array) or elem.count != 2 do continue
82			key = elem[1]
83			val = substituteString elem[2] "\"" ""
84			--format "\"%\"=\"%\"\n" key val
85			case key of
86			(
87				"ExportTarget": (
88					nw4c.doesExportSelected = ((stricmp val "Selection") == 0)
89				)
90				"OutputFileName": nw4c.filename = val
91				"OutputFolder": nw4c.outFolder = val
92				"OutputMode": nw4c.UseCreativeStudio = ((stricmp val "CreativeStudio") == 0)
93				"MergeCmdl": nw4c.UseMerge = val as booleanclass
94				"MergeCmdlPath": nw4c.MergeFilename = val
95				"Magnify": nw4c.Magnify = val as float
96				"FrameRange": nw4c.doesExportAllFrames =  ((stricmp val "Range") != 0)
97				"StartFrame": nw4c.StartFrame = val as integer
98				"EndFrame": nw4c.EndFrame = val as integer
99				"UseFigureMode": nw4c.useFiguremode = val as booleanclass
100				"OutputCmdl": nw4c.doesExportModel = val as booleanclass
101				"OutputCtex": nw4c.doesExportTexture = val as booleanclass
102				"OutputCskla": nw4c.doesExportAnimation = val as booleanclass
103				"OutputCmata": nw4c.doesExportMtlAnim = val as booleanclass
104				"OutputCcam": nw4c.doesExportCamera = val as booleanclass
105				"OutputClgt": nw4c.doesExportLight = val as booleanclass
106				"OutputCenv": nw4c.doesExportEnv = val as booleanclass
107				"OutputCmdla": nw4c.doesExportModelAnim = val as booleanclass
108				"CompressNode": (
109					nw4c.CompressNode = case val of
110					(
111						"Cull": 2
112						"CullUninfluential": 3
113						"UniteCompressible": 4
114						"UniteAll": 5
115						default: 1
116					)
117				)
118				"CompressMaterial": ()--nw4c.
119				"OptimizePrimitive": nw4c.OptimizePrimitive = val as booleanclass
120				"ConvertToModel": nw4c.DisableModelSimplification = not (val as booleanclass)
121				"QuantizePos": nw4c.QuantPos = QuantStrToInt val
122				"QuantizeNrm": nw4c.QuantNormal = QuantStrToInt val
123				"QuantizeTex": nw4c.QuantTex = QuantStrToInt val
124				"NonUniformScale": nw4c.NonUniformScale = val as booleanclass
125				"MaxReservedUniformRegisters": nw4c.ReservedUniformRegister = val as integer
126				"MeshVisibilityMode":  nw4c.VisibilityBindByName = ((stricmp val "BindByName") == 0)
127
128				"BakeAllAnim": ()
129				"FramePrecision": (
130					local fval = val as float
131					if fval > 0.0 do
132					(
133						local ival = (1.0 / fval) as integer
134						nw4c.AnimPrecision = case of
135						(
136							(ival <= 1): 1
137							(ival <= 2): 2
138							(ival <= 5): 5
139							default: 10
140						)
141					)
142				 )
143				"LoopAnim": nw4c.IsLoop = val as booleanclass
144				"FrameFormat": nw4c.IsFrameFormat = val as booleanclass
145				"BinPrecisionScale": ()--nw4c.PrecisionScale = val as float
146				"BinPrecisionRotate": ()--nw4c.PrecisionRotate = val as float
147				"BinPrecisionTranslate": ()--nw4c.PrecisionTrans = val as float
148				"ScaleQuantizeQuality": nw4c.ScaleQuality = val as integer
149				"RotateQuantizeQuality": nw4c.RotateQuality = val as integer
150				"TranslateQuantizeQuality": nw4c.TransQuality = val as integer
151
152				"ToleranceScale": nw4c.ToleranceScale = val as float
153				"ToleranceRotate": nw4c.ToleranceRotate = val as float
154				"ToleranceTranslate": nw4c.ToleranceTrans = val as float
155				"ToleranceUVScale": nw4c.ToleranceUVScale = val as float
156				"ToleranceUVRotate": nw4c.ToleranceUVRotate = val as float
157				"ToleranceUVTranslate": nw4c.ToleranceUVTrans = val as float
158				"ToleranceColor": nw4c.ToleranceColor= val as float
159
160				"3dsmax_SaveToScene": (
161					 --if isDefault do enableSaveToScene = val as booleanclass
162				 )
163				 "SettingsVersion": ()
164				 "GeneratorName": ()
165				 "GeneratorVersion": ()
166				 "Date": ()
167				 default: format "WARNING: unknown key found in c3es(%=%)\n" key val
168			)
169			--detect eof
170			if eof fs do
171			(
172				isEOF = true
173			)
174		)
175
176		return true
177	),
178
179	fn saveSettingToFile filename isDefault:false =
180	(
181		local nw4c = nw4cmax
182		if nw4c == undefined do return false
183		if filename == undefined do return false
184		local fs = openFile filename mode:"wt"
185		if fs == undefined do
186		(
187			format "File Open Error(%)\n" filename
188			return false
189		)
190		outComment fs "NW4C_Export settings"
191		local ver = nw4c.GetVersion()
192		outVersion fs "SettingsVersion" ver
193		local str = "3ds Max "
194		append str (((maxVersion())[1] / 1000 + 1998) as string)
195		append str " NW4C_Export"
196		outValue fs "GeneratorName" str
197		outVersion fs "GeneratorVersion" ver
198		local dt = getLocalTime()
199		local ss = stringStream ""
200		format "%-%-%T%:%:%" dt[1] dt[2] dt[4] dt[5] dt[6] dt[7]  to:ss
201		outValue fs "Date" (ss as string)
202		if isDefault do
203		(
204			--outValue fs "3dsmax_SaveToScene" enableSaveToScene
205		)
206		outReturn fs
207		--
208		outComment fs "Output Options"
209		outValue fs "ExportTarget" (if nw4c.doesExportSelected then "Selection" else "All")
210		outValue fs "OutputFileName" nw4c.filename
211		outValue fs "OutputMode" (if nw4c.UseCreativeStudio then "CreativeStudio" else "File")
212		outValue fs "OutputFolder" nw4c.outFolder
213		outValue fs "MergeCmdl" nw4c.UseMerge
214		outValue fs "MergeCmdlPath" nw4c.MergeFilename
215		outReturn fs
216		--
217		outComment fs "General Options"
218		outValue fs "Magnify" nw4c.Magnify
219		outValue fs "FrameRange" (if nw4c.doesExportAllFrames then "All" else "Range")
220		outValue fs "StartFrame" nw4c.StartFrame
221		outValue fs "EndFrame" nw4c.EndFrame
222		outValue fs "UseFigureMode" nw4c.useFiguremode
223		outReturn fs
224		--
225		outComment fs "Output File Selection"
226		outValue fs "OutputCmdl" nw4c.doesExportModel
227		outValue fs "OutputCtex" nw4c.doesExportTexture
228		outValue fs "OutputCskla" nw4c.doesExportAnimation
229		outValue fs "OutputCmata" nw4c.doesExportMtlAnim
230		outValue fs "OutputCcam" nw4c.doesExportCamera
231		outValue fs "OutputClgt" nw4c.doesExportLight
232		outValue fs "OutputCenv" nw4c.doesExportEnv
233		outValue fs "OutputCmdla" nw4c.doesExportModelAnim
234		outReturn fs
235		--
236		outComment fs "Optimization Options"
237		outValue fs "CompressNode" ( case nw4c.CompressNode of
238			(
239				2: "Cull"
240				3: "CullUninfluential"
241				4: "UniteCompressible"
242				5: "UniteAll"
243				default: "None"
244			)
245		)
246		outValue fs "CompressMaterial" nw4c.OptimizeMaterial
247		outValue fs "OptimizePrimitive" nw4c.OptimizePrimitive
248		outValue fs "ConvertToModel" (not nw4c.DisableModelSimplification)
249		outReturn fs
250		--
251		outComment fs "Quantization Options"
252		outQuantValue fs "QuantizePos" nw4c.QuantPos
253		outQuantValue fs "QuantizeNrm" nw4c.QuantNormal
254		outQuantValue fs "QuantizeTex" nw4c.QuantTex
255		outReturn fs
256		--
257		outComment fs "Model Options"
258		outValue fs "NonUniformScale" nw4c.NonUniformScale
259		outValue fs "MaxReservedUniformRegisters" nw4c.ReservedUniformRegister
260		outValue fs "MeshVisibilityMode" ( if nw4c.VisibilityBindByName then "BindByName" else "BindByIndex")
261
262		outReturn fs
263		--
264		outComment fs "Animation Options"
265		outValue fs "BakeAllAnim" true
266		outValue fs "FramePrecision" (1.0 / nw4c.AnimPrecision)
267		outValue fs "LoopAnim" nw4c.IsLoop
268		outValue fs "FrameFormat" nw4c.IsFrameFormat
269		--outValue fs "BinPrecisionScale" nw4c.PrecisionScale
270		--outValue fs "BinPrecisionRotate" nw4c.PrecisionRotate
271		--outValue fs "BinPrecisionTranslate" nw4c.PrecisionTrans
272		outValue fs	"ScaleQuantizeQuality"  nw4c.ScaleQuality
273		outValue fs	"RotateQuantizeQuality"  nw4c.RotateQuality
274		outValue fs	"TranslateQuantizeQuality"  nw4c.TransQuality
275		outReturn fs
276		--
277		outComment fs "Tolerance Options"
278		outValue fs "ToleranceScale" nw4c.ToleranceScale
279		outValue fs "ToleranceRotate" nw4c.ToleranceRotate
280		outValue fs "ToleranceTranslate" nw4c.ToleranceTrans
281		outValue fs "ToleranceUVScale" nw4c.ToleranceUVScale
282		outValue fs "ToleranceUVRotate" nw4c.ToleranceUVRotate
283		outValue fs "ToleranceUVTranslate" nw4c.ToleranceUVTrans
284		outValue fs "ToleranceColor" nw4c.ToleranceColor
285
286		close fs
287		return true
288	),
289
290	fn loadSettingConfig =
291	(
292		local fname = GetDir #plugcfg
293		append fname "\\nw4cmax.ini"
294		local val = getINISetting fname "nw4c_exporter" "enableSaveToScene"
295		enableSaveToScene = if val == "false" then false else true
296	),
297
298	fn saveSettingConfig =
299	(
300		local fname = GetDir #plugcfg
301		append fname "\\nw4cmax.ini"
302		setINISetting fname "nw4c_exporter" "enableSaveToScene" (enableSaveToScene as string)
303	),
304
305	fn loadSettingDefault =
306	(
307		--print "loadSettingDefault"
308		local fname = GetDir #plugcfg
309		append fname "\\nw4cmax.c3es"
310		if doesFileExist fname then
311		(
312			loadSettingFromFile fname isDefault:true
313			loadSettingConfig()
314		)
315		else
316		(
317			false
318		)
319	),
320
321	fn saveSettingDefault =
322	(
323		--print "saveSettingDefault"
324		local fname = GetDir #plugcfg
325		append fname "\\nw4cmax.c3es"
326		saveSettingToFile fname isDefault:true
327		saveSettingConfig()
328	)
329)
330