Compare commits

...

161 Commits

Author SHA1 Message Date
Anonymous-275
8c967a5608 test state commit 2023-06-23 08:09:56 +01:00
Anonymous-275
57a58b1a38 fix hang and crash caused by client kick 2023-04-24 11:41:09 +01:00
Lion Kortlepel
2cf27ad837 potentially fix ghost car bug 2022-12-20 02:56:04 +01:00
Lion Kortlepel
e8ef7de4b9 fix a bug in TriggerLocalEvent which would cause the server to std::terminate()
it was refusing to push multiple return values onto a result table. Not
sure what changed to make this happen, but oh well.
2022-12-19 12:41:45 +01:00
Lion Kortlepel
8ea7b75874 fix sol assert not actually checking the assertion *facepalm* 2022-12-19 12:41:16 +01:00
Lion Kortlepel
59503ccc90 use a beammp_lua_errorf instead of a std::terminate on sol2 assertion failure 2022-12-19 11:52:22 +01:00
Lion Kortlepel
7316dff519 fix not removing client when disconnecting them 2022-11-25 14:40:21 +01:00
Lion Kortlepel
b1a89125fe fix spinning a single thread in lua result check thread 2022-11-25 14:21:45 +01:00
Lion Kortlepel
4a6504ec2c fix "disconnect was called twice" error
this would cause players to stick around after being kicked
2022-11-23 09:34:51 +01:00
Lion Kortlepel
b3594dd147 remove invalid naming of settings again
ive added this in what can only be described as complete delirium, as it
makes no sense whatsoever. reverted.
2022-11-23 09:34:51 +01:00
Lion Kortlepel
7cc426cb96 fix reading and defaulting the IncludeSubdirectories config option 2022-11-23 09:34:51 +01:00
20dka
a91eb02f5f remove debug print 2022-11-23 09:34:51 +01:00
20dka
6b65907a7f add ID parameter to onPlayerRequestMods and update Changelog.md 2022-11-23 09:34:51 +01:00
20dka
468a6b340e add event 'onPlayerRequestMods', restructure modloading
now every player has their own list of allowed client mods, this can be modified by lua upon joining and is later used as a whitelist to ensure only those files can be sent to each client
2022-11-23 09:34:51 +01:00
20dka
056827546e assign IDs to new players earlier in the connection process 2022-11-23 09:34:51 +01:00
20dka
6a47521c7c fix lua autocomplete crash when a table contains number keys 2022-11-23 09:34:51 +01:00
Lion Kortlepel
eaab5eaf30 fix crash which could happen when closing an invalid socket
for some reason boost::asio doesn't account for this and lets us simply
die when/if this happens.
2022-11-23 09:34:51 +01:00
Lion Kortlepel
35b38f35bb replace usages of ForEachClientWeak with ForEachClient 2022-11-23 09:34:51 +01:00
Lion Kortlepel
2572530957 ensure clients are valid when iterated over 2022-11-23 09:34:51 +01:00
Lion Kortlepel
8551e68184 fix bug which could cause multiple players to have the same ID (closes #154) 2022-11-23 09:34:51 +01:00
Lion Kortlepel
2f85c708c5 rework GetClient to use new ForEachClient, return shared_ptr
this simplifies a lot of functions, and removes a lot of potential
errors when using the function. You now only check whether the return
value is null, and if it's not, you have a valid, reference-counted,
client pointer.
2022-11-23 09:34:51 +01:00
Lion Kortlepel
844b64f5d9 rework ForEachClient to use concepts instead of SFINAE 2022-11-23 09:34:51 +01:00
Lion Kortlepel
d7369c3bc5 deprecate ForEachClientWeak in favor of new ForEachClient 2022-11-23 09:34:51 +01:00
Lion Kortlepel
a8ad9034b2 update lk-result 2022-11-23 09:34:50 +01:00
Lion Kortlepel
b756ce3c3f fix formatting in all files 2022-11-23 09:34:50 +01:00
Lion Kortlepel
3f05b72cc2 change internal setting names to set apart categories 2022-11-23 09:34:50 +01:00
Lion Kortlepel
152393f8bf switch boost variant for std::variant for settings, for sol2 2022-11-23 09:34:50 +01:00
Lion Kortlepel
dd36299436 add lk/result to includes 2022-11-23 09:34:50 +01:00
Lion Kortlepel
da65e97ed1 add lk/result
a header-only library to deal with results
2022-11-23 09:34:50 +01:00
Lion Kortlepel
4cc163ea1c switch to c++20
this will allow us to use more convenience features of the language, and
leave more legacy code behind - also we can use c++20 libraries ;)
2022-11-23 09:34:50 +01:00
Lion Kortlepel
01bcc3d18c replace all clocks with a generic TimeType 2022-11-23 09:34:50 +01:00
Lion Kortlepel
c7f6835702 use high_resolution_clock instead of system_clock 2022-11-23 09:34:50 +01:00
Lion Kortlepel
0327b41611 change to system_clock for times converted to time_t 2022-11-23 09:34:50 +01:00
Lion Kortlepel
48135f4544 add new onShutdown timing to changelog 2022-11-23 09:34:50 +01:00
Lion Kortlepel
0411ba533d change onShutdown to be called before all players are kicked 2022-11-23 09:34:50 +01:00
Lion Kortlepel
41dd4ff678 add version to changelog 2022-11-23 09:34:50 +01:00
Lion Kortlepel
e1c2d0d5fb add version command 2022-11-23 09:34:50 +01:00
Lion Kortlepel
69c2868025 fix server not closing after detecting an invalid config (fixes #151) 2022-11-23 09:34:50 +01:00
Lion Kortlepel
c8ca4564a1 fix error which complains about AuthKey not being present before reading the config file 2022-11-23 09:34:50 +01:00
Lion Kortlepel
0c6112c28a add boost to requirements in README for windows 2022-11-23 09:34:50 +01:00
Lion Kortlepel
4860849f2f add boost-uuid to vcpkg on windows 2022-11-23 09:34:50 +01:00
Lion Kortlepel
3bd67d959f handle all exceptions which happen inside a logging macro
and print them ;)
2022-11-23 09:34:50 +01:00
Lion Kortlepel
13a86d3e77 add explanative debug messages to parse errors in GetPidVid 2022-11-23 09:34:50 +01:00
Lion Kortlepel
dff94a41be fix potential out of bounds exception in GetPidVid 2022-11-23 09:34:50 +01:00
Lion Kortlepel
79ee5915b4 fix unhandled out of bounds in Authentication 2022-11-23 09:34:50 +01:00
Lion Kortlepel
3fcf23977b fix fix for out-of-bounds crash, add disconnect messages to some failure conditions 2022-11-23 09:34:50 +01:00
Lion Kortlepel
487917482f fix out-of-range crash in SendFile
when the UnsafeName does not contain a `/`, the server would simply
crash, as an invalid substr() index was given, which triggered a
std::out_of_range.
2022-11-23 09:34:50 +01:00
Lion Kortlepel
2ca39a7368 fix cmakelists 2022-11-23 09:34:50 +01:00
20dka
9d8aeef423 refactor of vehicle packet handling 2022-11-23 09:34:50 +01:00
20dka
dee7f74906 remove unused PPSMonitor parameter 2022-11-23 09:34:50 +01:00
20dka
66f014ae42 add autocomplete for lua,kick,settings commands 2022-11-23 09:34:50 +01:00
20dka
12245d81a1 fix compilation error and use boost for string lowercase 2022-11-23 09:34:50 +01:00
20dka
a6e0332e3c add command 'quit' as alternative to 'exit' 2022-11-23 09:34:50 +01:00
20dka
c461a63d9d update client pointer lock approach 2022-11-23 09:34:50 +01:00
20dka
69726a9b03 add owner check to other vehicle packets 2022-11-23 09:34:50 +01:00
20dka
9acb6951d6 add owner check to position packets 2022-11-23 09:34:50 +01:00
Lion Kortlepel
6ebfe5743c add :Wait(timeout_s) function to the result of MP.TriggerGlobalEvent 2022-11-23 09:34:50 +01:00
Lion Kortlepel
3c138e2891 change date format from d/m/y to Y/m/d 2022-11-23 09:34:50 +01:00
Lion Kortlepel
2279ba4d6b attempt to report errors from event triggers 2022-11-23 09:34:50 +01:00
Lion Kortlepel
30b038a6bb revert adding nil to json
it causes issues, not worth it
2022-11-23 09:34:50 +01:00
Lion Kortlepel
c39c7bb0a4 add scripts & dockerfiles to build on multiple platforms 2022-11-23 09:34:50 +01:00
Lion Kortlepel
06b238d63f remove unused code 2022-11-23 09:34:50 +01:00
Lion Kortlepel
1856dd2002 fix various little things in the json encode and decode 2022-11-23 09:34:50 +01:00
Lion Kortlepel
b068a9b48f fix TriggerGlobalEvent not passing event arguments correctly (closes #106) 2022-11-23 09:34:50 +01:00
Lion Kortlepel
e8d66ef983 add git hash to version print on startup
this helps us debug issues
2022-11-23 09:34:50 +01:00
Lion Kortlepel
7f47337e1b modify authkey notice to mention the possibility of entering an invalid key
this makes it clear that you may put anything as your key, as long as
you only want private servers.
2022-11-23 09:34:50 +01:00
Lion Kortlepel
b0b4dc51b0 fix crash when the header of a TCP packet is negative 2022-11-23 09:34:50 +01:00
Lion Kortlepel
7abfae425d move around includes to satisfy the magical msvc 2022-11-23 09:34:50 +01:00
Lion Kortlepel
4791af4453 use system_clock::to_time_t instead of high_resolution_clock::* 2022-11-23 09:34:50 +01:00
Lion Kortlepel
a44f9b3dba fix various build warnings and errors with clang++ 2022-11-23 09:34:50 +01:00
Lion Kortlepel
c51d713969 add uuid source files
were missed last commit related to uuids
2022-11-23 09:34:50 +01:00
Lion Kortlepel
3781f2960a add debug to the changelog 2022-11-23 09:34:50 +01:00
Lion Kortlepel
9e469b04f1 add Util.GenerateUUID lua api
This api generates a pseudorandom RFC 4122 compliant UUID
(https://www.ietf.org/rfc/rfc4122.txt). It's pseudorandom, so it is
*unlikely* to have collisions.
2022-11-23 09:34:49 +01:00
Lion Kortlepel
96273e1d06 fix invalid include, reformat TConsole 2022-11-23 09:34:49 +01:00
Lion Kortlepel
c717037895 add debug command to track statistics and debug client connections
The `debug` command shows info useful to develpers of the client and
server
2022-11-23 09:34:49 +01:00
Lion Kortlepel
692129cb81 add ToHumanReadableSize, which formats data sizes into MiB, GiB, etc.
this is needed in order to display data sizes in upcoming changes
related to tracking statistics about the server's performance
2022-11-23 09:34:49 +01:00
Lion Kortlepel
6842dccfc3 rework UDP recv loop, add packet statistics tracking
The UDPServerMain was badly designed and had multiple potential bugs and
unexpected & silent failures. The changes simplify the code here, and
tracks those unexpected failures in app-wide statistics. Also added,
while at that, some more tracking of how many packets and bytes are
sent & received per client
2022-11-23 09:34:49 +01:00
Lion Kortlepel
e77dfd5a57 add boost to vcpkg requirements 2022-11-23 09:34:49 +01:00
Lion Kortlepel
54ad1c2715 add settings to changelog 2022-11-23 09:34:49 +01:00
Lion Kortlepel
48ce7c9721 add settings set/get/list/help console command
this makes use of the new settings-in-a-hashmap in order to make
changing any settings (except authkey) easy.
2022-11-23 09:34:49 +01:00
Lion Kortlepel
3aa6784627 bump version to 3.2.0 2022-11-23 09:34:49 +01:00
Lion Kortlepel
bec09a4761 change settings to be a hash map instead of a fixed struct
this makes adding settings and changing settings incredibly easy, both
from the console and from lua.
2022-11-23 09:34:49 +01:00
Lion Kortlepel
7d35595683 potential fix for #141 2022-11-23 09:34:49 +01:00
Lion Kortlepel
982cbf116c potential fix to #141 2022-11-23 09:34:49 +01:00
Lion Kortlepel
43e70d80d2 fix dockerfie for archlinux 2022-11-23 09:34:49 +01:00
Lion Kortlepel
00a35a636c add scripts & dockerfiles to build on multiple platforms 2022-11-23 09:34:49 +01:00
Lion
47e64a7343 merge release candidate v3.1.1 (#142)
patches and hotfixes!
2022-10-31 11:32:16 +01:00
Lion Kortlepel
7f5b3919f4 make destructors of virtual classes virtual
this causes warnings in clang, and rightfully so :^)
2022-10-31 11:31:16 +01:00
Lion Kortlepel
896e777e23 update changelog 2022-10-31 11:31:16 +01:00
Lion Kortlepel
aa58c1e211 another potential fix for #141 2022-10-31 11:31:15 +01:00
Lion Kortlepel
49a9226dca update changelog to mention fixes 2022-10-31 11:31:15 +01:00
Lion Kortlepel
b10d5d0f4e fix empty events causing issues in the server
an empty event packet, if sent just right, could crash the server
2022-10-31 11:31:15 +01:00
Lion Kortlepel
5581fd1692 fix chat message impersonation issue
instead of using the supplied name, we ignore it entirely and use the
server's internal name for the client
2022-10-31 11:31:15 +01:00
Lion Kortlepel
d36bb7962c ignore empty chat messages
this could happen with a malicious client of some kind, we should simply
ignore them
2022-10-31 11:31:15 +01:00
Lion Kortlepel
4e8bd993d5 remove J packet handler
it wasn't used by anything in the launcher, mod, or during the join
sequence, so i removed it for now.
2022-10-31 11:31:15 +01:00
Lion Kortlepel
abff9bfbdb fix crash when chat message is malformed 2022-10-31 11:31:15 +01:00
Lion Kortlepel
b024533f90 fix crash when the header of a TCP packet is negative 2022-10-31 11:31:15 +01:00
Lion Kortlepel
f9251ff92c add more warning prints on unexpected cases 2022-10-31 11:31:15 +01:00
Lion Kortlepel
99f41c28cb fix #135 by making onPlayerDisconnect blocking, and calling it before removing the player
before, the handlers were not waited for, so the client was usually
destructed before lua got to the actual event handler call. Now, the
handler is called and waited on, and once all handlers are done, the
client is properly removed from the players internally, thus making
calls to GetPlayerName, GetPlayerIdentifiers, etc. return nil etc.
2022-10-31 11:31:15 +01:00
Lion Kortlepel
bbd27c9cba fix formatting bug in status (closes #143) 2022-10-31 11:31:15 +01:00
Lion Kortlepel
4682922467 add more fixes for msvc static linking 2022-10-31 11:31:15 +01:00
Lion Kortlepel
83fb387dfe potential fix to #141 2022-10-31 11:31:09 +01:00
Lion Kortlepel
2b61f11a86 fix EnsureArgsCount not properly printing min/max 2022-10-31 11:24:13 +01:00
Lion Kortlepel
a8b1a205f7 bump version to 3.1.1
I'm expecting to release a 3.1.1 with some fixes
2022-10-31 11:24:13 +01:00
Lion
dd9376447a Fix README errors (#138) 2022-10-26 14:34:01 +02:00
Lion
aa185afabf Update README.md 2022-10-23 03:35:39 +02:00
Lion
e8caeb9126 merge release candidate v3.1.0 (#83)
See https://github.com/BeamMP/BeamMP-Server/blob/rc-v3.1.0/Changelog.md for
a list of changes
2022-10-22 23:24:48 +02:00
Lion Kortlepel
fa1944dbef update readme to reflect dependency- and code-changes in 3.1.0 2022-10-22 23:22:42 +02:00
Lion Kortlepel
093f905fd8 remove redundant addition libboost library install in cmake-linux action 2022-10-22 23:06:11 +02:00
Lion Kortlepel
2a45d2282d update rc-v3.1.0 to master 2022-10-22 22:55:12 +02:00
Lion Kortlepel
aeb024953a fix sentry line that caused build to fail 2022-10-22 22:48:23 +02:00
ㄗㄠˋ ㄑㄧˊ
12a0ab7fdd document dependencies for macos (#109) 2022-10-22 22:47:53 +02:00
Lion Kortlepel
917c501faf fix typo causing beammp forum id not to show in identifiers (fix #137) 2022-10-22 21:16:31 +02:00
Lion Kortlepel
340933bbb3 fix dependencies for test run (add curl, fix ssl) 2022-10-22 21:01:44 +02:00
Lion Kortlepel
a63359479e rename dependencies for test run in github actions 2022-10-22 20:44:47 +02:00
Lion Kortlepel
bdf2da758c remove pps from heartbeat
"PPS has no meaning anymore and is completely irrelevant. You should
ignore it, it is not an indicator of ANYTHING. If it’s high, that means
NOTHING. If it’s low, that means NOTHING. If it’s -, that means
NOTHING."

It's packets per second per player per vehicle, but is only sent every
30 seconds, its not averaged, and on the client-side, it shows a ping
icon next to it.

A client can open a new connection to the server and send a `P`, and
measure the time to the `P` response packet. The connection is then
closed. This was added ages ago, please use this instead for ping :)
2022-10-17 14:05:51 +02:00
Lion Kortlepel
88c0ed56e4 add _WIN32_WINNT and move CRT no warnings flag 2022-10-17 12:25:13 +02:00
Lion Kortlepel
4256977400 remove crt's "this function or variable may be unsafe" warnings
they are useless, as they are in dependencies or parts of the code
we don't care about. Also, the "safe" alternatives straightup dont work
on linux.
2022-10-17 12:21:27 +02:00
Lion Kortlepel
309a9d1fa9 fix invalid windows workflows indentation 2022-10-17 12:12:55 +02:00
Lion Kortlepel
ad860835ca fix linux tests run not installing openssl properly 2022-10-17 12:10:52 +02:00
Lion Kortlepel
c6c2efb0b1 revert "update linux and windows workflows to run on pr open, reopen, review submit"
This reverts commit 23e9941704.
2022-10-17 12:09:36 +02:00
Lion
c4c3b03b7a replace networking with boost::asio (synchronous) (#134)
- replaced all networking with boost::asio abstractions
- rewrote the SetStatus(), GetStatus() crap (now IsDisconnected() and
Disconnect() with reason)
- fixed bug related to ghost players / ghost cars
- handles packets as binary for the most part (vector<uint8_t> instead
of string) to fix various issues
2022-10-17 12:03:43 +02:00
Lion Kortlepel
fd51336a91 update vcpkg 2022-10-17 12:01:08 +02:00
Lion Kortlepel
466845b314 add udp binary data fix to Changelog 2022-10-16 00:05:02 +02:00
Lion Kortlepel
92632b53b5 fix binary data breaking in UDPRcvFromClient 2022-10-15 23:30:09 +02:00
Lion Kortlepel
331a597ec7 add info about new networking to changelog 2022-10-15 23:19:33 +02:00
Lion Kortlepel
87965433c2 change log levels of common warnings and errors to debug
this hides a lot of the "standard" errors we get behind the debug flag.
for example, disconnecting a disconnected player would be such an error
2022-10-15 23:16:16 +02:00
Lion Kortlepel
75ff9f7571 remove "backend response failed to parse as valid json" 2022-10-15 23:16:16 +02:00
Lion Kortlepel
94c0547a35 fix crash when the client disconnects while sending first identify setting 2022-10-15 23:16:16 +02:00
Lion Kortlepel
98f77e157f add WIN32_STATIC_RUNTIME option to cmake 2022-10-15 23:16:16 +02:00
Lion Kortlepel
54730d2baf remove heartbeat spam logging 2022-10-15 23:16:16 +02:00
Lion Kortlepel
064e71e59f fix client version check 2022-10-15 23:16:16 +02:00
Lion Kortlepel
2678234d67 dont check for -Werror=zero-as-null-pointer-constant 2022-10-15 23:16:16 +02:00
Lion Kortlepel
4320a91e5c use message() instead of what() for ec 2022-10-15 23:16:16 +02:00
Lion Kortlepel
7d1318653c fix boost::system::error_code 2022-10-15 23:16:16 +02:00
Lion Kortlepel
67d02d4cf2 remove unused error check 2022-10-15 23:16:16 +02:00
Lion Kortlepel
93b2559120 switch to boost 1.74 2022-10-15 23:16:05 +02:00
Lion Kortlepel
ed872f730d link against boost::system 2022-10-06 00:57:31 +02:00
Lion Kortlepel
b25f4a875c run on latest ubuntu 2022-10-06 00:54:57 +02:00
Lion Kortlepel
cc6b7846b2 add system include 2022-10-06 00:51:51 +02:00
Lion Kortlepel
88f5db514f remove unused headers 2022-10-06 00:46:35 +02:00
Lion Kortlepel
e595192829 rename header to boost errc 2022-10-06 00:43:17 +02:00
Lion Kortlepel
c69418ea5e add boost_system dependency for linux gh actions 2022-10-06 00:40:39 +02:00
Lion Kortlepel
917e3f98ab fix github actions dependencies for linux to use proper boost version 2022-10-06 00:36:57 +02:00
Lion Kortlepel
c42a523532 remove SO_SNDTIMEO for now 2022-10-06 00:24:13 +02:00
Lion Kortlepel
95ae0f5d03 fix 'Od' and 'Or' packets not being broadcast 2022-10-05 22:17:56 +02:00
Lion Kortlepel
fc0a509bd9 fix clientversion parameter in heartbeat 2022-10-05 21:15:11 +02:00
Lion Kortlepel
6249397fb5 add libboost-all-dev to github actions
it's a new dependency
2022-10-05 20:50:28 +02:00
Lion Kortlepel
231b13a0e7 fix a ghost client bug 2022-10-05 18:17:18 +02:00
Lion Kortlepel
7d2e4d4581 replace tcp networking with boost::asio tcp networking 2022-10-05 18:17:04 +02:00
Lion Kortlepel
7446526a19 fix binding of udp server socket
it was not binding properly because it wasn't open()ed, i guess
2022-10-05 13:06:36 +02:00
Lion Kortlepel
6e97a3cd6e switch udp networking to boost implementation 2022-10-05 12:14:25 +02:00
Lion Kortlepel
30482d290a add boost 1.75 dependency
this should be available on most platforms.
boost allows us to simplify a LOT of code.
2022-10-05 11:50:15 +02:00
Lion Kortlepel
5f1d003077 fix various potential crashes in TServer::HandlePosition 2022-10-03 17:06:32 +02:00
Lion Kortlepel
5d3dff3c88 add identifiers (beammp id, ip) as an argument to onPlayerAuth 2022-10-03 15:31:32 +02:00
Lion Kortlepel
cb0cb30797 fix windows compiler not understanding a CLEAR AND SIMPLE FUNCTION-STYLE
CONSTRUCTOR CALL

AHHHHHHH
2022-10-03 15:12:59 +02:00
Lion Kortlepel
1f14de2e71 revert 9c6127a105 and apply proper fix 2022-10-03 15:11:26 +02:00
Lion Kortlepel
658b37acac fix error sometimes not displaying when failing inside global event handler 2022-10-03 14:38:19 +02:00
Lion Kortlepel
d63c84286e replace logging functions with new fmt versions in ParseVehicle 2022-10-01 22:25:45 +02:00
Lion Kortlepel
9c6127a105 fix bug which may cause a server to crash when a car is spawned
thanks @Anonymous275
2022-10-01 19:10:21 +02:00
ㄗㄠˋ ㄑㄧˊ
fdf24815bb document dependencies for macos (#109) 2022-09-26 12:18:47 +02:00
49 changed files with 2094 additions and 1330 deletions

View File

@@ -1,18 +1,13 @@
name: CMake Linux Build
on:
push:
pull_request:
types: [opened, reopened]
pull_request_review:
types: [submitted]
on: [push]
env:
BUILD_TYPE: Release
jobs:
linux-build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v2
@@ -25,7 +20,7 @@ jobs:
run: |
echo ${#beammp_sentry_url}
sudo apt-get update
sudo apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev cmake g++-10
sudo apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev cmake g++-10 libboost1.74-all-dev
- name: Create Build Environment
run: cmake -E make_directory ${{github.workspace}}/build-linux
@@ -61,23 +56,23 @@ jobs:
run-tests:
needs: linux-build
runs-on: ubuntu-20.04
runs-on: ubuntu-22.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
sudo apt-get install -y liblua5.3-0 libssl3 curl
- name: Test
working-directory: ${{github.workspace}}
shell: bash
run: |
chmod +x ./BeamMP-Server-tests
./BeamMP-Server-tests
chmod +x ./BeamMP-Server-tests
./BeamMP-Server-tests

View File

@@ -1,11 +1,6 @@
name: CMake Windows Build
on:
push:
pull_request:
types: [opened, reopened]
pull_request_review:
types: [submitted]
on: [push]
env:
BUILD_TYPE: Release
@@ -13,20 +8,20 @@ env:
jobs:
windows-build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
with:
submodules: "recursive"
submodules: 'recursive'
- name: Restore artifacts, or run vcpkg, build and cache artifacts
uses: lukka/run-vcpkg@v7
id: runvcpkg
with:
vcpkgArguments: "lua zlib rapidjson openssl websocketpp curl"
vcpkgDirectory: "${{ runner.workspace }}/b/vcpkg"
vcpkgGitCommitId: "a106de33bbee694e3be6243718aa2a549a692832"
vcpkgTriplet: "x64-windows-static"
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl boost-variant boost-spirit boost-phoenix boost-core boost-system boost-asio boost-uuid'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: "06b5f4a769d848d1a20fa0acd556019728b56273"
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment
run: cmake -E make_directory ${{github.workspace}}/build-windows
@@ -54,9 +49,10 @@ jobs:
shell: bash
run: |
cmake --build . --config Debug
- name: Archive debug artifacts
uses: actions/upload-artifact@v2
with:
name: BeamMP-Server-debug.exe
path: ${{github.workspace}}/build-windows/Debug/BeamMP-Server.exe

View File

@@ -32,7 +32,7 @@ jobs:
upload-release-files-linux:
name: Upload Linux Release Files
runs-on: ubuntu-latest
runs-on: ubuntu-22.04
needs: create-release
steps:
- uses: actions/checkout@v2
@@ -42,7 +42,7 @@ jobs:
- name: Install Dependencies
run: |
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 libboost-dev libboost1.74-all-dev libboost1.74-dev
- name: Create Build Environment
run: cmake -E make_directory ${{github.workspace}}/build-linux
@@ -83,9 +83,9 @@ jobs:
uses: lukka/run-vcpkg@v7
id: runvcpkg
with:
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl'
vcpkgArguments: 'lua zlib rapidjson openssl websocketpp curl boost-variant boost-spirit boost-phoenix boost-core boost-system boost-asio'
vcpkgDirectory: '${{ runner.workspace }}/b/vcpkg'
vcpkgGitCommitId: 'a106de33bbee694e3be6243718aa2a549a692832'
vcpkgGitCommitId: '06b5f4a769d848d1a20fa0acd556019728b56273'
vcpkgTriplet: 'x64-windows-static'
- name: Create Build Environment

3
.gitmodules vendored
View File

@@ -31,3 +31,6 @@
[submodule "deps/doctest"]
path = deps/doctest
url = https://github.com/doctest/doctest
[submodule "deps/lk-result"]
path = deps/lk-result
url = https://gitlab.com/lionkor/lk-result.git

View File

@@ -4,11 +4,32 @@ cmake_minimum_required(VERSION 3.4 FATAL_ERROR)
message(STATUS "You can find build instructions and a list of dependencies in the README at \
https://github.com/BeamMP/BeamMP-Server")
include(FetchContent)
FetchContent_Declare(
cmake-tools
GIT_REPOSITORY https://github.com/brobeson/cmake-tools.git
GIT_TAG main
)
FetchContent_MakeAvailable(cmake-tools)
list(APPEND CMAKE_MODULE_PATH "${cmake-tools_SOURCE_DIR}")
include(CMakeToolsVersionFromGit)
execute_process(
COMMAND git log -1 --format=%h
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
OUTPUT_VARIABLE GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
project(BeamMP-Server
DESCRIPTION "Server for BeamMP - The Multiplayer Mod for BeamNG.drive"
HOMEPAGE_URL https://beammp.com
LANGUAGES CXX C)
# quality of life stuff
set(CMAKE_COLOR_DIAGNOSTICS ON)
find_package(Git REQUIRED)
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
@@ -22,12 +43,13 @@ if(GIT_SUBMODULE)
endif()
endif()
set(HTTPLIB_REQUIRE_OPENSSL ON)
set(SENTRY_BUILD_SHARED_LIBS OFF)
add_compile_definitions(CPPHTTPLIB_OPENSSL_SUPPORT=1)
option(WIN32_STATIC_RUNTIME "Build statically-linked runtime on windows (don't touch unless you know what you're doing)" ON)
# ------------------------ APPLE ---------------------------------
if(APPLE)
if(IS_DIRECTORY /opt/homebrew/Cellar/lua@5.3/5.3.6)
@@ -47,14 +69,13 @@ if(APPLE)
endif()
# ------------------------ WINDOWS ---------------------------------
elseif (WIN32)
# this has to happen before sentry, so that crashpad on windows links with these settings.
message(STATUS "MSVC -> forcing use of statically-linked runtime.")
STRING(REPLACE "/MD" "/MT" CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS_RELEASE})
STRING(REPLACE "/MDd" "/MTd" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
# ------------------------ LINUX ---------------------------------
if (WIN32_STATIC_RUNTIME)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
# ------------------------ UNIX ------------------------------------
elseif (UNIX)
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")
option(SANITIZE "Turns on thread and UB sanitizers" OFF)
if (SANITIZE)
message(STATUS "sanitize is ON")
@@ -78,10 +99,7 @@ endif()
add_subdirectory("deps/sentry-native")
# ------------------------ C++ SETUP ---------------------------------
set(CMAKE_CXX_STANDARD 17)
if (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif ()
set(CMAKE_CXX_STANDARD 20)
# ------------------------ DEPENDENCIES ------------------------------
message(STATUS "Adding local source dependencies")
@@ -95,6 +113,8 @@ include(FindOpenSSL)
include(FindThreads)
include(FindZLIB)
find_package(Boost 1.70 REQUIRED COMPONENTS system)
set(BeamMP_Sources
include/TConsole.h src/TConsole.cpp
include/TServer.h src/TServer.cpp
@@ -117,6 +137,8 @@ set(BeamMP_Sources
include/ArgsParser.h src/ArgsParser.cpp
include/TPluginMonitor.h src/TPluginMonitor.cpp
include/Environment.h
include/BoostAliases.h
include/Uuid.h src/Uuid.cpp
)
set(BeamMP_Includes
@@ -128,13 +150,21 @@ set(BeamMP_Includes
"${CMAKE_CURRENT_SOURCE_DIR}/deps/sol2/include"
"${CMAKE_CURRENT_SOURCE_DIR}/deps/rapidjson/include"
"${CMAKE_CURRENT_SOURCE_DIR}/deps/asio/asio/include"
"${CMAKE_CURRENT_SOURCE_DIR}/deps/lk-result/include"
"${CMAKE_CURRENT_SOURCE_DIR}/deps"
"${CMAKE_CURRENT_SOURCE_DIR}/deps/fmt/include"
)
set(BeamMP_Definitions
SECRET_SENTRY_URL="${BEAMMP_SECRET_SENTRY_URL}"
BEAMMP_GIT_HASH="${GIT_HASH}"
LK_RESULT_USE_FMT
)
if (WIN32)
list(APPEND BeamMP_Definitions _WIN32_WINNT=0x0601)
list(APPEND BeamMP_Definitions _CRT_SECURE_NO_WARNINGS)
endif()
if (UNIX)
set(BeamMP_CompileOptions
-Wall
@@ -156,30 +186,37 @@ if (UNIX)
-Werror=missing-field-initializers
-Werror=write-strings
-Werror=ctor-dtor-privacy
-Werror=switch-enum
-Werror=switch-default
-Wswitch-enum
-Wswitch-default
-Werror=old-style-cast
-Werror=overloaded-virtual
-Werror=zero-as-null-pointer-constant
-Werror=overloaded-virtual
-Werror=missing-include-dirs
-Werror=unused-result
-fstack-protector
-Wzero-as-null-pointer-constant
)
else()
set(BeamMP_CompileOptions
/bigobj
/INCREMENTAL:NO /NODEFAULTLIB:MSVCRT /NODEFAULTLIB:LIBCMT
)
endif()
set(BeamMP_Libraries
Boost::boost
Boost::system
doctest::doctest
OpenSSL::SSL
OpenSSL::Crypto
sol2::sol2
fmt::fmt
Threads::Threads
ZLIB::ZLIB
${LUA_LIBRARIES}
commandline
sentry
fmt::fmt
)
if (WIN32)
@@ -210,7 +247,7 @@ target_include_directories(BeamMP-Server SYSTEM PRIVATE
${BeamMP_Includes}
)
target_link_libraries(BeamMP-Server
target_link_libraries(BeamMP-Server
${BeamMP_Libraries}
${BeamMP_PlatformLibs}
)

View File

@@ -1,3 +1,19 @@
# v3.2.0
- ADDED `settings` command, which lets you `get`, `list`, and `set` config options from the console
- ADDED `debug` command, which shows info about connected clients & networking (for developers)
- ADDED `Util.GenerateUUID()`, which generates an RFC4122 UUID (universally unique identifier)
- ADDED `version` command, which shows version information
- ADDED `onPlayerRequestMods` event, letting Lua disallow individual mods from being sent to clients
- CHANGED `onShutdown` to be called before all players are kicked
# v3.1.1
- FIXED bug which caused GetPlayerIdentifiers, GetPlayerName, etc not to work in `onPlayerDisconnect`
- FIXED some issues which could cause the server to crash when receiving malformed data
- FIXED a bug which caused a server to crash during authentication when receiving malformed data
- FIXED minor vulnerability in chat message handling
- FIXED a minor formatting bug in the `status` command
# v3.1.0
@@ -10,6 +26,9 @@
- ADDED error messages to some lua functions
- ADDED HOME and END button working in console
- ADDED `MP.TriggerClientEventJson()` which takes a table as the data argument and sends it as JSON
- ADDED identifiers (beammp id, ip) to onPlayerAuth (4th argument)
- ADDED more network debug logging
- CHANGED all networking to be more stable, performant, and safe
- FIXED `ip` in MP.GetPlayerIdentifiers
- FIXED issue with client->server events which contain `:`
- FIXED a fatal exception on LuaEngine startup if Resources/Server is a symlink
@@ -18,6 +37,8 @@
- FIXED bug which caused hot-reload not to report syntax errors
- FIXED missing error messages on some event handler calls
- FIXED vehicles not deleting for all players if an edit was cancelled by Lua
- FIXED server not handling binary UDP packets properly
- REMOVED "Backend response failed to parse as valid json" message
# v3.0.2

117
README.md
View File

@@ -29,7 +29,7 @@ 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.
TLDR; [Issues](https://github.com/BeamMP/BeamMP-Server/issues) with the "help wanted" or "good first issue" label or with nobody assigned.
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.
@@ -39,7 +39,7 @@ If you need support with understanding the codebase, please write us in the Disc
## 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 checkout a tag with `git checkout tags/TAGNAME`, where `TAGNAME` is the tag, for example `v3.1.0`.
## Supported Operating Systems
@@ -51,7 +51,7 @@ You can find precompiled binaries under [Releases](https://github.com/BeamMP/Bea
## Build Instructions
**__Do not compile from `master`. Always build from a release tag, i.e. `tags/v2.3.3`!__**
**__Do not compile from `master`. Always build from a release tag, i.e. `tags/v3.1.0`!__**
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)).
@@ -59,35 +59,105 @@ Currently only Linux and Windows are supported (generally). See [Releases](https
#### Windows
There are **no runtime libraries** needed for Windows.
Please use the prepackaged binaries in [Releases](https://github.com/BeamMP/BeamMP-Server/releases/).
Dependencies for **Windows** can be installed with `vcpkg`.
These are:
```
lua
zlib
rapidjson
openssl
websocketpp
curl
lua zlib rapidjson openssl websocketpp curl boost
```
The triplet we use for releases is `x64-windows-static`.
#### Linux
Runtime dependencies - you want to find packages for:
- libz
- rapidjson
- lua5.3
- ssl / openssl
- websocketpp
- curl (with ssl support)
We recommend Ubuntu 22.04 or Arch Linux. Any Linux distribution will work, but you have to figure out the package names yourself (please feel free to PR in a change to this README with that info).
Build-time dependencies are:
##### Runtime Dependencies
These are needed to *run* the server.
<details>
<summary>
Ubuntu 22.04
</summary>
`apt-get install` the following libraries:
```
liblua5.3-0
libssl3
curl
```
</details>
<details>
<summary>
Arch Linux
</summary>
`pacman -Syu` the following libraries:
```
lua53
openssl
curl
```
</details>
##### Build Dependencies
These are needed for you to *build* the server, in addition to the [runtime dependencies](#runtime-dependencies).
<details>
<summary>
Ubuntu 22.04
</summary>
`apt-get install` the following libraries and programs:
```
git
make
libz-dev
rapidjson-dev
liblua5.3
libssl-dev
libwebsocketpp-dev
libcurl4-openssl-dev
cmake
g++-10
libboost1.74-all-dev
libssl3
curl
```
</details>
<details>
<summary>
Arch Linux
</summary>
`pacman -Syu` the following libraries and programs:
```
lua53
openssl
curl
git
cmake
g++
cmake
zlib
boost
websocketpp
```
</details>
#### 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
@@ -95,13 +165,12 @@ g++
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`.
2. Clone the repository in a location of your choice with `git clone https://github.com/BeamMP/BeamMP-Server` .
3. Change into the BeamMP-Server directory by running `cd BeamMP-Server`.
4. Checkout the branch of the release you want to compile, for example `git checkout tags/v3.0.2` for version 3.0.2. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags).
5. Ensure that all submodules are initialized by running `git submodule update --init --recursive`
6. Run `cmake . -DCMAKE_BUILD_TYPE=Release` (with `.`)
7. Run `make`
8. 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-installation) for further setup after installation (which we just did), such as port-forwarding and getting a key to actually run the server.
4. Checkout the branch or tag of the release you want to compile, for example `git checkout tags/v3.0.2` for version 3.0.2. You can find the latest version [here](https://github.com/BeamMP/BeamMP-Server/tags).
6. Run `cmake . -DCMAKE_BUILD_TYPE=Release` (with `.`). This may take some time, and will update all submodules and prepare the build.
7. Run `make -j` . This step will take some time and will use a lot of CPU and RAM. Remove the `-j` if you run out of memory. *If you change something in the source code, you only have to re-run this step.*
8. You 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-installation) 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 &`*.*

1
deps/lk-result vendored Submodule

Submodule deps/lk-result added at 4a4eda8092

6
include/BoostAliases.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <boost/asio.hpp>
#include <boost/system/error_code.hpp>
using namespace boost::asio;

View File

@@ -7,8 +7,10 @@
#include <string>
#include <unordered_set>
#include "BoostAliases.h"
#include "Common.h"
#include "Compat.h"
#include "TResourceManager.h"
#include "VehicleData.h"
class TServer;
@@ -19,9 +21,8 @@ class TServer;
#endif // WINDOWS
struct TConnection final {
SOCKET Socket;
struct sockaddr SockAddr;
socklen_t SockAddrLen;
ip::tcp::socket Socket;
ip::tcp::endpoint SockAddr;
};
class TClient final {
@@ -33,8 +34,9 @@ public:
std::unique_lock<std::mutex> Lock;
};
explicit TClient(TServer& Server);
TClient(TServer& Server, ip::tcp::socket&& Socket);
TClient(const TClient&) = delete;
~TClient();
TClient& operator=(const TClient&) = delete;
void AddNewCar(int Ident, const std::string& Data);
@@ -46,16 +48,20 @@ public:
void SetIdentifier(const std::string& key, const std::string& value) { mIdentifiers[key] = value; }
std::string GetCarData(int Ident);
std::string GetCarPositionRaw(int Ident);
void SetUDPAddr(sockaddr_in Addr) { mUDPAddress = Addr; }
void SetDownSock(SOCKET CSock) { mSocket[1] = CSock; }
void SetTCPSock(SOCKET CSock) { mSocket[0] = CSock; }
void SetStatus(int Status) { mStatus = Status; }
void SetUDPAddr(const ip::udp::endpoint& Addr) { mUDPAddress = Addr; }
void SetDownSock(ip::tcp::socket&& CSock) { mDownSocket = std::move(CSock); }
void SetTCPSock(ip::tcp::socket&& CSock) { mSocket = std::move(CSock); }
void Disconnect(std::string_view Reason);
bool IsDisconnected() const { return !mSocket.is_open(); }
// locks
void DeleteCar(int Ident);
[[nodiscard]] const std::unordered_map<std::string, std::string>& GetIdentifiers() const { return mIdentifiers; }
[[nodiscard]] sockaddr_in GetUDPAddr() const { return mUDPAddress; }
[[nodiscard]] SOCKET GetDownSock() const { return mSocket[1]; }
[[nodiscard]] SOCKET GetTCPSock() const { return mSocket[0]; }
[[nodiscard]] const ip::udp::endpoint& GetUDPAddr() const { return mUDPAddress; }
[[nodiscard]] ip::udp::endpoint& GetUDPAddr() { return mUDPAddress; }
[[nodiscard]] ip::tcp::socket& GetDownSock() { return mDownSocket; }
[[nodiscard]] const ip::tcp::socket& GetDownSock() const { return mDownSocket; }
[[nodiscard]] ip::tcp::socket& GetTCPSock() { return mSocket; }
[[nodiscard]] const ip::tcp::socket& GetTCPSock() const { return mSocket; }
[[nodiscard]] std::string GetRoles() const { return mRole; }
[[nodiscard]] std::string GetName() const { return mName; }
void SetUnicycleID(int ID) { mUnicycleID = ID; }
@@ -63,9 +69,9 @@ public:
[[nodiscard]] int GetOpenCarID() const;
[[nodiscard]] int GetCarCount() const;
void ClearCars();
[[nodiscard]] int GetStatus() const { return mStatus; }
[[nodiscard]] int GetID() const { return mID; }
[[nodiscard]] int GetUnicycleID() const { return mUnicycleID; }
bool mUDPCONNECTED = false;
[[nodiscard]] bool IsConnected() const { return mIsConnected; }
[[nodiscard]] bool IsSynced() const { return mIsSynced; }
[[nodiscard]] bool IsSyncing() const { return mIsSyncing; }
@@ -73,9 +79,9 @@ public:
void SetIsGuest(bool NewIsGuest) { mIsGuest = NewIsGuest; }
void SetIsSynced(bool NewIsSynced) { mIsSynced = NewIsSynced; }
void SetIsSyncing(bool NewIsSyncing) { mIsSyncing = NewIsSyncing; }
void EnqueuePacket(const std::string& Packet);
[[nodiscard]] std::queue<std::string>& MissedPacketQueue() { return mPacketsSync; }
[[nodiscard]] const std::queue<std::string>& MissedPacketQueue() const { return mPacketsSync; }
void EnqueuePacket(const std::vector<uint8_t>& Packet);
[[nodiscard]] std::queue<std::vector<uint8_t>>& MissedPacketQueue() { return mPacketsSync; }
[[nodiscard]] const std::queue<std::vector<uint8_t>>& MissedPacketQueue() const { return mPacketsSync; }
[[nodiscard]] size_t MissedPacketQueueSize() const { return mPacketsSync.size(); }
[[nodiscard]] std::mutex& MissedPacketQueueMutex() const { return mMissedPacketsMutex; }
void SetIsConnected(bool NewIsConnected) { mIsConnected = NewIsConnected; }
@@ -83,6 +89,24 @@ public:
void UpdatePingTime();
int SecondsSinceLastPing();
// bytes received on UDP
std::atomic_size_t UdpReceived = 0;
// number of packets received on UDP
std::atomic_size_t UdpPacketsReceived = 0;
// bytes sent on UDP
std::atomic_size_t UdpSent = 0;
// number of packets sent on UDP
std::atomic_size_t UdpPacketsSent = 0;
// bytes received on TCP
std::atomic_size_t TcpReceived = 0;
// bytes sent on TCP
std::atomic_size_t TcpSent = 0;
TimeType::time_point ConnectionTime {};
ModMap AllowedMods;
private:
void InsertVehicle(int ID, const std::string& Data);
@@ -91,7 +115,7 @@ private:
bool mIsSynced = false;
bool mIsSyncing = false;
mutable std::mutex mMissedPacketsMutex;
std::queue<std::string> mPacketsSync;
std::queue<std::vector<uint8_t>> mPacketsSync;
std::unordered_map<std::string, std::string> mIdentifiers;
bool mIsGuest = false;
mutable std::mutex mVehicleDataMutex;
@@ -99,14 +123,16 @@ private:
TSetOfVehicleData mVehicleData;
SparseArray<std::string> mVehiclePosition;
std::string mName = "Unknown Client";
SOCKET mSocket[2] { SOCKET(0), SOCKET(0) };
sockaddr_in mUDPAddress {}; // is this initialization OK? yes it is
ip::tcp::socket mSocket;
ip::tcp::socket mDownSocket;
ip::udp::endpoint mUDPAddress {};
int mUnicycleID = -1;
std::string mRole;
std::string mDID;
int mStatus = 0;
int mID = -1;
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime;
std::chrono::time_point<TimeType> mLastPingTime;
std::mutex mDisconnectMtx;
};
std::optional<std::weak_ptr<TClient>> GetClient(class TServer& Server, int ID);
// Returns a valid client, or nullptr if no such client exists
std::shared_ptr<TClient> GetClient(class TServer& Server, int ID);

View File

@@ -1,5 +1,6 @@
#pragma once
#include "CustomAssert.h"
#include "TSentry.h"
extern TSentry Sentry;
@@ -23,6 +24,50 @@ namespace fs = std::filesystem;
#include "TConsole.h"
#include <boost/container/flat_map.hpp>
#include <boost/variant.hpp>
#include <chrono>
#include <variant>
using TimeType = std::chrono::system_clock;
#include <lk/Result.h>
template <typename T>
using Result = lk::Result<T>;
using Error = lk::Error;
// General
constexpr std::string_view StrDebug = "Debug";
constexpr std::string_view StrPrivate = "Private";
constexpr std::string_view StrPort = "Port";
constexpr std::string_view StrMaxCars = "MaxCars";
constexpr std::string_view StrMaxPlayers = "MaxPlayers";
constexpr std::string_view StrMap = "Map";
constexpr std::string_view StrName = "Name";
constexpr std::string_view StrDescription = "Description";
constexpr std::string_view StrResourceFolder = "ResourceFolder";
constexpr std::string_view StrAuthKey = "AuthKey";
constexpr std::string_view StrLogChat = "LogChat";
// Misc
constexpr std::string_view StrSendErrors = "SendErrors";
constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
constexpr std::string_view StrHideUpdateMessages = "ImScaredOfUpdates";
constexpr std::string_view StrIncludeSubdirectories = "IncludeSubdirectories";
// HTTP
constexpr std::string_view StrHTTPServerEnabled = "HTTPServerEnabled";
constexpr std::string_view StrHTTPServerUseSSL = "UseSSL";
constexpr std::string_view StrSSLKeyPath = "SSLKeyPath";
constexpr std::string_view StrSSLCertPath = "SSLCertPath";
constexpr std::string_view StrHTTPServerPort = "HTTPServerPort";
constexpr std::string_view StrHTTPServerIP = "HTTPServerIP";
// Unused
constexpr std::string_view StrCustomIP = "Unused.CustomIP";
struct Version {
uint8_t major;
uint8_t minor;
@@ -35,6 +80,12 @@ struct Version {
template <typename T>
using SparseArray = std::unordered_map<size_t, T>;
template <typename K, typename V>
using HashMap = std::unordered_map<K, V>;
using boost::variant;
using boost::container::flat_map;
// static class handling application start, shutdown, etc.
// yes, static classes, singletons, globals are all pretty
// bad idioms. In this case we need a central way to access
@@ -43,30 +94,17 @@ using SparseArray = std::unordered_map<size_t, T>;
class Application final {
public:
// types
struct TSettings {
std::string ServerName { "BeamMP Server" };
std::string ServerDesc { "BeamMP Default Description" };
std::string Resource { "Resources" };
std::string MapName { "/levels/gridmap_v2/info.json" };
std::string Key {};
std::string SSLKeyPath { "./.ssl/HttpServer/key.pem" };
std::string SSLCertPath { "./.ssl/HttpServer/cert.pem" };
bool HTTPServerEnabled { false };
int MaxPlayers { 8 };
bool Private { true };
int MaxCars { 1 };
bool DebugModeEnabled { false };
int Port { 30814 };
std::string CustomIP {};
bool LogChat { true };
bool SendErrors { true };
bool SendErrorsMessageEnabled { true };
int HTTPServerPort { 8080 };
std::string HTTPServerIP { "127.0.0.1" };
bool HTTPServerUseSSL { false };
bool HideUpdateMessages { false };
[[nodiscard]] bool HasCustomIP() const { return !CustomIP.empty(); }
};
using SettingValue = std::variant<std::string, bool, int>;
using SettingsMap = flat_map<std::string_view, SettingValue>;
static SettingsMap mSettings;
static std::string GetSettingString(std::string_view Name);
static int GetSettingInt(std::string_view Name);
static bool GetSettingBool(std::string_view Name);
static void SetSetting(std::string_view Name, const SettingValue& value);
using TShutdownHandler = std::function<void()>;
@@ -80,12 +118,10 @@ public:
static TConsole& Console() { return *mConsole; }
static std::string ServerVersionString();
static const Version& ServerVersion() { return mVersion; }
static std::string ClientVersionString() { return "2.0"; }
static uint8_t ClientMajorVersion() { return 2; }
static std::string PPS() { return mPPS; }
static void SetPPS(const std::string& NewPPS) { mPPS = NewPPS; }
static TSettings Settings;
static std::vector<std::string> GetBackendUrlsInOrder() {
return {
"backend.beammp.com",
@@ -125,6 +161,16 @@ public:
static void SetSubsystemStatus(const std::string& Subsystem, Status status);
static std::string SettingToString(const SettingValue& Value);
// Keeps track of how many packets we dropped on UDP due to fundamentally being malformed
static inline std::atomic_size_t MalformedUdpPackets { 0 };
// Keeps track of how many packets we dropped on UDP due to
// 1) not having a valid (known) player id
// 2) player disconnecting
// 3) packet failing to parse
static inline std::atomic_size_t InvalidUdpPackets { 0 };
private:
static void SetShutdown(bool Val);
@@ -137,7 +183,7 @@ private:
static inline std::mutex mShutdownHandlersMutex {};
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
static inline Version mVersion { 3, 1, 0 };
static inline Version mVersion { 3, 2, 0 };
};
std::string ThreadName(bool DebugModeOverride = false);
@@ -147,6 +193,7 @@ void RegisterThread(const std::string& str);
#define KB 1024llu
#define MB (KB * 1024llu)
#define GB (MB * 1024llu)
#define TB (GB * 1024llu)
#define SSU_UNRAW SECRET_SENTRY_URL
#define _file_basename std::filesystem::path(__FILE__).filename().string()
@@ -187,41 +234,88 @@ void RegisterThread(const std::string& str);
#endif // defined(DEBUG)
#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 { \
Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \
Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \
#define beammp_internal_error(x) Application::Console().Write(_this_location + std::string("[INTERNAL ERROR] ") + (x))
#define beammp_warn(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[WARN] ") + (x)); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define beammp_lua_error(x) \
do { \
Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \
#define beammp_info(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[INFO] ") + (x)); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define beammp_lua_warn(x) \
do { \
Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \
#define beammp_error(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[ERROR] ") + (x)); \
Sentry.AddErrorBreadcrumb((x), _file_basename, _line); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
#define beammp_debug(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
} \
#define beammp_lua_error(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[LUA ERROR] ") + (x)); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define beammp_event(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \
} \
#define beammp_lua_warn(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[LUA WARN] ") + (x)); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define luaprint(x) \
do { \
try{ \
Application::Console().Write(_this_location + std::string("[LUA] ") + (x)); \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define beammp_debug(x) \
do { \
try{ \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
} \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
#define beammp_event(x) \
do { \
try{ \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[EVENT] ") + (x)); \
} \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}': {}", #x, e.what()));\
} \
} while (false)
// trace() is a debug-build debug()
#if defined(DEBUG)
#define beammp_trace(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \
} \
#define beammp_trace(x) \
do { \
try{ \
if (Application::GetSettingBool("Debug")) { \
Application::Console().Write(_this_location + std::string("[TRACE] ") + (x)); \
} \
} catch (const std::exception& e) { \
beammp_internal_error(fmt::format("Exception in logging function, failed to print '{}'", #x)); \
} \
} while (false)
#else
#define beammp_trace(x)
@@ -321,3 +415,5 @@ inline T DeComp(const T& Compressed) {
std::string GetPlatformAgnosticErrorString();
#define S_DSN SU_RAW
std::string ToHumanReadableSize(size_t Size);

View File

@@ -5,54 +5,23 @@
// ======================= UNIX ========================
#ifdef BEAMMP_LINUX
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <termios.h>
#include <unistd.h>
using SOCKET = int;
using DWORD = unsigned long;
using PDWORD = unsigned long*;
using LPDWORD = unsigned long*;
char _getch();
inline void CloseSocketProper(int TheSocket) {
shutdown(TheSocket, SHUT_RDWR);
close(TheSocket);
}
#endif // unix
// ======================= APPLE ========================
#ifdef BEAMMP_APPLE
#include <arpa/inet.h>
#include <errno.h>
#include <sys/socket.h>
#include <termios.h>
#include <unistd.h>
using SOCKET = int;
using DWORD = unsigned long;
using PDWORD = unsigned long*;
using LPDWORD = unsigned long*;
char _getch();
inline void CloseSocketProper(int TheSocket) {
shutdown(TheSocket, SHUT_RDWR);
close(TheSocket);
}
#endif // unix
// ======================= WINDOWS =======================
#ifdef BEAMMP_WINDOWS
#include <conio.h>
#include <winsock2.h>
inline void CloseSocketProper(SOCKET TheSocket) {
shutdown(TheSocket, 2); // 2 == SD_BOTH
closesocket(TheSocket);
}
#endif // WIN32
#ifdef INVALID_SOCKET
static inline constexpr int BEAMMP_INVALID_SOCKET = INVALID_SOCKET;
#else
static inline constexpr int BEAMMP_INVALID_SOCKET = -1;
#endif

View File

@@ -66,9 +66,9 @@ inline void _assert([[maybe_unused]] const char* file, [[maybe_unused]] const ch
Sentry.LogAssert(#cond, _file_basename, _line, __func__); \
} \
} while (false)
#define beammp_assert_not_reachable() \
do { \
#define beammp_assert_not_reachable() \
do { \
beammp_errorf("Assertion failed in '{}:{}': Unreachable code reached. This may result in a crash or undefined state of the program.", __func__, _line); \
Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \
Sentry.LogAssert("code is unreachable", _file_basename, _line, __func__); \
} while (false)
#endif // DEBUG

View File

@@ -0,0 +1,6 @@
#pragma once
enum IterationDecision {
Continue,
Break,
};

View File

@@ -3,7 +3,7 @@
//
#pragma once
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"

View File

@@ -6,8 +6,8 @@
* and write locks and read locks are mutually exclusive.
*/
#include <shared_mutex>
#include <mutex>
#include <shared_mutex>
// Use ReadLock(m) and WriteLock(m) to lock it.
using RWMutex = std::shared_mutex;

View File

@@ -22,9 +22,7 @@ private:
void CreateConfigFile();
void ParseFromFile(std::string_view name);
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, bool& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, int& OutValue);
void TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key);
void ParseOldFormat();
bool IsDefault();

View File

@@ -36,6 +36,12 @@ private:
void Command_Status(const std::string& cmd, const std::vector<std::string>& args);
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_Debug(const std::string&, const std::vector<std::string>& args);
void Command_Version(const std::string&, const std::vector<std::string>& args);
void Autocomplete_Lua(const std::string&, std::vector<std::string>& suggestions);
void Autocomplete_Kick(const std::string&, std::vector<std::string>& suggestions);
void Autocomplete_Settings(const std::string&, std::vector<std::string>& suggestions);
void Command_Say(const std::string& FullCommand);
bool EnsureArgsCount(const std::vector<std::string>& args, size_t n);
@@ -52,9 +58,17 @@ private:
{ "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); } },
{ "debug", [this](const auto& a, const auto& b) { Command_Debug(a, b); } },
{ "version", [this](const auto& a, const auto& b) { Command_Version(a, b); } },
{ "say", [this](const auto&, const auto&) { Command_Say(""); } }, // shouldn't actually be called
};
std::unordered_map<std::string, std::function<void(const std::string&, std::vector<std::string>&)>> mCommandAutocompleteMap = {
{ "lua", [this](const auto& a, auto& b) { Autocomplete_Lua(a, b); } },
{ "kick", [this](const auto& a, auto& b) { Autocomplete_Kick(a, b); } },
{ "settings", [this](const auto& a, auto& b) { Autocomplete_Settings(a, b); } },
};
Commandline mCommandline;
std::vector<std::string> mCachedLuaHistory;
std::vector<std::string> mCachedRegularHistory;

View File

@@ -10,6 +10,7 @@
#include <lua.hpp>
#include <memory>
#include <mutex>
#include <nlohmann/json.hpp>
#include <queue>
#include <random>
#include <set>
@@ -18,18 +19,40 @@
#include <vector>
#define SOL_ALL_SAFETIES_ON 1
#define SOL_USER_C_ASSERT SOL_ON
#define SOL_C_ASSERT(...) \
do { \
if (!(__VA_ARGS__)) { \
beammp_lua_errorf("SOL2 assertion failure: Assertion `{}` failed in {}:{}. This *should* be a fatal error, but BeamMP Server overrides it to not be fatal. This may cause the Lua Engine to crash, or cause other issues.", #__VA_ARGS__, __FILE__, __LINE__); \
std::abort(); \
} \
} while (0)
#include <sol/sol.hpp>
struct JsonString {
std::string value;
};
// value used to keep nils in a table or array, across serialization boundaries like
// JsonEncode, so that the nil stays at the same index and isn't treated like a special
// value (e.g. one that can be ignored or discarded).
const inline std::string BEAMMP_INTERNAL_NIL = "BEAMMP_SERVER_INTERNAL_NIL_VALUE";
using TLuaStateId = std::string;
namespace fs = std::filesystem;
/**
* std::variant means, that TLuaArgTypes may be one of the Types listed as template args
*/
using TLuaArgTypes = std::variant<std::string, int, sol::variadic_args, bool>;
static constexpr size_t TLuaArgTypes_String = 0;
static constexpr size_t TLuaArgTypes_Int = 1;
static constexpr size_t TLuaArgTypes_VariadicArgs = 2;
static constexpr size_t TLuaArgTypes_Bool = 3;
using TLuaValue = std::variant<std::string, int, JsonString, bool, std::unordered_map<std::string, std::string>, std::unordered_map<std::string, size_t>, float>;
enum TLuaType {
String = 0,
Int = 1,
Json = 2,
Bool = 3,
StringStringMap = 4,
StringSizeTMap = 5,
Float = 6,
};
class TLuaPlugin;
@@ -70,12 +93,12 @@ public:
struct QueuedFunction {
std::string FunctionName;
std::shared_ptr<TLuaResult> Result;
std::vector<TLuaArgTypes> Args;
std::vector<TLuaValue> Args;
std::string EventName; // optional, may be empty
};
TLuaEngine();
~TLuaEngine() noexcept {
virtual ~TLuaEngine() noexcept {
beammp_debug("Lua Engine terminated");
}
@@ -97,8 +120,8 @@ public:
return mLuaStates.size();
}
std::vector<std::string> GetLuaStateNames() {
std::vector<std::string> names{};
for(auto const& [stateId, _ ] : mLuaStates) {
std::vector<std::string> names {};
for (auto const& [stateId, _] : mLuaStates) {
names.push_back(stateId);
}
return names;
@@ -119,11 +142,11 @@ public:
}
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results,
const std::optional<std::chrono::high_resolution_clock::duration>& Max = std::nullopt);
const std::optional<TimeType::duration>& Max = std::nullopt);
void ReportErrors(const std::vector<std::shared_ptr<TLuaResult>>& Results);
bool HasState(TLuaStateId StateId);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
[[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<TLuaValue>& Args);
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
void RegisterEvent(const std::string& EventName, TLuaStateId StateId, const std::string& FunctionName);
/**
@@ -143,7 +166,7 @@ public:
}
std::vector<std::shared_ptr<TLuaResult>> Results;
std::vector<TLuaArgTypes> Arguments { TLuaArgTypes { std::forward<ArgsT>(Args) }... };
std::vector<TLuaValue> Arguments { TLuaValue { std::forward<ArgsT>(Args) }... };
for (const auto& Event : mLuaEvents.at(EventName)) {
for (const auto& Function : Event.second) {
@@ -162,7 +185,7 @@ public:
return {};
}
std::vector<std::shared_ptr<TLuaResult>> Results;
std::vector<TLuaArgTypes> Arguments { TLuaArgTypes { std::forward<ArgsT>(Args) }... };
std::vector<TLuaValue> Arguments { TLuaValue { std::forward<ArgsT>(Args) }... };
const auto Handlers = GetEventHandlersForState(EventName, StateId);
for (const auto& Handler : Handlers) {
Results.push_back(EnqueueFunctionCall(StateId, Handler, Arguments));
@@ -197,10 +220,10 @@ private:
public:
StateThreadData(const std::string& Name, TLuaStateId StateId, TLuaEngine& Engine);
StateThreadData(const StateThreadData&) = delete;
~StateThreadData() noexcept { beammp_debug("\"" + mStateId + "\" destroyed"); }
virtual ~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);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaValue>& Args);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaValue>& 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;
@@ -242,11 +265,12 @@ private:
std::recursive_mutex mPathsMutex;
std::mt19937 mMersenneTwister;
std::uniform_real_distribution<double> mUniformRealDistribution01;
std::vector<sol::object> JsonStringToArray(JsonString Str);
};
struct TimedEvent {
std::chrono::high_resolution_clock::duration Duration {};
std::chrono::high_resolution_clock::time_point LastCompletion {};
TimeType::duration Duration {};
TimeType::time_point LastCompletion {};
std::string EventName;
TLuaStateId StateId;
CallStrategy Strategy;

View File

@@ -1,8 +1,11 @@
#pragma once
#include "BoostAliases.h"
#include "Compat.h"
#include "TResourceManager.h"
#include "TServer.h"
#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/udp.hpp>
struct TConnection;
@@ -10,19 +13,18 @@ class TNetwork {
public:
TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
[[nodiscard]] bool TCPSend(TClient& c, const std::string& Data, bool IsSync = false);
[[nodiscard]] bool SendLarge(TClient& c, std::string Data, bool isSync = false);
[[nodiscard]] bool Respond(TClient& c, const std::string& MSG, bool Rel, bool isSync = false);
std::shared_ptr<TClient> CreateClient(SOCKET TCPSock);
std::string TCPRcv(TClient& c);
[[nodiscard]] bool TCPSend(TClient& c, const std::vector<uint8_t>& Data, bool IsSync = false);
[[nodiscard]] bool SendLarge(TClient& c, std::vector<uint8_t> Data, bool isSync = false);
[[nodiscard]] bool Respond(TClient& c, const std::vector<uint8_t>& MSG, bool Rel, bool isSync = false);
std::shared_ptr<TClient> CreateClient(ip::tcp::socket&& TCPSock);
std::vector<uint8_t> TCPRcv(TClient& c);
void ClientKick(TClient& c, const std::string& R);
[[nodiscard]] bool SyncClient(const std::weak_ptr<TClient>& c);
void Identify(const TConnection& client);
void Authentication(const TConnection& ClientConnection);
[[nodiscard]] bool CheckBytes(TClient& c, int32_t BytesRcv);
void Identify(TConnection&& client);
std::shared_ptr<TClient> Authentication(TConnection&& ClientConnection);
void SyncResources(TClient& c);
[[nodiscard]] bool UDPSend(TClient& Client, std::string Data) const;
void SendToAll(TClient* c, const std::string& Data, bool Self, bool Rel);
[[nodiscard]] bool UDPSend(TClient& Client, std::vector<uint8_t> Data);
void SendToAll(TClient* c, const std::vector<uint8_t>& Data, bool Self, bool Rel);
void UpdatePlayer(TClient& Client);
private:
@@ -31,21 +33,25 @@ private:
TServer& mServer;
TPPSMonitor& mPPSMonitor;
SOCKET mUDPSock {};
ip::udp::socket mUDPSock;
TResourceManager& mResourceManager;
std::thread mUDPThread;
std::thread mTCPThread;
std::mutex mOpenIDMutex;
std::string UDPRcvFromClient(sockaddr_in& client) const;
void HandleDownload(SOCKET TCPSock);
std::vector<uint8_t> UDPRcvFromClient(ip::udp::endpoint& ClientEndpoint);
void HandleDownload(TConnection&& TCPSock);
void OnConnect(const std::weak_ptr<TClient>& c);
void TCPClient(const std::weak_ptr<TClient>& c);
void Looper(const std::weak_ptr<TClient>& c);
int OpenID();
void OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked);
void Parse(TClient& c, const std::string& Packet);
void OnDisconnect(const std::weak_ptr<TClient>& ClientPtr);
ModMap GetClientMods(TClient& Client);
void HandleResourcePackets(TClient& c, const std::vector<uint8_t>& Packet);
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, ip::tcp::socket& socket, const uint8_t* Data, size_t Size);
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);
static const uint8_t* SendSplit(TClient& c, ip::tcp::socket& Socket, const uint8_t* DataPtr, size_t Size);
};
std::vector<uint8_t> StringToVector(const std::string& Str);

View File

@@ -1,21 +1,22 @@
#pragma once
#include "Common.h"
#include <optional>
using ModMap = HashMap<std::string, size_t>;
class TResourceManager {
public:
TResourceManager();
[[nodiscard]] size_t MaxModSize() const { return mMaxModSize; }
[[nodiscard]] std::string FileList() const { return mFileList; }
[[nodiscard]] std::string TrimmedList() const { return mTrimmedList; }
[[nodiscard]] std::string FileSizes() const { return mFileSizes; }
[[nodiscard]] int ModsLoaded() const { return mModsLoaded; }
[[nodiscard]] size_t TotalModsSize() const { return mTotalModSize; }
[[nodiscard]] ModMap FileMap() const { return mMods; }
[[nodiscard]] static std::string FormatForBackend(const ModMap& mods);
[[nodiscard]] static std::string FormatForClient(const ModMap& mods);
[[nodiscard]] static std::optional<std::string> IsModValid(std::string& pathString, const ModMap& mods);
[[nodiscard]] int LoadedModCount() const { return mMods.size(); }
private:
size_t mMaxModSize = 0;
std::string mFileSizes;
std::string mFileList;
std::string mTrimmedList;
int mModsLoaded = 0;
size_t mTotalModSize = 0; // size of all mods
ModMap mMods; // map of mod names
};

View File

@@ -4,6 +4,8 @@
#include <functional>
#include <string>
#include "Common.h"
class TScopedTimer {
public:
TScopedTimer();
@@ -11,7 +13,7 @@ public:
TScopedTimer(std::function<void(size_t)> OnDestroy);
~TScopedTimer();
auto GetElapsedTime() const {
auto EndTime = std::chrono::high_resolution_clock::now();
auto EndTime = TimeType::now();
auto Delta = EndTime - mStartTime;
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
return TimeDelta;
@@ -20,6 +22,6 @@ public:
std::function<void(size_t /* time_ms */)> OnDestroy { nullptr };
private:
std::chrono::high_resolution_clock::time_point mStartTime;
TimeType::time_point mStartTime;
std::string Name;
};

View File

@@ -1,17 +1,37 @@
#pragma once
#include "IThreaded.h"
#include "IterationDecision.h"
#include "RWMutex.h"
#include "TScopedTimer.h"
#include <concepts>
#include <functional>
#include <memory>
#include <mutex>
#include <type_traits>
#include <unordered_set>
#include "BoostAliases.h"
class TClient;
class TNetwork;
class TPPSMonitor;
// clang-format doesn't know how to deal with concepts
// clang-format off
template <typename FnT>
concept ForEachHandlerWithDecision = requires(FnT Fn, const std::shared_ptr<TClient>& Ptr) {
requires std::invocable<FnT, const std::shared_ptr<TClient>&> ;
{ std::invoke(Fn, Ptr) } -> std::convertible_to<IterationDecision>;
};
template <typename FnT>
concept ForEachHandler = requires(FnT Fn, const std::shared_ptr<TClient>& Ptr) {
requires std::invocable <FnT, const std::shared_ptr<TClient>&> ;
{ std::invoke(Fn, Ptr) } -> std::same_as<void>;
};
// clang-format on
class TServer final {
public:
using TClientSet = std::unordered_set<std::shared_ptr<TClient>>;
@@ -19,24 +39,71 @@ public:
TServer(const std::vector<std::string_view>& Arguments);
void InsertClient(const std::shared_ptr<TClient>& Ptr);
std::weak_ptr<TClient> InsertNewClient();
void RemoveClient(const std::weak_ptr<TClient>&);
void RemoveClientById(int Id);
// in Fn, return true to continue, return false to break
void ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
[[deprecated("use ForEachClient instead")]] void ForEachClientWeak(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
// in Fn, return Break or Continue
template <ForEachHandlerWithDecision FnT>
void ForEachClient(FnT Fn) {
decltype(mClients) Clients;
{
ReadLock lock(mClientsMutex);
Clients = mClients;
}
for (auto& Client : Clients) {
if (Client) [[likely]] {
IterationDecision Decision = std::invoke(Fn, Client);
if (Decision == IterationDecision::Break) {
break;
}
} else {
beammp_assert_not_reachable();
}
}
}
template <ForEachHandler FnT>
void ForEachClient(FnT Fn) {
decltype(mClients) Clients;
{
ReadLock lock(mClientsMutex);
Clients = mClients;
}
for (auto& Client : Clients) {
if (Client) [[likely]] {
std::invoke(Fn, Client);
} else {
beammp_assert_not_reachable();
}
}
}
size_t ClientCount() const;
static void GlobalParser(const std::weak_ptr<TClient>& Client, std::string Packet, TPPSMonitor& PPSMonitor, TNetwork& Network);
static void GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TNetwork& Network);
static void HandleEvent(TClient& c, const std::string& Data);
RWMutex& GetClientMutex() const { return mClientsMutex; }
const TScopedTimer UptimeTimer;
// asio io context
io_context& IoCtx() { return mIoCtx; }
private:
io_context mIoCtx {};
TClientSet mClients;
mutable RWMutex mClientsMutex;
static void ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network);
static bool ShouldSpawn(TClient& c, const std::string& CarJson, int ID);
static bool IsUnicycle(TClient& c, const std::string& CarJson);
static void Apply(TClient& c, int VID, const std::string& pckt);
static void HandlePosition(TClient& c, const std::string& Packet);
static bool HandlePosition(TClient& c, const std::string& PacketStr);
static bool HandleVehicleUpdate(const std::string& PacketStr, const int playerID);
};
struct BufferView {
uint8_t* Data { nullptr };
size_t Size { 0 };
const uint8_t* data() const { return Data; }
uint8_t* data() { return Data; }
size_t size() const { return Size; }
};

9
include/Uuid.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <string>
namespace uuid {
std::string GenerateUuid();
}

View File

@@ -0,0 +1,3 @@
FROM archlinux
RUN pacman -Syu --noconfirm lua53 openssl curl git cmake gcc make zlib boost websocketpp

View File

@@ -0,0 +1,9 @@
FROM debian:11
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y git cmake g++-10 curl libboost1.74-all-dev libssl-dev libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev
ENV CXX=g++-10

View File

@@ -0,0 +1,9 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev git make cmake g++
RUN apt-get install -y libboost1.71-all-dev

View File

@@ -0,0 +1,9 @@
FROM ubuntu:22.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y
RUN apt-get install -y git libz-dev rapidjson-dev liblua5.3 libssl-dev libwebsocketpp-dev libcurl4-openssl-dev cmake g++-10 libboost1.74-all-dev libssl3 curl
ENV CXX=g++-10

18
scripts/build-all.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/sh
set -e
printf "enter DSN (optional): "
read DSN
docker build -f Ubuntu-20.04-Dockerfile . -t beammp-server-build:Ubuntu-20.04
docker build -f Ubuntu-22.04-Dockerfile . -t beammp-server-build:Ubuntu-22.04
docker build -f ArchLinux-Dockerfile . -t beammp-server-build:ArchLinux
docker build -f Debian-11-Dockerfile . -t beammp-server-build:Debian-11
CMD="cd /beammp; cmake . -DGIT_SUBMODULE=OFF -DCMAKE_BUILD_TYPE=Release -DBEAMMP_SECRET_SENTRY_URL=\"${DSN}\" -B /build && make -j -C /build BeamMP-Server"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-ubuntu-20.04:/build -it --rm beammp-server-build:Ubuntu-20.04 bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-ubuntu-22.04:/build -it --rm beammp-server-build:Ubuntu-22.04 bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-archlinux:/build -it --rm beammp-server-build:ArchLinux bash -c "${CMD}"
docker run -v $(pwd)/..:/beammp -v $(pwd)/../build-debian-11:/build -it --rm beammp-server-build:Debian-11 bash -c "${CMD}"

5
scripts/install.sh Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/sh
cmake . -B build -DCMAKE_BUILD_TYPE=Release -DGIT_SUBMODULE=OFF
make -j -C build BeamMP-Server

View File

@@ -13,7 +13,7 @@ void ArgsParser::Parse(const std::vector<std::string_view>& ArgList) {
ConsumeLongFlag(std::string(Arg));
}
} else {
beammp_errorf("Error parsing commandline arguments: Supplied argument '{}' is not a valid argument and was ignored.", Arg);
fmt::print(stderr, "Error parsing commandline arguments: Supplied argument '{}' is not a valid argument and was ignored.", Arg);
}
}
}
@@ -22,17 +22,17 @@ bool ArgsParser::Verify() {
bool Ok = true;
for (const auto& RegisteredArg : mRegisteredArguments) {
if (RegisteredArg.Flags & Flags::REQUIRED && !FoundArgument(RegisteredArg.Names)) {
beammp_errorf("Error in commandline arguments: Argument '{}' is required but wasn't found.", RegisteredArg.Names.at(0));
fmt::print(stderr, "Error in commandline arguments: Argument '{}' is required but wasn't found.", RegisteredArg.Names.at(0));
Ok = false;
continue;
} else if (FoundArgument(RegisteredArg.Names)) {
if (RegisteredArg.Flags & Flags::HAS_VALUE) {
if (!GetValueOfArgument(RegisteredArg.Names).has_value()) {
beammp_error("Error in commandline arguments: Argument '" + std::string(RegisteredArg.Names.at(0)) + "' expects a value, but no value was given.");
fmt::print(stderr, "Error in commandline arguments: Argument '{}' expects a value, but no value was given.", RegisteredArg.Names.at(0));
Ok = false;
}
} else if (GetValueOfArgument(RegisteredArg.Names).has_value()) {
beammp_error("Error in commandline arguments: Argument '" + std::string(RegisteredArg.Names.at(0)) + "' does not expect a value, but one was given.");
fmt::print(stderr, "Error in commandline arguments: Argument '{}' does not expect a value, but one was given.", RegisteredArg.Names.at(0));
Ok = false;
}
}
@@ -81,7 +81,7 @@ void ArgsParser::ConsumeLongAssignment(const std::string& Arg) {
auto Value = Arg.substr(Arg.rfind("=") + 1);
auto Name = Arg.substr(2, Arg.rfind("=") - 2);
if (!IsRegistered(Name)) {
beammp_warn("Argument '" + Name + "' was supplied but isn't a known argument, so it is likely being ignored.");
fmt::print(stdout, "Argument '{}' was supplied but isn't a known argument, so it is likely being ignored.", Name);
}
mFoundArgs.push_back({ Name, Value });
}
@@ -90,7 +90,7 @@ void ArgsParser::ConsumeLongFlag(const std::string& Arg) {
auto Name = Arg.substr(2, Arg.rfind("=") - 2);
mFoundArgs.push_back({ Name, std::nullopt });
if (!IsRegistered(Name)) {
beammp_warn("Argument '" + Name + "' was supplied but isn't a known argument, so it is likely being ignored.");
fmt::print(stdout, "Argument '{}' was supplied but isn't a known argument, so it is likely being ignored.", Name);
}
}

View File

@@ -1,16 +1,20 @@
#include "Client.h"
#include "CustomAssert.h"
#include "LuaAPI.h"
#include "TServer.h"
#include <memory>
#include <optional>
void TClient::DeleteCar(int Ident) {
// TODO: Send delete packets
std::unique_lock lock(mVehicleDataMutex);
auto iter = std::find_if(mVehicleData.begin(), mVehicleData.end(), [&](auto& elem) {
return Ident == elem.ID();
});
if (iter != mVehicleData.end()) {
std::string Destroy = "Od:" + std::to_string(GetID()) + "-" + std::to_string(iter->ID());
LuaAPI::MP::Engine->Network().SendToAll(nullptr, StringToVector(Destroy), true, true);
mVehicleData.erase(iter);
} else {
beammp_debug("tried to erase a vehicle that doesn't exist (not an error)");
@@ -19,6 +23,10 @@ void TClient::DeleteCar(int Ident) {
void TClient::ClearCars() {
std::unique_lock lock(mVehicleDataMutex);
for (const auto& Car : mVehicleData) {
std::string Destroy = "Od:" + std::to_string(GetID()) + "-" + std::to_string(Car.ID());
LuaAPI::MP::Engine->Network().SendToAll(this, StringToVector(Destroy), false, true);
}
mVehicleData.clear();
}
@@ -49,16 +57,33 @@ TClient::TVehicleDataLockPair TClient::GetAllCars() {
std::string TClient::GetCarPositionRaw(int Ident) {
std::unique_lock lock(mVehiclePositionMutex);
try
{
try {
return mVehiclePosition.at(Ident);
}
catch (const std::out_of_range& oor) {
} catch (const std::out_of_range& oor) {
return "";
}
return "";
}
void TClient::Disconnect(std::string_view Reason) {
// we need exclusivity here in case we can't
std::unique_lock Lock(mDisconnectMtx);
beammp_debugf("Disconnecting client {} for reason: {}", GetID(), Reason);
if (mSocket.is_open()) {
beammp_debugf("Socket for client {} already closed", GetID());
}
boost::system::error_code ec;
mSocket.shutdown(socket_base::shutdown_both, ec);
if (ec) {
beammp_debugf("Failed to shutdown client socket: {}", ec.message());
}
mSocket.close(ec);
if (ec) {
beammp_debugf("Failed to close client socket: {}", ec.message());
}
mServer.RemoveClientById(GetID());
}
void TClient::SetCarPosition(int Ident, const std::string& Data) {
std::unique_lock lock(mVehiclePositionMutex);
mVehiclePosition[Ident] = Data;
@@ -98,38 +123,40 @@ TServer& TClient::Server() const {
return mServer;
}
void TClient::EnqueuePacket(const std::string& Packet) {
void TClient::EnqueuePacket(const std::vector<uint8_t>& Packet) {
std::unique_lock Lock(mMissedPacketsMutex);
mPacketsSync.push(Packet);
}
TClient::TClient(TServer& Server)
TClient::TClient(TServer& Server, ip::tcp::socket&& Socket)
: mServer(Server)
, mLastPingTime(std::chrono::high_resolution_clock::now()) {
, mSocket(std::move(Socket))
, mDownSocket(ip::tcp::socket(Server.IoCtx()))
, mLastPingTime(TimeType::now()) {
}
TClient::~TClient() {
beammp_debugf("client destroyed: {} ('{}')", this->GetID(), this->GetName());
}
void TClient::UpdatePingTime() {
mLastPingTime = std::chrono::high_resolution_clock::now();
mLastPingTime = TimeType::now();
}
int TClient::SecondsSinceLastPing() {
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::high_resolution_clock::now() - mLastPingTime)
TimeType::now() - mLastPingTime)
.count();
return int(seconds);
}
std::optional<std::weak_ptr<TClient>> GetClient(TServer& Server, int ID) {
std::optional<std::weak_ptr<TClient>> MaybeClient { std::nullopt };
Server.ForEachClient([&](std::weak_ptr<TClient> CPtr) -> bool {
ReadLock Lock(Server.GetClientMutex());
if (!CPtr.expired()) {
auto C = CPtr.lock();
if (C->GetID() == ID) {
MaybeClient = CPtr;
return false;
}
std::shared_ptr<TClient> GetClient(TServer& Server, int ID) {
std::shared_ptr<TClient> Result {};
Server.ForEachClient([&](const auto& Client) {
if (Client->GetID() == ID) {
Result = Client;
return Break;
}
return true;
return Continue;
});
return MaybeClient;
return Result;
}

View File

@@ -13,10 +13,85 @@
#include "CustomAssert.h"
#include "Http.h"
Application::SettingsMap Application::mSettings = {
{ StrName, std::string("BeamMP Server") },
{ StrDescription, std::string("No description") },
{ StrResourceFolder, std::string("Resources") },
{ StrMap, std::string("/levels/gridmap_v2/info.json") },
{ StrSSLKeyPath, std::string("./.ssl/HttpServer/key.pem") },
{ StrSSLCertPath, std::string("./.ssl/HttpServer/cert.pem") },
{ StrHTTPServerEnabled, false },
{ StrMaxPlayers, int(8) },
{ StrPrivate, true },
{ StrMaxCars, int(1) },
{ StrDebug, false },
{ StrPort, int(30814) },
{ StrCustomIP, std::string("") },
{ StrLogChat, true },
{ StrSendErrors, true },
{ StrSendErrorsMessageEnabled, true },
{ StrHTTPServerPort, int(8080) },
{ StrHTTPServerIP, std::string("127.0.0.1") },
{ StrHTTPServerUseSSL, false },
{ StrHideUpdateMessages, false },
{ StrAuthKey, std::string("") },
{ StrIncludeSubdirectories, false },
};
// global, yes, this is ugly, no, it cant be done another way
TSentry Sentry {};
Application::TSettings Application::Settings = {};
std::string Application::SettingToString(const Application::SettingValue& Value) {
switch (Value.index()) {
case 0:
return fmt::format("{}", std::get<std::string>(Value));
case 1:
return fmt::format("{}", std::get<bool>(Value));
case 2:
return fmt::format("{}", std::get<int>(Value));
default:
return "<unknown type>";
}
}
std::string Application::GetSettingString(std::string_view Name) {
try {
return std::get<std::string>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get string setting '{}': {}", Name, e.what());
return "";
}
}
int Application::GetSettingInt(std::string_view Name) {
try {
return std::get<int>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get int setting '{}': {}", Name, e.what());
return 0;
}
}
bool Application::GetSettingBool(std::string_view Name) {
try {
return std::get<bool>(Application::mSettings.at(Name));
} catch (const std::exception& e) {
beammp_errorf("Failed to get bool setting '{}': {}", Name, e.what());
return false;
}
}
void Application::SetSetting(std::string_view Name, const Application::SettingValue& Value) {
if (mSettings.contains(Name)) {
if (mSettings[Name].index() == Value.index()) {
mSettings[Name] = Value;
} else {
beammp_errorf("Could not change value of setting '{}', because it has a different type.", Name);
}
} else {
mSettings[Name] = Value;
}
}
void Application::RegisterShutdownHandler(const TShutdownHandler& Handler) {
std::unique_lock Lock(mShutdownHandlersMutex);
@@ -239,7 +314,7 @@ static std::mutex ThreadNameMapMutex {};
std::string ThreadName(bool DebugModeOverride) {
auto Lock = std::unique_lock(ThreadNameMapMutex);
if (DebugModeOverride || Application::Settings.DebugModeEnabled) {
if (DebugModeOverride || Application::GetSettingBool(StrDebug)) {
auto id = std::this_thread::get_id();
if (threadNameMap.find(id) != threadNameMap.end()) {
// found
@@ -251,21 +326,21 @@ std::string ThreadName(bool DebugModeOverride) {
TEST_CASE("ThreadName") {
RegisterThread("MyThread");
auto OrigDebug = Application::Settings.DebugModeEnabled;
auto OrigDebug = Application::GetSettingBool(StrDebug);
// ThreadName adds a space at the end, legacy but we need it still
SUBCASE("Debug mode enabled") {
Application::Settings.DebugModeEnabled = true;
Application::SetSetting(StrDebug, true);
CHECK(ThreadName(true) == "MyThread ");
CHECK(ThreadName(false) == "MyThread ");
}
SUBCASE("Debug mode disabled") {
Application::Settings.DebugModeEnabled = false;
Application::SetSetting(StrDebug, false);
CHECK(ThreadName(true) == "MyThread ");
CHECK(ThreadName(false) == "");
}
// cleanup
Application::Settings.DebugModeEnabled = OrigDebug;
Application::SetSetting(StrDebug, OrigDebug);
}
void RegisterThread(const std::string& str) {
@@ -277,7 +352,7 @@ void RegisterThread(const std::string& str) {
#elif defined(BEAMMP_LINUX)
ThreadId = std::to_string(gettid());
#endif
if (Application::Settings.DebugModeEnabled) {
if (Application::GetSettingBool(StrDebug)) {
std::ofstream ThreadFile(".Threads.log", std::ios::app);
ThreadFile << ("Thread \"" + str + "\" is TID " + ThreadId) << std::endl;
}
@@ -310,7 +385,7 @@ TEST_CASE("Version::AsString") {
}
void LogChatMessage(const std::string& name, int id, const std::string& msg) {
if (Application::Settings.LogChat) {
if (Application::GetSettingBool(StrLogChat)) {
std::stringstream ss;
ss << ThreadName();
ss << "[CHAT] ";
@@ -354,3 +429,17 @@ std::string GetPlatformAgnosticErrorString() {
return "(no human-readable errors on this platform)";
#endif
}
std::string ToHumanReadableSize(size_t Size) {
if (Size > TB) {
return fmt::format("{:.2f} TiB", double(Size) / TB);
} else if (Size > GB) {
return fmt::format("{:.2f} GiB", double(Size) / GB);
} else if (Size > MB) {
return fmt::format("{:.2f} MiB", double(Size) / MB);
} else if (Size > KB) {
return fmt::format("{:.2f} KiB", double(Size) / KB);
} else {
return fmt::format("{} B", Size);
}
}

View File

@@ -154,7 +154,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
}
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::GetSettingInt(StrHTTPServerPort)));
std::unique_ptr<httplib::Server> HttpLibServerInstance;
HttpLibServerInstance = std::make_unique<httplib::Server>();
// todo: make this IP agnostic so people can set their own IP
@@ -196,7 +196,7 @@ void Http::Server::THttpServerInstance::operator()() try {
beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status));
});
Application::SetSubsystemStatus("HTTPServer", Application::Status::Good);
auto ret = HttpLibServerInstance->listen(Application::Settings.HTTPServerIP.c_str(), Application::Settings.HTTPServerPort);
auto ret = HttpLibServerInstance->listen(Application::GetSettingString(StrHTTPServerIP).c_str(), Application::GetSettingInt(StrHTTPServerPort));
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.");
}

View File

@@ -116,18 +116,17 @@ TEST_CASE("LuaAPI::MP::GetServerVersion") {
static inline std::pair<bool, std::string> InternalTriggerClientEvent(int PlayerID, const std::string& EventName, const std::string& Data) {
std::string Packet = "E:" + EventName + ":" + Data;
if (PlayerID == -1) {
LuaAPI::MP::Engine->Network().SendToAll(nullptr, Packet, true, true);
LuaAPI::MP::Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
return { true, "" };
} else {
auto MaybeClient = GetClient(LuaAPI::MP::Engine->Server(), PlayerID);
if (!MaybeClient || MaybeClient.value().expired()) {
auto Client = GetClient(LuaAPI::MP::Engine->Server(), PlayerID);
if (!Client) {
beammp_lua_errorf("TriggerClientEvent invalid Player ID '{}'", PlayerID);
return { false, "Invalid Player ID" };
}
auto c = MaybeClient.value().lock();
if (!LuaAPI::MP::Engine->Network().Respond(*c, Packet, true)) {
if (!LuaAPI::MP::Engine->Network().Respond(*Client, StringToVector(Packet), true)) {
beammp_lua_errorf("Respond failed, dropping client {}", PlayerID);
LuaAPI::MP::Engine->Network().ClientKick(*c, "Disconnected after failing to receive packets");
LuaAPI::MP::Engine->Network().ClientKick(*Client, "Disconnected after failing to receive packets");
return { false, "Respond failed, dropping client" };
}
return { true, "" };
@@ -140,13 +139,12 @@ std::pair<bool, std::string> LuaAPI::MP::TriggerClientEvent(int PlayerID, const
}
std::pair<bool, std::string> LuaAPI::MP::DropPlayer(int ID, std::optional<std::string> MaybeReason) {
auto MaybeClient = GetClient(Engine->Server(), ID);
if (!MaybeClient || MaybeClient.value().expired()) {
auto Client = GetClient(Engine->Server(), ID);
if (!Client) {
beammp_lua_errorf("Tried to drop client with id {}, who doesn't exist", ID);
return { false, "Player does not exist" };
}
auto c = MaybeClient.value().lock();
LuaAPI::MP::Engine->Network().ClientKick(*c, MaybeReason.value_or("No reason"));
LuaAPI::MP::Engine->Network().ClientKick(*Client, MaybeReason.value_or("No reason"));
return { true, "" };
}
@@ -155,19 +153,18 @@ std::pair<bool, std::string> LuaAPI::MP::SendChatMessage(int ID, const std::stri
std::string Packet = "C:Server: " + Message;
if (ID == -1) {
LogChatMessage("<Server> (to everyone) ", -1, Message);
Engine->Network().SendToAll(nullptr, Packet, true, true);
Engine->Network().SendToAll(nullptr, StringToVector(Packet), true, true);
Result.first = true;
} else {
auto MaybeClient = GetClient(Engine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
auto c = MaybeClient.value().lock();
if (!c->IsSynced()) {
auto Client = GetClient(Engine->Server(), ID);
if (Client) {
if (!Client->IsSynced()) {
Result.first = false;
Result.second = "Player still syncing data";
return Result;
}
LogChatMessage("<Server> (to \"" + c->GetName() + "\")", -1, Message);
if (!Engine->Network().Respond(*c, Packet, true)) {
LogChatMessage("<Server> (to \"" + Client->GetName() + "\")", -1, Message);
if (!Engine->Network().Respond(*Client, StringToVector(Packet), true)) {
beammp_errorf("Failed to send chat message back to sender (id {}) - did the sender disconnect?", ID);
// TODO: should we return an error here?
}
@@ -184,18 +181,17 @@ std::pair<bool, std::string> LuaAPI::MP::SendChatMessage(int ID, const std::stri
std::pair<bool, std::string> LuaAPI::MP::RemoveVehicle(int PID, int VID) {
std::pair<bool, std::string> Result;
auto MaybeClient = GetClient(Engine->Server(), PID);
if (!MaybeClient || MaybeClient.value().expired()) {
auto Client = GetClient(Engine->Server(), PID);
if (!Client) {
beammp_lua_error("RemoveVehicle invalid Player ID");
Result.first = false;
Result.second = "Invalid Player ID";
return Result;
}
auto c = MaybeClient.value().lock();
if (!c->GetCarData(VID).empty()) {
if (!Client->GetCarData(VID).empty()) {
std::string Destroy = "Od:" + std::to_string(PID) + "-" + std::to_string(VID);
Engine->Network().SendToAll(nullptr, Destroy, true, true);
c->DeleteCar(VID);
Engine->Network().SendToAll(nullptr, StringToVector(Destroy), true, true);
Client->DeleteCar(VID);
Result.first = true;
} else {
Result.first = false;
@@ -208,56 +204,56 @@ void LuaAPI::MP::Set(int ConfigID, sol::object NewValue) {
switch (ConfigID) {
case 0: // debug
if (NewValue.is<bool>()) {
Application::Settings.DebugModeEnabled = NewValue.as<bool>();
beammp_info(std::string("Set `Debug` to ") + (Application::Settings.DebugModeEnabled ? "true" : "false"));
Application::SetSetting(StrDebug, NewValue.as<bool>());
beammp_info(std::string("Set `Debug` to ") + (Application::GetSettingBool(StrDebug) ? "true" : "false"));
} else {
beammp_lua_error("set invalid argument [2] expected boolean");
}
break;
case 1: // private
if (NewValue.is<bool>()) {
Application::Settings.Private = NewValue.as<bool>();
beammp_info(std::string("Set `Private` to ") + (Application::Settings.Private ? "true" : "false"));
Application::SetSetting(StrPrivate, NewValue.as<bool>());
beammp_info(std::string("Set `Private` to ") + (Application::GetSettingBool(StrPrivate) ? "true" : "false"));
} else {
beammp_lua_error("set invalid argument [2] expected boolean");
}
break;
case 2: // max cars
if (NewValue.is<int>()) {
Application::Settings.MaxCars = NewValue.as<int>();
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::Settings.MaxCars));
Application::SetSetting(StrMaxCars, NewValue.as<int>());
beammp_info(std::string("Set `MaxCars` to ") + std::to_string(Application::GetSettingInt(StrMaxCars)));
} else {
beammp_lua_error("set invalid argument [2] expected integer");
}
break;
case 3: // max players
if (NewValue.is<int>()) {
Application::Settings.MaxPlayers = NewValue.as<int>();
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::Settings.MaxPlayers));
Application::SetSetting(StrMaxPlayers, NewValue.as<int>());
beammp_info(std::string("Set `MaxPlayers` to ") + std::to_string(Application::GetSettingInt(StrMaxPlayers)));
} else {
beammp_lua_error("set invalid argument [2] expected integer");
}
break;
case 4: // Map
if (NewValue.is<std::string>()) {
Application::Settings.MapName = NewValue.as<std::string>();
beammp_info(std::string("Set `Map` to ") + Application::Settings.MapName);
Application::SetSetting(StrMap, NewValue.as<std::string>());
beammp_info(std::string("Set `Map` to ") + Application::GetSettingString(StrMap));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}
break;
case 5: // Name
if (NewValue.is<std::string>()) {
Application::Settings.ServerName = NewValue.as<std::string>();
beammp_info(std::string("Set `Name` to ") + Application::Settings.ServerName);
Application::SetSetting(StrName, NewValue.as<std::string>());
beammp_info(std::string("Set `Name` to ") + Application::GetSettingString(StrName));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}
break;
case 6: // Desc
if (NewValue.is<std::string>()) {
Application::Settings.ServerDesc = NewValue.as<std::string>();
beammp_info(std::string("Set `Description` to ") + Application::Settings.ServerDesc);
Application::SetSetting(StrDescription, NewValue.as<std::string>());
beammp_info(std::string("Set `Description` to ") + Application::GetSettingString(StrDescription));
} else {
beammp_lua_error("set invalid argument [2] expected string");
}
@@ -273,18 +269,18 @@ void LuaAPI::MP::Sleep(size_t Ms) {
}
bool LuaAPI::MP::IsPlayerConnected(int ID) {
auto MaybeClient = GetClient(Engine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
return MaybeClient.value().lock()->IsConnected();
auto Client = GetClient(Engine->Server(), ID);
if (Client) {
return Client->IsConnected();
} else {
return false;
}
}
bool LuaAPI::MP::IsPlayerGuest(int ID) {
auto MaybeClient = GetClient(Engine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
return MaybeClient.value().lock()->IsGuest();
auto Client = GetClient(Engine->Server(), ID);
if (Client) {
return Client->IsGuest();
} else {
return false;
}
@@ -302,6 +298,7 @@ void LuaAPI::MP::PrintRaw(sol::variadic_args Args) {
}
int LuaAPI::PanicHandler(lua_State* State) {
// FIXME: unsafe operation, can cause stack overflow
beammp_lua_error("PANIC: " + sol::stack::get<std::string>(State, 1));
return 0;
}
@@ -526,7 +523,7 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
beammp_lua_error("json serialize will not go deeper than 100 nested tables, internal references assumed, aborted this path");
return;
}
std::string key{};
std::string key {};
switch (left.get_type()) {
case sol::type::lua_nil:
case sol::type::none:
@@ -572,7 +569,11 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
value = right.as<std::string>();
break;
case sol::type::number:
value = right.as<double>();
if (right.is<int>()) {
value = right.as<int>();
} else {
value = right.as<float>();
}
break;
case sol::type::function:
beammp_lua_warn("unsure what to do with function in JsonEncode, ignoring");
@@ -582,6 +583,7 @@ static void JsonEncodeRecursive(nlohmann::json& json, const sol::object& left, c
for (const auto& pair : right.as<sol::table>()) {
if (pair.first.get_type() != sol::type::number) {
local_is_array = false;
break;
}
}
for (const auto& pair : right.as<sol::table>()) {
@@ -606,10 +608,11 @@ std::string LuaAPI::MP::JsonEncode(const sol::table& object) {
for (const auto& pair : object.as<sol::table>()) {
if (pair.first.get_type() != sol::type::number) {
is_array = false;
break;
}
}
for (const auto& entry : object) {
JsonEncodeRecursive(json, entry.first, entry.second, is_array);
for (const auto& [key, value] : object) {
JsonEncodeRecursive(json, key, value, is_array);
}
return json.dump();
}

View File

@@ -1,6 +1,8 @@
#include "SignalHandling.h"
#include "Common.h"
#include "Compat.h"
#if defined(BEAMMP_LINUX) || defined(BEAMMP_APPLE)
#include <csignal>
static void UnixSignalHandler(int sig) {

View File

@@ -6,32 +6,6 @@
#include <istream>
#include <sstream>
// General
static constexpr std::string_view StrDebug = "Debug";
static constexpr std::string_view StrPrivate = "Private";
static constexpr std::string_view StrPort = "Port";
static constexpr std::string_view StrMaxCars = "MaxCars";
static constexpr std::string_view StrMaxPlayers = "MaxPlayers";
static constexpr std::string_view StrMap = "Map";
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";
static constexpr std::string_view StrLogChat = "LogChat";
// Misc
static constexpr std::string_view StrSendErrors = "SendErrors";
static constexpr std::string_view StrSendErrorsMessageEnabled = "SendErrorsShowMessage";
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";
TEST_CASE("TConfig::TConfig") {
const std::string CfgFile = "beammp_server_testconfig.toml";
fs::remove(CfgFile);
@@ -93,35 +67,37 @@ void SetComment(CommentsT& Comments, const std::string& Comment) {
void TConfig::FlushToFile() {
// auto data = toml::parse<toml::preserve_comments>(mConfigFileName);
auto data = toml::value {};
data["General"][StrAuthKey.data()] = Application::Settings.Key;
data["General"][StrAuthKey.data()] = Application::GetSettingString(StrAuthKey.data());
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;
data["General"][StrLogChat.data()] = Application::GetSettingBool(StrLogChat.data());
SetComment(data["General"][StrLogChat.data()].comments(), " Whether to log chat messages in the console / log");
data["General"][StrDebug.data()] = Application::Settings.DebugModeEnabled;
data["General"][StrPrivate.data()] = Application::Settings.Private;
data["General"][StrPort.data()] = Application::Settings.Port;
data["General"][StrName.data()] = Application::Settings.ServerName;
data["General"][StrMaxCars.data()] = Application::Settings.MaxCars;
data["General"][StrMaxPlayers.data()] = Application::Settings.MaxPlayers;
data["General"][StrMap.data()] = Application::Settings.MapName;
data["General"][StrDescription.data()] = Application::Settings.ServerDesc;
data["General"][StrResourceFolder.data()] = Application::Settings.Resource;
data["General"][StrDebug.data()] = Application::GetSettingBool(StrDebug.data());
data["General"][StrPrivate.data()] = Application::GetSettingBool(StrPrivate.data());
data["General"][StrPort.data()] = Application::GetSettingInt(StrPort.data());
data["General"][StrName.data()] = Application::GetSettingString(StrName.data());
data["General"][StrMaxCars.data()] = Application::GetSettingInt(StrMaxCars.data());
data["General"][StrMaxPlayers.data()] = Application::GetSettingInt(StrMaxPlayers.data());
data["General"][StrMap.data()] = Application::GetSettingString(StrMap.data());
data["General"][StrDescription.data()] = Application::GetSettingString(StrDescription.data());
data["General"][StrResourceFolder.data()] = Application::GetSettingString(StrResourceFolder.data());
// Misc
data["Misc"][StrHideUpdateMessages.data()] = Application::Settings.HideUpdateMessages;
data["Misc"][StrHideUpdateMessages.data()] = Application::GetSettingBool(StrHideUpdateMessages.data());
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;
data["Misc"][StrSendErrors.data()] = Application::GetSettingBool(StrSendErrors.data());
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;
data["Misc"][StrSendErrorsMessageEnabled.data()] = Application::GetSettingBool(StrSendErrorsMessageEnabled.data());
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`");
data["Misc"][StrIncludeSubdirectories.data()] = Application::GetSettingBool(StrIncludeSubdirectories.data());
SetComment(data["Misc"][StrIncludeSubdirectories.data()].comments(), " Whether or not to include subdirectories in General.ResourceFolder/Client when searching for `.zip`s");
// HTTP
data["HTTP"][StrSSLKeyPath.data()] = Application::Settings.SSLKeyPath;
data["HTTP"][StrSSLCertPath.data()] = Application::Settings.SSLCertPath;
data["HTTP"][StrHTTPServerPort.data()] = Application::Settings.HTTPServerPort;
data["HTTP"][StrSSLKeyPath.data()] = Application::GetSettingString(StrSSLKeyPath.data());
data["HTTP"][StrSSLCertPath.data()] = Application::GetSettingString(StrSSLCertPath.data());
data["HTTP"][StrHTTPServerPort.data()] = Application::GetSettingInt(StrHTTPServerPort.data());
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;
data["HTTP"][StrHTTPServerIP.data()] = Application::GetSettingString(StrHTTPServerIP.data());
data["HTTP"][StrHTTPServerUseSSL.data()] = Application::GetSettingBool(StrHTTPServerUseSSL.data());
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::GetSettingBool(StrHTTPServerEnabled.data());
SetComment(data["HTTP"][StrHTTPServerEnabled.data()].comments(), " Enables the internal HTTP server");
std::stringstream Ss;
Ss << "# This is the BeamMP-Server config file.\n"
@@ -156,50 +132,42 @@ void TConfig::CreateConfigFile() {
FlushToFile();
}
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) {
if (Table[Category.c_str()][Key.data()].is_string()) {
OutValue = Table[Category.c_str()][Key.data()].as_string();
Application::SetSetting(Key, std::string(Table[Category.c_str()][Key.data()].as_string()));
} else if (Table[Category.c_str()][Key.data()].is_boolean()) {
Application::SetSetting(Key, bool(Table[Category.c_str()][Key.data()].as_boolean()));
} else if (Table[Category.c_str()][Key.data()].is_integer()) {
Application::SetSetting(Key, int(Table[Category.c_str()][Key.data()].as_integer()));
}
}
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, bool& OutValue) {
if (Table[Category.c_str()][Key.data()].is_boolean()) {
OutValue = Table[Category.c_str()][Key.data()].as_boolean();
}
}
void TConfig::TryReadValue(toml::value& Table, const std::string& Category, const std::string_view& Key, int& OutValue) {
if (Table[Category.c_str()][Key.data()].is_integer()) {
OutValue = int(Table[Category.c_str()][Key.data()].as_integer());
}
}
void TConfig::ParseFromFile(std::string_view name) {
try {
toml::value data = toml::parse<toml::preserve_comments>(name.data());
// GENERAL
TryReadValue(data, "General", StrDebug, Application::Settings.DebugModeEnabled);
TryReadValue(data, "General", StrPrivate, Application::Settings.Private);
TryReadValue(data, "General", StrPort, Application::Settings.Port);
TryReadValue(data, "General", StrMaxCars, Application::Settings.MaxCars);
TryReadValue(data, "General", StrMaxPlayers, Application::Settings.MaxPlayers);
TryReadValue(data, "General", StrMap, Application::Settings.MapName);
TryReadValue(data, "General", StrName, Application::Settings.ServerName);
TryReadValue(data, "General", StrDescription, Application::Settings.ServerDesc);
TryReadValue(data, "General", StrResourceFolder, Application::Settings.Resource);
TryReadValue(data, "General", StrAuthKey, Application::Settings.Key);
TryReadValue(data, "General", StrLogChat, Application::Settings.LogChat);
TryReadValue(data, "General", StrDebug);
TryReadValue(data, "General", StrPrivate);
TryReadValue(data, "General", StrPort);
TryReadValue(data, "General", StrMaxCars);
TryReadValue(data, "General", StrMaxPlayers);
TryReadValue(data, "General", StrMap);
TryReadValue(data, "General", StrName);
TryReadValue(data, "General", StrDescription);
TryReadValue(data, "General", StrResourceFolder);
TryReadValue(data, "General", StrAuthKey);
TryReadValue(data, "General", StrLogChat);
// Misc
TryReadValue(data, "Misc", StrSendErrors, Application::Settings.SendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages, Application::Settings.HideUpdateMessages);
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled, Application::Settings.SendErrorsMessageEnabled);
TryReadValue(data, "Misc", StrSendErrors);
TryReadValue(data, "Misc", StrHideUpdateMessages);
TryReadValue(data, "Misc", StrSendErrorsMessageEnabled);
TryReadValue(data, "Misc", StrIncludeSubdirectories);
// 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);
TryReadValue(data, "HTTP", StrSSLKeyPath);
TryReadValue(data, "HTTP", StrSSLCertPath);
TryReadValue(data, "HTTP", StrHTTPServerPort);
TryReadValue(data, "HTTP", StrHTTPServerIP);
TryReadValue(data, "HTTP", StrHTTPServerEnabled);
TryReadValue(data, "HTTP", StrHTTPServerUseSSL);
} catch (const std::exception& err) {
beammp_error("Error parsing config file value: " + std::string(err.what()));
mFailed = true;
@@ -211,86 +179,28 @@ void TConfig::ParseFromFile(std::string_view name) {
// Update in any case
FlushToFile();
// all good so far, let's check if there's a key
if (Application::Settings.Key.empty()) {
beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server.");
if (Application::GetSettingString(StrAuthKey).empty()) {
beammp_error("No AuthKey specified in the \"" + std::string(mConfigFileName) + "\" file. Please get an AuthKey, enter it into the config file, and restart this server. Alternatively, if you only want a private server (one that doesn't show up in the server list), you may put anything as your AuthKey, like \"hello\".");
Application::SetSubsystemStatus("Config", Application::Status::Bad);
mFailed = true;
return;
}
Application::SetSubsystemStatus("Config", Application::Status::Good);
if (Application::Settings.Key.size() != 36) {
if (Application::GetSettingString(StrAuthKey).size() != 36) {
beammp_warn("AuthKey specified is the wrong length and likely isn't valid.");
}
}
void TConfig::PrintDebug() {
beammp_debug(std::string(StrDebug) + ": " + std::string(Application::Settings.DebugModeEnabled ? "true" : "false"));
beammp_debug(std::string(StrPrivate) + ": " + std::string(Application::Settings.Private ? "true" : "false"));
beammp_debug(std::string(StrPort) + ": " + std::to_string(Application::Settings.Port));
beammp_debug(std::string(StrMaxCars) + ": " + std::to_string(Application::Settings.MaxCars));
beammp_debug(std::string(StrMaxPlayers) + ": " + std::to_string(Application::Settings.MaxPlayers));
beammp_debug(std::string(StrMap) + ": \"" + Application::Settings.MapName + "\"");
beammp_debug(std::string(StrName) + ": \"" + Application::Settings.ServerName + "\"");
beammp_debug(std::string(StrDescription) + ": \"" + Application::Settings.ServerDesc + "\"");
beammp_debug(std::string(StrLogChat) + ": \"" + (Application::Settings.LogChat ? "true" : "false") + "\"");
beammp_debug(std::string(StrResourceFolder) + ": \"" + Application::Settings.Resource + "\"");
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()) + "");
for (const auto& [k, v] : Application::mSettings) {
if (k == StrAuthKey) {
beammp_debugf("AuthKey: length {}", std::get<std::string>(v).size());
continue;
}
beammp_debugf("{}: {}", k, Application::SettingToString(v));
}
}
void TConfig::ParseOldFormat() {
std::ifstream File("Server.cfg");
// read all, strip comments
std::string Content;
for (;;) {
std::string Line;
std::getline(File, Line);
if (!Line.empty() && Line.at(0) != '#') {
Line = Line.substr(0, Line.find_first_of('#'));
Content += Line + "\n";
}
if (!File.good()) {
break;
}
}
std::stringstream Str(Content);
std::string Key, Ignore, Value;
for (;;) {
Str >> Key >> std::ws >> Ignore >> std::ws;
std::getline(Str, Value);
if (Str.eof()) {
break;
}
std::stringstream ValueStream(Value);
ValueStream >> std::ws; // strip leading whitespace if any
Value = ValueStream.str();
if (Key == "Debug") {
Application::Settings.DebugModeEnabled = Value.find("true") != std::string::npos;
} else if (Key == "Private") {
Application::Settings.Private = Value.find("true") != std::string::npos;
} else if (Key == "Port") {
ValueStream >> Application::Settings.Port;
} else if (Key == "Cars") {
ValueStream >> Application::Settings.MaxCars;
} else if (Key == "MaxPlayers") {
ValueStream >> Application::Settings.MaxPlayers;
} else if (Key == "Map") {
Application::Settings.MapName = Value.substr(1, Value.size() - 3);
} else if (Key == "Name") {
Application::Settings.ServerName = Value.substr(1, Value.size() - 3);
} else if (Key == "Desc") {
Application::Settings.ServerDesc = Value.substr(1, Value.size() - 3);
} else if (Key == "use") {
Application::Settings.Resource = Value.substr(1, Value.size() - 3);
} else if (Key == "AuthKey") {
Application::Settings.Key = Value.substr(1, Value.size() - 3);
} else {
beammp_warn("unknown key in old auth file (ignored): " + Key);
}
Str >> std::ws;
}
beammp_warnf("You still have a 'Server.cfg' - this will not be used (this server uses 'ServerConfig.toml'. Since v3.0.2 we no longer parse and import these old settings. Remove the file to avoid confusion and disable this message.");
}

View File

@@ -7,13 +7,34 @@
#include "LuaAPI.h"
#include "TLuaEngine.h"
#include <boost/asio/ip/address.hpp>
#include <boost/spirit/home/qi/directive/lexeme.hpp>
#include <boost/spirit/home/qi/parse.hpp>
#include <chrono>
#include <ctime>
#include <fmt/chrono.h>
#include <sstream>
#include <boost/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/algorithm/string.hpp>
static inline bool StringStartsWith(const std::string& What, const std::string& StartsWith) {
return What.size() >= StartsWith.size() && What.substr(0, StartsWith.size()) == StartsWith;
}
static inline bool StringStartsWithLower(const std::string& Name1, const std::string& Name2) {
std::string Name1Lower = boost::algorithm::to_lower_copy(Name1);
return StringStartsWith(Name1Lower, Name2) || StringStartsWith(Name2, Name1Lower);
};
static inline bool StringStartsWithLowerBoth(const std::string& Name1, const std::string& Name2) {
std::string Name1Lower = boost::algorithm::to_lower_copy(Name1);
std::string Name2Lower = boost::algorithm::to_lower_copy(Name2);
return StringStartsWith(Name1Lower, Name2Lower) || StringStartsWith(Name2Lower, Name1Lower);
};
TEST_CASE("StringStartsWith") {
CHECK(StringStartsWith("Hello, World", "Hello"));
CHECK(StringStartsWith("Hello, World", "H"));
@@ -63,13 +84,13 @@ static inline void SplitString(std::string const& str, const char delim, std::ve
}
static std::string GetDate() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
time_t tt = std::chrono::system_clock::to_time_t(now);
TimeType::time_point now = TimeType::now();
time_t tt = TimeType::to_time_t(now);
auto local_tm = std::localtime(&tt);
char buf[30];
std::string date;
if (Application::Settings.DebugModeEnabled) {
std::strftime(buf, sizeof(buf), "[%d/%m/%y %T.", local_tm);
if (Application::GetSettingBool(StrDebug)) {
std::strftime(buf, sizeof(buf), "[%Y/%m/%d %T.", local_tm);
date += buf;
auto seconds = std::chrono::time_point_cast<std::chrono::seconds>(now);
auto fraction = now - seconds;
@@ -79,7 +100,7 @@ static std::string GetDate() {
date += fracstr;
date += "] ";
} else {
std::strftime(buf, sizeof(buf), "[%d/%m/%y %T] ", local_tm);
std::strftime(buf, sizeof(buf), "[%Y/%m/%d %T] ", local_tm);
date += buf;
}
@@ -198,10 +219,10 @@ bool TConsole::EnsureArgsCount(const std::vector<std::string>& args, size_t min,
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.");
Application::Console().WriteRaw("Too many arguments. At most " + std::to_string(max) + " argument(s) 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.");
Application::Console().WriteRaw("Too few arguments. At least " + std::to_string(min) + " argument(s) expected, got " + std::to_string(args.size()) + " instead.");
return false;
}
}
@@ -232,14 +253,17 @@ void TConsole::Command_Help(const std::string&, const std::vector<std::string>&
static constexpr const char* sHelpString = R"(
Commands:
help displays this help
exit shuts down the server
exit
quit shuts down the server
kick <name> [reason] kicks specified player with an optional reason
list lists all players and info about them
say <message> sends the message to all players in chat
lua [state id] switches to lua, optionally into a specific state id's lua
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)";
debug internal error and debug state of the server (for development)
clear clears the console window
version version info)";
Application::Console().WriteRaw("BeamMP-Server Console: " + std::string(sHelpString));
}
@@ -259,33 +283,114 @@ void TConsole::Command_Clear(const std::string&, const std::vector<std::string>&
mCommandline.write("\x1b[;H\x1b[2J");
}
void TConsole::Command_Debug(const std::string&, const std::vector<std::string>& args) {
if (!EnsureArgsCount(args, 0)) {
return;
}
Application::Console().WriteRaw(fmt::format(R"(Debug info (for developers):
UDP:
Malformed packets: {}
Invalid packets: {})",
Application::MalformedUdpPackets,
Application::InvalidUdpPackets));
Application::Console().WriteRaw(fmt::format(R"( Clients:
Note: All data/second rates are an average across the total time since
connection and do not necessarily reflect the *current* data rate
of that client.
)"));
mLuaEngine->Server().ForEachClient([&](const auto& Client) {
std::string State = "";
if (Client->IsSyncing()) {
State += "Syncing";
}
if (Client->IsSynced()) {
if (!State.empty()) {
State += " & ";
}
State += "Synced";
}
if (Client->IsConnected()) {
if (!State.empty()) {
State += " & ";
}
State += "Connected";
}
if (Client->IsDisconnected()) {
if (!State.empty()) {
State += " & ";
}
State += "Disconnected";
}
auto Now = TimeType::now();
auto Seconds = std::chrono::duration_cast<std::chrono::seconds>(Now - Client->ConnectionTime);
std::string ConnectedSince = fmt::format("{:%Y/%m/%d %H:%M:%S}, {:%H:%M:%S} ago ({} seconds)",
fmt::localtime(TimeType::to_time_t(Client->ConnectionTime)),
Seconds,
Seconds.count());
Application::Console().WriteRaw(fmt::format(
R"( {} ('{}'):
Roles: {}
Cars: {}
Is guest: {}
Has unicycle: {}
TCP: {} (on port {})
UDP: {} (on port {})
Sent via TCP: {}
Received via TCP: {}
Sent via UDP: {} ({} packets)
Received via UDP: {} ({} packets)
Status: {}
Queued packets: {}
Latest packet: {}s ago
Connected since: {}
Average send: {}/s
Average receive: {}/s)",
Client->GetID(), Client->GetName(),
Client->GetRoles(),
Client->GetCarCount(),
Client->IsGuest() ? "yes" : "no",
Client->GetUnicycleID() == -1 ? "no" : "yes",
Client->GetTCPSock().remote_endpoint().address() == ip::address::from_string("0.0.0.0") ? "not connected" : "connected", Client->GetTCPSock().remote_endpoint().port(),
Client->GetUDPAddr().address() == ip::address::from_string("0.0.0.0") ? "NOT connected" : "connected", Client->GetUDPAddr().port(),
ToHumanReadableSize(Client->TcpSent),
ToHumanReadableSize(Client->TcpReceived),
ToHumanReadableSize(Client->UdpSent), Client->UdpPacketsSent,
ToHumanReadableSize(Client->UdpReceived), Client->UdpPacketsReceived,
State.empty() ? "None (likely pre-sync)" : State,
Client->MissedPacketQueueSize(),
Client->SecondsSinceLastPing(),
ConnectedSince,
ToHumanReadableSize((Client->TcpSent + Client->UdpSent) / Seconds.count()),
ToHumanReadableSize((Client->TcpReceived + Client->UdpReceived) / Seconds.count())));
});
}
void TConsole::Command_Version(const std::string&, const std::vector<std::string>& args) {
if (!EnsureArgsCount(args, 0)) {
return;
}
Application::Console().WriteRaw(fmt::format("BeamMP Server v{} ({})", Application::ServerVersionString(), BEAMMP_GIT_HASH));
}
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);
auto Name = boost::algorithm::to_lower_copy(args.at(0));
std::string Reason = "Kicked by server console";
if (args.size() > 1) {
Reason = ConcatArgs({ args.begin() + 1, args.end() });
}
beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'");
bool Kicked = false;
// TODO: this sucks, tolower is locale-dependent.
auto NameCompare = [](std::string Name1, std::string Name2) -> bool {
std::for_each(Name1.begin(), Name1.end(), [](char& c) { c = char(std::tolower(char(c))); });
std::for_each(Name2.begin(), Name2.end(), [](char& c) { c = char(std::tolower(char(c))); });
return StringStartsWith(Name1, Name2) || StringStartsWith(Name2, Name1);
};
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
if (NameCompare(locked->GetName(), Name)) {
mLuaEngine->Network().ClientKick(*locked, Reason);
Kicked = true;
return false;
}
mLuaEngine->Server().ForEachClient([&](const auto& Client) -> IterationDecision {
if (StringStartsWithLower(Client->GetName(), Name)) {
mLuaEngine->Network().ClientKick(*Client, Reason);
Kicked = true;
return Break;
}
return true;
return Continue;
});
if (!Kicked) {
Application::Console().WriteRaw("Error: No player with name matching '" + Name + "' was found.");
@@ -350,16 +455,80 @@ std::tuple<std::string, std::vector<std::string>> TConsole::ParseCommand(const s
}
void TConsole::Command_Settings(const std::string&, const std::vector<std::string>& args) {
if (!EnsureArgsCount(args, 0)) {
if (!EnsureArgsCount(args, 1, 100)) {
return;
}
static const char* SETTINGS_HELP = R"(Settings:
settings help Displays this help
settings list Lists all settings
settings get <setting> Prints the current value of the specified setting
settings set <setting> <value> Sets the specified setting to the value)";
if (args.at(0) == "help") {
Application::Console().WriteRaw(SETTINGS_HELP);
} else if (args.at(0) == "list") {
Application::Console().WriteRaw("Available settings:");
Application::Console().WriteRaw(fmt::format("\t{:<25} {}", "<NAME>", "<CURRENT VALUE>"));
for (const auto& [k, v] : Application::mSettings) {
if (k == StrAuthKey) {
Application::Console().WriteRaw(fmt::format("\t{:<25} <key of length {}>", k, Application::SettingToString(v).size()));
} else {
Application::Console().WriteRaw(fmt::format("\t{:<25} {}", k, Application::SettingToString(v)));
}
}
} else if (args.at(0) == "get") {
if (args.size() < 2) {
Application::Console().WriteRaw("Not enough arguments: `settings get` requires a setting name.");
} else {
if (Application::mSettings.contains(args.at(1))) {
if (args.at(1) != StrAuthKey) {
Application::Console().WriteRaw(fmt::format("{} = {}", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1)))));
} else {
Application::Console().WriteRaw(fmt::format("{} = <key of length {}>", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1))).size()));
}
} else {
Application::Console().WriteRaw(fmt::format("Setting '{}' doesn't exist.", args.at(1)));
}
}
} else if (args.at(0) == "set") {
if (args.size() < 3) {
Application::Console().WriteRaw("Not enough arguments: `settings set` requires a setting name and value.");
} else {
if (args.at(1) == StrAuthKey) {
Application::Console().WriteRaw("It's not allowed to set the AuthKey during runtime.");
} else {
using namespace boost::spirit;
using qi::_1;
std::string ValueString = args.at(2);
Application::SettingValue Value;
qi::rule<std::string::iterator, std::string()> StringRule;
StringRule
%= qi::lexeme['"' >> *(qi::char_ - '"') >> '"']
| +(qi::char_ - '"');
qi::rule<std::string::iterator, Application::SettingValue()> ValueRule
= qi::bool_
| qi::int_
| StringRule;
auto It = ValueString.begin();
if (qi::phrase_parse(It, ValueString.end(), ValueRule[boost::phoenix::ref(Value) = _1], ascii::space)
&& It == ValueString.end()) {
Application::SetSetting(args.at(1), Value);
Application::Console().WriteRaw(fmt::format("{} := {}", args.at(1), Application::SettingToString(Application::mSettings.at(args.at(1)))));
} else {
Application::Console().WriteRaw(fmt::format("New value '{}' did not parse as a valid value.", ValueString));
}
}
}
} else {
Application::Console().WriteRaw(fmt::format("Unknown argument '{}' - 'settings {}' is not a valid command.", args.at(0), args.at(0)));
}
}
void TConsole::Command_Say(const std::string& FullCmd) {
if (FullCmd.size() > 3) {
auto Message = FullCmd.substr(4);
LuaAPI::MP::SendChatMessage(-1, Message);
if (!Application::Settings.LogChat) {
if (!Application::GetSettingBool(StrLogChat)) {
Application::Console().WriteRaw("Chat message sent!");
}
}
@@ -374,14 +543,10 @@ void TConsole::Command_List(const std::string&, const std::vector<std::string>&
} else {
std::stringstream ss;
ss << std::left << std::setw(25) << "Name" << std::setw(6) << "ID" << std::setw(6) << "Cars" << std::endl;
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
ss << std::left << std::setw(25) << locked->GetName()
<< std::setw(6) << locked->GetID()
<< std::setw(6) << locked->GetCarCount() << "\n";
}
return true;
mLuaEngine->Server().ForEachClient([&](const auto& Client) {
ss << std::left << std::setw(25) << Client->GetName()
<< std::setw(6) << Client->GetID()
<< std::setw(6) << Client->GetCarCount() << "\n";
});
auto Str = ss.str();
Application::Console().WriteRaw(Str.substr(0, Str.size() - 1));
@@ -401,20 +566,16 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
size_t SyncingCount = 0;
size_t MissedPacketQueueSum = 0;
int LargestSecondsSinceLastPing = 0;
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto Locked = Client.lock();
CarCount += Locked->GetCarCount();
ConnectedCount += Locked->IsConnected() ? 1 : 0;
GuestCount += Locked->IsGuest() ? 1 : 0;
SyncedCount += Locked->IsSynced() ? 1 : 0;
SyncingCount += Locked->IsSyncing() ? 1 : 0;
MissedPacketQueueSum += Locked->MissedPacketQueueSize();
if (Locked->SecondsSinceLastPing() < LargestSecondsSinceLastPing) {
LargestSecondsSinceLastPing = Locked->SecondsSinceLastPing();
}
mLuaEngine->Server().ForEachClient([&](const auto& Client) {
CarCount += Client->GetCarCount();
ConnectedCount += Client->IsConnected() ? 1 : 0;
GuestCount += Client->IsGuest() ? 1 : 0;
SyncedCount += Client->IsSynced() ? 1 : 0;
SyncingCount += Client->IsSyncing() ? 1 : 0;
MissedPacketQueueSum += Client->MissedPacketQueueSize();
if (Client->SecondsSinceLastPing() < LargestSecondsSinceLastPing) {
LargestSecondsSinceLastPing = Client->SecondsSinceLastPing();
}
return true;
});
size_t SystemsStarting = 0;
@@ -478,17 +639,66 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
<< "\t\tEvent handlers: " << mLuaEngine->GetRegisteredEventHandlerCount() << "\n"
<< "\tSubsystems:\n"
<< "\t\tGood/Starting/Bad: " << SystemsGood << "/" << SystemsStarting << "/" << SystemsBad << "\n"
<< "\t\tShutting down/Shut down: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
<< "\t\tShutting down/Shut down: " << SystemsShuttingDown << "/" << SystemsShutdown << "\n"
<< "\t\tGood: [ " << SystemsGoodList << " ]\n"
<< "\t\tStarting: [ " << SystemsStartingList << " ]\n"
<< "\t\tBad: [ " << SystemsBadList << " ]\n"
<< "\t\tShutting down: [ " << SystemsShuttingDownList << " ]\n"
<< "\t\tShut down: [ " << SystemsShutdownList << " ]\n"
<< "\t\tShut down: [ " << SystemsShutdownList << " ]\n"
<< "";
Application::Console().WriteRaw(Status.str());
}
void TConsole::Autocomplete_Lua(const std::string& stub, std::vector<std::string>& suggestions) {
auto stateNames = mLuaEngine->GetLuaStateNames();
for (const auto& name : stateNames) {
if (name.find(stub) == 0) {
suggestions.push_back("lua " + name);
}
}
}
void TConsole::Autocomplete_Kick(const std::string& stub, std::vector<std::string>& suggestions) {
std::string stub_lower = boost::algorithm::to_lower_copy(stub);
mLuaEngine->Server().ForEachClient([&](const auto& Client) {
if (StringStartsWithLower(Client->GetName(), stub_lower)) {
suggestions.push_back("kick " + Client->GetName());
}
});
}
void TConsole::Autocomplete_Settings(const std::string& stub, std::vector<std::string>& suggestions) {
const std::string subcommands[] = { "help", "list", "set", "get" };
auto [command, args] = ParseCommand(stub);
std::string arg;
if (!args.empty())
arg = boost::algorithm::to_lower_copy(args.at(0));
// suggest setting names
if (command == "set" || command == "get") {
for (const auto& [k, v] : Application::mSettings) {
std::string key = std::string(k);
if (StringStartsWithLower(key, arg)) {
suggestions.push_back("settings " + command + " " + key);
}
}
return;
}
// suggest subcommands
for (const auto& cmd : subcommands) {
if (cmd.find(command) == 0) {
suggestions.push_back("settings " + cmd);
}
}
}
void TConsole::RunAsCommand(const std::string& cmd, bool IgnoreNotACommand) {
auto FutureIsNonNil =
[](const std::shared_ptr<TLuaResult>& Future) {
@@ -590,7 +800,6 @@ Commands
TConsole::TConsole() {
mCommandline.enable_history();
mCommandline.set_history_limit(20);
mCommandline.set_prompt("> ");
BackupOldLog();
mCommandline.on_command = [this](Commandline& c) {
try {
@@ -615,7 +824,7 @@ TConsole::TConsole() {
} else {
if (!mLuaEngine) {
beammp_error("Attempted to run a command before Lua engine started. Please wait and try again.");
} else if (cmd == "exit") {
} else if (cmd == "exit" || cmd == "quit") {
beammp_info("gracefully shutting down");
Application::GracefullyShutdown();
} else if (cmd == "say") {
@@ -677,16 +886,15 @@ TConsole::TConsole() {
}
}
} else { // if not lua
if (stub.find("lua") == 0) { // starts with "lua" means we should suggest state names
std::string after_prefix = TrimString(stub.substr(3));
auto stateNames = mLuaEngine->GetLuaStateNames();
for (const auto& name : stateNames) {
if (name.find(after_prefix) == 0) {
suggestions.push_back("lua " + name);
}
for (const auto& [cmd_name, autocomplete_fn] : mCommandAutocompleteMap) {
if (stub.find(cmd_name) == 0) { // input starts with a full command (that has autocomplete)
std::size_t cmd_len = cmd_name.length();
std::string trimmed = TrimString(stub.substr(cmd_len));
autocomplete_fn(trimmed, suggestions);
break;
}
} else {
}
if (suggestions.empty()) {
for (const auto& [cmd_name, cmd_fn] : mCommandMap) {
if (cmd_name.find(stub) == 0) {
suggestions.push_back(cmd_name);

View File

@@ -17,14 +17,14 @@ void THeartbeatThread::operator()() {
// these are "hot-change" related variables
static std::string Last;
static std::chrono::high_resolution_clock::time_point LastNormalUpdateTime = std::chrono::high_resolution_clock::now();
static TimeType::time_point LastNormalUpdateTime = TimeType::now();
bool isAuth = false;
size_t UpdateReminderCounter = 0;
while (!Application::IsShuttingDown()) {
++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();
auto Now = TimeType::now();
bool Unchanged = Last == Body;
auto TimePassed = (Now - LastNormalUpdateTime);
auto Threshold = Unchanged ? 30 : 5;
@@ -36,14 +36,10 @@ void THeartbeatThread::operator()() {
Last = Body;
LastNormalUpdateTime = Now;
if (!Application::Settings.CustomIP.empty()) {
Body += "&ip=" + Application::Settings.CustomIP;
if (!Application::GetSettingString(StrCustomIP).empty()) {
Body += "&ip=" + Application::GetSettingString(StrCustomIP);
}
Body += "&pps=" + Application::PPS();
beammp_trace("heartbeat body: '" + Body + "'");
auto SentryReportError = [&](const std::string& transaction, int status) {
auto Lock = Sentry.CreateExclusiveContext();
Sentry.SetContext("heartbeat",
@@ -61,11 +57,10 @@ void THeartbeatThread::operator()() {
bool Ok = false;
for (const auto& Url : Application::GetBackendUrlsInOrder()) {
T = Http::POST(Url, 443, Target, Body, "application/x-www-form-urlencoded", &ResponseCode, { { "api-v", "2" } });
beammp_trace(T);
Doc.Parse(T.data(), T.size());
if (Doc.HasParseError() || !Doc.IsObject()) {
if (!Application::Settings.Private) {
beammp_error("Backend response failed to parse as valid json");
if (!Application::GetSettingBool(StrPrivate)) {
beammp_trace("Backend response failed to parse as valid json");
beammp_trace("Response was: `" + T + "`");
}
Sentry.SetContext("JSON Response", { { "reponse", T } });
@@ -110,12 +105,12 @@ void THeartbeatThread::operator()() {
Sentry.LogError("Missing/invalid json members in backend response", __FILE__, std::to_string(__LINE__));
}
} else {
if (!Application::Settings.Private) {
if (!Application::GetSettingBool(StrPrivate)) {
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 && !Application::Settings.Private) {
if (Ok && !isAuth && !Application::GetSettingBool(StrPrivate)) {
if (Status == "2000") {
beammp_info(("Authenticated! " + Message));
isAuth = true;
@@ -129,10 +124,10 @@ void THeartbeatThread::operator()() {
beammp_error("Backend REFUSED the auth key. Reason: " + Message);
}
}
if (isAuth || Application::Settings.Private) {
if (isAuth || Application::GetSettingBool(StrPrivate)) {
Application::SetSubsystemStatus("Heartbeat", Application::Status::Good);
}
if (!Application::Settings.HideUpdateMessages && UpdateReminderCounter % 5) {
if (!Application::GetSettingBool(StrHideUpdateMessages) && UpdateReminderCounter % 5) {
Application::CheckForUpdates();
}
}
@@ -141,20 +136,20 @@ void THeartbeatThread::operator()() {
std::string THeartbeatThread::GenerateCall() {
std::stringstream Ret;
Ret << "uuid=" << Application::Settings.Key
Ret << "uuid=" << Application::GetSettingString(StrAuthKey)
<< "&players=" << mServer.ClientCount()
<< "&maxplayers=" << Application::Settings.MaxPlayers
<< "&port=" << Application::Settings.Port
<< "&map=" << Application::Settings.MapName
<< "&private=" << (Application::Settings.Private ? "true" : "false")
<< "&maxplayers=" << Application::GetSettingInt(StrMaxPlayers)
<< "&port=" << Application::GetSettingInt(StrPort)
<< "&map=" << Application::GetSettingString(StrMap)
<< "&private=" << (Application::GetSettingBool(StrPrivate) ? "true" : "false")
<< "&version=" << Application::ServerVersionString()
<< "&clientversion=" << Application::ClientVersionString()
<< "&name=" << Application::Settings.ServerName
<< "&modlist=" << mResourceManager.TrimmedList()
<< "&modstotalsize=" << mResourceManager.MaxModSize()
<< "&modstotal=" << mResourceManager.ModsLoaded()
<< "&clientversion=" << std::to_string(Application::ClientMajorVersion()) + ".0" // FIXME: Wtf.
<< "&name=" << Application::GetSettingString(StrName)
<< "&modlist=" << TResourceManager::FormatForBackend(mResourceManager.FileMap())
<< "&modstotalsize=" << mResourceManager.TotalModsSize()
<< "&modstotal=" << mResourceManager.LoadedModCount()
<< "&playerslist=" << GetPlayers()
<< "&desc=" << Application::Settings.ServerDesc;
<< "&desc=" << Application::GetSettingString(StrDescription);
return Ret.str();
}
THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& Server)
@@ -172,12 +167,8 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
}
std::string THeartbeatThread::GetPlayers() {
std::string Return;
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
ReadLock Lock(mServer.GetClientMutex());
if (!ClientPtr.expired()) {
Return += ClientPtr.lock()->GetName() + ";";
}
return true;
mServer.ForEachClient([&](const auto& Client) {
Return += Client->GetName() + ";";
});
return Return;
}

View File

@@ -4,6 +4,8 @@
#include "Http.h"
#include "LuaAPI.h"
#include "TLuaPlugin.h"
#include "Uuid.h"
#include "sol/types.hpp"
#include <chrono>
#include <condition_variable>
@@ -15,11 +17,11 @@
TLuaEngine* LuaAPI::MP::Engine;
TLuaEngine::TLuaEngine()
: mResourceServerPath(fs::path(Application::Settings.Resource) / "Server") {
: mResourceServerPath(fs::path(Application::GetSettingString(StrResourceFolder)) / "Server") {
Application::SetSubsystemStatus("LuaEngine", Application::Status::Starting);
LuaAPI::MP::Engine = this;
if (!fs::exists(Application::Settings.Resource)) {
fs::create_directory(Application::Settings.Resource);
if (!fs::exists(Application::GetSettingString(StrResourceFolder))) {
fs::create_directory(Application::GetSettingString(StrResourceFolder));
}
if (!fs::exists(mResourceServerPath)) {
fs::create_directory(mResourceServerPath);
@@ -35,7 +37,7 @@ TLuaEngine::TLuaEngine()
}
TEST_CASE("TLuaEngine ctor & dtor") {
Application::Settings.Resource = "beammp_server_test_resources";
Application::SetSetting(StrResourceFolder, "beammp_server_test_resources");
TLuaEngine engine;
Application::GracefullyShutdown();
}
@@ -70,13 +72,12 @@ void TLuaEngine::operator()() {
}
return false;
});
} else {
mResultsToCheckCond.wait_for(Lock, std::chrono::milliseconds(20));
}
mResultsToCheckCond.wait_for(Lock, std::chrono::milliseconds(10));
}
});
// event loop
auto Before = std::chrono::high_resolution_clock::now();
auto Before = TimeType::now();
while (!Application::IsShuttingDown()) {
{ // Timed Events Scope
std::unique_lock Lock(mTimedEventsMutex);
@@ -109,14 +110,14 @@ void TLuaEngine::operator()() {
} else {
constexpr double NsFactor = 1000000.0;
constexpr double Expected = 10.0; // ms
const auto Diff = (std::chrono::high_resolution_clock::now() - Before).count() / NsFactor;
const auto Diff = (TimeType::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 = TimeType::now();
}
if (ResultCheckThread.joinable()) {
@@ -252,11 +253,15 @@ std::vector<std::string> TLuaEngine::StateThreadData::GetStateTableKeys(const st
} else if (i == keys.size() - 1) {
if (obj.get_type() == sol::type::table) {
for (const auto& [key, value] : obj.as<sol::table>()) {
std::string s = key.as<std::string>();
if (value.get_type() == sol::type::function) {
s += "(";
if (key.get_type() == sol::type::string) {
std::string s = key.as<std::string>();
if (value.get_type() == sol::type::function) {
s += "(";
}
Result.push_back(s);
}
Result.push_back(s);
}
} else {
Result = { obj.as<std::string>() };
@@ -280,7 +285,7 @@ std::vector<std::string> TLuaEngine::StateThreadData::GetStateTableKeys(const st
*/
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<TimeType::duration>& Max) {
for (const auto& Result : Results) {
bool Cancelled = false;
size_t ms = 0;
@@ -329,7 +334,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const
return mLuaStates.at(StateID)->EnqueueScript(Script);
}
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args) {
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaValue>& Args) {
std::unique_lock Lock(mLuaStatesMutex);
return mLuaStates.at(StateID)->EnqueueFunctionCall(FunctionName, Args);
}
@@ -409,15 +414,56 @@ std::set<std::string> TLuaEngine::GetEventHandlersForState(const std::string& Ev
return mLuaEvents[EventName][StateId];
}
std::vector<sol::object> TLuaEngine::StateThreadData::JsonStringToArray(JsonString Str) {
auto LocalTable = Lua_JsonDecode(Str.value).as<std::vector<sol::object>>();
for (auto& value : LocalTable) {
if (value.is<std::string>() && value.as<std::string>() == BEAMMP_INTERNAL_NIL) {
value = sol::object {};
}
}
return LocalTable;
}
sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
auto Return = mEngine->TriggerEvent(EventName, mStateId, EventArgs);
auto Table = mStateView.create_table();
for (const sol::stack_proxy& Arg : EventArgs) {
switch (Arg.get_type()) {
case sol::type::none:
case sol::type::userdata:
case sol::type::lightuserdata:
case sol::type::thread:
case sol::type::function:
case sol::type::poly:
Table.add(BEAMMP_INTERNAL_NIL);
beammp_warnf("Passed a value of type '{}' to TriggerGlobalEvent(\"{}\", ...). This type can not be serialized, and cannot be passed between states. It will arrive as <nil> in handlers.", sol::type_name(EventArgs.lua_state(), Arg.get_type()), EventName);
break;
case sol::type::lua_nil:
Table.add(BEAMMP_INTERNAL_NIL);
break;
case sol::type::string:
case sol::type::number:
case sol::type::boolean:
case sol::type::table:
Table.add(Arg);
break;
}
}
JsonString Str { "" };
if (!Table.empty()) {
Str.value = LuaAPI::MP::JsonEncode(Table);
}
auto Return = mEngine->TriggerEvent(EventName, mStateId, Str);
mEngine->ReportErrors(Return);
auto MyHandlers = mEngine->GetEventHandlersForState(EventName, mStateId);
sol::variadic_results LocalArgs = Str.value.empty() ? sol::variadic_results {} : JsonStringToArray(Str);
for (const auto& Handler : MyHandlers) {
auto Fn = mStateView[Handler];
if (Fn.valid()) {
auto LuaResult = Fn(EventArgs);
auto LuaResult = LocalArgs.empty() ? Fn() : Fn(LocalArgs);
auto Result = std::make_shared<TLuaResult>();
Result->Ready = true;
if (LuaResult.valid()) {
Result->Error = false;
Result->Result = LuaResult;
@@ -425,6 +471,7 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
Result->Error = true;
Result->ErrorMessage = "Function result in TriggerGlobalEvent was invalid";
}
Result->Ready = true;
Return.push_back(Result);
}
}
@@ -454,31 +501,44 @@ sol::table TLuaEngine::StateThreadData::Lua_TriggerGlobalEvent(const std::string
}
return Result;
});
AsyncEventReturn.set_function("Wait",
[](const sol::table& Self, std::optional<float> TimeoutS) {
auto Vector = Self.get<std::vector<std::shared_ptr<TLuaResult>>>("ReturnValueImpl");
if (TimeoutS.has_value()) {
TLuaEngine::WaitForAll(Vector, std::chrono::milliseconds(size_t(TimeoutS.value() * 1000.0f)));
} else {
TLuaEngine::WaitForAll(Vector);
}
});
return AsyncEventReturn;
}
sol::table TLuaEngine::StateThreadData::Lua_TriggerLocalEvent(const std::string& EventName, sol::variadic_args EventArgs) {
// TODO: make asynchronous?
sol::table Result = mStateView.create_table();
auto Result = mStateView.create_table();
for (const auto& Handler : mEngine->GetEventHandlersForState(EventName, mStateId)) {
auto Fn = mStateView[Handler];
if (Fn.valid() && Fn.get_type() == sol::type::function) {
auto FnRet = Fn(EventArgs);
if (FnRet.valid()) {
Result.add(FnRet);
for (const auto& Res : FnRet) {
Result.add(Res);
}
} else {
sol::error Err = FnRet;
beammp_lua_error(std::string("TriggerLocalEvent: ") + Err.what());
}
} else {
beammp_lua_errorf("Handler '{}' for event '{}' in state '{}' is NOT a function, and will be ignored.", Handler, EventName, mStateId);
}
}
return Result;
}
sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
auto MaybeClient = GetClient(mEngine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
auto IDs = MaybeClient.value().lock()->GetIdentifiers();
auto Client = GetClient(mEngine->Server(), ID);
if (Client) {
auto IDs = Client->GetIdentifiers();
if (IDs.empty()) {
return sol::lua_nil;
}
@@ -494,27 +554,20 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
sol::table Result = mStateView.create_table();
mEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
Result[locked->GetID()] = locked->GetName();
}
return true;
mEngine->Server().ForEachClient([&](const auto& Client) {
Result[Client->GetID()] = Client->GetName();
});
return Result;
}
int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) {
int Id = -1;
mEngine->mServer->ForEachClient([&Id, &Name](std::weak_ptr<TClient> Client) -> bool {
if (!Client.expired()) {
auto locked = Client.lock();
if (locked->GetName() == Name) {
Id = locked->GetID();
return false;
}
mEngine->mServer->ForEachClient([&Id, &Name](const auto& Client) -> IterationDecision {
if (Client->GetName() == Name) {
Id = Client->GetID();
return Break;
}
return true;
return Continue;
});
return Id;
}
@@ -546,18 +599,17 @@ sol::table TLuaEngine::StateThreadData::Lua_FS_ListDirectories(const std::string
}
std::string TLuaEngine::StateThreadData::Lua_GetPlayerName(int ID) {
auto MaybeClient = GetClient(mEngine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
return MaybeClient.value().lock()->GetName();
auto Client = GetClient(mEngine->Server(), ID);
if (Client) {
return Client->GetName();
} else {
return "";
}
}
sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) {
auto MaybeClient = GetClient(mEngine->Server(), ID);
if (MaybeClient && !MaybeClient.value().expired()) {
auto Client = MaybeClient.value().lock();
auto Client = GetClient(mEngine->Server(), ID);
if (Client) {
TClient::TSetOfVehicleData VehicleData;
{ // Vehicle Data Lock Scope
auto LockedData = Client->GetAllCars();
@@ -578,33 +630,30 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerVehicles(int ID) {
std::pair<sol::table, std::string> TLuaEngine::StateThreadData::Lua_GetPositionRaw(int PID, int VID) {
std::pair<sol::table, std::string> Result;
auto MaybeClient = GetClient(mEngine->Server(), PID);
if (MaybeClient && !MaybeClient.value().expired()) {
auto Client = MaybeClient.value().lock();
auto Client = GetClient(mEngine->Server(), PID);
if (Client) {
std::string VehiclePos = Client->GetCarPositionRaw(VID);
if (VehiclePos.empty()) {
//return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Vehicle not found"));
// return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Vehicle not found"));
Result.second = "Vehicle not found";
return Result;
}
sol::table t = Lua_JsonDecode(VehiclePos);
if (t == sol::lua_nil){
if (t == sol::lua_nil) {
Result.second = "Packet decode failed";
}
//return std::make_tuple(Result, sol::make_object(StateView, sol::lua_nil));
// return std::make_tuple(Result, sol::make_object(StateView, sol::lua_nil));
Result.first = t;
return Result;
}
else {
//return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Client expired"));
} else {
// return std::make_tuple(sol::lua_nil, sol::make_object(StateView, "Client expired"));
Result.second = "Client expired";
return Result;
}
}
sol::table TLuaEngine::StateThreadData::Lua_HttpCreateConnection(const std::string& host, uint16_t port) {
auto table = mStateView.create_table();
constexpr const char* InternalClient = "__InternalClient";
@@ -727,14 +776,14 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
MPTable.set_function("CreateTimer", [&]() -> sol::table {
sol::state_view StateView(mState);
sol::table Result = StateView.create_table();
Result["__StartTime"] = std::chrono::high_resolution_clock::now();
Result["__StartTime"] = TimeType::now();
Result.set_function("GetCurrent", [&](const sol::table& Table) -> float {
auto End = std::chrono::high_resolution_clock::now();
auto Start = Table.get<std::chrono::high_resolution_clock::time_point>("__StartTime");
auto End = TimeType::now();
auto Start = Table.get<TimeType::time_point>("__StartTime");
return std::chrono::duration_cast<std::chrono::microseconds>(End - Start).count() / 1000000.0f;
});
Result.set_function("Start", [&](sol::table Table) {
Table["__StartTime"] = std::chrono::high_resolution_clock::now();
Table["__StartTime"] = TimeType::now();
});
return Result;
});
@@ -746,9 +795,10 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
MPTable.set_function("TriggerGlobalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> sol::table {
return Lua_TriggerGlobalEvent(EventName, EventArgs);
});
MPTable.set_function("TriggerLocalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> sol::table {
return Lua_TriggerLocalEvent(EventName, EventArgs);
});
MPTable.set_function(
"TriggerLocalEvent", [&](const std::string& EventName, sol::variadic_args EventArgs) -> auto{
return Lua_TriggerLocalEvent(EventName, EventArgs);
});
MPTable.set_function("TriggerClientEvent", &LuaAPI::MP::TriggerClientEvent);
MPTable.set_function("TriggerClientEventJson", &LuaAPI::MP::TriggerClientEventJson);
MPTable.set_function("GetPlayerCount", &LuaAPI::MP::GetPlayerCount);
@@ -819,6 +869,7 @@ TLuaEngine::StateThreadData::StateThreadData(const std::string& Name, TLuaStateI
UtilTable.set_function("JsonUnflatten", &LuaAPI::MP::JsonUnflatten);
UtilTable.set_function("JsonPrettify", &LuaAPI::MP::JsonPrettify);
UtilTable.set_function("JsonMinify", &LuaAPI::MP::JsonMinify);
UtilTable.set_function("GenerateUUID", &uuid::GenerateUuid);
UtilTable.set_function("Random", [this] {
return mUniformRealDistribution01(mMersenneTwister);
});
@@ -875,7 +926,7 @@ 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) {
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFromCustomEvent(const std::string& FunctionName, const std::vector<TLuaValue>& Args, const std::string& EventName, CallStrategy Strategy) {
// TODO: Document all this
decltype(mStateFunctionQueue)::iterator Iter = mStateFunctionQueue.end();
if (Strategy == CallStrategy::BestEffort) {
@@ -897,7 +948,7 @@ std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCallFrom
}
}
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args) {
std::shared_ptr<TLuaResult> TLuaEngine::StateThreadData::EnqueueFunctionCall(const std::string& FunctionName, const std::vector<TLuaValue>& Args) {
auto Result = std::make_shared<TLuaResult>();
Result->StateId = mStateId;
Result->Function = FunctionName;
@@ -948,7 +999,6 @@ void TLuaEngine::StateThreadData::operator()() {
}
sol::state_view StateView(mState);
auto Res = StateView.safe_script(*S.first.Content, sol::script_pass_on_error, S.first.FileName);
S.second->Ready = true;
if (Res.valid()) {
S.second->Error = false;
S.second->Result = std::move(Res);
@@ -957,6 +1007,7 @@ void TLuaEngine::StateThreadData::operator()() {
sol::error Err = Res;
S.second->ErrorMessage = Err.what();
}
S.second->Ready = true;
}
}
{ // StateFunctionQueue Scope
@@ -982,18 +1033,41 @@ void TLuaEngine::StateThreadData::operator()() {
continue;
}
switch (Arg.index()) {
case TLuaArgTypes_String:
case TLuaType::String:
LuaArgs.push_back(sol::make_object(StateView, std::get<std::string>(Arg)));
break;
case TLuaArgTypes_Int:
case TLuaType::Int:
LuaArgs.push_back(sol::make_object(StateView, std::get<int>(Arg)));
break;
case TLuaArgTypes_VariadicArgs:
LuaArgs.push_back(sol::make_object(StateView, std::get<sol::variadic_args>(Arg)));
case TLuaType::Json: {
auto Str = std::get<JsonString>(Arg);
if (!Str.value.empty()) {
auto LocalArgs = JsonStringToArray(Str);
LuaArgs.insert(LuaArgs.end(), LocalArgs.begin(), LocalArgs.end());
}
break;
case TLuaArgTypes_Bool:
}
case TLuaType::Bool:
LuaArgs.push_back(sol::make_object(StateView, std::get<bool>(Arg)));
break;
case TLuaType::StringStringMap: {
auto Map = std::get<std::unordered_map<std::string, std::string>>(Arg);
auto Table = StateView.create_table();
for (const auto& [k, v] : Map) {
Table[k] = v;
}
LuaArgs.push_back(sol::make_object(StateView, Table));
break;
}
case TLuaType::StringSizeTMap: {
auto Map = std::get<std::unordered_map<std::string, size_t>>(Arg);
auto Table = StateView.create_table();
for (const auto& [k, v] : Map) {
Table[k] = v;
}
LuaArgs.push_back(sol::make_object(StateView, Table));
break;
}
default:
beammp_error("Unknown argument type, passed as nil");
break;
@@ -1032,8 +1106,8 @@ std::vector<TLuaEngine::QueuedFunction> TLuaEngine::StateThreadData::Debug_GetSt
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(),
TimeType::duration { std::chrono::milliseconds(IntervalMS) },
TimeType::now(),
EventName,
StateId,
Strategy
@@ -1076,10 +1150,10 @@ TLuaChunk::TLuaChunk(std::shared_ptr<std::string> Content, std::string FileName,
}
bool TLuaEngine::TimedEvent::Expired() {
auto Waited = (std::chrono::high_resolution_clock::now() - LastCompletion);
auto Waited = (TimeType::now() - LastCompletion);
return Waited >= Duration;
}
void TLuaEngine::TimedEvent::Reset() {
LastCompletion = std::chrono::high_resolution_clock::now();
LastCompletion = TimeType::now();
}

File diff suppressed because it is too large Load Diff

View File

@@ -33,26 +33,16 @@ void TPPSMonitor::operator()() {
Application::SetPPS("-");
continue;
}
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
std::shared_ptr<TClient> c;
{
ReadLock Lock(mServer.GetClientMutex());
if (!ClientPtr.expired()) {
c = ClientPtr.lock();
} else
return true;
}
if (c->GetCarCount() > 0) {
mServer.ForEachClient([&](const auto& Client) {
if (Client->GetCarCount() > 0) {
C++;
V += c->GetCarCount();
V += Client->GetCarCount();
}
// kick on "no ping"
if (c->SecondsSinceLastPing() > (20 * 60)) {
beammp_debug("client " + std::string("(") + std::to_string(c->GetID()) + ")" + c->GetName() + " timing out: " + std::to_string(c->SecondsSinceLastPing()) + ", pps: " + Application::PPS());
TimedOutClients.push_back(c);
if (Client->SecondsSinceLastPing() > (20 * 60)) {
beammp_debugf("Client {} ({}) timing out: {}s since last contact", Client->GetName(), Client->GetID(), Client->SecondsSinceLastPing());
TimedOutClients.push_back(Client);
}
return true;
});
for (auto& ClientToKick : TimedOutClients) {
Network().ClientKick(*ClientToKick, "Timeout (no ping for way too long)");

View File

@@ -5,31 +5,79 @@
namespace fs = std::filesystem;
std::string TResourceManager::FormatForBackend(const ModMap& mods) {
std::string monkey;
for (const auto& [name, size] : mods) {
monkey += fs::path(name).filename().string() + ';';
}
return monkey;
}
std::string TResourceManager::FormatForClient(const ModMap& mods) {
std::string monkey;
for (const auto& [name, size] : mods) {
monkey += '/' + name + ';';
}
for (const auto& [name, size] : mods) {
monkey += std::to_string(size) + ';';
}
return monkey;
}
/// @brief Sanitizes a requested mod string
/// @param pathString Raw mod path string
/// @param mods List of allowed mods for this client
/// @return Error, if any
std::optional<std::string> TResourceManager::IsModValid(std::string& pathString, const ModMap& mods) {
auto path = fs::path(pathString);
if (!path.has_filename()) {
beammp_warn("File " + pathString + " is not a file!");
return { "the requested file doesn't contain a valid filename" };
}
auto BasePath = fs::path(Application::GetSettingString(StrResourceFolder) + "/Client");
auto CombinedPath = fs::path(BasePath.string() + pathString).lexically_normal();
// beammp_infof("path: {}, base: {}, combined: {}", pathString, BasePath.string(), CombinedPath.string());
if (!std::filesystem::exists(CombinedPath)) {
beammp_warn("File " + pathString + " could not be accessed!");
return { "the requested file doesn't exist or couldn't be accessed" };
}
auto relative = fs::relative(CombinedPath, BasePath);
if (mods.count(relative.string()) == 0) {
beammp_warn("File " + pathString + " is disallowed for this player!");
return { "the requested file is disallowed for this player" };
}
pathString = CombinedPath.string();
return {};
}
TResourceManager::TResourceManager() {
Application::SetSubsystemStatus("ResourceManager", Application::Status::Starting);
std::string Path = Application::Settings.Resource + "/Client";
if (!fs::exists(Path))
fs::create_directories(Path);
for (const auto& entry : fs::directory_iterator(Path)) {
std::string File(entry.path().string());
if (auto pos = File.find(".zip"); pos != std::string::npos) {
if (File.length() - pos == 4) {
std::replace(File.begin(), File.end(), '\\', '/');
mFileList += File + ';';
if (auto i = File.find_last_of('/'); i != std::string::npos) {
++i;
File = File.substr(i, pos - i);
}
mTrimmedList += "/" + fs::path(File).filename().string() + ';';
mFileSizes += std::to_string(size_t(fs::file_size(entry.path()))) + ';';
mMaxModSize += size_t(fs::file_size(entry.path()));
mModsLoaded++;
}
std::string BasePath = Application::GetSettingString(StrResourceFolder) + "/Client";
if (!fs::exists(BasePath))
fs::create_directories(BasePath);
std::vector<std::string> modNames;
auto iterator = fs::recursive_directory_iterator(BasePath, fs::directory_options::follow_directory_symlink | fs::directory_options::skip_permission_denied);
for (const auto& entry : iterator) {
if (iterator.depth() > 0 && !Application::GetSettingBool(StrIncludeSubdirectories))
continue;
if ((entry.is_regular_file() || entry.is_symlink()) && entry.path().extension() == ".zip") {
auto relativePath = fs::relative(entry.path(), BasePath);
mMods[relativePath.string()] = entry.file_size();
mTotalModSize += entry.file_size();
}
}
if (mModsLoaded) {
beammp_info("Loaded " + std::to_string(mModsLoaded) + " Mods");
if (!mMods.empty()) {
beammp_infof("Loaded {} mod{}", mMods.size(), mMods.size() != 1 ? 's' : ' ');
}
Application::SetSubsystemStatus("ResourceManager", Application::Status::Good);

View File

@@ -2,21 +2,21 @@
#include "Common.h"
TScopedTimer::TScopedTimer()
: mStartTime(std::chrono::high_resolution_clock::now()) {
: mStartTime(TimeType::now()) {
}
TScopedTimer::TScopedTimer(const std::string& mName)
: mStartTime(std::chrono::high_resolution_clock::now())
: mStartTime(TimeType::now())
, Name(mName) {
}
TScopedTimer::TScopedTimer(std::function<void(size_t)> OnDestroy)
: OnDestroy(OnDestroy)
, mStartTime(std::chrono::high_resolution_clock::now()) {
, mStartTime(TimeType::now()) {
}
TScopedTimer::~TScopedTimer() {
auto EndTime = std::chrono::high_resolution_clock::now();
auto EndTime = TimeType::now();
auto Delta = EndTime - mStartTime;
size_t TimeDelta = Delta / std::chrono::milliseconds(1);
if (OnDestroy) {

View File

@@ -28,22 +28,22 @@ TSentry::~TSentry() {
void TSentry::PrintWelcome() {
if (mValid) {
if (!Application::Settings.SendErrors) {
if (!Application::GetSettingBool("SendErrors")) {
mValid = false;
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Opted out of error reporting (SendErrors), Sentry disabled.");
} else {
beammp_info("Sentry disabled");
}
} else {
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Sentry started! Reporting errors automatically. This sends data to the developers in case of errors and crashes. You can learn more, turn this message off or opt-out of this in the ServerConfig.toml.");
} else {
beammp_info("Sentry started");
}
}
} else {
if (Application::Settings.SendErrorsMessageEnabled) {
if (Application::GetSettingBool(StrSendErrors)) {
beammp_info("Sentry disabled in unofficial build. Automatic error reporting disabled.");
} else {
beammp_info("Sentry disabled in unofficial build");
@@ -57,8 +57,8 @@ void TSentry::SetupUser() {
}
Application::SetSubsystemStatus("Sentry", Application::Status::Good);
sentry_value_t user = sentry_value_new_object();
if (Application::Settings.Key.size() == 36) {
sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::Settings.Key.c_str()));
if (Application::GetSettingString(StrAuthKey).size() == 36) {
sentry_value_set_by_key(user, "id", sentry_value_new_string(Application::GetSettingString(StrAuthKey).c_str()));
} else {
sentry_value_set_by_key(user, "id", sentry_value_new_string("unauthenticated"));
}

View File

@@ -1,13 +1,15 @@
#include "TServer.h"
#include "Client.h"
#include "Common.h"
#include "CustomAssert.h"
#include "IterationDecision.h"
#include "TNetwork.h"
#include "TPPSMonitor.h"
#include <TLuaPlugin.h>
#include <algorithm>
#include <any>
#include <sstream>
#include <nlohmann/json.hpp>
#include <sstream>
#include "LuaAPI.h"
@@ -15,20 +17,66 @@
#include "Json.h"
static std::optional<std::pair<int, int>> GetPidVid(const std::string& str) {
struct VehiclePacket {
std::string Data;
int Pid;
int Vid;
};
static Result<std::pair<int, int>> GetPidVid(const std::string& str) {
auto IDSep = str.find('-');
if (IDSep == std::string::npos) {
return Error("Invalid packet: Could not parse pid/vid from packet, as there is no '-' separator: '{}'", str);
}
std::string pid = str.substr(0, IDSep);
std::string vid = str.substr(IDSep + 1);
if (pid.find_first_not_of("0123456789") == std::string::npos && vid.find_first_not_of("0123456789") == std::string::npos) {
try {
int PID = stoi(pid);
int VID = stoi(vid);
int PID = std::stoi(pid);
int VID = std::stoi(vid);
return { { PID, VID } };
} catch (const std::exception&) {
return std::nullopt;
return Error("Invalid packet: Could not parse pid/vid from packet, as one or both are not valid numbers: '{}'", str);
}
}
return Error("Invalid packet: Could not parse pid/vid from packet: '{}'", str);
}
static std::optional<VehiclePacket> ParseVehiclePacket(const std::string& Packet, const int playerID) {
if (Packet.size() < 8) { // 2 for code, 3<= for pidvid, 1<= for data, 2 for dividers
// invalid packet
return std::nullopt;
}
// Zp:serverVehicleID:data
// 0-0:data
std::string withoutCode = Packet.substr(3);
auto NameDataSep = withoutCode.find(':', 3);
if (NameDataSep == std::string::npos) {
beammp_debugf("Invalid packet from {} in ParseVehiclePacket: No ':' separator, assuming corrupted packet. Packet: '{}'", playerID, Packet);
return std::nullopt;
}
if (NameDataSep + 1 > withoutCode.size()) {
beammp_debugf("Invalid packet from {} in ParseVehiclePacket: Separator in unexpected place, assuming corrupted packet. Packet: '{}'", playerID, Packet);
return std::nullopt;
}
std::string ServerVehicleID = withoutCode.substr(0, NameDataSep);
std::string Data = withoutCode.substr(NameDataSep + 1);
// parse veh ID
auto MaybePidVid = GetPidVid(ServerVehicleID);
if (MaybePidVid) {
int PID, VID;
std::tie(PID, VID) = MaybePidVid.value();
if (PID == playerID) {
return { { Data, PID, VID } }; // std::vector<char>(Data.begin(), Data.end())
}
}
beammp_debugf("Failed to parse packet from player {}", playerID);
return std::nullopt;
}
@@ -75,15 +123,48 @@ TEST_CASE("GetPidVid") {
}
}
TEST_CASE("ParseVehiclePacket") {
SUBCASE("Valid packet") {
const auto valid = ParseVehiclePacket("Zp:0-0:{jsonstring}", 0);
CHECK(valid.has_value());
}
SUBCASE("Valid packet 2") {
const auto packet = ParseVehiclePacket("Zp:12-3:{jsonstring}", 12);
CHECK(packet.has_value());
CHECK_EQ(packet.value().Pid, 12);
CHECK_EQ(packet.value().Vid, 3);
}
SUBCASE("Missing packet") {
const auto valid = ParseVehiclePacket("", 0);
CHECK(!valid.has_value());
}
SUBCASE("Missing ServerVehicleID") {
const auto valid = ParseVehiclePacket("Zp:{jsonstring}", 0);
CHECK(!valid.has_value());
}
SUBCASE("Missing data") {
const auto valid = ParseVehiclePacket("Zp:0-0:", 0);
CHECK(!valid.has_value());
}
SUBCASE("Incorrect Pid") {
const auto valid = ParseVehiclePacket("Zp:0-0:{jsonstring}", 1);
CHECK(!valid.has_value());
}
SUBCASE("Incorrect Pid 2") {
const auto valid = ParseVehiclePacket("Zp:12-0:{jsonstring}", 13);
CHECK(!valid.has_value());
}
}
TServer::TServer(const std::vector<std::string_view>& Arguments) {
beammp_info("BeamMP Server v" + Application::ServerVersionString());
Application::SetSubsystemStatus("Server", Application::Status::Starting);
if (Arguments.size() > 1) {
Application::Settings.CustomIP = Arguments[0];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');
auto p = Application::Settings.CustomIP.find_first_not_of(".0123456789");
if (p != std::string::npos || n != 3 || Application::Settings.CustomIP.substr(0, 3) == "127") {
Application::Settings.CustomIP.clear();
Application::SetSetting(StrCustomIP, std::string(Arguments[0]));
auto CustomIP = Application::GetSettingString(StrCustomIP);
size_t n = std::count(CustomIP.begin(), CustomIP.end(), '.');
auto p = Application::GetSettingString(StrCustomIP).find_first_not_of(".0123456789");
if (p != std::string::npos || n != 3 || CustomIP.substr(0, 3) == "127") {
Application::SetSetting(StrCustomIP, "");
beammp_warn("IP Specified is invalid! Ignoring");
} else {
beammp_info("server started with custom IP");
@@ -93,23 +174,32 @@ TServer::TServer(const std::vector<std::string_view>& Arguments) {
}
void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
if (!WeakClientPtr.expired()) {
TClient& Client = *WeakClientPtr.lock();
beammp_debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
Client.ClearCars();
std::shared_ptr<TClient> LockedClientPtr { nullptr };
try {
LockedClientPtr = WeakClientPtr.lock();
} catch (const std::exception&) {
// silently fail, as there's nothing to do
return;
}
beammp_assert(LockedClientPtr != nullptr);
TClient& Client = *LockedClientPtr;
beammp_debug("removing client " + Client.GetName() + " (" + std::to_string(ClientCount()) + ")");
// TODO: Send delete packets for all cars
Client.ClearCars();
WriteLock Lock(mClientsMutex);
mClients.erase(WeakClientPtr.lock());
}
void TServer::RemoveClientById(int Id) {
auto Client = GetClient(*this, Id);
if (Client) {
Client->ClearCars();
WriteLock Lock(mClientsMutex);
mClients.erase(WeakClientPtr.lock());
mClients.erase(Client);
}
}
std::weak_ptr<TClient> TServer::InsertNewClient() {
beammp_debug("inserting new client (" + std::to_string(ClientCount()) + ")");
WriteLock Lock(mClientsMutex);
auto [Iter, Replaced] = mClients.insert(std::make_shared<TClient>(*this));
return *Iter;
}
void TServer::ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn) {
void TServer::ForEachClientWeak(const std::function<bool(std::weak_ptr<TClient>)>& Fn) {
decltype(mClients) Clients;
{
ReadLock lock(mClientsMutex);
@@ -127,12 +217,11 @@ size_t TServer::ClientCount() const {
return mClients.size();
}
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Packet, TPPSMonitor& PPSMonitor, TNetwork& Network) {
if (Packet.find("Zp") != std::string::npos && Packet.size() > 500) {
// abort();
}
if (Packet.substr(0, 4) == "ABG:") {
Packet = DeComp(Packet.substr(4));
void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TNetwork& Network) {
constexpr std::string_view ABG = "ABG:";
if (Packet.size() >= ABG.size() && std::equal(Packet.begin(), Packet.begin() + ABG.size(), ABG.begin(), ABG.end())) {
Packet.erase(Packet.begin(), Packet.begin() + ABG.size());
Packet = DeComp(Packet);
}
if (Packet.empty()) {
return;
@@ -146,46 +235,53 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
std::any Res;
char Code = Packet.at(0);
std::string StringPacket(reinterpret_cast<const char*>(Packet.data()), Packet.size());
// V to Y
if (Code <= 89 && Code >= 86) {
PPSMonitor.IncrementInternalPPS();
Network.SendToAll(LockedClient.get(), Packet, false, false);
if (HandleVehicleUpdate(StringPacket, LockedClient->GetID())) {
Network.SendToAll(LockedClient.get(), Packet, false, false);
} else {
beammp_debugf("Invalid vehicle update packet received from '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
}
return;
}
switch (Code) {
case 'H': // initial connection
beammp_trace(std::string("got 'H' packet: '") + Packet + "' (" + std::to_string(Packet.size()) + ")");
if (!Network.SyncClient(Client)) {
// TODO handle
}
return;
case 'p':
if (!Network.Respond(*LockedClient, ("p"), false)) {
if (!Network.Respond(*LockedClient, StringToVector("p"), false)) {
// failed to send
if (LockedClient->GetStatus() > -1) {
LockedClient->SetStatus(-1);
}
LockedClient->Disconnect("Failed to send ping");
} else {
Network.UpdatePlayer(*LockedClient);
}
return;
case 'O':
if (Packet.length() > 1000) {
beammp_debug(("Received data from: ") + LockedClient->GetName() + (" Size: ") + std::to_string(Packet.length()));
if (Packet.size() > 1000) {
beammp_debug(("Received data from: ") + LockedClient->GetName() + (" Size: ") + std::to_string(Packet.size()));
}
ParseVehicle(*LockedClient, Packet, Network);
return;
case 'J':
beammp_trace(std::string(("got 'J' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
Network.SendToAll(LockedClient.get(), Packet, false, true);
ParseVehicle(*LockedClient, StringPacket, Network);
return;
case 'C': {
beammp_trace(std::string(("got 'C' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
if (Packet.length() < 4 || Packet.find(':', 3) == std::string::npos)
if (Packet.size() < 4 || std::find(Packet.begin() + 3, Packet.end(), ':') == Packet.end())
break;
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Packet.substr(Packet.find(':', 3) + 2));
const auto PacketAsString = std::string(reinterpret_cast<const char*>(Packet.data()), Packet.size());
std::string Message = "";
const auto ColonPos = PacketAsString.find(':', 3);
if (ColonPos != std::string::npos && ColonPos + 2 < PacketAsString.size()) {
Message = PacketAsString.substr(ColonPos + 2);
}
if (Message.empty()) {
beammp_debugf("Empty chat message received from '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
return;
}
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onChatMessage", "", LockedClient->GetID(), LockedClient->GetName(), Message);
TLuaEngine::WaitForAll(Futures);
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), Packet.substr(Packet.find(':', 3) + 1));
LogChatMessage(LockedClient->GetName(), LockedClient->GetID(), PacketAsString.substr(PacketAsString.find(':', 3) + 1));
if (std::any_of(Futures.begin(), Futures.end(),
[](const std::shared_ptr<TLuaResult>& Elem) {
return !Elem->Error
@@ -194,22 +290,23 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
})) {
break;
}
Network.SendToAll(nullptr, Packet, true, true);
std::string SanitizedPacket = fmt::format("C:{}: {}", LockedClient->GetName(), Message);
Network.SendToAll(nullptr, StringToVector(SanitizedPacket), true, true);
return;
}
case 'E':
beammp_trace(std::string(("got 'E' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
HandleEvent(*LockedClient, Packet);
HandleEvent(*LockedClient, StringPacket);
return;
case 'N':
beammp_trace("got 'N' packet (" + std::to_string(Packet.size()) + ")");
Network.SendToAll(LockedClient.get(), Packet, false, true);
return;
case 'Z': // position packet
PPSMonitor.IncrementInternalPPS();
Network.SendToAll(LockedClient.get(), Packet, false, false);
HandlePosition(*LockedClient, Packet);
if (HandlePosition(*LockedClient, StringPacket)) {
Network.SendToAll(LockedClient.get(), Packet, false, false);
} else {
beammp_debugf("Invalid vehicle position packet received from '{}' ({}), ignoring it", LockedClient->GetName(), LockedClient->GetID());
}
default:
return;
}
@@ -218,6 +315,10 @@ void TServer::GlobalParser(const std::weak_ptr<TClient>& Client, std::string Pac
void TServer::HandleEvent(TClient& c, const std::string& RawData) {
// E:Name:Data
// Data is allowed to have ':'
if (RawData.size() < 2) {
beammp_debugf("Client '{}' ({}) tried to send an empty event, ignoring", c.GetName(), c.GetID());
return;
}
auto NameDataSep = RawData.find(':', 2);
if (NameDataSep == std::string::npos) {
beammp_warn("received event in invalid format (missing ':'), got: '" + RawData + "'");
@@ -235,7 +336,7 @@ bool TServer::IsUnicycle(TClient& c, const std::string& CarJson) {
return true;
}
} catch (const std::exception& e) {
beammp_error("Failed to parse vehicle data as json for client " + std::to_string(c.GetID()) + ": '" + CarJson + "'");
beammp_warn("Failed to parse vehicle data as json for client " + std::to_string(c.GetID()) + ": '" + CarJson + "'.");
}
return false;
}
@@ -245,12 +346,12 @@ bool TServer::ShouldSpawn(TClient& c, const std::string& CarJson, int ID) {
c.SetUnicycleID(ID);
return true;
} else {
return c.GetCarCount() < Application::Settings.MaxCars;
return c.GetCarCount() < Application::GetSettingInt(StrMaxCars);
}
}
void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Network) {
if (Pckt.length() < 4)
if (Pckt.length() < 6)
return;
std::string Packet = Pckt;
char Code = Packet.at(1);
@@ -259,10 +360,10 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
std::string Data = Packet.substr(3), pid, vid;
switch (Code) { // Spawned Destroyed Switched/Moved NotFound Reset
case 's':
beammp_trace(std::string(("got 'Os' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
beammp_tracef("got 'Os' packet: '{}' ({})", Packet, Packet.size());
if (Data.at(0) == '0') {
int CarID = c.GetOpenCarID();
beammp_debug(c.GetName() + (" created a car with ID ") + std::to_string(CarID));
beammp_debugf("'{}' created a car with ID {}", c.GetName(), CarID);
std::string CarJson = Packet.substr(5);
Packet = "Os:" + c.GetRoles() + ":" + c.GetName() + ":" + std::to_string(c.GetID()) + "-" + std::to_string(CarID) + ":" + CarJson;
@@ -275,16 +376,16 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
if (ShouldSpawn(c, CarJson, CarID) && !ShouldntSpawn) {
c.AddNewCar(CarID, Packet);
Network.SendToAll(nullptr, Packet, true, true);
Network.SendToAll(nullptr, StringToVector(Packet), true, true);
} else {
if (!Network.Respond(c, Packet, true)) {
if (!Network.Respond(c, StringToVector(Packet), true)) {
// TODO: handle
}
std::string Destroy = "Od:" + std::to_string(c.GetID()) + "-" + std::to_string(CarID);
if (!Network.Respond(c, Destroy, true)) {
if (!Network.Respond(c, StringToVector(Destroy), true)) {
// TODO: handle
}
beammp_debug(c.GetName() + (" (force : car limit/lua) removed ID ") + std::to_string(CarID));
beammp_debugf("{} (force : car limit/lua) removed ID {}", c.GetName(), CarID);
}
}
return;
@@ -306,14 +407,14 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
FoundPos = FoundPos == std::string::npos ? 0 : FoundPos; // attempt at sanitizing this
if ((c.GetUnicycleID() != VID || IsUnicycle(c, Packet.substr(FoundPos)))
&& !ShouldntAllow) {
Network.SendToAll(&c, Packet, false, true);
Network.SendToAll(&c, StringToVector(Packet), false, true);
Apply(c, VID, Packet);
} else {
if (c.GetUnicycleID() == VID) {
c.SetUnicycleID(-1);
}
std::string Destroy = "Od:" + std::to_string(c.GetID()) + "-" + std::to_string(VID);
Network.SendToAll(nullptr, Destroy, true, true);
Network.SendToAll(nullptr, StringToVector(Destroy), true, true);
c.DeleteCar(VID);
}
}
@@ -321,7 +422,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
}
case 'd': {
beammp_trace(std::string(("got 'Od' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
auto MaybePidVid = GetPidVid(Data);
auto MaybePidVid = GetPidVid(Data.substr(0, Data.find(':', 1)));
if (MaybePidVid) {
std::tie(PID, VID) = MaybePidVid.value();
}
@@ -329,7 +430,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
if (c.GetUnicycleID() == VID) {
c.SetUnicycleID(-1);
}
Network.SendToAll(nullptr, Packet, true, true);
Network.SendToAll(nullptr, StringToVector(Packet), true, true);
// TODO: should this trigger on all vehicle deletions?
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onVehicleDeleted", "", c.GetID(), VID));
c.DeleteCar(VID);
@@ -339,7 +440,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
}
case 'r': {
beammp_trace(std::string(("got 'Or' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
auto MaybePidVid = GetPidVid(Data);
auto MaybePidVid = GetPidVid(Data.substr(0, Data.find(':', 1)));
if (MaybePidVid) {
std::tie(PID, VID) = MaybePidVid.value();
}
@@ -347,16 +448,16 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
if (PID != -1 && VID != -1 && PID == c.GetID()) {
Data = Data.substr(Data.find('{'));
LuaAPI::MP::Engine->ReportErrors(LuaAPI::MP::Engine->TriggerEvent("onVehicleReset", "", c.GetID(), VID, Data));
Network.SendToAll(&c, Packet, false, true);
Network.SendToAll(&c, StringToVector(Packet), false, true);
}
return;
}
case 't':
beammp_trace(std::string(("got 'Ot' packet: '")) + Packet + ("' (") + std::to_string(Packet.size()) + (")"));
Network.SendToAll(&c, Packet, false, true);
Network.SendToAll(&c, StringToVector(Packet), false, true);
return;
case 'm':
Network.SendToAll(&c, Packet, true, true);
Network.SendToAll(&c, StringToVector(Packet), true, true);
return;
default:
beammp_trace(std::string(("possibly not implemented: '") + Packet + ("' (") + std::to_string(Packet.size()) + (")")));
@@ -424,20 +525,19 @@ void TServer::InsertClient(const std::shared_ptr<TClient>& NewClient) {
(void)mClients.insert(NewClient);
}
void TServer::HandlePosition(TClient& c, const std::string& Packet) {
// Zp:serverVehicleID:data
std::string withoutCode = Packet.substr(3);
auto NameDataSep = withoutCode.find(':', 2);
std::string ServerVehicleID = withoutCode.substr(2, NameDataSep - 2);
std::string Data = withoutCode.substr(NameDataSep + 1);
bool TServer::HandlePosition(TClient& c, const std::string& PacketStr) {
auto MaybePacket = ParseVehiclePacket(PacketStr, c.GetID());
// parse veh ID
auto MaybePidVid = GetPidVid(ServerVehicleID);
if (MaybePidVid) {
int PID = -1;
int VID = -1;
std::tie(PID, VID) = MaybePidVid.value();
c.SetCarPosition(VID, Data);
if (MaybePacket) {
auto packet = MaybePacket.value();
c.SetCarPosition(packet.Vid, packet.Data);
return true;
}
return false;
}
bool TServer::HandleVehicleUpdate(const std::string& PacketStr, const int playerID) {
auto MaybePacket = ParseVehiclePacket(PacketStr, playerID);
return MaybePacket.has_value();
}

15
src/Uuid.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "Uuid.h"
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <fmt/format.h>
#include <fmt/ostream.h>
std::string uuid::GenerateUuid() {
static thread_local boost::uuids::random_generator Generator {};
boost::uuids::uuid Id { Generator() };
return boost::uuids::to_string(Id);
}

View File

@@ -73,6 +73,7 @@ int main(int argc, char** argv) {
int BeamMPServerMain(MainArguments Arguments) {
setlocale(LC_ALL, "C");
Application::InitializeConsole();
Application::Console().Internal().set_prompt("");
ArgsParser Parser;
Parser.RegisterArgument({ "help" }, ArgsParser::NONE);
Parser.RegisterArgument({ "version" }, ArgsParser::NONE);
@@ -83,16 +84,14 @@ int BeamMPServerMain(MainArguments Arguments) {
return 1;
}
if (Parser.FoundArgument({ "help" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw(sCommandlineArguments);
return 0;
}
if (Parser.FoundArgument({ "version" })) {
Application::Console().Internal().set_prompt("");
Application::Console().WriteRaw("BeamMP-Server v" + Application::ServerVersionString());
Application::Console().WriteRaw(fmt::format("BeamMP Server v{} ({})", Application::ServerVersionString(), BEAMMP_GIT_HASH));
return 0;
}
std::string ConfigPath = "ServerConfig.toml";
if (Parser.FoundArgument({ "config" })) {
auto MaybeConfigPath = Parser.GetValueOfArgument({ "config" });
@@ -115,36 +114,37 @@ int BeamMPServerMain(MainArguments Arguments) {
Application::SetSubsystemStatus("Main", Application::Status::Starting);
Application::Console().StartLoggingToFile();
SetupSignalHandlers();
beammp_infof("BeamMP Server v{} ({})", Application::ServerVersionString(), BEAMMP_GIT_HASH);
TConfig Config(ConfigPath);
if (Config.Failed()) {
beammp_info("Closing in 10 seconds");
// loop to make it possible to ctrl+c instead
Application::SleepSafeSeconds(5);
beammp_info("Closing in 5 seconds");
Application::SleepSafeSeconds(5);
Application::GracefullyShutdown();
return 1;
}
Application::Console().Internal().set_prompt("> ");
Application::Console().StartLoggingToFile();
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, std::chrono::seconds(5));
});
TServer Server(Arguments.List);
TConfig Config(ConfigPath);
auto LuaEngine = std::make_shared<TLuaEngine>();
LuaEngine->SetServer(&Server);
Application::Console().InitializeLuaConsole(*LuaEngine);
if (Config.Failed()) {
beammp_info("Closing in 10 seconds");
// loop to make it possible to ctrl+c instead
for (size_t i = 0; i < 20; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return 1;
}
RegisterThread("Main");
beammp_trace("Running in debug mode on a debug build");
@@ -158,9 +158,9 @@ int BeamMPServerMain(MainArguments Arguments) {
PPSMonitor.SetNetwork(Network);
Application::CheckForUpdates();
TPluginMonitor PluginMonitor(fs::path(Application::Settings.Resource) / "Server", LuaEngine);
TPluginMonitor PluginMonitor(fs::path(Application::GetSettingString(StrResourceFolder)) / "Server", LuaEngine);
if (Application::Settings.HTTPServerEnabled) {
if (Application::GetSettingBool(StrHTTPServerEnabled)) {
Http::Server::THttpServerInstance HttpServerInstance {};
}