(svn r23606) -Add: GameScanner, to auto-detect game scripts, and wire it in the console
parent
14325e2391
commit
17fea560ba
@ -0,0 +1,45 @@
|
||||
/* $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_config.cpp Implementation of GameConfig. */
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include "../settings_type.h"
|
||||
#include "../core/random_func.hpp"
|
||||
#include "game.hpp"
|
||||
#include "game_config.hpp"
|
||||
#include "game_info.hpp"
|
||||
|
||||
/* static */ GameConfig *GameConfig::GetConfig(ScriptSettingSource source)
|
||||
{
|
||||
GameConfig **config;
|
||||
if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) {
|
||||
config = &_settings_newgame.game_config;
|
||||
} else {
|
||||
config = &_settings_game.game_config;
|
||||
}
|
||||
if (*config == NULL) *config = new GameConfig();
|
||||
return *config;
|
||||
}
|
||||
|
||||
class GameInfo *GameConfig::GetInfo() const
|
||||
{
|
||||
return static_cast<class GameInfo *>(ScriptConfig::GetInfo());
|
||||
}
|
||||
|
||||
ScriptInfo *GameConfig::FindInfo(const char *name, int version, bool force_exact_match)
|
||||
{
|
||||
return static_cast<ScriptInfo *>(Game::FindInfo(name, version, force_exact_match));
|
||||
}
|
||||
|
||||
bool GameConfig::ResetInfo(bool force_exact_match)
|
||||
{
|
||||
this->info = (ScriptInfo *)Game::FindInfo(this->name, force_exact_match ? this->version : -1, force_exact_match);
|
||||
return this->info != NULL;
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/* $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_config.hpp GameConfig stores the configuration settings of every Game. */
|
||||
|
||||
#ifndef GAME_CONFIG_HPP
|
||||
#define GAME_CONFIG_HPP
|
||||
|
||||
#include "../script/script_config.hpp"
|
||||
|
||||
class GameConfig : public ScriptConfig {
|
||||
public:
|
||||
/**
|
||||
* Get the config of a company.
|
||||
*/
|
||||
static GameConfig *GetConfig(ScriptSettingSource source = SSS_DEFAULT);
|
||||
|
||||
GameConfig() :
|
||||
ScriptConfig()
|
||||
{}
|
||||
|
||||
GameConfig(const GameConfig *config) :
|
||||
ScriptConfig(config)
|
||||
{}
|
||||
|
||||
class GameInfo *GetInfo() const;
|
||||
|
||||
/**
|
||||
* When ever the Game Scanner is reloaded, all infos become invalid. This
|
||||
* function tells GameConfig about this.
|
||||
* @param force_exact_match If true try to find the exact same version
|
||||
* as specified. If false any version is ok.
|
||||
* @return \c true if the reset was successful, \c false if the Game was no longer
|
||||
* found.
|
||||
*/
|
||||
bool ResetInfo(bool force_exact_match);
|
||||
|
||||
protected:
|
||||
/* virtual */ ScriptInfo *FindInfo(const char *name, int version, bool force_exact_match);
|
||||
};
|
||||
|
||||
#endif /* GAME_CONFIG_HPP */
|
@ -0,0 +1,101 @@
|
||||
/* $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_info.cpp Implementation of GameInfo */
|
||||
|
||||
#include "../stdafx.h"
|
||||
|
||||
#include "../script/squirrel_helper.hpp"
|
||||
#include "../script/squirrel_class.hpp"
|
||||
#include "game_info.hpp"
|
||||
#include "game_scanner.hpp"
|
||||
#include "../settings_type.h"
|
||||
#include "../debug.h"
|
||||
#include "../rev.h"
|
||||
#include "game.hpp"
|
||||
|
||||
/**
|
||||
* Check if the API version provided by the Game is supported.
|
||||
* @param api_version The API version as provided by the Game.
|
||||
*/
|
||||
static bool CheckAPIVersion(const char *api_version)
|
||||
{
|
||||
return strcmp(api_version, "1.2") == 0;
|
||||
}
|
||||
|
||||
#if defined(WIN32)
|
||||
#undef GetClassName
|
||||
#endif /* WIN32 */
|
||||
template <> const char *GetClassName<GameInfo, ST_GS>() { return "GSInfo"; }
|
||||
|
||||
/* static */ void GameInfo::RegisterAPI(Squirrel *engine)
|
||||
{
|
||||
/* Create the GSInfo class, and add the RegisterGS function */
|
||||
DefSQClass<GameInfo, ST_GS> SQGSInfo("GSInfo");
|
||||
SQGSInfo.PreRegister(engine);
|
||||
SQGSInfo.AddConstructor<void (GameInfo::*)(), 1>(engine, "x");
|
||||
SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddSetting, "AddSetting");
|
||||
SQGSInfo.DefSQAdvancedMethod(engine, &GameInfo::AddLabels, "AddLabels");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_NONE, "CONFIG_NONE");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_RANDOM, "CONFIG_RANDOM");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_BOOLEAN, "CONFIG_BOOLEAN");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_INGAME, "CONFIG_INGAME");
|
||||
SQGSInfo.DefSQConst(engine, SCRIPTCONFIG_DEVELOPER, "CONFIG_DEVELOPER");
|
||||
|
||||
SQGSInfo.PostRegister(engine);
|
||||
engine->AddMethod("RegisterGS", &GameInfo::Constructor, 2, "tx");
|
||||
}
|
||||
|
||||
/* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm)
|
||||
{
|
||||
/* Get the GameInfo */
|
||||
SQUserPointer instance = NULL;
|
||||
if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, 0)) || instance == NULL) return sq_throwerror(vm, _SC("Pass an instance of a child class of GameInfo to RegisterGame"));
|
||||
GameInfo *info = (GameInfo *)instance;
|
||||
|
||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
||||
if (res != 0) return res;
|
||||
|
||||
if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) {
|
||||
if (!info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad", &info->min_loadable_version, MAX_GET_OPS)) return SQ_ERROR;
|
||||
} else {
|
||||
info->min_loadable_version = info->GetVersion();
|
||||
}
|
||||
|
||||
/* Try to get the API version the AI is written for. */
|
||||
if (!info->CheckMethod("GetAPIVersion")) return SQ_ERROR;
|
||||
if (!info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAPIVersion", &info->api_version, MAX_GET_OPS)) return SQ_ERROR;
|
||||
if (!CheckAPIVersion(info->api_version)) {
|
||||
DEBUG(script, 1, "Loading info.nut from (%s.%d): GetAPIVersion returned invalid version", info->GetName(), info->GetVersion());
|
||||
return SQ_ERROR;
|
||||
}
|
||||
|
||||
/* Remove the link to the real instance, else it might get deleted by RegisterGame() */
|
||||
sq_setinstanceup(vm, 2, NULL);
|
||||
/* Register the Game to the base system */
|
||||
info->GetScanner()->RegisterScript(info);
|
||||
return 0;
|
||||
}
|
||||
|
||||
GameInfo::GameInfo() :
|
||||
min_loadable_version(0),
|
||||
api_version(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
GameInfo::~GameInfo()
|
||||
{
|
||||
free(this->api_version);
|
||||
}
|
||||
|
||||
bool GameInfo::CanLoadFromVersion(int version) const
|
||||
{
|
||||
if (version == -1) return true;
|
||||
return version >= this->min_loadable_version && version <= this->GetVersion();
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/* $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_info.hpp GameInfo keeps track of all information of an Game, like Author, Description, ... */
|
||||
|
||||
#ifndef GAME_INFO_HPP
|
||||
#define GAME_INFO_HPP
|
||||
|
||||
#include "../script/script_info.hpp"
|
||||
#include "../script/script_config.hpp"
|
||||
|
||||
/** All static information from an Game like name, version, etc. */
|
||||
class GameInfo : public ScriptInfo {
|
||||
public:
|
||||
GameInfo();
|
||||
~GameInfo();
|
||||
|
||||
/**
|
||||
* Register the functions of this class.
|
||||
*/
|
||||
static void RegisterAPI(Squirrel *engine);
|
||||
|
||||
/**
|
||||
* Create an Game, using this GameInfo as start-template.
|
||||
*/
|
||||
static SQInteger Constructor(HSQUIRRELVM vm);
|
||||
|
||||
/**
|
||||
* Check if we can start this Game.
|
||||
*/
|
||||
bool CanLoadFromVersion(int version) const;
|
||||
|
||||
/**
|
||||
* Get the API version this Game is written for.
|
||||
*/
|
||||
const char *GetAPIVersion() const { return this->api_version; }
|
||||
|
||||
private:
|
||||
int min_loadable_version; ///< The Game can load savegame data if the version is equal or greater than this.
|
||||
const char *api_version; ///< API version used by this Game.
|
||||
};
|
||||
|
||||
#endif /* GAME_INFO_HPP */
|
@ -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;
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/* $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.hpp declarations of the class for Game scanner */
|
||||
|
||||
#ifndef GAME_SCANNER_HPP
|
||||
#define GAME_SCANNER_HPP
|
||||
|
||||
#include "../script/script_scanner.hpp"
|
||||
|
||||
class GameScannerInfo : public ScriptScanner {
|
||||
public:
|
||||
GameScannerInfo();
|
||||
|
||||
/* virtual */ void Initialize();
|
||||
|
||||
/**
|
||||
* Check if we have a game by name and version available in our list.
|
||||
* @param nameParam The name of the game script.
|
||||
* @param versionParam The versionof the game script, or -1 if you want the latest.
|
||||
* @param force_exact_match Only match name+version, never latest.
|
||||
* @return NULL if no match found, otherwise the game script that matched.
|
||||
*/
|
||||
class GameInfo *FindInfo(const char *nameParam, int versionParam, bool force_exact_match);
|
||||
|
||||
protected:
|
||||
/* virtual */ void GetScriptName(ScriptInfo *info, char *name, int len);
|
||||
/* virtual */ const char *GetFileName() const { return PATHSEP "info.nut"; }
|
||||
/* virtual */ Subdirectory GetDirectory() const { return GAME_DIR; }
|
||||
/* virtual */ const char *GetScannerName() const { return "Game Scripts"; }
|
||||
/* virtual */ void RegisterAPI(class Squirrel *engine);
|
||||
};
|
||||
|
||||
#endif /* GAME_SCANNER_HPP */
|
Loading…
Reference in New Issue