2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file squirrel.hpp defines the Squirrel class */
15 /** The type of script we're working with, i.e. for who is it? */
16 enum class ScriptType
{
17 AI
, ///< The script is for AI scripts.
18 GS
, ///< The script is for Game scripts.
21 struct ScriptAllocator
;
24 friend class ScriptAllocatorScope
;
27 typedef void (SQPrintFunc
)(bool error_msg
, const std::string
&message
);
29 HSQUIRRELVM vm
; ///< The VirtualMachine instance for squirrel
30 void *global_pointer
; ///< Can be set by who ever initializes Squirrel
31 SQPrintFunc
*print_func
; ///< Points to either nullptr, or a custom print handler
32 bool crashed
; ///< True if the squirrel script made an error.
33 int overdrawn_ops
; ///< The amount of operations we have overdrawn.
34 const char *APIName
; ///< Name of the API used for this squirrel.
35 std::unique_ptr
<ScriptAllocator
> allocator
; ///< Allocator object used by this script.
38 * The internal RunError handler. It looks up the real error and calls RunError with it.
40 static SQInteger
_RunError(HSQUIRRELVM vm
);
45 const char *GetAPIName() { return this->APIName
; }
47 /** Perform all initialization steps to create the engine. */
49 /** Perform all the cleanups for the engine. */
54 * The CompileError handler.
56 static void CompileError(HSQUIRRELVM vm
, const SQChar
*desc
, const SQChar
*source
, SQInteger line
, SQInteger column
);
59 * The RunError handler.
61 static void RunError(HSQUIRRELVM vm
, const SQChar
*error
);
64 * If a user runs 'print' inside a script, this function gets the params.
66 static void PrintFunc(HSQUIRRELVM vm
, const std::string
&s
);
69 * If an error has to be print, this function is called.
71 static void ErrorPrintFunc(HSQUIRRELVM vm
, const std::string
&s
);
74 Squirrel(const char *APIName
);
78 * Get the squirrel VM. Try to avoid using this.
80 HSQUIRRELVM
GetVM() { return this->vm
; }
84 * @param script The full script-name to load.
85 * @return False if loading failed.
87 bool LoadScript(const std::string
&script
);
88 bool LoadScript(HSQUIRRELVM vm
, const std::string
&script
, bool in_root
= true);
91 * Load a file to a given VM.
93 SQRESULT
LoadFile(HSQUIRRELVM vm
, const std::string
&filename
, SQBool printerror
);
96 * Adds a function to the stack. Depending on the current state this means
97 * either a method or a global function.
99 void AddMethod(const char *method_name
, SQFUNCTION proc
, uint nparam
= 0, const char *params
= nullptr, void *userdata
= nullptr, int size
= 0);
102 * Adds a const to the stack. Depending on the current state this means
103 * either a const to a class or to the global space.
105 void AddConst(const char *var_name
, int value
);
108 * Adds a const to the stack. Depending on the current state this means
109 * either a const to a class or to the global space.
111 void AddConst(const char *var_name
, uint value
) { this->AddConst(var_name
, (int)value
); }
114 * Adds a const to the stack. Depending on the current state this means
115 * either a const to a class or to the global space.
117 void AddConst(const char *var_name
, bool value
);
120 * Adds a class to the global scope. Make sure to call AddClassEnd when you
121 * are done adding methods.
123 void AddClassBegin(const char *class_name
);
126 * Adds a class to the global scope, extending 'parent_class'.
127 * Make sure to call AddClassEnd when you are done adding methods.
129 void AddClassBegin(const char *class_name
, const char *parent_class
);
132 * Finishes adding a class to the global scope. If this isn't called, no
133 * class is really created.
138 * Resume a VM when it was suspended via a throw.
140 bool Resume(int suspend
= -1);
143 * Resume the VM with an error so it prints a stack trace.
148 * Tell the VM to do a garbage collection run.
150 void CollectGarbage();
152 void InsertResult(bool result
);
153 void InsertResult(int result
);
154 void InsertResult(uint result
) { this->InsertResult((int)result
); }
157 * Call a method of an instance, in various flavors.
158 * @return False if the script crashed or returned a wrong type.
160 bool CallMethod(HSQOBJECT instance
, const char *method_name
, HSQOBJECT
*ret
, int suspend
);
161 bool CallMethod(HSQOBJECT instance
, const char *method_name
, int suspend
) { return this->CallMethod(instance
, method_name
, nullptr, suspend
); }
162 bool CallStringMethod(HSQOBJECT instance
, const char *method_name
, std::string
*res
, int suspend
);
163 bool CallIntegerMethod(HSQOBJECT instance
, const char *method_name
, int *res
, int suspend
);
164 bool CallBoolMethod(HSQOBJECT instance
, const char *method_name
, bool *res
, int suspend
);
167 * Check if a method exists in an instance.
169 bool MethodExists(HSQOBJECT instance
, const char *method_name
);
172 * Creates a class instance.
173 * @param vm The VM to create the class instance for
174 * @param class_name The name of the class of which we create an instance.
175 * @param real_instance The instance to the real class, if it represents a real class.
176 * @param instance Returning value with the pointer to the instance.
177 * @param release_hook Optional param to give a release hook.
178 * @param prepend_API_name Optional parameter; if true, the class_name is prefixed with the current API name.
179 * @return False if creating failed.
181 static bool CreateClassInstanceVM(HSQUIRRELVM vm
, const std::string
&class_name
, void *real_instance
, HSQOBJECT
*instance
, SQRELEASEHOOK release_hook
, bool prepend_API_name
= false);
184 * Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
186 bool CreateClassInstance(const std::string
&class_name
, void *real_instance
, HSQOBJECT
*instance
);
189 * Get the real-instance pointer.
190 * @note This will only work just after a function-call from within Squirrel
191 * to your C++ function.
193 static bool GetRealInstance(HSQUIRRELVM vm
, SQUserPointer
*ptr
) { return SQ_SUCCEEDED(sq_getinstanceup(vm
, 1, ptr
, nullptr)); }
196 * Get the Squirrel-instance pointer.
197 * @note This will only work just after a function-call from within Squirrel
198 * to your C++ function.
200 static bool GetInstance(HSQUIRRELVM vm
, HSQOBJECT
*ptr
, int pos
= 1) { sq_getclass(vm
, pos
); sq_getstackobj(vm
, pos
, ptr
); sq_pop(vm
, 1); return true; }
203 * Convert a Squirrel-object to a string.
205 static const char *ObjectToString(HSQOBJECT
*ptr
) { return sq_objtostring(ptr
); }
208 * Convert a Squirrel-object to an integer.
210 static int ObjectToInteger(HSQOBJECT
*ptr
) { return sq_objtointeger(ptr
); }
213 * Convert a Squirrel-object to a bool.
215 static bool ObjectToBool(HSQOBJECT
*ptr
) { return sq_objtobool(ptr
) == 1; }
218 * Sets a pointer in the VM that is reachable from where ever you are in SQ.
219 * Useful to keep track of the main instance.
221 void SetGlobalPointer(void *ptr
) { this->global_pointer
= ptr
; }
224 * Get the pointer as set by SetGlobalPointer.
226 static void *GetGlobalPointer(HSQUIRRELVM vm
) { return ((Squirrel
*)sq_getforeignptr(vm
))->global_pointer
; }
229 * Set a custom print function, so you can handle outputs from SQ yourself.
231 void SetPrintFunction(SQPrintFunc
*func
) { this->print_func
= func
; }
234 * Throw a Squirrel error that will be nicely displayed to the user.
236 void ThrowError(const std::string_view error
) { sq_throwerror(this->vm
, error
); }
239 * Release a SQ object.
241 void ReleaseObject(HSQOBJECT
*ptr
) { sq_release(this->vm
, ptr
); }
244 * Tell the VM to remove \c amount ops from the number of ops till suspend.
246 static void DecreaseOps(HSQUIRRELVM vm
, int amount
);
249 * Did the squirrel code suspend or return normally.
250 * @return True if the function suspended.
255 * Find out if the squirrel script made an error before.
257 bool HasScriptCrashed();
260 * Set the script status to crashed.
262 void CrashOccurred();
265 * Are we allowed to suspend the squirrel script at this moment?
270 * How many operations can we execute till suspension?
272 SQInteger
GetOpsTillSuspend();
275 * Completely reset the engine; start from scratch.
280 * Get number of bytes allocated by this VM.
282 size_t GetAllocatedMemory() const noexcept
;
286 extern ScriptAllocator
*_squirrel_allocator
;
288 class ScriptAllocatorScope
{
289 ScriptAllocator
*old_allocator
;
292 ScriptAllocatorScope(const Squirrel
*engine
)
294 this->old_allocator
= _squirrel_allocator
;
295 /* This may get called with a nullptr engine, in case of a crashed script */
296 _squirrel_allocator
= engine
!= nullptr ? engine
->allocator
.get() : nullptr;
299 ~ScriptAllocatorScope()
301 _squirrel_allocator
= this->old_allocator
;
305 #endif /* SQUIRREL_HPP */