This commit is contained in:
Anonymous275 2020-07-02 00:20:35 +03:00
parent 1b8c7abea5
commit 6c93ea7fb2
11 changed files with 77 additions and 39 deletions

View File

@ -6,7 +6,6 @@
#include "../logger.h"
#include <thread>
std::set<Lua*> PluginEngine;
bool NewFile(const std::string&Path){
for(Lua*Script : PluginEngine){
if(Path == Script->GetFileName())return false;

View File

@ -95,15 +95,30 @@ int lua_TriggerEventG(lua_State *L)
}
return 0;
}
void CallAsync(Lua* Script,const std::string&FuncName,LuaArg* args){
Script->CallFunction(FuncName,args);
void CallAsync(Lua* lua,const std::string& FuncName,LuaArg* args){
if(lua->HasThread){
SendError(lua->GetState(),"CreateThread there is a thread already running");
return;
}
lua->HasThread = true;
std::this_thread::sleep_for(std::chrono::milliseconds(200));
lua_getglobal(lua->GetState(), FuncName.c_str());
if(lua_isfunction(lua->GetState(), -1)) {
int Size = 0;
if(args != nullptr){
Size = args->args.size();
args->PushArgs(lua->GetState());
}
CheckLua(lua->GetState(), lua_pcall(lua->GetState(), Size, 0, 0));
}
lua->HasThread = false;
}
int lua_CreateThread(lua_State *L){
int Args = lua_gettop(L);
Lua* Script = GetScript(L);
if(Args > 0){
if(lua_isstring(L,1)) {
std::thread Worker(CallAsync,Script,lua_tostring(L,1),CreateArg(L,Args));
std::string STR = lua_tostring(L,1);
std::thread Worker(CallAsync,GetScript(L),STR,CreateArg(L,Args));
Worker.detach();
}else SendError(L,"CreateThread wrong argument [1] need string");
}else SendError(L,"CreateThread not enough arguments");
@ -223,12 +238,12 @@ int lua_sendChat(lua_State *L){
if(lua_isstring(L,2)){
int ID = lua_tointeger(L,1);
if(ID == -1){
std::string Packet = "C:Server: " + std::string(lua_tostring(L, 1));
std::string Packet = "C:Server: " + std::string(lua_tostring(L, 2));
SendToAll(nullptr,Packet,true,true);
}else{
Client*c = GetClient(ID);
if(c != nullptr) {
std::string Packet = "C:Server: " + std::string(lua_tostring(L, 1));
std::string Packet = "C:Server: " + std::string(lua_tostring(L, 2));
Respond(c, Packet, true);
}else SendError(L,"SendChatMessage invalid argument [1] invalid ID");
}
@ -282,23 +297,24 @@ void Lua::Init(){
lua_register(luaState,"Sleep",lua_Sleep);
Reload();
}
void Lua::Reload(){
if(CheckLua(luaState,luaL_dofile(luaState,FileName.c_str()))){
CallFunction("onInit",{});
}
}
int Lua::CallFunction(const std::string&FuncName,LuaArg* Arg){
int Lua::CallFunction(const std::string& FuncName,LuaArg* Arg){
lua_getglobal(luaState, FuncName.c_str());
if (lua_isfunction(luaState, -1)) {
if(lua_isfunction(luaState, -1)) {
int Size = 0;
if(Arg != nullptr){
Size = Arg->args.size();
Arg->PushArgs(luaState);
}
if(CheckLua(luaState, lua_pcall(luaState, Size, 1, 0))){
if(lua_isnumber(luaState,-1)){
return lua_tointeger(luaState,-1);
if (CheckLua(luaState, lua_pcall(luaState, Size, 1, 0))) {
if (lua_isnumber(luaState, -1)) {
return lua_tointeger(luaState, -1);
}
}
}

View File

@ -5,6 +5,7 @@
#pragma once
#include <set>
#include <any>
#include <thread>
#include <vector>
#include <iostream>
#include <filesystem>
@ -42,9 +43,9 @@ private:
public:
void RegisterEvent(const std::string&Event,const std::string&FunctionName);
int CallFunction(const std::string& FuncName,LuaArg* args);
std::string GetRegistered(const std::string&Event);
void UnRegisterEvent(const std::string&Event);
int CallFunction(const std::string&FuncName,LuaArg* args);
void SetLastWrite(fs::file_time_type time);
bool IsRegistered(const std::string&Event);
void SetPluginName(const std::string&Name);
@ -52,6 +53,7 @@ public:
fs::file_time_type GetLastWrite();
std::string GetPluginName();
std::string GetFileName();
bool HasThread = false;
lua_State* GetState();
void Reload();
void Init();

View File

@ -8,6 +8,5 @@ std::set<Client*> Clients;
void NetMain() {
std::thread TCP(TCPServerMain);
TCP.detach();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
UDPServerMain();
}

View File

@ -8,7 +8,7 @@
std::string StatReport = "-";
int PPS = 0;
[[noreturn]] void Monitor(){
int R,C,V=0;
int R,C,V;
while(true){
if(Clients.empty()){
StatReport = "-";
@ -24,7 +24,6 @@ int PPS = 0;
StatReport = "-";
}else{
R = (PPS/C)/V;
std::cout << PPS << std::endl;
StatReport = std::to_string(R);
}
PPS = 0;

View File

@ -58,11 +58,11 @@ void Check(Sequence* S){
}
}
int Max(){
int T = MaxPlayers;
int M = MaxPlayers;
for(Client*c : Clients){
if(c->GetRole() == "MDEV")T--;
if(c->GetRole() == "MDEV")M--;
}
return T;
return M;
}
void Identification(SOCKET TCPSock){
auto* S = new Sequence;
@ -73,7 +73,7 @@ void Identification(SOCKET TCPSock){
S->Done = true;
if(Ver.size() > 3 && Ver.substr(0,2) == "VC"){
Ver = Ver.substr(2);
if(Ver.length() > 4 || Ver < ClientVersion){
if(Ver.length() > 4 || Ver != ClientVersion){
closesocket(TCPSock);
return;
}

View File

@ -4,6 +4,7 @@
extern std::string ServerVersion;
extern std::string ClientVersion;
extern std::string ServerName;
extern std::string ServerDesc;
extern std::string StatReport;
extern std::string FileSizes;
extern std::string Resource;

View File

@ -5,13 +5,15 @@
#include <iostream>
#include <fstream>
#include <string>
#include <thread>
#include "logger.h"
void GenerateConfig();
std::string RemoveComments(const std::string& Line);
void SetValues(const std::string& Line, int Index);
std::string MapName = "/levels/gridmap/info.json";
std::string ServerName = "BeamMP Server";
std::string ServerName = "BeamMP New Server";
std::string ServerDesc = "BeamMP Default Description";
std::string Resource = "Resources";
std::string Key;
bool Private = false;
@ -25,11 +27,22 @@ void ParseConfig(){
std::ifstream InFileStream;
InFileStream.open("Server.cfg");
if(InFileStream.good()){ //Checks if Config Exists
info("Config Found Updating Values");
std::string line;
int index = 1;
while (getline(InFileStream, line)) {
if(line.rfind('#', 0) != 0){ //Checks if it starts as Comment
index++;
}
if(index-1 < 11){
error("Outdated/Incorrect config please remove it server will close in 5 secs");
std::this_thread::sleep_for(std::chrono::seconds(5));
exit(3);
}
InFileStream.close();
InFileStream.open("Server.cfg");
info("Config Found Updating Values");
index = 1;
while (getline(InFileStream, line)) {
if(line.rfind('#', 0) != 0 && line.rfind(' ', 0) != 0){ //Checks if it starts as Comment
std::string CleanLine = RemoveComments(line); //Cleans it from the Comments
SetValues(CleanLine,index); //sets the values
index++;
@ -80,9 +93,11 @@ void SetValues(const std::string& Line, int Index) {
break;
case 7 : ServerName = Data; //Name
break;
case 8 : Resource = Data; //File name
case 8 : ServerDesc = Data; //desc
break;
case 9 : Key = Data; //File name
case 9 : Resource = Data; //File name
break;
case 10 : Key = Data; //File name
}
}
@ -100,6 +115,7 @@ void GenerateConfig(){
"MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"/levels/gridmap/info.json\" # Default Map\n"
"Name = \"BeamMP New Server\" # Server Name\n"
"Desc = \"BeamMP Default Description\" # Server Description\n"
"use = \"Resources\" # Resource file name\n"
"AuthKey = \"\" # Auth Key";
FileStream.close();

View File

@ -36,7 +36,8 @@ void Heartbeat(){
R = "uuid="+Key+"&players="+std::to_string(Clients.size())+"&maxplayers="+std::to_string(MaxPlayers)+"&port="
+ std::to_string(Port) + "&map=" + MapName + "&private="+State+"&version="+ServerVersion+
"&clientversion="+ClientVersion+"&name="+ServerName+"&pps="+StatReport+"&modlist="+FileList+
"&modstotalsize="+std::to_string(MaxModSize)+"&modstotal="+std::to_string(ModsLoaded);
"&modstotalsize="+std::to_string(MaxModSize)+"&modstotal="+std::to_string(ModsLoaded)
+"&playerslist="+GetPlayers()+"&desc="+ServerDesc;
if(!CustomIP.empty())R+="&ip="+CustomIP;
//https://beamng-mp.com/heartbeatv2
T = PostHTTP(HTA("68747470733a2f2f6265616d6e672d6d702e636f6d2f6865617274626561747632"),R);

View File

@ -61,30 +61,34 @@ std::stringstream getDate() {
void info(const std::string& toPrint) {
if (loggerlevel <= 2){
std::cout << getDate().str() << "[INFO] " << toPrint << std::endl;
addToLog(getDate().str() + "[INFO] " + toPrint + "\n");
std::string Print = getDate().str() + "[INFO] " + toPrint + "\n";
std::cout << Print;
addToLog(Print);
}
}
void error(const std::string& toPrint) {
if (loggerlevel <= 4) {
std::cout << getDate().str() << "[ERROR] " << toPrint << std::endl;
addToLog(getDate().str() + "[ERROR] " + toPrint + "\n");
std::string Print = getDate().str() + "[ERROR] " + toPrint + "\n";
std::cout << Print;
addToLog(Print);
}
}
void warn(const std::string& toPrint) {
if (loggerlevel <= 3) {
std::cout << getDate().str() << "[WARN] " << toPrint << std::endl;
addToLog(getDate().str() + "[WARN] " + toPrint + "\n");
std::string Print = getDate().str() + "[WARN] " + toPrint + "\n";
std::cout << Print;
addToLog(Print);
}
}
void debug(const std::string& toPrint) {
if (loggerlevel <= 1) {
std::cout << getDate().str() << "[DEBUG] " << toPrint << std::endl;
addToLog(getDate().str() + "[DEBUG] " + toPrint + "\n");
std::string Print = getDate().str() + "[DEBUG] " + toPrint + "\n";
std::cout << Print;
addToLog(Print);
}
}

View File

@ -15,8 +15,8 @@ void ParseConfig();
void addToLog(const std::string& Data);
//void ServerMain(int Port, int MaxClients);
void HeartbeatInit();
std::string ServerVersion = "0.42";
std::string ClientVersion = "1.41";
std::string ServerVersion = "0.43";
std::string ClientVersion = "1.43";
std::string CustomIP;
void HandleResources(std::string path);
void StatInit();
@ -32,9 +32,9 @@ int main(int argc, char* argv[]) {
warn("IP Specified is invalid!");
}else info("Started with custom ip : " + CustomIP);
}
info("BeamMP Server Running version " + ServerVersion);
LogInit();
ParseConfig();
info("BeamMP Server Running version " + ServerVersion);
HandleResources(Resource);
HeartbeatInit();
if(Debug)DebugData();
@ -57,7 +57,8 @@ void DebugData(){
debug("Max Cars : " + std::to_string(MaxCars));
debug("MaxPlayers : " + std::to_string(MaxPlayers));
debug("MapName : " + MapName);
debug("ServerName : " + ServerName );
debug("ServerName : " + ServerName);
debug("ServerDesc : " + ServerDesc);
debug("File : " + Resource);
debug("Auth Key : " + Key);
}