Skip to content

Commit

Permalink
Merge pull request #73 from Chr1Z93/scriptstate
Browse files Browse the repository at this point in the history
Added scriptstate JSON formatting
  • Loading branch information
argonui authored Aug 5, 2024
2 parents 9b4b1a6 + e4cba9e commit d083210
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 27 deletions.
6 changes: 3 additions & 3 deletions compare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ModCreator

go 1.17
go 1.18

require (
github.com/davecgh/go-spew v1.1.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
21 changes: 17 additions & 4 deletions mod/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"ModCreator/handler"
"ModCreator/objects"
"ModCreator/types"
"encoding/json"
"fmt"
)

Expand Down Expand Up @@ -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(<value>, %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(<value>, %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(<value>, %s) : %v", createdFile, err)
}
}

raw[strKey+pathExt] = createdFile
delete(raw, strKey)
}
Expand Down
7 changes: 3 additions & 4 deletions mod/reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand All @@ -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",
},
},
Expand All @@ -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",
},
},
Expand Down Expand Up @@ -277,5 +277,4 @@ func TestReverse(t *testing.T) {
}
})
}

}
20 changes: 16 additions & 4 deletions objects/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"ModCreator/file"
"ModCreator/handler"
. "ModCreator/types"
"encoding/json"
"fmt"
"path"
"regexp"
"strings"

"fmt"
)

type objConfig struct {
Expand Down Expand Up @@ -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(<obj %s>)", 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(<obj %s>)", o.guid)
}
} else {
// default to the regular text writing
if err := p.Lua.EncodeToFile(script, createdFile); err != nil {
return fmt.Errorf("EncodeToFile(<obj %s>)", o.guid)
}
}

delete(out, "LuaScriptState")
}
}
Expand Down
70 changes: 60 additions & 10 deletions objects/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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)
}
}
Expand Down

0 comments on commit d083210

Please sign in to comment.