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