Functions and new Config

This commit is contained in:
Anonymous275 2020-04-09 21:32:32 +03:00
parent 3c244c7e7f
commit 640a9c2e54
7 changed files with 49 additions and 49 deletions

View File

@ -1,6 +1,7 @@
// ///
// Created by Anonymous275 on 4/2/2020. /// Created by Anonymous275 on 4/2/2020
// ///
#include "enet.h" #include "enet.h"
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -8,7 +9,6 @@
#include "../logger.h" #include "../logger.h"
std::vector<std::string> Split(const std::string& String,const std::string& delimiter); std::vector<std::string> Split(const std::string& String,const std::string& delimiter);
void OnConnect(ENetPeer*peer,const std::string& data);
void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data
std::string Packet = (char*)packet->data; std::string Packet = (char*)packet->data;
@ -21,12 +21,7 @@ void ParseData(ENetPacket*packet,ENetPeer*peer){ //here we will parse the data
split = Split(header, ":"); //1st is reliable - 2nd is Code - 3rd is VehID split = Split(header, ":"); //1st is reliable - 2nd is Code - 3rd is VehID
} }
if(!data.empty()){ if(!data.empty()){
switch (stoi(split.at(1))){ std::cout << data << std::endl;
case 2000:
OnConnect(peer,data);
break;
}
//std::cout << data << std::endl;
} }
} }

View File

@ -1,6 +1,6 @@
// ///
// Created by Anonymous275 on 4/2/2020. /// Created by Anonymous275 on 4/2/2020
// ///
#define ENET_IMPLEMENTATION #define ENET_IMPLEMENTATION
#include "enet.h" #include "enet.h"
@ -9,6 +9,7 @@
#include "../logger.h" #include "../logger.h"
void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser void ParseData(ENetPacket*packet,ENetPeer*peer); //Data Parser
void OnConnect(ENetPeer*peer);
ENetPacket* packet; ENetPacket* packet;
@ -18,16 +19,18 @@ void host_server(ENetHost *server) {
while (enet_host_service(server, &event, 2) > 0) { while (enet_host_service(server, &event, 2) > 0) {
switch (event.type) { switch (event.type) {
case ENET_EVENT_TYPE_CONNECT: case ENET_EVENT_TYPE_CONNECT:
printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port); //Help xD printf("A new client connected from %x:%u.\n", event.peer->address.host, event.peer->address.port);
//the data should be the client info could be name for now it's Client information //the data should be the client info could be name for now it's Client information
event.peer->Name = (void *)"Client information"; event.peer->Name = (void *)"Client information";
event.peer->gameVehicleID[0] = 15; event.peer->gameVehicleID[0] = 15;
event.peer->serverVehicleID = 17; event.peer->serverVehicleID = 17;
OnConnect(event.peer);
break; break;
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
ParseData(event.packet,event.peer/*->dataLength,event.packet->data, (char *)event.peer->data, event.channelID*/); //We grab and Parse the Data ParseData(event.packet,event.peer);
/*->dataLength,event.packet->data, (char *)event.peer->data, event.channelID*/ //We grab and Parse the Data
/* Clean up the packet now that we're done using it. */ /* Clean up the packet now that we're done using it. */
enet_packet_destroy (event.packet); enet_packet_destroy (event.packet);
break; break;

View File

@ -18,13 +18,9 @@ std::vector<std::string> Split(const std::string& String,const std::string& deli
return Val; return Val;
} }
void OnConnect(ENetPeer*peer,const std::string& data){ void OnConnect(ENetPeer*peer){
std::vector<std::string> Data = Split(data,":"); ENetPacket* packet = enet_packet_create ("NameRequest", //Send A Name Request to the Client
if(strcmp((char*)peer->Name,"Client information")==0){ //Checks if the Client has no name strlen ("NameRequest") + 1,
peer->Name = (void *)Data.at(0).c_str(); ENET_PACKET_FLAG_RELIABLE); //Create A reliable packet using the data
char Info[100]; enet_peer_send(peer, 0, packet);
info("Client Name is " + Data.at(0) + " ID : " + std::to_string(peer->connectID)); //ID System //Logs the data
peer->serverVehicleID = (int)peer->connectID; //test to see if it works
info(Data.at(0)+" ServerVehicleID : "+std::to_string(peer->serverVehicleID)+" GameVehicleID : " + std::to_string(peer->gameVehicleID[0]));
}
} }

View File

@ -1,6 +1,6 @@
// ///
// Created by Anonymous275 on 1/28/2020. /// Created by Anonymous275 on 1/28/2020
// ///
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
@ -8,15 +8,16 @@
#include "logger.h" #include "logger.h"
using namespace std; //nameSpace STD using namespace std; //nameSpace STD
void GenerateConfig(); void GenerateConfig();
string RemoveComments(string Line); string RemoveComments(const string& Line);
string convertToString(char* a, int size); string convertToString(char* a, int size);
void SetValues(string Line, int Index); void SetValues(const string& Line, int Index);
void SetMainValues(bool D,int P,int MP,string Name,string serverName); void SetMainValues(bool,int,int,string,string,string);
bool D; bool D;
int P; int P;
int MP; int MP;
string M; string M;
string S; string S;
string F;
//Generates or Reads Config //Generates or Reads Config
@ -34,7 +35,7 @@ void ParseConfig(){
index++; index++;
} }
} }
SetMainValues(D,P,MP,M,S); //gives the values to Main SetMainValues(D,P,MP,M,S,F); //gives the values to Main
}else{ }else{
info("Config Not Found Generating A new One"); info("Config Not Found Generating A new One");
GenerateConfig(); GenerateConfig();
@ -44,12 +45,12 @@ void ParseConfig(){
void SetValues(string Line, int Index) { void SetValues(const string& Line, int Index) {
int i = 0, state = 0; int i = 0, state = 0;
char Data[50] = ""; char Data[50] = "";
bool Switch = false; bool Switch = false;
if (Index > 3) { Switch = true; } if (Index > 3) { Switch = true; }
for (char &c : Line) { for (char c : Line) {
if (Switch) { if (Switch) {
if (c == '\"') { state++; } if (c == '\"') { state++; }
if (state > 0 && state < 2) { if (state > 0 && state < 2) {
@ -71,7 +72,7 @@ void SetValues(string Line, int Index) {
bool Boolean = (convertToString(Data,i-1).find("true") != string::npos);//searches for "true" bool Boolean = (convertToString(Data,i-1).find("true") != string::npos);//searches for "true"
switch (Index){ switch (Index){
case 1 : case 1 :
if(Boolean){D = true;}else{D = false;}//checks and sets the Debug Value D = Boolean;//checks and sets the Debug Value
break; break;
case 2 : P = stoi(Data, &sz);//sets the Port case 2 : P = stoi(Data, &sz);//sets the Port
break; break;
@ -80,6 +81,7 @@ void SetValues(string Line, int Index) {
case 4 : M = Data; //Map case 4 : M = Data; //Map
break; break;
case 5 : S = Data; //Name case 5 : S = Data; //Name
case 6 : F = Data; //File name
} }
} }
@ -94,15 +96,16 @@ void GenerateConfig(){
"Port = 30814 # Port to run the server on\n" "Port = 30814 # Port to run the server on\n"
"MaxPlayers = 10 # Maximum Amount of Clients\n" "MaxPlayers = 10 # Maximum Amount of Clients\n"
"Map = \"levels/gridmap/level.json\"\n" "Map = \"levels/gridmap/level.json\"\n"
"Name = \"BeamNG-MP FTW\""; "Name = \"BeamNG-MP FTW\"\n"
"use = \"/Resources\"";
FileStream.close(); FileStream.close();
} }
string RemoveComments(string Line){ string RemoveComments(const string& Line){
int i = 0; int i = 0;
char Data[50] = ""; char Data[50] = "";
for(char& c : Line) { for(char c : Line) {
if(c == '#'){break;} //when it finds the # it will stop if(c == '#'){break;} //when it finds the # it will stop
Data[i] = c; Data[i] = c;
i++; i++;

View File

@ -1,6 +1,6 @@
// ///
// Created by Mitch on 04/02/2020. /// Created by Mitch on 04/02/2020
// ///
#include "heartbeat.h" #include "heartbeat.h"
#include <stdio.h> #include <stdio.h>

View File

@ -1,6 +1,6 @@
// ///
// Created by jojos38 on 28.01.2020. /// Created by jojos38 on 28/01/2020
// ///
#include <fstream> #include <fstream>
@ -32,7 +32,7 @@ void setLoggerLevel(char level_string[]) {
stringstream getDate() { stringstream getDate() {
// current date/time based on current system // current date/time based on current system
time_t now = time(0); time_t now = time(nullptr);
tm* ltm = localtime(&now); tm* ltm = localtime(&now);
int month = 1 + ltm->tm_mon; int month = 1 + ltm->tm_mon;

View File

@ -1,6 +1,6 @@
// ///
// Created by Anonymous275 on 28.01.2020. /// Created by Anonymous275 on 28/01/2020
// ///
#include <iostream> #include <iostream>
#include <string> #include <string>
@ -21,6 +21,7 @@ int Port = 30814;
int MaxPlayers = 10; int MaxPlayers = 10;
string MapName = "levels/gridmap/level.json"; string MapName = "levels/gridmap/level.json";
string ServerName = "BeamNG-MP FTW"; string ServerName = "BeamNG-MP FTW";
string Resource = "/Resources";
//Entry //Entry
int main() { int main() {
@ -41,14 +42,16 @@ void DebugData(){
cout << "MaxPlayers : " << MaxPlayers << "\n"; cout << "MaxPlayers : " << MaxPlayers << "\n";
cout << "MapName : " << MapName << "\n"; cout << "MapName : " << MapName << "\n";
cout << "ServerName : " << ServerName << "\n"; cout << "ServerName : " << ServerName << "\n";
cout << "File : " << Resource << "\n";
} }
void SetMainValues(bool D, int P,int MP,string Name,string serverName){ void SetMainValues(bool D, int P,int MP,string Name,string serverName,string filename){
Debug = D; Debug = D;
Port = P; Port = P;
MapName = Name; MapName = Name;
ServerName = serverName; ServerName = serverName;
MaxPlayers = MP; MaxPlayers = MP;
Resource = filename;
} }
void LogInit(){ void LogInit(){