Skip to content

Commit

Permalink
Merge c8921a1 into 0f7a218
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoshinonyaruko authored Feb 8, 2024
2 parents 0f7a218 + c8921a1 commit 26b46b0
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 35 deletions.
9 changes: 3 additions & 6 deletions front/palworld-front/src/components/BotManage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ const getBot = async () => {
apiResponse.value = await response.text();
};
const getBotLink = async () => {
const response = await fetch('/api/getbotlink', {
method: 'POST',
credentials: 'include',
});
apiResponse.value = await response.text();
const getBotLink = () => {
window.location.href =
'https://qm.qq.com/cgi-bin/qm/qr?k=7aDJFyxLC6a450LMZfzG9Q1lwWy2veQj&jump_from=webapi&authKey=Im38dkervx6ISKg78NudQZgZnZv/O4bmb8f8CFrgMmJKY+MH5LsVNJ+KdJB5byY1';
};
const copyResponse = () => {
Expand Down
28 changes: 27 additions & 1 deletion front/palworld-front/src/pages/IndexView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@
<!-- 守护配置修改页面内容 -->
<div class="q-gutter-xs q-mt-md">
<div class="text-subtitle2">守护配置修改</div>

<!-- 保存按钮 -->
<q-btn
color="primary"
label="保存"
@click="saveConfig"
class="q-mt-md"
/>
<!-- 重启服务端按钮 -->
<q-btn
color="secondary"
label="重启服务端"
@click="restartServer"
class="q-mt-md"
/>
<!-- 文本输入框 -->
<q-input
filled
Expand Down Expand Up @@ -1043,6 +1056,19 @@ const redirectToSav = () => {
window.location.href = '/sav/index.html'; // 重定向到 /sav 路径
};
// // 难度选项
// const difficultyOptions = [
// { label: '简单', value: 'Eazy' },
// { label: '困难', value: 'Difficult' },
// ];
// // 死亡掉落选项
// const deathPenaltyOptions = [
// { label: '不掉落', value: 'None' },
// { label: '只掉落物品', value: 'Item' },
// { label: '掉落物品和装备', value: 'ItemAndEquipment' },
// { label: '掉落物品、装备和帕鲁', value: 'All' },
// ];
const config = ref({});
// 增加一个消息到数组
Expand Down
Empty file added go
Empty file.
Binary file modified mod/embeds/PalServerInject.exe
Binary file not shown.
Binary file modified mod/embeds/pal-plugin-loader.dll
Binary file not shown.
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ https://github.com/N00byKing/PalWorld-ServerInjector

本项目简化并自动了上方项目的dll注入,并内置了其注入器的可执行文件。

感谢腾讯云提供的折扣服务器

https://curl.qcloud.com/8K1KhiJd

【腾讯云】热卖套餐配置低至26元/月起,助您一键开服,即刻畅玩,立享优惠!

## 机器人
初步实现了Onebotv11标准的palworld机器人

Expand Down
9 changes: 9 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var memoryIssueDetected bool = false
var successReadGameWorldSettings bool = false
var manualServerShutdown bool = false // 存储是否手动关闭服务器的状态
var globalpid int = 0
var globalsubpid int = 0

// SetMemoryIssueDetected 设置内存问题检测标志
func SetMemoryIssueDetected(flag bool) {
Expand Down Expand Up @@ -43,3 +44,11 @@ func SetGlobalPid(pid int) {
func GetGlobalPid() int {
return globalpid
}

func SetGlobalSubPid(pid int) {
globalsubpid = pid
}

func GetGlobalSubPid() int {
return globalsubpid
}
22 changes: 13 additions & 9 deletions supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -71,13 +72,16 @@ func (s *Supervisor) hasDefunct() bool {
}

func (s *Supervisor) isServiceRunning() bool {
var cmd *exec.Cmd
pid := status.GetGlobalPid() // 假设这是从之前存储的地方获取PID
if pid == 0 {
return false
}

var cmd *exec.Cmd
if runtime.GOOS == "windows" {
cmd = exec.Command("tasklist")
cmd = exec.Command("tasklist", "/FI", fmt.Sprintf("PID eq %d", pid))
} else {
// Unix/Linux,假设'pgrep'可用
cmd = exec.Command("pgrep", "-f", s.Config.ProcessName+".sh")
cmd = exec.Command("ps", "-p", strconv.Itoa(pid))
}

var out bytes.Buffer
Expand All @@ -88,12 +92,12 @@ func (s *Supervisor) isServiceRunning() bool {
}

if runtime.GOOS == "windows" {
// Windows,检查任务列表输出中是否包含进程名
return strings.Contains(out.String(), s.Config.ProcessName)
// 检查输出中是否有行包含PID,这意味着进程存在
return strings.Contains(out.String(), strconv.Itoa(pid))
} else {
// Unix/Linux,如果`ps`命令找到了PID,它会返回成功
return true
}

// Unix/Linux,假如'pgrep'找到了进程,它会返回成功
return true
}

func (s *Supervisor) restartService() {
Expand Down
72 changes: 55 additions & 17 deletions sys/restart_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/hoshinonyaruko/palworld-go/config"
"github.com/hoshinonyaruko/palworld-go/status"
"gopkg.in/ini.v1"
)

// WindowsRestarter implements the Restarter interface for Windows systems.
Expand Down Expand Up @@ -79,12 +80,33 @@ func setConsoleTitleWindows(title string) error {
}

func KillProcess() error {
pid := status.GetGlobalPid() // 从全局变量获取PID
fmt.Printf("获取到当前服务端进程pid:%v", pid)
pid := status.GetGlobalPid()
subPid := status.GetGlobalSubPid()

fmt.Printf("获取到当前服务端进程pid:%v\n", pid)
if pid == 0 {
return fmt.Errorf("invalid PID: %d", pid)
}

// 结束主进程
err := killByPid(pid)
if err != nil {
return err
}

// 如果存在SUBPID,则结束SUBPID
if subPid != 0 {
fmt.Printf("获取到子进程pid:%v\n", subPid)
err := killByPid(subPid)
if err != nil {
return err
}
}

return nil
}

func killByPid(pid int) error {
var cmd *exec.Cmd

if runtime.GOOS == "windows" {
Expand All @@ -101,7 +123,13 @@ func KillProcess() error {
cmd.SysProcAttr.HideWindow = true
}

return cmd.Run()
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to kill process with PID %d: %v", pid, err)
}

fmt.Printf("成功结束进程 PID %d\n", pid)
return nil
}

// RunViaBatch 函数接受配置,程序路径和参数数组
Expand Down Expand Up @@ -136,29 +164,39 @@ func RunViaBatch(config config.Config, exepath string, args []string) error {
// 等待一段时间,确保C++程序有足够的时间写入PID到文件
time.Sleep(1 * time.Second)

// 构造pid.ini文件的路径
pidFilePath := filepath.Join(config.GamePath, "pid.ini")

// 读取pid.ini文件来获取PID
pidData, err := os.ReadFile(pidFilePath)
err = parsePidFile(config)
if err != nil {
log.Printf("failed to read pid.ini: %v", err)
log.Fatalf("Error parsing pid.ini: %v", err)
}
return nil
}

pidString := strings.TrimSpace(string(pidData))
parts := strings.Split(pidString, "=") // 用"="分割字符串
func parsePidFile(config config.Config) error {
pidFilePath := filepath.Join(config.GamePath, "pid.ini")

if len(parts) != 2 {
log.Fatalf("Unexpected PID format: %s", pidString)
// 使用ini库加载和解析文件
cfg, err := ini.Load(pidFilePath)
if err != nil {
return fmt.Errorf("failed to read pid.ini: %v", err)
}

pid, err := strconv.Atoi(parts[1]) // 将分割后的第二部分转换为int
// 读取PID
pidString := cfg.Section("").Key("PID").String()
pid, err := strconv.Atoi(pidString)
if err != nil {
log.Fatalf("Failed to convert PID string to int: %v", err)
return fmt.Errorf("failed to convert PID string to int: %v", err)
}

log.Printf("Game server started successfully with PID %d", pid)

status.SetGlobalPid(pid) // 存储转换后的PID

// 读取SUBPID
subPidString := cfg.Section("").Key("SUBPID").String()
subPid, err := strconv.Atoi(subPidString)
if err != nil {
return fmt.Errorf("failed to convert SUBPID string to int: %v", err)
}
log.Printf("Subprocess started successfully with SUBPID %d", subPid)
status.SetGlobalSubPid(subPid) // 存储转换后的SUBPID

return nil
}
9 changes: 7 additions & 2 deletions webui/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,16 +594,17 @@ func restartService(cfg config.Config, kill bool) {
log.Printf("use bat")
sys.RunViaBatch(cfg, exePath, args)
} else {
var cmd *exec.Cmd
if cfg.GameService && cfg.GameServiceName != "" {
cmd := exec.Command("sudo", "systemctl", "restart", cfg.GameServiceName)
cmd = exec.Command("sudo", "systemctl", "restart", cfg.GameServiceName)
// 启动进程
if err := cmd.Start(); err != nil {
log.Printf("Failed to restart game server: %v", err)
} else {
log.Printf("Game server restarted successfully")
}
} else {
cmd := exec.Command(exePath, args...)
cmd = exec.Command(exePath, args...)
cmd.Dir = cfg.GamePath // 设置工作目录为游戏路径

// 启动进程
Expand All @@ -613,6 +614,10 @@ func restartService(cfg config.Config, kill bool) {
log.Printf("Game server restarted successfully")
}
}

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

Expand Down

0 comments on commit 26b46b0

Please sign in to comment.