Skip to content

Commit

Permalink
v2.2.2 script模块支持--no_subdir参数,输出和输入路径不在自动追加SCRIPT.PAK子目录
Browse files Browse the repository at this point in the history
  • Loading branch information
wetor committed Dec 6, 2023
1 parent df504dc commit 69e8561
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var (
)

func init() {
if os.Args[1] == "-h" || os.Args[1] == "--help" {
if len(os.Args) <= 1 || os.Args[1] == "-h" || os.Args[1] == "--help" {
_ = rootCmd.Help()
os.Exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
ScriptExportDir string
ScriptImportDir string
ScriptImportOutput string
ScriptNoSubDir bool
)

func init() {
Expand All @@ -38,6 +39,6 @@ func init() {
scriptCmd.PersistentFlags().StringVarP(&Charset, "charset", "c", string(charset.UTF_8), "PAK文件字符串编码")
scriptCmd.PersistentFlags().StringVarP(&ScriptOpcode, "opcode", "O", "", "游戏的OPCODE文件")
scriptCmd.PersistentFlags().StringVarP(&ScriptPlugin, "plugin", "p", "", "游戏OPCODE解析插件")

scriptCmd.PersistentFlags().BoolVarP(&ScriptNoSubDir, "no_subdir", "n", false, "输入和输出路径的不追加 '/SCRIPT.PAK/' 子目录")
scriptCmd.MarkPersistentFlagRequired("source")
}
4 changes: 2 additions & 2 deletions cmd/scriptDecompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ var scriptDecompileCmd = &cobra.Command{
g.LoadScriptResources(ScriptSource)
g.RunScript()

g.ExportScript(ScriptExportDir)
g.ExportScript(ScriptExportDir, ScriptNoSubDir)
},
}

func init() {
scriptCmd.AddCommand(scriptDecompileCmd)

scriptDecompileCmd.Flags().StringVarP(&ScriptExportDir, "output", "o", "output", "反编译输出路径(目录不能有SCRIPT.PAK同名文件)")
scriptDecompileCmd.Flags().StringVarP(&ScriptExportDir, "output", "o", "output", "反编译输出路径")

// Here you will define your flags and configuration settings.

Expand Down
4 changes: 2 additions & 2 deletions cmd/scriptImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var scriptImportCmd = &cobra.Command{
Mode: enum.VMRunImport,
})
g.LoadScriptResources(ScriptSource)
g.ImportScript(ScriptImportDir)
g.ImportScript(ScriptImportDir, ScriptNoSubDir)
g.RunScript()
g.ImportScriptWrite(ScriptImportOutput)

Expand All @@ -36,7 +36,7 @@ var scriptImportCmd = &cobra.Command{
func init() {
scriptCmd.AddCommand(scriptImportCmd)

scriptImportCmd.Flags().StringVarP(&ScriptImportDir, "input", "i", "output", "同反编译输出路径(目录内包含SCRIPT.PAK文件夹)")
scriptImportCmd.Flags().StringVarP(&ScriptImportDir, "input", "i", "output", "输出的反编译脚本路径")
scriptImportCmd.Flags().StringVarP(&ScriptImportOutput, "output", "o", "SCRIPT.PAK.out", "输出的SCRIPT.PAK文件")

scriptImportCmd.MarkFlagsRequiredTogether("input", "output")
Expand Down
32 changes: 24 additions & 8 deletions game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ func (g *Game) RunScript() {

}

func (g *Game) ExportScript(dir string) {
dir = path.Join(dir, ResScript)
func (g *Game) ExportScript(dir string, noSubDir bool) {
if !noSubDir {
dir = path.Join(dir, ResScript)
}
exist, isDir := IsExistDir(dir)
if exist && !isDir {
panic("已存在同名文件")
Expand All @@ -123,8 +125,14 @@ func (g *Game) ExportScript(dir string) {
os.MkdirAll(dir, os.ModePerm)
}
for _, name := range g.ScriptList {
f, _ := os.Create(path.Join(dir, name+ResScriptExt))
g.VM.Scripts[name].Export(f)
f, err := os.Create(path.Join(dir, name+ResScriptExt))
if err != nil {
panic(err)
}
err = g.VM.Scripts[name].Export(f)
if err != nil {
panic(err)
}
f.Close()
}

Expand All @@ -133,11 +141,19 @@ func (g *Game) ExportScript(dir string) {
//}
}

func (g *Game) ImportScript(dir string) {
dir = path.Join(dir, ResScript)
func (g *Game) ImportScript(dir string, noSubDir bool) {
if !noSubDir {
dir = path.Join(dir, ResScript)
}
for _, name := range g.ScriptList {
f, _ := os.Open(path.Join(dir, name+ResScriptExt))
g.VM.Scripts[name].Import(f)
f, err := os.Open(path.Join(dir, name+ResScriptExt))
if err != nil {
panic(err)
}
err = g.VM.Scripts[name].Import(f)
if err != nil {
panic(err)
}
f.Close()
}
}
Expand Down
4 changes: 2 additions & 2 deletions game/game_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestLoadPak(t *testing.T) {
game.LoadResources()
game.RunScript()

game.ExportScript("C:/Users/wetor/Desktop/Prototype/Export")
game.ExportScript("C:/Users/wetor/Desktop/Prototype/Export", false)

}

Expand All @@ -49,7 +49,7 @@ func TestLoadPak2(t *testing.T) {
Mode: enum.VMRunImport,
})
game.LoadResources()
game.ImportScript("C:/Users/wetor/Desktop/Prototype/Export")
game.ImportScript("C:/Users/wetor/Desktop/Prototype/Export", false)
game.RunScript()

game.ImportScriptWrite("C:/Users/wetor/Desktop/Prototype/Import/SCRIPT.PAK")
Expand Down
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestLoopersExportScript(t *testing.T) {
g.LoadScriptResources("D:\\Game\\LOOPERS\\LOOPERS\\files\\src\\SCRIPT.PAK")
g.RunScript()

g.ExportScript("D:\\Game\\LOOPERS\\LOOPERS\\files\\Export")
g.ExportScript("D:\\Game\\LOOPERS\\LOOPERS\\files\\Export", false)

}

Expand All @@ -237,7 +237,7 @@ func TestLoopersImportScript(t *testing.T) {
Mode: enum.VMRunImport,
})
g.LoadScriptResources("D:\\Game\\LOOPERS\\LOOPERS\\files\\src\\SCRIPT.PAK")
g.ImportScript("D:\\Game\\LOOPERS\\LOOPERS\\files\\Export")
g.ImportScript("D:\\Game\\LOOPERS\\LOOPERS\\files\\Export", false)
g.RunScript()

g.ImportScriptWrite("D:\\Game\\LOOPERS\\LOOPERS\\files\\Import\\SCRIPT.PAK")
Expand Down

0 comments on commit 69e8561

Please sign in to comment.