OpenTTD Source: src/script/squirrel_std.cpp Source File

OpenTTD Source 1.11.0-RC1

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 <http://www.gnu.org/licenses/>.
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 "../core/alloc_func.hpp"
16 #include "../core/math_func.hpp"
17 #include "../string_func.h"
18
19 #include "../safeguards.h"
20
21
22 SQInteger SquirrelStd::min(HSQUIRRELVM vm)
23 {
24 SQInteger tmp1, tmp2;
25
26 sq_getinteger(vm, 2, &tmp1);
27 sq_getinteger(vm, 3, &tmp2);
28 sq_pushinteger(vm, std::min(tmp1, tmp2));
29 return 1;
30 }
31
32 SQInteger SquirrelStd::max(HSQUIRRELVM vm)
33 {
34 SQInteger tmp1, tmp2;
35
36 sq_getinteger(vm, 2, &tmp1);
37 sq_getinteger(vm, 3, &tmp2);
38 sq_pushinteger(vm, std::max(tmp1, tmp2));
39 return 1;
40 }
41
42 SQInteger SquirrelStd::require(HSQUIRRELVM vm)
43 {
44 SQInteger top = sq_gettop(vm);
45 const SQChar *filename;
46
47 sq_getstring(vm, 2, &filename);
48
49 /* Get the script-name of the current file, so we can work relative from it */
50 SQStackInfos si;
51 sq_stackinfos(vm, 1, &si);
52 if (si.source == nullptr) {
53 DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
54 return SQ_ERROR;
55 }
56
57 char path[MAX_PATH];
58 strecpy(path, si.source, lastof(path));
59 /* Keep the dir, remove the rest */
60 SQChar *s = strrchr(path, PATHSEPCHAR);
61 if (s != nullptr) {
62 /* Keep the PATHSEPCHAR there, remove the rest */
63 s++;
64 *s = '\0';
65 }
66 strecat(path, filename, lastof(path));
67 #if (PATHSEPCHAR != '/')
68 for (char *n = path; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
69 #endif
70
71 Squirrel *engine = (Squirrel *)sq_getforeignptr(vm);
72 bool ret = engine->LoadScript(vm, path);
73
74 /* Reset the top, so the stack stays correct */
75 sq_settop(vm, top);
76
77 return ret ? 0 : SQ_ERROR;
78 }
79
80 SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
81 {
82 SQBool b;
83
84 if (sq_gettop(vm) >= 1) {
85 if (SQ_SUCCEEDED(sq_getbool(vm, -1, &b))) {
86 sq_notifyallexceptions(vm, b);
87 return 0;
88 }
89 }
90
91 return SQ_ERROR;
92 }
93
94 void squirrel_register_global_std(Squirrel *engine)
95 {
96 /* We don't use squirrel_helper here, as we want to register to the global
97 * scope and not to a class. */
98 engine->AddMethod("require", &SquirrelStd::require, 2, ".s");
99 engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, 2, ".b");
100 }
101
102 void squirrel_register_std(Squirrel *engine)
103 {
104 /* We don't use squirrel_helper here, as we want to register to the global
105 * scope and not to a class. */
106 engine->AddMethod("min", &SquirrelStd::min, 3, ".ii");
107 engine->AddMethod("max", &SquirrelStd::max, 3, ".ii");
108
109 sqstd_register_mathlib(engine->GetVM());
110 }
SquirrelStd::max
static SQInteger max(HSQUIRRELVM vm)
Get the highest of two integers.
Definition: squirrel_std.cpp:32
Squirrel::GetVM
HSQUIRRELVM GetVM()
Get the squirrel VM.
Definition: squirrel.hpp:80
SquirrelStd::min
static SQInteger min(HSQUIRRELVM vm)
Get the lowest of two integers.
Definition: squirrel_std.cpp:22
Squirrel
Definition: squirrel.hpp:23
Squirrel::LoadScript
bool LoadScript(const char *script)
Load a script.
Definition: squirrel.cpp:678
SquirrelStd::notifyallexceptions
static SQInteger notifyallexceptions(HSQUIRRELVM vm)
Enable/disable stack trace showing for handled exceptions.
Definition: squirrel_std.cpp:80
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
squirrel_register_global_std
void squirrel_register_global_std(Squirrel *engine)
Register all standard functions that are available on first startup.
Definition: squirrel_std.cpp:94
squirrel_register_std
void squirrel_register_std(Squirrel *engine)
Register all standard functions we want to give to a script.
Definition: squirrel_std.cpp:102
SquirrelStd::require
static SQInteger require(HSQUIRRELVM vm)
Load another file on runtime.
Definition: squirrel_std.cpp:42
Squirrel::AddMethod
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=nullptr, void *userdata=nullptr, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:225
squirrel_std.hpp
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
strecat
char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
Definition: string.cpp:84
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383