game_info.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 "../stdafx.h"
00013 
00014 #include "../script/squirrel_class.hpp"
00015 #include "game_info.hpp"
00016 #include "game_scanner.hpp"
00017 #include "../debug.h"
00018 
00023 static bool CheckAPIVersion(const char *api_version)
00024 {
00025   return strcmp(api_version, "1.2") == 0 || strcmp(api_version, "1.3") == 0 || strcmp(api_version, "1.4") == 0;
00026 }
00027 
00028 #if defined(WIN32)
00029 #undef GetClassName
00030 #endif /* WIN32 */
00031 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
00032 
00033 /* static */ void GameInfo::RegisterAPI(Squirrel *engine)
00034 {
00035   /* Create the GSInfo class, and add the RegisterGS function */
00036   DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
00037   SQGSInfo.PreRegister(engine);
00038   SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
00039   SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
00040   SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
00041   SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
00042   SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
00043   SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
00044   SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
00045   SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
00046 
00047   SQGSInfo.PostRegister(engine);
00048   engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
00049 }
00050 
00051 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
00052 {
00053   /* Get the GameInfo */
00054   SQUserPointer instance = NULL;
00055   if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of GameInfo to RegisterGame"));
00056   GameInfo *info = (GameInfo *)instance;
00057 
00058   SQInteger res = ScriptInfo::Constructor(vm, info);
00059   if (res != 0) return res;
00060 
00061   if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
00062     if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
00063   } else {
00064     info->min_loadable_version = info->GetVersion();
00065   }
00066   /* When there is an IsSelectable function, call it. */
00067   if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
00068     if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
00069   } else {
00070     info->is_developer_only = false;
00071   }
00072   /* Try to get the API version the AI is written for. */
00073   if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
00074   if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
00075   if (!CheckAPIVersion(info->api_version)) {
00076     DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
00077     return SQ_ERROR;
00078   }
00079 
00080   /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
00081   sq_setinstanceup(vm, 2, NULL);
00082   /* Register the Game to the base system */
00083   info->GetScanner()->RegisterScript(info);
00084   return 0;
00085 }
00086 
00087 GameInfo::GameInfo() :
00088   min_loadable_version(0),
00089   is_developer_only(false),
00090   api_version(NULL)
00091 {
00092 }
00093 
00094 GameInfo::~GameInfo()
00095 {
00096   free(this->api_version);
00097 }
00098 
00099 bool GameInfo::CanLoadFromVersion(int version) const
00100 {
00101   if (version == -1) return true;
00102   return version >= this->min_loadable_version && version <= this->GetVersion();
00103 }
00104 
00105 
00106 GameLibrary::~GameLibrary()
00107 {
00108   free(this->category);
00109 }
00110 
00111 /* static */ void GameLibrary::RegisterAPI(Squirrel *engine)
00112 {
00113   /* Create the GameLibrary class, and add the RegisterLibrary function */
00114   engine->AddClassBegin("GSLibrary");
00115   engine->AddClassEnd();
00116   engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
00117 }
00118 
00119 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
00120 {
00121   /* Create a new library */
00122   GameLibrary *library = new GameLibrary();
00123 
00124   SQInteger res = ScriptInfo::Constructor(vm, library);
00125   if (res != 0) {
00126     delete library;
00127     return res;
00128   }
00129 
00130   /* Cache the category */
00131   if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
00132     delete library;
00133     return SQ_ERROR;
00134   }
00135 
00136   /* Register the Library to the base system */
00137   library->GetScanner()->RegisterScript(library);
00138 
00139   return 0;
00140 }