OpenTTD
game_info.cpp
Go to the documentation of this file.
1 /* $Id: game_info.cpp 27993 2018-03-14 19:36:41Z 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 || strcmp(api_version, "1.9") == 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 }
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:74
This setting will only be visible when the Script development tools are active.
void AddMethod(const char *method_name, SQFUNCTION proc, uint nparam=0, const char *params=NULL, void *userdata=NULL, int size=0)
Adds a function to the stack.
Definition: squirrel.cpp:116
int version
Version of the script.
HSQOBJECT * SQ_instance
The Squirrel instance created for this info.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:37
bool CheckMethod(const char *name) const
Check if a given method exists.
Definition: script_info.cpp:49
bool is_developer_only
Is the script selectable by non-developers?
Definition: game_info.hpp:47
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:59
All static information from an Game like name, version, etc.
Definition: game_info.hpp:18
No flags set.
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:52
The template to define classes in Squirrel.
void RegisterScript(class ScriptInfo *info)
Register a ScriptInfo to the scanner.
SQInteger AddLabels(HSQUIRRELVM vm)
Add labels for a setting.
static SQInteger Constructor(HSQUIRRELVM vm)
Create an Game, using this GameInfo as start-template.
Definition: game_info.cpp:55
int min_loadable_version
The Game can load savegame data if the version is equal or greater than this.
Definition: game_info.hpp:46
static SQInteger Constructor(HSQUIRRELVM vm)
Create an GSLibrary, using this GSInfo as start-template.
Definition: game_info.cpp:123
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:103
const char * category
The category this library is in.
Definition: game_info.hpp:73
When randomizing the Script, pick any value between min_value and max_value when on custom difficulty...
This value is a boolean (either 0 (false) or 1 (true) ).
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:115
virtual class ScriptScanner * GetScanner()
Get the scanner which has found this ScriptInfo.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
SQInteger AddSetting(HSQUIRRELVM vm)
Set a setting.
declarations of the class for Game scanner
const char * api_version
API version used by this Game.
Definition: game_info.hpp:48
static const int MAX_GET_OPS
Number of operations to get the author and similar information.
Definition: script_info.hpp:27
bool MethodExists(HSQOBJECT instance, const char *method_name)
Check if a method exists in an instance.
Definition: squirrel.cpp:171
void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, const char *function_name)
This defines a method inside a class for Squirrel, which has access to the &#39;engine&#39; (experts only!)...
static bool CheckAPIVersion(const char *api_version)
Check if the API version provided by the Game is supported.
Definition: game_info.cpp:25
void AddClassBegin(const char *class_name)
Adds a class to the global scope.
Definition: squirrel.cpp:145
This setting can be changed while the Script is running.
class Squirrel * engine
Engine used to register for Squirrel.
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info)
Process the creation of a FileInfo object.
Definition: script_info.cpp:60
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
void AddClassEnd()
Finishes adding a class to the global scope.
Definition: squirrel.cpp:165