Skip to content

Commit

Permalink
Merge ff271bc into 35f5d80
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored Feb 8, 2024
2 parents 35f5d80 + ff271bc commit 7373882
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 208 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
go backupTask.Schedule()

if !supervisor.isServiceRunning() {
supervisor.restartService()
sys.RestartService(jsonconfig)
} else {
fmt.Printf("当前服务端正常运行中,守护和内存助手已启动\n")
}
Expand Down
111 changes: 1 addition & 110 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ package main
import (
"bytes"
"fmt"
"log"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"

"github.com/hoshinonyaruko/palworld-go/config"
"github.com/hoshinonyaruko/palworld-go/mod"
"github.com/hoshinonyaruko/palworld-go/status"
"github.com/hoshinonyaruko/palworld-go/sys"
)
Expand Down Expand Up @@ -44,7 +40,7 @@ func (s *Supervisor) Start() {
}

if !s.isServiceRunning() {
s.restartService()
sys.RestartService(s.Config)
} else {
fmt.Println("当前正常运行中~")
}
Expand Down Expand Up @@ -100,108 +96,3 @@ func (s *Supervisor) isServiceRunning() bool {
return true
}
}

func (s *Supervisor) restartService() {
var exePath string
var args []string

if runtime.GOOS == "windows" {
if s.Config.CommunityServer {
exePath = filepath.Join(s.Config.SteamPath, "Steam.exe")
args = []string{"-applaunch", "2394010"}
} else if s.Config.UseDll {
err := mod.CheckAndWriteFiles(filepath.Join(s.Config.GamePath, "Pal", "Binaries", "Win64"))
if err != nil {
log.Printf("Failed to write files: %v", err)
return
}
exePath = filepath.Join(s.Config.GamePath, "Pal", "Binaries", "Win64", "PalServerInject.exe")
args = []string{
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", s.Config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", s.Config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", s.Config.WorldSettings.ServerPlayerMaxNum),
}
} else {
exePath = filepath.Join(s.Config.GamePath, "Pal", "Binaries", "Win64", "PalServer-Win64-Test-Cmd.exe")
//exePath = "\"" + exePath + "\""
args = []string{
"Pal",
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", s.Config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", s.Config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", s.Config.WorldSettings.ServerPlayerMaxNum),
}
}
} else {
// 对于非Windows系统的处理保持不变
exePath = filepath.Join(s.Config.GamePath, s.Config.ProcessName+".sh")
args = []string{
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", s.Config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", s.Config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", s.Config.WorldSettings.ServerPlayerMaxNum),
}
}

args = append(args, s.Config.ServerOptions...) // 添加GameWorldSettings参数

// 执行启动命令
log.Printf("启动命令: %s %s", exePath, strings.Join(args, " "))
if s.Config.UseDll && runtime.GOOS == "windows" {
log.Printf("use bat")
sys.RunViaBatch(s.Config, exePath, args)
log.Printf("use bat success")
} else {
cmd := exec.Command(exePath, args...)
cmd.Dir = s.Config.GamePath // 设置工作目录为游戏路径
if runtime.GOOS == "windows" {
// 仅在Windows平台上设置
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: 16,
}
}

// 启动进程
if err := cmd.Start(); err != nil {
log.Printf("Failed to restart game server: %v", err)
} else {
log.Printf("Game server restarted successfully")
}

// 获取并打印 PID
log.Printf("Game server started successfully with PID %d", cmd.Process.Pid)
status.SetGlobalPid(cmd.Process.Pid)
}

}

// func constructGameLaunchArguments(settings *GameWorldSettings) []string {
// var args []string

// sValue := reflect.ValueOf(settings).Elem()
// sType := sValue.Type()

// for i := 0; i < sType.NumField(); i++ {
// field := sType.Field(i)
// fieldValue := sValue.Field(i)

// jsonTag := firstToUpper(strings.Split(field.Tag.Get("json"), ",")[0]) // 获取json标签的第一部分,并将首字母转换为大写

// var arg string
// switch fieldValue.Kind() {
// case reflect.String:
// arg = fmt.Sprintf("%s=%s", jsonTag, fieldValue.String())
// case reflect.Float64:
// arg = fmt.Sprintf("%s=%s", jsonTag, strconv.FormatFloat(fieldValue.Float(), 'f', 6, 64))
// case reflect.Int:
// arg = fmt.Sprintf("%s=%d", jsonTag, fieldValue.Int())
// case reflect.Bool:
// arg = fmt.Sprintf("%s=%t", jsonTag, fieldValue.Bool())
// }

// args = append(args, arg)
// }

// return args
// }
39 changes: 39 additions & 0 deletions sys/restart_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package sys

import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"syscall"

"github.com/hoshinonyaruko/palworld-go/config"
"github.com/hoshinonyaruko/palworld-go/status"
)

// UnixRestarter implements the Restarter interface for Unix-like systems.
Expand Down Expand Up @@ -71,3 +75,38 @@ func KillProcess() error {
func RunViaBatch(config config.Config, exepath string, args []string) error {
return nil
}

// linux
func RestartService(config config.Config) {
var exePath string
var args []string

// 对于非Windows系统的处理保持不变
exePath = filepath.Join(config.GamePath, config.ProcessName+".sh")
args = []string{
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", config.WorldSettings.ServerPlayerMaxNum),
}

args = append(args, config.ServerOptions...) // 添加GameWorldSettings参数

// 执行启动命令
log.Printf("启动命令: %s %s", exePath, strings.Join(args, " "))

cmd := exec.Command(exePath, args...)
cmd.Dir = config.GamePath // 设置工作目录为游戏路径

// 启动进程
if err := cmd.Start(); err != nil {
log.Printf("Failed to restart game server: %v", err)
} else {
log.Printf("Game server restarted successfully")
}

// 获取并打印 PID
log.Printf("Game server started successfully with PID %d", cmd.Process.Pid)
status.SetGlobalPid(cmd.Process.Pid)

}
65 changes: 65 additions & 0 deletions sys/restart_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"unsafe"

"github.com/hoshinonyaruko/palworld-go/config"
"github.com/hoshinonyaruko/palworld-go/mod"
"github.com/hoshinonyaruko/palworld-go/status"
"gopkg.in/ini.v1"
)
Expand Down Expand Up @@ -200,3 +201,67 @@ func parsePidFile(config config.Config) error {

return nil
}

func RestartService(config config.Config) {
var exePath string
var args []string

if config.CommunityServer {
exePath = filepath.Join(config.SteamPath, "Steam.exe")
args = []string{"-applaunch", "2394010"}
} else if config.UseDll {
err := mod.CheckAndWriteFiles(filepath.Join(config.GamePath, "Pal", "Binaries", "Win64"))
if err != nil {
log.Printf("Failed to write files: %v", err)
return
}
exePath = filepath.Join(config.GamePath, "Pal", "Binaries", "Win64", "PalServerInject.exe")
args = []string{
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", config.WorldSettings.ServerPlayerMaxNum),
}
} else {
exePath = filepath.Join(config.GamePath, "Pal", "Binaries", "Win64", "PalServer-Win64-Test-Cmd.exe")
//exePath = "\"" + exePath + "\""
args = []string{
"Pal",
"-RconEnabled=True",
fmt.Sprintf("-AdminPassword=%s", config.WorldSettings.AdminPassword),
fmt.Sprintf("-port=%d", config.WorldSettings.PublicPort),
fmt.Sprintf("-players=%d", config.WorldSettings.ServerPlayerMaxNum),
}
}

args = append(args, config.ServerOptions...) // 添加GameWorldSettings参数

// 执行启动命令
log.Printf("启动命令: %s %s", exePath, strings.Join(args, " "))
if config.UseDll && runtime.GOOS == "windows" {
log.Printf("use bat")
RunViaBatch(config, exePath, args)
log.Printf("use bat success")
} else {
cmd := exec.Command(exePath, args...)
cmd.Dir = config.GamePath // 设置工作目录为游戏路径
if runtime.GOOS == "windows" {
// 仅在Windows平台上设置
cmd.SysProcAttr = &syscall.SysProcAttr{
CreationFlags: 16,
}
}

// 启动进程
if err := cmd.Start(); err != nil {
log.Printf("Failed to restart game server: %v", err)
} else {
log.Printf("Game server restarted successfully")
}

// 获取并打印 PID
log.Printf("Game server started successfully with PID %d", cmd.Process.Pid)
status.SetGlobalPid(cmd.Process.Pid)
}

}
Loading

0 comments on commit 7373882

Please sign in to comment.