src/squirrel.cpp
author truebrain
Thu, 12 Jun 2008 18:03:50 +0000
branchnoai
changeset 10938 df6235dd2b7a
parent 10887 5c81038449f2
child 11098 37d15a8951b8
permissions -rw-r--r--
(svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     1
/* $Id$ */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     2
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     3
/** @file squirrel.cpp the implementation of the Squirrel class. It handles all Squirrel-stuff and gives a nice API back to work with. */
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     4
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     5
#include <squirrel.h>
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     6
#include <stdarg.h>
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     7
#include "stdafx.h"
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     8
#include "debug.h"
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     9
#include "squirrel.hpp"
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
    10
#include "squirrel_std.hpp"
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
    11
#include "fileio.h"
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    12
#include <sqstdaux.h>
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    13
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    14
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    15
{
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    16
	SQChar buf[1024];
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    17
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    18
#ifdef _SQ64
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    19
	scsnprintf(buf, lengthof(buf), _SC("Error %s:%ld/%ld: %s"), source, line, column, desc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    20
#else
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    21
	scsnprintf(buf, lengthof(buf), _SC("Error %s:%d/%d: %s"), source, line, column, desc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    22
#endif
10650
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    23
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    24
	/* Check if we have a custom print function */
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    25
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    26
	if (func == NULL) {
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    27
		scfprintf(stderr, _SC("%s"), buf);
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    28
	} else {
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    29
		(*func)(true, buf);
30fc5395b1b8 (svn r13194) [NoAI] -Change [API CHANGE]: split 'main.nut' in 'info.nut' and 'main.nut'. The first contains the information about the AI, the second the AI. This avoid several problems we had. It also speeds up OpenTTD start-up.
truebrain
parents: 10649
diff changeset
    30
	}
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    31
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    32
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    33
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    34
{
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    35
	va_list arglist;
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    36
	SQChar buf[1024];
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    37
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    38
	va_start(arglist, s);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    39
	scvsnprintf(buf, lengthof(buf), s, arglist);
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    40
	va_end(arglist);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    41
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    42
	/* Check if we have a custom print function */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    43
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    44
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    45
		scfprintf(stderr, _SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    46
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    47
		(*func)(true, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    48
	}
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    49
}
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    50
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    51
void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    52
{
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    53
	/* Set the print function to something that prints to stderr */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    54
	SQPRINTFUNCTION pf = sq_getprintfunc(vm);
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    55
	sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    56
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    57
	/* Check if we have a custom print function */
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    58
	SQChar buf[1024];
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    59
	scsnprintf(buf, lengthof(buf), _SC("Your script made an error: %s\n"), error);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    60
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    61
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    62
		scfprintf(stderr, _SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    63
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    64
		(*func)(true, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    65
	}
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    66
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    67
	/* Print below the error the stack, so the users knows what is happening */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    68
	sqstd_printcallstack(vm);
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    69
	/* Reset the old print function */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    70
	sq_setprintfunc(vm, pf);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    71
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    72
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    73
SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    74
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    75
	const SQChar *sErr = 0;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    76
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    77
	if (sq_gettop(vm) >= 1) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    78
		if (SQ_SUCCEEDED(sq_getstring(vm, -1, &sErr))) {
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    79
			Squirrel::RunError(vm, sErr);
9535
522eb10df893 (svn r9457) [NoAI] -Fix: the errors reported by SQ were a) not via DEBUG, b) always ending with: "Unknown Error"
truelight
parents: 9525
diff changeset
    80
			return 0;
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    81
		}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    82
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    83
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    84
	Squirrel::RunError(vm, _SC("unknown error"));
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    85
	return 0;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    86
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    87
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    88
void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    89
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    90
	va_list arglist;
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    91
	SQChar buf[1024];
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    92
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    93
	va_start(arglist, s);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    94
	scvsnprintf(buf, lengthof(buf) - 2, s, arglist);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    95
	va_end(arglist);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    96
	scstrcat(buf, _SC("\n"));
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    97
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    98
	/* Check if we have a custom print function */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    99
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   100
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   101
		scprintf(_SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   102
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   103
		(*func)(false, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   104
	}
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   105
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   106
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   107
void Squirrel::AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params, void *userdata, int size)
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   108
{
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   109
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   110
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   111
	if (size != 0) {
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   112
		void *ptr = sq_newuserdata(vm, size);
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   113
		memcpy(ptr, userdata, size);
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   114
	}
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   115
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   116
	sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   117
	if (nparam != 0) sq_setparamscheck(this->vm, nparam, OTTD2FS(params));
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   118
	sq_setnativeclosurename(this->vm, -1, OTTD2FS(method_name));
9525
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   119
	sq_newslot(this->vm, -3, SQFalse);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   120
}
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   121
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   122
void Squirrel::AddConst(const char *var_name, int value)
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   123
{
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   124
	sq_pushstring(this->vm, OTTD2FS(var_name), -1);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   125
	sq_pushinteger(this->vm, value);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   126
	sq_newslot(this->vm, -3, SQTrue);
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   127
}
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   128
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   129
void Squirrel::AddClassBegin(const char *class_name)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   130
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   131
	sq_pushroottable(this->vm);
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   132
	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   133
	sq_newclass(this->vm, SQFalse);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   134
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   135
9588
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   136
void Squirrel::AddClassBegin(const char *class_name, const char *parent_class)
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   137
{
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   138
	sq_pushroottable(this->vm);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   139
	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   140
	sq_pushstring(this->vm, OTTD2FS(parent_class), -1);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   141
	if (SQ_FAILED(sq_get(this->vm, -3))) {
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   142
		DEBUG(misc, 0, "[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   143
		DEBUG(misc, 0, "[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   144
		return;
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   145
	}
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   146
	sq_newclass(this->vm, SQTrue);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   147
}
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   148
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   149
void Squirrel::AddClassEnd()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   150
{
9525
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   151
	sq_newslot(vm, -3, SQFalse);
10887
5c81038449f2 (svn r13438) [NoAI] -Fix: sq_pop when needed, to make sure the stack keeps as clean as possible
truebrain
parents: 10650
diff changeset
   152
	sq_pop(vm, 1);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   153
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   154
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   155
bool Squirrel::MethodExists(HSQOBJECT instance, const char *method_name)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   156
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   157
	int top = sq_gettop(this->vm);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   158
	/* Go to the instance-root */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   159
	sq_pushobject(this->vm, instance);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   160
	/* Find the function-name inside the script */
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   161
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   162
	if (SQ_FAILED(sq_get(this->vm, -2))) {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   163
		sq_settop(this->vm, top);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   164
		return false;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   165
	}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   166
	sq_settop(this->vm, top);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   167
	return true;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   168
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   169
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   170
void Squirrel::CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   171
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   172
	/* Store the current top */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   173
	int top = sq_gettop(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   174
	/* Go to the instance-root */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   175
	sq_pushobject(this->vm, instance);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   176
	/* Find the function-name inside the script */
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   177
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   178
	if (SQ_FAILED(sq_get(this->vm, -2))) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   179
		DEBUG(misc, 0, "[squirrel] Could not find '%s' in the class", method_name);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   180
		sq_settop(this->vm, top);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   181
		return;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   182
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   183
	/* Call the method */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   184
	sq_pushobject(this->vm, instance);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   185
	sq_call(this->vm, 1, ret == NULL ? SQFalse : SQTrue, SQFalse);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   186
	if (ret != NULL) sq_getstackobj(vm, -1, ret);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   187
	/* Reset the top */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   188
	sq_settop(this->vm, top);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   189
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   190
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   191
/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   192
{
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   193
	int oldtop = sq_gettop(vm);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   194
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   195
	/* First, find the class */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   196
	sq_pushroottable(vm);
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   197
	sq_pushstring(vm, OTTD2FS(class_name), -1);
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   198
	if (SQ_FAILED(sq_get(vm, -2))) {
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   199
		DEBUG(misc, 0, "[squirrel] Failed to find class by the name '%s'", class_name);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   200
		sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   201
		return false;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   202
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   203
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   204
	/* Create the instance */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   205
	if (SQ_FAILED(sq_createinstance(vm, -1))) {
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   206
		DEBUG(misc, 0, "[squirrel] Failed to create instance for class '%s'", class_name);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   207
		sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   208
		return false;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   209
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   210
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   211
	if (instance != NULL) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   212
		/* Find our instance */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   213
		sq_getstackobj(vm, -1, instance);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   214
		/* Add a reference to it, so it survives for ever */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   215
		sq_addref(vm, instance);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   216
	}
9679
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   217
	sq_remove(vm, -2); // Class-name
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   218
	sq_remove(vm, -2); // Root-table
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   219
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   220
	/* Store it in the class */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   221
	sq_setinstanceup(vm, -1, real_instance);
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   222
	if (release_hook != NULL) sq_setreleasehook(vm, -1, release_hook);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   223
9679
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   224
	if (instance != NULL) sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   225
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   226
	return true;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   227
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   228
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   229
bool Squirrel::CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance)
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   230
{
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   231
	return Squirrel::CreateClassInstanceVM(this->vm, class_name, real_instance, instance, NULL);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   232
}
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   233
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   234
Squirrel::Squirrel()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   235
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   236
	this->vm = sq_open(1024);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   237
	this->print_func = NULL;
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   238
	this->global_pointer = NULL;
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   239
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   240
	/* Handle compile-errors ourself, so we can display it nicely */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   241
	sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   242
	sq_notifyallexceptions(this->vm, SQTrue);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   243
	/* Set a good print-function */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   244
	sq_setprintfunc(this->vm, &Squirrel::PrintFunc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   245
	/* Handle runtime-errors ourself, so we can display it nicely */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   246
	sq_newclosure(this->vm, &Squirrel::_RunError, 0);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   247
	sq_seterrorhandler(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   248
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   249
	/* Set the foreigh pointer, so we can always find this instance from within the VM */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   250
	sq_setforeignptr(this->vm, this);
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   251
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   252
	sq_pushroottable(this->vm);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   253
	squirrel_register_global_std(this);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   254
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   255
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   256
static SQInteger _io_file_lexfeed_ASCII(SQUserPointer file)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   257
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   258
	SQInteger ret;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   259
	char c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   260
	if ((ret = fread(&c, sizeof(c), 1, (FILE *)file) > 0)) return c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   261
	return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   262
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   263
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   264
static SQInteger _io_file_lexfeed_UTF8(SQUserPointer file)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   265
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   266
	static const SQInteger utf8_lengths[16] =
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   267
	{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   268
		1, 1, 1, 1, 1, 1, 1, 1, /* 0000 to 0111 : 1 byte (plain ASCII) */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   269
		0, 0, 0, 0,             /* 1000 to 1011 : not valid */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   270
		2, 2,                   /* 1100, 1101 : 2 bytes */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   271
		3,                      /* 1110 : 3 bytes */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   272
		4                       /* 1111 : 4 bytes */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   273
	};
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   274
	static unsigned char byte_masks[5] = {0, 0, 0x1F, 0x0F, 0x07};
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   275
	unsigned char inchar;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   276
	SQInteger c = 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   277
	if (fread(&inchar, sizeof(inchar), 1, (FILE *)file) != 1) return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   278
	c = inchar;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   279
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   280
	if (c >= 0x80) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   281
		SQInteger tmp;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   282
		SQInteger codelen = utf8_lengths[c >> 4];
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   283
		if (codelen == 0) return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   284
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   285
		tmp = c & byte_masks[codelen];
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   286
		for (SQInteger n = 0; n < codelen - 1; n++) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   287
			tmp <<= 6;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   288
			if (fread(&inchar, sizeof(inchar), 1, (FILE *)file) != 1) return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   289
			tmp |= inchar & 0x3F;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   290
		}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   291
		c = tmp;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   292
	}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   293
	return c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   294
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   295
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   296
static SQInteger _io_file_lexfeed_UCS2_LE(SQUserPointer file)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   297
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   298
	SQInteger ret;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   299
	wchar_t c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   300
	if ((ret = fread(&c, sizeof(c), 1, (FILE *)file) > 0)) return (SQChar)c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   301
	return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   302
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   303
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   304
static SQInteger _io_file_lexfeed_UCS2_BE(SQUserPointer file)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   305
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   306
	SQInteger ret;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   307
	unsigned short c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   308
	if ((ret = fread(&c, sizeof(c), 1, (FILE *)file) > 0)) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   309
		c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   310
		return (SQChar)c;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   311
	}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   312
	return 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   313
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   314
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   315
static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   316
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   317
	SQInteger ret = fread(buf, 1, size, (FILE *)file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   318
	if (ret == 0) return -1;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   319
	return ret;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   320
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   321
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   322
/* static */ SQRESULT Squirrel::LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printerror)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   323
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   324
	FILE *file = FioFOpenFile(filename, "rb", AI_DIR);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   325
	SQInteger ret;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   326
	unsigned short us;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   327
	unsigned char uc;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   328
	SQLEXREADFUNC func;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   329
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   330
	if (file != NULL) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   331
		ret = fread(&us, 1, sizeof(us), file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   332
		/* Most likely an empty file */
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   333
		if (ret != 2) us = 0;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   334
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   335
		switch (us) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   336
			case SQ_BYTECODE_STREAM_TAG: // BYTECODE
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   337
				fseek(file, -2, SEEK_CUR);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   338
				if (SQ_SUCCEEDED(sq_readclosure(vm, _io_file_read, file))) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   339
					FioFCloseFile(file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   340
					return SQ_OK;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   341
				}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   342
				return sq_throwerror(vm, _SC("Couldn't read bytecode"));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   343
			case 0xFFFE: func = _io_file_lexfeed_UCS2_BE; break; // UTF-16 little endian
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   344
			case 0xFEFF: func = _io_file_lexfeed_UCS2_LE; break; // UTF-16 big endian
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   345
			case 0xBBEF: // UTF-8
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   346
				if (fread(&uc, 1, sizeof(uc), file) == 0) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   347
					FioFCloseFile(file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   348
					return sq_throwerror(vm, _SC("I/O error"));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   349
				}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   350
				if (uc != 0xBF) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   351
					FioFCloseFile(file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   352
					return sq_throwerror(vm, _SC("Unrecognized encoding"));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   353
				}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   354
				func = _io_file_lexfeed_UTF8;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   355
				break;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   356
			default: func = _io_file_lexfeed_ASCII; fseek(file, -2, SEEK_CUR); break; // ASCII
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   357
		}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   358
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   359
		if (SQ_SUCCEEDED(sq_compile(vm, func, file, OTTD2FS(filename), printerror))) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   360
			FioFCloseFile(file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   361
			return SQ_OK;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   362
		}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   363
		FioFCloseFile(file);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   364
		return SQ_ERROR;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   365
	}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   366
	return sq_throwerror(vm, _SC("cannot open the file"));
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   367
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   368
10887
5c81038449f2 (svn r13438) [NoAI] -Fix: sq_pop when needed, to make sure the stack keeps as clean as possible
truebrain
parents: 10650
diff changeset
   369
/* static */ bool Squirrel::LoadScript(HSQUIRRELVM vm, const char *script, bool in_root)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   370
{
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   371
	/* Make sure we are always in the root-table */
10887
5c81038449f2 (svn r13438) [NoAI] -Fix: sq_pop when needed, to make sure the stack keeps as clean as possible
truebrain
parents: 10650
diff changeset
   372
	if (in_root) sq_pushroottable(vm);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   373
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   374
	/* Load and run the script */
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   375
	if (SQ_SUCCEEDED(LoadFile(vm, script, SQTrue))) {
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   376
		sq_push(vm, -2);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   377
		if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
10887
5c81038449f2 (svn r13438) [NoAI] -Fix: sq_pop when needed, to make sure the stack keeps as clean as possible
truebrain
parents: 10650
diff changeset
   378
			sq_pop(vm, 1);
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   379
			return true;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   380
		}
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   381
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   382
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   383
	DEBUG(misc, 0, "[squirrel] Failed to compile '%s'", script);
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   384
	return false;
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   385
}
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   386
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   387
bool Squirrel::LoadScript(const char *script)
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   388
{
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 10171
diff changeset
   389
	return LoadScript(this->vm, script);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   390
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   391
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   392
Squirrel::~Squirrel()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   393
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   394
	/* Clean up the stuff */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   395
	sq_pop(this->vm, 1);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   396
	sq_close(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   397
}