Rigs of Rods 2026.01
Soft-body Physics Simulation
Loading...
Searching...
No Matches
ServerScriptEngine.h
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#pragma once
28
29#ifdef USE_ANGELSCRIPT
30
32#include "ServerScriptLogger.h"
34#include "ServerScript.h"
35#include <map>
36#include <mutex>
37#include <thread>
38#include <vector>
39#include "angelscript.h"
40#include "RoRnet.h"
41
42namespace RoR { // RIGSOFRODS
43
48 AngelScript::asIScriptObject *obj{nullptr};
49 AngelScript::asIScriptFunction *func{nullptr};
50};
51typedef std::vector<rorserver_callback_t> callbackList;
52
53
55public:
56 enum class ThreadState
57 {
59 RUNNING,
61 };
62
64
66
67 int loadScript(std::string scriptName);
68
69 void unloadScript(); // RIGSOFRODS: Unload the script (only one can run at a time).
70
73
74 void playerDeleted(int uid, int crash = 0);
75
76 void playerAdded(RoRnet::UserInfo& user);
77
78 int streamAdded(int uid, RoRnet::StreamRegister *reg);
79
80 int playerChat(int uid, std::string msg);
81
82 void gameCmd(int uid, const std::string &cmd);
83
90 void curlStatus(ServerScriptCurlStatusType type, int n1, int n2, std::string displayname, std::string message);
91
92 int frameStep(float dt);
93
95
100 AngelScript::asIScriptEngine *getEngine() { return engine; };
101
106 void setException(const std::string &message);
107
114 void addCallback(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj);
115
122 void addCallbackScript(const std::string &type, const std::string &func, AngelScript::asIScriptObject *obj);
123
130 void deleteCallback(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj);
131
138 void deleteCallbackScript(const std::string &type, const std::string &_func, AngelScript::asIScriptObject *obj);
139
143 void deleteAllCallbacks();
144
152 bool callbackExists(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj);
153
154 // Timer thread control
156 void StopTimerThread();
158
159protected:
161 AngelScript::asIScriptEngine *engine;
162 AngelScript::asIScriptContext *context;
163 std::map<std::string, callbackList> callbacks;
164
165 // Timer thread context
166 std::thread m_timer_thread;
169
173 void init();
174
181 void msgCallback(const AngelScript::asSMessageInfo *msg);
182
189 void ExceptionCallback(AngelScript::asIScriptContext *ctx, void *param);
190
196 void PrintVariables(AngelScript::asIScriptContext *ctx, int stackLevel);
197
201 void LineCallback(AngelScript::asIScriptContext *ctx, void *param);
202
206 void TimerThreadMain();
207};
208
209} // namespace RoR
210
211#endif // USE_ANGELSCRIPT
212
bool callbackExists(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj)
This checks if a script callback exists.
void init()
This function initialzies the engine and registeres all types.
void TimerThreadMain()
A loop that periodically the frameStep() script callback.
void deleteAllCallbacks()
Deletes all script callbacks.
AngelScript::asIScriptEngine * engine
instance of the scripting engine
void addCallback(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj)
Adds a script callback.
void playerDeleted(int uid, int crash=0)
void playerAdded(RoRnet::UserInfo &user)
void deleteCallback(const std::string &type, AngelScript::asIScriptFunction *func, AngelScript::asIScriptObject *obj)
Deletes a script callback.
AngelScript::asIScriptContext * context
context in which all scripting happens
void curlStatus(ServerScriptCurlStatusType type, int n1, int n2, std::string displayname, std::string message)
Params n1, n2 and message depend on status type :
std::map< std::string, callbackList > callbacks
A map containing the script callbacks by type.
void setException(const std::string &message)
Sets an exception that aborts the currently running script and shows the exception in the log file.
void LineCallback(AngelScript::asIScriptContext *ctx, void *param)
unused
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 gameCmd(int uid, const std::string &cmd)
ServerScriptSequencer * seq
int streamAdded(int uid, RoRnet::StreamRegister *reg)
AngelScript::asIScriptEngine * getEngine()
Gets the currently used AngelScript script engine.
int loadScript(std::string scriptName)
void ExceptionCallback(AngelScript::asIScriptContext *ctx, void *param)
This callback gets called when an exception occurs in the script.
void PrintVariables(AngelScript::asIScriptContext *ctx, int stackLevel)
This logs all variables and their values at the specified stack level.
int playerChat(int uid, std::string msg)
void msgCallback(const AngelScript::asSMessageInfo *msg)
This is the callback function that gets called when script error occur.
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.
std::vector< rorserver_callback_t > callbackList
This struct holds the information for a script callback.
AngelScript::asIScriptObject * obj
The object instance that will need to be used with the function.
AngelScript::asIScriptFunction * func
The function or method pointer that will be called.
< Sent from the client to server and vice versa, to broadcast a new stream
Definition RoRnet.h:160