12 #include "../stdafx.h" 15 #include "../fileio_func.h" 16 #include "../string_func.h" 18 #include "../settings_type.h" 20 #include <../squirrel/sqpcheader.h> 21 #include <../squirrel/sqvm.h> 22 #include "../core/alloc_func.hpp" 24 #include "../safeguards.h" 38 #ifdef SCRIPT_DEBUG_ALLOCATIONS 39 std::map<void *, size_t> allocations;
42 void CheckLimit()
const 44 if (this->allocated_size > this->allocation_limit)
throw Script_FatalError(
"Maximum memory allocation exceeded");
47 void *Malloc(SQUnsignedInteger size)
49 void *p = MallocT<char>(size);
50 this->allocated_size += size;
52 #ifdef SCRIPT_DEBUG_ALLOCATIONS 54 assert(this->allocations.find(p) == this->allocations.end());
55 this->allocations[p] = size;
61 void *Realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
64 return this->Malloc(size);
67 this->Free(p, oldsize);
71 #ifdef SCRIPT_DEBUG_ALLOCATIONS 72 assert(this->allocations[p] == oldsize);
73 this->allocations.erase(p);
76 void *new_p = ReallocT<char>(
static_cast<char *
>(p), size);
78 this->allocated_size -= oldsize;
79 this->allocated_size += size;
81 #ifdef SCRIPT_DEBUG_ALLOCATIONS 82 assert(new_p !=
nullptr);
83 assert(this->allocations.find(p) == this->allocations.end());
84 this->allocations[new_p] = size;
90 void Free(
void *p, SQUnsignedInteger size)
92 if (p ==
nullptr)
return;
94 this->allocated_size -= size;
96 #ifdef SCRIPT_DEBUG_ALLOCATIONS 97 assert(this->allocations.at(p) == size);
98 this->allocations.erase(p);
104 this->allocated_size = 0;
106 if (this->allocation_limit == 0) this->allocation_limit =
SAFE_LIMIT;
111 #ifdef SCRIPT_DEBUG_ALLOCATIONS 112 assert(this->allocations.size() == 0);
120 #ifndef SQUIRREL_DEFAULT_ALLOCATOR 121 void *sq_vm_malloc(SQUnsignedInteger size) {
return _squirrel_allocator->Malloc(size); }
122 void *sq_vm_realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size) {
return _squirrel_allocator->Realloc(p, oldsize, size); }
123 void sq_vm_free(
void *p, SQUnsignedInteger size) { _squirrel_allocator->Free(p, size); }
128 assert(this->allocator !=
nullptr);
129 return this->allocator->allocated_size;
137 seprintf(buf,
lastof(buf),
"Error %s:" OTTD_PRINTF64
"/" OTTD_PRINTF64
": %s", source, line, column, desc);
143 if (func ==
nullptr) {
144 DEBUG(misc, 0,
"[Squirrel] Compile error: %s", buf);
155 va_start(arglist, s);
160 SQPrintFunc *func = ((
Squirrel *)sq_getforeignptr(vm))->print_func;
161 if (func ==
nullptr) {
162 fprintf(stderr,
"%s", buf);
171 SQPRINTFUNCTION pf = sq_getprintfunc(vm);
176 seprintf(buf,
lastof(buf),
"Your script made an error: %s\n", error);
179 if (func ==
nullptr) {
180 fprintf(stderr,
"%s", buf);
186 sqstd_printcallstack(vm);
188 sq_setprintfunc(vm, pf);
193 const SQChar *sErr = 0;
195 if (sq_gettop(vm) >= 1) {
196 if (SQ_SUCCEEDED(sq_getstring(vm, -1, &sErr))) {
211 va_start(arglist, s);
217 SQPrintFunc *func = ((
Squirrel *)sq_getforeignptr(vm))->print_func;
218 if (func ==
nullptr) {
225 void Squirrel::AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam,
const char *params,
void *userdata,
int size)
229 sq_pushstring(this->vm, method_name, -1);
232 void *ptr = sq_newuserdata(vm, size);
233 memcpy(ptr, userdata, size);
236 sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);
237 if (nparam != 0) sq_setparamscheck(this->vm, nparam, params);
238 sq_setnativeclosurename(this->vm, -1, method_name);
239 sq_newslot(this->vm, -3, SQFalse);
246 sq_pushstring(this->vm, var_name, -1);
247 sq_pushinteger(this->vm, value);
248 sq_newslot(this->vm, -3, SQTrue);
255 sq_pushstring(this->vm, var_name, -1);
256 sq_pushbool(this->vm, value);
257 sq_newslot(this->vm, -3, SQTrue);
264 sq_pushroottable(this->vm);
265 sq_pushstring(this->vm, class_name, -1);
266 sq_newclass(this->vm, SQFalse);
273 sq_pushroottable(this->vm);
274 sq_pushstring(this->vm, class_name, -1);
275 sq_pushstring(this->vm, parent_class, -1);
276 if (SQ_FAILED(sq_get(this->vm, -3))) {
277 DEBUG(misc, 0,
"[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
278 DEBUG(misc, 0,
"[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
281 sq_newclass(this->vm, SQTrue);
288 sq_newslot(vm, -3, SQFalse);
294 assert(!this->crashed);
297 int top = sq_gettop(this->vm);
299 sq_pushobject(this->vm, instance);
301 sq_pushstring(this->vm, method_name, -1);
302 if (SQ_FAILED(sq_get(this->vm, -2))) {
303 sq_settop(this->vm, top);
306 sq_settop(this->vm, top);
312 assert(!this->crashed);
317 if (this->overdrawn_ops > 0 && suspend > 0) {
318 this->overdrawn_ops -= suspend;
320 if (this->overdrawn_ops >= 0)
return true;
323 suspend = -this->overdrawn_ops;
326 this->crashed = !sq_resumecatch(this->vm, suspend);
327 this->overdrawn_ops = -this->vm->_ops_till_suspend;
328 this->allocator->CheckLimit();
329 return this->vm->_suspended != 0;
334 assert(!this->crashed);
336 sq_resumeerror(this->vm);
342 sq_collectgarbage(this->vm);
347 assert(!this->crashed);
349 this->allocator->CheckLimit();
354 SQInteger last_target = this->vm->_suspended_target;
356 int top = sq_gettop(this->vm);
358 sq_pushobject(this->vm, instance);
360 sq_pushstring(this->vm, method_name, -1);
361 if (SQ_FAILED(sq_get(this->vm, -2))) {
362 DEBUG(misc, 0,
"[squirrel] Could not find '%s' in the class", method_name);
363 sq_settop(this->vm, top);
367 sq_pushobject(this->vm, instance);
368 if (SQ_FAILED(sq_call(this->vm, 1, ret ==
nullptr ? SQFalse : SQTrue, SQTrue, suspend)))
return false;
369 if (ret !=
nullptr) sq_getstackobj(vm, -1, ret);
372 if (suspend == -1 || !this->IsSuspended()) sq_settop(this->vm, top);
374 this->vm->_suspended_target = last_target;
379 bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance,
const char *method_name,
const char **res,
int suspend)
382 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
383 if (ret._type != OT_STRING)
return false;
384 *res =
stredup(ObjectToString(&ret));
389 bool Squirrel::CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend)
392 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
393 if (ret._type != OT_INTEGER)
return false;
394 *res = ObjectToInteger(&ret);
398 bool Squirrel::CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend)
401 if (!this->CallMethod(instance, method_name, &ret, suspend))
return false;
402 if (ret._type != OT_BOOL)
return false;
403 *res = ObjectToBool(&ret);
407 bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm,
const char *class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name)
411 int oldtop = sq_gettop(vm);
414 sq_pushroottable(vm);
416 if (prepend_API_name) {
417 size_t len = strlen(class_name) + strlen(engine->
GetAPIName()) + 1;
418 char *class_name2 = (
char *)alloca(len);
419 seprintf(class_name2, class_name2 + len - 1,
"%s%s", engine->
GetAPIName(), class_name);
421 sq_pushstring(vm, class_name2, -1);
423 sq_pushstring(vm, class_name, -1);
426 if (SQ_FAILED(sq_get(vm, -2))) {
427 DEBUG(misc, 0,
"[squirrel] Failed to find class by the name '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
428 sq_settop(vm, oldtop);
433 if (SQ_FAILED(sq_createinstance(vm, -1))) {
434 DEBUG(misc, 0,
"[squirrel] Failed to create instance for class '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
435 sq_settop(vm, oldtop);
439 if (instance !=
nullptr) {
441 sq_getstackobj(vm, -1, instance);
443 sq_addref(vm, instance);
449 sq_setinstanceup(vm, -1, real_instance);
450 if (release_hook !=
nullptr) sq_setreleasehook(vm, -1, release_hook);
452 if (instance !=
nullptr) sq_settop(vm, oldtop);
463 Squirrel::Squirrel(
const char *APIName) :
473 this->global_pointer =
nullptr;
474 this->print_func =
nullptr;
475 this->crashed =
false;
476 this->overdrawn_ops = 0;
477 this->vm = sq_open(1024);
481 sq_notifyallexceptions(this->vm, SQTrue);
486 sq_seterrorhandler(this->vm);
489 sq_setforeignptr(this->vm,
this);
491 sq_pushroottable(this->vm);
502 SQFile(FILE *file,
size_t size) : file(file), size(size), pos(0) {}
504 size_t Read(
void *buf,
size_t elemsize,
size_t count)
506 assert(elemsize != 0);
507 if (this->pos + (elemsize * count) > this->size) {
508 count = (this->size - this->pos) / elemsize;
510 if (count == 0)
return 0;
511 size_t ret = fread(buf, elemsize, count, this->file);
512 this->pos += ret * elemsize;
517 static WChar _io_file_lexfeed_ASCII(SQUserPointer file)
520 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return c;
524 static WChar _io_file_lexfeed_UTF8(SQUserPointer file)
529 if (((
SQFile *)file)->Read(buffer,
sizeof(buffer[0]), 1) != 1)
return 0;
531 if (len == 0)
return -1;
534 if (len > 1 && ((
SQFile *)file)->Read(buffer + 1,
sizeof(buffer[0]), len - 1) != len - 1)
return 0;
543 static WChar _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
546 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return (
WChar)c;
550 static WChar _io_file_lexfeed_UCS2_swap(SQUserPointer file)
553 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0) {
554 c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
560 static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
562 SQInteger ret = ((
SQFile *)file)->Read(buf, 1, size);
563 if (ret == 0)
return -1;
573 if (strncmp(this->GetAPIName(),
"AI", 2) == 0) {
576 }
else if (strncmp(this->GetAPIName(),
"GS", 2) == 0) {
583 if (file ==
nullptr) {
584 return sq_throwerror(vm,
"cannot open the file");
586 unsigned short bom = 0;
588 size_t sr = fread(&bom, 1,
sizeof(bom), file);
594 case SQ_BYTECODE_STREAM_TAG: {
595 if (fseek(file, -2, SEEK_CUR) < 0) {
597 return sq_throwerror(vm,
"cannot seek the file");
601 if (SQ_SUCCEEDED(sq_readclosure(vm, _io_file_read, &f))) {
606 return sq_throwerror(vm,
"Couldn't read bytecode");
612 func = _io_file_lexfeed_UCS2_swap;
616 func = _io_file_lexfeed_UCS2_no_swap;
624 return sq_throwerror(vm,
"I/O error");
627 if (fread(&uc, 1,
sizeof(uc), file) !=
sizeof(uc) || uc != 0xBF) {
629 return sq_throwerror(vm,
"Unrecognized encoding");
631 func = _io_file_lexfeed_UTF8;
636 func = _io_file_lexfeed_ASCII;
638 if (size >= 2 && fseek(file, -2, SEEK_CUR) < 0) {
640 return sq_throwerror(vm,
"cannot seek the file");
646 if (SQ_SUCCEEDED(sq_compile(vm, func, &f, filename, printerror))) {
659 if (in_root) sq_pushroottable(vm);
661 SQInteger ops_left = vm->_ops_till_suspend;
663 if (SQ_SUCCEEDED(LoadFile(vm, script, SQTrue))) {
665 if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue, 100000))) {
668 vm->_ops_till_suspend = ops_left;
673 vm->_ops_till_suspend = ops_left;
674 DEBUG(misc, 0,
"[squirrel] Failed to compile '%s'", script);
680 return LoadScript(this->vm, script);
683 Squirrel::~Squirrel()
685 this->Uninitialize();
699 this->Uninitialize();
703 void Squirrel::InsertResult(
bool result)
707 sq_pushbool(this->vm, result);
708 if (this->IsSuspended()) {
709 vm->GetAt(vm->_stackbase + vm->_suspended_target) = vm->GetUp(-1);
714 void Squirrel::InsertResult(
int result)
718 sq_pushinteger(this->vm, result);
719 if (this->IsSuspended()) {
720 vm->GetAt(vm->_stackbase + vm->_suspended_target) = vm->GetUp(-1);
727 vm->DecreaseOps(ops);
732 return this->vm->_suspended != 0;
737 return this->crashed;
742 this->crashed =
true;
748 return sq_can_suspend(this->vm);
753 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.