# script_info.cpp

Go to the documentation of this file.

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