Skip to content

Commit

Permalink
feat:(Version 0.1.2) add RestartCommand field to Config for custom se…
Browse files Browse the repository at this point in the history
…rver restarts
  • Loading branch information
zzqqw committed Feb 1, 2024
1 parent 77f3f1e commit 40360e6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# linux amd64
wget https://github.com/limitcool/palworld-admin/releases/download/v0.1.3/palworld-admin-v0.1.3-linux-amd64.tar.gz
tar -xzvf palworld-admin-v0.1.3-linux-amd64.tar.gz
chmod u+x
chmod u+x palworld-admin
./palworld-admin
```

Expand Down
3 changes: 0 additions & 3 deletions config.yaml

This file was deleted.

9 changes: 5 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ type SaveConfig struct {
BackupDirectory string `yaml:"BackupDirectory"` // Directory to retain game saves
}
type Config struct {
PalSavedPath string `yaml:"PalSavedPath"`
AdminPassword string `yaml:"AdminPassword"`
Port int `yaml:"Port"`
SaveConfig SaveConfig `yaml:"SaveConfig"`
PalSavedPath string `yaml:"PalSavedPath"`
AdminPassword string `yaml:"AdminPassword"`
Port int `yaml:"Port"`
SaveConfig SaveConfig `yaml:"SaveConfig"`
RestartCommand string `yaml:"RestartCommand"` // 新添加的字段,表示用户用于重启的命令
}

// 初始化并生成默认配置
Expand Down
2 changes: 1 addition & 1 deletion global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ var AppDir = util.AppDataDir("palworld-admin", false)
// var ConfigPath = filepath.Join(AppDir, "config")
var ConfigFile = filepath.Join(AppDir, "config.yaml")

const VERSION = "0.1.3"
const VERSION = "0.1.4"
34 changes: 33 additions & 1 deletion internal/handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package handlers

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"

"github.com/limitcool/palworld-admin/config"
"github.com/limitcool/palworld-admin/global"
sp "github.com/limitcool/palworld-admin/settings-parse"
"github.com/limitcool/palworld-admin/util"
Expand Down Expand Up @@ -50,12 +54,40 @@ func UpdateConfig(c *gin.Context) {
}
path := filepath.Join(global.Config.PalSavedPath, "Config", util.GetPath(), PalGameWorldSettingsName)
cfg.SaveTo(path)
if global.Config.RestartCommand != "" {
err = restartServer(global.Config)
}
// fmt.Printf("cfg.Sections(): %v\n", cfg.SectionStrings())
code.AutoResponse(c, nil, nil)
code.AutoResponse(c, nil, err)
}

type PalGameWorldSettings struct {
Settings struct {
OptionSettings string `ini:"OptionSettings"`
} `ini:"/Script/Pal.PalGameWorldSettings"`
}

func restartServer(config config.Config) error {
restartCmd := config.RestartCommand

var cmd *exec.Cmd

switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd", "/C", restartCmd)
case "linux", "darwin":
cmd = exec.Command("sh", "-c", restartCmd)
default:
return fmt.Errorf("unsupported operating system: %s", runtime.GOOS)
}

cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

err := cmd.Run()
if err != nil {
return err
}

return nil
}

0 comments on commit 40360e6

Please sign in to comment.