src/squirrel_std.cpp
author glx
Mon, 26 May 2008 17:40:33 +0000
branchnoai
changeset 10718 7e9d9e40e16f
parent 10649 9034b80fdbdb
child 10938 df6235dd2b7a
permissions -rw-r--r--
(svn r13268) [NoAI] -Fix: kill AIs on game abortion
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     1
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     2
#include <squirrel.h>
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     3
#include "stdafx.h"
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
     4
#include "debug.h"
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     5
#include "squirrel.hpp"
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     6
#include "squirrel_std.hpp"
9725
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
     7
#include "core/alloc_func.hpp"
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     8
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     9
/* abs() is normally defined to myabs(), which we don't want in this file */
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    10
#undef abs
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    11
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    12
SQInteger SquirrelStd::abs(HSQUIRRELVM vm)
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    13
{
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    14
	SQInteger tmp;
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    15
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    16
	sq_getinteger(vm, 2, &tmp);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9578
diff changeset
    17
	sq_pushinteger(vm, ::abs(tmp));
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    18
	return 1;
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    19
}
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    20
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    21
SQInteger SquirrelStd::require(HSQUIRRELVM vm)
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    22
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    23
	SQInteger top = sq_gettop(vm);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    24
	const SQChar *filename;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    25
	SQChar *real_filename;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    26
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    27
	sq_getstring(vm, 2, &filename);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    28
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    29
	/* Get the script-name of the current file, so we can work relative from it */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    30
	SQStackInfos si;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    31
	sq_stackinfos(vm, 1, &si);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    32
	if (si.source == NULL) {
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    33
		DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    34
		return SQ_ERROR;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    35
	}
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    36
	real_filename = scstrdup(si.source);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    37
	/* Keep the dir, remove the rest */
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    38
	SQChar *s = scstrrchr(real_filename, PATHSEPCHAR);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    39
	if (s != NULL) {
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    40
		/* Keep the PATHSEPCHAR there, remove the rest */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    41
		*s++;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    42
		*s = '\0';
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    43
	}
9725
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    44
	/* And now we concat, so we are relative from the current script
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    45
	 * First, we have to make sure we have enough space for the full path */
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    46
	real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1);
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    47
	scstrcat(real_filename, filename);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    48
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: 9727
diff changeset
    49
	bool ret = Squirrel::LoadScript(vm, FS2OTTD(real_filename));
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    50
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    51
	/* Reset the top, so the stack stays correct */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    52
	sq_settop(vm, top);
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    53
	free(real_filename);
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    54
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: 9727
diff changeset
    55
	return ret ? 0: SQ_ERROR;
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    56
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    57
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    58
void squirrel_register_global_std(Squirrel *engine)
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    59
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    60
	/* We don't use squirrel_helper here, as we want to register to the global
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    61
	 *  scope and not to a class. */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    62
	engine->AddMethod("require", &SquirrelStd::require, 2, "?s");
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    63
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    64
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    65
void squirrel_register_std(Squirrel *engine)
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    66
{
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    67
	/* We don't use squirrel_helper here, as we want to register to the global
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    68
	 *  scope and not to a class. */
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    69
	engine->AddMethod("abs", &SquirrelStd::abs, 2, "xi");
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    70
}