diff --git a/bundler/xmlbundler.go b/bundler/xmlbundler.go index 8516231..54cb43b 100644 --- a/bundler/xmlbundler.go +++ b/bundler/xmlbundler.go @@ -59,8 +59,7 @@ func indentString(s string, indent string) string { return strings.Join(final, "\n") } -// UnbundleAllXML converts a bundled xml file to mapping of filenames to -// contents +// UnbundleAllXML converts a bundled xml file to mapping of filenames to contents func UnbundleAllXML(rawxml string) (map[string]string, error) { type inc struct { name string @@ -83,7 +82,7 @@ func UnbundleAllXML(rawxml string) (map[string]string, error) { indent := string(submatches[1]) indentedvals := xmlarray[stack[0].start+1 : ln] - // do not let mods specify relative paths. + // do not let mods specify relative paths storedName := strings.Replace(stack[0].name, "../", "", -1) store[storedName] = unindentAndJoin(indentedvals, indent) @@ -95,7 +94,7 @@ func UnbundleAllXML(rawxml string) (map[string]string, error) { ln = stack[0].start stack = stack[1:] } else { - stack = append([]inc{inc{name: key, start: ln}}, stack...) + stack = append([]inc{{name: key, start: ln}}, stack...) } } if len(stack) != 0 { diff --git a/main.go b/main.go index 6f9d3c5..f704885 100644 --- a/main.go +++ b/main.go @@ -10,15 +10,15 @@ import ( "io/ioutil" "log" "os" - "path" + "path/filepath" ) var ( moddir = flag.String("moddir", "testdata/simple", "a directory containing tts mod configs") - rev = flag.Bool("reverse", false, "Instead of building a json from file structure, build file structure from json.") - writeToSrc = flag.Bool("writesrc", false, "When unbundling Lua, save the included 'require' files to the src/ directory.") + rev = flag.Bool("reverse", false, "instead of building a json from file structure, build file structure from json.") + writeToSrc = flag.Bool("writesrc", false, "when unbundling Lua, save the included 'require' files to the src/ directory.") modfile = flag.String("modfile", "", "where to read from when reversing.") - objin = flag.String("objin", "", "If non-empty, don't build/reverse a full mod, only an object state array") + objin = flag.String("objin", "", "if non-empty, don't build/reverse a full mod, only an object state array") objout = flag.String("objout", "", "if building only object state list, output to this filename") ) @@ -37,53 +37,55 @@ func main() { } lua := file.NewTextOpsMulti( - []string{path.Join(*moddir, luasrcSubdir), path.Join(*moddir, objectsSubdir)}, - path.Join(*moddir, objectsSubdir), + []string{filepath.Join(*moddir, luasrcSubdir), filepath.Join(*moddir, objectsSubdir)}, + filepath.Join(*moddir, objectsSubdir), ) xml := file.NewTextOpsMulti( - []string{path.Join(*moddir, xmlsrcSubdir), path.Join(*moddir, objectsSubdir)}, - path.Join(*moddir, objectsSubdir), + []string{filepath.Join(*moddir, xmlsrcSubdir), filepath.Join(*moddir, objectsSubdir)}, + filepath.Join(*moddir, objectsSubdir), ) - xmlSrc := file.NewTextOps(path.Join(*moddir, xmlsrcSubdir)) - luaSrc := file.NewTextOps(path.Join(*moddir, luasrcSubdir)) - ms := file.NewJSONOps(path.Join(*moddir, modsettingsDir)) - objs := file.NewJSONOps(path.Join(*moddir, objectsSubdir)) - objdir := file.NewDirOps(path.Join(*moddir, objectsSubdir)) + xmlSrc := file.NewTextOps(filepath.Join(*moddir, xmlsrcSubdir)) + luaSrc := file.NewTextOps(filepath.Join(*moddir, luasrcSubdir)) + ms := file.NewJSONOps(filepath.Join(*moddir, modsettingsDir)) + objs := file.NewJSONOps(filepath.Join(*moddir, objectsSubdir)) + objdir := file.NewDirOps(filepath.Join(*moddir, objectsSubdir)) rootops := file.NewJSONOps(*moddir) - basename := path.Base(*modfile) - outputOps := file.NewJSONOps(path.Dir(*modfile)) + basename := filepath.Base(*modfile) + outputOps := file.NewJSONOps(filepath.Dir(*modfile)) + // handling for saved objects instead of a full savegame if *objin != "" { - objdir = file.NewDirOps(path.Dir(*objin)) - objs = file.NewJSONOps(path.Dir(*objin)) + objs = file.NewJSONOps(filepath.Dir(*objin)) + objdir = file.NewDirOps(filepath.Dir(*objin)) lua = file.NewTextOpsMulti( - []string{path.Join(*moddir, luasrcSubdir), path.Dir(*objin)}, - path.Dir(*objout), + []string{filepath.Join(*moddir, luasrcSubdir), filepath.Dir(*objin)}, + filepath.Dir(*objout), ) xml = file.NewTextOpsMulti( - []string{path.Join(*moddir, xmlsrcSubdir), path.Dir(*objin)}, - path.Dir(*objout), + []string{filepath.Join(*moddir, xmlsrcSubdir), filepath.Dir(*objin)}, + filepath.Dir(*objout), ) - basename = path.Base(*objout) - outputOps = file.NewJSONOps(path.Dir(*objout)) + basename = filepath.Base(*objout) + outputOps = file.NewJSONOps(filepath.Dir(*objout)) } if *rev { if *objin != "" { *modfile = *objin - objs = file.NewJSONOps(path.Dir(*objout)) + objs = file.NewJSONOps(filepath.Dir(*objout)) } raw, err := prepForReverse(*moddir, *modfile) if err != nil { log.Fatalf("prepForReverse (%s) failed : %v", *modfile, err) } + r := mod.Reverser{ ModSettingsWriter: ms, LuaWriter: lua, XMLWriter: xml, ObjWriter: objs, - ObjDirCreeator: objdir, + ObjDirCreator: objdir, RootWrite: rootops, OnlyObjState: *objin, } @@ -98,11 +100,11 @@ func main() { return } if *modfile == "" { - *modfile = path.Join(*moddir, "output.json") + *modfile = filepath.Join(*moddir, "output.json") } // setting this to empty instead of default return value (".") if not found - OnlyObjStates := path.Base(*objin) + OnlyObjStates := filepath.Base(*objin) if OnlyObjStates == "." { OnlyObjStates = "" } @@ -133,7 +135,7 @@ func prepForReverse(cPath, modfile string) (types.J, error) { subDirs := []string{luasrcSubdir, modsettingsDir, objectsSubdir, xmlsrcSubdir} for _, s := range subDirs { - p := path.Join(cPath, s) + p := filepath.Join(cPath, s) if _, err := os.Stat(p); err == nil { // directory already exists } else if os.IsNotExist(err) { diff --git a/mod/generate.go b/mod/generate.go index 2943293..d97d0a0 100644 --- a/mod/generate.go +++ b/mod/generate.go @@ -29,15 +29,14 @@ var ( // Mod is used as the accurate representation of what gets printed when // module creation is done type Mod struct { - Data types.J - - RootRead file.JSONReader - RootWrite file.JSONWriter - Lua file.TextReader - XML file.TextReader - Modsettings file.JSONReader - Objs file.JSONReader - Objdirs file.DirExplorer + Data types.J + RootRead file.JSONReader + RootWrite file.JSONWriter + Lua file.TextReader + XML file.TextReader + Modsettings file.JSONReader + Objs file.JSONReader + Objdirs file.DirExplorer // If not-empty: this holds the root filename for the object state json object OnlyObjStates string diff --git a/mod/reverse.go b/mod/reverse.go index e8f920d..19fe29f 100644 --- a/mod/reverse.go +++ b/mod/reverse.go @@ -5,9 +5,7 @@ import ( "ModCreator/handler" "ModCreator/objects" "ModCreator/types" - "encoding/json" "fmt" - "io/ioutil" ) // Reverser holds interfaces and configs for the reversing process @@ -18,11 +16,11 @@ type Reverser struct { XMLWriter file.TextWriter XMLSrcWriter file.TextWriter ObjWriter file.JSONWriter - ObjDirCreeator file.DirCreator + ObjDirCreator file.DirCreator RootWrite file.JSONWriter // If not empty: holds the entire filename (C:...) of the json to read - OnlyObjState string + OnlyObjState string } func (r *Reverser) writeOnlyObjStates(raw map[string]interface{}) error { @@ -30,7 +28,7 @@ func (r *Reverser) writeOnlyObjStates(raw map[string]interface{}) error { Lua: r.LuaWriter, LuaSrc: r.LuaSrcWriter, J: r.ObjWriter, - Dir: r.ObjDirCreeator, + Dir: r.ObjDirCreator, } arraywrap := []map[string]interface{}{raw} _, err := printer.PrintObjectStates("", arraywrap) @@ -58,7 +56,7 @@ func (r *Reverser) Write(raw map[string]interface{}) error { return fmt.Errorf("expected string value in key %s, got %v", strKey, rawVal) } if strKey == "LuaScript" || strKey == "XmlUI" { - // let the LuaHAndler handle the complicated case + // let the LuaHandler handle the complicated case continue } ext := ".luascriptstate" @@ -168,7 +166,7 @@ func (r *Reverser) Write(raw map[string]interface{}) error { XML: r.XMLWriter, XMLSrc: r.XMLSrcWriter, J: r.ObjWriter, - Dir: r.ObjDirCreeator, + Dir: r.ObjDirCreator, } order, err := printer.PrintObjectStates("", objStates) if err != nil { @@ -183,18 +181,10 @@ func (r *Reverser) Write(raw map[string]interface{}) error { delete(raw, DateKey) delete(raw, EpochKey) - // write all that's Left + // write all that's left err = r.RootWrite.WriteObj(raw, "config.json") if err != nil { return fmt.Errorf("WriteObj(, %s) : %v", "config.json", err) } return nil } - -func writeJSON(raw map[string]interface{}, filename string) error { - b, err := json.MarshalIndent(raw, "", " ") - if err != nil { - return fmt.Errorf("json.MarshalIndent() : %v", err) - } - return ioutil.WriteFile(filename, b, 0644) -} diff --git a/mod/reverse_test.go b/mod/reverse_test.go index e06705a..d78e18b 100644 --- a/mod/reverse_test.go +++ b/mod/reverse_test.go @@ -249,7 +249,7 @@ func TestReverse(t *testing.T) { LuaSrcWriter: srcTexts, XMLWriter: srcTexts, ObjWriter: objsAndLua, - ObjDirCreeator: objsAndLua, + ObjDirCreator: objsAndLua, RootWrite: finalOutput, } err := r.Write(tc.input) diff --git a/tests/e2e_test.go b/tests/e2e_test.go index 7657f0b..183890a 100644 --- a/tests/e2e_test.go +++ b/tests/e2e_test.go @@ -56,7 +56,7 @@ func TestAllReverseThenBuild(t *testing.T) { XMLSrcWriter: objsAndLua, LuaSrcWriter: objsAndLua, ObjWriter: objsAndLua, - ObjDirCreeator: objsAndLua, + ObjDirCreator: objsAndLua, RootWrite: finalOutput, } err = r.Write(j)