00001
00002
00003
00004
00005
00006
00007
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
00035 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
00036
00037 void GameInfo::RegisterAPI(Squirrel *engine)
00038 {
00039
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 SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
00056 {
00057
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
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
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
00085 sq_setinstanceup(vm, 2, NULL);
00086
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 void GameLibrary::RegisterAPI(Squirrel *engine)
00116 {
00117
00118 engine->AddClassBegin("GSLibrary");
00119 engine->AddClassEnd();
00120 engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
00121 }
00122
00123 SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
00124 {
00125
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
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
00141 library->GetScanner()->RegisterScript(library);
00142
00143 return 0;
00144 }