OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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