squirrel_std.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include <squirrel.h>
00013 #include <sqstdmath.h>
00014 #include "../stdafx.h"
00015 #include "../debug.h"
00016 #include "squirrel_std.hpp"
00017 #include "../core/alloc_func.hpp"
00018 #include "../core/math_func.hpp"
00019 
00020 
00021 SQInteger SquirrelStd::min(HSQUIRRELVM vm)
00022 {
00023   SQInteger tmp1, tmp2;
00024 
00025   sq_getinteger(vm, 2, &tmp1);
00026   sq_getinteger(vm, 3, &tmp2);
00027   sq_pushinteger(vm, ::min(tmp1, tmp2));
00028   return 1;
00029 }
00030 
00031 SQInteger SquirrelStd::max(HSQUIRRELVM vm)
00032 {
00033   SQInteger tmp1, tmp2;
00034 
00035   sq_getinteger(vm, 2, &tmp1);
00036   sq_getinteger(vm, 3, &tmp2);
00037   sq_pushinteger(vm, ::max(tmp1, tmp2));
00038   return 1;
00039 }
00040 
00041 SQInteger SquirrelStd::require(HSQUIRRELVM vm)
00042 {
00043   SQInteger top = sq_gettop(vm);
00044   const SQChar *filename;
00045   SQChar *real_filename;
00046 
00047   sq_getstring(vm, 2, &filename);
00048 
00049   /* Get the script-name of the current file, so we can work relative from it */
00050   SQStackInfos si;
00051   sq_stackinfos(vm, 1, &si);
00052   if (si.source == NULL) {
00053     DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
00054     return SQ_ERROR;
00055   }
00056   real_filename = scstrdup(si.source);
00057   /* Keep the dir, remove the rest */
00058   SQChar *s = scstrrchr(real_filename, PATHSEPCHAR);
00059   if (s != NULL) {
00060     /* Keep the PATHSEPCHAR there, remove the rest */
00061     s++;
00062     *s = '\0';
00063   }
00064   /* And now we concat, so we are relative from the current script
00065    * First, we have to make sure we have enough space for the full path */
00066   real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1);
00067   scstrcat(real_filename, filename);
00068   /* Tars dislike opening files with '/' on Windows.. so convert it to '\\' ;) */
00069   char *filen = strdup(SQ2OTTD(real_filename));
00070 #if (PATHSEPCHAR != '/')
00071   for (char *n = filen; *n != '\0'; n++) if (*n == '/') *n = PATHSEPCHAR;
00072 #endif
00073 
00074   bool ret = Squirrel::LoadScript(vm, filen);
00075 
00076   /* Reset the top, so the stack stays correct */
00077   sq_settop(vm, top);
00078   free(real_filename);
00079   free(filen);
00080 
00081   return ret ? 0 : SQ_ERROR;
00082 }
00083 
00084 SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
00085 {
00086   SQBool b;
00087 
00088   if (sq_gettop(vm) >= 1) {
00089     if (SQ_SUCCEEDED(sq_getbool(vm, -1, &b))) {
00090       sq_notifyallexceptions(vm, b);
00091       return 0;
00092     }
00093   }
00094 
00095   return SQ_ERROR;
00096 }
00097 
00098 void squirrel_register_global_std(Squirrel *engine)
00099 {
00100   /* We don't use squirrel_helper here, as we want to register to the global
00101    *  scope and not to a class. */
00102   engine->AddMethod("require",             &SquirrelStd::require,             2, ".s");
00103   engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, 2, ".b");
00104 }
00105 
00106 void squirrel_register_std(Squirrel *engine)
00107 {
00108   /* We don't use squirrel_helper here, as we want to register to the global
00109    *  scope and not to a class. */
00110   engine->AddMethod("min", &SquirrelStd::min, 3, ".ii");
00111   engine->AddMethod("max", &SquirrelStd::max, 3, ".ii");
00112 
00113   sqstd_register_mathlib(engine->GetVM());
00114 }

Generated on Sun Jun 5 04:20:02 2011 for OpenTTD by  doxygen 1.6.1