mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-07-13 10:13:42 +00:00
Compare commits
120 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b209e70d62 | |||
| 5394122d77 | |||
| f48b8ff1a1 | |||
| de2a3795cc | |||
| 054016a099 | |||
| 8b57f6e35a | |||
| f21d3d0389 | |||
| 2ed92c4aa1 | |||
| fd7b11f436 | |||
| 6a94060970 | |||
| 51ccf31373 | |||
| a2cc629153 | |||
| ad414ec5c9 | |||
| 0a8e7d8e50 | |||
| bbd026e399 | |||
| 04bbdff6b7 | |||
| 2d8ce09b2c | |||
| 05251efc06 | |||
| f8d622352f | |||
| 6c1d02a425 | |||
| 38eeec39b4 | |||
| 696e080e1c | |||
| 98681254e6 | |||
| 420e6c3533 | |||
| 06f8ba5a0e | |||
| 9420d8a7a0 | |||
| dfa90da8af | |||
| 6e46d5aca9 | |||
| 817bd4b588 | |||
| 25391fa0c7 | |||
| 28270072d4 | |||
| 758d5b2c96 | |||
| 1970d97ea4 | |||
| d8526f0649 | |||
| 0621c0bf82 | |||
| 36547d1e9e | |||
| f06f31c2a0 | |||
| 46b92b4992 | |||
| 70e53c2a70 | |||
| cc35d83834 | |||
| b4f97a6da0 | |||
| 3d7db6d0bc | |||
| 1c3b1ecc76 | |||
| 303da19f48 | |||
| c455d4855c | |||
| 8df61fe44c | |||
| 7224e90c68 | |||
| fde5bc7fb6 | |||
| 22b63220c7 | |||
| 811ace1999 | |||
| 2cf083a7e4 | |||
| 28c43a51ee | |||
| 00f156cb86 | |||
| 67b8565a4d | |||
| ed8d8a6419 | |||
| 895058f85f | |||
| a0876ed58c | |||
| 4f69ca1ad0 | |||
| bc1628afeb | |||
| 019c5202ea | |||
| 2dd181d492 | |||
| ed03096cf5 | |||
| 88f1976668 | |||
| 58e2383b2e | |||
| 952631bb80 | |||
| 23af76dba1 | |||
| 5755ead9be | |||
| 450f0a6875 | |||
| 104737571c | |||
| d86efabb1a | |||
| b2f27c21be | |||
| b780a08f73 | |||
| cd4332b790 | |||
| 7a814ed35e | |||
| 9e0d02c6db | |||
| d0bb32ec63 | |||
| 5180c96e2b | |||
| dbfe4a4d11 | |||
| 4cb299061e | |||
| ef902a03f3 | |||
| c1e216957b | |||
| 39db1a5e42 | |||
| be498be661 | |||
| 0466ae55a4 | |||
| 6a43694c0f | |||
| fe06726d75 | |||
| 3c08e54471 | |||
| 710b15535e | |||
| 2caa74d913 | |||
| e3d9d11bbd | |||
| 09c9b24cbf | |||
| a450531b8a | |||
| ace7aaada7 | |||
| ca3314b416 | |||
| 7d97c3b560 | |||
| dd5cf1a4af | |||
| 9a0cdc6517 | |||
| 965935a0e6 | |||
| 96a0e4b6fb | |||
| b52677e585 | |||
| 95b417bb36 | |||
| 588c68ebe1 | |||
| 548b2512cc | |||
| 8ff94a57d7 | |||
| a44684f6e7 | |||
| 944b68c6d5 | |||
| 01268821dc | |||
| 17c571811a | |||
| fdb7c9ce71 | |||
| c391abcc93 | |||
| 144ccf14ec | |||
| 40b23cbbe6 | |||
| 7b458e3e27 | |||
| 69656f95db | |||
| 0f5c476d09 | |||
| 86b6aed350 | |||
| 81780294f8 | |||
| 7c1fb12625 | |||
| 9f892af997 | |||
| fd12ee672d |
@@ -7,12 +7,12 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
linux-build:
|
linux-build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
with:
|
with:
|
||||||
submodules: 'recursive'
|
submodules: "recursive"
|
||||||
|
|
||||||
- name: Install Dependencies
|
- name: Install Dependencies
|
||||||
env:
|
env:
|
||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
echo ${#beammp_sentry_url}
|
echo ${#beammp_sentry_url}
|
||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev
|
sudo apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev cmake g++-10
|
||||||
|
|
||||||
- name: Create Build Environment
|
- name: Create Build Environment
|
||||||
run: cmake -E make_directory ${{github.workspace}}/build-linux
|
run: cmake -E make_directory ${{github.workspace}}/build-linux
|
||||||
@@ -32,13 +32,47 @@ jobs:
|
|||||||
beammp_sentry_url: ${{ secrets.BEAMMP_SECRET_SENTRY_URL }}
|
beammp_sentry_url: ${{ secrets.BEAMMP_SECRET_SENTRY_URL }}
|
||||||
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=g++-10 -DBEAMMP_SECRET_SENTRY_URL="$beammp_sentry_url"
|
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_CXX_COMPILER=g++-10 -DBEAMMP_SECRET_SENTRY_URL="$beammp_sentry_url"
|
||||||
|
|
||||||
- name: Build
|
- name: Build Server
|
||||||
working-directory: ${{github.workspace}}/build-linux
|
working-directory: ${{github.workspace}}/build-linux
|
||||||
shell: bash
|
shell: bash
|
||||||
run: cmake --build . --config $BUILD_TYPE
|
run: cmake --build . --config $BUILD_TYPE -t BeamMP-Server --parallel
|
||||||
|
|
||||||
- name: Archive artifacts
|
- name: Build Tests
|
||||||
|
working-directory: ${{github.workspace}}/build-linux
|
||||||
|
shell: bash
|
||||||
|
run: cmake --build . --config $BUILD_TYPE -t BeamMP-Server-tests --parallel
|
||||||
|
|
||||||
|
- name: Archive server artifact
|
||||||
uses: actions/upload-artifact@v2
|
uses: actions/upload-artifact@v2
|
||||||
with:
|
with:
|
||||||
name: BeamMP-Server-linux
|
name: BeamMP-Server-linux
|
||||||
path: ${{github.workspace}}/build-linux/BeamMP-Server
|
path: ${{github.workspace}}/build-linux/BeamMP-Server
|
||||||
|
|
||||||
|
- name: Archive test artifact
|
||||||
|
uses: actions/upload-artifact@v2
|
||||||
|
with:
|
||||||
|
name: BeamMP-Server-linux-tests
|
||||||
|
path: ${{github.workspace}}/build-linux/BeamMP-Server-tests
|
||||||
|
|
||||||
|
run-tests:
|
||||||
|
needs: linux-build
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/download-artifact@master
|
||||||
|
with:
|
||||||
|
name: BeamMP-Server-linux-tests
|
||||||
|
path: ${{github.workspace}}
|
||||||
|
|
||||||
|
- name: Install Runtime Dependencies
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install -y liblua5.3 openssl
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
working-directory: ${{github.workspace}}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
chmod +x ./BeamMP-Server-tests
|
||||||
|
./BeamMP-Server-tests
|
||||||
|
|||||||
+33
@@ -1,3 +1,36 @@
|
|||||||
[submodule "deps/commandline"]
|
[submodule "deps/commandline"]
|
||||||
path = deps/commandline
|
path = deps/commandline
|
||||||
url = https://github.com/lionkor/commandline
|
url = https://github.com/lionkor/commandline
|
||||||
|
[submodule "deps/asio"]
|
||||||
|
path = deps/asio
|
||||||
|
url = https://github.com/chriskohlhoff/asio
|
||||||
|
[submodule "deps/rapidjson"]
|
||||||
|
path = deps/rapidjson
|
||||||
|
url = https://github.com/Tencent/rapidjson
|
||||||
|
[submodule "deps/toml11"]
|
||||||
|
path = deps/toml11
|
||||||
|
url = https://github.com/ToruNiina/toml11
|
||||||
|
[submodule "deps/sentry-native"]
|
||||||
|
path = deps/sentry-native
|
||||||
|
url = https://github.com/getsentry/sentry-native
|
||||||
|
[submodule "deps/sol2"]
|
||||||
|
path = deps/sol2
|
||||||
|
url = https://github.com/ThePhD/sol2
|
||||||
|
[submodule "deps/libzip"]
|
||||||
|
path = deps/libzip
|
||||||
|
url = https://github.com/nih-at/libzip
|
||||||
|
[submodule "deps/cpp-httplib"]
|
||||||
|
path = deps/cpp-httplib
|
||||||
|
url = https://github.com/yhirose/cpp-httplib
|
||||||
|
[submodule "deps/json"]
|
||||||
|
path = deps/json
|
||||||
|
url = https://github.com/nlohmann/json
|
||||||
|
[submodule "deps/fmt"]
|
||||||
|
path = deps/fmt
|
||||||
|
url = https://github.com/fmtlib/fmt
|
||||||
|
[submodule "deps/doctest"]
|
||||||
|
path = deps/doctest
|
||||||
|
url = https://github.com/doctest/doctest
|
||||||
|
[submodule "deps/dlhttp"]
|
||||||
|
path = deps/dlhttp
|
||||||
|
url = https://github.com/lionkor/dlhttp
|
||||||
|
|||||||
+131
-69
@@ -1,16 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.0)
|
# 3.4 is required for imported targets.
|
||||||
|
cmake_minimum_required(VERSION 3.4 FATAL_ERROR)
|
||||||
if (WIN32)
|
|
||||||
set(VCPKG_TARGET_TRIPLET "x64-windows-static")
|
|
||||||
elseif(UNIX)
|
|
||||||
set(VCPKG_TARGET_TRIPLET "x64-linux")
|
|
||||||
endif()
|
|
||||||
|
|
||||||
option(USE_VCPKG "USE_VCPKG" ON)
|
|
||||||
|
|
||||||
if(USE_VCPKG)
|
|
||||||
include(vcpkg.cmake)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
message(STATUS "You can find build instructions and a list of dependencies in the README at \
|
message(STATUS "You can find build instructions and a list of dependencies in the README at \
|
||||||
https://github.com/BeamMP/BeamMP-Server")
|
https://github.com/BeamMP/BeamMP-Server")
|
||||||
@@ -20,50 +9,66 @@ project(BeamMP-Server
|
|||||||
HOMEPAGE_URL https://beammp.com
|
HOMEPAGE_URL https://beammp.com
|
||||||
LANGUAGES CXX C)
|
LANGUAGES CXX C)
|
||||||
|
|
||||||
|
option(BEAMMP_RUN_GIT "Run git to make sure submodules are updated (leave this on unless you know what you're doing)" ON)
|
||||||
|
|
||||||
|
if (BEAMMP_RUN_GIT)
|
||||||
|
find_package(Git REQUIRED)
|
||||||
|
# Update submodules as needed
|
||||||
|
option(GIT_SUBMODULE "Check submodules during build" ON)
|
||||||
|
if(GIT_SUBMODULE)
|
||||||
|
message(STATUS "Submodule update")
|
||||||
|
execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
RESULT_VARIABLE GIT_SUBMOD_RESULT)
|
||||||
|
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
|
||||||
|
message(FATAL_ERROR "git submodule update --init --recursive failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|
||||||
set(HTTPLIB_REQUIRE_OPENSSL ON)
|
set(HTTPLIB_REQUIRE_OPENSSL ON)
|
||||||
|
set(SENTRY_BUILD_SHARED_LIBS OFF)
|
||||||
|
|
||||||
add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT)
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/asio/asio/include")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/rapidjson/include")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/websocketpp")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/commandline")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/sol2/include")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/cpp-httplib")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps/json/single_include")
|
||||||
|
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/deps")
|
||||||
|
|
||||||
|
add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT=1)
|
||||||
|
|
||||||
|
# ------------------------ APPLE ---------------------------------
|
||||||
if(APPLE)
|
if(APPLE)
|
||||||
set(LUA_INCLUDE_DIR /usr/local/Cellar/lua@5.3/5.3.6/include/lua5.3)
|
set(LUA_INCLUDE_DIR /usr/local/Cellar/lua@5.3/5.3.6/include/lua5.3)
|
||||||
set(LUA_LIBRARIES lua)
|
set(LUA_LIBRARIES lua)
|
||||||
include_directories(/usr/local/opt/openssl@1.1/include)
|
include_directories(/usr/local/opt/openssl@1.1/include)
|
||||||
link_directories(/usr/local/Cellar/lua@5.3/5.3.6/lib)
|
link_directories(/usr/local/Cellar/lua@5.3/5.3.6/lib)
|
||||||
link_directories(/usr/local/opt/openssl@1.1/lib)
|
link_directories(/usr/local/opt/openssl@1.1/lib)
|
||||||
endif()
|
# ------------------------ WINDOWS ---------------------------------
|
||||||
|
elseif (WIN32)
|
||||||
if (WIN32)
|
|
||||||
# this has to happen before sentry, so that crashpad on windows links with these settings.
|
# this has to happen before sentry, so that crashpad on windows links with these settings.
|
||||||
message(STATUS "MSVC -> forcing use of statically-linked runtime.")
|
message(STATUS "MSVC -> forcing use of statically-linked runtime.")
|
||||||
STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
|
STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
|
||||||
STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
|
STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
|
||||||
endif()
|
# ------------------------ LINUX ---------------------------------
|
||||||
|
|
||||||
if (MSVC)
|
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
|
|
||||||
endif ()
|
|
||||||
|
|
||||||
message(STATUS "Adding local source dependencies")
|
|
||||||
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
|
|
||||||
add_subdirectory(deps)
|
|
||||||
|
|
||||||
message(STATUS "Setting compiler flags")
|
|
||||||
if (WIN32)
|
|
||||||
|
|
||||||
#-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET=x64-windows-static
|
|
||||||
set(VcpkgRoot ${_VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET})
|
|
||||||
include_directories(${VcpkgRoot}/include)
|
|
||||||
link_directories(${VcpkgRoot}/lib)
|
|
||||||
elseif (UNIX)
|
elseif (UNIX)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -static-libstdc++")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic")
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2 -fno-builtin")
|
||||||
|
option(SANITIZE "Turns on thread and UB sanitizers" OFF)
|
||||||
if (SANITIZE)
|
if (SANITIZE)
|
||||||
message(STATUS "sanitize is ON")
|
message(STATUS "sanitize is ON")
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,thread")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined,thread")
|
||||||
endif (SANITIZE)
|
endif (SANITIZE)
|
||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
|
include_directories("include/sentry-native/include")
|
||||||
|
set(BUILD_SHARED_LIBS OFF)
|
||||||
|
# ------------------------ SENTRY ---------------------------------
|
||||||
message(STATUS "Checking for Sentry URL")
|
message(STATUS "Checking for Sentry URL")
|
||||||
# this is set by the build system.
|
# this is set by the build system.
|
||||||
# IMPORTANT: if you're building from source, just leave this empty
|
# IMPORTANT: if you're building from source, just leave this empty
|
||||||
@@ -73,17 +78,29 @@ if (NOT DEFINED BEAMMP_SECRET_SENTRY_URL)
|
|||||||
set(BEAMMP_SECRET_SENTRY_URL "")
|
set(BEAMMP_SECRET_SENTRY_URL "")
|
||||||
set(SENTRY_BACKEND none)
|
set(SENTRY_BACKEND none)
|
||||||
else()
|
else()
|
||||||
string(LENGTH ${BEAMMP_SECRET_SENTRY_URL} URL_LEN)
|
|
||||||
message(STATUS "Sentry URL is length ${URL_LEN}")
|
|
||||||
set(SENTRY_BACKEND breakpad)
|
set(SENTRY_BACKEND breakpad)
|
||||||
endif()
|
endif()
|
||||||
|
add_subdirectory("deps/sentry-native")
|
||||||
|
|
||||||
|
# ------------------------ C++ SETUP ---------------------------------
|
||||||
set(CMAKE_CXX_STANDARD 17)
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
if (MSVC)
|
||||||
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
|
||||||
|
endif ()
|
||||||
|
|
||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
|
# ------------------------ DEPENDENCIES ------------------------------
|
||||||
|
message(STATUS "Adding local source dependencies")
|
||||||
|
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
|
||||||
|
add_subdirectory(deps)
|
||||||
|
|
||||||
add_executable(BeamMP-Server
|
# ------------------------ VARIABLES ---------------------------------
|
||||||
src/main.cpp
|
|
||||||
|
include(FindLua)
|
||||||
|
include(FindOpenSSL)
|
||||||
|
include(FindThreads)
|
||||||
|
include(FindZLIB)
|
||||||
|
|
||||||
|
set(BeamMP_Sources
|
||||||
include/TConsole.h src/TConsole.cpp
|
include/TConsole.h src/TConsole.cpp
|
||||||
include/TServer.h src/TServer.cpp
|
include/TServer.h src/TServer.cpp
|
||||||
include/Compat.h src/Compat.cpp
|
include/Compat.h src/Compat.cpp
|
||||||
@@ -103,39 +120,84 @@ add_executable(BeamMP-Server
|
|||||||
include/TScopedTimer.h src/TScopedTimer.cpp
|
include/TScopedTimer.h src/TScopedTimer.cpp
|
||||||
include/SignalHandling.h src/SignalHandling.cpp
|
include/SignalHandling.h src/SignalHandling.cpp
|
||||||
include/ArgsParser.h src/ArgsParser.cpp
|
include/ArgsParser.h src/ArgsParser.cpp
|
||||||
include/Environment.h)
|
include/TPluginMonitor.h src/TPluginMonitor.cpp
|
||||||
|
include/Environment.h
|
||||||
|
)
|
||||||
|
|
||||||
target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}")
|
set(BeamMP_Includes
|
||||||
include_directories(BeamMP-Server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
"${CMAKE_CURRENT_SOURCE_DIR}/include"
|
||||||
|
"${CMAKE_CURRENT_SOURCE_DIR}/commandline"
|
||||||
include(FindThreads)
|
|
||||||
|
|
||||||
find_package(OpenSSL REQUIRED)
|
|
||||||
find_package(ZLIB REQUIRED)
|
|
||||||
find_package(CURL CONFIG REQUIRED)
|
|
||||||
find_package(Lua REQUIRED 5.3)
|
|
||||||
find_package(nlohmann_json CONFIG REQUIRED)
|
|
||||||
find_package(RapidJSON CONFIG REQUIRED)
|
|
||||||
find_package(sentry CONFIG REQUIRED)
|
|
||||||
find_package(sol2 CONFIG REQUIRED)
|
|
||||||
find_package(toml11 CONFIG REQUIRED)
|
|
||||||
find_path(CPP_HTTPLIB_INCLUDE_DIRS "httplib.h")
|
|
||||||
|
|
||||||
target_include_directories(BeamMP-Server PRIVATE
|
|
||||||
${CPP_HTTPLIB_INCLUDE_DIRS}
|
|
||||||
${LUA_INCLUDE_DIR}
|
${LUA_INCLUDE_DIR}
|
||||||
|
${CURL_INCLUDE_DIRS}
|
||||||
|
"include/tomlplusplus"
|
||||||
|
"include/sentry-native/include"
|
||||||
|
"include/curl/include"
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(BeamMP-Server PRIVATE OpenSSL::SSL OpenSSL::Crypto
|
set(BeamMP_Definitions
|
||||||
ZLIB::ZLIB
|
SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}"
|
||||||
CURL::libcurl
|
)
|
||||||
${LUA_LIBRARIES}
|
|
||||||
Threads::Threads
|
set(BeamMP_Libraries
|
||||||
commandline
|
doctest::doctest
|
||||||
nlohmann_json::nlohmann_json
|
OpenSSL::SSL
|
||||||
rapidjson
|
OpenSSL::Crypto
|
||||||
sentry::sentry
|
|
||||||
sol2::sol2
|
sol2::sol2
|
||||||
toml11::toml11
|
fmt::fmt
|
||||||
|
Threads::Threads
|
||||||
|
ZLIB::ZLIB
|
||||||
|
${LUA_LIBRARIES}
|
||||||
|
commandline
|
||||||
|
sentry
|
||||||
|
dlhttp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (WIN32)
|
||||||
|
set(BeamMP_PlatformLibs wsock32 ws2_32)
|
||||||
|
endif ()
|
||||||
|
|
||||||
|
# ------------------------ BEAMMP SERVER -----------------------------
|
||||||
|
|
||||||
|
add_executable(BeamMP-Server
|
||||||
|
src/main.cpp
|
||||||
|
${BeamMP_Sources}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(BeamMP-Server PRIVATE
|
||||||
|
${BeamMP_Definitions}
|
||||||
|
DOCTEST_CONFIG_DISABLE
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(BeamMP-Server PUBLIC
|
||||||
|
${BeamMP_Includes}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(BeamMP-Server
|
||||||
|
${BeamMP_Libraries}
|
||||||
|
${BeamMP_PlatformLibs}
|
||||||
|
)
|
||||||
|
|
||||||
|
# ------------------------ BEAMMP SERVER TESTS -----------------------
|
||||||
|
|
||||||
|
option(BUILD_TESTS "Build BeamMP-Server tests" ON)
|
||||||
|
|
||||||
|
if(BUILD_TESTS)
|
||||||
|
add_executable(BeamMP-Server-tests
|
||||||
|
test/test_main.cpp
|
||||||
|
${BeamMP_Sources}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(BeamMP-Server-tests PRIVATE
|
||||||
|
${BeamMP_Definitions}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(BeamMP-Server-tests PUBLIC
|
||||||
|
${BeamMP_Includes}
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(BeamMP-Server-tests
|
||||||
|
${BeamMP_Libraries}
|
||||||
|
${BeamMP_PlatformLibs}
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,19 @@
|
|||||||
|
|
||||||
|
# v3.1.0
|
||||||
|
|
||||||
|
- ADDED Tab autocomplete in console, smart tab autocomplete (understands lua tables and types) in the lua console
|
||||||
|
- ADDED lua debug facilities (type `:help` when attached to lua via `lua`)
|
||||||
|
- ADDED MP.JsonEncode() and MP.JsonDecode(), which turn lua tables into json and vice-versa
|
||||||
|
- ADDED FS.ListFiles and FS.ListDirectories
|
||||||
|
- ADDED onFileChanged event, triggered when a server plugin file changes
|
||||||
|
- FIXED `ip` in MP.GetIdentifiers
|
||||||
|
- FIXED issue with client->server events which contain ':'
|
||||||
|
- FIXED a fatal exception on LuaEngine startup if Resources/Server is a symlink
|
||||||
|
- FIXED onInit not being called on hot-reload
|
||||||
|
- FIXED incorrect timing calculation of Lua EventTimer loop
|
||||||
|
- FIXED bug which caused hot-reload not to report syntax errors
|
||||||
|
- FIXED missing error messages on some event handler calls
|
||||||
|
|
||||||
# v3.0.2
|
# v3.0.2
|
||||||
|
|
||||||
- ADDED Periodic update message if a new server is released
|
- ADDED Periodic update message if a new server is released
|
||||||
@@ -7,6 +23,7 @@
|
|||||||
- FIXED `MP.CreateEventTimer` filling up the queue (see <https://wiki.beammp.com/en/Scripting/new-lua-scripting#mpcreateeventtimerevent_name-string-interval_ms-number-strategy-number-since-v302>)
|
- FIXED `MP.CreateEventTimer` filling up the queue (see <https://wiki.beammp.com/en/Scripting/new-lua-scripting#mpcreateeventtimerevent_name-string-interval_ms-number-strategy-number-since-v302>)
|
||||||
- FIXED `MP.TriggerClientEvent` not kicking the client if it failed
|
- FIXED `MP.TriggerClientEvent` not kicking the client if it failed
|
||||||
- FIXED Lua result queue handling not checking all results
|
- FIXED Lua result queue handling not checking all results
|
||||||
|
- FIXED bug which caused ServerConfig.toml to generate incorrectly
|
||||||
|
|
||||||
# v3.0.1
|
# v3.0.1
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ On windows, use git-bash for these commands. On Linux, these should work in your
|
|||||||
1. Make sure you have all [prerequisites](#prerequisites) installed
|
1. Make sure you have all [prerequisites](#prerequisites) installed
|
||||||
2. Clone the repository in a location of your choice with `git clone --recurse-submodules https://github.com/BeamMP/BeamMP-Server`.
|
2. Clone the repository in a location of your choice with `git clone --recurse-submodules https://github.com/BeamMP/BeamMP-Server`.
|
||||||
3. Ensure that all submodules are initialized by running `git submodule update --init --recursive`. Then change into the cloned directory by running `cd BeamMP-Server`.
|
3. Ensure that all submodules are initialized by running `git submodule update --init --recursive`. Then change into the cloned directory by running `cd BeamMP-Server`.
|
||||||
4. Checkout the branch of the release you want to compile (`master` is often unstable), for example `git checkout tags/v2.3.3` for version 2.3.3. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags).
|
4. Checkout the branch of the release you want to compile (`master` is often unstable), for example `git checkout tags/v3.0.1` for version 3.0.1. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags).
|
||||||
5. Run `cmake . -DCMAKE_BUILD_TYPE=Release` (with `.`)
|
5. Run `cmake . -DCMAKE_BUILD_TYPE=Release` (with `.`)
|
||||||
6. Run `make`
|
6. Run `make`
|
||||||
7. You will now have a `BeamMP-Server` file in your directory, which is executable with `./BeamMP-Server` (`.\BeamMP-Server.exe` for windows). Follow the (windows or linux, doesnt matter) instructions on the [wiki](https://wiki.beammp.com/en/home/Server_Mod) for further setup after installation (which we just did), such as port-forwarding and getting a key to actually run the server.
|
7. You will now have a `BeamMP-Server` file in your directory, which is executable with `./BeamMP-Server` (`.\BeamMP-Server.exe` for windows). Follow the (windows or linux, doesnt matter) instructions on the [wiki](https://wiki.beammp.com/en/home/Server_Mod) for further setup after installation (which we just did), such as port-forwarding and getting a key to actually run the server.
|
||||||
|
|||||||
Vendored
+10
@@ -2,3 +2,13 @@ include_directories("${PROJECT_SOURCE_DIR}/deps")
|
|||||||
include_directories("${PROJECT_SOURCE_DIR}/deps/commandline")
|
include_directories("${PROJECT_SOURCE_DIR}/deps/commandline")
|
||||||
|
|
||||||
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/commandline")
|
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/commandline")
|
||||||
|
if (NOT TARGET fmt)
|
||||||
|
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/fmt")
|
||||||
|
endif()
|
||||||
|
if (NOT TARGET sol2)
|
||||||
|
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/sol2")
|
||||||
|
endif()
|
||||||
|
if (NOT TARGET doctest)
|
||||||
|
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/doctest")
|
||||||
|
endif()
|
||||||
|
add_subdirectory("${PROJECT_SOURCE_DIR}/deps/dlhttp")
|
||||||
|
|||||||
+1
Submodule deps/asio added at d038fb3c2f
+1
Submodule deps/cpp-httplib added at 47044c05a8
+1
Submodule deps/dlhttp added at f507c6c4e6
+1
Submodule deps/doctest added at 7b98851331
+1
Submodule deps/fmt added at ce246aaf74
+1
Submodule deps/json added at ede6667858
+1
Submodule deps/libzip added at 76df02f86b
+1
Submodule deps/rapidjson added at 00dbcf2c6e
+1
Submodule deps/sentry-native added at 90966cc102
+1
Submodule deps/sol2 added at c068aefbed
+1
Submodule deps/toml11 added at 1400dd223f
+97
-42
@@ -8,12 +8,18 @@ extern TSentry Sentry;
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <fmt/format.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <shared_mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
#include <filesystem>
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
#include "Compat.h"
|
#include "Compat.h"
|
||||||
|
|
||||||
#include "TConsole.h"
|
#include "TConsole.h"
|
||||||
@@ -49,7 +55,9 @@ public:
|
|||||||
int MaxCars { 1 };
|
int MaxCars { 1 };
|
||||||
bool DebugModeEnabled { false };
|
bool DebugModeEnabled { false };
|
||||||
int Port { 30814 };
|
int Port { 30814 };
|
||||||
|
int DownloadThreads { 4 };
|
||||||
std::string CustomIP {};
|
std::string CustomIP {};
|
||||||
|
bool LogChat { true };
|
||||||
bool SendErrors { true };
|
bool SendErrors { true };
|
||||||
bool SendErrorsMessageEnabled { true };
|
bool SendErrorsMessageEnabled { true };
|
||||||
int HTTPServerPort { 8080 };
|
int HTTPServerPort { 8080 };
|
||||||
@@ -90,6 +98,8 @@ public:
|
|||||||
static void CheckForUpdates();
|
static void CheckForUpdates();
|
||||||
static std::array<uint8_t, 3> VersionStrToInts(const std::string& str);
|
static std::array<uint8_t, 3> VersionStrToInts(const std::string& str);
|
||||||
static bool IsOutdated(const Version& Current, const Version& Newest);
|
static bool IsOutdated(const Version& Current, const Version& Newest);
|
||||||
|
static bool IsShuttingDown();
|
||||||
|
static void SleepSafeSeconds(size_t Seconds);
|
||||||
|
|
||||||
static void InitializeConsole() {
|
static void InitializeConsole() {
|
||||||
if (!mConsole) {
|
if (!mConsole) {
|
||||||
@@ -115,99 +125,144 @@ public:
|
|||||||
static void SetSubsystemStatus(const std::string& Subsystem, Status status);
|
static void SetSubsystemStatus(const std::string& Subsystem, Status status);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static void SetShutdown(bool Val);
|
||||||
|
|
||||||
static inline SystemStatusMap mSystemStatusMap {};
|
static inline SystemStatusMap mSystemStatusMap {};
|
||||||
static inline std::mutex mSystemStatusMapMutex {};
|
static inline std::mutex mSystemStatusMapMutex {};
|
||||||
static inline std::string mPPS;
|
static inline std::string mPPS;
|
||||||
static inline std::unique_ptr<TConsole> mConsole;
|
static inline std::unique_ptr<TConsole> mConsole;
|
||||||
|
static inline std::shared_mutex mShutdownMtx {};
|
||||||
|
static inline bool mShutdown { false };
|
||||||
static inline std::mutex mShutdownHandlersMutex {};
|
static inline std::mutex mShutdownHandlersMutex {};
|
||||||
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
|
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
|
||||||
|
|
||||||
static inline Version mVersion { 3, 0, 2 };
|
static inline Version mVersion { 3, 1, 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string ThreadName(bool DebugModeOverride = false);
|
std::string ThreadName(bool DebugModeOverride = false);
|
||||||
void RegisterThread(const std::string& str);
|
void RegisterThread(const std::string& str);
|
||||||
#define RegisterThreadAuto() RegisterThread(__func__)
|
#define RegisterThreadAuto() RegisterThread(__func__)
|
||||||
|
|
||||||
#define KB 1024
|
#define KB 1024llu
|
||||||
#define MB (KB * 1024)
|
#define MB (KB * 1024llu)
|
||||||
|
#define GB (MB * 1024llu)
|
||||||
#define SSU_UNRAW SECRET_SENTRY_URL
|
#define SSU_UNRAW SECRET_SENTRY_URL
|
||||||
|
|
||||||
#define _file_basename std::filesystem::path(__FILE__).filename().string()
|
#define _file_basename std::filesystem::path(__FILE__).filename().string()
|
||||||
#define _line std::to_string(__LINE__)
|
#define _line std::to_string(__LINE__)
|
||||||
#define _in_lambda (std::string(__func__) == "operator()")
|
#define _in_lambda (std::string(__func__) == "operator()")
|
||||||
|
|
||||||
// we would like the full function signature 'void a::foo() const'
|
// for those times when you just need to ignore something :^)
|
||||||
// on windows this is __FUNCSIG__, on GCC it's __PRETTY_FUNCTION__,
|
// explicity disables a [[nodiscard]] warning
|
||||||
// feel free to add more
|
#define beammp_ignore(x) (void)x
|
||||||
#if defined(WIN32)
|
|
||||||
#define _function_name std::string(__FUNCSIG__)
|
|
||||||
#elif defined(__unix) || defined(__unix__)
|
|
||||||
#define _function_name std::string(__PRETTY_FUNCTION__)
|
|
||||||
#else
|
|
||||||
#define _function_name std::string(__func__)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(DEBUG)
|
// clang-format off
|
||||||
|
#ifdef DOCTEST_CONFIG_DISABLE
|
||||||
|
|
||||||
// if this is defined, we will show the full function signature infront of
|
// we would like the full function signature 'void a::foo() const'
|
||||||
// each info/debug/warn... call instead of the 'filename:line' format.
|
// on windows this is __FUNCSIG__, on GCC it's __PRETTY_FUNCTION__,
|
||||||
#if defined(BMP_FULL_FUNCTION_NAMES)
|
// feel free to add more
|
||||||
#define _this_location (ThreadName() + _function_name + " ")
|
#if defined(WIN32)
|
||||||
#else
|
#define _function_name std::string(__FUNCSIG__)
|
||||||
#define _this_location (ThreadName() + _file_basename + ":" + _line + " ")
|
#elif defined(__unix) || defined(__unix__)
|
||||||
#endif
|
#define _function_name std::string(__PRETTY_FUNCTION__)
|
||||||
#define SU_RAW SSU_UNRAW
|
#else
|
||||||
|
#define _function_name std::string(__func__)
|
||||||
|
#endif
|
||||||
|
|
||||||
#else // !defined(DEBUG)
|
#ifndef NDEBUG
|
||||||
|
#define DEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SU_RAW RAWIFY(SSU_UNRAW)
|
#if defined(DEBUG)
|
||||||
#define _this_location (ThreadName())
|
|
||||||
|
|
||||||
#endif // defined(DEBUG)
|
// if this is defined, we will show the full function signature infront of
|
||||||
|
// each info/debug/warn... call instead of the 'filename:line' format.
|
||||||
|
#if defined(BMP_FULL_FUNCTION_NAMES)
|
||||||
|
#define _this_location (ThreadName() + _function_name + " ")
|
||||||
|
#else
|
||||||
|
#define _this_location (ThreadName() + _file_basename + ":" + _line + " ")
|
||||||
|
#endif
|
||||||
|
|
||||||
#define beammp_warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
|
#endif // defined(DEBUG)
|
||||||
#define beammp_info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
|
|
||||||
#define beammp_error(x) \
|
#define beammp_warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
|
||||||
|
#define beammp_info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
|
||||||
|
#define beammp_error(x) \
|
||||||
do { \
|
do { \
|
||||||
Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \
|
||||||
Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \
|
Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \
|
||||||
} while (false)
|
} while (false)
|
||||||
#define beammp_lua_error(x) \
|
#define beammp_lua_error(x) \
|
||||||
do { \
|
do { \
|
||||||
Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \
|
||||||
} while (false)
|
} while (false)
|
||||||
#define beammp_lua_warn(x) \
|
#define beammp_lua_warn(x) \
|
||||||
do { \
|
do { \
|
||||||
Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \
|
||||||
} while (false)
|
} while (false)
|
||||||
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
|
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
|
||||||
#define beammp_debug(x) \
|
#define beammp_debug(x) \
|
||||||
do { \
|
do { \
|
||||||
if (Application::Settings.DebugModeEnabled) { \
|
if (Application::Settings.DebugModeEnabled) { \
|
||||||
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
|
||||||
} \
|
} \
|
||||||
} while (false)
|
} while (false)
|
||||||
#define beammp_event(x) \
|
#define beammp_event(x) \
|
||||||
do { \
|
do { \
|
||||||
if (Application::Settings.DebugModeEnabled) { \
|
if (Application::Settings.DebugModeEnabled) { \
|
||||||
Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \
|
||||||
} \
|
} \
|
||||||
} while (false)
|
} while (false)
|
||||||
// for those times when you just need to ignore something :^)
|
// trace() is a debug-build debug()
|
||||||
// explicity disables a [[nodiscard]] warning
|
#if defined(DEBUG)
|
||||||
#define beammp_ignore(x) (void)x
|
#define beammp_trace(x) \
|
||||||
// trace() is a debug-build debug()
|
|
||||||
#if defined(DEBUG)
|
|
||||||
#define beammp_trace(x) \
|
|
||||||
do { \
|
do { \
|
||||||
if (Application::Settings.DebugModeEnabled) { \
|
if (Application::Settings.DebugModeEnabled) { \
|
||||||
Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \
|
Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \
|
||||||
} \
|
} \
|
||||||
} while (false)
|
} while (false)
|
||||||
|
#else
|
||||||
|
#define beammp_trace(x)
|
||||||
|
#endif // defined(DEBUG)
|
||||||
|
|
||||||
|
#define beammp_errorf(...) beammp_error(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_infof(...) beammp_info(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_debugf(...) beammp_debug(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_warnf(...) beammp_warn(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_tracef(...) beammp_trace(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_lua_errorf(...) beammp_lua_error(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_lua_warnf(...) beammp_lua_warn(fmt::format(__VA_ARGS__))
|
||||||
|
|
||||||
|
#else // DOCTEST_CONFIG_DISABLE
|
||||||
|
|
||||||
|
#define beammp_error(x) /* x */
|
||||||
|
#define beammp_lua_error(x) /* x */
|
||||||
|
#define beammp_warn(x) /* x */
|
||||||
|
#define beammp_lua_warn(x) /* x */
|
||||||
|
#define beammp_info(x) /* x */
|
||||||
|
#define beammp_event(x) /* x */
|
||||||
|
#define beammp_debug(x) /* x */
|
||||||
|
#define beammp_trace(x) /* x */
|
||||||
|
#define luaprint(x) /* x */
|
||||||
|
#define beammp_errorf(...) beammp_error(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_infof(...) beammp_info(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_warnf(...) beammp_warn(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_debugf(...) beammp_debug(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_tracef(...) beammp_trace(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_lua_errorf(...) beammp_lua_error(fmt::format(__VA_ARGS__))
|
||||||
|
#define beammp_lua_warnf(...) beammp_lua_warn(fmt::format(__VA_ARGS__))
|
||||||
|
|
||||||
|
#endif // DOCTEST_CONFIG_DISABLE
|
||||||
|
|
||||||
|
#if defined(DEBUG)
|
||||||
|
#define SU_RAW SSU_UNRAW
|
||||||
#else
|
#else
|
||||||
#define beammp_trace(x)
|
#define SU_RAW RAWIFY(SSU_UNRAW)
|
||||||
#endif // defined(DEBUG)
|
#define _this_location (ThreadName())
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
void LogChatMessage(const std::string& name, int id, const std::string& msg);
|
void LogChatMessage(const std::string& name, int id, const std::string& msg);
|
||||||
|
|
||||||
|
|||||||
+8
-3
@@ -6,10 +6,10 @@
|
|||||||
|
|
||||||
#ifdef BEAMMP_LINUX
|
#ifdef BEAMMP_LINUX
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
|
||||||
using SOCKET = int;
|
using SOCKET = int;
|
||||||
using DWORD = unsigned long;
|
using DWORD = unsigned long;
|
||||||
using PDWORD = unsigned long*;
|
using PDWORD = unsigned long*;
|
||||||
@@ -25,10 +25,10 @@ inline void CloseSocketProper(int TheSocket) {
|
|||||||
|
|
||||||
#ifdef BEAMMP_APPLE
|
#ifdef BEAMMP_APPLE
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
|
||||||
using SOCKET = int;
|
using SOCKET = int;
|
||||||
using DWORD = unsigned long;
|
using DWORD = unsigned long;
|
||||||
using PDWORD = unsigned long*;
|
using PDWORD = unsigned long*;
|
||||||
@@ -48,6 +48,11 @@ inline void CloseSocketProper(int TheSocket) {
|
|||||||
inline void CloseSocketProper(SOCKET TheSocket) {
|
inline void CloseSocketProper(SOCKET TheSocket) {
|
||||||
shutdown(TheSocket, 2); // 2 == SD_BOTH
|
shutdown(TheSocket, 2); // 2 == SD_BOTH
|
||||||
closesocket(TheSocket);
|
closesocket(TheSocket);
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif // WIN32
|
#endif // WIN32
|
||||||
|
|
||||||
|
#ifdef INVALID_SOCKET
|
||||||
|
static inline constexpr int BEAMMP_INVALID_SOCKET = INVALID_SOCKET;
|
||||||
|
#else
|
||||||
|
static inline constexpr int BEAMMP_INVALID_SOCKET = -1;
|
||||||
|
#endif
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
#include <Common.h>
|
#include <Common.h>
|
||||||
#include <IThreaded.h>
|
#include <IThreaded.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <openssl/pem.h>
|
|
||||||
#include <openssl/x509.h>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -19,10 +17,6 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
namespace Crypto {
|
|
||||||
constexpr size_t RSA_DEFAULT_KEYLENGTH { 2048 };
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr);
|
std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr);
|
||||||
std::string POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const httplib::Headers& headers = {});
|
std::string POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const httplib::Headers& headers = {});
|
||||||
@@ -32,13 +26,9 @@ namespace Status {
|
|||||||
const std::string ErrorString = "-1";
|
const std::string ErrorString = "-1";
|
||||||
|
|
||||||
namespace Server {
|
namespace Server {
|
||||||
void SetupEnvironment();
|
|
||||||
// todo: Add non TLS Server Instance, this one is TLS only
|
|
||||||
class THttpServerInstance {
|
class THttpServerInstance {
|
||||||
public:
|
public:
|
||||||
THttpServerInstance();
|
THttpServerInstance();
|
||||||
static fs::path KeyFilePath;
|
|
||||||
static fs::path CertFilePath;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void operator()();
|
void operator()();
|
||||||
@@ -46,15 +36,5 @@ namespace Server {
|
|||||||
private:
|
private:
|
||||||
std::thread mThread;
|
std::thread mThread;
|
||||||
};
|
};
|
||||||
// todo: all of these functions are likely unsafe,
|
|
||||||
// todo: replace with something that's managed by a domain specific crypto library
|
|
||||||
class Tx509KeypairGenerator {
|
|
||||||
public:
|
|
||||||
static long GenerateRandomId();
|
|
||||||
static bool EnsureTLSConfigExists();
|
|
||||||
static X509* GenerateCertificate(EVP_PKEY& pkey);
|
|
||||||
static EVP_PKEY* GenerateKey();
|
|
||||||
static void GenerateAndWriteToDisk(const fs::path& KeyFilePath, const fs::path& CertFilePath);
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -12,7 +12,8 @@ namespace MP {
|
|||||||
|
|
||||||
std::string GetOSName();
|
std::string GetOSName();
|
||||||
std::tuple<int, int, int> GetServerVersion();
|
std::tuple<int, int, int> GetServerVersion();
|
||||||
bool TriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data);
|
bool TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& Data);
|
||||||
|
bool TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data);
|
||||||
inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); }
|
inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); }
|
||||||
void DropPlayer(int ID, std::optional<std::string> MaybeReason);
|
void DropPlayer(int ID, std::optional<std::string> MaybeReason);
|
||||||
void SendChatMessage(int ID, const std::string& Message);
|
void SendChatMessage(int ID, const std::string& Message);
|
||||||
@@ -22,7 +23,15 @@ namespace MP {
|
|||||||
bool IsPlayerConnected(int ID);
|
bool IsPlayerConnected(int ID);
|
||||||
void Sleep(size_t Ms);
|
void Sleep(size_t Ms);
|
||||||
void PrintRaw(sol::variadic_args);
|
void PrintRaw(sol::variadic_args);
|
||||||
|
std::string JsonEncode(const sol::table& object);
|
||||||
|
std::string JsonDiff(const std::string& a, const std::string& b);
|
||||||
|
std::string JsonDiffApply(const std::string& data, const std::string& patch);
|
||||||
|
std::string JsonPrettify(const std::string& json);
|
||||||
|
std::string JsonMinify(const std::string& json);
|
||||||
|
std::string JsonFlatten(const std::string& json);
|
||||||
|
std::string JsonUnflatten(const std::string& json);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace FS {
|
namespace FS {
|
||||||
std::pair<bool, std::string> CreateDirectory(const std::string& Path);
|
std::pair<bool, std::string> CreateDirectory(const std::string& Path);
|
||||||
std::pair<bool, std::string> Remove(const std::string& Path);
|
std::pair<bool, std::string> Remove(const std::string& Path);
|
||||||
|
|||||||
+3
-2
@@ -3,9 +3,10 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT
|
#define TOML11_PRESERVE_COMMENTS_BY_DEFAULT
|
||||||
#include <toml.hpp> // header-only version of TOML++
|
#include <toml11/toml.hpp> // header-only version of TOML++
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ public:
|
|||||||
void FlushToFile();
|
void FlushToFile();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CreateConfigFile(std::string_view name);
|
void CreateConfigFile();
|
||||||
void ParseFromFile(std::string_view name);
|
void ParseFromFile(std::string_view name);
|
||||||
void PrintDebug();
|
void PrintDebug();
|
||||||
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue);
|
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue);
|
||||||
|
|||||||
+31
-6
@@ -4,6 +4,11 @@
|
|||||||
#include "commandline.h"
|
#include "commandline.h"
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <functional>
|
||||||
|
#include <string>
|
||||||
|
#include <tuple>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class TLuaEngine;
|
class TLuaEngine;
|
||||||
|
|
||||||
@@ -22,13 +27,33 @@ private:
|
|||||||
void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false);
|
void RunAsCommand(const std::string& cmd, bool IgnoreNotACommand = false);
|
||||||
void ChangeToLuaConsole(const std::string& LuaStateId);
|
void ChangeToLuaConsole(const std::string& LuaStateId);
|
||||||
void ChangeToRegularConsole();
|
void ChangeToRegularConsole();
|
||||||
|
void HandleLuaInternalCommand(const std::string& cmd);
|
||||||
|
|
||||||
void Command_Lua(const std::string& cmd);
|
void Command_Lua(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
void Command_Help(const std::string& cmd);
|
void Command_Help(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
void Command_Kick(const std::string& cmd);
|
void Command_Kick(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
void Command_Say(const std::string& cmd);
|
void Command_List(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
void Command_List(const std::string& cmd);
|
void Command_Status(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
void Command_Status(const std::string& cmd);
|
void Command_Settings(const std::string& cmd, const std::vector<std::string>& args);
|
||||||
|
void Command_Clear(const std::string&, const std::vector<std::string>& args);
|
||||||
|
|
||||||
|
void Command_Say(const std::string& FullCommand);
|
||||||
|
bool EnsureArgsCount(const std::vector<std::string>& args, size_t n);
|
||||||
|
bool EnsureArgsCount(const std::vector<std::string>& args, size_t min, size_t max);
|
||||||
|
|
||||||
|
static std::tuple<std::string, std::vector<std::string>> ParseCommand(const std::string& cmd);
|
||||||
|
static std::string ConcatArgs(const std::vector<std::string>& args, char space = ' ');
|
||||||
|
|
||||||
|
std::unordered_map<std::string, std::function<void(const std::string&, const std::vector<std::string>&)>> mCommandMap = {
|
||||||
|
{ "lua", [this](const auto& a, const auto& b) { Command_Lua(a, b); } },
|
||||||
|
{ "help", [this](const auto& a, const auto& b) { Command_Help(a, b); } },
|
||||||
|
{ "kick", [this](const auto& a, const auto& b) { Command_Kick(a, b); } },
|
||||||
|
{ "list", [this](const auto& a, const auto& b) { Command_List(a, b); } },
|
||||||
|
{ "status", [this](const auto& a, const auto& b) { Command_Status(a, b); } },
|
||||||
|
{ "settings", [this](const auto& a, const auto& b) { Command_Settings(a, b); } },
|
||||||
|
{ "clear", [this](const auto& a, const auto& b) { Command_Clear(a, b); } },
|
||||||
|
{ "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called
|
||||||
|
};
|
||||||
|
|
||||||
Commandline mCommandline;
|
Commandline mCommandline;
|
||||||
std::vector<std::string> mCachedLuaHistory;
|
std::vector<std::string> mCachedLuaHistory;
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ private:
|
|||||||
std::string GenerateCall();
|
std::string GenerateCall();
|
||||||
std::string GetPlayers();
|
std::string GetPlayers();
|
||||||
|
|
||||||
bool mShutdown = false;
|
|
||||||
TResourceManager& mResourceManager;
|
TResourceManager& mResourceManager;
|
||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
};
|
};
|
||||||
|
|||||||
+44
-23
@@ -11,8 +11,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
#include <random>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <toml.hpp>
|
#include <toml11/toml.hpp>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -33,8 +34,8 @@ static constexpr size_t TLuaArgTypes_Bool = 3;
|
|||||||
class TLuaPlugin;
|
class TLuaPlugin;
|
||||||
|
|
||||||
struct TLuaResult {
|
struct TLuaResult {
|
||||||
std::atomic_bool Ready;
|
bool Ready;
|
||||||
std::atomic_bool Error;
|
bool Error;
|
||||||
std::string ErrorMessage;
|
std::string ErrorMessage;
|
||||||
sol::object Result { sol::lua_nil };
|
sol::object Result { sol::lua_nil };
|
||||||
TLuaStateId StateId;
|
TLuaStateId StateId;
|
||||||
@@ -47,6 +48,7 @@ struct TLuaPluginConfig {
|
|||||||
static inline const std::string FileName = "PluginConfig.toml";
|
static inline const std::string FileName = "PluginConfig.toml";
|
||||||
TLuaStateId StateId;
|
TLuaStateId StateId;
|
||||||
// TODO: Add execute list
|
// TODO: Add execute list
|
||||||
|
// TODO: Build a better toml serializer, or some way to do this in an easier way
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TLuaChunk {
|
struct TLuaChunk {
|
||||||
@@ -58,20 +60,7 @@ struct TLuaChunk {
|
|||||||
std::string PluginPath;
|
std::string PluginPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TPluginMonitor : IThreaded {
|
class TLuaEngine : public std::enable_shared_from_this<TLuaEngine>, IThreaded {
|
||||||
public:
|
|
||||||
TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::atomic_bool& Shutdown);
|
|
||||||
|
|
||||||
void operator()();
|
|
||||||
|
|
||||||
private:
|
|
||||||
TLuaEngine& mEngine;
|
|
||||||
fs::path mPath;
|
|
||||||
std::atomic_bool& mShutdown;
|
|
||||||
std::unordered_map<std::string, fs::file_time_type> mFileTimes;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TLuaEngine : IThreaded {
|
|
||||||
public:
|
public:
|
||||||
enum CallStrategy : int {
|
enum CallStrategy : int {
|
||||||
BestEffort,
|
BestEffort,
|
||||||
@@ -102,6 +91,7 @@ public:
|
|||||||
std::unique_lock Lock(mResultsToCheckMutex);
|
std::unique_lock Lock(mResultsToCheckMutex);
|
||||||
return mResultsToCheck.size();
|
return mResultsToCheck.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetLuaStateCount() {
|
size_t GetLuaStateCount() {
|
||||||
std::unique_lock Lock(mLuaStatesMutex);
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
return mLuaStates.size();
|
return mLuaStates.size();
|
||||||
@@ -129,7 +119,6 @@ public:
|
|||||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
|
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
|
||||||
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
|
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
|
||||||
void RegisterEvent(const std::string& EventName, TLuaStateId StateId, const std::string& FunctionName);
|
void RegisterEvent(const std::string& EventName, TLuaStateId StateId, const std::string& FunctionName);
|
||||||
template <typename... ArgsT>
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @tparam ArgsT Template Arguments for the event (Metadata) todo: figure out what this means
|
* @tparam ArgsT Template Arguments for the event (Metadata) todo: figure out what this means
|
||||||
@@ -138,6 +127,7 @@ public:
|
|||||||
* @param Args
|
* @param Args
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
|
template <typename... ArgsT>
|
||||||
[[nodiscard]] std::vector<std::shared_ptr<TLuaResult>> TriggerEvent(const std::string& EventName, TLuaStateId IgnoreId, ArgsT&&... Args) {
|
[[nodiscard]] std::vector<std::shared_ptr<TLuaResult>> TriggerEvent(const std::string& EventName, TLuaStateId IgnoreId, ArgsT&&... Args) {
|
||||||
std::unique_lock Lock(mLuaEventsMutex);
|
std::unique_lock Lock(mLuaEventsMutex);
|
||||||
beammp_event(EventName);
|
beammp_event(EventName);
|
||||||
@@ -157,6 +147,21 @@ public:
|
|||||||
}
|
}
|
||||||
return Results; //
|
return Results; //
|
||||||
}
|
}
|
||||||
|
template <typename... ArgsT>
|
||||||
|
[[nodiscard]] std::vector<std::shared_ptr<TLuaResult>> TriggerLocalEvent(const TLuaStateId& StateId, const std::string& EventName, ArgsT&&... Args) {
|
||||||
|
std::unique_lock Lock(mLuaEventsMutex);
|
||||||
|
beammp_event(EventName + " in '" + StateId + "'");
|
||||||
|
if (mLuaEvents.find(EventName) == mLuaEvents.end()) { // if no event handler is defined for 'EventName', return immediately
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
std::vector<std::shared_ptr<TLuaResult>> Results;
|
||||||
|
std::vector<TLuaArgTypes> Arguments { TLuaArgTypes { std::forward<ArgsT>(Args) }... };
|
||||||
|
const auto Handlers = GetEventHandlersForState(EventName, StateId);
|
||||||
|
for (const auto& Handler : Handlers) {
|
||||||
|
Results.push_back(EnqueueFunctionCall(StateId, Handler, Arguments));
|
||||||
|
}
|
||||||
|
return Results;
|
||||||
|
}
|
||||||
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
|
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
|
||||||
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy);
|
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy);
|
||||||
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
|
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
|
||||||
@@ -166,6 +171,14 @@ public:
|
|||||||
|
|
||||||
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
|
static constexpr const char* BeamMPFnNotFoundError = "BEAMMP_FN_NOT_FOUND";
|
||||||
|
|
||||||
|
std::vector<std::string> GetStateGlobalKeysForState(TLuaStateId StateId);
|
||||||
|
|
||||||
|
// Debugging functions (slow)
|
||||||
|
std::unordered_map<std::string /*event name */, std::vector<std::string> /* handlers */> Debug_GetEventsForState(TLuaStateId StateId);
|
||||||
|
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> Debug_GetStateExecuteQueueForState(TLuaStateId StateId);
|
||||||
|
std::vector<QueuedFunction> Debug_GetStateFunctionQueueForState(TLuaStateId StateId);
|
||||||
|
std::vector<TLuaResult> Debug_GetResultsToCheckForState(TLuaStateId StateId);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void CollectAndInitPlugins();
|
void CollectAndInitPlugins();
|
||||||
void InitializePlugin(const fs::path& Folder, const TLuaPluginConfig& Config);
|
void InitializePlugin(const fs::path& Folder, const TLuaPluginConfig& Config);
|
||||||
@@ -174,7 +187,7 @@ private:
|
|||||||
|
|
||||||
class StateThreadData : IThreaded {
|
class StateThreadData : IThreaded {
|
||||||
public:
|
public:
|
||||||
StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine);
|
StateThreadData(const std::string& Name, TLuaStateId StateId, TLuaEngine& Engine);
|
||||||
StateThreadData(const StateThreadData&) = delete;
|
StateThreadData(const StateThreadData&) = delete;
|
||||||
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
|
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
|
||||||
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const TLuaChunk& Script);
|
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const TLuaChunk& Script);
|
||||||
@@ -185,6 +198,12 @@ private:
|
|||||||
void operator()() override;
|
void operator()() override;
|
||||||
sol::state_view State() { return sol::state_view(mState); }
|
sol::state_view State() { return sol::state_view(mState); }
|
||||||
|
|
||||||
|
std::vector<std::string> GetStateGlobalKeys();
|
||||||
|
|
||||||
|
// Debug functions, slow
|
||||||
|
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> Debug_GetStateExecuteQueue();
|
||||||
|
std::vector<TLuaEngine::QueuedFunction> Debug_GetStateFunctionQueue();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sol::table Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs);
|
sol::table Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs);
|
||||||
sol::table Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs);
|
sol::table Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs);
|
||||||
@@ -193,10 +212,12 @@ private:
|
|||||||
std::string Lua_GetPlayerName(int ID);
|
std::string Lua_GetPlayerName(int ID);
|
||||||
sol::table Lua_GetPlayerVehicles(int ID);
|
sol::table Lua_GetPlayerVehicles(int ID);
|
||||||
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
|
sol::table Lua_HttpCreateConnection(const std::string& host, uint16_t port);
|
||||||
|
sol::table Lua_JsonDecode(const std::string& str);
|
||||||
int Lua_GetPlayerIDByName(const std::string& Name);
|
int Lua_GetPlayerIDByName(const std::string& Name);
|
||||||
|
sol::table Lua_FS_ListFiles(const std::string& Path);
|
||||||
|
sol::table Lua_FS_ListDirectories(const std::string& Path);
|
||||||
|
|
||||||
std::string mName;
|
std::string mName;
|
||||||
std::atomic_bool& mShutdown;
|
|
||||||
TLuaStateId mStateId;
|
TLuaStateId mStateId;
|
||||||
lua_State* mState;
|
lua_State* mState;
|
||||||
std::thread mThread;
|
std::thread mThread;
|
||||||
@@ -209,6 +230,8 @@ private:
|
|||||||
sol::state_view mStateView { mState };
|
sol::state_view mStateView { mState };
|
||||||
std::queue<fs::path> mPaths;
|
std::queue<fs::path> mPaths;
|
||||||
std::recursive_mutex mPathsMutex;
|
std::recursive_mutex mPathsMutex;
|
||||||
|
std::mt19937 mMersenneTwister;
|
||||||
|
std::uniform_real_distribution<double> mUniformRealDistribution01;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct TimedEvent {
|
struct TimedEvent {
|
||||||
@@ -223,9 +246,7 @@ private:
|
|||||||
|
|
||||||
TNetwork* mNetwork;
|
TNetwork* mNetwork;
|
||||||
TServer* mServer;
|
TServer* mServer;
|
||||||
TPluginMonitor mPluginMonitor;
|
const fs::path mResourceServerPath;
|
||||||
std::atomic_bool mShutdown { false };
|
|
||||||
fs::path mResourceServerPath;
|
|
||||||
std::vector<std::shared_ptr<TLuaPlugin>> mLuaPlugins;
|
std::vector<std::shared_ptr<TLuaPlugin>> mLuaPlugins;
|
||||||
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
std::unordered_map<TLuaStateId, std::unique_ptr<StateThreadData>> mLuaStates;
|
||||||
std::recursive_mutex mLuaStatesMutex;
|
std::recursive_mutex mLuaStatesMutex;
|
||||||
|
|||||||
+1
-1
@@ -32,7 +32,6 @@ private:
|
|||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
TPPSMonitor& mPPSMonitor;
|
TPPSMonitor& mPPSMonitor;
|
||||||
SOCKET mUDPSock {};
|
SOCKET mUDPSock {};
|
||||||
bool mShutdown { false };
|
|
||||||
TResourceManager& mResourceManager;
|
TResourceManager& mResourceManager;
|
||||||
std::thread mUDPThread;
|
std::thread mUDPThread;
|
||||||
std::thread mTCPThread;
|
std::thread mTCPThread;
|
||||||
@@ -48,4 +47,5 @@ private:
|
|||||||
void SendFile(TClient& c, const std::string& Name);
|
void SendFile(TClient& c, const std::string& Name);
|
||||||
static bool TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size);
|
static bool TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size);
|
||||||
static void SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name);
|
static void SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name);
|
||||||
|
static uint8_t* SendSplit(TClient& c, SOCKET Socket, uint8_t* DataPtr, size_t Size);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,6 +22,5 @@ private:
|
|||||||
|
|
||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
std::optional<std::reference_wrapper<TNetwork>> mNetwork { std::nullopt };
|
std::optional<std::reference_wrapper<TNetwork>> mNetwork { std::nullopt };
|
||||||
bool mShutdown { false };
|
|
||||||
int mInternalPPS { 0 };
|
int mInternalPPS { 0 };
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "Common.h"
|
||||||
|
#include "IThreaded.h"
|
||||||
|
|
||||||
|
#include <atomic>
|
||||||
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
class TLuaEngine;
|
||||||
|
|
||||||
|
class TPluginMonitor : IThreaded, public std::enable_shared_from_this<TPluginMonitor> {
|
||||||
|
public:
|
||||||
|
TPluginMonitor(const fs::path& Path, std::shared_ptr<TLuaEngine> Engine);
|
||||||
|
|
||||||
|
void operator()();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<TLuaEngine> mEngine;
|
||||||
|
fs::path mPath;
|
||||||
|
std::unordered_map<std::string, fs::file_time_type> mFileTimes;
|
||||||
|
};
|
||||||
+78
-2
@@ -1,6 +1,7 @@
|
|||||||
#include "ArgsParser.h"
|
#include "ArgsParser.h"
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
void ArgsParser::Parse(const std::vector<std::string_view>& ArgList) {
|
void ArgsParser::Parse(const std::vector<std::string_view>& ArgList) {
|
||||||
for (const auto& Arg : ArgList) {
|
for (const auto& Arg : ArgList) {
|
||||||
@@ -12,7 +13,7 @@ void ArgsParser::Parse(const std::vector<std::string_view>& ArgList) {
|
|||||||
ConsumeLongFlag(std::string(Arg));
|
ConsumeLongFlag(std::string(Arg));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
beammp_error("Error parsing commandline arguments: Supplied argument '" + std::string(Arg) + "' is not a valid argument and was ignored.");
|
beammp_errorf("Error parsing commandline arguments: Supplied argument '{}' is not a valid argument and was ignored.", Arg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -21,7 +22,7 @@ bool ArgsParser::Verify() {
|
|||||||
bool Ok = true;
|
bool Ok = true;
|
||||||
for (const auto& RegisteredArg : mRegisteredArguments) {
|
for (const auto& RegisteredArg : mRegisteredArguments) {
|
||||||
if (RegisteredArg.Flags & Flags::REQUIRED && !FoundArgument(RegisteredArg.Names)) {
|
if (RegisteredArg.Flags & Flags::REQUIRED && !FoundArgument(RegisteredArg.Names)) {
|
||||||
beammp_error("Error in commandline arguments: Argument '" + std::string(RegisteredArg.Names.at(0)) + "' is required but wasn't found.");
|
beammp_errorf("Error in commandline arguments: Argument '{}' is required but wasn't found.", RegisteredArg.Names.at(0));
|
||||||
Ok = false;
|
Ok = false;
|
||||||
continue;
|
continue;
|
||||||
} else if (FoundArgument(RegisteredArg.Names)) {
|
} else if (FoundArgument(RegisteredArg.Names)) {
|
||||||
@@ -92,3 +93,78 @@ void ArgsParser::ConsumeLongFlag(const std::string& Arg) {
|
|||||||
beammp_warn("Argument '" + Name + "' was supplied but isn't a known argument, so it is likely being ignored.");
|
beammp_warn("Argument '" + Name + "' was supplied but isn't a known argument, so it is likely being ignored.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ArgsParser") {
|
||||||
|
ArgsParser parser;
|
||||||
|
|
||||||
|
SUBCASE("Simple args") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::NONE);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::NONE);
|
||||||
|
parser.Parse({ "--a", "--hello" });
|
||||||
|
CHECK(parser.Verify());
|
||||||
|
CHECK(parser.FoundArgument({ "a" }));
|
||||||
|
CHECK(parser.FoundArgument({ "hello" }));
|
||||||
|
CHECK(parser.FoundArgument({ "a", "hello" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "b" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "goodbye" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
SUBCASE("No args") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::NONE);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::NONE);
|
||||||
|
parser.Parse({});
|
||||||
|
CHECK(parser.Verify());
|
||||||
|
CHECK(!parser.FoundArgument({ "a" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "hello" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "a", "hello" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "b" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "goodbye" }));
|
||||||
|
CHECK(!parser.FoundArgument({ "" }));
|
||||||
|
}
|
||||||
|
|
||||||
|
SUBCASE("Value args") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::HAS_VALUE);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::HAS_VALUE);
|
||||||
|
parser.Parse({ "--a=5", "--hello=world" });
|
||||||
|
CHECK(parser.Verify());
|
||||||
|
REQUIRE(parser.FoundArgument({ "a" }));
|
||||||
|
REQUIRE(parser.FoundArgument({ "hello" }));
|
||||||
|
CHECK(parser.GetValueOfArgument({ "a" }).has_value());
|
||||||
|
CHECK(parser.GetValueOfArgument({ "a" }).value() == "5");
|
||||||
|
CHECK(parser.GetValueOfArgument({ "hello" }).has_value());
|
||||||
|
CHECK(parser.GetValueOfArgument({ "hello" }).value() == "world");
|
||||||
|
}
|
||||||
|
|
||||||
|
SUBCASE("Mixed value & no-value args") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::HAS_VALUE);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::NONE);
|
||||||
|
parser.Parse({ "--a=5", "--hello" });
|
||||||
|
CHECK(parser.Verify());
|
||||||
|
REQUIRE(parser.FoundArgument({ "a" }));
|
||||||
|
REQUIRE(parser.FoundArgument({ "hello" }));
|
||||||
|
CHECK(parser.GetValueOfArgument({ "a" }).has_value());
|
||||||
|
CHECK(parser.GetValueOfArgument({ "a" }).value() == "5");
|
||||||
|
CHECK(!parser.GetValueOfArgument({ "hello" }).has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
SUBCASE("Required args") {
|
||||||
|
SUBCASE("Two required, two present") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.Parse({ "--a", "--hello" });
|
||||||
|
CHECK(parser.Verify());
|
||||||
|
}
|
||||||
|
SUBCASE("Two required, one present") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.Parse({ "--a" });
|
||||||
|
CHECK(!parser.Verify());
|
||||||
|
}
|
||||||
|
SUBCASE("Two required, none present") {
|
||||||
|
parser.RegisterArgument({ "a" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.RegisterArgument({ "hello" }, ArgsParser::Flags::REQUIRED);
|
||||||
|
parser.Parse({ "--b" });
|
||||||
|
CHECK(!parser.Verify());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
// FIXME: add debug prints
|
|
||||||
|
|
||||||
void TClient::DeleteCar(int Ident) {
|
void TClient::DeleteCar(int Ident) {
|
||||||
std::unique_lock lock(mVehicleDataMutex);
|
std::unique_lock lock(mVehicleDataMutex);
|
||||||
auto iter = std::find_if(mVehicleData.begin(), mVehicleData.end(), [&](auto& elem) {
|
auto iter = std::find_if(mVehicleData.begin(), mVehicleData.end(), [&](auto& elem) {
|
||||||
|
|||||||
+131
-5
@@ -12,6 +12,9 @@
|
|||||||
#include "CustomAssert.h"
|
#include "CustomAssert.h"
|
||||||
#include "Http.h"
|
#include "Http.h"
|
||||||
|
|
||||||
|
// global, yes, this is ugly, no, it cant be done another way
|
||||||
|
TSentry Sentry {};
|
||||||
|
|
||||||
Application::TSettings Application::Settings = {};
|
Application::TSettings Application::Settings = {};
|
||||||
|
|
||||||
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
|
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
|
||||||
@@ -22,6 +25,7 @@ void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Application::GracefullyShutdown() {
|
void Application::GracefullyShutdown() {
|
||||||
|
SetShutdown(true);
|
||||||
static bool AlreadyShuttingDown = false;
|
static bool AlreadyShuttingDown = false;
|
||||||
static uint8_t ShutdownAttempts = 0;
|
static uint8_t ShutdownAttempts = 0;
|
||||||
if (AlreadyShuttingDown) {
|
if (AlreadyShuttingDown) {
|
||||||
@@ -43,6 +47,7 @@ void Application::GracefullyShutdown() {
|
|||||||
beammp_info("Subsystem " + std::to_string(i + 1) + "/" + std::to_string(mShutdownHandlers.size()) + " shutting down");
|
beammp_info("Subsystem " + std::to_string(i + 1) + "/" + std::to_string(mShutdownHandlers.size()) + " shutting down");
|
||||||
mShutdownHandlers[i]();
|
mShutdownHandlers[i]();
|
||||||
}
|
}
|
||||||
|
// std::exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Application::ServerVersionString() {
|
std::string Application::ServerVersionString() {
|
||||||
@@ -60,7 +65,23 @@ std::array<uint8_t, 3> Application::VersionStrToInts(const std::string& str) {
|
|||||||
return Version;
|
return Version;
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: This should be used by operator< on Version
|
TEST_CASE("Application::VersionStrToInts") {
|
||||||
|
auto v = Application::VersionStrToInts("1.2.3");
|
||||||
|
CHECK(v[0] == 1);
|
||||||
|
CHECK(v[1] == 2);
|
||||||
|
CHECK(v[2] == 3);
|
||||||
|
|
||||||
|
v = Application::VersionStrToInts("10.20.30");
|
||||||
|
CHECK(v[0] == 10);
|
||||||
|
CHECK(v[1] == 20);
|
||||||
|
CHECK(v[2] == 30);
|
||||||
|
|
||||||
|
v = Application::VersionStrToInts("100.200.255");
|
||||||
|
CHECK(v[0] == 100);
|
||||||
|
CHECK(v[1] == 200);
|
||||||
|
CHECK(v[2] == 255);
|
||||||
|
}
|
||||||
|
|
||||||
bool Application::IsOutdated(const Version& Current, const Version& Newest) {
|
bool Application::IsOutdated(const Version& Current, const Version& Newest) {
|
||||||
if (Newest.major > Current.major) {
|
if (Newest.major > Current.major) {
|
||||||
return true;
|
return true;
|
||||||
@@ -73,6 +94,65 @@ bool Application::IsOutdated(const Version& Current, const Version& Newest) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Application::IsShuttingDown() {
|
||||||
|
std::shared_lock Lock(mShutdownMtx);
|
||||||
|
return mShutdown;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Application::SleepSafeSeconds(size_t Seconds) {
|
||||||
|
// Sleeps for 500 ms, checks if a shutdown occurred, and so forth
|
||||||
|
for (size_t i = 0; i < Seconds * 2; ++i) {
|
||||||
|
if (Application::IsShuttingDown()) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Application::IsOutdated (version check)") {
|
||||||
|
SUBCASE("Same version") {
|
||||||
|
CHECK(!Application::IsOutdated({ 1, 2, 3 }, { 1, 2, 3 }));
|
||||||
|
}
|
||||||
|
// we need to use over 1-2 digits to test against lexical comparisons
|
||||||
|
SUBCASE("Patch outdated") {
|
||||||
|
for (uint8_t Patch = 0; Patch < 10; ++Patch) {
|
||||||
|
for (uint8_t Minor = 0; Minor < 10; ++Minor) {
|
||||||
|
for (uint8_t Major = 0; Major < 10; ++Major) {
|
||||||
|
CHECK(Application::IsOutdated({ uint8_t(Major), uint8_t(Minor), uint8_t(Patch) }, { uint8_t(Major), uint8_t(Minor), uint8_t(Patch + 1) }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("Minor outdated") {
|
||||||
|
for (uint8_t Patch = 0; Patch < 10; ++Patch) {
|
||||||
|
for (uint8_t Minor = 0; Minor < 10; ++Minor) {
|
||||||
|
for (uint8_t Major = 0; Major < 10; ++Major) {
|
||||||
|
CHECK(Application::IsOutdated({ uint8_t(Major), uint8_t(Minor), uint8_t(Patch) }, { uint8_t(Major), uint8_t(Minor + 1), uint8_t(Patch) }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("Major outdated") {
|
||||||
|
for (uint8_t Patch = 0; Patch < 10; ++Patch) {
|
||||||
|
for (uint8_t Minor = 0; Minor < 10; ++Minor) {
|
||||||
|
for (uint8_t Major = 0; Major < 10; ++Major) {
|
||||||
|
CHECK(Application::IsOutdated({ uint8_t(Major), uint8_t(Minor), uint8_t(Patch) }, { uint8_t(Major + 1), uint8_t(Minor), uint8_t(Patch) }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SUBCASE("All outdated") {
|
||||||
|
for (uint8_t Patch = 0; Patch < 10; ++Patch) {
|
||||||
|
for (uint8_t Minor = 0; Minor < 10; ++Minor) {
|
||||||
|
for (uint8_t Major = 0; Major < 10; ++Major) {
|
||||||
|
CHECK(Application::IsOutdated({ uint8_t(Major), uint8_t(Minor), uint8_t(Patch) }, { uint8_t(Major + 1), uint8_t(Minor + 1), uint8_t(Patch + 1) }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Application::SetSubsystemStatus(const std::string& Subsystem, Status status) {
|
void Application::SetSubsystemStatus(const std::string& Subsystem, Status status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case Status::Good:
|
case Status::Good:
|
||||||
@@ -95,6 +175,20 @@ void Application::SetSubsystemStatus(const std::string& Subsystem, Status status
|
|||||||
mSystemStatusMap[Subsystem] = status;
|
mSystemStatusMap[Subsystem] = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Application::SetShutdown(bool Val) {
|
||||||
|
std::unique_lock Lock(mShutdownMtx);
|
||||||
|
mShutdown = Val;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Application::SetSubsystemStatus") {
|
||||||
|
Application::SetSubsystemStatus("Test", Application::Status::Good);
|
||||||
|
auto Map = Application::GetSubsystemStatuses();
|
||||||
|
CHECK(Map.at("Test") == Application::Status::Good);
|
||||||
|
Application::SetSubsystemStatus("Test", Application::Status::Bad);
|
||||||
|
Map = Application::GetSubsystemStatuses();
|
||||||
|
CHECK(Map.at("Test") == Application::Status::Bad);
|
||||||
|
}
|
||||||
|
|
||||||
void Application::CheckForUpdates() {
|
void Application::CheckForUpdates() {
|
||||||
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Starting);
|
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Starting);
|
||||||
static bool FirstTime = true;
|
static bool FirstTime = true;
|
||||||
@@ -152,6 +246,25 @@ std::string ThreadName(bool DebugModeOverride) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("ThreadName") {
|
||||||
|
RegisterThread("MyThread");
|
||||||
|
auto OrigDebug = Application::Settings.DebugModeEnabled;
|
||||||
|
|
||||||
|
// ThreadName adds a space at the end, legacy but we need it still
|
||||||
|
SUBCASE("Debug mode enabled") {
|
||||||
|
Application::Settings.DebugModeEnabled = true;
|
||||||
|
CHECK(ThreadName(true) == "MyThread ");
|
||||||
|
CHECK(ThreadName(false) == "MyThread ");
|
||||||
|
}
|
||||||
|
SUBCASE("Debug mode disabled") {
|
||||||
|
Application::Settings.DebugModeEnabled = false;
|
||||||
|
CHECK(ThreadName(true) == "MyThread ");
|
||||||
|
CHECK(ThreadName(false) == "");
|
||||||
|
}
|
||||||
|
// cleanup
|
||||||
|
Application::Settings.DebugModeEnabled = OrigDebug;
|
||||||
|
}
|
||||||
|
|
||||||
void RegisterThread(const std::string& str) {
|
void RegisterThread(const std::string& str) {
|
||||||
std::string ThreadId;
|
std::string ThreadId;
|
||||||
#ifdef BEAMMP_WINDOWS
|
#ifdef BEAMMP_WINDOWS
|
||||||
@@ -162,13 +275,18 @@ void RegisterThread(const std::string& str) {
|
|||||||
ThreadId = std::to_string(gettid());
|
ThreadId = std::to_string(gettid());
|
||||||
#endif
|
#endif
|
||||||
if (Application::Settings.DebugModeEnabled) {
|
if (Application::Settings.DebugModeEnabled) {
|
||||||
std::ofstream ThreadFile("Threads.log", std::ios::app);
|
std::ofstream ThreadFile(".Threads.log", std::ios::app);
|
||||||
ThreadFile << ("Thread \"" + str + "\" is TID " + ThreadId) << std::endl;
|
ThreadFile << ("Thread \"" + str + "\" is TID " + ThreadId) << std::endl;
|
||||||
}
|
}
|
||||||
auto Lock = std::unique_lock(ThreadNameMapMutex);
|
auto Lock = std::unique_lock(ThreadNameMapMutex);
|
||||||
threadNameMap[std::this_thread::get_id()] = str;
|
threadNameMap[std::this_thread::get_id()] = str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("RegisterThread") {
|
||||||
|
RegisterThread("MyThread");
|
||||||
|
CHECK(threadNameMap.at(std::this_thread::get_id()) == "MyThread");
|
||||||
|
}
|
||||||
|
|
||||||
Version::Version(uint8_t major, uint8_t minor, uint8_t patch)
|
Version::Version(uint8_t major, uint8_t minor, uint8_t patch)
|
||||||
: major(major)
|
: major(major)
|
||||||
, minor(minor)
|
, minor(minor)
|
||||||
@@ -179,12 +297,17 @@ Version::Version(const std::array<uint8_t, 3>& v)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string Version::AsString() {
|
std::string Version::AsString() {
|
||||||
std::stringstream ss {};
|
return fmt::format("{:d}.{:d}.{:d}", major, minor, patch);
|
||||||
ss << int(major) << "." << int(minor) << "." << int(patch);
|
}
|
||||||
return ss.str();
|
|
||||||
|
TEST_CASE("Version::AsString") {
|
||||||
|
CHECK(Version { 0, 0, 0 }.AsString() == "0.0.0");
|
||||||
|
CHECK(Version { 1, 2, 3 }.AsString() == "1.2.3");
|
||||||
|
CHECK(Version { 255, 255, 255 }.AsString() == "255.255.255");
|
||||||
}
|
}
|
||||||
|
|
||||||
void LogChatMessage(const std::string& name, int id, const std::string& msg) {
|
void LogChatMessage(const std::string& name, int id, const std::string& msg) {
|
||||||
|
if (Application::Settings.LogChat) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << ThreadName();
|
ss << ThreadName();
|
||||||
ss << "[CHAT] ";
|
ss << "[CHAT] ";
|
||||||
@@ -194,7 +317,10 @@ void LogChatMessage(const std::string& name, int id, const std::string& msg) {
|
|||||||
ss << name << "";
|
ss << name << "";
|
||||||
}
|
}
|
||||||
ss << msg;
|
ss << msg;
|
||||||
|
#ifdef DOCTEST_CONFIG_DISABLE
|
||||||
Application::Console().Write(ss.str());
|
Application::Console().Write(ss.str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GetPlatformAgnosticErrorString() {
|
std::string GetPlatformAgnosticErrorString() {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
#include "Compat.h"
|
#include "Compat.h"
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
|
|
||||||
static struct termios old, current;
|
static struct termios old, current;
|
||||||
@@ -20,6 +23,23 @@ void resetTermios(void) {
|
|||||||
tcsetattr(0, TCSANOW, &old);
|
tcsetattr(0, TCSANOW, &old);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("init and reset termios") {
|
||||||
|
if (isatty(STDIN_FILENO)) {
|
||||||
|
struct termios original;
|
||||||
|
tcgetattr(0, &original);
|
||||||
|
SUBCASE("no echo") {
|
||||||
|
initTermios(false);
|
||||||
|
}
|
||||||
|
SUBCASE("yes echo") {
|
||||||
|
initTermios(true);
|
||||||
|
}
|
||||||
|
resetTermios();
|
||||||
|
struct termios current;
|
||||||
|
tcgetattr(0, ¤t);
|
||||||
|
CHECK(std::memcmp(&original, ¤t, sizeof(struct termios)) == 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char getch_(int echo) {
|
char getch_(int echo) {
|
||||||
char ch;
|
char ch;
|
||||||
initTermios(echo);
|
initTermios(echo);
|
||||||
|
|||||||
+13
-192
@@ -7,17 +7,13 @@
|
|||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <rapidjson/document.h>
|
|
||||||
#include <rapidjson/rapidjson.h>
|
|
||||||
#include <rapidjson/stringbuffer.h>
|
|
||||||
#include <rapidjson/writer.h>
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
fs::path Http::Server::THttpServerInstance::KeyFilePath;
|
|
||||||
fs::path Http::Server::THttpServerInstance::CertFilePath;
|
|
||||||
// TODO: Add sentry error handling back
|
// TODO: Add sentry error handling back
|
||||||
|
|
||||||
namespace json = rapidjson;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
std::string Http::GET(const std::string& host, int port, const std::string& target, unsigned int* status) {
|
||||||
httplib::SSLClient client(host, port);
|
httplib::SSLClient client(host, port);
|
||||||
@@ -146,145 +142,10 @@ std::string Http::Status::ToString(int Code) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
long Http::Server::Tx509KeypairGenerator::GenerateRandomId() {
|
TEST_CASE("Http::Status::ToString") {
|
||||||
std::random_device R;
|
CHECK(Http::Status::ToString(200) == "OK");
|
||||||
std::default_random_engine E1(R());
|
CHECK(Http::Status::ToString(696969) == "696969");
|
||||||
std::uniform_int_distribution<long> UniformDist(0, ULONG_MAX);
|
CHECK(Http::Status::ToString(-1) == "Invalid Response Code");
|
||||||
return UniformDist(E1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Http::Server::THttpServerInstance::THttpServerInstance() { }
|
|
||||||
EVP_PKEY* Http::Server::Tx509KeypairGenerator::GenerateKey() {
|
|
||||||
/**
|
|
||||||
* Allocate memory for the pkey
|
|
||||||
*/
|
|
||||||
EVP_PKEY* PKey = EVP_PKEY_new();
|
|
||||||
if (PKey == nullptr) {
|
|
||||||
beammp_error("Could not allocate memory for X.509 private key (PKEY) generation.");
|
|
||||||
throw std::runtime_error { std::string { "X.509 PKEY allocation error" } };
|
|
||||||
}
|
|
||||||
BIGNUM* E = BN_new();
|
|
||||||
beammp_assert(E); // TODO: replace all these asserts with beammp_errors
|
|
||||||
unsigned char three = 3;
|
|
||||||
BIGNUM* EErr = BN_bin2bn(&three, sizeof(three), E);
|
|
||||||
beammp_assert(EErr);
|
|
||||||
RSA* Rsa = RSA_new();
|
|
||||||
beammp_assert(Rsa);
|
|
||||||
int Ret = RSA_generate_key_ex(Rsa, Crypto::RSA_DEFAULT_KEYLENGTH, E, nullptr);
|
|
||||||
beammp_assert(Ret == 1);
|
|
||||||
BN_free(E);
|
|
||||||
if (!EVP_PKEY_assign_RSA(PKey, Rsa)) {
|
|
||||||
EVP_PKEY_free(PKey);
|
|
||||||
beammp_error(std::string("Could not generate " + std::to_string(Crypto::RSA_DEFAULT_KEYLENGTH) + "-bit RSA key."));
|
|
||||||
throw std::runtime_error { std::string("X.509 RSA key generation error") };
|
|
||||||
}
|
|
||||||
// todo: figure out if returning by reference instead of passing pointers is a security breach
|
|
||||||
return PKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
X509* Http::Server::Tx509KeypairGenerator::GenerateCertificate(EVP_PKEY& PKey) {
|
|
||||||
X509* X509 = X509_new();
|
|
||||||
if (X509 == nullptr) {
|
|
||||||
X509_free(X509);
|
|
||||||
beammp_error("Could not allocate memory for X.509 certificate generation.");
|
|
||||||
throw std::runtime_error { std::string("X.509 certificate generation error") };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**Set the metadata of the certificate*/
|
|
||||||
ASN1_INTEGER_set(X509_get_serialNumber(X509), GenerateRandomId());
|
|
||||||
|
|
||||||
/**Set the cert validity to a year*/
|
|
||||||
X509_gmtime_adj(X509_get_notBefore(X509), 0);
|
|
||||||
X509_gmtime_adj(X509_get_notAfter(X509), 31536000L);
|
|
||||||
|
|
||||||
/**Set the public key of the cert*/
|
|
||||||
X509_set_pubkey(X509, &PKey);
|
|
||||||
|
|
||||||
X509_NAME* Name = X509_get_subject_name(X509);
|
|
||||||
|
|
||||||
/**Set cert metadata*/
|
|
||||||
X509_NAME_add_entry_by_txt(Name, "C", MBSTRING_ASC, (unsigned char*)"GB", -1, -1, 0);
|
|
||||||
X509_NAME_add_entry_by_txt(Name, "O", MBSTRING_ASC, (unsigned char*)"BeamMP Ltd.", -1, -1, 0);
|
|
||||||
X509_NAME_add_entry_by_txt(Name, "CN", MBSTRING_ASC, (unsigned char*)"localhost", -1, -1, 0);
|
|
||||||
|
|
||||||
X509_set_issuer_name(X509, Name);
|
|
||||||
|
|
||||||
// TODO: Hashing with sha256 might cause problems, check later
|
|
||||||
if (!X509_sign(X509, &PKey, EVP_sha1())) {
|
|
||||||
X509_free(X509);
|
|
||||||
beammp_error("Could not sign X.509 certificate.");
|
|
||||||
throw std::runtime_error { std::string("X.509 certificate signing error") };
|
|
||||||
}
|
|
||||||
return X509;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Http::Server::Tx509KeypairGenerator::GenerateAndWriteToDisk(const fs::path& KeyFilePath, const fs::path& CertFilePath) {
|
|
||||||
// todo: generate directories for ssl keys
|
|
||||||
FILE* KeyFile = std::fopen(reinterpret_cast<const char*>(KeyFilePath.c_str()), "wb");
|
|
||||||
if (!KeyFile) {
|
|
||||||
beammp_error("Could not create file 'key.pem', check your permissions");
|
|
||||||
throw std::runtime_error("Could not create file 'key.pem'");
|
|
||||||
}
|
|
||||||
|
|
||||||
EVP_PKEY* PKey = Http::Server::Tx509KeypairGenerator::GenerateKey();
|
|
||||||
|
|
||||||
bool WriteOpResult = PEM_write_PrivateKey(KeyFile, PKey, nullptr, nullptr, 0, nullptr, nullptr);
|
|
||||||
fclose(KeyFile);
|
|
||||||
|
|
||||||
if (!WriteOpResult) {
|
|
||||||
beammp_error("Could not write to file 'key.pem', check your permissions");
|
|
||||||
throw std::runtime_error("Could not write to file 'key.pem'");
|
|
||||||
}
|
|
||||||
|
|
||||||
FILE* CertFile = std::fopen(reinterpret_cast<const char*>(CertFilePath.c_str()), "wb"); // x509 file
|
|
||||||
if (!CertFile) {
|
|
||||||
beammp_error("Could not create file 'cert.pem', check your permissions");
|
|
||||||
throw std::runtime_error("Could not create file 'cert.pem'");
|
|
||||||
}
|
|
||||||
|
|
||||||
X509* x509 = Http::Server::Tx509KeypairGenerator::GenerateCertificate(*PKey);
|
|
||||||
WriteOpResult = PEM_write_X509(CertFile, x509);
|
|
||||||
fclose(CertFile);
|
|
||||||
|
|
||||||
if (!WriteOpResult) {
|
|
||||||
beammp_error("Could not write to file 'cert.pem', check your permissions");
|
|
||||||
throw std::runtime_error("Could not write to file 'cert.pem'");
|
|
||||||
}
|
|
||||||
EVP_PKEY_free(PKey);
|
|
||||||
X509_free(x509);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Http::Server::Tx509KeypairGenerator::EnsureTLSConfigExists() {
|
|
||||||
if (fs::is_regular_file(Application::Settings.SSLKeyPath)
|
|
||||||
&& fs::is_regular_file(Application::Settings.SSLCertPath)) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Http::Server::SetupEnvironment() {
|
|
||||||
if (!Application::Settings.HTTPServerUseSSL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
auto parent = fs::path(Application::Settings.SSLKeyPath).parent_path();
|
|
||||||
if (!fs::exists(parent))
|
|
||||||
fs::create_directories(parent);
|
|
||||||
|
|
||||||
Application::TSettings defaultSettings {};
|
|
||||||
if (!Tx509KeypairGenerator::EnsureTLSConfigExists()) {
|
|
||||||
beammp_warn(std::string("No default TLS Key / Cert found. "
|
|
||||||
"IF YOU HAVE NOT MODIFIED THE SSLKeyPath OR SSLCertPath VALUES "
|
|
||||||
"THIS IS NORMAL ON FIRST STARTUP! BeamMP will generate it's own certs in the default directory "
|
|
||||||
"(Check for permissions or corrupted key-/certfile)"));
|
|
||||||
Tx509KeypairGenerator::GenerateAndWriteToDisk(defaultSettings.SSLKeyPath, defaultSettings.SSLCertPath);
|
|
||||||
Http::Server::THttpServerInstance::KeyFilePath = defaultSettings.SSLKeyPath;
|
|
||||||
Http::Server::THttpServerInstance::CertFilePath = defaultSettings.SSLCertPath;
|
|
||||||
} else {
|
|
||||||
Http::Server::THttpServerInstance::KeyFilePath = Application::Settings.SSLKeyPath;
|
|
||||||
Http::Server::THttpServerInstance::CertFilePath = Application::Settings.SSLCertPath;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Http::Server::THttpServerInstance::THttpServerInstance() {
|
Http::Server::THttpServerInstance::THttpServerInstance() {
|
||||||
@@ -296,13 +157,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
|
|||||||
void Http::Server::THttpServerInstance::operator()() try {
|
void Http::Server::THttpServerInstance::operator()() try {
|
||||||
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
|
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
|
||||||
std::unique_ptr<httplib::Server> HttpLibServerInstance;
|
std::unique_ptr<httplib::Server> HttpLibServerInstance;
|
||||||
if (Application::Settings.HTTPServerUseSSL) {
|
|
||||||
HttpLibServerInstance = std::make_unique<httplib::SSLServer>(
|
|
||||||
reinterpret_cast<const char*>(Http::Server::THttpServerInstance::CertFilePath.c_str()),
|
|
||||||
reinterpret_cast<const char*>(Http::Server::THttpServerInstance::KeyFilePath.c_str()));
|
|
||||||
} else {
|
|
||||||
HttpLibServerInstance = std::make_unique<httplib::Server>();
|
HttpLibServerInstance = std::make_unique<httplib::Server>();
|
||||||
}
|
|
||||||
// todo: make this IP agnostic so people can set their own IP
|
// todo: make this IP agnostic so people can set their own IP
|
||||||
HttpLibServerInstance->Get("/", [](const httplib::Request&, httplib::Response& res) {
|
HttpLibServerInstance->Get("/", [](const httplib::Request&, httplib::Response& res) {
|
||||||
res.set_content("<!DOCTYPE html><article><h1>Hello World!</h1><section><p>BeamMP Server can now serve HTTP requests!</p></section></article></html>", "text/html");
|
res.set_content("<!DOCTYPE html><article><h1>Hello World!</h1><section><p>BeamMP Server can now serve HTTP requests!</p></section></article></html>", "text/html");
|
||||||
@@ -324,48 +179,14 @@ void Http::Server::THttpServerInstance::operator()() try {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res.set_content(SystemsBad == 0 ? "0" : "1", "text/plain");
|
res.set_content(
|
||||||
|
json {
|
||||||
|
{ "ok", SystemsBad == 0 },
|
||||||
|
}
|
||||||
|
.dump(),
|
||||||
|
"application/json");
|
||||||
res.status = 200;
|
res.status = 200;
|
||||||
});
|
});
|
||||||
/*
|
|
||||||
HttpLibServerInstance->Get("/status", [](const httplib::Request&, httplib::Response& res) {
|
|
||||||
try {
|
|
||||||
json::Document response;
|
|
||||||
response.SetObject();
|
|
||||||
rapidjson::Document::AllocatorType& Allocator = response.GetAllocator();
|
|
||||||
// add to response
|
|
||||||
auto& Server = LuaAPI::MP::Engine->Server();
|
|
||||||
size_t CarCount = 0;
|
|
||||||
size_t GuestCount = 0;
|
|
||||||
json::Value Array(rapidjson::kArrayType);
|
|
||||||
LuaAPI::MP::Engine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
|
||||||
if (!Client.expired()) {
|
|
||||||
auto Locked = Client.lock();
|
|
||||||
CarCount += Locked->GetCarCount();
|
|
||||||
GuestCount += Locked->IsGuest() ? 1 : 0;
|
|
||||||
json::Value Player(json::kObjectType);
|
|
||||||
Player.AddMember("name", json::StringRef(Locked->GetName().c_str()), Allocator);
|
|
||||||
Player.AddMember("id", Locked->GetID(), Allocator);
|
|
||||||
Array.PushBack(Player, Allocator);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
response.AddMember("players", Array, Allocator);
|
|
||||||
response.AddMember("player_count", Server.ClientCount(), Allocator);
|
|
||||||
response.AddMember("guest_count", GuestCount, Allocator);
|
|
||||||
response.AddMember("car_count", CarCount, Allocator);
|
|
||||||
|
|
||||||
// compile & send response
|
|
||||||
json::StringBuffer sb;
|
|
||||||
json::Writer<json::StringBuffer> writer(sb);
|
|
||||||
response.Accept(writer);
|
|
||||||
res.set_content(sb.GetString(), "application/json");
|
|
||||||
} catch (const std::exception& e) {
|
|
||||||
beammp_error("Exception in /status endpoint: " + std::string(e.what()));
|
|
||||||
res.status = 500;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
// magic endpoint
|
// magic endpoint
|
||||||
HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
|
HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
|
||||||
res.set_content(std::string(Magic), "text/plain");
|
res.set_content(std::string(Magic), "text/plain");
|
||||||
|
|||||||
+312
-16
@@ -3,6 +3,8 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "TLuaEngine.h"
|
#include "TLuaEngine.h"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
#define SOL_ALL_SAFETIES_ON 1
|
#define SOL_ALL_SAFETIES_ON 1
|
||||||
#include <sol/sol.hpp>
|
#include <sol/sol.hpp>
|
||||||
|
|
||||||
@@ -100,26 +102,39 @@ void LuaAPI::Print(sol::variadic_args Args) {
|
|||||||
luaprint(ToPrint);
|
luaprint(ToPrint);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) {
|
TEST_CASE("LuaAPI::MP::GetServerVersion") {
|
||||||
|
const auto [ma, mi, pa] = LuaAPI::MP::GetServerVersion();
|
||||||
|
const auto real = Application::ServerVersion();
|
||||||
|
CHECK(ma == real.major);
|
||||||
|
CHECK(mi == real.minor);
|
||||||
|
CHECK(pa == real.patch);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline bool InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) {
|
||||||
std::string Packet = "E:" + EventName + ":" + Data;
|
std::string Packet = "E:" + EventName + ":" + Data;
|
||||||
if (PlayerID == -1)
|
if (PlayerID == -1)
|
||||||
Engine->Network().SendToAll(nullptr, Packet, true, true);
|
LuaAPI::MP::Engine->Network().SendToAll(nullptr, Packet, true, true);
|
||||||
else {
|
else {
|
||||||
auto MaybeClient = GetClient(Engine->Server(), PlayerID);
|
auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), PlayerID);
|
||||||
if (!MaybeClient || MaybeClient.value().expired()) {
|
if (!MaybeClient || MaybeClient.value().expired()) {
|
||||||
beammp_lua_error("TriggerClientEvent invalid Player ID");
|
beammp_lua_error("TriggerClientEvent invalid Player ID");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto c = MaybeClient.value().lock();
|
auto c = MaybeClient.value().lock();
|
||||||
if (!Engine->Network().Respond(*c, Packet, true)) {
|
if (!LuaAPI::MP::Engine->Network().Respond(*c, Packet, true)) {
|
||||||
beammp_lua_error("Respond failed, dropping client " + std::to_string(PlayerID));
|
beammp_lua_error("Respond failed, dropping client " + std::to_string(PlayerID));
|
||||||
Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets");
|
LuaAPI::MP::Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName, const sol::object& DataObj) {
|
||||||
|
std::string Data = DataObj.as<std::string>();
|
||||||
|
return InternalTriggerClientEvent(PlayerID, EventName, Data);
|
||||||
|
}
|
||||||
|
|
||||||
void LuaAPI::MP::DropPlayer(int ID, std::optional<std::string> MaybeReason) {
|
void LuaAPI::MP::DropPlayer(int ID, std::optional<std::string> MaybeReason) {
|
||||||
auto MaybeClient = GetClient(Engine->Server(), ID);
|
auto MaybeClient = GetClient(Engine->Server(), ID);
|
||||||
if (!MaybeClient || MaybeClient.value().expired()) {
|
if (!MaybeClient || MaybeClient.value().expired()) {
|
||||||
@@ -169,50 +184,57 @@ void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
|
|||||||
if (NewValue.is<bool>()) {
|
if (NewValue.is<bool>()) {
|
||||||
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
|
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
|
||||||
beammp_info(std::string("Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
|
beammp_info(std::string("Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected boolean");
|
beammp_lua_error("set invalid argument [2] expected boolean");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 1: // private
|
case 1: // private
|
||||||
if (NewValue.is<bool>()) {
|
if (NewValue.is<bool>()) {
|
||||||
Application::Settings.Private = NewValue.as<bool>();
|
Application::Settings.Private = NewValue.as<bool>();
|
||||||
beammp_info(std::string("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
|
beammp_info(std::string("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected boolean");
|
beammp_lua_error("set invalid argument [2] expected boolean");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 2: // max cars
|
case 2: // max cars
|
||||||
if (NewValue.is<int>()) {
|
if (NewValue.is<int>()) {
|
||||||
Application::Settings.MaxCars = NewValue.as<int>();
|
Application::Settings.MaxCars = NewValue.as<int>();
|
||||||
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
|
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected integer");
|
beammp_lua_error("set invalid argument [2] expected integer");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 3: // max players
|
case 3: // max players
|
||||||
if (NewValue.is<int>()) {
|
if (NewValue.is<int>()) {
|
||||||
Application::Settings.MaxPlayers = NewValue.as<int>();
|
Application::Settings.MaxPlayers = NewValue.as<int>();
|
||||||
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
|
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected integer");
|
beammp_lua_error("set invalid argument [2] expected integer");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 4: // Map
|
case 4: // Map
|
||||||
if (NewValue.is<std::string>()) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.MapName = NewValue.as<std::string>();
|
Application::Settings.MapName = NewValue.as<std::string>();
|
||||||
beammp_info(std::string("Set `Map` to ") + Application::Settings.MapName);
|
beammp_info(std::string("Set `Map` to ") + Application::Settings.MapName);
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected string");
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 5: // Name
|
case 5: // Name
|
||||||
if (NewValue.is<std::string>()) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.ServerName = NewValue.as<std::string>();
|
Application::Settings.ServerName = NewValue.as<std::string>();
|
||||||
beammp_info(std::string("Set `Name` to ") + Application::Settings.ServerName);
|
beammp_info(std::string("Set `Name` to ") + Application::Settings.ServerName);
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected string");
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 6: // Desc
|
case 6: // Desc
|
||||||
if (NewValue.is<std::string>()) {
|
if (NewValue.is<std::string>()) {
|
||||||
Application::Settings.ServerDesc = NewValue.as<std::string>();
|
Application::Settings.ServerDesc = NewValue.as<std::string>();
|
||||||
beammp_info(std::string("Set `Description` to ") + Application::Settings.ServerDesc);
|
beammp_info(std::string("Set `Description` to ") + Application::Settings.ServerDesc);
|
||||||
} else
|
} else {
|
||||||
beammp_lua_error("set invalid argument [2] expected string");
|
beammp_lua_error("set invalid argument [2] expected string");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
beammp_warn("Invalid config ID \"" + std::to_string(ConfigID) + "\". Use `MP.Settings.*` enum for this.");
|
beammp_warn("Invalid config ID \"" + std::to_string(ConfigID) + "\". Use `MP.Settings.*` enum for this.");
|
||||||
@@ -248,7 +270,9 @@ void LuaAPI::MP::PrintRaw(sol::variadic_args Args) {
|
|||||||
ToPrint += LuaToString(static_cast<const sol::object>(Arg));
|
ToPrint += LuaToString(static_cast<const sol::object>(Arg));
|
||||||
ToPrint += "\t";
|
ToPrint += "\t";
|
||||||
}
|
}
|
||||||
|
#ifdef DOCTEST_CONFIG_DISABLE
|
||||||
Application::Console().WriteRaw(ToPrint);
|
Application::Console().WriteRaw(ToPrint);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int LuaAPI::PanicHandler(lua_State* State) {
|
int LuaAPI::PanicHandler(lua_State* State) {
|
||||||
@@ -271,7 +295,7 @@ static std::pair<bool, std::string> FSWrapper(FnT Fn, ArgsT&&... Args) {
|
|||||||
std::pair<bool, std::string> LuaAPI::FS::CreateDirectory(const std::string& Path) {
|
std::pair<bool, std::string> LuaAPI::FS::CreateDirectory(const std::string& Path) {
|
||||||
std::error_code errc;
|
std::error_code errc;
|
||||||
std::pair<bool, std::string> Result;
|
std::pair<bool, std::string> Result;
|
||||||
fs::create_directories(fs::relative(Path), errc);
|
fs::create_directories(Path, errc);
|
||||||
Result.first = errc == std::error_code {};
|
Result.first = errc == std::error_code {};
|
||||||
if (!Result.first) {
|
if (!Result.first) {
|
||||||
Result.second = errc.message();
|
Result.second = errc.message();
|
||||||
@@ -279,6 +303,33 @@ std::pair<bool, std::string> LuaAPI::FS::CreateDirectory(const std::string& Path
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::CreateDirectory") {
|
||||||
|
std::string TestDir = "beammp_test_dir";
|
||||||
|
fs::remove_all(TestDir);
|
||||||
|
SUBCASE("Single level dir") {
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK(Err == "");
|
||||||
|
CHECK(fs::exists(TestDir));
|
||||||
|
}
|
||||||
|
SUBCASE("Multi level dir") {
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir + "/a/b/c");
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK(Err == "");
|
||||||
|
CHECK(fs::exists(TestDir + "/a/b/c"));
|
||||||
|
}
|
||||||
|
SUBCASE("Already exists") {
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::CreateDirectory(TestDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK(Err == "");
|
||||||
|
CHECK(fs::exists(TestDir));
|
||||||
|
const auto [Ok2, Err2] = LuaAPI::FS::CreateDirectory(TestDir);
|
||||||
|
CHECK(Ok2);
|
||||||
|
CHECK(Err2 == "");
|
||||||
|
}
|
||||||
|
fs::remove_all(TestDir);
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> LuaAPI::FS::Remove(const std::string& Path) {
|
std::pair<bool, std::string> LuaAPI::FS::Remove(const std::string& Path) {
|
||||||
std::error_code errc;
|
std::error_code errc;
|
||||||
std::pair<bool, std::string> Result;
|
std::pair<bool, std::string> Result;
|
||||||
@@ -290,10 +341,30 @@ std::pair<bool, std::string> LuaAPI::FS::Remove(const std::string& Path) {
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::Remove") {
|
||||||
|
const std::string TestFileOrDir = "beammp_test_thing";
|
||||||
|
SUBCASE("Remove existing directory") {
|
||||||
|
fs::create_directory(TestFileOrDir);
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::Remove(TestFileOrDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK_EQ(Err, "");
|
||||||
|
CHECK(!fs::exists(TestFileOrDir));
|
||||||
|
}
|
||||||
|
SUBCASE("Remove non-existing directory") {
|
||||||
|
fs::remove_all(TestFileOrDir);
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::Remove(TestFileOrDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK_EQ(Err, "");
|
||||||
|
CHECK(!fs::exists(TestFileOrDir));
|
||||||
|
}
|
||||||
|
// TODO: add tests for files
|
||||||
|
// TODO: add tests for files and folders without access permissions (failure)
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> LuaAPI::FS::Rename(const std::string& Path, const std::string& NewPath) {
|
std::pair<bool, std::string> LuaAPI::FS::Rename(const std::string& Path, const std::string& NewPath) {
|
||||||
std::error_code errc;
|
std::error_code errc;
|
||||||
std::pair<bool, std::string> Result;
|
std::pair<bool, std::string> Result;
|
||||||
fs::rename(fs::relative(Path), fs::relative(NewPath), errc);
|
fs::rename(Path, NewPath, errc);
|
||||||
Result.first = errc == std::error_code {};
|
Result.first = errc == std::error_code {};
|
||||||
if (!Result.first) {
|
if (!Result.first) {
|
||||||
Result.second = errc.message();
|
Result.second = errc.message();
|
||||||
@@ -301,10 +372,25 @@ std::pair<bool, std::string> LuaAPI::FS::Rename(const std::string& Path, const s
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::Rename") {
|
||||||
|
const auto TestDir = "beammp_test_dir";
|
||||||
|
const auto OtherTestDir = "beammp_test_dir_2";
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::create_directory(TestDir);
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::Rename(TestDir, OtherTestDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK_EQ(Err, "");
|
||||||
|
CHECK(!fs::exists(TestDir));
|
||||||
|
CHECK(fs::exists(OtherTestDir));
|
||||||
|
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::remove_all(TestDir);
|
||||||
|
}
|
||||||
|
|
||||||
std::pair<bool, std::string> LuaAPI::FS::Copy(const std::string& Path, const std::string& NewPath) {
|
std::pair<bool, std::string> LuaAPI::FS::Copy(const std::string& Path, const std::string& NewPath) {
|
||||||
std::error_code errc;
|
std::error_code errc;
|
||||||
std::pair<bool, std::string> Result;
|
std::pair<bool, std::string> Result;
|
||||||
fs::copy(fs::relative(Path), fs::relative(NewPath), fs::copy_options::recursive, errc);
|
fs::copy(Path, NewPath, fs::copy_options::recursive, errc);
|
||||||
Result.first = errc == std::error_code {};
|
Result.first = errc == std::error_code {};
|
||||||
if (!Result.first) {
|
if (!Result.first) {
|
||||||
Result.second = errc.message();
|
Result.second = errc.message();
|
||||||
@@ -312,30 +398,86 @@ std::pair<bool, std::string> LuaAPI::FS::Copy(const std::string& Path, const std
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::Copy") {
|
||||||
|
const auto TestDir = "beammp_test_dir";
|
||||||
|
const auto OtherTestDir = "beammp_test_dir_2";
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::create_directory(TestDir);
|
||||||
|
const auto [Ok, Err] = LuaAPI::FS::Copy(TestDir, OtherTestDir);
|
||||||
|
CHECK(Ok);
|
||||||
|
CHECK_EQ(Err, "");
|
||||||
|
CHECK(fs::exists(TestDir));
|
||||||
|
CHECK(fs::exists(OtherTestDir));
|
||||||
|
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::remove_all(TestDir);
|
||||||
|
}
|
||||||
|
|
||||||
bool LuaAPI::FS::Exists(const std::string& Path) {
|
bool LuaAPI::FS::Exists(const std::string& Path) {
|
||||||
return fs::exists(fs::relative(Path));
|
return fs::exists(Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::Exists") {
|
||||||
|
const auto TestDir = "beammp_test_dir";
|
||||||
|
const auto OtherTestDir = "beammp_test_dir_2";
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::create_directory(TestDir);
|
||||||
|
|
||||||
|
CHECK(LuaAPI::FS::Exists(TestDir));
|
||||||
|
CHECK(!LuaAPI::FS::Exists(OtherTestDir));
|
||||||
|
|
||||||
|
fs::remove_all(OtherTestDir);
|
||||||
|
fs::remove_all(TestDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string LuaAPI::FS::GetFilename(const std::string& Path) {
|
std::string LuaAPI::FS::GetFilename(const std::string& Path) {
|
||||||
return fs::path(Path).filename().string();
|
return fs::path(Path).filename().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::GetFilename") {
|
||||||
|
CHECK(LuaAPI::FS::GetFilename("test.txt") == "test.txt");
|
||||||
|
CHECK(LuaAPI::FS::GetFilename("/test.txt") == "test.txt");
|
||||||
|
CHECK(LuaAPI::FS::GetFilename("place/test.txt") == "test.txt");
|
||||||
|
CHECK(LuaAPI::FS::GetFilename("/some/../place/test.txt") == "test.txt");
|
||||||
|
}
|
||||||
|
|
||||||
std::string LuaAPI::FS::GetExtension(const std::string& Path) {
|
std::string LuaAPI::FS::GetExtension(const std::string& Path) {
|
||||||
return fs::path(Path).extension().string();
|
return fs::path(Path).extension().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::GetExtension") {
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("test.txt") == ".txt");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/test.txt") == ".txt");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("place/test.txt") == ".txt");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/some/../place/test.txt") == ".txt");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/some/../place/test") == "");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/some/../place/test.a.b.c") == ".c");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/some/../place/test.") == ".");
|
||||||
|
CHECK(LuaAPI::FS::GetExtension("/some/../place/test.a.b.") == ".");
|
||||||
|
}
|
||||||
|
|
||||||
std::string LuaAPI::FS::GetParentFolder(const std::string& Path) {
|
std::string LuaAPI::FS::GetParentFolder(const std::string& Path) {
|
||||||
return fs::path(Path).parent_path().string();
|
return fs::path(Path).parent_path().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("LuaAPI::FS::GetParentFolder") {
|
||||||
|
CHECK(LuaAPI::FS::GetParentFolder("test.txt") == "");
|
||||||
|
CHECK(LuaAPI::FS::GetParentFolder("/test.txt") == "/");
|
||||||
|
CHECK(LuaAPI::FS::GetParentFolder("place/test.txt") == "place");
|
||||||
|
CHECK(LuaAPI::FS::GetParentFolder("/some/../place/test.txt") == "/some/../place");
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: add tests
|
||||||
bool LuaAPI::FS::IsDirectory(const std::string& Path) {
|
bool LuaAPI::FS::IsDirectory(const std::string& Path) {
|
||||||
return fs::is_directory(Path);
|
return fs::is_directory(Path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add tests
|
||||||
bool LuaAPI::FS::IsFile(const std::string& Path) {
|
bool LuaAPI::FS::IsFile(const std::string& Path) {
|
||||||
return fs::is_regular_file(Path);
|
return fs::is_regular_file(Path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: add tests
|
||||||
std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
|
std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
|
||||||
fs::path Path;
|
fs::path Path;
|
||||||
for (size_t i = 0; i < Args.size(); ++i) {
|
for (size_t i = 0; i < Args.size(); ++i) {
|
||||||
@@ -352,3 +494,157 @@ std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
|
|||||||
auto Result = Path.lexically_normal().string();
|
auto Result = Path.lexically_normal().string();
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, const sol::object& right, bool is_array, size_t depth = 0) {
|
||||||
|
if (depth > 100) {
|
||||||
|
beammp_lua_error("json serialize will not go deeper than 100 nested tables, internal references assumed, aborted this path");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string key;
|
||||||
|
switch (left.get_type()) {
|
||||||
|
case sol::type::lua_nil:
|
||||||
|
case sol::type::none:
|
||||||
|
case sol::type::poly:
|
||||||
|
case sol::type::boolean:
|
||||||
|
case sol::type::lightuserdata:
|
||||||
|
case sol::type::userdata:
|
||||||
|
case sol::type::thread:
|
||||||
|
case sol::type::function:
|
||||||
|
case sol::type::table:
|
||||||
|
beammp_lua_error("JsonEncode: left side of table field is unexpected type");
|
||||||
|
return;
|
||||||
|
case sol::type::string:
|
||||||
|
key = left.as<std::string>();
|
||||||
|
break;
|
||||||
|
case sol::type::number:
|
||||||
|
key = std::to_string(left.as<double>());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
nlohmann::json value;
|
||||||
|
switch (right.get_type()) {
|
||||||
|
case sol::type::lua_nil:
|
||||||
|
case sol::type::none:
|
||||||
|
return;
|
||||||
|
case sol::type::poly:
|
||||||
|
beammp_lua_warn("unsure what to do with poly type in JsonEncode, ignoring");
|
||||||
|
return;
|
||||||
|
case sol::type::boolean:
|
||||||
|
value = right.as<bool>();
|
||||||
|
break;
|
||||||
|
case sol::type::lightuserdata:
|
||||||
|
beammp_lua_warn("unsure what to do with lightuserdata in JsonEncode, ignoring");
|
||||||
|
return;
|
||||||
|
case sol::type::userdata:
|
||||||
|
beammp_lua_warn("unsure what to do with userdata in JsonEncode, ignoring");
|
||||||
|
return;
|
||||||
|
case sol::type::thread:
|
||||||
|
beammp_lua_warn("unsure what to do with thread in JsonEncode, ignoring");
|
||||||
|
return;
|
||||||
|
case sol::type::string:
|
||||||
|
value = right.as<std::string>();
|
||||||
|
break;
|
||||||
|
case sol::type::number:
|
||||||
|
value = right.as<double>();
|
||||||
|
break;
|
||||||
|
case sol::type::function:
|
||||||
|
beammp_lua_warn("unsure what to do with function in JsonEncode, ignoring");
|
||||||
|
return;
|
||||||
|
case sol::type::table: {
|
||||||
|
bool local_is_array = true;
|
||||||
|
for (const auto& pair : right.as<sol::table>()) {
|
||||||
|
if (pair.first.get_type() != sol::type::number) {
|
||||||
|
local_is_array = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& pair : right.as<sol::table>()) {
|
||||||
|
JsonEncodeRecursive(value, pair.first, pair.second, local_is_array, depth + 1);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (is_array) {
|
||||||
|
json.push_back(value);
|
||||||
|
} else {
|
||||||
|
json[key] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonEncode(const sol::table& object) {
|
||||||
|
nlohmann::json json;
|
||||||
|
// table
|
||||||
|
bool is_array = true;
|
||||||
|
for (const auto& pair : object.as<sol::table>()) {
|
||||||
|
if (pair.first.get_type() != sol::type::number) {
|
||||||
|
is_array = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const auto& entry : object) {
|
||||||
|
JsonEncodeRecursive(json, entry.first, entry.second, is_array);
|
||||||
|
}
|
||||||
|
return json.dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonDiff(const std::string& a, const std::string& b) {
|
||||||
|
if (!nlohmann::json::accept(a)) {
|
||||||
|
beammp_lua_error("JsonDiff first argument is not valid json: `" + a + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!nlohmann::json::accept(b)) {
|
||||||
|
beammp_lua_error("JsonDiff second argument is not valid json: `" + b + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
auto a_json = nlohmann::json::parse(a);
|
||||||
|
auto b_json = nlohmann::json::parse(b);
|
||||||
|
return nlohmann::json::diff(a_json, b_json).dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonDiffApply(const std::string& data, const std::string& patch) {
|
||||||
|
if (!nlohmann::json::accept(data)) {
|
||||||
|
beammp_lua_error("JsonDiffApply first argument is not valid json: `" + data + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
if (!nlohmann::json::accept(patch)) {
|
||||||
|
beammp_lua_error("JsonDiffApply second argument is not valid json: `" + patch + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
auto a_json = nlohmann::json::parse(data);
|
||||||
|
auto b_json = nlohmann::json::parse(patch);
|
||||||
|
a_json.patch(b_json);
|
||||||
|
return a_json.dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonPrettify(const std::string& json) {
|
||||||
|
if (!nlohmann::json::accept(json)) {
|
||||||
|
beammp_lua_error("JsonPrettify argument is not valid json: `" + json + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return nlohmann::json::parse(json).dump(4);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonMinify(const std::string& json) {
|
||||||
|
if (!nlohmann::json::accept(json)) {
|
||||||
|
beammp_lua_error("JsonMinify argument is not valid json: `" + json + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return nlohmann::json::parse(json).dump(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonFlatten(const std::string& json) {
|
||||||
|
if (!nlohmann::json::accept(json)) {
|
||||||
|
beammp_lua_error("JsonFlatten argument is not valid json: `" + json + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return nlohmann::json::parse(json).flatten().dump(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string LuaAPI::MP::JsonUnflatten(const std::string& json) {
|
||||||
|
if (!nlohmann::json::accept(json)) {
|
||||||
|
beammp_lua_error("JsonUnflatten argument is not valid json: `" + json + "`");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return nlohmann::json::parse(json).unflatten().dump(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LuaAPI::MP::TriggerClientEventJson(int PlayerID, const std::string& EventName, const sol::table& Data) {
|
||||||
|
return InternalTriggerClientEvent(PlayerID, EventName, JsonEncode(Data));
|
||||||
|
}
|
||||||
|
|||||||
+72
-44
@@ -17,6 +17,8 @@ static constexpr std::string_view StrName = "Name";
|
|||||||
static constexpr std::string_view StrDescription = "Description";
|
static constexpr std::string_view StrDescription = "Description";
|
||||||
static constexpr std::string_view StrResourceFolder = "ResourceFolder";
|
static constexpr std::string_view StrResourceFolder = "ResourceFolder";
|
||||||
static constexpr std::string_view StrAuthKey = "AuthKey";
|
static constexpr std::string_view StrAuthKey = "AuthKey";
|
||||||
|
static constexpr std::string_view StrLogChat = "LogChat";
|
||||||
|
static constexpr std::string_view StrDownloadThreads = "DownloadThreads";
|
||||||
|
|
||||||
// Misc
|
// Misc
|
||||||
static constexpr std::string_view StrSendErrors = "SendErrors";
|
static constexpr std::string_view StrSendErrors = "SendErrors";
|
||||||
@@ -31,12 +33,37 @@ static constexpr std::string_view StrSSLCertPath = "SSLCertPath";
|
|||||||
static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
|
static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
|
||||||
static constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
|
static constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
|
||||||
|
|
||||||
|
TEST_CASE("TConfig::TConfig") {
|
||||||
|
const std::string CfgFile = "beammp_server_testconfig.toml";
|
||||||
|
fs::remove(CfgFile);
|
||||||
|
|
||||||
|
TConfig Cfg(CfgFile);
|
||||||
|
|
||||||
|
CHECK(fs::file_size(CfgFile) != 0);
|
||||||
|
|
||||||
|
std::string buf;
|
||||||
|
{
|
||||||
|
buf.resize(fs::file_size(CfgFile));
|
||||||
|
auto fp = std::fopen(CfgFile.c_str(), "r");
|
||||||
|
std::fread(buf.data(), 1, buf.size(), fp);
|
||||||
|
std::fclose(fp);
|
||||||
|
}
|
||||||
|
INFO("file contents are:", buf);
|
||||||
|
|
||||||
|
const auto table = toml::parse(CfgFile);
|
||||||
|
CHECK(table.at("General").is_table());
|
||||||
|
CHECK(table.at("Misc").is_table());
|
||||||
|
CHECK(table.at("HTTP").is_table());
|
||||||
|
|
||||||
|
fs::remove(CfgFile);
|
||||||
|
}
|
||||||
|
|
||||||
TConfig::TConfig(const std::string& ConfigFileName)
|
TConfig::TConfig(const std::string& ConfigFileName)
|
||||||
: mConfigFileName(ConfigFileName) {
|
: mConfigFileName(ConfigFileName) {
|
||||||
Application::SetSubsystemStatus("Config", Application::Status::Starting);
|
Application::SetSubsystemStatus("Config", Application::Status::Starting);
|
||||||
if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) {
|
if (!fs::exists(mConfigFileName) || !fs::is_regular_file(mConfigFileName)) {
|
||||||
beammp_info("No config file found! Generating one...");
|
beammp_info("No config file found! Generating one...");
|
||||||
CreateConfigFile(mConfigFileName);
|
CreateConfigFile();
|
||||||
}
|
}
|
||||||
if (!mFailed) {
|
if (!mFailed) {
|
||||||
if (fs::exists("Server.cfg")) {
|
if (fs::exists("Server.cfg")) {
|
||||||
@@ -62,9 +89,12 @@ void SetComment(CommentsT& Comments, const std::string& Comment) {
|
|||||||
* whether it is in TConfig.cpp or the configuration file.
|
* whether it is in TConfig.cpp or the configuration file.
|
||||||
*/
|
*/
|
||||||
void TConfig::FlushToFile() {
|
void TConfig::FlushToFile() {
|
||||||
auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
|
// auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
|
||||||
|
auto data = toml::value {};
|
||||||
data["General"][StrAuthKey.data()] = Application::Settings.Key;
|
data["General"][StrAuthKey.data()] = Application::Settings.Key;
|
||||||
SetComment(data["General"][StrAuthKey.data()].comments(), " AuthKey has to be filled out in order to run the server");
|
SetComment(data["General"][StrAuthKey.data()].comments(), " AuthKey has to be filled out in order to run the server");
|
||||||
|
data["General"][StrLogChat.data()] = Application::Settings.LogChat;
|
||||||
|
SetComment(data["General"][StrLogChat.data()].comments(), " Whether to log chat messages in the console / log");
|
||||||
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
|
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
|
||||||
data["General"][StrPrivate.data()] = Application::Settings.Private;
|
data["General"][StrPrivate.data()] = Application::Settings.Private;
|
||||||
data["General"][StrPort.data()] = Application::Settings.Port;
|
data["General"][StrPort.data()] = Application::Settings.Port;
|
||||||
@@ -74,6 +104,8 @@ void TConfig::FlushToFile() {
|
|||||||
data["General"][StrMap.data()] = Application::Settings.MapName;
|
data["General"][StrMap.data()] = Application::Settings.MapName;
|
||||||
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
|
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
|
||||||
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
|
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
|
||||||
|
data["General"][StrDownloadThreads.data()] = Application::Settings.DownloadThreads;
|
||||||
|
SetComment(data["General"][StrDownloadThreads.data()].comments(), " How many simultaneous downloads may run at the same time");
|
||||||
// Misc
|
// Misc
|
||||||
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages;
|
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages;
|
||||||
SetComment(data["Misc"][StrHideUpdateMessages.data()].comments(), " Hides the periodic update message which notifies you of a new server version. You should really keep this on and always update as soon as possible. For more information visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server. An update message will always appear at startup regardless.");
|
SetComment(data["Misc"][StrHideUpdateMessages.data()].comments(), " Hides the periodic update message which notifies you of a new server version. You should really keep this on and always update as soon as possible. For more information visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server. An update message will always appear at startup regardless.");
|
||||||
@@ -91,11 +123,25 @@ void TConfig::FlushToFile() {
|
|||||||
SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to have enabled for servers which face the internet. With SSL the server will serve https and requires valid key and cert files");
|
SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to have enabled for servers which face the internet. With SSL the server will serve https and requires valid key and cert files");
|
||||||
data["HTTP"][StrHTTPServerEnabled.data()] = Application::Settings.HTTPServerEnabled;
|
data["HTTP"][StrHTTPServerEnabled.data()] = Application::Settings.HTTPServerEnabled;
|
||||||
SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server");
|
SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server");
|
||||||
std::ofstream Stream(mConfigFileName, std::ios::trunc | std::ios::out);
|
std::stringstream Ss;
|
||||||
Stream << data << std::flush;
|
Ss << "# This is the BeamMP-Server config file.\n"
|
||||||
|
"# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\n"
|
||||||
|
"# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n"
|
||||||
|
<< data;
|
||||||
|
auto File = std::fopen(mConfigFileName.c_str(), "w+");
|
||||||
|
if (!File) {
|
||||||
|
beammp_error("Failed to create/write to config file: " + GetPlatformAgnosticErrorString());
|
||||||
|
throw std::runtime_error("Failed to create/write to config file");
|
||||||
|
}
|
||||||
|
auto Str = Ss.str();
|
||||||
|
auto N = std::fwrite(Str.data(), sizeof(char), Str.size(), File);
|
||||||
|
if (N != Str.size()) {
|
||||||
|
beammp_error("Failed to write to config file properly, config file might be misshapen");
|
||||||
|
}
|
||||||
|
std::fclose(File);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConfig::CreateConfigFile(std::string_view name) {
|
void TConfig::CreateConfigFile() {
|
||||||
// build from old config Server.cfg
|
// build from old config Server.cfg
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -107,32 +153,7 @@ void TConfig::CreateConfigFile(std::string_view name) {
|
|||||||
beammp_error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
|
beammp_error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
|
|
||||||
{ // create file context
|
|
||||||
std::ofstream ofs(name.data());
|
|
||||||
}
|
|
||||||
|
|
||||||
FlushToFile();
|
FlushToFile();
|
||||||
|
|
||||||
size_t FileSize = fs::file_size(name);
|
|
||||||
std::fstream ofs { std::string(name), std::ios::in | std::ios::out };
|
|
||||||
if (ofs.good()) {
|
|
||||||
std::string Contents {};
|
|
||||||
Contents.resize(FileSize);
|
|
||||||
ofs.readsome(Contents.data(), FileSize);
|
|
||||||
ofs.seekp(0);
|
|
||||||
ofs << "# This is the BeamMP-Server config file.\n"
|
|
||||||
"# Help & Documentation: `https://wiki.beammp.com/en/home/server-maintenance`\n"
|
|
||||||
"# IMPORTANT: Fill in the AuthKey with the key you got from `https://beammp.com/k/dashboard` on the left under \"Keys\"\n"
|
|
||||||
<< '\n'
|
|
||||||
<< Contents;
|
|
||||||
beammp_error("There was no \"" + std::string(mConfigFileName) + "\" file (this is normal for the first time running the server), so one was generated for you. It was automatically filled with the settings from your Server.cfg, if you have one. Please open ServerConfig.toml and ensure your AuthKey and other settings are filled in and correct, then restart the server. The old Server.cfg file will no longer be used and causes a warning if it exists from now on.");
|
|
||||||
mFailed = true;
|
|
||||||
ofs.close();
|
|
||||||
} else {
|
|
||||||
beammp_error("Couldn't create " + std::string(name) + ". Check permissions, try again, and contact support if it continues not to work.");
|
|
||||||
Application::SetSubsystemStatus("Config", Application::Status::Bad);
|
|
||||||
mFailed = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue) {
|
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, std::string& OutValue) {
|
||||||
@@ -167,6 +188,8 @@ void TConfig::ParseFromFile(std::string_view name) {
|
|||||||
TryReadValue(data, "General", StrDescription, Application::Settings.ServerDesc);
|
TryReadValue(data, "General", StrDescription, Application::Settings.ServerDesc);
|
||||||
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
|
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
|
||||||
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
|
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
|
||||||
|
TryReadValue(data, "General", StrLogChat, Application::Settings.LogChat);
|
||||||
|
TryReadValue(data, "General", StrDownloadThreads, Application::Settings.DownloadThreads);
|
||||||
// Misc
|
// Misc
|
||||||
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
|
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
|
||||||
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
|
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
|
||||||
@@ -202,21 +225,26 @@ void TConfig::ParseFromFile(std::string_view name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TConfig::PrintDebug() {
|
void TConfig::PrintDebug() {
|
||||||
beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
|
beammp_debugf("{}: {}", StrDebug, Application::Settings.DebugModeEnabled ? "true" : "false");
|
||||||
beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false"));
|
beammp_debugf("{}: {}", StrPrivate, Application::Settings.Private ? "true" : "false");
|
||||||
beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port));
|
beammp_debugf("{}: {}", StrPort, Application::Settings.Port);
|
||||||
beammp_debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars));
|
beammp_debugf("{}: {}", StrMaxCars, Application::Settings.MaxCars);
|
||||||
beammp_debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers));
|
beammp_debugf("{}: {}", StrMaxPlayers, Application::Settings.MaxPlayers);
|
||||||
beammp_debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\"");
|
beammp_debugf("{}: '{}'", StrMap, Application::Settings.MapName);
|
||||||
beammp_debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\"");
|
beammp_debugf("{}: '{}'", StrName, Application::Settings.ServerName);
|
||||||
beammp_debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\"");
|
beammp_debugf("{}: '{}'", StrDescription, Application::Settings.ServerDesc);
|
||||||
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
|
beammp_debugf("{}: {}", StrLogChat, Application::Settings.LogChat ? "true" : "false");
|
||||||
beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\"");
|
beammp_debugf("{}: '{}'", StrResourceFolder, Application::Settings.Resource);
|
||||||
beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\"");
|
beammp_debugf("{}: '{}'", StrSSLKeyPath, Application::Settings.SSLKeyPath);
|
||||||
beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\"");
|
beammp_debugf("{}: '{}'", StrSSLCertPath, Application::Settings.SSLCertPath);
|
||||||
beammp_debug(std::string(StrHTTPServerIP) + ": \"" + Application::Settings.HTTPServerIP + "\"");
|
beammp_debugf("{}: {}", StrHTTPServerPort, Application::Settings.HTTPServerPort);
|
||||||
|
beammp_debugf("{}: '{}'", StrHTTPServerIP, Application::Settings.HTTPServerIP);
|
||||||
|
beammp_debugf("{}: {}", StrDownloadThreads, Application::Settings.DownloadThreads);
|
||||||
|
if (Application::Settings.DownloadThreads < 1) {
|
||||||
|
beammp_warnf("'{}' of <1 is not allowed, will use 1", StrDownloadThreads);
|
||||||
|
}
|
||||||
// special!
|
// special!
|
||||||
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
|
beammp_debugf("Key Length: {}", Application::Settings.Key.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConfig::ParseOldFormat() {
|
void TConfig::ParseOldFormat() {
|
||||||
|
|||||||
+279
-50
@@ -14,6 +14,17 @@ static inline bool StringStartsWith(const std::string& What, const std::string&
|
|||||||
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
|
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("StringStartsWith") {
|
||||||
|
CHECK(StringStartsWith("Hello, World", "Hello"));
|
||||||
|
CHECK(StringStartsWith("Hello, World", "H"));
|
||||||
|
CHECK(StringStartsWith("Hello, World", ""));
|
||||||
|
CHECK(!StringStartsWith("Hello, World", "ello"));
|
||||||
|
CHECK(!StringStartsWith("Hello, World", "World"));
|
||||||
|
CHECK(StringStartsWith("", ""));
|
||||||
|
CHECK(!StringStartsWith("", "hello"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trims leading and trailing spaces, newlines, tabs, etc.
|
||||||
static inline std::string TrimString(std::string S) {
|
static inline std::string TrimString(std::string S) {
|
||||||
S.erase(S.begin(), std::find_if(S.begin(), S.end(), [](unsigned char ch) {
|
S.erase(S.begin(), std::find_if(S.begin(), S.end(), [](unsigned char ch) {
|
||||||
return !std::isspace(ch);
|
return !std::isspace(ch);
|
||||||
@@ -25,6 +36,21 @@ static inline std::string TrimString(std::string S) {
|
|||||||
return S;
|
return S;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("TrimString") {
|
||||||
|
CHECK(TrimString("hel lo") == "hel lo");
|
||||||
|
CHECK(TrimString(" hel lo") == "hel lo");
|
||||||
|
CHECK(TrimString(" hel lo ") == "hel lo");
|
||||||
|
CHECK(TrimString("hel lo ") == "hel lo");
|
||||||
|
CHECK(TrimString(" hel lo") == "hel lo");
|
||||||
|
CHECK(TrimString("hel lo ") == "hel lo");
|
||||||
|
CHECK(TrimString(" hel lo ") == "hel lo");
|
||||||
|
CHECK(TrimString("\t\thel\nlo\n\n") == "hel\nlo");
|
||||||
|
CHECK(TrimString("\n\thel\tlo\n\t") == "hel\tlo");
|
||||||
|
CHECK(TrimString(" ") == "");
|
||||||
|
CHECK(TrimString(" \t\n\r ") == "");
|
||||||
|
CHECK(TrimString("") == "");
|
||||||
|
}
|
||||||
|
|
||||||
std::string GetDate() {
|
std::string GetDate() {
|
||||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||||
time_t tt = std::chrono::system_clock::to_time_t(now);
|
time_t tt = std::chrono::system_clock::to_time_t(now);
|
||||||
@@ -118,10 +144,10 @@ void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
|
|||||||
mStateId = LuaStateId;
|
mStateId = LuaStateId;
|
||||||
mIsLuaConsole = true;
|
mIsLuaConsole = true;
|
||||||
if (mStateId != mDefaultStateId) {
|
if (mStateId != mDefaultStateId) {
|
||||||
Application::Console().WriteRaw("Entered Lua console for state '" + mStateId + "'. To exit, type `exit()`");
|
Application::Console().WriteRaw("Attached to Lua state '" + mStateId + "'. For help, type `:help`. To detach, type `:detach`");
|
||||||
mCommandline.set_prompt("lua @" + LuaStateId + "> ");
|
mCommandline.set_prompt("lua @" + LuaStateId + "> ");
|
||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Entered Lua console. To exit, type `exit()`");
|
Application::Console().WriteRaw("Attached to Lua. For help, type `:help`. To detach, type `:detach`");
|
||||||
mCommandline.set_prompt("lua> ");
|
mCommandline.set_prompt("lua> ");
|
||||||
}
|
}
|
||||||
mCachedRegularHistory = mCommandline.history();
|
mCachedRegularHistory = mCommandline.history();
|
||||||
@@ -133,9 +159,9 @@ void TConsole::ChangeToRegularConsole() {
|
|||||||
if (mIsLuaConsole) {
|
if (mIsLuaConsole) {
|
||||||
mIsLuaConsole = false;
|
mIsLuaConsole = false;
|
||||||
if (mStateId != mDefaultStateId) {
|
if (mStateId != mDefaultStateId) {
|
||||||
Application::Console().WriteRaw("Left Lua console for state '" + mStateId + "'.");
|
Application::Console().WriteRaw("Detached from Lua state '" + mStateId + "'.");
|
||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Left Lua console.");
|
Application::Console().WriteRaw("Detached from Lua.");
|
||||||
}
|
}
|
||||||
mCachedLuaHistory = mCommandline.history();
|
mCachedLuaHistory = mCommandline.history();
|
||||||
mCommandline.set_history(mCachedRegularHistory);
|
mCommandline.set_history(mCachedRegularHistory);
|
||||||
@@ -144,21 +170,54 @@ void TConsole::ChangeToRegularConsole() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Lua(const std::string& cmd) {
|
bool TConsole::EnsureArgsCount(const std::vector<std::string>& args, size_t n) {
|
||||||
if (cmd.size() > 3) {
|
if (n == 0 && args.size() != 0) {
|
||||||
auto NewStateId = cmd.substr(4);
|
Application::Console().WriteRaw("This command expects no arguments.");
|
||||||
|
return false;
|
||||||
|
} else if (args.size() != n) {
|
||||||
|
Application::Console().WriteRaw("Expected " + std::to_string(n) + " argument(s), instead got " + std::to_string(args.size()));
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TConsole::EnsureArgsCount(const std::vector<std::string>& args, size_t min, size_t max) {
|
||||||
|
if (min == max) {
|
||||||
|
return EnsureArgsCount(args, min);
|
||||||
|
} else {
|
||||||
|
if (args.size() > max) {
|
||||||
|
Application::Console().WriteRaw("Too many arguments. At most " + std::to_string(max) + " arguments expected, got " + std::to_string(args.size()) + " instead.");
|
||||||
|
return false;
|
||||||
|
} else if (args.size() < min) {
|
||||||
|
Application::Console().WriteRaw("Too few arguments. At least " + std::to_string(max) + " arguments expected, got " + std::to_string(args.size()) + " instead.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TConsole::Command_Lua(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0, 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (args.size() == 1) {
|
||||||
|
auto NewStateId = args.at(0);
|
||||||
beammp_assert(!NewStateId.empty());
|
beammp_assert(!NewStateId.empty());
|
||||||
if (mLuaEngine->HasState(NewStateId)) {
|
if (mLuaEngine->HasState(NewStateId)) {
|
||||||
ChangeToLuaConsole(NewStateId);
|
ChangeToLuaConsole(NewStateId);
|
||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Lua state '" + NewStateId + "' is not a known state. Didn't switch to Lua.");
|
Application::Console().WriteRaw("Lua state '" + NewStateId + "' is not a known state. Didn't switch to Lua.");
|
||||||
}
|
}
|
||||||
} else {
|
} else if (args.size() == 0) {
|
||||||
ChangeToLuaConsole(mDefaultStateId);
|
ChangeToLuaConsole(mDefaultStateId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Help(const std::string&) {
|
void TConsole::Command_Help(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
static constexpr const char* sHelpString = R"(
|
static constexpr const char* sHelpString = R"(
|
||||||
Commands:
|
Commands:
|
||||||
help displays this help
|
help displays this help
|
||||||
@@ -167,18 +226,36 @@ void TConsole::Command_Help(const std::string&) {
|
|||||||
list lists all players and info about them
|
list lists all players and info about them
|
||||||
say <message> sends the message to all players in chat
|
say <message> sends the message to all players in chat
|
||||||
lua [state id] switches to lua, optionally into a specific state id's lua
|
lua [state id] switches to lua, optionally into a specific state id's lua
|
||||||
status how the server is doing and what it's up to)";
|
settings [command] sets or gets settings for the server, run `settings help` for more info
|
||||||
|
status how the server is doing and what it's up to
|
||||||
|
clear clears the console window)";
|
||||||
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Kick(const std::string& cmd) {
|
std::string TConsole::ConcatArgs(const std::vector<std::string>& args, char space) {
|
||||||
if (cmd.size() > 4) {
|
std::string Result;
|
||||||
auto Name = cmd.substr(5);
|
for (const auto& arg : args) {
|
||||||
|
Result += arg + space;
|
||||||
|
}
|
||||||
|
Result = Result.substr(0, Result.size() - 1); // strip trailing space
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TConsole::Command_Clear(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0, size_t(-1))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mCommandline.write("\x1b[;H\x1b[2J");
|
||||||
|
}
|
||||||
|
|
||||||
|
void TConsole::Command_Kick(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 1, size_t(-1))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto Name = args.at(0);
|
||||||
std::string Reason = "Kicked by server console";
|
std::string Reason = "Kicked by server console";
|
||||||
auto SpacePos = Name.find(' ');
|
if (args.size() > 1) {
|
||||||
if (SpacePos != Name.npos) {
|
Reason = ConcatArgs({ args.begin() + 1, args.end() });
|
||||||
Reason = Name.substr(SpacePos + 1);
|
|
||||||
Name = cmd.substr(5, cmd.size() - Reason.size() - 5 - 1);
|
|
||||||
}
|
}
|
||||||
beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'");
|
beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'");
|
||||||
bool Kicked = false;
|
bool Kicked = false;
|
||||||
@@ -203,17 +280,81 @@ void TConsole::Command_Kick(const std::string& cmd) {
|
|||||||
} else {
|
} else {
|
||||||
Application::Console().WriteRaw("Kicked player '" + Name + "' for reason: '" + Reason + "'.");
|
Application::Console().WriteRaw("Kicked player '" + Name + "' for reason: '" + Reason + "'.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<std::string, std::vector<std::string>> TConsole::ParseCommand(const std::string& CommandWithArgs) {
|
||||||
|
// Algorithm designed and implemented by Lion Kortlepel (c) 2022
|
||||||
|
// It correctly splits arguments, including respecting single and double quotes, as well as backticks
|
||||||
|
auto End_i = CommandWithArgs.find_first_of(' ');
|
||||||
|
std::string Command = CommandWithArgs.substr(0, End_i);
|
||||||
|
std::string ArgsStr {};
|
||||||
|
if (End_i != std::string::npos) {
|
||||||
|
ArgsStr = CommandWithArgs.substr(End_i);
|
||||||
|
}
|
||||||
|
std::vector<std::string> Args;
|
||||||
|
char* PrevPtr = ArgsStr.data();
|
||||||
|
char* Ptr = ArgsStr.data();
|
||||||
|
const char* End = ArgsStr.data() + ArgsStr.size();
|
||||||
|
while (Ptr != End) {
|
||||||
|
std::string Arg = "";
|
||||||
|
// advance while space
|
||||||
|
while (Ptr != End && std::isspace(*Ptr))
|
||||||
|
++Ptr;
|
||||||
|
PrevPtr = Ptr;
|
||||||
|
// advance while NOT space, also handle quotes
|
||||||
|
while (Ptr != End && !std::isspace(*Ptr)) {
|
||||||
|
// TODO: backslash escaping quotes
|
||||||
|
for (char Quote : { '"', '\'', '`' }) {
|
||||||
|
if (*Ptr == Quote) {
|
||||||
|
// seek if there's a closing quote
|
||||||
|
// if there is, go there and continue, otherwise ignore
|
||||||
|
char* Seeker = Ptr + 1;
|
||||||
|
while (Seeker != End && *Seeker != Quote)
|
||||||
|
++Seeker;
|
||||||
|
if (Seeker != End) {
|
||||||
|
// found closing quote
|
||||||
|
Ptr = Seeker;
|
||||||
|
}
|
||||||
|
break; // exit for loop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
++Ptr;
|
||||||
|
}
|
||||||
|
Arg = std::string(PrevPtr, Ptr - PrevPtr);
|
||||||
|
// remove quotes if enclosed in quotes
|
||||||
|
for (char Quote : { '"', '\'', '`' }) {
|
||||||
|
if (!Arg.empty() && Arg.at(0) == Quote && Arg.at(Arg.size() - 1) == Quote) {
|
||||||
|
Arg = Arg.substr(1, Arg.size() - 2);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!Arg.empty()) {
|
||||||
|
Args.push_back(Arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { Command, Args };
|
||||||
|
}
|
||||||
|
|
||||||
|
void TConsole::Command_Settings(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0)) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Say(const std::string& cmd) {
|
void TConsole::Command_Say(const std::string& FullCmd) {
|
||||||
if (cmd.size() > 3) {
|
if (FullCmd.size() > 3) {
|
||||||
auto Message = cmd.substr(4);
|
auto Message = FullCmd.substr(4);
|
||||||
LuaAPI::MP::SendChatMessage(-1, Message);
|
LuaAPI::MP::SendChatMessage(-1, Message);
|
||||||
|
if (!Application::Settings.LogChat) {
|
||||||
|
Application::Console().WriteRaw("Chat message sent!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_List(const std::string&) {
|
void TConsole::Command_List(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (mLuaEngine->Server().ClientCount() == 0) {
|
if (mLuaEngine->Server().ClientCount() == 0) {
|
||||||
Application::Console().WriteRaw("No players online.");
|
Application::Console().WriteRaw("No players online.");
|
||||||
} else {
|
} else {
|
||||||
@@ -233,7 +374,10 @@ void TConsole::Command_List(const std::string&) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Command_Status(const std::string&) {
|
void TConsole::Command_Status(const std::string&, const std::vector<std::string>& args) {
|
||||||
|
if (!EnsureArgsCount(args, 0)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
std::stringstream Status;
|
std::stringstream Status;
|
||||||
|
|
||||||
size_t CarCount = 0;
|
size_t CarCount = 0;
|
||||||
@@ -318,12 +462,12 @@ void TConsole::Command_Status(const std::string&) {
|
|||||||
<< "\t\tEvent handlers: " << mLuaEngine->GetRegisteredEventHandlerCount() << "\n"
|
<< "\t\tEvent handlers: " << mLuaEngine->GetRegisteredEventHandlerCount() << "\n"
|
||||||
<< "\tSubsystems:\n"
|
<< "\tSubsystems:\n"
|
||||||
<< "\t\tGood/Starting/Bad: " << SystemsGood << "/" << SystemsStarting << "/" << SystemsBad << "\n"
|
<< "\t\tGood/Starting/Bad: " << SystemsGood << "/" << SystemsStarting << "/" << SystemsBad << "\n"
|
||||||
<< "\t\tShutting down/Shutdown: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
|
<< "\t\tShutting down/Shut down: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
|
||||||
<< "\t\tGood: [ " << SystemsGoodList << " ]\n"
|
<< "\t\tGood: [ " << SystemsGoodList << " ]\n"
|
||||||
<< "\t\tStarting: [ " << SystemsStartingList << " ]\n"
|
<< "\t\tStarting: [ " << SystemsStartingList << " ]\n"
|
||||||
<< "\t\tBad: [ " << SystemsBadList << " ]\n"
|
<< "\t\tBad: [ " << SystemsBadList << " ]\n"
|
||||||
<< "\t\tShutting down: [ " << SystemsShuttingDownList << " ]\n"
|
<< "\t\tShutting down: [ " << SystemsShuttingDownList << " ]\n"
|
||||||
<< "\t\tShutdown: [ " << SystemsShutdownList << " ]\n"
|
<< "\t\tShut down: [ " << SystemsShutdownList << " ]\n"
|
||||||
<< "";
|
<< "";
|
||||||
|
|
||||||
Application::Console().WriteRaw(Status.str());
|
Application::Console().WriteRaw(Status.str());
|
||||||
@@ -375,6 +519,58 @@ void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TConsole::HandleLuaInternalCommand(const std::string& cmd) {
|
||||||
|
if (cmd == "exit") {
|
||||||
|
ChangeToRegularConsole();
|
||||||
|
} else if (cmd == "queued") {
|
||||||
|
auto QueuedFunctions = LuaAPI::MP::Engine->Debug_GetStateFunctionQueueForState(mStateId);
|
||||||
|
Application::Console().WriteRaw("Pending functions in State '" + mStateId + "'");
|
||||||
|
std::unordered_map<std::string, size_t> FunctionsCount;
|
||||||
|
std::vector<std::string> FunctionsInOrder;
|
||||||
|
while (!QueuedFunctions.empty()) {
|
||||||
|
auto Tuple = QueuedFunctions.front();
|
||||||
|
QueuedFunctions.erase(QueuedFunctions.begin());
|
||||||
|
FunctionsInOrder.push_back(Tuple.FunctionName);
|
||||||
|
FunctionsCount[Tuple.FunctionName] += 1;
|
||||||
|
}
|
||||||
|
std::set<std::string> Uniques;
|
||||||
|
for (const auto& Function : FunctionsInOrder) {
|
||||||
|
if (Uniques.count(Function) == 0) {
|
||||||
|
Uniques.insert(Function);
|
||||||
|
if (FunctionsCount.at(Function) > 1) {
|
||||||
|
Application::Console().WriteRaw(" " + Function + " (" + std::to_string(FunctionsCount.at(Function)) + "x)");
|
||||||
|
} else {
|
||||||
|
Application::Console().WriteRaw(" " + Function);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Application::Console().WriteRaw("Executed functions waiting to be checked in State '" + mStateId + "'");
|
||||||
|
for (const auto& Function : LuaAPI::MP::Engine->Debug_GetResultsToCheckForState(mStateId)) {
|
||||||
|
Application::Console().WriteRaw(" '" + Function.Function + "' (Ready? " + (Function.Ready ? "Yes" : "No") + ", Error? " + (Function.Error ? "Yes: '" + Function.ErrorMessage + "'" : "No") + ")");
|
||||||
|
}
|
||||||
|
} else if (cmd == "events") {
|
||||||
|
auto Events = LuaAPI::MP::Engine->Debug_GetEventsForState(mStateId);
|
||||||
|
Application::Console().WriteRaw("Registered Events + Handlers for State '" + mStateId + "'");
|
||||||
|
for (const auto& EventHandlerPair : Events) {
|
||||||
|
Application::Console().WriteRaw(" Event '" + EventHandlerPair.first + "'");
|
||||||
|
for (const auto& Handler : EventHandlerPair.second) {
|
||||||
|
Application::Console().WriteRaw(" " + Handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (cmd == "help") {
|
||||||
|
Application::Console().WriteRaw(R"(BeamMP Lua Debugger
|
||||||
|
All commands must be prefixed with a `:`. Non-prefixed commands are interpreted as Lua.
|
||||||
|
|
||||||
|
Commands
|
||||||
|
:exit detaches (exits) from this Lua console
|
||||||
|
:help displays this help
|
||||||
|
:events shows a list of currently registered events
|
||||||
|
:queued shows a list of all pending and queued functions)");
|
||||||
|
} else {
|
||||||
|
beammp_error("internal command '" + cmd + "' is not known");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TConsole::TConsole() {
|
TConsole::TConsole() {
|
||||||
mCommandline.enable_history();
|
mCommandline.enable_history();
|
||||||
mCommandline.set_history_limit(20);
|
mCommandline.set_history_limit(20);
|
||||||
@@ -382,21 +578,22 @@ TConsole::TConsole() {
|
|||||||
BackupOldLog();
|
BackupOldLog();
|
||||||
mCommandline.on_command = [this](Commandline& c) {
|
mCommandline.on_command = [this](Commandline& c) {
|
||||||
try {
|
try {
|
||||||
auto cmd = c.get_command();
|
auto TrimmedCmd = c.get_command();
|
||||||
cmd = TrimString(cmd);
|
TrimmedCmd = TrimString(TrimmedCmd);
|
||||||
mCommandline.write(mCommandline.prompt() + cmd);
|
auto [cmd, args] = ParseCommand(TrimmedCmd);
|
||||||
|
mCommandline.write(mCommandline.prompt() + TrimmedCmd);
|
||||||
if (mIsLuaConsole) {
|
if (mIsLuaConsole) {
|
||||||
if (!mLuaEngine) {
|
if (!mLuaEngine) {
|
||||||
beammp_info("Lua not started yet, please try again in a second");
|
beammp_info("Lua not started yet, please try again in a second");
|
||||||
} else if (cmd == "exit()") {
|
} else if (!cmd.empty() && cmd.at(0) == ':') {
|
||||||
ChangeToRegularConsole();
|
HandleLuaInternalCommand(cmd.substr(1));
|
||||||
} else {
|
} else {
|
||||||
auto Future = mLuaEngine->EnqueueScript(mStateId, { std::make_shared<std::string>(cmd), "", "" });
|
auto Future = mLuaEngine->EnqueueScript(mStateId, { std::make_shared<std::string>(TrimmedCmd), "", "" });
|
||||||
while (!Future->Ready) {
|
while (!Future->Ready) {
|
||||||
std::this_thread::yield(); // TODO: Add a timeout
|
std::this_thread::yield(); // TODO: Add a timeout
|
||||||
}
|
}
|
||||||
if (Future->Error) {
|
if (Future->Error) {
|
||||||
beammp_lua_error(Future->ErrorMessage);
|
beammp_lua_error("error in " + mStateId + ": " + Future->ErrorMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -405,31 +602,63 @@ TConsole::TConsole() {
|
|||||||
} else if (cmd == "exit") {
|
} else if (cmd == "exit") {
|
||||||
beammp_info("gracefully shutting down");
|
beammp_info("gracefully shutting down");
|
||||||
Application::GracefullyShutdown();
|
Application::GracefullyShutdown();
|
||||||
} else if (StringStartsWith(cmd, "lua")) {
|
} else if (cmd == "say") {
|
||||||
Command_Lua(cmd);
|
RunAsCommand(TrimmedCmd, true);
|
||||||
} else if (StringStartsWith(cmd, "help")) {
|
Command_Say(TrimmedCmd);
|
||||||
RunAsCommand(cmd, true);
|
} else {
|
||||||
Command_Help(cmd);
|
if (mCommandMap.find(cmd) != mCommandMap.end()) {
|
||||||
} else if (StringStartsWith(cmd, "kick")) {
|
mCommandMap.at(cmd)(cmd, args);
|
||||||
RunAsCommand(cmd, true);
|
RunAsCommand(TrimmedCmd, true);
|
||||||
Command_Kick(cmd);
|
} else {
|
||||||
} else if (StringStartsWith(cmd, "say")) {
|
RunAsCommand(TrimmedCmd);
|
||||||
RunAsCommand(cmd, true);
|
}
|
||||||
Command_Say(cmd);
|
|
||||||
} else if (StringStartsWith(cmd, "list")) {
|
|
||||||
RunAsCommand(cmd, true);
|
|
||||||
Command_List(cmd);
|
|
||||||
} else if (StringStartsWith(cmd, "status")) {
|
|
||||||
RunAsCommand(cmd, true);
|
|
||||||
Command_Status(cmd);
|
|
||||||
} else if (!cmd.empty()) {
|
|
||||||
RunAsCommand(cmd);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
|
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
mCommandline.on_autocomplete = [this](Commandline&, std::string stub, int) {
|
||||||
|
std::vector<std::string> suggestions;
|
||||||
|
try {
|
||||||
|
auto cmd = TrimString(stub);
|
||||||
|
// beammp_error("yes 1");
|
||||||
|
// beammp_error(stub);
|
||||||
|
if (mIsLuaConsole) { // if lua
|
||||||
|
if (!mLuaEngine) {
|
||||||
|
beammp_info("Lua not started yet, please try again in a second");
|
||||||
|
} else {
|
||||||
|
std::string prefix {};
|
||||||
|
for (size_t i = stub.length(); i > 0; i--) {
|
||||||
|
if (!std::isalnum(stub[i - 1]) && stub[i - 1] != '_') {
|
||||||
|
prefix = stub.substr(0, i);
|
||||||
|
stub = stub.substr(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auto keys = mLuaEngine->GetStateGlobalKeysForState(mStateId);
|
||||||
|
for (const auto& key : keys) {
|
||||||
|
std::string::size_type n = key.find(stub);
|
||||||
|
if (n == 0) {
|
||||||
|
suggestions.push_back(prefix + key);
|
||||||
|
// beammp_warn(cmd_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else { // if not lua
|
||||||
|
for (const auto& [cmd_name, cmd_fn] : mCommandMap) {
|
||||||
|
std::string::size_type n = cmd_name.find(stub);
|
||||||
|
if (n == 0) {
|
||||||
|
suggestions.push_back(cmd_name);
|
||||||
|
// beammp_warn(cmd_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
beammp_error("Console died with: " + std::string(e.what()) + ". This could be a fatal error and could cause the server to terminate.");
|
||||||
|
}
|
||||||
|
return suggestions;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void TConsole::Write(const std::string& str) {
|
void TConsole::Write(const std::string& str) {
|
||||||
|
|||||||
+11
-10
@@ -20,7 +20,7 @@ void THeartbeatThread::operator()() {
|
|||||||
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
|
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
|
||||||
bool isAuth = false;
|
bool isAuth = false;
|
||||||
size_t UpdateReminderCounter = 0;
|
size_t UpdateReminderCounter = 0;
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
++UpdateReminderCounter;
|
++UpdateReminderCounter;
|
||||||
Body = GenerateCall();
|
Body = GenerateCall();
|
||||||
// a hot-change occurs when a setting has changed, to update the backend of that change.
|
// a hot-change occurs when a setting has changed, to update the backend of that change.
|
||||||
@@ -64,8 +64,10 @@ void THeartbeatThread::operator()() {
|
|||||||
beammp_trace(T);
|
beammp_trace(T);
|
||||||
Doc.Parse(T.data(), T.size());
|
Doc.Parse(T.data(), T.size());
|
||||||
if (Doc.HasParseError() || !Doc.IsObject()) {
|
if (Doc.HasParseError() || !Doc.IsObject()) {
|
||||||
beammp_debug("Failed to contact backend at " + Url + " (this is not an error).");
|
if (!Application::Settings.Private) {
|
||||||
beammp_trace("Response was: " + T);
|
beammp_error("Backend response failed to parse as valid json");
|
||||||
|
beammp_trace("Response was: `" + T + "`");
|
||||||
|
}
|
||||||
Sentry.SetContext("JSON Response", { { "reponse", T } });
|
Sentry.SetContext("JSON Response", { { "reponse", T } });
|
||||||
SentryReportError(Url + Target, ResponseCode);
|
SentryReportError(Url + Target, ResponseCode);
|
||||||
} else if (ResponseCode != 200) {
|
} else if (ResponseCode != 200) {
|
||||||
@@ -113,21 +115,21 @@ void THeartbeatThread::operator()() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Ok && !isAuth) {
|
if (Ok && !isAuth && !Application::Settings.Private) {
|
||||||
if (Status == "2000") {
|
if (Status == "2000") {
|
||||||
beammp_info(("Authenticated!"));
|
beammp_info(("Authenticated! " + Message));
|
||||||
isAuth = true;
|
isAuth = true;
|
||||||
} else if (Status == "200") {
|
} else if (Status == "200") {
|
||||||
beammp_info(("Resumed authenticated session!"));
|
beammp_info(("Resumed authenticated session! " + Message));
|
||||||
isAuth = true;
|
isAuth = true;
|
||||||
} else {
|
} else {
|
||||||
if (Message.empty()) {
|
if (Message.empty()) {
|
||||||
Message = "Backend didn't provide a reason";
|
Message = "Backend didn't provide a reason.";
|
||||||
}
|
}
|
||||||
beammp_error("Backend REFUSED the auth key. " + Message);
|
beammp_error("Backend REFUSED the auth key. Reason: " + Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (isAuth) {
|
if (isAuth || Application::Settings.Private) {
|
||||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
|
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
|
||||||
}
|
}
|
||||||
if (!Application::Settings.HideUpdateMessages && UpdateReminderCounter % 5) {
|
if (!Application::Settings.HideUpdateMessages && UpdateReminderCounter % 5) {
|
||||||
@@ -162,7 +164,6 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
|
|||||||
Application::RegisterShutdownHandler([&] {
|
Application::RegisterShutdownHandler([&] {
|
||||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::ShuttingDown);
|
Application::SetSubsystemStatus("Heartbeat", Application::Status::ShuttingDown);
|
||||||
if (mThread.joinable()) {
|
if (mThread.joinable()) {
|
||||||
mShutdown = true;
|
|
||||||
mThread.join();
|
mThread.join();
|
||||||
}
|
}
|
||||||
Application::SetSubsystemStatus("Heartbeat", Application::Status::Shutdown);
|
Application::SetSubsystemStatus("Heartbeat", Application::Status::Shutdown);
|
||||||
|
|||||||
+240
-83
@@ -8,6 +8,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
@@ -15,26 +16,29 @@
|
|||||||
TLuaEngine* LuaAPI::MP::Engine;
|
TLuaEngine* LuaAPI::MP::Engine;
|
||||||
|
|
||||||
TLuaEngine::TLuaEngine()
|
TLuaEngine::TLuaEngine()
|
||||||
: mPluginMonitor(fs::path(Application::Settings.Resource) / "Server", *this, mShutdown) {
|
: mResourceServerPath(fs::path(Application::Settings.Resource) / "Server") {
|
||||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::Starting);
|
Application::SetSubsystemStatus("LuaEngine", Application::Status::Starting);
|
||||||
LuaAPI::MP::Engine = this;
|
LuaAPI::MP::Engine = this;
|
||||||
if (!fs::exists(Application::Settings.Resource)) {
|
if (!fs::exists(Application::Settings.Resource)) {
|
||||||
fs::create_directory(Application::Settings.Resource);
|
fs::create_directory(Application::Settings.Resource);
|
||||||
}
|
}
|
||||||
fs::path Path = fs::path(Application::Settings.Resource) / "Server";
|
if (!fs::exists(mResourceServerPath)) {
|
||||||
if (!fs::exists(Path)) {
|
fs::create_directory(mResourceServerPath);
|
||||||
fs::create_directory(Path);
|
|
||||||
}
|
}
|
||||||
mResourceServerPath = Path;
|
|
||||||
Application::RegisterShutdownHandler([&] {
|
Application::RegisterShutdownHandler([&] {
|
||||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::ShuttingDown);
|
Application::SetSubsystemStatus("LuaEngine", Application::Status::ShuttingDown);
|
||||||
mShutdown = true;
|
|
||||||
if (mThread.joinable()) {
|
if (mThread.joinable()) {
|
||||||
mThread.join();
|
mThread.join();
|
||||||
}
|
}
|
||||||
Application::SetSubsystemStatus("LuaEngine", Application::Status::Shutdown);
|
Application::SetSubsystemStatus("LuaEngine", Application::Status::Shutdown);
|
||||||
});
|
});
|
||||||
Start();
|
IThreaded::Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("TLuaEngine ctor & dtor") {
|
||||||
|
Application::Settings.Resource = "beammp_server_test_resources";
|
||||||
|
TLuaEngine engine;
|
||||||
|
Application::GracefullyShutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::operator()() {
|
void TLuaEngine::operator()() {
|
||||||
@@ -53,30 +57,28 @@ void TLuaEngine::operator()() {
|
|||||||
|
|
||||||
auto ResultCheckThread = std::thread([&] {
|
auto ResultCheckThread = std::thread([&] {
|
||||||
RegisterThread("ResultCheckThread");
|
RegisterThread("ResultCheckThread");
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
std::unique_lock Lock(mResultsToCheckMutex);
|
std::unique_lock Lock(mResultsToCheckMutex);
|
||||||
mResultsToCheckCond.wait_for(Lock, std::chrono::milliseconds(20));
|
|
||||||
if (!mResultsToCheck.empty()) {
|
if (!mResultsToCheck.empty()) {
|
||||||
mResultsToCheck.remove_if([](const std::shared_ptr<TLuaResult>& Ptr) -> bool {
|
mResultsToCheck.remove_if([](const std::shared_ptr<TLuaResult>& Ptr) -> bool {
|
||||||
if (Ptr->Ready) {
|
if (Ptr->Ready) {
|
||||||
return true;
|
if (Ptr->Error) {
|
||||||
} else if (Ptr->Error) {
|
|
||||||
if (Ptr->ErrorMessage != BeamMPFnNotFoundError) {
|
if (Ptr->ErrorMessage != BeamMPFnNotFoundError) {
|
||||||
beammp_lua_error(Ptr->Function + ": " + Ptr->ErrorMessage);
|
beammp_lua_error(Ptr->Function + ": " + Ptr->ErrorMessage);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
mResultsToCheckCond.wait_for(Lock, std::chrono::milliseconds(20));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// event loop
|
// event loop
|
||||||
auto Before = std::chrono::high_resolution_clock::now();
|
auto Before = std::chrono::high_resolution_clock::now();
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
if (mLuaStates.size() == 0) {
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(100));
|
|
||||||
}
|
|
||||||
{ // Timed Events Scope
|
{ // Timed Events Scope
|
||||||
std::unique_lock Lock(mTimedEventsMutex);
|
std::unique_lock Lock(mTimedEventsMutex);
|
||||||
for (auto& Timer : mTimedEvents) {
|
for (auto& Timer : mTimedEvents) {
|
||||||
@@ -102,12 +104,18 @@ void TLuaEngine::operator()() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const auto Expected = std::chrono::milliseconds(10);
|
if (mLuaStates.size() == 0) {
|
||||||
if (auto Diff = std::chrono::high_resolution_clock::now() - Before;
|
beammp_trace("No Lua states, event loop running extremely sparsely");
|
||||||
Diff < Expected) {
|
Application::SleepSafeSeconds(10);
|
||||||
std::this_thread::sleep_for(Expected - Diff);
|
|
||||||
} else {
|
} else {
|
||||||
beammp_trace("Event loop cannot keep up! Running " + std::to_string(Diff.count()) + "s behind");
|
constexpr double NsFactor = 1000000.0;
|
||||||
|
constexpr double Expected = 10.0; // ms
|
||||||
|
const auto Diff = (std::chrono::high_resolution_clock::now() - Before).count() / NsFactor;
|
||||||
|
if (Diff < Expected) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::nanoseconds(size_t((Expected - Diff) * NsFactor)));
|
||||||
|
} else {
|
||||||
|
beammp_tracef("Event loop cannot keep up! Running {}ms behind", Diff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Before = std::chrono::high_resolution_clock::now();
|
Before = std::chrono::high_resolution_clock::now();
|
||||||
}
|
}
|
||||||
@@ -154,10 +162,70 @@ void TLuaEngine::AddResultToCheck(const std::shared_ptr<TLuaResult>& Result) {
|
|||||||
mResultsToCheckCond.notify_one();
|
mResultsToCheckCond.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::unordered_map<std::string /* event name */, std::vector<std::string> /* handlers */> TLuaEngine::Debug_GetEventsForState(TLuaStateId StateId) {
|
||||||
|
std::unordered_map<std::string, std::vector<std::string>> Result;
|
||||||
|
std::unique_lock Lock(mLuaEventsMutex);
|
||||||
|
for (const auto& EventNameToEventMap : mLuaEvents) {
|
||||||
|
for (const auto& IdSetOfHandlersPair : EventNameToEventMap.second) {
|
||||||
|
if (IdSetOfHandlersPair.first == StateId) {
|
||||||
|
for (const auto& Handler : IdSetOfHandlersPair.second) {
|
||||||
|
Result[EventNameToEventMap.first].push_back(Handler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> TLuaEngine::Debug_GetStateExecuteQueueForState(TLuaStateId StateId) {
|
||||||
|
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> Result;
|
||||||
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
|
Result = mLuaStates.at(StateId)->Debug_GetStateExecuteQueue();
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<TLuaEngine::QueuedFunction> TLuaEngine::Debug_GetStateFunctionQueueForState(TLuaStateId StateId) {
|
||||||
|
std::vector<TLuaEngine::QueuedFunction> Result;
|
||||||
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
|
Result = mLuaStates.at(StateId)->Debug_GetStateFunctionQueue();
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<TLuaResult> TLuaEngine::Debug_GetResultsToCheckForState(TLuaStateId StateId) {
|
||||||
|
std::unique_lock Lock(mResultsToCheckMutex);
|
||||||
|
auto ResultsToCheckCopy = mResultsToCheck;
|
||||||
|
Lock.unlock();
|
||||||
|
std::vector<TLuaResult> Result;
|
||||||
|
while (!ResultsToCheckCopy.empty()) {
|
||||||
|
auto ResultToCheck = std::move(ResultsToCheckCopy.front());
|
||||||
|
ResultsToCheckCopy.pop_front();
|
||||||
|
if (ResultToCheck->StateId == StateId) {
|
||||||
|
Result.push_back(*ResultToCheck);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> TLuaEngine::GetStateGlobalKeysForState(TLuaStateId StateId) {
|
||||||
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
|
auto Result = mLuaStates.at(StateId)->GetStateGlobalKeys();
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> TLuaEngine::StateThreadData::GetStateGlobalKeys() {
|
||||||
|
auto globals = mStateView.globals();
|
||||||
|
std::vector<std::string> Result;
|
||||||
|
for (const auto& [key, value] : globals) {
|
||||||
|
Result.push_back(key.as<std::string>());
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& Max) {
|
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& Max) {
|
||||||
for (const auto& Result : Results) {
|
for (const auto& Result : Results) {
|
||||||
bool Cancelled = false;
|
bool Cancelled = false;
|
||||||
size_t ms = 0;
|
size_t ms = 0;
|
||||||
|
std::set<std::string> WarnedResults;
|
||||||
while (!Result->Ready && !Cancelled) {
|
while (!Result->Ready && !Cancelled) {
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
ms += 10;
|
ms += 10;
|
||||||
@@ -165,9 +233,13 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, c
|
|||||||
beammp_trace("'" + Result->Function + "' in '" + Result->StateId + "' did not finish executing in time (took: " + std::to_string(ms) + "ms).");
|
beammp_trace("'" + Result->Function + "' in '" + Result->StateId + "' did not finish executing in time (took: " + std::to_string(ms) + "ms).");
|
||||||
Cancelled = true;
|
Cancelled = true;
|
||||||
} else if (ms > 1000 * 60) {
|
} else if (ms > 1000 * 60) {
|
||||||
|
auto ResultId = Result->StateId + "_" + Result->Function;
|
||||||
|
if (WarnedResults.count(ResultId) == 0) {
|
||||||
|
WarnedResults.insert(ResultId);
|
||||||
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' is taking very long. The event it's handling is too important to discard the result of this handler, but may block this event and possibly the whole lua state.");
|
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' is taking very long. The event it's handling is too important to discard the result of this handler, but may block this event and possibly the whole lua state.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (Cancelled) {
|
if (Cancelled) {
|
||||||
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time.");
|
beammp_lua_warn("'" + Result->Function + "' in '" + Result->StateId + "' failed to execute in time and was not waited for. It may still finish executing at a later time.");
|
||||||
LuaAPI::MP::Engine->ReportErrors({ Result });
|
LuaAPI::MP::Engine->ReportErrors({ Result });
|
||||||
@@ -204,6 +276,9 @@ std::shared_ptr<TLuaResult> TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::CollectAndInitPlugins() {
|
void TLuaEngine::CollectAndInitPlugins() {
|
||||||
|
if (!fs::exists(mResourceServerPath)) {
|
||||||
|
fs::create_directories(mResourceServerPath);
|
||||||
|
}
|
||||||
for (const auto& Dir : fs::directory_iterator(mResourceServerPath)) {
|
for (const auto& Dir : fs::directory_iterator(mResourceServerPath)) {
|
||||||
auto Path = Dir.path();
|
auto Path = Dir.path();
|
||||||
Path = fs::relative(Path);
|
Path = fs::relative(Path);
|
||||||
@@ -253,7 +328,7 @@ void TLuaEngine::EnsureStateExists(TLuaStateId StateId, const std::string& Name,
|
|||||||
std::unique_lock Lock(mLuaStatesMutex);
|
std::unique_lock Lock(mLuaStatesMutex);
|
||||||
if (mLuaStates.find(StateId) == mLuaStates.end()) {
|
if (mLuaStates.find(StateId) == mLuaStates.end()) {
|
||||||
beammp_debug("Creating lua state for state id \"" + StateId + "\"");
|
beammp_debug("Creating lua state for state id \"" + StateId + "\"");
|
||||||
auto DataPtr = std::make_unique<StateThreadData>(Name, mShutdown, StateId, *this);
|
auto DataPtr = std::make_unique<StateThreadData>(Name, StateId, *this);
|
||||||
mLuaStates[StateId] = std::move(DataPtr);
|
mLuaStates[StateId] = std::move(DataPtr);
|
||||||
RegisterEvent("onInit", StateId, "onInit");
|
RegisterEvent("onInit", StateId, "onInit");
|
||||||
if (!DontCallOnInit) {
|
if (!DontCallOnInit) {
|
||||||
@@ -277,7 +352,6 @@ std::set<std::string> TLuaEngine::GetEventHandlersForState(const std::string& Ev
|
|||||||
|
|
||||||
sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
|
sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
|
||||||
auto Return = mEngine->TriggerEvent(EventName, mStateId, EventArgs);
|
auto Return = mEngine->TriggerEvent(EventName, mStateId, EventArgs);
|
||||||
// TODO Synchronous call to the event handlers
|
|
||||||
auto MyHandlers = mEngine->GetEventHandlersForState(EventName, mStateId);
|
auto MyHandlers = mEngine->GetEventHandlersForState(EventName, mStateId);
|
||||||
for (const auto& Handler : MyHandlers) {
|
for (const auto& Handler : MyHandlers) {
|
||||||
auto Fn = mStateView[Handler];
|
auto Fn = mStateView[Handler];
|
||||||
@@ -335,7 +409,7 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string&
|
|||||||
Result.add(FnRet);
|
Result.add(FnRet);
|
||||||
} else {
|
} else {
|
||||||
sol::error Err = FnRet;
|
sol::error Err = FnRet;
|
||||||
beammp_lua_error(Err.what());
|
beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -386,6 +460,32 @@ int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name)
|
|||||||
return Id;
|
return Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_FS_ListFiles(const std::string& Path) {
|
||||||
|
if (!std::filesystem::exists(Path)) {
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
auto table = mStateView.create_table();
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(Path)) {
|
||||||
|
if (entry.is_regular_file() || entry.is_symlink()) {
|
||||||
|
table[table.size() + 1] = entry.path().lexically_relative(Path).string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_FS_ListDirectories(const std::string& Path) {
|
||||||
|
if (!std::filesystem::exists(Path)) {
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
auto table = mStateView.create_table();
|
||||||
|
for (const auto& entry : std::filesystem::directory_iterator(Path)) {
|
||||||
|
if (entry.is_directory()) {
|
||||||
|
table[table.size() + 1] = entry.path().lexically_relative(Path).string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
|
||||||
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
auto MaybeClient = GetClient(mEngine->Server(), ID);
|
||||||
if (MaybeClient && !MaybeClient.value().expired()) {
|
if (MaybeClient && !MaybeClient.value().expired()) {
|
||||||
@@ -439,9 +539,85 @@ sol::table TLuaEngine::StateThreadData::Lua_HttpCreateConnection(const std::stri
|
|||||||
return table;
|
return table;
|
||||||
}
|
}
|
||||||
|
|
||||||
TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomic_bool& Shutdown, TLuaStateId StateId, TLuaEngine& Engine)
|
template <typename T>
|
||||||
|
static void AddToTable(sol::table& table, const std::string& left, const T& value) {
|
||||||
|
if (left.empty()) {
|
||||||
|
table[table.size() + 1] = value;
|
||||||
|
} else {
|
||||||
|
table[left] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void JsonDecodeRecursive(sol::state_view& StateView, sol::table& table, const std::string& left, const nlohmann::json& right) {
|
||||||
|
switch (right.type()) {
|
||||||
|
case nlohmann::detail::value_t::null:
|
||||||
|
return;
|
||||||
|
case nlohmann::detail::value_t::object: {
|
||||||
|
auto value = table.create();
|
||||||
|
value.clear();
|
||||||
|
for (const auto& entry : right.items()) {
|
||||||
|
JsonDecodeRecursive(StateView, value, entry.key(), entry.value());
|
||||||
|
}
|
||||||
|
AddToTable(table, left, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case nlohmann::detail::value_t::array: {
|
||||||
|
auto value = table.create();
|
||||||
|
value.clear();
|
||||||
|
for (const auto& entry : right.items()) {
|
||||||
|
JsonDecodeRecursive(StateView, value, "", entry.value());
|
||||||
|
}
|
||||||
|
AddToTable(table, left, value);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case nlohmann::detail::value_t::string:
|
||||||
|
AddToTable(table, left, right.get<std::string>());
|
||||||
|
break;
|
||||||
|
case nlohmann::detail::value_t::boolean:
|
||||||
|
AddToTable(table, left, right.get<bool>());
|
||||||
|
break;
|
||||||
|
case nlohmann::detail::value_t::number_integer:
|
||||||
|
AddToTable(table, left, right.get<int64_t>());
|
||||||
|
break;
|
||||||
|
case nlohmann::detail::value_t::number_unsigned:
|
||||||
|
AddToTable(table, left, right.get<uint64_t>());
|
||||||
|
break;
|
||||||
|
case nlohmann::detail::value_t::number_float:
|
||||||
|
AddToTable(table, left, right.get<double>());
|
||||||
|
break;
|
||||||
|
case nlohmann::detail::value_t::binary:
|
||||||
|
beammp_lua_error("JsonDecode can't handle binary blob in json, ignoring");
|
||||||
|
return;
|
||||||
|
case nlohmann::detail::value_t::discarded:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sol::table TLuaEngine::StateThreadData::Lua_JsonDecode(const std::string& str) {
|
||||||
|
sol::state_view StateView(mState);
|
||||||
|
auto table = StateView.create_table();
|
||||||
|
if (!nlohmann::json::accept(str)) {
|
||||||
|
beammp_lua_error("string given to JsonDecode is not valid json: `" + str + "`");
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
nlohmann::json json = nlohmann::json::parse(str);
|
||||||
|
if (json.is_object()) {
|
||||||
|
for (const auto& entry : json.items()) {
|
||||||
|
JsonDecodeRecursive(StateView, table, entry.key(), entry.value());
|
||||||
|
}
|
||||||
|
} else if (json.is_array()) {
|
||||||
|
for (const auto& entry : json) {
|
||||||
|
JsonDecodeRecursive(StateView, table, "", entry);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
beammp_lua_error("JsonDecode expected array or object json, instead got " + std::string(json.type_name()));
|
||||||
|
return sol::lua_nil;
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateId StateId, TLuaEngine& Engine)
|
||||||
: mName(Name)
|
: mName(Name)
|
||||||
, mShutdown(Shutdown)
|
|
||||||
, mStateId(StateId)
|
, mStateId(StateId)
|
||||||
, mState(luaL_newstate())
|
, mState(luaL_newstate())
|
||||||
, mEngine(&Engine) {
|
, mEngine(&Engine) {
|
||||||
@@ -484,6 +660,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
return Lua_TriggerLocalEvent(EventName, EventArgs);
|
return Lua_TriggerLocalEvent(EventName, EventArgs);
|
||||||
});
|
});
|
||||||
MPTable.set_function("TriggerClientEvent", &LuaAPI::MP::TriggerClientEvent);
|
MPTable.set_function("TriggerClientEvent", &LuaAPI::MP::TriggerClientEvent);
|
||||||
|
MPTable.set_function("TriggerClientEventJson", &LuaAPI::MP::TriggerClientEventJson);
|
||||||
MPTable.set_function("GetPlayerCount", &LuaAPI::MP::GetPlayerCount);
|
MPTable.set_function("GetPlayerCount", &LuaAPI::MP::GetPlayerCount);
|
||||||
MPTable.set_function("IsPlayerConnected", &LuaAPI::MP::IsPlayerConnected);
|
MPTable.set_function("IsPlayerConnected", &LuaAPI::MP::IsPlayerConnected);
|
||||||
MPTable.set_function("GetPlayerIDByName", [&](const std::string& Name) -> int {
|
MPTable.set_function("GetPlayerIDByName", [&](const std::string& Name) -> int {
|
||||||
@@ -538,6 +715,27 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
mEngine->CancelEventTimers(EventName, mStateId);
|
mEngine->CancelEventTimers(EventName, mStateId);
|
||||||
});
|
});
|
||||||
MPTable.set_function("Set", &LuaAPI::MP::Set);
|
MPTable.set_function("Set", &LuaAPI::MP::Set);
|
||||||
|
|
||||||
|
auto UtilTable = StateView.create_named_table("Util");
|
||||||
|
UtilTable.set_function("JsonEncode", &LuaAPI::MP::JsonEncode);
|
||||||
|
UtilTable.set_function("JsonDecode", [this](const std::string& str) {
|
||||||
|
return Lua_JsonDecode(str);
|
||||||
|
});
|
||||||
|
UtilTable.set_function("JsonDiff", &LuaAPI::MP::JsonDiff);
|
||||||
|
UtilTable.set_function("JsonFlatten", &LuaAPI::MP::JsonFlatten);
|
||||||
|
UtilTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten);
|
||||||
|
UtilTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify);
|
||||||
|
UtilTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify);
|
||||||
|
UtilTable.set_function("Random", [this] {
|
||||||
|
return mUniformRealDistribution01(mMersenneTwister);
|
||||||
|
});
|
||||||
|
UtilTable.set_function("RandomRange", [this](double min, double max) -> double {
|
||||||
|
return std::uniform_real_distribution(min, max)(mMersenneTwister);
|
||||||
|
});
|
||||||
|
UtilTable.set_function("RandomIntRange", [this](int64_t min, int64_t max) -> int64_t {
|
||||||
|
return std::uniform_int_distribution(min, max)(mMersenneTwister);
|
||||||
|
});
|
||||||
|
|
||||||
auto HttpTable = StateView.create_named_table("Http");
|
auto HttpTable = StateView.create_named_table("Http");
|
||||||
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
HttpTable.set_function("CreateConnection", [this](const std::string& host, uint16_t port) {
|
||||||
return Lua_HttpCreateConnection(host, port);
|
return Lua_HttpCreateConnection(host, port);
|
||||||
@@ -568,6 +766,12 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
|
|||||||
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
|
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
|
||||||
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
|
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
|
||||||
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
|
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
|
||||||
|
FSTable.set_function("ListFiles", [this](const std::string& Path) {
|
||||||
|
return Lua_FS_ListFiles(Path);
|
||||||
|
});
|
||||||
|
FSTable.set_function("ListDirectories", [this](const std::string& Path) {
|
||||||
|
return Lua_FS_ListDirectories(Path);
|
||||||
|
});
|
||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -616,7 +820,7 @@ void TLuaEngine::StateThreadData::RegisterEvent(const std::string& EventName, co
|
|||||||
|
|
||||||
void TLuaEngine::StateThreadData::operator()() {
|
void TLuaEngine::StateThreadData::operator()() {
|
||||||
RegisterThread("Lua:" + mStateId);
|
RegisterThread("Lua:" + mStateId);
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
{ // StateExecuteQueue Scope
|
{ // StateExecuteQueue Scope
|
||||||
std::unique_lock Lock(mStateExecuteQueueMutex);
|
std::unique_lock Lock(mStateExecuteQueueMutex);
|
||||||
if (!mStateExecuteQueue.empty()) {
|
if (!mStateExecuteQueue.empty()) {
|
||||||
@@ -722,6 +926,16 @@ void TLuaEngine::StateThreadData::operator()() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> TLuaEngine::StateThreadData::Debug_GetStateExecuteQueue() {
|
||||||
|
std::unique_lock Lock(mStateExecuteQueueMutex);
|
||||||
|
return mStateExecuteQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<TLuaEngine::QueuedFunction> TLuaEngine::StateThreadData::Debug_GetStateFunctionQueue() {
|
||||||
|
std::unique_lock Lock(mStateFunctionQueueMutex);
|
||||||
|
return mStateFunctionQueue;
|
||||||
|
}
|
||||||
|
|
||||||
void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy) {
|
void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy) {
|
||||||
std::unique_lock Lock(mTimedEventsMutex);
|
std::unique_lock Lock(mTimedEventsMutex);
|
||||||
TimedEvent Event {
|
TimedEvent Event {
|
||||||
@@ -776,60 +990,3 @@ bool TLuaEngine::TimedEvent::Expired() {
|
|||||||
void TLuaEngine::TimedEvent::Reset() {
|
void TLuaEngine::TimedEvent::Reset() {
|
||||||
LastCompletion = std::chrono::high_resolution_clock::now();
|
LastCompletion = std::chrono::high_resolution_clock::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
TPluginMonitor::TPluginMonitor(const fs::path& Path, TLuaEngine& Engine, std::atomic_bool& Shutdown)
|
|
||||||
: mEngine(Engine)
|
|
||||||
, mPath(Path)
|
|
||||||
, mShutdown(Shutdown) {
|
|
||||||
if (!fs::exists(mPath)) {
|
|
||||||
fs::create_directories(mPath);
|
|
||||||
}
|
|
||||||
for (const auto& Entry : fs::recursive_directory_iterator(mPath)) {
|
|
||||||
// TODO: trigger an event when a subfolder file changes
|
|
||||||
if (Entry.is_regular_file()) {
|
|
||||||
mFileTimes[Entry.path().string()] = fs::last_write_time(Entry.path());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TPluginMonitor::operator()() {
|
|
||||||
RegisterThread("PluginMonitor");
|
|
||||||
beammp_info("PluginMonitor started");
|
|
||||||
while (!mShutdown) {
|
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
|
||||||
for (const auto& Pair : mFileTimes) {
|
|
||||||
auto CurrentTime = fs::last_write_time(Pair.first);
|
|
||||||
if (CurrentTime != Pair.second) {
|
|
||||||
mFileTimes[Pair.first] = CurrentTime;
|
|
||||||
// grandparent of the path should be Resources/Server
|
|
||||||
if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) {
|
|
||||||
beammp_info("File \"" + Pair.first + "\" changed, reloading");
|
|
||||||
// is in root folder, so reload
|
|
||||||
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
|
|
||||||
auto Size = std::filesystem::file_size(Pair.first);
|
|
||||||
auto Contents = std::make_shared<std::string>();
|
|
||||||
Contents->resize(Size);
|
|
||||||
FileStream.read(Contents->data(), Contents->size());
|
|
||||||
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
|
|
||||||
auto StateID = mEngine.GetStateIDForPlugin(fs::path(Pair.first).parent_path());
|
|
||||||
auto Res = mEngine.EnqueueScript(StateID, Chunk);
|
|
||||||
// TODO: call onInit
|
|
||||||
mEngine.AddResultToCheck(Res);
|
|
||||||
} else {
|
|
||||||
// TODO: trigger onFileChanged event
|
|
||||||
beammp_trace("Change detected in file \"" + Pair.first + "\", event trigger not implemented yet");
|
|
||||||
/*
|
|
||||||
// is in subfolder, dont reload, just trigger an event
|
|
||||||
auto Results = mEngine.TriggerEvent("onFileChanged", "", Pair.first);
|
|
||||||
mEngine.WaitForAll(Results);
|
|
||||||
for (const auto& Result : Results) {
|
|
||||||
if (Result->Error) {
|
|
||||||
beammp_lua_error(Result->ErrorMessage);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
+127
-45
@@ -6,6 +6,7 @@
|
|||||||
#include <Http.h>
|
#include <Http.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <dlhttp/dlhttp.h>
|
||||||
|
|
||||||
TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager)
|
TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager)
|
||||||
: mServer(Server)
|
: mServer(Server)
|
||||||
@@ -25,7 +26,6 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
|||||||
Application::RegisterShutdownHandler([&] {
|
Application::RegisterShutdownHandler([&] {
|
||||||
Application::SetSubsystemStatus("UDPNetwork", Application::Status::ShuttingDown);
|
Application::SetSubsystemStatus("UDPNetwork", Application::Status::ShuttingDown);
|
||||||
if (mUDPThread.joinable()) {
|
if (mUDPThread.joinable()) {
|
||||||
mShutdown = true;
|
|
||||||
mUDPThread.detach();
|
mUDPThread.detach();
|
||||||
}
|
}
|
||||||
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Shutdown);
|
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Shutdown);
|
||||||
@@ -33,7 +33,6 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
|||||||
Application::RegisterShutdownHandler([&] {
|
Application::RegisterShutdownHandler([&] {
|
||||||
Application::SetSubsystemStatus("TCPNetwork", Application::Status::ShuttingDown);
|
Application::SetSubsystemStatus("TCPNetwork", Application::Status::ShuttingDown);
|
||||||
if (mTCPThread.joinable()) {
|
if (mTCPThread.joinable()) {
|
||||||
mShutdown = true;
|
|
||||||
mTCPThread.detach();
|
mTCPThread.detach();
|
||||||
}
|
}
|
||||||
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Shutdown);
|
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Shutdown);
|
||||||
@@ -68,7 +67,7 @@ void TNetwork::UDPServerMain() {
|
|||||||
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Good);
|
Application::SetSubsystemStatus("UDPNetwork", Application::Status::Good);
|
||||||
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
|
beammp_info(("Vehicle data network online on port ") + std::to_string(Application::Settings.Port) + (" with a Max of ")
|
||||||
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
|
+ std::to_string(Application::Settings.MaxPlayers) + (" Clients"));
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
try {
|
try {
|
||||||
sockaddr_in client {};
|
sockaddr_in client {};
|
||||||
std::string Data = UDPRcvFromClient(client); // Receives any data from Socket
|
std::string Data = UDPRcvFromClient(client); // Receives any data from Socket
|
||||||
@@ -108,43 +107,57 @@ void TNetwork::TCPServerMain() {
|
|||||||
#if defined(BEAMMP_WINDOWS)
|
#if defined(BEAMMP_WINDOWS)
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
if (WSAStartup(514, &wsaData)) {
|
if (WSAStartup(514, &wsaData)) {
|
||||||
beammp_error("Can't start Winsock!");
|
beammp_error("Can't start Winsock! Shutting down");
|
||||||
return;
|
Application::GracefullyShutdown();
|
||||||
}
|
}
|
||||||
#endif // WINDOWS
|
#endif // WINDOWS
|
||||||
TConnection client {};
|
TConnection client {};
|
||||||
SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
SOCKET Listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
int optval = 1;
|
if (Listener == BEAMMP_INVALID_SOCKET) {
|
||||||
|
beammp_error("Failed to create socket: " + GetPlatformAgnosticErrorString()
|
||||||
|
+ ". This is a fatal error, as a socket is needed for the server to operate. Shutting down.");
|
||||||
|
Application::GracefullyShutdown();
|
||||||
|
}
|
||||||
#if defined(BEAMMP_WINDOWS)
|
#if defined(BEAMMP_WINDOWS)
|
||||||
const char* optval_ptr = reinterpret_cast<const char*>(&optval);
|
const char optval = 0;
|
||||||
|
int ret = ::setsockopt(Listener, SOL_SOCKET, SO_DONTLINGER, &optval, sizeof(optval));
|
||||||
#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE)
|
#elif defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE)
|
||||||
void* optval_ptr = reinterpret_cast<void*>(&optval);
|
int optval = true;
|
||||||
|
int ret = ::setsockopt(Listener, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<void*>(&optval), sizeof(optval));
|
||||||
#endif
|
#endif
|
||||||
setsockopt(Listener, SOL_SOCKET, SO_REUSEADDR, optval_ptr, sizeof(optval));
|
// not a fatal error
|
||||||
// TODO: check optval or return value idk
|
if (ret < 0) {
|
||||||
|
beammp_error("Failed to set up listening socket to not linger / reuse address. "
|
||||||
|
"This may cause the socket to refuse to bind(). Error: "
|
||||||
|
+ GetPlatformAgnosticErrorString());
|
||||||
|
}
|
||||||
sockaddr_in addr {};
|
sockaddr_in addr {};
|
||||||
addr.sin_addr.s_addr = INADDR_ANY;
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(uint16_t(Application::Settings.Port));
|
addr.sin_port = htons(uint16_t(Application::Settings.Port));
|
||||||
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) != 0) {
|
if (bind(Listener, (sockaddr*)&addr, sizeof(addr)) < 0) {
|
||||||
beammp_error("bind() failed: " + GetPlatformAgnosticErrorString());
|
beammp_error("bind() failed, the server cannot operate and will shut down now. "
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
"Error: "
|
||||||
exit(-1); // TODO: Wtf.
|
+ GetPlatformAgnosticErrorString());
|
||||||
|
Application::GracefullyShutdown();
|
||||||
}
|
}
|
||||||
if (Listener == -1) {
|
if (listen(Listener, SOMAXCONN) < 0) {
|
||||||
beammp_error("Invalid listening socket");
|
beammp_error("listen() failed, which is needed for the server to operate. "
|
||||||
return;
|
"Shutting down. Error: "
|
||||||
}
|
+ GetPlatformAgnosticErrorString());
|
||||||
if (listen(Listener, SOMAXCONN)) {
|
Application::GracefullyShutdown();
|
||||||
beammp_error("listen() failed: " + GetPlatformAgnosticErrorString());
|
|
||||||
// FIXME leak Listener
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Good);
|
Application::SetSubsystemStatus("TCPNetwork", Application::Status::Good);
|
||||||
beammp_info(("Vehicle event network online"));
|
beammp_info("Vehicle event network online");
|
||||||
|
const auto ThreadCount = std::max(1, Application::Settings.DownloadThreads);
|
||||||
|
beammp_debugf("Using {} download threads", ThreadCount);
|
||||||
|
dlhttp::AsyncContext Ctx(ThreadCount);
|
||||||
|
dlhttp::EndpointHandlerMap Map {
|
||||||
|
{ "/modlist", [this]() -> dlhttp::Response { return { mResourceManager.FileList() }; } },
|
||||||
|
};
|
||||||
do {
|
do {
|
||||||
try {
|
try {
|
||||||
if (mShutdown) {
|
if (Application::IsShuttingDown()) {
|
||||||
beammp_debug("shutdown during TCP wait for accept loop");
|
beammp_debug("shutdown during TCP wait for accept loop");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -154,25 +167,34 @@ void TNetwork::TCPServerMain() {
|
|||||||
beammp_warn(("Got an invalid client socket on connect! Skipping..."));
|
beammp_warn(("Got an invalid client socket on connect! Skipping..."));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// set timeout (DWORD, aka uint32_t)
|
// set timeout
|
||||||
uint32_t SendTimeoutMS = 30 * 1000;
|
size_t SendTimeoutMS = 30 * 1000;
|
||||||
#if defined(BEAMMP_WINDOWS)
|
#if defined(BEAMMP_WINDOWS)
|
||||||
int ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&SendTimeoutMS), sizeof(SendTimeoutMS));
|
int ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&SendTimeoutMS), sizeof(SendTimeoutMS));
|
||||||
#else // POSIX
|
#else // POSIX
|
||||||
struct timeval optval;
|
struct timeval optval;
|
||||||
optval.tv_sec = (int)(SendTimeoutMS / 1000);
|
optval.tv_sec = (int)(SendTimeoutMS / 1000);
|
||||||
optval.tv_usec = (SendTimeoutMS % 1000) * 1000;
|
optval.tv_usec = (SendTimeoutMS % 1000) * 1000;
|
||||||
int ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<void*>(&optval), sizeof(optval));
|
ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<void*>(&optval), sizeof(optval));
|
||||||
#endif
|
#endif
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString());
|
throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check for http
|
||||||
|
if (dlhttp::is_http(client.Socket)) {
|
||||||
|
beammp_info("IS HTTP!");
|
||||||
|
dlhttp::handle_http(client.Socket, Ctx, Map);
|
||||||
|
} else {
|
||||||
|
beammp_info("IS NOT HTTP!");
|
||||||
|
}
|
||||||
|
|
||||||
std::thread ID(&TNetwork::Identify, this, client);
|
std::thread ID(&TNetwork::Identify, this, client);
|
||||||
ID.detach(); // TODO: Add to a queue and attempt to join periodically
|
ID.detach(); // TODO: Add to a queue and attempt to join periodically
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
beammp_error(("fatal: ") + std::string(e.what()));
|
beammp_error("fatal: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
} while (client.Socket);
|
} while (client.Socket != BEAMMP_INVALID_SOCKET);
|
||||||
|
|
||||||
beammp_debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
beammp_debug("all ok, arrived at " + std::string(__func__) + ":" + std::to_string(__LINE__));
|
||||||
|
|
||||||
@@ -231,13 +253,26 @@ void TNetwork::HandleDownload(SOCKET TCPSock) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int get_ip_str(const struct sockaddr* sa, char* strBuf, size_t strBufSize) {
|
||||||
|
switch (sa->sa_family) {
|
||||||
|
case AF_INET:
|
||||||
|
inet_ntop(AF_INET, &(((struct sockaddr_in*)sa)->sin_addr), strBuf, strBufSize);
|
||||||
|
break;
|
||||||
|
case AF_INET6:
|
||||||
|
inet_ntop(AF_INET6, &(((struct sockaddr_in6*)sa)->sin6_addr), strBuf, strBufSize);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void TNetwork::Authentication(const TConnection& ClientConnection) {
|
void TNetwork::Authentication(const TConnection& ClientConnection) {
|
||||||
auto Client = CreateClient(ClientConnection.Socket);
|
auto Client = CreateClient(ClientConnection.Socket);
|
||||||
char AddrBuf[64];
|
char AddrBuf[INET6_ADDRSTRLEN];
|
||||||
// TODO: IPv6 would need this to be changed
|
get_ip_str(&ClientConnection.SockAddr, AddrBuf, sizeof(AddrBuf));
|
||||||
auto str = inet_ntop(AF_INET, reinterpret_cast<const void*>(&ClientConnection.SockAddr), AddrBuf, sizeof(ClientConnection.SockAddr));
|
beammp_trace("This thread is ip " + std::string(AddrBuf));
|
||||||
beammp_trace("This thread is ip " + std::string(str));
|
Client->SetIdentifier("ip", AddrBuf);
|
||||||
Client->SetIdentifier("ip", str);
|
|
||||||
|
|
||||||
std::string Rc; // TODO: figure out why this is not default constructed
|
std::string Rc; // TODO: figure out why this is not default constructed
|
||||||
beammp_info("Identifying new ClientConnection...");
|
beammp_info("Identifying new ClientConnection...");
|
||||||
@@ -477,7 +512,7 @@ std::string TNetwork::TCPRcv(TClient& c) {
|
|||||||
void TNetwork::ClientKick(TClient& c, const std::string& R) {
|
void TNetwork::ClientKick(TClient& c, const std::string& R) {
|
||||||
beammp_info("Client kicked: " + R);
|
beammp_info("Client kicked: " + R);
|
||||||
if (!TCPSend(c, "K" + R)) {
|
if (!TCPSend(c, "K" + R)) {
|
||||||
// TODO handle
|
beammp_warn("tried to kick player '" + c.GetName() + "' (id " + std::to_string(c.GetID()) + "), but was already disconnected");
|
||||||
}
|
}
|
||||||
c.SetStatus(-2);
|
c.SetStatus(-2);
|
||||||
|
|
||||||
@@ -753,14 +788,63 @@ void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static std::pair<size_t /* count */, size_t /* last chunk */> SplitIntoChunks(size_t FullSize, size_t ChunkSize) {
|
||||||
|
if (FullSize < ChunkSize) {
|
||||||
|
return { 0, FullSize };
|
||||||
|
}
|
||||||
|
size_t Count = FullSize / (FullSize / ChunkSize);
|
||||||
|
size_t LastChunkSize = FullSize - (Count * ChunkSize);
|
||||||
|
return { Count, LastChunkSize };
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("SplitIntoChunks") {
|
||||||
|
size_t FullSize;
|
||||||
|
size_t ChunkSize;
|
||||||
|
SUBCASE("Normal case") {
|
||||||
|
FullSize = 1234567;
|
||||||
|
ChunkSize = 1234;
|
||||||
|
}
|
||||||
|
SUBCASE("Zero original size") {
|
||||||
|
FullSize = 0;
|
||||||
|
ChunkSize = 100;
|
||||||
|
}
|
||||||
|
SUBCASE("Equal full size and chunk size") {
|
||||||
|
FullSize = 125;
|
||||||
|
ChunkSize = 125;
|
||||||
|
}
|
||||||
|
SUBCASE("Even split") {
|
||||||
|
FullSize = 10000;
|
||||||
|
ChunkSize = 100;
|
||||||
|
}
|
||||||
|
SUBCASE("Odd split") {
|
||||||
|
FullSize = 13;
|
||||||
|
ChunkSize = 2;
|
||||||
|
}
|
||||||
|
SUBCASE("Large sizes") {
|
||||||
|
FullSize = 10 * GB;
|
||||||
|
ChunkSize = 125 * MB;
|
||||||
|
}
|
||||||
|
auto [Count, LastSize] = SplitIntoChunks(FullSize, ChunkSize);
|
||||||
|
CHECK((Count * ChunkSize) + LastSize == FullSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* /* end ptr */ TNetwork::SendSplit(TClient& c, SOCKET Socket, uint8_t* DataPtr, size_t Size) {
|
||||||
|
if (TCPSendRaw(c, Socket, reinterpret_cast<char*>(DataPtr), Size)) {
|
||||||
|
return DataPtr + Size;
|
||||||
|
} else {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name) {
|
void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name) {
|
||||||
std::ifstream f(Name.c_str(), std::ios::binary);
|
std::ifstream f(Name.c_str(), std::ios::binary);
|
||||||
uint32_t Split = 0x7735940; // 125MB
|
auto Buf = f.rdbuf();
|
||||||
char* Data;
|
uint32_t Split = 125 * MB;
|
||||||
|
std::vector<uint8_t> Data;
|
||||||
if (Size > Split)
|
if (Size > Split)
|
||||||
Data = new char[Split];
|
Data.resize(Split);
|
||||||
else
|
else
|
||||||
Data = new char[Size];
|
Data.resize(Size);
|
||||||
SOCKET TCPSock;
|
SOCKET TCPSock;
|
||||||
if (D)
|
if (D)
|
||||||
TCPSock = c.GetDownSock();
|
TCPSock = c.GetDownSock();
|
||||||
@@ -771,8 +855,8 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
size_t Diff = Size - Sent;
|
size_t Diff = Size - Sent;
|
||||||
if (Diff > Split) {
|
if (Diff > Split) {
|
||||||
f.seekg(Sent, std::ios_base::beg);
|
f.seekg(Sent, std::ios_base::beg);
|
||||||
f.read(Data, Split);
|
f.read(reinterpret_cast<char*>(Data.data()), Split);
|
||||||
if (!TCPSendRaw(c, TCPSock, Data, Split)) {
|
if (!TCPSendRaw(c, TCPSock, reinterpret_cast<char*>(Data.data()), Split)) {
|
||||||
if (c.GetStatus() > -1)
|
if (c.GetStatus() > -1)
|
||||||
c.SetStatus(-1);
|
c.SetStatus(-1);
|
||||||
break;
|
break;
|
||||||
@@ -780,8 +864,8 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
Sent += Split;
|
Sent += Split;
|
||||||
} else {
|
} else {
|
||||||
f.seekg(Sent, std::ios_base::beg);
|
f.seekg(Sent, std::ios_base::beg);
|
||||||
f.read(Data, Diff);
|
f.read(reinterpret_cast<char*>(Data.data()), Diff);
|
||||||
if (!TCPSendRaw(c, TCPSock, Data, int32_t(Diff))) {
|
if (!TCPSendRaw(c, TCPSock, reinterpret_cast<char*>(Data.data()), int32_t(Diff))) {
|
||||||
if (c.GetStatus() > -1)
|
if (c.GetStatus() > -1)
|
||||||
c.SetStatus(-1);
|
c.SetStatus(-1);
|
||||||
break;
|
break;
|
||||||
@@ -789,8 +873,6 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
Sent += Diff;
|
Sent += Diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete[] Data;
|
|
||||||
f.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TNetwork::TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size) {
|
bool TNetwork::TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size) {
|
||||||
|
|||||||
+1
-2
@@ -10,7 +10,6 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
|
|||||||
Application::SetSubsystemStatus("PPSMonitor", Application::Status::ShuttingDown);
|
Application::SetSubsystemStatus("PPSMonitor", Application::Status::ShuttingDown);
|
||||||
if (mThread.joinable()) {
|
if (mThread.joinable()) {
|
||||||
beammp_debug("shutting down PPSMonitor");
|
beammp_debug("shutting down PPSMonitor");
|
||||||
mShutdown = true;
|
|
||||||
mThread.join();
|
mThread.join();
|
||||||
beammp_debug("shut down PPSMonitor");
|
beammp_debug("shut down PPSMonitor");
|
||||||
}
|
}
|
||||||
@@ -27,7 +26,7 @@ void TPPSMonitor::operator()() {
|
|||||||
beammp_debug("PPSMonitor starting");
|
beammp_debug("PPSMonitor starting");
|
||||||
Application::SetSubsystemStatus("PPSMonitor", Application::Status::Good);
|
Application::SetSubsystemStatus("PPSMonitor", Application::Status::Good);
|
||||||
std::vector<std::shared_ptr<TClient>> TimedOutClients;
|
std::vector<std::shared_ptr<TClient>> TimedOutClients;
|
||||||
while (!mShutdown) {
|
while (!Application::IsShuttingDown()) {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
int C = 0, V = 0;
|
int C = 0, V = 0;
|
||||||
if (mServer.ClientCount() == 0) {
|
if (mServer.ClientCount() == 0) {
|
||||||
|
|||||||
@@ -0,0 +1,75 @@
|
|||||||
|
#include "TPluginMonitor.h"
|
||||||
|
|
||||||
|
#include "TLuaEngine.h"
|
||||||
|
|
||||||
|
TPluginMonitor::TPluginMonitor(const fs::path& Path, std::shared_ptr<TLuaEngine> Engine)
|
||||||
|
: mEngine(Engine)
|
||||||
|
, mPath(Path) {
|
||||||
|
Application::SetSubsystemStatus("PluginMonitor", Application::Status::Starting);
|
||||||
|
if (!fs::exists(mPath)) {
|
||||||
|
fs::create_directories(mPath);
|
||||||
|
}
|
||||||
|
for (const auto& Entry : fs::recursive_directory_iterator(mPath)) {
|
||||||
|
// TODO: trigger an event when a subfolder file changes
|
||||||
|
if (Entry.is_regular_file()) {
|
||||||
|
mFileTimes[Entry.path().string()] = fs::last_write_time(Entry.path());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Application::RegisterShutdownHandler([this] {
|
||||||
|
if (mThread.joinable()) {
|
||||||
|
mThread.join();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void TPluginMonitor::operator()() {
|
||||||
|
RegisterThread("PluginMonitor");
|
||||||
|
beammp_info("PluginMonitor started");
|
||||||
|
Application::SetSubsystemStatus("PluginMonitor", Application::Status::Good);
|
||||||
|
while (!Application::IsShuttingDown()) {
|
||||||
|
std::vector<std::string> ToRemove;
|
||||||
|
for (const auto& Pair : mFileTimes) {
|
||||||
|
try {
|
||||||
|
auto CurrentTime = fs::last_write_time(Pair.first);
|
||||||
|
if (CurrentTime > Pair.second) {
|
||||||
|
mFileTimes[Pair.first] = CurrentTime;
|
||||||
|
// grandparent of the path should be Resources/Server
|
||||||
|
if (fs::equivalent(fs::path(Pair.first).parent_path().parent_path(), mPath)) {
|
||||||
|
beammp_infof("File \"{}\" changed, reloading", Pair.first);
|
||||||
|
// is in root folder, so reload
|
||||||
|
std::ifstream FileStream(Pair.first, std::ios::in | std::ios::binary);
|
||||||
|
auto Size = std::filesystem::file_size(Pair.first);
|
||||||
|
auto Contents = std::make_shared<std::string>();
|
||||||
|
Contents->resize(Size);
|
||||||
|
FileStream.read(Contents->data(), Contents->size());
|
||||||
|
TLuaChunk Chunk(Contents, Pair.first, fs::path(Pair.first).parent_path().string());
|
||||||
|
auto StateID = mEngine->GetStateIDForPlugin(fs::path(Pair.first).parent_path());
|
||||||
|
auto Res = mEngine->EnqueueScript(StateID, Chunk);
|
||||||
|
Res->WaitUntilReady();
|
||||||
|
if (Res->Error) {
|
||||||
|
beammp_lua_errorf("Error while hot-reloading \"{}\": {}", Pair.first, Res->ErrorMessage);
|
||||||
|
} else {
|
||||||
|
mEngine->ReportErrors(mEngine->TriggerLocalEvent(StateID, "onInit"));
|
||||||
|
mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// is in subfolder, dont reload, just trigger an event
|
||||||
|
beammp_debugf("File \"{}\" changed, not reloading because it's in a subdirectory. Triggering 'onFileChanged' event instead", Pair.first);
|
||||||
|
mEngine->ReportErrors(mEngine->TriggerEvent("onFileChanged", "", Pair.first));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
ToRemove.push_back(Pair.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Application::SleepSafeSeconds(3);
|
||||||
|
for (const auto& File : ToRemove) {
|
||||||
|
mFileTimes.erase(File);
|
||||||
|
beammp_warnf("File \"{}\" couldn't be accessed, so it was removed from plugin hot reload monitor (probably got deleted)", File);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Application::SetSubsystemStatus("PluginMonitor", Application::Status::Shutdown);
|
||||||
|
}
|
||||||
@@ -28,8 +28,9 @@ TResourceManager::TResourceManager() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mModsLoaded)
|
if (mModsLoaded) {
|
||||||
beammp_info("Loaded " + std::to_string(mModsLoaded) + " Mods");
|
beammp_info("Loaded " + std::to_string(mModsLoaded) + " Mods");
|
||||||
|
}
|
||||||
|
|
||||||
Application::SetSubsystemStatus("ResourceManager", Application::Status::Good);
|
Application::SetSubsystemStatus("ResourceManager", Application::Status::Good);
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-19
@@ -125,7 +125,7 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
|
|||||||
break;
|
break;
|
||||||
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 2));
|
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 2));
|
||||||
TLuaEngine::WaitForAll(Futures);
|
TLuaEngine::WaitForAll(Futures);
|
||||||
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), Packet.substr(Packet.find(':', 3) + 1)); // FIXME: this needs to be adjusted once lua is merged
|
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), Packet.substr(Packet.find(':', 3) + 1));
|
||||||
if (std::any_of(Futures.begin(), Futures.end(),
|
if (std::any_of(Futures.begin(), Futures.end(),
|
||||||
[](const std::shared_ptr<TLuaResult>& Elem) {
|
[](const std::shared_ptr<TLuaResult>& Elem) {
|
||||||
return !Elem->Error
|
return !Elem->Error
|
||||||
@@ -150,26 +150,18 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TServer::HandleEvent(TClient& c, const std::string& Data) {
|
void TServer::HandleEvent(TClient& c, const std::string& RawData) {
|
||||||
std::stringstream ss(Data);
|
// E:Name:Data
|
||||||
std::string t, Name;
|
// Data is allowed to have ':'
|
||||||
int a = 0;
|
auto NameDataSep = RawData.find(':', 2);
|
||||||
while (std::getline(ss, t, ':')) {
|
if (NameDataSep == std::string::npos) {
|
||||||
switch (a) {
|
beammp_warn("received event in invalid format (missing ':'), got: '" + RawData + "'");
|
||||||
case 1:
|
|
||||||
Name = t;
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent(Name, "", c.GetID(), t));
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (a == 2)
|
|
||||||
break;
|
|
||||||
a++;
|
|
||||||
}
|
}
|
||||||
|
std::string Name = RawData.substr(2, NameDataSep - 2);
|
||||||
|
std::string Data = RawData.substr(NameDataSep + 1);
|
||||||
|
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent(Name, "", c.GetID(), Data));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TServer::IsUnicycle(TClient& c, const std::string& CarJson) {
|
bool TServer::IsUnicycle(TClient& c, const std::string& CarJson) {
|
||||||
try {
|
try {
|
||||||
auto Car = nlohmann::json::parse(CarJson);
|
auto Car = nlohmann::json::parse(CarJson);
|
||||||
|
|||||||
+10
-12
@@ -11,13 +11,14 @@
|
|||||||
#include "TLuaEngine.h"
|
#include "TLuaEngine.h"
|
||||||
#include "TNetwork.h"
|
#include "TNetwork.h"
|
||||||
#include "TPPSMonitor.h"
|
#include "TPPSMonitor.h"
|
||||||
|
#include "TPluginMonitor.h"
|
||||||
#include "TResourceManager.h"
|
#include "TResourceManager.h"
|
||||||
#include "TScopedTimer.h"
|
#include "TScopedTimer.h"
|
||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#define CPPHTTPLIB_OPENSSL_SUPPORT 1
|
|
||||||
static const std::string sCommandlineArguments = R"(
|
static const std::string sCommandlineArguments = R"(
|
||||||
USAGE:
|
USAGE:
|
||||||
BeamMP-Server [arguments]
|
BeamMP-Server [arguments]
|
||||||
@@ -44,10 +45,6 @@ EXAMPLES:
|
|||||||
'MyWestCoastServerConfig.toml'.
|
'MyWestCoastServerConfig.toml'.
|
||||||
)";
|
)";
|
||||||
|
|
||||||
// this is provided by the build system, leave empty for source builds
|
|
||||||
// global, yes, this is ugly, no, it cant be done another way
|
|
||||||
TSentry Sentry {};
|
|
||||||
|
|
||||||
struct MainArguments {
|
struct MainArguments {
|
||||||
int argc {};
|
int argc {};
|
||||||
char** argv {};
|
char** argv {};
|
||||||
@@ -72,7 +69,7 @@ int main(int argc, char** argv) {
|
|||||||
Sentry.LogException(e, _file_basename, _line);
|
Sentry.LogException(e, _file_basename, _line);
|
||||||
MainRet = -1;
|
MainRet = -1;
|
||||||
}
|
}
|
||||||
return MainRet;
|
std::exit(MainRet);
|
||||||
}
|
}
|
||||||
|
|
||||||
int BeamMPServerMain(MainArguments Arguments) {
|
int BeamMPServerMain(MainArguments Arguments) {
|
||||||
@@ -113,7 +110,7 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
try {
|
try {
|
||||||
fs::current_path(fs::path(MaybeWorkingDirectory.value()));
|
fs::current_path(fs::path(MaybeWorkingDirectory.value()));
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
beammp_error("Could not set working directory to '" + MaybeWorkingDirectory.value() + "': " + e.what());
|
beammp_errorf("Could not set working directory to '{}': {}", MaybeWorkingDirectory.value(), e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,9 +134,9 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
|
|
||||||
TServer Server(Arguments.List);
|
TServer Server(Arguments.List);
|
||||||
TConfig Config(ConfigPath);
|
TConfig Config(ConfigPath);
|
||||||
TLuaEngine LuaEngine;
|
auto LuaEngine = std::make_shared<TLuaEngine>();
|
||||||
LuaEngine.SetServer(&Server);
|
LuaEngine->SetServer(&Server);
|
||||||
Application::Console().InitializeLuaConsole(LuaEngine);
|
Application::Console().InitializeLuaConsole(*LuaEngine);
|
||||||
|
|
||||||
if (Config.Failed()) {
|
if (Config.Failed()) {
|
||||||
beammp_info("Closing in 10 seconds");
|
beammp_info("Closing in 10 seconds");
|
||||||
@@ -159,12 +156,13 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
TPPSMonitor PPSMonitor(Server);
|
TPPSMonitor PPSMonitor(Server);
|
||||||
THeartbeatThread Heartbeat(ResourceManager, Server);
|
THeartbeatThread Heartbeat(ResourceManager, Server);
|
||||||
TNetwork Network(Server, PPSMonitor, ResourceManager);
|
TNetwork Network(Server, PPSMonitor, ResourceManager);
|
||||||
LuaEngine.SetNetwork(&Network);
|
LuaEngine->SetNetwork(&Network);
|
||||||
PPSMonitor.SetNetwork(Network);
|
PPSMonitor.SetNetwork(Network);
|
||||||
Application::CheckForUpdates();
|
Application::CheckForUpdates();
|
||||||
|
|
||||||
|
TPluginMonitor PluginMonitor(fs::path(Application::Settings.Resource) / "Server", LuaEngine);
|
||||||
|
|
||||||
if (Application::Settings.HTTPServerEnabled) {
|
if (Application::Settings.HTTPServerEnabled) {
|
||||||
Http::Server::SetupEnvironment();
|
|
||||||
Http::Server::THttpServerInstance HttpServerInstance {};
|
Http::Server::THttpServerInstance HttpServerInstance {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#define DOCTEST_CONFIG_IMPLEMENT
|
||||||
|
#include <doctest/doctest.h>
|
||||||
|
|
||||||
|
#include <Common.h>
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
doctest::Context context;
|
||||||
|
|
||||||
|
// Application::InitializeConsole();
|
||||||
|
|
||||||
|
context.applyCommandLine(argc, argv);
|
||||||
|
|
||||||
|
int res = context.run(); // run
|
||||||
|
|
||||||
|
if (context.shouldExit()) // important - query flags (and --exit) rely on the user doing this
|
||||||
|
return res; // propagate the result of the tests
|
||||||
|
|
||||||
|
int client_stuff_return_code = 0;
|
||||||
|
// your program - if the testing framework is integrated in your production code
|
||||||
|
|
||||||
|
return res + client_stuff_return_code; // the result from doctest is propagated here as well
|
||||||
|
}
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
include(FetchContent)
|
|
||||||
|
|
||||||
message(STATUS "Getting, checking and running vcpkg, this may take a while")
|
|
||||||
|
|
||||||
FetchContent_Declare(
|
|
||||||
vcpkg
|
|
||||||
GIT_REPOSITORY https://github.com/microsoft/vcpkg.git
|
|
||||||
)
|
|
||||||
|
|
||||||
FetchContent_GetProperties(vcpkg)
|
|
||||||
|
|
||||||
if(NOT vcpkg_POPULATED)
|
|
||||||
FetchContent_Populate(vcpkg)
|
|
||||||
execute_process(COMMAND ./${vcpkg_SOURCE_DIR}/bootstrap-vcpkg.sh)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
set(CMAKE_TOOLCHAIN_FILE ${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake
|
|
||||||
CACHE STRING "Vcpkg toolchain file")
|
|
||||||
-17
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "beammp-server",
|
|
||||||
"version-string": "3.0.2",
|
|
||||||
"homepage": "https://beammp.com",
|
|
||||||
"dependencies": [
|
|
||||||
"sentry-native",
|
|
||||||
"lua",
|
|
||||||
"zlib",
|
|
||||||
"rapidjson",
|
|
||||||
"nlohmann-json",
|
|
||||||
"openssl",
|
|
||||||
"curl",
|
|
||||||
"sol2",
|
|
||||||
"cpp-httplib",
|
|
||||||
"toml11"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user