summaryrefslogtreecommitdiff
path: root/src/game/game_scanner.cpp
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2011-12-19 20:55:56 +0000
committertruebrain <truebrain@openttd.org>2011-12-19 20:55:56 +0000
commitc99950c21537f9c890e4eac6d077c0ec6f31b7aa (patch)
treec130b8aa9cf12c7c3a1c9ef014bb35df3f53035f /src/game/game_scanner.cpp
parentb4f832f29f44dcd48e8f0806d47ce78b1963d639 (diff)
downloadopenttd-c99950c21537f9c890e4eac6d077c0ec6f31b7aa.tar.xz
(svn r23606) -Add: GameScanner, to auto-detect game scripts, and wire it in the console
Diffstat (limited to 'src/game/game_scanner.cpp')
-rw-r--r--src/game/game_scanner.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/game/game_scanner.cpp b/src/game/game_scanner.cpp
new file mode 100644
index 000000000..54b684c3e
--- /dev/null
+++ b/src/game/game_scanner.cpp
@@ -0,0 +1,89 @@
+/* $Id$ */
+
+/*
+ * This file is part of OpenTTD.
+ * 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.
+ * 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.
+ * 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/>.
+ */
+
+/** @file game_scanner.cpp allows scanning Game scripts */
+
+#include "../stdafx.h"
+#include "../debug.h"
+#include "../fileio_func.h"
+#include "../network/network.h"
+#include "../core/random_func.hpp"
+
+#include "../script/squirrel_class.hpp"
+#include "game_info.hpp"
+#include "game_scanner.hpp"
+#include "../script/api/script_controller.hpp"
+
+
+GameScannerInfo::GameScannerInfo() :
+ ScriptScanner()
+{
+}
+
+void GameScannerInfo::Initialize()
+{
+ ScriptScanner::Initialize("GSScanner");
+}
+
+void GameScannerInfo::GetScriptName(ScriptInfo *info, char *name, int len)
+{
+ snprintf(name, len, "%s", info->GetName());
+}
+
+void GameScannerInfo::RegisterAPI(class Squirrel *engine)
+{
+ GameInfo::RegisterAPI(engine);
+}
+
+GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, bool force_exact_match)
+{
+ if (this->info_list.size() == 0) return NULL;
+ if (nameParam == NULL) return NULL;
+
+ char game_name[1024];
+ ttd_strlcpy(game_name, nameParam, sizeof(game_name));
+ strtolower(game_name);
+
+ GameInfo *info = NULL;
+ int version = -1;
+
+ if (versionParam == -1) {
+ /* We want to load the latest version of this Game script; so find it */
+ if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast<GameInfo *>(this->info_single_list[game_name]);
+
+ /* If we didn't find a match Game script, maybe the user included a version */
+ char *e = strrchr(game_name, '.');
+ if (e == NULL) return NULL;
+ *e = '\0';
+ e++;
+ versionParam = atoi(e);
+ /* FALL THROUGH, like we were calling this function with a version. */
+ }
+
+ if (force_exact_match) {
+ /* Try to find a direct 'name.version' match */
+ char game_name_tmp[1024];
+ snprintf(game_name_tmp, sizeof(game_name_tmp), "%s.%d", game_name, versionParam);
+ strtolower(game_name_tmp);
+ if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast<GameInfo *>(this->info_list[game_name_tmp]);
+ }
+
+ /* See if there is a compatible Game script which goes by that name, with the highest
+ * version which allows loading the requested version */
+ ScriptInfoList::iterator it = this->info_list.begin();
+ for (; it != this->info_list.end(); it++) {
+ GameInfo *i = static_cast<GameInfo *>((*it).second);
+ if (strcasecmp(game_name, i->GetName()) == 0 && i->CanLoadFromVersion(versionParam) && (version == -1 || i->GetVersion() > version)) {
+ version = (*it).second->GetVersion();
+ info = i;
+ }
+ }
+
+ return info;
+}