squirrel_std.cpp

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

Generated on Wed Dec 23 23:27:54 2009 for OpenTTD by  doxygen 1.5.6