OpenTTD Source  12.0-beta2
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 {
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  if (versionParam == -1) {
44  /* We want to load the latest version of this Game script; so find it */
45  if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);
46  return nullptr;
47  }
48 
49  if (force_exact_match) {
50  /* Try to find a direct 'name.version' match */
51  char game_name_tmp[1024];
52  seprintf(game_name_tmp, lastof(game_name_tmp), "%s.%d", game_name, versionParam);
53  strtolower(game_name_tmp);
54  if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
55  return nullptr;
56  }
57 
58  GameInfo *info = nullptr;
59  int version = -1;
60 
61  /* See if there is a compatible Game script which goes by that name, with the highest
62  * version which allows loading the requested version */
63  for (const auto &item : this->info_list) {
64  GameInfo *i = static_cast<GameInfo *>(item.second);
65  if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
66  version = item.second->GetVersion();
67  info = i;
68  }
69  }
70 
71  return info;
72 }
73 
74 
75 void GameScannerLibrary::Initialize()
76 {
77  ScriptScanner::Initialize("GSScanner");
78 }
79 
80 void GameScannerLibrary::GetScriptName(ScriptInfo *info, char *name, const char *last)
81 {
82  GameLibrary *library = static_cast<GameLibrary *>(info);
83  seprintf(name, last, "%s.%s", library->GetCategory(), library->GetInstanceName());
84 }
85 
87 {
89 }
90 
91 GameLibrary *GameScannerLibrary::FindLibrary(const char *library, int version)
92 {
93  /* Internally we store libraries as 'library.version' */
94  char library_name[1024];
95  seprintf(library_name, lastof(library_name), "%s.%d", library, version);
96  strtolower(library_name);
97 
98  /* Check if the library + version exists */
99  ScriptInfoList::iterator it = this->info_list.find(library_name);
100  if (it == this->info_list.end()) return nullptr;
101 
102  return static_cast<GameLibrary *>((*it).second);
103 }
game_info.hpp
strtolower
bool strtolower(char *str)
Convert a given ASCII string to lowercase.
Definition: string.cpp:447
ScriptScanner::engine
class Squirrel * engine
The engine we're scanning with.
Definition: script_scanner.hpp:86
Squirrel
Definition: squirrel.hpp:23
GameScannerInfo::RegisterAPI
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: game_scanner.cpp:29
GameInfo::CanLoadFromVersion
bool CanLoadFromVersion(int version) const
Check if we can start this Game.
Definition: game_info.cpp:101
GameScannerLibrary::FindLibrary
class GameLibrary * FindLibrary(const char *library, int version)
Find a library in the pool.
Definition: game_scanner.cpp:91
GameLibrary::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:113
GameLibrary::GetCategory
const char * GetCategory() const
Get the category this library is in.
Definition: game_info.hpp:68
game_scanner.hpp
ScriptInfo::GetInstanceName
const char * GetInstanceName() const
Get the name of the instance of the script to create.
Definition: script_info.hpp:80
GameScannerInfo::GetScriptName
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
Definition: game_scanner.cpp:24
GameScannerLibrary::GetScriptName
void GetScriptName(ScriptInfo *info, char *name, const char *last) override
Get the script name how to store the script in memory.
Definition: game_scanner.cpp:80
GameInfo::RegisterAPI
static void RegisterAPI(Squirrel *engine)
Register the functions of this class.
Definition: game_info.cpp:35
ScriptScanner::info_list
ScriptInfoList info_list
The list of all script.
Definition: script_scanner.hpp:90
ScriptInfo::GetVersion
int GetVersion() const
Get the version of the script.
Definition: script_info.hpp:70
GameInfo
All static information from an Game like name, version, etc.
Definition: game_info.hpp:16
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:535
GameScannerLibrary::RegisterAPI
void RegisterAPI(class Squirrel *engine) override
Register the API for this ScriptInfo.
Definition: game_scanner.cpp:86
GameLibrary
All static information from an Game library like name, version, etc.
Definition: game_info.hpp:50
ScriptInfo::GetName
const char * GetName() const
Get the Name of the script.
Definition: script_info.hpp:55
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
ScriptInfo
All static information from an Script like name, version, etc.
Definition: script_info.hpp:30
GameScannerInfo::FindInfo
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.
Definition: game_scanner.cpp:34
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:394
ScriptScanner::info_single_list
ScriptInfoList info_single_list
The list of all unique script. The best script (highest version) is shown.
Definition: script_scanner.hpp:91