OpenTTD
game_scanner.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 
12 #include "../script/squirrel_class.hpp"
13 #include "game_info.hpp"
14 #include "game_scanner.hpp"
15 
16 #include "../safeguards.h"
17 
18 
19 void GameScannerInfo::Initialize()
20 {
21  ScriptScanner::Initialize("GSScanner");
22 }
23 
24 void GameScannerInfo::GetScriptName(ScriptInfo *info, char *name, const char *last)
25 {
26  seprintf(name, last, "%s", info->GetName());
27 }
28 
30 {
31  GameInfo::RegisterAPI(engine);
32 }
33 
34 GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
35 {
36  if (this->info_list.size() == 0) return nullptr;
37  if (nameParam == nullptr) return nullptr;
38 
39  char game_name[1024];
40  strecpy(game_name, nameParam, lastof(game_name));
41  strtolower(game_name);
42 
43  GameInfo *info = nullptr;
44  int version = -1;
45 
46  if (versionParam == -1) {
47  /* We want to load the latest version of this Game script; so find it */
48  if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);
49 
50  /* If we didn't find a match Game script, maybe the user included a version */
51  char *e = strrchr(game_name, '.');
52  if (e == nullptr) return nullptr;
53  *e = '\0';
54  e++;
55  versionParam = atoi(e);
56  /* Continue like we were calling this function with a version. */
57  }
58 
59  if (force_exact_match) {
60  /* Try to find a direct 'name.version' match */
61  char game_name_tmp[1024];
62  seprintf(game_name_tmp, lastof(game_name_tmp), "%s.%d", game_name, versionParam);
63  strtolower(game_name_tmp);
64  if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
65  }
66 
67  /* See if there is a compatible Game script which goes by that name, with the highest
68  * version which allows loading the requested version */
69  ScriptInfoList::iterator it = this->info_list.begin();
70  for (; it != this->info_list.end(); it++) {
71  GameInfo *i = static_cast<GameInfo *>((*it).second);
72  if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
73  version = (*it).second->GetVersion();
74  info = i;
75  }
76  }
77 
78  return info;
79 }
80 
81 
82 void GameScannerLibrary::Initialize()
83 {
84  ScriptScanner::Initialize("GSScanner");
85 }
86 
87 void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
88 {
89  GameLibrary *library = static_cast<GameLibrary *>(info);
90  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
91 }
92 
94 {
96 }
97 
98 GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
99 {
100  /* Internally we store libraries as 'library.version' */
101  char library_name[1024];
102  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
103  strtolower(library_name);
104 
105  /* Check if the library + version exists */
106  ScriptInfoList::iterator iter = this->info_list.find(library_name);
107  if (iter == this->info_list.end()) return nullptr;
108 
109  return static_cast<GameLibrary *>((*iter).second);
110 }
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:72
class GameLibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:35
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:407
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:57
class GameInfo * FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
Check if we have a game by name and version available in our list.
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
#define lastof(x)
Get the last element of an fixed size array.
Definition: depend.cpp:48
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:330
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:50
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:101
ScriptInfoList info_list
The list of all script.
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:113
GameInfo keeps track of all information of an Game, like Author, Description, ... ...
declarations of the class for Game scanner
const char * GetCategory() const
Get the category this library is in.
Definition: game_info.hpp:68
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: depend.cpp:66
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:82
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
class Squirrel * engine
The engine we&#39;re scanning with.