2009-01-13 15:49:32 +00:00
|
|
|
/* $Id$ */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-06-01 12:56:18 +00:00
|
|
|
/** @file squirrel_std.cpp Implements the Squirrel Standard Function class */
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
#include <squirrel.h>
|
2009-09-10 20:19:12 +00:00
|
|
|
#include <sqstdmath.h>
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "squirrel.hpp"
|
|
|
|
#include "squirrel_std.hpp"
|
|
|
|
#include "../core/alloc_func.hpp"
|
|
|
|
#include "../core/math_func.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
SQInteger SquirrelStd::min(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
SQInteger tmp1, tmp2;
|
|
|
|
|
|
|
|
sq_getinteger(vm, 2, &tmp1);
|
|
|
|
sq_getinteger(vm, 3, &tmp2);
|
|
|
|
sq_pushinteger(vm, ::min(tmp1, tmp2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQInteger SquirrelStd::max(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
SQInteger tmp1, tmp2;
|
|
|
|
|
|
|
|
sq_getinteger(vm, 2, &tmp1);
|
|
|
|
sq_getinteger(vm, 3, &tmp2);
|
|
|
|
sq_pushinteger(vm, ::max(tmp1, tmp2));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SQInteger SquirrelStd::require(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
SQInteger top = sq_gettop(vm);
|
|
|
|
const SQChar *filename;
|
|
|
|
SQChar *real_filename;
|
|
|
|
|
|
|
|
sq_getstring(vm, 2, &filename);
|
|
|
|
|
|
|
|
/* Get the script-name of the current file, so we can work relative from it */
|
|
|
|
SQStackInfos si;
|
|
|
|
sq_stackinfos(vm, 1, &si);
|
|
|
|
if (si.source == NULL) {
|
|
|
|
DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
|
|
|
|
return SQ_ERROR;
|
|
|
|
}
|
|
|
|
real_filename = scstrdup(si.source);
|
|
|
|
/* Keep the dir, remove the rest */
|
|
|
|
SQChar *s = scstrrchr(real_filename, PATHSEPCHAR);
|
|
|
|
if (s != NULL) {
|
|
|
|
/* Keep the PATHSEPCHAR there, remove the rest */
|
2009-05-03 21:56:46 +00:00
|
|
|
s++;
|
2009-01-12 17:11:45 +00:00
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
/* And now we concat, so we are relative from the current script
|
|
|
|
* First, we have to make sure we have enough space for the full path */
|
|
|
|
real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1);
|
|
|
|
scstrcat(real_filename, filename);
|
|
|
|
/* Tars dislike opening files with '/' on Windows.. so convert it to '\\' ;) */
|
2010-01-18 15:41:38 +00:00
|
|
|
char *filen = strdup(SQ2OTTD(real_filename));
|
2009-01-12 17:11:45 +00:00
|
|
|
#if (PATHSEPCHAR != '/')
|
|
|
|
for (char *n = filen; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool ret = Squirrel::LoadScript(vm, filen);
|
|
|
|
|
|
|
|
/* Reset the top, so the stack stays correct */
|
|
|
|
sq_settop(vm, top);
|
|
|
|
free(real_filename);
|
|
|
|
free(filen);
|
|
|
|
|
2009-03-11 07:32:31 +00:00
|
|
|
return ret ? 0 : SQ_ERROR;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
SQBool b;
|
|
|
|
|
|
|
|
if (sq_gettop(vm) >= 1) {
|
|
|
|
if (SQ_SUCCEEDED(sq_getbool(vm, -1, &b))) {
|
|
|
|
sq_notifyallexceptions(vm, b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return SQ_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void squirrel_register_global_std(Squirrel *engine)
|
|
|
|
{
|
|
|
|
/* We don't use squirrel_helper here, as we want to register to the global
|
|
|
|
* scope and not to a class. */
|
2009-02-21 00:43:18 +00:00
|
|
|
engine->AddMethod("require", &SquirrelStd::require, 2, ".s");
|
|
|
|
engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, 2, ".b");
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void squirrel_register_std(Squirrel *engine)
|
|
|
|
{
|
|
|
|
/* We don't use squirrel_helper here, as we want to register to the global
|
|
|
|
* scope and not to a class. */
|
2009-02-21 00:43:18 +00:00
|
|
|
engine->AddMethod("min", &SquirrelStd::min, 3, ".ii");
|
|
|
|
engine->AddMethod("max", &SquirrelStd::max, 3, ".ii");
|
2009-09-10 20:19:12 +00:00
|
|
|
|
|
|
|
sqstd_register_mathlib(engine->GetVM());
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|