Skip to content

Commit cb6a96f

Browse files
authored
Merge pull request #72 from Chr1Z93/main
2 parents 0e1a965 + 09495a1 commit cb6a96f

File tree

6 files changed

+49
-59
lines changed

6 files changed

+49
-59
lines changed

bundler/xmlbundler.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ func indentString(s string, indent string) string {
5959
return strings.Join(final, "\n")
6060
}
6161

62-
// UnbundleAllXML converts a bundled xml file to mapping of filenames to
63-
// contents
62+
// UnbundleAllXML converts a bundled xml file to mapping of filenames to contents
6463
func UnbundleAllXML(rawxml string) (map[string]string, error) {
6564
type inc struct {
6665
name string
@@ -83,7 +82,7 @@ func UnbundleAllXML(rawxml string) (map[string]string, error) {
8382
indent := string(submatches[1])
8483
indentedvals := xmlarray[stack[0].start+1 : ln]
8584

86-
// do not let mods specify relative paths.
85+
// do not let mods specify relative paths
8786
storedName := strings.Replace(stack[0].name, "../", "", -1)
8887

8988
store[storedName] = unindentAndJoin(indentedvals, indent)
@@ -95,7 +94,7 @@ func UnbundleAllXML(rawxml string) (map[string]string, error) {
9594
ln = stack[0].start
9695
stack = stack[1:]
9796
} else {
98-
stack = append([]inc{inc{name: key, start: ln}}, stack...)
97+
stack = append([]inc{{name: key, start: ln}}, stack...)
9998
}
10099
}
101100
if len(stack) != 0 {

main.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
"io/ioutil"
1111
"log"
1212
"os"
13-
"path"
13+
"path/filepath"
1414
)
1515

1616
var (
1717
moddir = flag.String("moddir", "testdata/simple", "a directory containing tts mod configs")
18-
rev = flag.Bool("reverse", false, "Instead of building a json from file structure, build file structure from json.")
19-
writeToSrc = flag.Bool("writesrc", false, "When unbundling Lua, save the included 'require' files to the src/ directory.")
18+
rev = flag.Bool("reverse", false, "instead of building a json from file structure, build file structure from json.")
19+
writeToSrc = flag.Bool("writesrc", false, "when unbundling Lua, save the included 'require' files to the src/ directory.")
2020
modfile = flag.String("modfile", "", "where to read from when reversing.")
21-
objin = flag.String("objin", "", "If non-empty, don't build/reverse a full mod, only an object state array")
21+
objin = flag.String("objin", "", "if non-empty, don't build/reverse a full mod, only an object state array")
2222
objout = flag.String("objout", "", "if building only object state list, output to this filename")
2323
)
2424

@@ -37,53 +37,55 @@ func main() {
3737
}
3838

3939
lua := file.NewTextOpsMulti(
40-
[]string{path.Join(*moddir, luasrcSubdir), path.Join(*moddir, objectsSubdir)},
41-
path.Join(*moddir, objectsSubdir),
40+
[]string{filepath.Join(*moddir, luasrcSubdir), filepath.Join(*moddir, objectsSubdir)},
41+
filepath.Join(*moddir, objectsSubdir),
4242
)
4343
xml := file.NewTextOpsMulti(
44-
[]string{path.Join(*moddir, xmlsrcSubdir), path.Join(*moddir, objectsSubdir)},
45-
path.Join(*moddir, objectsSubdir),
44+
[]string{filepath.Join(*moddir, xmlsrcSubdir), filepath.Join(*moddir, objectsSubdir)},
45+
filepath.Join(*moddir, objectsSubdir),
4646
)
47-
xmlSrc := file.NewTextOps(path.Join(*moddir, xmlsrcSubdir))
48-
luaSrc := file.NewTextOps(path.Join(*moddir, luasrcSubdir))
49-
ms := file.NewJSONOps(path.Join(*moddir, modsettingsDir))
50-
objs := file.NewJSONOps(path.Join(*moddir, objectsSubdir))
51-
objdir := file.NewDirOps(path.Join(*moddir, objectsSubdir))
47+
xmlSrc := file.NewTextOps(filepath.Join(*moddir, xmlsrcSubdir))
48+
luaSrc := file.NewTextOps(filepath.Join(*moddir, luasrcSubdir))
49+
ms := file.NewJSONOps(filepath.Join(*moddir, modsettingsDir))
50+
objs := file.NewJSONOps(filepath.Join(*moddir, objectsSubdir))
51+
objdir := file.NewDirOps(filepath.Join(*moddir, objectsSubdir))
5252
rootops := file.NewJSONOps(*moddir)
5353

54-
basename := path.Base(*modfile)
55-
outputOps := file.NewJSONOps(path.Dir(*modfile))
54+
basename := filepath.Base(*modfile)
55+
outputOps := file.NewJSONOps(filepath.Dir(*modfile))
5656

57+
// handling for saved objects instead of a full savegame
5758
if *objin != "" {
58-
objdir = file.NewDirOps(path.Dir(*objin))
59-
objs = file.NewJSONOps(path.Dir(*objin))
59+
objs = file.NewJSONOps(filepath.Dir(*objin))
60+
objdir = file.NewDirOps(filepath.Dir(*objin))
6061
lua = file.NewTextOpsMulti(
61-
[]string{path.Join(*moddir, luasrcSubdir), path.Dir(*objin)},
62-
path.Dir(*objout),
62+
[]string{filepath.Join(*moddir, luasrcSubdir), filepath.Dir(*objin)},
63+
filepath.Dir(*objout),
6364
)
6465
xml = file.NewTextOpsMulti(
65-
[]string{path.Join(*moddir, xmlsrcSubdir), path.Dir(*objin)},
66-
path.Dir(*objout),
66+
[]string{filepath.Join(*moddir, xmlsrcSubdir), filepath.Dir(*objin)},
67+
filepath.Dir(*objout),
6768
)
68-
basename = path.Base(*objout)
69-
outputOps = file.NewJSONOps(path.Dir(*objout))
69+
basename = filepath.Base(*objout)
70+
outputOps = file.NewJSONOps(filepath.Dir(*objout))
7071
}
7172

7273
if *rev {
7374
if *objin != "" {
7475
*modfile = *objin
75-
objs = file.NewJSONOps(path.Dir(*objout))
76+
objs = file.NewJSONOps(filepath.Dir(*objout))
7677
}
7778
raw, err := prepForReverse(*moddir, *modfile)
7879
if err != nil {
7980
log.Fatalf("prepForReverse (%s) failed : %v", *modfile, err)
8081
}
82+
8183
r := mod.Reverser{
8284
ModSettingsWriter: ms,
8385
LuaWriter: lua,
8486
XMLWriter: xml,
8587
ObjWriter: objs,
86-
ObjDirCreeator: objdir,
88+
ObjDirCreator: objdir,
8789
RootWrite: rootops,
8890
OnlyObjState: *objin,
8991
}
@@ -98,11 +100,11 @@ func main() {
98100
return
99101
}
100102
if *modfile == "" {
101-
*modfile = path.Join(*moddir, "output.json")
103+
*modfile = filepath.Join(*moddir, "output.json")
102104
}
103105

104106
// setting this to empty instead of default return value (".") if not found
105-
OnlyObjStates := path.Base(*objin)
107+
OnlyObjStates := filepath.Base(*objin)
106108
if OnlyObjStates == "." {
107109
OnlyObjStates = ""
108110
}
@@ -133,7 +135,7 @@ func prepForReverse(cPath, modfile string) (types.J, error) {
133135
subDirs := []string{luasrcSubdir, modsettingsDir, objectsSubdir, xmlsrcSubdir}
134136

135137
for _, s := range subDirs {
136-
p := path.Join(cPath, s)
138+
p := filepath.Join(cPath, s)
137139
if _, err := os.Stat(p); err == nil {
138140
// directory already exists
139141
} else if os.IsNotExist(err) {

mod/generate.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ var (
2929
// Mod is used as the accurate representation of what gets printed when
3030
// module creation is done
3131
type Mod struct {
32-
Data types.J
33-
34-
RootRead file.JSONReader
35-
RootWrite file.JSONWriter
36-
Lua file.TextReader
37-
XML file.TextReader
38-
Modsettings file.JSONReader
39-
Objs file.JSONReader
40-
Objdirs file.DirExplorer
32+
Data types.J
33+
RootRead file.JSONReader
34+
RootWrite file.JSONWriter
35+
Lua file.TextReader
36+
XML file.TextReader
37+
Modsettings file.JSONReader
38+
Objs file.JSONReader
39+
Objdirs file.DirExplorer
4140

4241
// If not-empty: this holds the root filename for the object state json object
4342
OnlyObjStates string

mod/reverse.go

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
55
"ModCreator/handler"
66
"ModCreator/objects"
77
"ModCreator/types"
8-
"encoding/json"
98
"fmt"
10-
"io/ioutil"
119
)
1210

1311
// Reverser holds interfaces and configs for the reversing process
@@ -18,19 +16,19 @@ type Reverser struct {
1816
XMLWriter file.TextWriter
1917
XMLSrcWriter file.TextWriter
2018
ObjWriter file.JSONWriter
21-
ObjDirCreeator file.DirCreator
19+
ObjDirCreator file.DirCreator
2220
RootWrite file.JSONWriter
2321

2422
// If not empty: holds the entire filename (C:...) of the json to read
25-
OnlyObjState string
23+
OnlyObjState string
2624
}
2725

2826
func (r *Reverser) writeOnlyObjStates(raw map[string]interface{}) error {
2927
printer := &objects.Printer{
3028
Lua: r.LuaWriter,
3129
LuaSrc: r.LuaSrcWriter,
3230
J: r.ObjWriter,
33-
Dir: r.ObjDirCreeator,
31+
Dir: r.ObjDirCreator,
3432
}
3533
arraywrap := []map[string]interface{}{raw}
3634
_, err := printer.PrintObjectStates("", arraywrap)
@@ -58,7 +56,7 @@ func (r *Reverser) Write(raw map[string]interface{}) error {
5856
return fmt.Errorf("expected string value in key %s, got %v", strKey, rawVal)
5957
}
6058
if strKey == "LuaScript" || strKey == "XmlUI" {
61-
// let the LuaHAndler handle the complicated case
59+
// let the LuaHandler handle the complicated case
6260
continue
6361
}
6462
ext := ".luascriptstate"
@@ -168,7 +166,7 @@ func (r *Reverser) Write(raw map[string]interface{}) error {
168166
XML: r.XMLWriter,
169167
XMLSrc: r.XMLSrcWriter,
170168
J: r.ObjWriter,
171-
Dir: r.ObjDirCreeator,
169+
Dir: r.ObjDirCreator,
172170
}
173171
order, err := printer.PrintObjectStates("", objStates)
174172
if err != nil {
@@ -183,18 +181,10 @@ func (r *Reverser) Write(raw map[string]interface{}) error {
183181
delete(raw, DateKey)
184182
delete(raw, EpochKey)
185183

186-
// write all that's Left
184+
// write all that's left
187185
err = r.RootWrite.WriteObj(raw, "config.json")
188186
if err != nil {
189187
return fmt.Errorf("WriteObj(<obj>, %s) : %v", "config.json", err)
190188
}
191189
return nil
192190
}
193-
194-
func writeJSON(raw map[string]interface{}, filename string) error {
195-
b, err := json.MarshalIndent(raw, "", " ")
196-
if err != nil {
197-
return fmt.Errorf("json.MarshalIndent() : %v", err)
198-
}
199-
return ioutil.WriteFile(filename, b, 0644)
200-
}

mod/reverse_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func TestReverse(t *testing.T) {
249249
LuaSrcWriter: srcTexts,
250250
XMLWriter: srcTexts,
251251
ObjWriter: objsAndLua,
252-
ObjDirCreeator: objsAndLua,
252+
ObjDirCreator: objsAndLua,
253253
RootWrite: finalOutput,
254254
}
255255
err := r.Write(tc.input)

tests/e2e_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestAllReverseThenBuild(t *testing.T) {
5656
XMLSrcWriter: objsAndLua,
5757
LuaSrcWriter: objsAndLua,
5858
ObjWriter: objsAndLua,
59-
ObjDirCreeator: objsAndLua,
59+
ObjDirCreator: objsAndLua,
6060
RootWrite: finalOutput,
6161
}
6262
err = r.Write(j)

0 commit comments

Comments
 (0)