diff --git a/compare_test.go b/compare_test.go index 453de6b..7c9241f 100644 --- a/compare_test.go +++ b/compare_test.go @@ -52,7 +52,7 @@ func compareDelta(t *testing.T, filea, fileb string) error { if err != nil { return fmt.Errorf("cannot cast to obj array %v", err) } - err = compareObjArrays(t, "root", asubOs, bsubOs) + err = compareObjArrays(t, asubOs, bsubOs) if err != nil { t.Errorf("compareObjs(<>) : %v", err) } @@ -83,7 +83,7 @@ func toObjArray(i interface{}) ([]map[string]interface{}, error) { return arr, nil } -func compareObjArrays(t *testing.T, guid string, a, b []map[string]interface{}) error { +func compareObjArrays(t *testing.T, a, b []map[string]interface{}) error { if len(a) != len(b) { return fmt.Errorf("length mismatch %v vs %v", len(a), len(b)) } @@ -141,7 +141,7 @@ func compareObjs(t *testing.T, guid string, a, b map[string]interface{}) error { return err } - err = compareObjArrays(t, guid, aArr, bArr) + err = compareObjArrays(t, aArr, bArr) if err != nil { return fmt.Errorf("subObjects of %s[ContainedObjects] have diff: %v", guid, err) } diff --git a/go.mod b/go.mod index a870cc0..289f111 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module ModCreator -go 1.17 +go 1.18 require ( github.com/davecgh/go-spew v1.1.0 // indirect diff --git a/main.go b/main.go index c65cc59..c5490c5 100644 --- a/main.go +++ b/main.go @@ -144,7 +144,7 @@ func prepForReverse(cPath, modfile string) (types.J, error) { return nil, err } } else { - return nil, fmt.Errorf("Undefined error checking for subdirectory %s : %v", s, err) + return nil, fmt.Errorf("undefined error checking for subdirectory %s : %v", s, err) } } diff --git a/mod/reverse.go b/mod/reverse.go index 19fe29f..2437bba 100644 --- a/mod/reverse.go +++ b/mod/reverse.go @@ -5,6 +5,7 @@ import ( "ModCreator/handler" "ModCreator/objects" "ModCreator/types" + "encoding/json" "fmt" ) @@ -67,11 +68,23 @@ func (r *Reverser) Write(raw map[string]interface{}) error { } createdFile := strKey + ext - - err := r.LuaWriter.EncodeToFile(strVal, createdFile) - if err != nil { - return fmt.Errorf("lua.EncodeToFile(, %s) : %v", createdFile, err) + + var jsonInterface map[string]interface{} + err := json.Unmarshal([]byte(strVal), &jsonInterface) + if err == nil { + // if it's JSON, use the JSONWriter + err := r.ObjWriter.WriteObj(jsonInterface, createdFile) + if err != nil { + return fmt.Errorf("j.WriteObj(, %s) : %v", createdFile, err) + } + } else { + // default to the regular text writing + err := r.LuaWriter.EncodeToFile(strVal, createdFile) + if err != nil { + return fmt.Errorf("lua.EncodeToFile(, %s) : %v", createdFile, err) + } } + raw[strKey+pathExt] = createdFile delete(raw, strKey) } diff --git a/mod/reverse_test.go b/mod/reverse_test.go index d78e18b..caa3c21 100644 --- a/mod/reverse_test.go +++ b/mod/reverse_test.go @@ -159,7 +159,7 @@ func TestReverse(t *testing.T) { }, wantObjs: map[string]types.J{}, wantModSettings: map[string]types.J{}, - wantSrcTexts: map[string]string{ + wantSrcTexts: map[string]string{ "playermat/SkillToken.ttslua": "MIN_VALUE = -99\r\nMAX_VALUE = 999\r\n\r\nfunction onload(saved_data)\r\n light_mode = false\r\n val = 0\r\n\r\n if saved_data ~= \"\" then\r\n local loaded_data = JSON.decode(saved_data)\r\n light_mode = loaded_data[1]\r\n val = loaded_data[2]\r\n end\r\n\r\n createAll()\r\nend\r\n\r\nfunction updateSave()\r\n local data_to_save = {light_mode, val}\r\n saved_data = JSON.encode(data_to_save)\r\n self.script_state = saved_data\r\nend\r\n\r\nfunction createAll()\r\n s_color = {0.5, 0.5, 0.5, 95}\r\n\r\n if light_mode then\r\n f_color = {1,1,1,95}\r\n else\r\n f_color = {0,0,0,100}\r\n end\r\n\r\n\r\n\r\n self.createButton({\r\n label=tostring(val),\r\n click_function=\"add_subtract\",\r\n function_owner=self,\r\n position={0,0.05,0},\r\n height=600,\r\n width=1000,\r\n alignment = 3,\r\n scale={x=1.5, y=1.5, z=1.5},\r\n font_size=600,\r\n font_color=f_color,\r\n color={0,0,0,0}\r\n })\r\n\r\n\r\n\r\n\r\n if light_mode then\r\n lightButtonText = \"[ Set dark ]\"\r\n else\r\n lightButtonText = \"[ Set light ]\"\r\n end\r\n\r\nend\r\n\r\nfunction removeAll()\r\n self.removeInput(0)\r\n self.removeInput(1)\r\n self.removeButton(0)\r\n self.removeButton(1)\r\n self.removeButton(2)\r\nend\r\n\r\nfunction reloadAll()\r\n removeAll()\r\n createAll()\r\n\r\n updateSave()\r\nend\r\n\r\nfunction swap_fcolor(_obj, _color, alt_click)\r\n light_mode = not light_mode\r\n reloadAll()\r\nend\r\n\r\nfunction swap_align(_obj, _color, alt_click)\r\n center_mode = not center_mode\r\n reloadAll()\r\nend\r\n\r\nfunction editName(_obj, _string, value)\r\n self.setName(value)\r\n setTooltips()\r\nend\r\n\r\nfunction add_subtract(_obj, _color, alt_click)\r\n mod = alt_click and -1 or 1\r\n new_value = math.min(math.max(val + mod, MIN_VALUE), MAX_VALUE)\r\n if val ~= new_value then\r\n val = new_value\r\n updateVal()\r\n updateSave()\r\n end\r\nend\r\n\r\nfunction updateVal()\r\n\r\n self.editButton({\r\n index = 0,\r\n label = tostring(val),\r\n\r\n })\r\nend\r\n\r\nfunction reset_val()\r\n val = 0\r\n updateVal()\r\n updateSave()\r\nend\r\n\r\nfunction setTooltips()\r\n self.editInput({\r\n index = 0,\r\n value = self.getName(),\r\n tooltip = ttText\r\n })\r\n self.editButton({\r\n index = 0,\r\n value = tostring(val),\r\n tooltip = ttText\r\n })\r\nend\r\n\r\nfunction null()\r\nend\r\n\r\nfunction keepSample(_obj, _string, value)\r\n reloadAll()\r\nend", }, wantObjTexts: map[string]string{}, @@ -175,7 +175,7 @@ func TestReverse(t *testing.T) { wantObjs: map[string]types.J{}, wantSrcTexts: map[string]string{}, wantModSettings: map[string]types.J{}, - wantObjTexts: map[string]string{ + wantObjTexts: map[string]string{ "LuaScript.ttslua": "var foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\n", }, }, @@ -192,7 +192,7 @@ func TestReverse(t *testing.T) { }, wantObjs: map[string]types.J{}, wantModSettings: map[string]types.J{}, - wantObjTexts: map[string]string{ + wantObjTexts: map[string]string{ "LuaScript.ttslua": "require(\"playermat/SkillToken\")\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42\nvar foo = 42", }, }, @@ -277,5 +277,4 @@ func TestReverse(t *testing.T) { } }) } - } diff --git a/objects/objects.go b/objects/objects.go index 6e79722..05eb52e 100644 --- a/objects/objects.go +++ b/objects/objects.go @@ -4,11 +4,11 @@ import ( "ModCreator/file" "ModCreator/handler" . "ModCreator/types" + "encoding/json" + "fmt" "path" "regexp" "strings" - - "fmt" ) type objConfig struct { @@ -238,9 +238,21 @@ func (o *objConfig) printToFile(filepath string, p *Printer) error { if len(script) > 80 { createdFile := path.Join(filepath, o.getAGoodFileName()+".luascriptstate") out["LuaScriptState_path"] = createdFile - if err := p.Lua.EncodeToFile(script, createdFile); err != nil { - return fmt.Errorf("EncodeToFile()", o.guid) + + var jsonInterface map[string]interface{} + err := json.Unmarshal([]byte(script), &jsonInterface) + if err == nil { + // if it's JSON, use the JSONWriter + if err := p.J.WriteObj(jsonInterface, createdFile); err != nil { + return fmt.Errorf("j.WriteObj()", o.guid) + } + } else { + // default to the regular text writing + if err := p.Lua.EncodeToFile(script, createdFile); err != nil { + return fmt.Errorf("EncodeToFile()", o.guid) + } } + delete(out, "LuaScriptState") } } diff --git a/objects/objects_test.go b/objects/objects_test.go index 6f76a3d..ca0b814 100644 --- a/objects/objects_test.go +++ b/objects/objects_test.go @@ -164,17 +164,19 @@ func TestObjPrinting(t *testing.T) { func TestObjPrintingToFile(t *testing.T) { type fileContent struct { - file, content string + file string + content string } type jsonContent struct { file string content types.J } for _, tc := range []struct { - o *objConfig - folder string - wantObjs []jsonContent - wantLSS fileContent + o *objConfig + folder string + wantObjs []jsonContent + wantLss fileContent + wantLssAsJson jsonContent }{ { o: &objConfig{ @@ -293,11 +295,47 @@ func TestObjPrintingToFile(t *testing.T) { }, }, }, - wantLSS: fileContent{ + wantLss: fileContent{ file: "foo/123456.luascriptstate", content: "fav color = green fav color = green fav color = green fav color = green fav color = green", }, }, + { + o: &objConfig{ + guid: "123456", + data: types.J{ + "LuaScriptState": "{\"acknowledgedUpgradeVersions\":[],\"optionPanel\":{\"cardLanguage\":\"en\",\"changePlayAreaImage\":false,\"playAreaConnectionColor\":{\"a\":1,\"b\":0.4,\"g\":0.4,\"r\":0.4},\"useResourceCounters\":\"disabled\"}}", + "GUID": "123456", + }, + }, + folder: "foo", + wantObjs: []jsonContent{ + { + file: "foo/123456.json", + content: types.J{ + "GUID": "123456", + "LuaScriptState_path": "foo/123456.luascriptstate", + }, + }, + }, + wantLssAsJson: jsonContent{ + file: "foo/123456.luascriptstate", + content: types.J{ + "acknowledgedUpgradeVersions": []any{}, + "optionPanel": map[string]any{ + "cardLanguage": string("en"), + "changePlayAreaImage": bool(false), + "playAreaConnectionColor": map[string]any{ + "a": float64(1), + "b": float64(0.4), + "g": float64(0.4), + "r": float64(0.4), + }, + "useResourceCounters": string("disabled"), + }, + }, + }, + }, } { ff := tests.NewFF() p := &Printer{ @@ -322,13 +360,25 @@ func TestObjPrintingToFile(t *testing.T) { } // compare lua script state - if tc.wantLSS.file != "" { - got, ok := ff.Fs[tc.wantLSS.file] + if tc.wantLss.file != "" { + got, ok := ff.Fs[tc.wantLss.file] + if !ok { + ff.DebugFileNames(t.Logf) + t.Errorf("wanted luascript state %s, didn't find", tc.wantLss.file) + } + if diff := cmp.Diff(tc.wantLss.content, got); diff != "" { + t.Errorf("want != got:\n%v\n", diff) + } + } + + // compare lua script state that should output as json + if tc.wantLssAsJson.file != "" { + got, ok := ff.Data[tc.wantLssAsJson.file] if !ok { ff.DebugFileNames(t.Logf) - t.Errorf("wanted luascript state %s, didn't find", tc.wantLSS.file) + t.Errorf("wanted luascript state %s, didn't find", tc.wantLssAsJson.file) } - if diff := cmp.Diff(tc.wantLSS.content, got); diff != "" { + if diff := cmp.Diff(tc.wantLssAsJson.content, got); diff != "" { t.Errorf("want != got:\n%v\n", diff) } }