OpenTTD

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