script_info.cpp

Go to the documentation of this file.
00001 /* $Id: script_info.cpp 21311 2010-11-24 17:00:37Z rubidium $ */
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 "../stdafx.h"
00013 
00014 #include "squirrel_helper.hpp"
00015 
00016 #include "script_info.hpp"
00017 #include "script_scanner.hpp"
00018 
00020 static const int MAX_GET_OPS            =   1000;
00022 static const int MAX_CREATEINSTANCE_OPS = 100000;
00023 
00024 ScriptFileInfo::~ScriptFileInfo()
00025 {
00026   free((void *)this->author);
00027   free((void *)this->name);
00028   free((void *)this->short_name);
00029   free((void *)this->description);
00030   free((void *)this->date);
00031   free((void *)this->instance_name);
00032   free((void *)this->url);
00033   free(this->main_script);
00034   free(this->SQ_instance);
00035 }
00036 
00037 bool ScriptFileInfo::CheckMethod(const char *name) const
00038 {
00039   if (!this->engine->MethodExists(*this->SQ_instance, name)) {
00040     char error[1024];
00041     snprintf(error, sizeof(error), "your info.nut/library.nut doesn't have the method '%s'", name);
00042     this->engine->ThrowError(error);
00043     return false;
00044   }
00045   return true;
00046 }
00047 
00048 /* static */ SQInteger ScriptFileInfo::Constructor(HSQUIRRELVM vm, ScriptFileInfo *info)
00049 {
00050   /* Set some basic info from the parent */
00051   info->SQ_instance = MallocT<SQObject>(1);
00052   Squirrel::GetInstance(vm, info->SQ_instance, 2);
00053   /* Make sure the instance stays alive over time */
00054   sq_addref(vm, info->SQ_instance);
00055   ScriptScanner *scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
00056   info->engine = scanner->GetEngine();
00057 
00058   static const char * const required_functions[] = {
00059     "GetAuthor",
00060     "GetName",
00061     "GetShortName",
00062     "GetDescription",
00063     "GetVersion",
00064     "GetDate",
00065     "CreateInstance",
00066   };
00067   for (size_t i = 0; i < lengthof(required_functions); i++) {
00068     if (!info->CheckMethod(required_functions[i])) return SQ_ERROR;
00069   }
00070 
00071   info->main_script = strdup(scanner->GetMainScript());
00072 
00073   /* Cache the data the info file gives us. */
00074   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
00075   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
00076   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
00077   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
00078   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
00079   if (!info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
00080   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
00081 
00082   /* The GetURL function is optional. */
00083   if (info->engine->MethodExists(*info->SQ_instance, "GetURL")) {
00084     if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
00085   }
00086 
00087   return 0;
00088 }

Generated on Sun Jan 9 16:02:01 2011 for OpenTTD by  doxygen 1.6.1