Compare commits

..

54 Commits

Author SHA1 Message Date
8ec4602ee3 Add PyAPI skell 2022-09-27 16:30:47 +03:00
5f4ae36bbf Del const 2022-09-27 15:55:48 +03:00
15f6321310 Change server dir 2022-09-27 15:55:38 +03:00
842d142a0e Modify LUA 2022-09-27 14:28:39 +03:00
fb52f01295 Fix for linux 2022-09-27 13:00:05 +03:00
b974f194da Fix for linux 2022-09-27 12:59:09 +03:00
18c01fddfe ReTab.. :) 2022-09-27 12:46:44 +03:00
cd5252a473 Change *.log naming. 2022-09-27 12:46:26 +03:00
924c946be9 Add new start argument - locale
Optimization for Russian locale.
2022-09-27 12:46:07 +03:00
660354574e Change vcpkg command 2022-09-27 05:18:44 +03:00
3fc998d402 Change in Clion Files 2022-09-27 05:04:06 +03:00
7abc4566ad Merge README from master branch 2022-09-27 04:27:37 +03:00
f77ec41303 Add vcpkg packages for x64 systems 2022-09-27 03:55:36 +03:00
e81acbe60d Fix debian dependencies 2022-09-27 03:52:46 +03:00
b8d38f0b6d Add a little formatting 2022-09-27 03:52:25 +03:00
c6f52dbcb7 Syntax fix 2022-09-27 03:51:26 +03:00
8162dd4d75 Add MacOs block 2022-09-27 03:26:52 +03:00
2d4d40c24c Change URL and set clone from branch with Python support. 2022-09-27 03:25:15 +03:00
Lion Kortlepel
f8d622352f generate toml from scratch 2022-07-09 22:42:38 +02:00
Lion Kortlepel
6c1d02a425 add fclose 2022-07-09 22:29:12 +02:00
Lion Kortlepel
38eeec39b4 another attempt to fix #105 2022-07-09 22:27:05 +02:00
Lion Kortlepel
696e080e1c fix #105 2022-07-09 22:01:53 +02:00
Lion Kortlepel
98681254e6 update toml11 2022-07-09 21:50:23 +02:00
Lion Kortlepel
420e6c3533 roll back to an ancient version of sentry
sentry-native deprecated compiling without error somewhere around one of
the next version
2022-07-09 21:40:19 +02:00
Lion Kortlepel
06f8ba5a0e update sentry-native to 0.4.18 2022-07-09 21:11:34 +02:00
Lion Kortlepel
758d5b2c96 Revert "let's try vcpkg"
This reverts commit 8d7505956d.
2022-05-29 14:37:50 +02:00
Lion Kortlepel
1970d97ea4 Revert "Remove unneeded submodules"
This reverts commit a5153e4bc1.
2022-05-29 14:37:41 +02:00
Lion Kortlepel
d8526f0649 TNetwork::SplitLoad: Use managed memory 2022-05-29 14:30:57 +02:00
Lion Kortlepel
a5153e4bc1 Remove unneeded submodules 2022-04-28 16:28:23 +02:00
Lion Kortlepel
8d7505956d let's try vcpkg 2022-04-28 16:26:30 +02:00
Lion Kortlepel
3b2016d09f Windows moment
Windows deprecated when
2022-04-28 14:59:41 +02:00
Lion Kortlepel
ca52d233c0 Use another git commit id for vcpkg
this should really be done properly, yikes
2022-04-28 14:34:05 +02:00
Lion Kortlepel
34b39aad4d add message to shutdown suggesting Ctrl+C if it takes too long
This is an ongoing issue that needs to be resolved properly,
but I'm not sure what's hanging it.
2022-04-28 14:30:44 +02:00
Lion Kortlepel
f4eb492d91 Actions: try to use master as vcpkgGitCommitId 2022-04-28 14:24:02 +02:00
Lion Kortlepel
7f41a2a574 Update Changelog to reflect latest changes 2022-04-28 14:18:10 +02:00
Lion Kortlepel
11c53e0b3a Remove unused error, as Sentry's code fails to build otherwise
Thanks, sentry.
2022-04-28 14:13:51 +02:00
Lion Kortlepel
974dda9f8b HTTPServer: Add config value to specify listen IP
Change default IP to localhost,
Set default SSL to false due to this.
2022-04-28 14:12:26 +02:00
Lion Kortlepel
0979c8b1e4 HTTPServer: Attempt to catch more errors 2022-04-28 14:04:54 +02:00
Lion Kortlepel
0761036c8c TConsole::StartLoggingToFile: implement 2022-04-28 13:46:25 +02:00
Lion Kortlepel
5ded713b4b Application::CheckForUpdates: only print status the first time 2022-04-28 13:33:38 +02:00
Lion Kortlepel
668cc496b6 update commandline, unused are now errors 2022-04-28 13:29:16 +02:00
Lion Kortlepel
056d20292a Make "unable to fetch version" a trace message 2022-04-28 13:14:28 +02:00
Lion Kortlepel
d8c33c03ee start work on new logger 2022-04-05 22:27:45 +02:00
Lion Kortlepel
1bab3276e9 fix setsockopt SO_SNDTIMEO for windows, bump version number to 3.0.2, update commandline 2022-04-05 10:59:16 +02:00
Lion Kortlepel
4ff69528bd fix some missing declaration 2022-03-31 23:56:02 +02:00
Lion Kortlepel
5e4c7eac51 add send timeout to client tcp socket 2022-03-31 23:53:10 +02:00
Lion Kortlepel
d01d79a49a update changelog 2022-03-31 20:27:08 +02:00
Lion Kortlepel
d4b30a2583 CreateEventTimer: Implement CallStrategy
There are two CallStrategies:

- BestEffort (default): Will try to get your event to trigger at the specified
  interval, but will refuse to queue handlers if a handler takes too
  long.
- Precise: Will enqueue event handlers at the exact interval specified.
  Can lead to the queue filling up if the handler takes longer than the
  interval.
2022-03-31 20:13:59 +02:00
Lion Kortlepel
81dbf747d5 Kick client if we fail to send them a client event 2022-03-31 16:50:00 +02:00
Lion Kortlepel
b97397132d TLuaEngine: improve result queue handling 2022-03-31 15:59:31 +02:00
Lion Kortlepel
de82caef33 Add HideUpdateMessages setting ("ImScaredOfUpdates") and periodic update reminders (every 5th heartbeat) 2022-03-25 13:34:28 +01:00
Lion Kortlepel
f8c58f363a Change default MaxPlayers to 8 2022-03-25 13:32:41 +01:00
Lion Kortlepel
71c2d4b859 Simplify "Backend heartbeat response" error (closes #97) 2022-03-25 12:55:35 +01:00
Lion
a97763a94f Merge pull request #91 from BeamMP/rc-v3.0.1
Release Candidate v3.0.1
2022-03-10 17:03:43 +01:00
29 changed files with 418 additions and 200 deletions

View File

@@ -20,7 +20,7 @@ jobs:
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '8dddc6c899ce6fdbeab38b525a31e7f23cb2d5bb'
vcpkgGitCommitId: 'a106de33bbee694e3be6243718aa2a549a692832'
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment

View File

@@ -85,7 +85,7 @@ jobs:
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: '8dddc6c899ce6fdbeab38b525a31e7f23cb2d5bb'
vcpkgGitCommitId: 'a106de33bbee694e3be6243718aa2a549a692832'
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment

3
.gitignore vendored
View File

@@ -27,8 +27,7 @@ mono_crash.*
out/
#Clion Files
cmake-build-debug/
cmake-build-release/
cmake-build-*/
.idea/
# Build results
[Dd]ebug/

View File

@@ -37,7 +37,7 @@ if (WIN32)
endif()
include_directories("include/sentry-native/include")
set(SENTRY_BUILD_SHARED_LIBS OFF)
set(BUILD_SHARED_LIBS OFF)
if (MSVC)
set(SENTRY_BUILD_RUNTIMESTATIC ON)
endif()
@@ -60,6 +60,10 @@ 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)
@@ -78,9 +82,6 @@ elseif (UNIX)
endif ()
message(STATUS "Adding local source dependencies")
# this has to happen before -DDEBUG since it wont compile properly with -DDEBUG
add_subdirectory(deps)
set(CMAKE_CXX_STANDARD 17)
@@ -107,7 +108,10 @@ add_executable(BeamMP-Server
include/TScopedTimer.h src/TScopedTimer.cpp
include/SignalHandling.h src/SignalHandling.cpp
include/ArgsParser.h src/ArgsParser.cpp
include/Environment.h)
include/Environment.h
src/TPyEngine.cpp include/TPyEngine.h
src/TPyPlugin.cpp include/TPyPlugin.h
src/PyAPI.cpp include/PyAPI.h)
target_compile_definitions(BeamMP-Server PRIVATE SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}")
include_directories(BeamMP-Server PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

View File

@@ -1,3 +1,14 @@
# v3.0.2
- ADDED Periodic update message if a new server is released
- ADDED Config setting for the IP the http server listens on
- CHANGED Default MaxPlayers to 8
- CHANGED Default http server listen IP to localhost
- 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 Lua result queue handling not checking all results
- FIXED bug which caused ServerConfig.toml to generate incorrectly
# v3.0.1
- ADDED Backup URLs to UpdateCheck (will fail less often now)

View File

@@ -4,9 +4,9 @@
[![CMake Linux Build](https://github.com/BeamMP/BeamMP-Server/workflows/CMake%20Linux%20Build/badge.svg?branch=master)](https://github.com/BeamMP/BeamMP-Server/actions?query=workflow%3A%22CMake+Linux+Build%22)
This is the server for the multiplayer mod **[BeamMP](https://beammp.com/)** for the game [BeamNG.drive](https://www.beamng.com/).
The server is the point throug which all clients communicate. You can write lua mods for the server, detailed instructions on the [BeamMP Wiki](https://wiki.beammp.com).
The server is the point through which all clients communicate. You can write Lua mods for the server, there are detailed instructions on the [BeamMP Wiki](https://wiki.beammp.com).
**For Linux, you __need__ the runtime dependencies, listed below under "prerequisites".**
**For Linux, you __need__ the runtime dependencies, listed below under "[prerequisites](#prerequisites)".**
## Support + Contact
@@ -14,6 +14,7 @@ Feel free to ask any questions via the following channels:
- **IRC**: `#beammp` on [irc.libera.chat](https://web.libera.chat/)
- **Discord**: [click for invite](https://discord.gg/beammp)
- **BeamMP Forum**: [BeamMP Forum Support](https://forum.beammp.com/c/support/33)
## Minimum Requirements
@@ -28,23 +29,23 @@ These values are guesstimated and are subject to change with each release.
## Contributing
TLDR; [Issues](https://github.com/BeamMP/BeamMP-Server/issues) with the "help wanted" label or with nobody assigned, any [trello](https://trello.com/b/Kw75j3zZ/beamngdrive-multiplayer) cards in the "To-Do" column.
TLDR; [Issues](https://github.com/BeamMP/BeamMP-Server/issues) with the "help wanted" label or with nobody assigned.
To contribute, look at the active [issues](https://github.com/BeamMP/BeamMP-Server/issues) and at the [trello](https://trello.com/b/Kw75j3zZ/beamngdrive-multiplayer). Any issues that have the "help wanted" label or don't have anyone assigned and any trello cards that aren't assigned or in the "In-Progress" section are good tasks to take on. You can either contribute by programming or by testing and adding more info and ideas.
To contribute, look at the active [issues](https://github.com/BeamMP/BeamMP-Server/issues). Any issues that have the "help wanted" label or don't have anyone assigned are good tasks to take on. You can either contribute by programming or by testing and adding more info and ideas.
Fork this repository, make a new branch for your feature, implement your feature or fix, and then create a pull-request here. Even incomplete features and fixes can be pull-requested.
If you need support with understanding the codebase, please write us in the discord. You'll need to be proficient in modern C++.
If you need support with understanding the codebase, please write us in the Discord. You'll need to be proficient in modern C++.
## About Building from Source
We only allow building unmodified (original) source code for public use. `master` is considered **unstable** and we will not provide technical support if such a build doesn't work, so always build from a tag. You can checkout a tag with `git checkout tags/TAGNAME`, where `TAGNAME` is the tag, for example `v1.20`.
We only allow building unmodified (original) source code for public use. `master` is considered **unstable,** and we will not provide technical support if such a build doesn't work, so always build from a tag. You can check out a tag with `git checkout tags/TAGNAME`, where `TAGNAME` is the tag, for example `v1.20`.
## Supported Operating Systems
The code itself supports (latest stable) Linux and Windows. In terms of actual build support, for now we usually only distribute windows binaries and sometimes linux. For any other distro or OS, you just have to find the same libraries listed in the Linux Build [Prerequisites](#prerequisites) further down the page, and it should build fine. We don't currently support any big-endian architectures.
The code itself supports (the latest stable) Linux and Windows. In terms of actual build support, for now we usually only distribute Windows binaries and sometimes Linux. For any other distro or OS, you just have to find the same libraries listed in the Linux Build [Prerequisites](#prerequisites) further down the page, and it should build fine. We don't currently support any big-endian architectures.
Recommended compilers: MSVC, GCC, CLANG.
Recommended compilers: MSVC, GCC, CLANG.
You can find precompiled binaries under [Releases](https://github.com/BeamMP/BeamMP-Server/releases/).
@@ -52,7 +53,7 @@ You can find precompiled binaries under [Releases](https://github.com/BeamMP/Bea
**__Do not compile from `master`. Always build from a release tag, i.e. `tags/v2.3.3`!__**
Currently only linux and windows are supported (generally). See [Releases](https://github.com/BeamMP/BeamMP-Server/releases/) for official binary releases. On systems to which we do not provide binaries (so anything but windows), you are allowed to compile the program and use it. Other restrictions, such as not being allowed to distribute those binaries, still apply (see [copyright notice](#copyright)).
Currently only Linux and Windows are supported (generally). See [Releases](https://github.com/BeamMP/BeamMP-Server/releases/) for official binary releases. On systems to which we do not provide binaries (so anything but windows), you are allowed to compile the program and use it. Other restrictions, such as not being allowed to distribute those binaries, still apply (see [copyright notice](#copyright)).
### Prerequisites
@@ -60,40 +61,24 @@ Currently only linux and windows are supported (generally). See [Releases](https
Please use the prepackaged binaries in [Releases](https://github.com/BeamMP/BeamMP-Server/releases/).
Dependencies for **windows** can be installed with `vcpkg`.
Dependencies for **windows** can be installed with [`vcpkg`](https://github.com/microsoft/vcpkg).
These are:
```
lua
zlib
rapidjson
openssl
websocketpp
curl
> vcpkg install lua:x64-windows-static zlib:x64-windows-static rapidjson:x64-windows-static openssl:x64-windows-static websocketpp:x64-windows-static curl:x64-windows-static
```
#### Linux
These package names are in the debian / ubuntu style. Feel free to PR your own guide for a different distro.
Runtime dependencies for **linux** are (debian/ubuntu):
```
libz-dev
rapidjson-dev
liblua5.3
libssl-dev
libwebsocketpp-dev
libcurl4-openssl-dev
Runtime dependencies for **linux**:
**Debian / Ubuntu**
```shell
$ sudo apt install libz-dev rapidjson-dev liblua5.3-dev libssl-dev libwebsocketpp-dev libcurl4-openssl-dev git make cmake g++
```
Build-time dependencies for **linux** are:
```
git
make
cmake
g++
```
For other distributions (e.g. Arch) you want to find packages for:
For other distributions (e.g. **Arch**) you want to find packages for:
- libz
- rapidjson
- lua5.3
@@ -102,22 +87,37 @@ For other distributions (e.g. Arch) you want to find packages for:
- curl (with ssl support)
- \+ the build time dependencies from above
#### macOS
Dependencies for **macOS** can be installed with homebrew.
```
brew install lua@5.3 rapidjson websocketpp cmake openssl@1.1
```
Some packages are included in **macOS,** but you might want to install homebrew versions.
```
brew install curl zlib git make
```
### How to build
On windows, use git-bash for these commands. On Linux, these should work in your shell.
On Windows, use git-bash for these commands. On Linux, these should work in your shell.
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`.
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).
2. Clone the repository in a location of your choice with `git clone -b v3.0.2-WithPython --recurse-submodules https://github.com/SantaSpeen/BeamMP-Server`.
3. Change into the BeamMP-Server directory by running `cd BeamMP-Server`.
4. Ensure that all submodules are initialized by running `git submodule update --init --recursive`. Then change into the cloned directory by running `cd BeamMP-Server`.
5. Run `cmake . -DCMAKE_BUILD_TYPE=Release` (with `.`)
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.
6. Run `make -j 4` (Set `-j {cores in your CPU}`. This param use for quickly compile.)
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, doesn't 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.
*tip: to run the server in the background, simply (in bash, zsh, etc) run:* `nohup ./BeamMP-Server &`*.*
## Support
The BeamMP project is supported by community donations via our [Patreon](https://www.patreon.com/BeamMP). This brings perks such as Patreon-only channels on our Discord, early access to new updates, and more server keys.
## Copyright
Copyright (c) 2019-present Anonymous275 (@Anonymous-275), Lion Kortlepel (@lionkor).
BeamMP-Server code is not in the public domain and is not free software. One must be granted explicit permission by the copyright holder(s) in order to modify or distribute any part of the source or binaries. Special permission to modify the source-code is implicitly granted only for the purpose of upstreaming those changes directly to github.com/BeamMP/BeamMP-Server via a GitHub pull-request.
Commercial usage is prohibited, unless explicit permission has been granted prior to usage.
Commercial usage is prohibited, unless explicit permission has been granted prior to usage.

2
deps/toml11 vendored

View File

@@ -92,7 +92,7 @@ private:
std::queue<std::string> mPacketsSync;
std::unordered_map<std::string, std::string> mIdentifiers;
bool mIsGuest = false;
std::mutex mVehicleDataMutex;
mutable std::mutex mVehicleDataMutex;
TSetOfVehicleData mVehicleData;
std::string mName = "Unknown Client";
SOCKET mSocket[2] { SOCKET(0), SOCKET(0) };

View File

@@ -7,6 +7,7 @@ extern TSentry Sentry;
#include <atomic>
#include <cstring>
#include <deque>
#include <filesystem>
#include <functional>
#include <memory>
#include <mutex>
@@ -43,7 +44,7 @@ public:
std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" };
std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" };
bool HTTPServerEnabled { false };
int MaxPlayers { 10 };
int MaxPlayers { 8 };
bool Private { true };
int MaxCars { 1 };
bool DebugModeEnabled { false };
@@ -52,7 +53,9 @@ public:
bool SendErrors { true };
bool SendErrorsMessageEnabled { true };
int HTTPServerPort { 8080 };
bool HTTPServerUseSSL { true };
std::string HTTPServerIP { "127.0.0.1" };
bool HTTPServerUseSSL { false };
bool HideUpdateMessages { false };
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
};
@@ -119,7 +122,7 @@ private:
static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 3, 0, 1 };
static inline Version mVersion { 3, 0, 2 };
};
std::string ThreadName(bool DebugModeOverride = false);

View File

@@ -4,36 +4,40 @@
#include <tuple>
namespace LuaAPI {
int PanicHandler(lua_State* State);
std::string LuaToString(const sol::object Value, size_t Indent = 1, bool QuoteStrings = false);
void Print(sol::variadic_args);
namespace MP {
extern TLuaEngine* Engine;
int PanicHandler(lua_State* State);
std::string LuaToString(sol::object Value, size_t Indent = 1, bool QuoteStrings = false);
void Print(sol::variadic_args);
namespace MP {
extern TLuaEngine* Engine;
std::string GetOSName();
std::tuple<int, int, int> GetServerVersion();
bool TriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data);
inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); }
void DropPlayer(int ID, std::optional<std::string> MaybeReason);
void SendChatMessage(int ID, const std::string& Message);
void RemoveVehicle(int PlayerID, int VehicleID);
void Set(int ConfigID, sol::object NewValue);
bool IsPlayerGuest(int ID);
bool IsPlayerConnected(int ID);
void Sleep(size_t Ms);
void PrintRaw(sol::variadic_args);
}
namespace FS {
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> Rename(const std::string& Path, const std::string& NewPath);
std::pair<bool, std::string> Copy(const std::string& Path, const std::string& NewPath);
std::string GetFilename(const std::string& Path);
std::string GetExtension(const std::string& Path);
std::string GetParentFolder(const std::string& Path);
bool Exists(const std::string& Path);
bool IsDirectory(const std::string& Path);
bool IsFile(const std::string& Path);
std::string ConcatPaths(sol::variadic_args Args);
}
std::string GetOSName();
std::tuple<int, int, int> GetServerVersion();
bool TriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data);
inline size_t GetPlayerCount() { return Engine->Server().ClientCount(); }
void DropPlayer(int ID, std::optional<std::string> MaybeReason);
void SendChatMessage(int ID, const std::string& Message);
void RemoveVehicle(int PlayerID, int VehicleID);
void Set(int ConfigID, sol::object NewValue);
bool IsPlayerGuest(int ID);
bool IsPlayerConnected(int ID);
void Sleep(size_t Ms);
void PrintRaw(sol::variadic_args);
}
namespace FS {
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> Rename(const std::string& Path, const std::string& NewPath);
std::pair<bool, std::string> Copy(const std::string& Path, const std::string& NewPath);
std::string GetFilename(const std::string& Path);
std::string GetExtension(const std::string& Path);
std::string GetParentFolder(const std::string& Path);
bool Exists(const std::string& Path);
bool IsDirectory(const std::string& Path);
bool IsFile(const std::string& Path);
std::string ConcatPaths(sol::variadic_args Args);
}
namespace CL {
void SendPacket(int ID, const std::string& Packet);
void SendNotify(int ID, const std::string& Notify);
}
}

13
include/PyAPI.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include "TPyEngine.h"
#include <tuple>
namespace PyAPI {
namespace MP {
}
namespace FS {
}
namespace CL {
}
}

View File

@@ -15,6 +15,7 @@ public:
void WriteRaw(const std::string& str);
void InitializeLuaConsole(TLuaEngine& Engine);
void BackupOldLog();
void StartLoggingToFile();
Commandline& Internal() { return mCommandline; }
private:
@@ -37,4 +38,6 @@ private:
bool mFirstTime { true };
std::string mStateId;
const std::string mDefaultStateId = "BEAMMP_SERVER_CONSOLE";
std::ofstream mLogFileStream;
std::mutex mLogFileStreamMtx;
};

View File

@@ -6,6 +6,7 @@
#include <condition_variable>
#include <filesystem>
#include <initializer_list>
#include <list>
#include <lua.hpp>
#include <memory>
#include <mutex>
@@ -72,6 +73,18 @@ private:
class TLuaEngine : IThreaded {
public:
enum CallStrategy : int {
BestEffort,
Precise,
};
struct QueuedFunction {
std::string FunctionName;
std::shared_ptr<TLuaResult> Result;
std::vector<TLuaArgTypes> Args;
std::string EventName; // optional, may be empty
};
TLuaEngine();
~TLuaEngine() noexcept {
beammp_debug("Lua Engine terminated");
@@ -145,7 +158,7 @@ public:
return Results; //
}
std::set<std::string> GetEventHandlersForState(const std::string& EventName, TLuaStateId StateId);
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS);
void CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy);
void CancelEventTimers(const std::string& EventName, TLuaStateId StateId);
sol::state_view GetStateForPlugin(const fs::path& PluginPath);
TLuaStateId GetStateIDForPlugin(const fs::path& PluginPath);
@@ -166,6 +179,7 @@ private:
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(const TLuaChunk& Script);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy);
void RegisterEvent(const std::string& EventName, const std::string& FunctionName);
void AddPath(const fs::path& Path); // to be added to path and cpath
void operator()() override;
@@ -188,7 +202,7 @@ private:
std::thread mThread;
std::queue<std::pair<TLuaChunk, std::shared_ptr<TLuaResult>>> mStateExecuteQueue;
std::recursive_mutex mStateExecuteQueueMutex;
std::queue<std::tuple<std::string, std::shared_ptr<TLuaResult>, std::vector<TLuaArgTypes>>> mStateFunctionQueue;
std::vector<QueuedFunction> mStateFunctionQueue;
std::mutex mStateFunctionQueueMutex;
std::condition_variable mStateFunctionQueueCond;
TLuaEngine* mEngine;
@@ -202,6 +216,7 @@ private:
std::chrono::high_resolution_clock::time_point LastCompletion {};
std::string EventName;
TLuaStateId StateId;
CallStrategy Strategy;
bool Expired();
void Reset();
};
@@ -218,8 +233,9 @@ private:
std::recursive_mutex mLuaEventsMutex;
std::vector<TimedEvent> mTimedEvents;
std::recursive_mutex mTimedEventsMutex;
std::queue<std::shared_ptr<TLuaResult>> mResultsToCheck;
std::recursive_mutex mResultsToCheckMutex;
std::list<std::shared_ptr<TLuaResult>> mResultsToCheck;
std::mutex mResultsToCheckMutex;
std::condition_variable mResultsToCheckCond;
};
// std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);

3
include/TPyEngine.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
class TPyPlugin;

1
include/TPyPlugin.h Normal file
View File

@@ -0,0 +1 @@
#pragma once

View File

@@ -27,6 +27,7 @@ void TClient::ClearCars() {
int TClient::GetOpenCarID() const {
int OpenID = 0;
bool found;
std::unique_lock lock(mVehicleDataMutex);
do {
found = true;
for (auto& v : mVehicleData) {

View File

@@ -97,6 +97,7 @@ void Application::SetSubsystemStatus(const std::string& Subsystem, Status status
void Application::CheckForUpdates() {
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Starting);
static bool FirstTime = true;
// checks current version against latest version
std::regex VersionRegex { R"(\d+\.\d+\.\d+\n*)" };
for (const auto& url : GetBackendUrlsInOrder()) {
@@ -107,24 +108,31 @@ void Application::CheckForUpdates() {
auto RemoteVersion = Version(VersionStrToInts(Response));
if (IsOutdated(MyVersion, RemoteVersion)) {
std::string RealVersionString = RemoteVersion.AsString();
beammp_warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION OUT! There's a new version (v" + RealVersionString + ") of the BeamMP-Server available! For more info visit https://wiki.beammp.com/en/home/server-maintenance#updating-the-server." + std::string(ANSI_RESET));
beammp_warn(std::string(ANSI_YELLOW_BOLD) + "NEW VERSION IS OUT! Please update to the new version (v" + RealVersionString + ") of the BeamMP-Server! Download it here: https://beammp.com/! For a guide on how to update, visit: https://wiki.beammp.com/en/home/server-maintenance#updating-the-server" + std::string(ANSI_RESET));
} else {
beammp_info("Server up-to-date!");
if (FirstTime) {
beammp_info("Server up-to-date!");
}
}
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Good);
break;
} else {
beammp_debug("Failed to fetch version from: " + url);
beammp_trace("got " + Response);
auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("get-response", { { "response", Response } });
Sentry.LogError("failed to get server version", _file_basename, _line);
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Bad);
if (FirstTime) {
beammp_debug("Failed to fetch version from: " + url);
beammp_trace("got " + Response);
auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("get-response", { { "response", Response } });
Sentry.LogError("failed to get server version", _file_basename, _line);
Application::SetSubsystemStatus("UpdateCheck", Application::Status::Bad);
}
}
}
if (Application::GetSubsystemStatuses().at("UpdateCheck") == Application::Status::Bad) {
beammp_warn("Unable to fetch version info from backend.");
if (FirstTime) {
beammp_warn("Unable to fetch version info from backend.");
}
}
FirstTime = false;
}
// thread name stuff

View File

@@ -293,7 +293,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
mThread.detach();
}
void Http::Server::THttpServerInstance::operator()() {
void Http::Server::THttpServerInstance::operator()() try {
beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
std::unique_ptr<httplib::Server> HttpLibServerInstance;
if (Application::Settings.HTTPServerUseSSL) {
@@ -370,6 +370,14 @@ void Http::Server::THttpServerInstance::operator()() {
HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
res.set_content(std::string(Magic), "text/plain");
});
HttpLibServerInstance->set_logger([](const httplib::Request& Req, const httplib::Response& Res) {
beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status));
});
Application::SetSubsystemStatus("HTTPServer", Application::Status::Good);
HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort);
auto ret = HttpLibServerInstance->listen(Application::Settings.HTTPServerIP.c_str(), Application::Settings.HTTPServerPort);
if (!ret) {
beammp_error("Failed to start http server (failed to listen). Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it.");
}
} catch (const std::exception& e) {
beammp_error("Failed to start http server. Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it. Error: " + std::string(e.what()));
}

View File

@@ -112,7 +112,8 @@ bool LuaAPI::MP::TriggerClientEvent(int PlayerID, const std::string& EventName,
}
auto c = MaybeClient.value().lock();
if (!Engine->Network().Respond(*c, Packet, true)) {
beammp_lua_error("Respond failed");
beammp_lua_error("Respond failed, dropping client " + std::to_string(PlayerID));
Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets");
return false;
}
}
@@ -351,3 +352,24 @@ std::string LuaAPI::FS::ConcatPaths(sol::variadic_args Args) {
auto Result = Path.lexically_normal().string();
return Result;
}
void LuaAPI::CL::SendPacket(int ID, const std::string& Packet) {
if (ID == -1) {
LuaAPI::MP::Engine->Network().SendToAll(nullptr, Packet, true, true);
} else {
auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
auto c = MaybeClient.value().lock();
if (!c->IsSynced())
return;
beammp_info("[LUA] Send packet to <" + c->GetName() + ">: " + Packet);
LuaAPI::MP::Engine->Network().Respond(*c, Packet, true);
} else {
beammp_lua_error("SendChatMessage invalid argument [1] invalid ID");
}
}
}
void LuaAPI::CL::SendNotify(int ID, const std::string& Notify) {
SendPacket(ID, "L:Server: " + Notify);
}

1
src/PyAPI.cpp Normal file
View File

@@ -0,0 +1 @@
#include "PyAPI.h"

View File

@@ -17,15 +17,19 @@ static constexpr std::string_view StrName = "Name";
static constexpr std::string_view StrDescription = "Description";
static constexpr std::string_view StrResourceFolder = "ResourceFolder";
static constexpr std::string_view StrAuthKey = "AuthKey";
// Misc
static constexpr std::string_view StrSendErrors = "SendErrors";
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
static constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
static constexpr std::string_view StrHTTPServerUseSSL = "UseSSL";
static constexpr std::string_view StrHideUpdateMessages = "ImScaredOfUpdates";
// HTTP
static constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
static constexpr std::string_view StrHTTPServerUseSSL = "UseSSL";
static constexpr std::string_view StrSSLKeyPath = "SSLKeyPath";
static constexpr std::string_view StrSSLCertPath = "SSLCertPath";
static constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
static constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
TConfig::TConfig(const std::string& ConfigFileName)
: mConfigFileName(ConfigFileName) {
@@ -58,8 +62,8 @@ void SetComment(CommentsT& Comments, const std::string& Comment) {
* whether it is in TConfig.cpp or the configuration file.
*/
void TConfig::FlushToFile() {
auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
data["General"] = toml::table();
// auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
auto data = toml::value {};
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");
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
@@ -71,19 +75,39 @@ void TConfig::FlushToFile() {
data["General"][StrMap.data()] = Application::Settings.MapName;
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
data["General"][StrSendErrors.data()] = Application::Settings.SendErrors;
SetComment(data["General"][StrSendErrors.data()].comments(), " You can turn on/off the SendErrors message you get on startup here");
data["General"][StrSendErrorsMessageEnabled.data()] = Application::Settings.SendErrorsMessageEnabled;
SetComment(data["General"][StrSendErrorsMessageEnabled.data()].comments(), " If SendErrors is `true`, the server will send helpful info about crashes and other issues back to the BeamMP developers. This info may include your config, who is on your server at the time of the error, and similar general information. This kind of data is vital in helping us diagnose and fix issues faster. This has no impact on server performance. You can opt-out of this system by setting this to `false`");
// Misc
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.");
data["Misc"][StrSendErrors.data()] = Application::Settings.SendErrors;
SetComment(data["Misc"][StrSendErrors.data()].comments(), " You can turn on/off the SendErrors message you get on startup here");
data["Misc"][StrSendErrorsMessageEnabled.data()] = Application::Settings.SendErrorsMessageEnabled;
SetComment(data["Misc"][StrSendErrorsMessageEnabled.data()].comments(), " If SendErrors is `true`, the server will send helpful info about crashes and other issues back to the BeamMP developers. This info may include your config, who is on your server at the time of the error, and similar general information. This kind of data is vital in helping us diagnose and fix issues faster. This has no impact on server performance. You can opt-out of this system by setting this to `false`");
// HTTP
data["HTTP"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath;
data["HTTP"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath;
data["HTTP"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort;
SetComment(data["HTTP"][StrHTTPServerIP.data()].comments(), " Which IP to listen on. Pick 0.0.0.0 for a public-facing server with no specific IP, and 127.0.0.1 or 'localhost' for a local server.");
data["HTTP"][StrHTTPServerIP.data()] = Application::Settings.HTTPServerIP;
data["HTTP"][StrHTTPServerUseSSL.data()] = Application::Settings.HTTPServerUseSSL;
SetComment(data["HTTP"][StrHTTPServerUseSSL.data()].comments(), " Recommended to keep enabled. 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;
SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server");
std::ofstream Stream(mConfigFileName);
Stream << data << std::flush;
std::stringstream Ss;
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) {
@@ -98,32 +122,7 @@ void TConfig::CreateConfigFile(std::string_view name) {
beammp_error("an error occurred and was ignored during config transfer: " + std::string(e.what()));
}
{ // create file context
std::ofstream ofs(name.data());
}
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) {
@@ -158,12 +157,15 @@ void TConfig::ParseFromFile(std::string_view name) {
TryReadValue(data, "General", StrDescription, Application::Settings.ServerDesc);
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
TryReadValue(data, "General", StrSendErrors, Application::Settings.SendErrors);
TryReadValue(data, "General", StrSendErrorsMessageEnabled, Application::Settings.SendErrorsMessageEnabled);
// Misc
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, Application::Settings.SendErrorsMessageEnabled);
// HTTP
TryReadValue(data, "HTTP", StrSSLKeyPath, Application::Settings.SSLKeyPath);
TryReadValue(data, "HTTP", StrSSLCertPath, Application::Settings.SSLCertPath);
TryReadValue(data, "HTTP", StrHTTPServerPort, Application::Settings.HTTPServerPort);
TryReadValue(data, "HTTP", StrHTTPServerIP, Application::Settings.HTTPServerIP);
TryReadValue(data, "HTTP", StrHTTPServerEnabled, Application::Settings.HTTPServerEnabled);
TryReadValue(data, "HTTP", StrHTTPServerUseSSL, Application::Settings.HTTPServerUseSSL);
} catch (const std::exception& err) {
@@ -202,6 +204,7 @@ void TConfig::PrintDebug() {
beammp_debug(std::string(StrSSLKeyPath) + ": \"" + Application::Settings.SSLKeyPath + "\"");
beammp_debug(std::string(StrSSLCertPath) + ": \"" + Application::Settings.SSLCertPath + "\"");
beammp_debug(std::string(StrHTTPServerPort) + ": \"" + std::to_string(Application::Settings.HTTPServerPort) + "\"");
beammp_debug(std::string(StrHTTPServerIP) + ": \"" + Application::Settings.HTTPServerIP + "\"");
// special!
beammp_debug("Key Length: " + std::to_string(Application::Settings.Key.length()) + "");
}

View File

@@ -50,7 +50,7 @@ std::string GetDate() {
}
void TConsole::BackupOldLog() {
fs::path Path = "Server.log";
fs::path Path = "server.log";
if (fs::exists(Path)) {
auto OldLog = Path.filename().stem().string() + ".old.log";
try {
@@ -97,6 +97,17 @@ void TConsole::BackupOldLog() {
}
}
void TConsole::StartLoggingToFile() {
mLogFileStream.open("server.log");
Application::Console().Internal().on_write = [this](const std::string& ToWrite) {
// TODO: Sanitize by removing all ansi escape codes (vt100)
std::unique_lock Lock(mLogFileStreamMtx);
mLogFileStream.write(ToWrite.c_str(), ToWrite.size());
mLogFileStream.write("\n", 1);
mLogFileStream.flush();
};
}
void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
if (!mIsLuaConsole) {
if (!mLuaEngine) {

View File

@@ -19,7 +19,9 @@ void THeartbeatThread::operator()() {
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
bool isAuth = false;
size_t UpdateReminderCounter = 0;
while (!mShutdown) {
++UpdateReminderCounter;
Body = GenerateCall();
// a hot-change occurs when a setting has changed, to update the backend of that change.
auto Now = std::chrono::high_resolution_clock::now();
@@ -62,8 +64,8 @@ void THeartbeatThread::operator()() {
beammp_trace(T);
Doc.Parse(T.data(), T.size());
if (Doc.HasParseError() || !Doc.IsObject()) {
beammp_error("Backend response failed to parse as valid json");
beammp_debug("Response was: `" + T + "`");
beammp_debug("Failed to contact backend at " + Url + " (this is not an error).");
beammp_trace("Response was: " + T);
Sentry.SetContext("JSON Response", { { "reponse", T } });
SentryReportError(Url + Target, ResponseCode);
} else if (ResponseCode != 200) {
@@ -105,6 +107,10 @@ void THeartbeatThread::operator()() {
beammp_error("Missing/invalid json members in backend response");
Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__));
}
} else {
if (!Application::Settings.Private) {
beammp_warn("Backend failed to respond to a heartbeat. Your server may temporarily disappear from the server list. This is not an error, and will likely resolve itself soon. Direct connect will still work.");
}
}
if (Ok && !isAuth) {
@@ -124,6 +130,9 @@ void THeartbeatThread::operator()() {
if (isAuth) {
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
}
if (!Application::Settings.HideUpdateMessages && UpdateReminderCounter % 5) {
Application::CheckForUpdates();
}
}
}

View File

@@ -15,13 +15,13 @@
TLuaEngine* LuaAPI::MP::Engine;
TLuaEngine::TLuaEngine()
: mPluginMonitor(fs::path(Application::Settings.Resource) / "Server", *this, mShutdown) {
: mPluginMonitor(fs::path(Application::Settings.Resource) / "server", *this, mShutdown) {
Application::SetSubsystemStatus("LuaEngine", Application::Status::Starting);
LuaAPI::MP::Engine = this;
if (!fs::exists(Application::Settings.Resource)) {
fs::create_directory(Application::Settings.Resource);
}
fs::path Path = fs::path(Application::Settings.Resource) / "Server";
fs::path Path = fs::path(Application::Settings.Resource) / "server";
if (!fs::exists(Path)) {
fs::create_directory(Path);
}
@@ -44,7 +44,7 @@ void TLuaEngine::operator()() {
CollectAndInitPlugins();
// now call all onInit's
auto Futures = TriggerEvent("onInit", "");
WaitForAll(Futures);
WaitForAll(Futures, std::chrono::seconds(5));
for (const auto& Future : Futures) {
if (Future->Error && Future->ErrorMessage != BeamMPFnNotFoundError) {
beammp_lua_error("Calling \"onInit\" on \"" + Future->StateId + "\" failed: " + Future->ErrorMessage);
@@ -54,25 +54,21 @@ void TLuaEngine::operator()() {
auto ResultCheckThread = std::thread([&] {
RegisterThread("ResultCheckThread");
while (!mShutdown) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
std::unique_lock Lock(mResultsToCheckMutex);
mResultsToCheckCond.wait_for(Lock, std::chrono::milliseconds(20));
if (!mResultsToCheck.empty()) {
auto Res = mResultsToCheck.front();
mResultsToCheck.pop();
Lock.unlock();
if (!Res->Ready) {
Lock.lock();
mResultsToCheck.push(Res);
Lock.unlock();
}
if (Res->Error) {
if (Res->ErrorMessage != BeamMPFnNotFoundError) {
beammp_lua_error(Res->Function + ": " + Res->ErrorMessage);
mResultsToCheck.remove_if([](const std::shared_ptr<TLuaResult>& Ptr) -> bool {
if (Ptr->Ready) {
return true;
} else if (Ptr->Error) {
if (Ptr->ErrorMessage != BeamMPFnNotFoundError) {
beammp_lua_error(Ptr->Function + ": " + Ptr->ErrorMessage);
}
return true;
}
}
return false;
});
}
std::this_thread::yield();
}
});
// event loop
@@ -85,21 +81,31 @@ void TLuaEngine::operator()() {
std::unique_lock Lock(mTimedEventsMutex);
for (auto& Timer : mTimedEvents) {
if (Timer.Expired()) {
auto LastCompletionBeforeReset = Timer.LastCompletion;
Timer.Reset();
auto Handlers = GetEventHandlersForState(Timer.EventName, Timer.StateId);
std::unique_lock StateLock(mLuaStatesMutex);
std::unique_lock Lock2(mResultsToCheckMutex);
for (auto& Handler : Handlers) {
auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCall(Handler, {});
mResultsToCheck.push(Res);
auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCallFromCustomEvent(Handler, {}, Timer.EventName, Timer.Strategy);
if (Res) {
mResultsToCheck.push_back(Res);
mResultsToCheckCond.notify_one();
} else {
// "revert" reset
Timer.LastCompletion = LastCompletionBeforeReset;
// beammp_trace("Reverted reset of \"" + Timer.EventName + "\" timer");
// no need to try to enqueue more handlers for this event (they will all fail)
break;
}
}
}
}
}
std::chrono::high_resolution_clock::duration Diff;
if ((Diff = std::chrono::high_resolution_clock::now() - Before)
< std::chrono::milliseconds(10)) {
std::this_thread::sleep_for(Diff);
const auto Expected = std::chrono::milliseconds(10);
if (auto Diff = std::chrono::high_resolution_clock::now() - Before;
Diff < Expected) {
std::this_thread::sleep_for(Expected - Diff);
} else {
beammp_trace("Event loop cannot keep up! Running " + std::to_string(Diff.count()) + "s behind");
}
@@ -144,7 +150,8 @@ TLuaStateId TLuaEngine::GetStateIDForPlugin(const fs::path& PluginPath) {
void TLuaEngine::AddResultToCheck(const std::shared_ptr<TLuaResult>& Result) {
std::unique_lock Lock(mResultsToCheckMutex);
mResultsToCheck.push(Result);
mResultsToCheck.push_back(Result);
mResultsToCheckCond.notify_one();
}
void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, const std::optional<std::chrono::high_resolution_clock::duration>& Max) {
@@ -155,8 +162,10 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, c
std::this_thread::sleep_for(std::chrono::milliseconds(10));
ms += 10;
if (Max.has_value() && std::chrono::milliseconds(ms) > Max.value()) {
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;
} else if (ms > 1000 * 60) {
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) {
@@ -174,7 +183,8 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results, c
void TLuaEngine::ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results) {
std::unique_lock Lock2(mResultsToCheckMutex);
for (const auto& Result : Results) {
mResultsToCheck.push(Result);
mResultsToCheck.push_back(Result);
mResultsToCheckCond.notify_one();
}
}
@@ -502,11 +512,27 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
return Lua_GetPlayerIdentifiers(ID);
});
MPTable.set_function("Sleep", &LuaAPI::MP::Sleep);
MPTable.set_function("CreateEventTimer", [&](const std::string& EventName, size_t IntervalMS) {
// const std::string& EventName, size_t IntervalMS, int strategy
MPTable.set_function("CreateEventTimer", [&](sol::variadic_args Args) {
if (Args.size() < 2 || Args.size() > 3) {
beammp_lua_error("CreateEventTimer expects 2 or 3 arguments.");
}
if (Args.get_type(0) != sol::type::string) {
beammp_lua_error("CreateEventTimer expects 1st argument to be a string");
}
if (Args.get_type(1) != sol::type::number) {
beammp_lua_error("CreateEventTimer expects 2nd argument to be a number");
}
if (Args.size() == 3 && Args.get_type(2) != sol::type::number) {
beammp_lua_error("CreateEventTimer expects 3rd argument to be a number (MP.CallStrategy)");
}
auto EventName = Args.get<std::string>(0);
auto IntervalMS = Args.get<size_t>(1);
CallStrategy Strategy = Args.size() > 2 ? Args.get<CallStrategy>(2) : CallStrategy::BestEffort;
if (IntervalMS < 25) {
beammp_warn("Timer for \"" + EventName + "\" on \"" + mStateId + "\" is set to trigger at <25ms, which is likely too fast and won't cancel properly.");
}
mEngine->CreateEventTimer(EventName, mStateId, IntervalMS);
mEngine->CreateEventTimer(EventName, mStateId, IntervalMS, Strategy);
});
MPTable.set_function("CancelEventTimer", [&](const std::string& EventName) {
mEngine->CancelEventTimers(EventName, mStateId);
@@ -526,6 +552,10 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
"Name", 5,
"Description", 6);
MPTable.create_named("CallStrategy",
"BestEffort", CallStrategy::BestEffort,
"Precise", CallStrategy::Precise);
auto FSTable = StateView.create_named_table("FS");
FSTable.set_function("CreateDirectory", &LuaAPI::FS::CreateDirectory);
FSTable.set_function("Exists", &LuaAPI::FS::Exists);
@@ -538,6 +568,11 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, std::atomi
FSTable.set_function("IsDirectory", &LuaAPI::FS::IsDirectory);
FSTable.set_function("IsFile", &LuaAPI::FS::IsFile);
FSTable.set_function("ConcatPaths", &LuaAPI::FS::ConcatPaths);
auto CLTable = StateView.create_named_table("CL");
CLTable.set_function("SendPacket", &LuaAPI::CL::SendPacket);
CLTable.set_function("SendNotify", &LuaAPI::CL::SendNotify);
Start();
}
@@ -548,12 +583,34 @@ std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueScript(const TLu
return Result;
}
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args, const std::string& EventName, CallStrategy Strategy) {
// TODO: Document all this
decltype(mStateFunctionQueue)::iterator Iter = mStateFunctionQueue.end();
if (Strategy == CallStrategy::BestEffort) {
Iter = std::find_if(mStateFunctionQueue.begin(), mStateFunctionQueue.end(),
[&EventName](const QueuedFunction& Element) {
return Element.EventName == EventName;
});
}
if (Iter == mStateFunctionQueue.end()) {
auto Result = std::make_shared<TLuaResult>();
Result->StateId = mStateId;
Result->Function = FunctionName;
std::unique_lock Lock(mStateFunctionQueueMutex);
mStateFunctionQueue.push_back({ FunctionName, Result, Args, EventName });
mStateFunctionQueueCond.notify_all();
return Result;
} else {
return nullptr;
}
}
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args) {
auto Result = std::make_shared<TLuaResult>();
Result->StateId = mStateId;
Result->Function = FunctionName;
std::unique_lock Lock(mStateFunctionQueueMutex);
mStateFunctionQueue.push({ FunctionName, Result, Args });
mStateFunctionQueue.push_back({ FunctionName, Result, Args, "" });
mStateFunctionQueueCond.notify_all();
return Result;
}
@@ -616,12 +673,13 @@ void TLuaEngine::StateThreadData::operator()() {
std::chrono::milliseconds(500),
[&]() -> bool { return !mStateFunctionQueue.empty(); });
if (NotExpired) {
auto FnNameResultPair = std::move(mStateFunctionQueue.front());
mStateFunctionQueue.pop();
auto TheQueuedFunction = std::move(mStateFunctionQueue.front());
mStateFunctionQueue.erase(mStateFunctionQueue.begin());
Lock.unlock();
auto& FnName = std::get<0>(FnNameResultPair);
auto& Result = std::get<1>(FnNameResultPair);
auto Args = std::get<2>(FnNameResultPair);
auto& FnName = TheQueuedFunction.FunctionName;
auto& Result = TheQueuedFunction.Result;
auto Args = TheQueuedFunction.Args;
// TODO: Use TheQueuedFunction.EventName for errors, warnings, etc
Result->StateId = mStateId;
sol::state_view StateView(mState);
auto Fn = StateView[FnName];
@@ -669,13 +727,14 @@ void TLuaEngine::StateThreadData::operator()() {
}
}
void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS) {
void TLuaEngine::CreateEventTimer(const std::string& EventName, TLuaStateId StateId, size_t IntervalMS, CallStrategy Strategy) {
std::unique_lock Lock(mTimedEventsMutex);
TimedEvent Event {
std::chrono::high_resolution_clock::duration { std::chrono::milliseconds(IntervalMS) },
std::chrono::high_resolution_clock::now(),
EventName,
StateId
StateId,
Strategy
};
mTimedEvents.push_back(std::move(Event));
beammp_trace("created event timer for \"" + EventName + "\" on \"" + StateId + "\" with " + std::to_string(IntervalMS) + "ms interval");

View File

@@ -154,6 +154,19 @@ void TNetwork::TCPServerMain() {
beammp_warn(("Got an invalid client socket on connect! Skipping..."));
continue;
}
// set timeout (DWORD, aka uint32_t)
uint32_t SendTimeoutMS = 30 * 1000;
#if defined(BEAMMP_WINDOWS)
int ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&SendTimeoutMS), sizeof(SendTimeoutMS));
#else // POSIX
struct timeval optval;
optval.tv_sec = (int)(SendTimeoutMS / 1000);
optval.tv_usec = (SendTimeoutMS % 1000) * 1000;
int ret = ::setsockopt(client.Socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<void*>(&optval), sizeof(optval));
#endif
if (ret < 0) {
throw std::runtime_error("setsockopt recv timeout: " + GetPlatformAgnosticErrorString());
}
std::thread ID(&TNetwork::Identify, this, client);
ID.detach(); // TODO: Add to a queue and attempt to join periodically
} catch (const std::exception& e) {
@@ -514,6 +527,7 @@ void TNetwork::Looper(const std::weak_ptr<TClient>& c) {
}
}
}
void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
// TODO: the c.expired() might cause issues here, remove if you end up here with your debugger
if (c.expired() || c.lock()->GetTCPSock() == -1) {
@@ -742,11 +756,11 @@ void TNetwork::SendFile(TClient& c, const std::string& UnsafeName) {
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);
uint32_t Split = 0x7735940; // 125MB
char* Data;
std::vector<char> Data;
if (Size > Split)
Data = new char[Split];
Data.resize(Split);
else
Data = new char[Size];
Data.resize(Size);
SOCKET TCPSock;
if (D)
TCPSock = c.GetDownSock();
@@ -757,8 +771,8 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
size_t Diff = Size - Sent;
if (Diff > Split) {
f.seekg(Sent, std::ios_base::beg);
f.read(Data, Split);
if (!TCPSendRaw(c, TCPSock, Data, Split)) {
f.read(Data.data(), Split);
if (!TCPSendRaw(c, TCPSock, Data.data(), Split)) {
if (c.GetStatus() > -1)
c.SetStatus(-1);
break;
@@ -766,8 +780,8 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
Sent += Split;
} else {
f.seekg(Sent, std::ios_base::beg);
f.read(Data, Diff);
if (!TCPSendRaw(c, TCPSock, Data, int32_t(Diff))) {
f.read(Data.data(), Diff);
if (!TCPSendRaw(c, TCPSock, Data.data(), int32_t(Diff))) {
if (c.GetStatus() > -1)
c.SetStatus(-1);
break;
@@ -775,8 +789,6 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
Sent += Diff;
}
}
delete[] Data;
f.close();
}
bool TNetwork::TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size) {

1
src/TPyEngine.cpp Normal file
View File

@@ -0,0 +1 @@
#include "TPyEngine.h"

1
src/TPyPlugin.cpp Normal file
View File

@@ -0,0 +1 @@
#include "TPyPlugin.h"

View File

@@ -36,6 +36,8 @@ ARGUMENTS:
including the path given in --config.
--version
Prints version info and exits.
--locale
Set console locale.
EXAMPLES:
BeamMP-Server --config=../MyWestCoastServerConfig.toml
@@ -55,17 +57,17 @@ struct MainArguments {
std::string InvokedAs;
};
int BeamMPServerMain(MainArguments Arguments);
int BeamMPServerMain(const MainArguments& Arguments);
int main(int argc, char** argv) {
MainArguments Args { argc, argv, {}, argv[0] };
Args.List.reserve(argc);
for (int i = 1; i < argc; ++i) {
Args.List.push_back(argv[i]);
Args.List.emplace_back(argv[i]);
}
int MainRet = 0;
int MainRet;
try {
MainRet = BeamMPServerMain(std::move(Args));
MainRet = BeamMPServerMain(Args);
} catch (const std::exception& e) {
beammp_error("A fatal exception has occurred and the server is forcefully shutting down.");
beammp_error(e.what());
@@ -75,7 +77,7 @@ int main(int argc, char** argv) {
return MainRet;
}
int BeamMPServerMain(MainArguments Arguments) {
int BeamMPServerMain(const MainArguments& Arguments) {
setlocale(LC_ALL, "C");
Application::InitializeConsole();
ArgsParser Parser;
@@ -83,6 +85,7 @@ int BeamMPServerMain(MainArguments Arguments) {
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
Parser.RegisterArgument({ "config" }, ArgsParser::HAS_VALUE);
Parser.RegisterArgument({ "working-directory" }, ArgsParser::HAS_VALUE);
Parser.RegisterArgument({ "locale" }, ArgsParser::HAS_VALUE);
Parser.Parse(Arguments.List);
if (!Parser.Verify()) {
return 1;
@@ -98,7 +101,7 @@ int BeamMPServerMain(MainArguments Arguments) {
return 0;
}
std::string ConfigPath = "ServerConfig.toml";
std::string ConfigPath = "config.toml";
if (Parser.FoundArgument({ "config" })) {
auto MaybeConfigPath = Parser.GetValueOfArgument({ "config" });
if (MaybeConfigPath.has_value()) {
@@ -118,22 +121,37 @@ int BeamMPServerMain(MainArguments Arguments) {
}
}
Application::SetSubsystemStatus("Main", Application::Status::Starting);
bool Success = Application::Console().Internal().enable_write_to_file("Server.log");
if (!Success) {
beammp_error("unable to open file for writing: \"Server.log\"");
if (Parser.FoundArgument({ "locale" })) {
auto MaybeLocale = Parser.GetValueOfArgument({ "locale" });
if (MaybeLocale.has_value()) {
std::string locale = MaybeLocale.value();
setlocale(LC_ALL, locale.c_str());
beammp_info("Custom locale requested via commandline arguments: '" + locale + "'");
#if WIN32
if (locale == "Russian" || locale == "Rus") {
beammp_info("Found Russian locale. Set CP1251 console coding.");
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
}
#endif
}
}
Application::SetSubsystemStatus("Main", Application::Status::Starting);
Application::Console().StartLoggingToFile();
SetupSignalHandlers();
bool Shutdown = false;
Application::RegisterShutdownHandler([&Shutdown] {
beammp_info("If this takes too long, you can press Ctrl+C repeatedly to force a shutdown.");
Application::SetSubsystemStatus("Main", Application::Status::ShuttingDown);
Shutdown = true;
});
Application::RegisterShutdownHandler([] {
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onShutdown", "");
TLuaEngine::WaitForAll(Futures);
TLuaEngine::WaitForAll(Futures, std::chrono::seconds(5));
});
TServer Server(Arguments.List);
@@ -172,6 +190,10 @@ int BeamMPServerMain(MainArguments Arguments) {
Application::SetSubsystemStatus("Main", Application::Status::Good);
RegisterThread("Main(Waiting)");
std::set<std::string> IgnoreSubsystems {
"UpdateCheck" // Ignore as not to confuse users (non-vital system)
};
bool FullyStarted = false;
while (!Shutdown) {
if (!FullyStarted) {
@@ -180,6 +202,9 @@ int BeamMPServerMain(MainArguments Arguments) {
std::string SystemsBadList {};
auto Statuses = Application::GetSubsystemStatuses();
for (const auto& NameStatusPair : Statuses) {
if (IgnoreSubsystems.count(NameStatusPair.first) > 0) {
continue; // ignore
}
if (NameStatusPair.second == Application::Status::Starting) {
FullyStarted = false;
} else if (NameStatusPair.second == Application::Status::Bad) {