OpenTTD
game_info.cpp
Go to the documentation of this file.
1 /* $Id: game_info.cpp 27780 2017-03-11 20:50:26Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "../stdafx.h"
13 
14 #include "../script/squirrel_class.hpp"
15 #include "game_info.hpp"
16 #include "game_scanner.hpp"
17 #include "../debug.h"
18 
19 #include "../safeguards.h"
20 
25 static bool CheckAPIVersion(const char *api_version)
26 {
27  return strcmp(api_version, "1.2") == 0 || strcmp(api_version, "1.3") == 0 || strcmp(api_version, "1.4") == 0 ||
28  strcmp(api_version, "1.5") == 0 || strcmp(api_version, "1.6") == 0 || strcmp(api_version, "1.7") == 0 ||
29  strcmp(api_version, "1.8") == 0;
30 }
31 
32 #if defined(WIN32)
33 #undef GetClassName
34 #endif /* WIN32 */
35 template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
36 
38 {
39  /* Create the GSInfo class, and add the RegisterGS function */
40  DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
41  SQGSInfo.PreRegister(engine);
42  SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
43  SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
44  SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
45  SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
46  SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
47  SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
48  SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
49  SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
50 
51  SQGSInfo.PostRegister(engine);
52  engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
53 }
54 
55 /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
56 {
57  /* Get the GameInfo */
58  SQUserPointer instance = NULL;
59  if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
60  GameInfo *info = (GameInfo *)instance;
61 
62  SQInteger res = ScriptInfo::Constructor(vm, info);
63  if (res != 0) return res;
64 
65  if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
66  if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
67  } else {
68  info->min_loadable_version = info->GetVersion();
69  }
70  /* When there is an IsSelectable function, call it. */
71  if (info->engine->MethodExists(*info->SQ_instance, "IsDeveloperOnly")) {
72  if (!info->engine->CallBoolMethod(*info->SQ_instance, "IsDeveloperOnly", &info->is_developer_only, MAX_GET_OPS)) return SQ_ERROR;
73  } else {
74  info->is_developer_only = false;
75  }
76  /* Try to get the API version the AI is written for. */
77  if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
78  if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
79  if (!CheckAPIVersion(info->api_version)) {
80  DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
81  return SQ_ERROR;
82  }
83 
84  /* Remove the link to the real instance, else it might get deleted by RegisterGame() */
85  sq_setinstanceup(vm, 2, NULL);
86  /* Register the Game to the base system */
87  info->GetScanner()->RegisterScript(info);
88  return 0;
89 }
90 
91 GameInfo::GameInfo() :
93  is_developer_only(false),
94  api_version(NULL)
95 {
96 }
97 
98 GameInfo::~GameInfo()
99 {
100  free(this->api_version);
101 }
102 
104 {
105  if (version == -1) return true;
106  return version >= this->min_loadable_version && version <= this->GetVersion();
107 }
108 
109 
110 GameLibrary::~GameLibrary()
111 {
112  free(this->category);
113 }
114 
116 {
117  /* Create the GameLibrary class, and add the RegisterLibrary function */
118  engine->AddClassBegin("GSLibrary");
119  engine->AddClassEnd();
120  engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, 2, "tx");
121 }
122 
123 /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
124 {
125  /* Create a new library */
126  GameLibrary *library = new GameLibrary();
127 
128  SQInteger res = ScriptInfo::Constructor(vm, library);
129  if (res != 0) {
130  delete library;
131  return res;
132  }
133 
134  /* Cache the category */
135  if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
136  delete library;
137  return SQ_ERROR;
138  }
139 
140  /* Register the Library to the base system */
141  library->GetScanner()->RegisterScript(library);
142 
143  return 0;
144 }