Skip to content

Commit

Permalink
Merge pull request #40 from argonui/fix_core_script
Browse files Browse the repository at this point in the history
Fix luascript bunding from root config
  • Loading branch information
argonui authored Nov 13, 2022
2 parents 52425f1 + e3456ba commit 8ccf4cf
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
22 changes: 22 additions & 0 deletions mod/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mod

import (
"ModCreator/bundler"
"ModCreator/file"
"ModCreator/objects"
"ModCreator/types"
Expand Down Expand Up @@ -72,6 +73,27 @@ func (m *Mod) generate(raw types.J) error {
tryPut(&m.Data, objarraybased+ext, objarraybased, objArray)
}

if spraw, ok := m.Data["LuaScript_path"]; ok {
sp, ok := spraw.(string)
if !ok {
return fmt.Errorf("Expected LuaScript_path to be type string, was %T", spraw)
}
encoded, err := m.Lua.EncodeFromFile(sp)
if err != nil {
return fmt.Errorf("l.EncodeFromFile(%s) : %v", sp, err)
}
m.Data["LuaScript"] = encoded
}
if sraw, ok := m.Data["LuaScript"]; ok {
if str, ok := sraw.(string); ok && str != "" {
bundleReqs, err := bundler.Bundle(str, m.Lua)
if err != nil {
return fmt.Errorf("Bundle(%s) : %v", str, err)
}
m.Data["LuaScript"] = bundleReqs
}
}

objOrder := []string{}
files, _, _ := m.Objdirs.ListFilesAndFolders("")
hasObjects := len(files) > 0
Expand Down
105 changes: 105 additions & 0 deletions mod/generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package mod

import (
"ModCreator/tests"
"ModCreator/types"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func TestGenerate(t *testing.T) {
for _, tc := range []struct {
name string
inputRoot types.J
inputModSettings map[string]interface{}
inputOjbs map[string]types.J
inputLuaSrc map[string]string
inputObjTexts map[string]string
want map[string]interface{}
}{
{
name: "LuaScript",
inputRoot: map[string]interface{}{
"LuaScript": "require(\"core/Global\")",
},
inputLuaSrc: map[string]string{
"core/Global.ttslua": "var a = 42",
},
want: map[string]interface{}{
"LuaScript": "-- Bundled by luabundle {\"version\":\"1.6.0\"}\nlocal __bundle_require, __bundle_loaded, __bundle_register, __bundle_modules = (function(superRequire)\n\tlocal loadingPlaceholder = {[{}] = true}\n\n\tlocal register\n\tlocal modules = {}\n\n\tlocal require\n\tlocal loaded = {}\n\n\tregister = function(name, body)\n\t\tif not modules[name] then\n\t\t\tmodules[name] = body\n\t\tend\n\tend\n\n\trequire = function(name)\n\t\tlocal loadedModule = loaded[name]\n\n\t\tif loadedModule then\n\t\t\tif loadedModule == loadingPlaceholder then\n\t\t\t\treturn nil\n\t\t\tend\n\t\telse\n\t\t\tif not modules[name] then\n\t\t\t\tif not superRequire then\n\t\t\t\t\tlocal identifier = type(name) == 'string' and '\\\"' .. name .. '\\\"' or tostring(name)\n\t\t\t\t\terror('Tried to require ' .. identifier .. ', but no such module has been registered')\n\t\t\t\telse\n\t\t\t\t\treturn superRequire(name)\n\t\t\t\tend\n\t\t\tend\n\n\t\t\tloaded[name] = loadingPlaceholder\n\t\t\tloadedModule = modules[name](require, loaded, register, modules)\n\t\t\tloaded[name] = loadedModule\n\t\tend\n\n\t\treturn loadedModule\n\tend\n\n\treturn require, loaded, register, modules\nend)(nil)\n__bundle_register(\"__root\", function(require, _LOADED, __bundle_register, __bundle_modules)\nrequire(\"core/Global\")\nend)\n__bundle_register(\"core/Global\", function(require, _LOADED, __bundle_register, __bundle_modules)\nvar a = 42\nend)\nreturn __bundle_require(\"__root\")",
"CameraStates": nil,
"ComponentTags": nil,
"MusicPlayer": nil,
"Sky": "",
"TabStates": nil,
"Note": "",
"ObjectStates": []interface{}{},
"SaveName": "",
"Table": "",
"LuaScriptState": "",
"SnapPoints": nil,
"XmlUI": "",
"Turns": nil,
"VersionNumber": "",
"GameMode": "",
"GameType": "",
"Hands": nil,
"Grid": nil,
"Lighting": nil,
"GameComplexity": "",
"Decals": nil,
"CustomUIAssets": nil,
"DecalPallet": nil,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
rootff := &tests.FakeFiles{
Data: map[string]types.J{
"config.json": tc.inputRoot,
},
}
luaReadff := &tests.FakeFiles{
Fs: tc.inputLuaSrc,
}
msff := &tests.FakeFiles{}
objs := &tests.FakeFiles{
Data: tc.inputOjbs,
Fs: tc.inputObjTexts,
}
m := Mod{
RootRead: rootff,
RootWrite: rootff,
Lua: luaReadff,
Modsettings: msff,
Objs: objs,
Objdirs: objs,
}
err := m.GenerateFromConfig()
if err != nil {
t.Fatalf("Error reading config %v", err)
}
err = m.Print()
if err != nil {
t.Fatalf("Error printing config %v", err)
}
got, err := rootff.ReadObj("output.json")
if err != nil {
t.Fatalf("Error reading output: %v", err)
}

ignoreUnpredictable := func(k string, v interface{}) bool {
if k == "Date" || k == "EpochTime" {
return true
}

return false
}
if diff := cmp.Diff(tc.want, got, cmpopts.IgnoreMapEntries(ignoreUnpredictable)); diff != "" {
t.Errorf("want != got:\n%v\n", diff)
}
})
}
}
5 changes: 3 additions & 2 deletions tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ func TestAllReverseThenBuild(t *testing.T) {
_, filename := filepath.Split(path)
testname := filename[:len(filename)-len(filepath.Ext(path))]
denyList := []string{
"small_lua", // currently tries to bundle it
"long_lua", // currently tries to bundle it
"small_lua", // currently tries to bundle it
"long_lua", // currently tries to bundle it
"bundled_core", // currently depends on src file that test doesn't know about
}

t.Run(testname, func(t *testing.T) {
Expand Down
27 changes: 27 additions & 0 deletions tests/testdata/e2e/bundled_core.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"CameraStates": [],
"ComponentTags": {},
"CustomUIAssets": [],
"Date": "",
"DecalPallet": [],
"Decals": [],
"GameComplexity": "",
"GameMode": "",
"GameType": "",
"Grid": {},
"Hands": {},
"Lighting": {},
"LuaScript": "-- Bundled by luabundle {\"version\":\"1.6.0\"}\nlocal __bundle_require, __bundle_loaded, __bundle_register, __bundle_modules = (function(superRequire)\n\tlocal loadingPlaceholder = {[{}] = true}\n\n\tlocal register\n\tlocal modules = {}\n\n\tlocal require\n\tlocal loaded = {}\n\n\tregister = function(name, body)\n\t\tif not modules[name] then\n\t\t\tmodules[name] = body\n\t\tend\n\tend\n\n\trequire = function(name)\n\t\tlocal loadedModule = loaded[name]\n\n\t\tif loadedModule then\n\t\t\tif loadedModule == loadingPlaceholder then\n\t\t\t\treturn nil\n\t\t\tend\n\t\telse\n\t\t\tif not modules[name] then\n\t\t\t\tif not superRequire then\n\t\t\t\t\tlocal identifier = type(name) == 'string' and '\\\"' .. name .. '\\\"' or tostring(name)\n\t\t\t\t\terror('Tried to require ' .. identifier .. ', but no such module has been registered')\n\t\t\t\telse\n\t\t\t\t\treturn superRequire(name)\n\t\t\t\tend\n\t\t\tend\n\n\t\t\tloaded[name] = loadingPlaceholder\n\t\t\tloadedModule = modules[name](require, loaded, register, modules)\n\t\t\tloaded[name] = loadedModule\n\t\tend\n\n\t\treturn loadedModule\n\tend\n\n\treturn require, loaded, register, modules\nend)(nil)\n__bundle_register(\"__root\", function(require, _LOADED, __bundle_register, __bundle_modules)\nrequire(\"core/Global\")\nend)\n__bundle_register(\"core/Global\", function(require, _LOADED, __bundle_register, __bundle_modules)\n\r\nfunction urldecode(str)\r\n str = string.gsub(str, \"%%(%x%x)\",\r\n function (h) return string.char(tonumber(h, 16)) end)\r\n return str\r\nend\r\n\nend)\nreturn __bundle_require(\"__root\")",
"LuaScriptState": "",
"MusicPlayer": {},
"Note": "",
"ObjectStates": [],
"SaveName": "",
"Sky": "",
"SnapPoints": [],
"TabStates": {},
"Table": "",
"Turns": {},
"VersionNumber": "",
"XmlUI": ""
}

0 comments on commit 8ccf4cf

Please sign in to comment.