Skip to content

Commit

Permalink
fix: save config before running the server once
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomartino committed Jan 28, 2024
1 parent 96fd00f commit 9520885
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dedicated-server/dedicated-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,25 @@ func (d *DedicatedServer) DownloadDedicatedServer() {
}
}

func (d *DedicatedServer) MonitorServerProcess() {
for {
time.Sleep(4 * time.Second)

// was killed via gui
if d.serverPid == 0 {
break
}

proc, err := utils.FindProcessByPid(d.serverPid)

if proc == nil || err != nil {
Print("Server seems to have stopped (crashed?)")
runtime.EventsEmit(ctx, "SET_SERVER_STATUS", "STOPPED")
break
}
}
}

func (d *DedicatedServer) Start() {
Print("Starting dedicated server...")
runtime.EventsEmit(ctx, "SET_SERVER_STATUS", "STARTING")
Expand Down Expand Up @@ -130,6 +149,8 @@ func (d *DedicatedServer) Start() {

Print("Server started")
runtime.EventsEmit(ctx, "SET_SERVER_STATUS", "STARTED")

go d.MonitorServerProcess()
}

func (d *DedicatedServer) Stop() {
Expand All @@ -154,6 +175,7 @@ func (d *DedicatedServer) Stop() {

Print("Server stopped")
runtime.EventsEmit(ctx, "SET_SERVER_STATUS", "STOPPED")
d.serverPid = 0
}

func (d *DedicatedServer) Restart() {
Expand Down Expand Up @@ -195,7 +217,12 @@ func (d *DedicatedServer) ReadConfig() string {
}

func (d *DedicatedServer) WriteConfig(configString string) {
if _, err := os.Stat(utils.Config.ServerConfigDir); os.IsNotExist(err) {
os.MkdirAll(utils.Config.ServerConfigDir, 0755)
}

if _, err := os.Stat(utils.Config.ServerConfigPath); os.IsNotExist(err) {

_, err := os.Create(utils.Config.ServerConfigPath)
if err != nil {
panic(err)
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/wailsjs/go/dedicatedserver/DedicatedServer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export function DownloadDedicatedServer():Promise<void>;

export function Init(arg1:context.Context):Promise<void>;

export function MonitorServerProcess():Promise<void>;

export function ReadConfig():Promise<string>;

export function Restart():Promise<void>;
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/wailsjs/go/dedicatedserver/DedicatedServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function Init(arg1) {
return window['go']['dedicatedserver']['DedicatedServer']['Init'](arg1);
}

export function MonitorServerProcess() {
return window['go']['dedicatedserver']['DedicatedServer']['MonitorServerProcess']();
}

export function ReadConfig() {
return window['go']['dedicatedserver']['DedicatedServer']['ReadConfig']();
}
Expand Down
17 changes: 17 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type AppConfig struct {
ServerPath string
ServerExe string
ServerDefaultConfigPath string
ServerConfigDir string
ServerConfigPath string
ServerProcessName string
AppId string
Expand All @@ -38,6 +39,7 @@ var Config AppConfig = AppConfig{
ServerPath: filepath.Join(GetCurrentDir(), "server"),
ServerExe: filepath.Join(GetCurrentDir(), "server", "PalServer.exe"),
ServerDefaultConfigPath: filepath.Join(GetCurrentDir(), "server", "DefaultPalWorldSettings.ini"),
ServerConfigDir: filepath.Join(GetCurrentDir(), "server", "Pal", "Saved", "Config", "WindowsServer"),
ServerConfigPath: filepath.Join(GetCurrentDir(), "server", "Pal", "Saved", "Config", "WindowsServer", "PalWorldSettings.ini"),
ServerProcessName: "PalServer-Win64-Test-Cmd.exe",
SteamCmdUrl: "https://steamcdn-a.akamaihd.net/client/installer/steamcmd.zip",
Expand Down Expand Up @@ -99,6 +101,21 @@ func FindProcessByName(processName string) (ps.Process, error) {
return nil, fmt.Errorf("process with name %s not found", processName)
}

func FindProcessByPid(pid int) (ps.Process, error) {
processes, err := ps.Processes()
if err != nil {
return nil, fmt.Errorf("error listing processes: %v", err)
}

for _, process := range processes {
if process.Pid() == pid {
return process, nil
}
}

return nil, fmt.Errorf("process with pid %d not found", pid)
}

func KillProcessByPid(pid int) error {
process, err := os.FindProcess(pid)
if err != nil {
Expand Down

0 comments on commit 9520885

Please sign in to comment.