14 #include "../stdafx.h" 17 #include "../fileio_func.h" 18 #include "../string_func.h" 20 #include "../settings_type.h" 22 #include <../squirrel/sqpcheader.h> 23 #include <../squirrel/sqvm.h> 24 #include "../core/alloc_func.hpp" 26 #include "../safeguards.h" 40 #ifdef SCRIPT_DEBUG_ALLOCATIONS 41 std::map<void *, size_t> allocations;
44 void CheckLimit()
const 46 if (this->allocated_size > this->allocation_limit)
throw Script_FatalError(
"Maximum memory allocation exceeded");
49 void *Malloc(SQUnsignedInteger size)
51 void *p = MallocT<char>(size);
52 this->allocated_size += size;
54 #ifdef SCRIPT_DEBUG_ALLOCATIONS 56 assert(this->allocations.find(p) == this->allocations.end());
57 this->allocations[p] = size;
63 void *Realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
66 return this->Malloc(size);
69 this->Free(p, oldsize);
73 #ifdef SCRIPT_DEBUG_ALLOCATIONS 74 assert(this->allocations[p] == oldsize);
75 this->allocations.erase(p);
78 void *new_p = ReallocT<char>(
static_cast<char *
>(p), size);
80 this->allocated_size -= oldsize;
81 this->allocated_size += size;
83 #ifdef SCRIPT_DEBUG_ALLOCATIONS 84 assert(new_p !=
nullptr);
85 assert(this->allocations.find(p) == this->allocations.end());
86 this->allocations[new_p] = size;
92 void Free(
void *p, SQUnsignedInteger size)
94 if (p ==
nullptr)
return;
96 this->allocated_size -= size;
98 #ifdef SCRIPT_DEBUG_ALLOCATIONS 99 assert(this->allocations.at(p) == size);
100 this->allocations.erase(p);
106 this->allocated_size = 0;
108 if (this->allocation_limit == 0) this->allocation_limit =
SAFE_LIMIT;
113 #ifdef SCRIPT_DEBUG_ALLOCATIONS 114 assert(this->allocations.size() == 0);
122 #ifndef SQUIRREL_DEFAULT_ALLOCATOR 123 void *sq_vm_malloc(SQUnsignedInteger size) {
return _squirrel_allocator->Malloc(size); }
124 void *sq_vm_realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size) {
return _squirrel_allocator->Realloc(p, oldsize, size); }
125 void sq_vm_free(
void *p, SQUnsignedInteger size) { _squirrel_allocator->Free(p, size); }
130 assert(this->allocator !=
nullptr);
131 return this->allocator->allocated_size;
139 seprintf(buf,
lastof(buf),
"Error %s:" OTTD_PRINTF64
"/" OTTD_PRINTF64
": %s", source, line, column, desc);
145 if (func ==
nullptr) {
146 DEBUG(misc, 0,
"[Squirrel] Compile error: %s", buf);
157 va_start(arglist, s);
162 SQPrintFunc *func = ((
Squirrel *)sq_getforeignptr(vm))->print_func;
163 if (func ==
nullptr) {
164 fprintf(stderr,
"%s", buf);
173 SQPRINTFUNCTION pf = sq_getprintfunc(vm);
178 seprintf(buf,
lastof(buf),
"Your script made an error: %s\n", error);
181 if (func ==
nullptr) {
182 fprintf(stderr,
"%s", buf);
188 sqstd_printcallstack(vm);
190 sq_setprintfunc(vm, pf);
195 const SQChar *sErr = 0;
197 if (sq_gettop(vm) >= 1) {
198 if (SQ_SUCCEEDED(sq_getstring(vm, -1, &sErr))) {
213 va_start(arglist, s);
219 SQPrintFunc *func = ((
Squirrel *)sq_getforeignptr(vm))->print_func;
220 if (func ==
nullptr) {
227 void Squirrel::AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam,
const char *params,
void *userdata,
int size)
231 sq_pushstring(this->vm, method_name, -1);
234 void *ptr = sq_newuserdata(vm, size);
235 memcpy(ptr, userdata, size);
238 sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);
239 if (nparam != 0) sq_setparamscheck(this->vm, nparam, params);
240 sq_setnativeclosurename(this->vm, -1, method_name);
241 sq_newslot(this->vm, -3, SQFalse);
248 sq_pushstring(this->vm, var_name, -1);
249 sq_pushinteger(this->vm, value);
250 sq_newslot(this->vm, -3, SQTrue);
257 sq_pushstring(this->vm, var_name, -1);
258 sq_pushbool(this->vm, value);
259 sq_newslot(this->vm, -3, SQTrue);
266 sq_pushroottable(this->vm);
267 sq_pushstring(this->vm, class_name, -1);
268 sq_newclass(this->vm, SQFalse);
275 sq_pushroottable(this->vm);
276 sq_pushstring(this->vm, class_name, -1);
277 sq_pushstring(this->vm, parent_class, -1);
278 if (SQ_FAILED(sq_get(this->vm, -3))) {
279 DEBUG(misc, 0,
"[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
280 DEBUG(misc, 0,
"[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
283 sq_newclass(this->vm, SQTrue);
290 sq_newslot(vm, -3, SQFalse);
296 assert(!this->crashed);
299 int top = sq_gettop(this->vm);
301 sq_pushobject(this->vm, instance);
303 sq_pushstring(this->vm, method_name, -1);
304 if (SQ_FAILED(sq_get(this->vm, -2))) {
305 sq_settop(this->vm, top);
308 sq_settop(this->vm, top);
314 assert(!this->crashed);
319 if (this->overdrawn_ops > 0 && suspend > 0) {
320 this->overdrawn_ops -= suspend;
322 if (this->overdrawn_ops >= 0)
return true;
325 suspend = -this->overdrawn_ops;
328 this->crashed = !sq_resumecatch(this->vm, suspend);
329 this->overdrawn_ops = -this->vm->_ops_till_suspend;
330 this->allocator->CheckLimit();
331 return this->vm->_suspended != 0;
336 assert(!this->crashed);
338 sq_resumeerror(this->vm);
344 sq_collectgarbage(this->vm);
349 assert(!this->crashed);
351 this->allocator->CheckLimit();
356 SQInteger last_target = this->vm->_suspended_target;
358 int top = sq_gettop(this->vm);
360 sq_pushobject(this->vm, instance);
362 sq_pushstring(this->vm, method_name, -1);
363 if (SQ_FAILED(sq_get(this->vm, -2))) {
364 DEBUG(misc, 0,
"[squirrel] Could not find '%s' in the class", method_name);
365 sq_settop(this->vm, top);
369 sq_pushobject(this->vm, instance);
370 if (SQ_FAILED(sq_call(this->vm, 1, ret ==
nullptr ? SQFalse : SQTrue, SQTrue, suspend)))
return false;
371 if (ret !=
nullptr) sq_getstackobj(vm, -1, ret);
374 if (suspend == -1 || !this->IsSuspended()) sq_settop(this->vm, top);
376 this->vm->_suspended_target = last_target;
381 bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance,
const char *method_name,
const char **res,
int suspend)
384 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
385 if (ret._type != OT_STRING)
return false;
386 *res =
stredup(ObjectToString(&ret));
391 bool Squirrel::CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend)
394 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
395 if (ret._type != OT_INTEGER)
return false;
396 *res = ObjectToInteger(&ret);
400 bool Squirrel::CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend)
403 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
404 if (ret._type != OT_BOOL)
return false;
405 *res = ObjectToBool(&ret);
409 bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm,
const char *class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name)
413 int oldtop = sq_gettop(vm);
416 sq_pushroottable(vm);
418 if (prepend_API_name) {
419 size_t len = strlen(class_name) + strlen(engine->
GetAPIName()) + 1;
420 char *class_name2 = (
char *)alloca(len);
421 seprintf(class_name2, class_name2 + len - 1,
"%s%s", engine->
GetAPIName(), class_name);
423 sq_pushstring(vm, class_name2, -1);
425 sq_pushstring(vm, class_name, -1);
428 if (SQ_FAILED(sq_get(vm, -2))) {
429 DEBUG(misc, 0,
"[squirrel] Failed to find class by the name '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
430 sq_settop(vm, oldtop);
435 if (SQ_FAILED(sq_createinstance(vm, -1))) {
436 DEBUG(misc, 0,
"[squirrel] Failed to create instance for class '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
437 sq_settop(vm, oldtop);
441 if (instance !=
nullptr) {
443 sq_getstackobj(vm, -1, instance);
445 sq_addref(vm, instance);
451 sq_setinstanceup(vm, -1, real_instance);
452 if (release_hook !=
nullptr) sq_setreleasehook(vm, -1, release_hook);
454 if (instance !=
nullptr) sq_settop(vm, oldtop);
465 Squirrel::Squirrel(
const char *APIName) :
475 this->global_pointer =
nullptr;
476 this->print_func =
nullptr;
477 this->crashed =
false;
478 this->overdrawn_ops = 0;
479 this->vm = sq_open(1024);
483 sq_notifyallexceptions(this->vm, SQTrue);
488 sq_seterrorhandler(this->vm);
491 sq_setforeignptr(this->vm,
this);
493 sq_pushroottable(this->vm);
504 SQFile(FILE *file,
size_t size) : file(file), size(size), pos(0) {}
506 size_t Read(
void *buf,
size_t elemsize,
size_t count)
508 assert(elemsize != 0);
509 if (this->pos + (elemsize * count) > this->size) {
510 count = (this->size - this->pos) / elemsize;
512 if (count == 0)
return 0;
513 size_t ret = fread(buf, elemsize, count, this->file);
514 this->pos += ret * elemsize;
519 static WChar _io_file_lexfeed_ASCII(SQUserPointer file)
522 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return c;
526 static WChar _io_file_lexfeed_UTF8(SQUserPointer file)
531 if (((
SQFile *)file)->Read(buffer,
sizeof(buffer[0]), 1) != 1)
return 0;
533 if (len == 0)
return -1;
536 if (len > 1 && ((
SQFile *)file)->Read(buffer + 1,
sizeof(buffer[0]), len - 1) != len - 1)
return 0;
545 static WChar _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
548 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return (
WChar)c;
552 static WChar _io_file_lexfeed_UCS2_swap(SQUserPointer file)
555 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0) {
556 c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
562 static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
564 SQInteger ret = ((
SQFile *)file)->Read(buf, 1, size);
565 if (ret == 0)
return -1;
575 if (strncmp(this->GetAPIName(),
"AI", 2) == 0) {
578 }
else if (strncmp(this->GetAPIName(),
"GS", 2) == 0) {
585 if (file ==
nullptr) {
586 return sq_throwerror(vm,
"cannot open the file");
588 unsigned short bom = 0;
590 size_t sr = fread(&bom, 1,
sizeof(bom), file);
596 case SQ_BYTECODE_STREAM_TAG: {
597 if (fseek(file, -2, SEEK_CUR) < 0) {
599 return sq_throwerror(vm,
"cannot seek the file");
603 if (SQ_SUCCEEDED(sq_readclosure(vm, _io_file_read, &f))) {
608 return sq_throwerror(vm,
"Couldn't read bytecode");
614 func = _io_file_lexfeed_UCS2_swap;
618 func = _io_file_lexfeed_UCS2_no_swap;
626 return sq_throwerror(vm,
"I/O error");
629 if (fread(&uc, 1,
sizeof(uc), file) !=
sizeof(uc) || uc != 0xBF) {
631 return sq_throwerror(vm,
"Unrecognized encoding");
633 func = _io_file_lexfeed_UTF8;
638 func = _io_file_lexfeed_ASCII;
640 if (size >= 2 && fseek(file, -2, SEEK_CUR) < 0) {
642 return sq_throwerror(vm,
"cannot seek the file");
648 if (SQ_SUCCEEDED(sq_compile(vm, func, &f, filename, printerror))) {
661 if (in_root) sq_pushroottable(vm);
663 SQInteger ops_left = vm->_ops_till_suspend;
665 if (SQ_SUCCEEDED(LoadFile(vm, script, SQTrue))) {
667 if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue, 100000))) {
670 vm->_ops_till_suspend = ops_left;
675 vm->_ops_till_suspend = ops_left;
676 DEBUG(misc, 0,
"[squirrel] Failed to compile '%s'", script);
682 return LoadScript(this->vm, script);
685 Squirrel::~Squirrel()
687 this->Uninitialize();
701 this->Uninitialize();
705 void Squirrel::InsertResult(
bool result)
709 sq_pushbool(this->vm, result);
710 if (this->IsSuspended()) {
711 vm->GetAt(vm->_stackbase + vm->_suspended_target) = vm->GetUp(-1);
716 void Squirrel::InsertResult(
int result)
720 sq_pushinteger(this->vm, result);
721 if (this->IsSuspended()) {
722 vm->GetAt(vm->_stackbase + vm->_suspended_target) = vm->GetUp(-1);
729 vm->DecreaseOps(ops);
734 return this->vm->_suspended != 0;
739 return this->crashed;
744 this->crashed =
true;
750 return sq_can_suspend(this->vm);
755 return this->vm->_ops_till_suspend;
GameSettings _settings_game
Game settings of a running game or the scenario editor.
static void PrintFunc(HSQUIRRELVM vm, const SQChar *s,...)
If a user runs 'print' inside a script, this function gets the params.
bool HasScriptCrashed()
Find out if the squirrel script made an error before.
static char * strecat(char *dst, const char *src, const char *last)
Appends characters from one string to another.
void FioFCloseFile(FILE *f)
Close a file in a safe way.
bool CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret, int suspend)
Call a method of an instance, in various flavors.
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
static void CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
The CompileError handler.
void CollectGarbage()
Tell the VM to do a garbage collection run.
SQInteger GetOpsTillSuspend()
How many operations can we execute till suspension?
size_t GetAllocatedMemory() const noexcept
Get number of bytes allocated by this VM.
The definition of Script_FatalError.
defines the Squirrel Standard Function class
static SQInteger _RunError(HSQUIRRELVM vm)
The internal RunError handler.
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
A throw-class that is given when the script made a fatal error.
void CrashOccurred()
Set the script status to crashed.
Subdirectory for all game scripts.
size_t Utf8Decode(WChar *c, const char *s)
Decode and consume the next UTF-8 encoded character.
static void DecreaseOps(HSQUIRRELVM vm, int amount)
Tell the VM to remove amount ops from the number of ops till suspend.
#define lastof(x)
Get the last element of an fixed size array.
bool CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance)
Exactly the same as CreateClassInstanceVM, only callable without instance of Squirrel.
uint32 script_max_memory_megabytes
limit on memory a single script instance may have allocated
bool crashed
True if the squirrel script made an error.
const char * GetAPIName()
Get the API name.
static int8 Utf8EncodedCharLen(char c)
Return the length of an UTF-8 encoded value based on a single char.
void ResumeError()
Resume the VM with an error so it prints a stack trace.
FILE * FioFOpenFile(const char *filename, const char *mode, Subdirectory subdir, size_t *filesize)
Opens a OpenTTD file somewhere in a personal or global directory.
size_t allocated_size
Sum of allocated data size.
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
static bool CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook, bool prepend_API_name=false)
Creates a class instance.
Subdirectory for all GS libraries.
bool IsSuspended()
Did the squirrel code suspend or return normally.
static const size_t SAFE_LIMIT
128 MiB, a safe choice for almost any situation
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.
ScriptSettings script
settings for scripts
#define DEBUG(name, level,...)
Output a line of debugging information.
Subdirectory for all AI libraries.
void Reset()
Completely reset the engine; start from scratch.
void squirrel_register_global_std(Squirrel *engine)
Register all standard functions that are available on first startup.
bool CanSuspend()
Are we allowed to suspend the squirrel script at this moment?
void AddConst(const char *var_name, int value)
Adds a const to the stack.
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
SQRESULT LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror)
Load a file to a given VM.
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Subdirectory for all AI files.
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
size_t allocation_limit
Maximum this allocator may use before allocations fail.
static void RunError(HSQUIRRELVM vm, const SQChar *error)
The RunError handler.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
bool LoadScript(const char *script)
Load a script.
void ValidateString(const char *str)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
void Uninitialize()
Perform all the cleanups for the engine.
void AddClassEnd()
Finishes adding a class to the global scope.
SQPrintFunc * print_func
Points to either nullptr, or a custom print handler.
uint32 WChar
Type for wide characters, i.e.
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s,...)
If an error has to be print, this function is called.
void Initialize()
Perform all initialization steps to create the engine.
bool Resume(int suspend=-1)
Resume a VM when it was suspended via a throw.