Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
ServerScript.cpp
Go to the documentation of this file.
1
2// =============================================================================
3// This file is adopted from rorserver at commit 4a7109ae2d9a081ccfdad8cc696dc54efe49acb3
4// Changes from the original are marked with "//RIGSOFRODS"
5// =============================================================================
6
7/*
8This file is part of "Rigs of Rods Server" (Relay mode)
9
10Copyright 2007 Pierre-Michel Ricordel
11Copyright 2014+ Rigs of Rods Community
12
13"Rigs of Rods Server" is free software: you can redistribute it
14and/or modify it under the terms of the GNU General Public License
15as published by the Free Software Foundation, either version 3
16of the License, or (at your option) any later version.
17
18"Rigs of Rods Server" is distributed in the hope that it will
19be useful, but WITHOUT ANY WARRANTY; without even the implied
20warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21See the GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with Foobar. If not, see <http://www.gnu.org/licenses/>.
25*/
26
27#include "ServerScript.h"
28
29#include "Console.h" // RIGSOFRODS: For putNetMessage()
30#include "ServerScriptEngine.h"
31#include "ServerScriptLogger.h"
33#include "Utils.h" // RIGSOFRODS: For SanitizeUtf8String
34
35using namespace RoR;
36using namespace AngelScript;
37
38/* class that implements the interface for the scripts */
39ServerScript::ServerScript(ServerScriptEngine *se, ServerScriptSequencer* sequencer) : mse(se), seq(sequencer) {
40}
41
44
45void ServerScript::log(std::string &msg) {
46 Logger::Log(LOG_INFO, "SCRIPT|%s", msg.c_str());
47}
48
49void ServerScript::say(std::string &msg, int uid, int type) {
50 seq->serverSay(msg, uid, type);
51 // RIGSOFRODS: Post message directly to console (thread safe)
53}
54
55void ServerScript::kick(int kuid, std::string &msg) {
56
58 mse->playerDeleted(kuid, 0);
59}
60
61void ServerScript::ban(int buid, std::string &msg) {
62
63 this->kick(buid, msg);
64
65}
66
67bool ServerScript::unban(int buid) {
68 return true; //RIGSOFRODS STUB // return seq->UnBan(buid);
69}
70
71std::string ServerScript::getUserName(int uid) {
72
74 if (!c) return "";
75
77}
78
79void ServerScript::setUserName(int uid, const std::string &username) {
80
82 if (!c) return;
83 std::string username_sane = SanitizeUtf8String(username);
84 strncpy(c->user.username, username_sane.c_str(), RORNET_MAX_USERNAME_LEN);
85}
86
87std::string ServerScript::getUserAuth(int uid) {
88
90 if (!c) return "none";
91 if (c->user.authstatus & RoRnet::AUTH_ADMIN) return "admin";
92 else if (c->user.authstatus & RoRnet::AUTH_MOD) return "moderator";
93 else if (c->user.authstatus & RoRnet::AUTH_RANKED) return "ranked";
94 else if (c->user.authstatus & RoRnet::AUTH_BOT) return "bot";
95
96 return "none";
97}
98
100
102 if (!c) return RoRnet::AUTH_NONE;
103 return c->user.authstatus;
104
105 return 0;
106}
107
108void ServerScript::setUserAuthRaw(int uid, int authmode) {
109
111 if (!c) return;
113
114}
115
117
119 if (!c) return 0;
120 return c->user.colournum;
121
122 return 0;
123}
124
125void ServerScript::setUserColourNum(int uid, int num) {
126
128 if (!c) return;
129 c->user.colournum = num;
130
131}
132
133std::string ServerScript::getUserToken(int uid) {
134
136 if (!c) return "";
137 return std::string(c->user.usertoken, 40);
138}
139
140std::string ServerScript::getUserVersion(int uid) {
141
143 if (!c) return "";
144 return std::string(c->user.clientversion, 25);
145
146 return "";
147}
148
149std::string ServerScript::getUserIPAddress(int uid) {
150 /* RIGSOFRODS: STUB
151 ServerScriptClient *client = seq->getClient(uid);
152 if (client != nullptr) {
153 return client->GetIpAddress();
154 }
155 */
156 return "";
157}
158
160 return ""; // RIGSOFRODS: STUB // return Config::getTerrainName();
161}
162
163int ServerScript::sendGameCommand(int uid, std::string cmd) {
164 seq->sendGameCommand(uid, cmd);
165 return 0;
166}
167
168void ServerScript::curlRequestAsync(std::string url, std::string displayname) {
169#if USE_CURL
170 CurlTaskContext context;
171 context.ctc_url = url;
172 context.ctc_displayname = displayname;
173 context.ctc_script_engine = this->mse;
174
175 std::packaged_task<void(CurlTaskContext)> pktask(ServerScriptCurlRequestThreadFunc);
176 std::thread(std::move(pktask), context).detach();
177#endif
178}
179
183
185 return seq->getStartTime();
186}
187
189 return (int) time(NULL);
190}
191
192void ServerScript::deleteCallback(const std::string &type, const std::string &func, void *obj, int refTypeId) {
193 if (refTypeId & asTYPEID_SCRIPTOBJECT && (refTypeId & asTYPEID_OBJHANDLE)) {
194 mse->deleteCallbackScript(type, func, *(asIScriptObject **) obj);
195 } else if (refTypeId == asTYPEID_VOID) {
196 mse->deleteCallbackScript(type, func, NULL);
197 } else if (refTypeId & asTYPEID_SCRIPTOBJECT) {
198 // We received an object instead of a handle of the object.
199 // We cannot allow this because this will crash if the deleteCallback is called from inside a constructor of a global variable.
201 "server.deleteCallback should be called with a handle of the object! (that is: put an @ sign in front of the object)");
202
203 // uncomment to enable anyway:
204 //mse->deleteCallbackScript(type, func, (asIScriptObject*)obj);
205 } else {
206 mse->setException("The object for the callback has to be a script-class or null!");
207 }
208}
209
210void ServerScript::setCallback(const std::string &type, const std::string &func, void *obj, int refTypeId) {
211 if (refTypeId & asTYPEID_SCRIPTOBJECT && (refTypeId & asTYPEID_OBJHANDLE)) {
212 mse->addCallbackScript(type, func, *(asIScriptObject **) obj);
213 } else if (refTypeId == asTYPEID_VOID) {
214 mse->addCallbackScript(type, func, NULL);
215 } else if (refTypeId & asTYPEID_SCRIPTOBJECT) {
216 // We received an object instead of a handle of the object.
217 // We cannot allow this because this will crash if the setCallback is called from inside a constructor of a global variable.
219 "server.setCallback should be called with a handle of the object! (that is: put an @ sign in front of the object)");
220
221 // uncomment to enable anyway:
222 //mse->addCallbackScript(type, func, (asIScriptObject*)obj);
223 } else {
224 mse->setException("The object for the callback has to be a script-class or null!");
225 }
226}
227
228void ServerScript::throwException(const std::string &message) {
229 mse->setException(message);
230}
231
233 return ""; // RIGSOFRODS: STUB // std::string(VERSION);
234}
235
237 return std::string(ANGELSCRIPT_VERSION_STRING);
238}
239
241 return std::string(RORNET_VERSION);
242}
243
244unsigned int ServerScript::get_maxClients() { return 0; } // RIGSOFRODS: STUB // return Config::getMaxClients(); }
245
246std::string ServerScript::get_serverName() { return ""; } // RIGSOFRODS: STUB // Config::getServerName(); }
247
248std::string ServerScript::get_IPAddr() { return ""; } // RIGSOFRODS: STUB // Config::getIPAddr(); }
249
250unsigned int ServerScript::get_listenPort() { return 0u; } // RIGSOFRODS: STUB // return Config::getListenPort(); }
251
252int ServerScript::get_serverMode() { return 0; } // RIGSOFRODS: STUB // (int)Config::getServerMode(); }
253
254std::string ServerScript::get_owner() { return ""; } // RIGSOFRODS: STUB // Config::getOwner(); }
255
256std::string ServerScript::get_website() { return ""; } // RIGSOFRODS: STUB // Config::getWebsite(); }
257
258std::string ServerScript::get_ircServ() { return ""; } // RIGSOFRODS: STUB // Config::getIRC(); }
259
260std::string ServerScript::get_voipServ() { return ""; } // RIGSOFRODS: STUB // Config::getVoIP(); }
261
262int ServerScript::rangeRandomInt(int from, int to) {
263 return (int) (from + (to - from) * ((float) rand() / (float) RAND_MAX));
264}
265
267 // RIGSOFRODS: STUB // seq->broadcastUserInfo(uid);
268}
269
@ LOG_INFO
void putNetMessage(int user_id, MessageType type, const char *text)
Definition Console.cpp:108
@ CONSOLE_SYSTEM_NETCHAT
Definition Console.h:55
void playerDeleted(int uid, int crash=0)
void setException(const std::string &message)
Sets an exception that aborts the currently running script and shows the exception in the log file.
void addCallbackScript(const std::string &type, const std::string &func, AngelScript::asIScriptObject *obj)
This method checks and converts the parameters and then adds a script callback.
void deleteCallbackScript(const std::string &type, const std::string &_func, AngelScript::asIScriptObject *obj)
This method checks and converts the parameters and then deletes a script callback.
int sendGameCommand(int uid, std::string cmd)
ServerScriptSequencer * seq
std::string getServerTerrain()
unsigned int get_listenPort()
void curlRequestAsync(std::string url, std::string displayname)
Launches a background task, use curlStatus callback to monitor progress and receive result.
std::string getUserName(int uid)
std::string get_owner()
std::string get_protocolVersion()
std::string getUserIPAddress(int uid)
void ban(int kuid, std::string &msg)
std::string get_serverName()
void log(std::string &msg)
void deleteCallback(const std::string &type, const std::string &func, void *obj, int refTypeId)
std::string getUserToken(int uid)
int rangeRandomInt(int from, int to)
void say(std::string &msg, int uid=-1, int type=0)
ServerScriptEngine * mse
local script engine pointer, used as proxy mostly
void setUserAuthRaw(int uid, int authmode)
std::string get_IPAddr()
void setUserName(int uid, const std::string &username)
std::string getUserVersion(int uid)
std::string get_voipServ()
void setUserColourNum(int uid, int num)
bool unban(int kuid)
std::string get_asVersion()
unsigned int get_maxClients()
int getUserAuthRaw(int uid)
int getUserColourNum(int uid)
std::string get_ircServ()
void throwException(const std::string &message)
std::string getUserAuth(int uid)
std::string get_version()
void broadcastUserInfo(int uid)
void kick(int kuid, std::string &msg)
void setCallback(const std::string &type, const std::string &func, void *obj, int refTypeId)
std::string get_website()
ServerScript(ServerScriptEngine *se, ServerScriptSequencer *sequencer)
void sendGameCommand(int uid, std::string cmd)
ServerScriptClient * getClient(int uid)
void serverSay(std::string msg, int uid=-1, int type=0)
void QueueClientForDisconnect(int client_id)
void Log(ServerLogLevel level, const char *format,...)
Console * GetConsole()
std::string SanitizeUtf8String(std::string const &str_in)
Definition Utils.cpp:120
void ServerScriptCurlRequestThreadFunc(CurlTaskContext context)
@ AUTH_MOD
moderator status
Definition RoRnet.h:80
@ AUTH_NONE
no authentication
Definition RoRnet.h:77
@ AUTH_BANNED
banned
Definition RoRnet.h:82
@ AUTH_BOT
bot status
Definition RoRnet.h:81
@ AUTH_RANKED
ranked status
Definition RoRnet.h:79
@ AUTH_ADMIN
admin on the server
Definition RoRnet.h:78
#define RORNET_VERSION
Definition RoRnet.h:35
#define RORNET_MAX_USERNAME_LEN
bytes.
Definition RoRnet.h:33
std::string ctc_displayname
Definition CurlHelpers.h:36
std::string ctc_url
Definition CurlHelpers.h:37
ServerScriptEngine * ctc_script_engine
Definition CurlHelpers.h:44
RoRnet::UserInfo user
user information
int32_t authstatus
auth status set by server: AUTH_*
Definition RoRnet.h:192
char username[RORNET_MAX_USERNAME_LEN]
the nickname of the user (UTF-8)
Definition RoRnet.h:196
int32_t colournum
colour set by server
Definition RoRnet.h:194
char usertoken[40]
user token
Definition RoRnet.h:197
char clientversion[25]
a version number of the client. For example 1 for RoR 0.35
Definition RoRnet.h:201