Skip to content
changnet edited this page Nov 9, 2026 · 13 revisions

编译

环境

  • Debian 12/Windows 10
  • GCC (>= C++17) 或者 Visual Studio 2022
  • Lua (>= 5.5)
  • MariaDB(MySQL)
  • MongoDB
  • PostgreSQL

注意

  • 功能仅在特定的系统下测试,其他发行版的Linux或者不同Windows版本可能需要适配。如果是更高版本,一般是运维脚本无法直接运行,主程序只要编译器和依赖库版本兼容都可运行
  • MariaDB、MongoDB、PostgreSQL可通过CMakeLists.txt控制开关,关闭后编译出的程序不再依赖对应的库,简化线上部署

下载

git clone https://github.com/changnet/MServer.git
cd MServer
git submodule update --init --recursive

安装编译环境及系统依赖(Linux)

  • 编译环境 指基础编译工具,如GCC、cmake等工具
  • 系统依赖 指一些基础的系统库依赖,如OpenSSL、MySQL等

线上部署一般使用Linux系统,故Linux下应当使用自己的包管理器(如apt)而不是vcpkg。否则运维在部署服务器时,依赖的库版本可能和开发用的版本不一致,出现兼容性bug

  1. 执行以下脚本可全部安装相关的工具
cd MServer/shell
./build_env.sh

注意:这个工程包含特定版本的第三方库,位于package目录下。执行此脚本可能会覆盖已有版本。如果安装其他版本,不一定兼容。此脚本在Debian 12上使用,其他系统无法直接运行。

  1. 设置运行环境 执行以下脚本设置程序的运行环境,比如core文件大小、最大文件句柄数量、core文件格式等待,这个设置不是必须的
cd MServer/shell
./build_env.sh set_sys_env
  1. 安装MariaDB 执行以下脚本会安装MariaDB,并且创建一个用于测试的数据库帐号及数据库
cd MServer/shell
./mariadb.sh install
  1. 安装MongoDB 执行以下脚本会安装MongoDB,并且创建一个用于测试的数据库帐号及数据库
cd MServer/shell
./mongodb.sh install
  1. 安装PostgreSQL 执行以下脚本会安装PostgreSQL,并且创建一个用于测试的数据库帐号及数据库
cd MServer/shell
./postgresql.sh install
  1. 编译主程序 如果上面的依赖均已正确编译,则下面的指令应该会在master/bin目录编译出来一个master二进制文件
cd MServer/shell
./build.sh

安装编译环境及系统依赖(Windows)

  1. 安装Visual Studio 2022(及以上版本) 请注意一定要安装英文语言包,下面的vcpkg需要用到。如果已经安装Visual Studio但没安装对应的语言包,重新执行Visual Studio installer,选择修改,勾上英文语言包即可。

  2. 安装vcpkg,然后用vcpkg安装依赖 这里以使用win下的cmd为例子,如果使用git bash,则命令可能会有些差异,尤其是在代理设置方面。

git clone https://github.com/microsoft/vcpkg
cd vcpkg
.\bootstrap-vcpkg.bat

安装完后,需要把vcpkg路径添加到系统Path,或者下面的指令直接指定vcpkg.exe路程

  1. 安装依赖包
cd MServer
vcpkg install --x-manifest-root=.\project --x-install-root=.\engine\vcpkg_win
vcpkg export --raw --x-manifest-root=.\project --x-install-root=.\engine\vcpkg_win --output-dir=.\engine\vcpkg_win_x64

本项目使用manifest模式,可以不指定安装目录x-install-root,默认是安装到vcpkg.json所在目录。安装完成后,不直接使用安装目录,而是导出依赖包到engine下的vcpkg_win_x64目录。这样可以直接复制整个依赖目录,未安装vcpkg的机器可直接使用该目录编译、开发。

每次导出,vcpkg生成的目录是带时间的(vcpkg-export-20250514-100442),所以每次都不一样。更新vcpkg的包时,需要修改CMakeList.txt中的路径

linux下libmariadb的头文件在mariadb目录下,但通过vcpkg安装的却是在mysql目录,因此需要进入刚导出的vcpkg目录,把installed\x64-windows\include\mysql拷贝为installed\x64-windows\include\mariadb

上面这些步骤,可通过shell/vcpkg.bat一键完成

  1. 用Visual Studio打开engine目录下的CMakeList.txt即可编译

  2. 当网络不好时,vcpkg可能无法下载依赖包,考虑使用代理。一般情况下,在设置系统http代理后,在power shell中运行的命令都会自动使用代理。如果使用Clash Verge等工具,在设置里开启系统代理即可。

如果还是不行。尝试需要手动设置,这里以sock5代理为例:

set http_proxy=socks5://127.0.0.1:10808
set https_proxy=socks5://127.0.0.1:10808

vcpkg使用的下载方式非常复杂(有时候使用powershell,有时候使用vcpkg.exe,有时候使用cmake来下载),上面设置的代理不一定会生效,这时候建议手动下载。如

https://github.com/mariadb-corporation/mariadb-connector-c/archive/7d304d26c787a3f0430624db977b615aba56e4bb.tar.gz: WinHttpSendRequest() failed: 12002

[DEBUG] Exiting after %s (%d us)


CMake Error at scripts/cmake/vcpkg_download_distfile.cmake:84 (message):

      Failed to download file with error: 1

这时候在浏览器打开上面的链接https://github.com/mariadb-corporation/mariadb-connector-c/archive/7d304d26c787a3f0430624db977b615aba56e4bb.tar.gz下载文件到vcpkg/downloads这个目录,然后重新执行上面的vcpkg install安装命令即可。

单元测试

cd MServer/shell
./start.sh test1 --filter=string --skip=perf

# --filter参数是可选

运行

服务器的配置格式为server\setting\setting_default.lua,复制此配置到server\var目录下,改为正确的参数。

服务器本身并不是一个纯粹的引擎,它本身带了一个例子,可以用以下命令来开启测试:

cd MServer/shell
./start.sh game1

同时还有一个机器人进程用于测试

cd MServer/shell
./start.sh bot1 --run test

Visual Studio调试

对于CMake工程,VS通常是能识别里面的工程,提供默认的选项的。在上面的菜单栏中,“选择启动项”里应该会有一个"master.exe"选项。

如果没有,则在VS的文件列表中,右键点击CMakeList.txt,选中“添加调试配置”可以初始化。

  1. 设置启动路径 默认情况下,启动配置的"type"是"default"类型,这个只能启动编译出来的exe。要想启动指定路径的,必须使用"launch"类型。
  2. 参数带空格 如果参数中带空格,必须用""括起来,但""本身是json的标识,所以需要用双引号。
  3. 添加后,在“选择启动项”应该就可以看到新的启动项了。但VS的显示比较奇怪,启动过的放上面一段菜单,没启动的放下面一段菜单。
  4. 如果CMakeList.txt中配置了安装,则启动项默认也会添加一个安装项master.exe (\u5B89\u88C5),这个会先执行编译,再执行安装,最后启动调试。

下面是一个常用的配置示例

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "master.exe (D:\\dev\\MServer\\server\\var\\master.exe)",
      "name": "master.exe (D:\\dev\\MServer\\server\\var\\master.exe)",
      "args": [
        "--node",
        "test1"
      ]
    },
    {
      "type": "launch",
      "name": "master test",
      "project": "CMakeLists.txt",
      "executable": "D:\\dev\\MServer\\server\\bin\\master.exe",
      "args": [
        "--node",
        "test1",
        "--filter",
        "\"http local server test\""
      ],
      "workingDirectory": "D:\\dev\\MServer\\server\\var"
    },
    {
      "type": "launch",
      "name": "master game",
      "project": "CMakeLists.txt",
      "executable": "D:\\dev\\MServer\\server\\bin\\master.exe",
      "args": [
        "--node",
        "game1"
      ],
      "workingDirectory": "D:\\dev\\MServer\\server\\var"
    },
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "master.exe (\u5B89\u88C5)",
      "name": "master.exe (\u5B89\u88C5)"
    }
  ]
}

常用操作备忘

更新submodule

git submodule foreach git pull origin master

删除submodule

# 要删除的路径为 engine/http-parser
git submodule deinit -f -- engine/deps/http-parser
rm -rf .git/modules/engine/http-parser
# 这个section并不是路径名,具体可以看.gitmodules里的名字
git config -f .git/config --remove-section submodule.master/deps/http-parser
git config -f .gitmodules --remove-section submodule.master/deps/http-parser

添加submodule

命令:git submodule add <url> <path>,例如

git submodule add https://github.com/changnet/websocket-parser.git engine/deps/websocket-parser

同步submodule到上游最新

git submodule update --init --remote --force -- engine/deps/pbc

Clone this wiki locally