Skip to content

Commit

Permalink
Merge pull request #72 from Chr1Z93/main
Browse files Browse the repository at this point in the history
  • Loading branch information
argonui authored Jul 26, 2024
2 parents 0e1a965 + 09495a1 commit cb6a96f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 59 deletions.
7 changes: 3 additions & 4 deletions bundler/xmlbundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
58 changes: 30 additions & 28 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand All @@ -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,
}
Expand All @@ -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 = ""
}
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 8 additions & 9 deletions mod/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 6 additions & 16 deletions mod/reverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,19 +16,19 @@ 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 {
printer := &objects.Printer{
Lua: r.LuaWriter,
LuaSrc: r.LuaSrcWriter,
J: r.ObjWriter,
Dir: r.ObjDirCreeator,
Dir: r.ObjDirCreator,
}
arraywrap := []map[string]interface{}{raw}
_, err := printer.PrintObjectStates("", arraywrap)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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(<obj>, %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)
}
2 changes: 1 addition & 1 deletion mod/reverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cb6a96f

Please sign in to comment.