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/>.
10 /** @file game_scanner.cpp allows scanning Game scripts */
12 #include "../stdafx.h"
14 #include "../script/squirrel_class.hpp"
15 #include "game_info.hpp"
16 #include "game_scanner.hpp"
19 void GameScannerInfo::Initialize()
21 ScriptScanner::Initialize("GSScanner");
24 void GameScannerInfo::GetScriptName(ScriptInfo
*info
, char *name
, int len
)
26 snprintf(name
, len
, "%s", info
->GetName());
29 void GameScannerInfo::RegisterAPI(class Squirrel
*engine
)
31 GameInfo::RegisterAPI(engine
);
34 GameInfo
*GameScannerInfo::FindInfo(const char *nameParam
, int versionParam
, bool force_exact_match
)
36 if (this->info_list
.size() == 0) return NULL
;
37 if (nameParam
== NULL
) return NULL
;
40 ttd_strlcpy(game_name
, nameParam
, sizeof(game_name
));
41 strtolower(game_name
);
43 GameInfo
*info
= NULL
;
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
]);
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
== NULL
) return NULL
;
55 versionParam
= atoi(e
);
56 /* FALL THROUGH, like we were calling this function with a version. */
59 if (force_exact_match
) {
60 /* Try to find a direct 'name.version' match */
61 char game_name_tmp
[1024];
62 snprintf(game_name_tmp
, sizeof(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
]);
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();
82 void GameScannerLibrary::Initialize()
84 ScriptScanner::Initialize("GSScanner");
87 void GameScannerLibrary::GetScriptName(ScriptInfo
*info
, char *name
, int len
)
89 GameLibrary
*library
= static_cast<GameLibrary
*>(info
);
90 snprintf(name
, len
, "%s.%s", library
->GetCategory(), library
->GetInstanceName());
93 void GameScannerLibrary::RegisterAPI(class Squirrel
*engine
)
95 GameLibrary::RegisterAPI(engine
);
98 GameLibrary
*GameScannerLibrary::FindLibrary(const char *library
, int version
)
100 /* Internally we store libraries as 'library.version' */
101 char library_name
[1024];
102 snprintf(library_name
, sizeof(library_name
), "%s.%d", library
, version
);
103 strtolower(library_name
);
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 NULL
;
109 return static_cast<GameLibrary
*>((*iter
).second
);