OpenTTD Source 20251215-master-ge1333abc3e
squirrel_std.cpp
Go to the documentation of this file.
1/*
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "../stdafx.h"
11#include <squirrel.h>
12#include <sqstdmath.h>
13#include "../debug.h"
14#include "squirrel_std.hpp"
15#include "../string_func.h"
16
17#include "../safeguards.h"
18
19
20SQInteger SquirrelStd::min(HSQUIRRELVM vm)
21{
22 SQInteger tmp1, tmp2;
23
24 sq_getinteger(vm, 2, &tmp1);
25 sq_getinteger(vm, 3, &tmp2);
26 sq_pushinteger(vm, std::min(tmp1, tmp2));
27 return 1;
28}
29
30SQInteger SquirrelStd::max(HSQUIRRELVM vm)
31{
32 SQInteger tmp1, tmp2;
33
34 sq_getinteger(vm, 2, &tmp1);
35 sq_getinteger(vm, 3, &tmp2);
36 sq_pushinteger(vm, std::max(tmp1, tmp2));
37 return 1;
38}
39
40SQInteger SquirrelStd::require(HSQUIRRELVM vm)
41{
42 SQInteger top = sq_gettop(vm);
43 std::string_view filename;
44
45 sq_getstring(vm, 2, filename);
46
47 /* Get the script-name of the current file, so we can work relative from it */
48 SQStackInfos si;
49 sq_stackinfos(vm, 1, &si);
50
51 /* Keep the dir, remove the rest */
52 std::string path{si.source};
53 auto p = path.find_last_of(PATHSEPCHAR);
54 /* Keep the PATHSEPCHAR there, remove the rest */
55 if (p != std::string::npos) path.erase(p + 1);
56 path += filename;
57#if (PATHSEPCHAR != '/')
58 std::transform(path.begin(), path.end(), path.begin(), [](char &c) { return c == '/' ? PATHSEPCHAR : c; });
59#endif
60
61 Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
62 bool ret = engine->LoadScript(vm, path);
63
64 /* Reset the top, so the stack stays correct */
65 sq_settop(vm, top);
66
67 return ret ? 0 : SQ_ERROR;
68}
69
70SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
71{
72 SQBool b;
73
74 if (sq_gettop(vm) >= 1) {
75 if (SQ_SUCCEEDED(sq_getbool(vm, -1, &b))) {
76 sq_notifyallexceptions(vm, b);
77 return 0;
78 }
79 }
80
81 return SQ_ERROR;
82}
83
85{
86 /* We don't use squirrel_helper here, as we want to register to the global
87 * scope and not to a class. */
88 engine.AddMethod("require", &SquirrelStd::require, ".s");
89 engine.AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, ".b");
90}
91
93{
94 /* We don't use squirrel_helper here, as we want to register to the global
95 * scope and not to a class. */
96 engine.AddMethod("min", &SquirrelStd::min, ".ii");
97 engine.AddMethod("max", &SquirrelStd::max, ".ii");
98
99 sqstd_register_mathlib(engine.GetVM());
100}
static SQInteger max(HSQUIRRELVM vm)
Get the highest of two integers.
static SQInteger require(HSQUIRRELVM vm)
Load another file on runtime.
static SQInteger notifyallexceptions(HSQUIRRELVM vm)
Enable/disable stack trace showing for handled exceptions.
static SQInteger min(HSQUIRRELVM vm)
Get the lowest of two integers.
void AddMethod(std::string_view method_name, SQFUNCTION proc, std::string_view params={}, void *userdata=nullptr, int size=0, bool suspendable=false)
Adds a function to the stack.
Definition squirrel.cpp:256
bool LoadScript(const std::string &script)
Load a script.
Definition squirrel.cpp:731
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition squirrel.hpp:82
void squirrel_register_global_std(Squirrel &engine)
Register all standard functions that are available on first startup.
void squirrel_register_std(Squirrel &engine)
Register all standard functions we want to give to a script.
defines the Squirrel Standard Function class