Allow using external full stk-assets

This commit is contained in:
Benau 2019-08-08 09:39:01 +08:00
parent c9656d4410
commit bdf8c589de
9 changed files with 197 additions and 65 deletions

View File

@ -26,6 +26,7 @@
#include "karts/kart_properties_manager.hpp"
#include "tracks/track_manager.hpp"
#include "utils/command_line.hpp"
#include "utils/extract_mobile_assets.hpp"
#include "utils/file_utils.hpp"
#include "utils/log.hpp"
#include "utils/string_utils.hpp"
@ -130,24 +131,7 @@ FileManager* file_manager = 0;
*/
FileManager::FileManager()
{
m_subdir_name.resize(ASSET_COUNT);
m_subdir_name[CHALLENGE ] = "challenges";
m_subdir_name[GFX ] = "gfx";
m_subdir_name[GRANDPRIX ] = "grandprix";
m_subdir_name[GUI_ICON ] = "gui/icons";
m_subdir_name[GUI_SCREEN ] = "gui/screens";
m_subdir_name[GUI_DIALOG ] = "gui/dialogs";
m_subdir_name[LIBRARY ] = "library";
m_subdir_name[MODEL ] = "models";
m_subdir_name[MUSIC ] = "music";
m_subdir_name[REPLAY ] = "replay";
m_subdir_name[SCRIPT ] = "tracks";
m_subdir_name[SFX ] = "sfx";
m_subdir_name[SKIN ] = "skins";
m_subdir_name[SHADER ] = "shaders";
m_subdir_name[TEXTURE ] = "textures";
m_subdir_name[TTF ] = "ttf";
m_subdir_name[TRANSLATION] = "po";
resetSubdir();
#ifdef __APPLE__
// irrLicht's createDevice method has a nasty habit of messing the CWD.
// since the code above may rely on it, save it to be able to restore
@ -229,26 +213,13 @@ FileManager::FileManager()
std::string assets_dir;
#ifdef MOBILE_STK
// Check if the bundled data includes stk-assets, if not download it later
// Check only 1 entry for now (karts)
if (!fileExists(root_dir + "/karts"))
{
assets_dir = getenv("HOME");
m_stk_assets_download_dir = getenv("HOME");
#ifdef IOS_STK
assets_dir += "/Library/Application Support/SuperTuxKart/stk-assets";
m_stk_assets_download_dir += "/Library/Application Support/SuperTuxKart/stk-assets-full/";
#elif defined (ANDROID)
assets_dir += "/stk-assets";
m_stk_assets_download_dir += "/stk-assets-full/";
#endif
m_stk_assets_download_dir = assets_dir;
// Those will be filled with real data later
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/karts");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/library");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/models");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/music");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/sfx");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/textures");
checkAndCreateDirectoryP(m_stk_assets_download_dir + "/tracks");
}
#else
if (getenv("SUPERTUXKART_ASSETS_DIR") != NULL)
{
@ -283,11 +254,38 @@ FileManager::FileManager()
redirectOutput();
} // FileManager
// ----------------------------------------------------------------------------
/** Reset subdirectories to initial state, for example after download assets
*/
void FileManager::resetSubdir()
{
m_subdir_name.clear();
m_subdir_name.resize(ASSET_COUNT);
m_subdir_name[CHALLENGE ] = "challenges";
m_subdir_name[GFX ] = "gfx";
m_subdir_name[GRANDPRIX ] = "grandprix";
m_subdir_name[GUI_ICON ] = "gui/icons";
m_subdir_name[GUI_SCREEN ] = "gui/screens";
m_subdir_name[GUI_DIALOG ] = "gui/dialogs";
m_subdir_name[LIBRARY ] = "library";
m_subdir_name[MODEL ] = "models";
m_subdir_name[MUSIC ] = "music";
m_subdir_name[REPLAY ] = "replay";
m_subdir_name[SCRIPT ] = "tracks";
m_subdir_name[SFX ] = "sfx";
m_subdir_name[SKIN ] = "skins";
m_subdir_name[SHADER ] = "shaders";
m_subdir_name[TEXTURE ] = "textures";
m_subdir_name[TTF ] = "ttf";
m_subdir_name[TRANSLATION] = "po";
} // resetSubdir
// ----------------------------------------------------------------------------
/** Detects where the assets are stored.
*/
void FileManager::discoverPaths()
{
resetSubdir();
// We can't use _() here, since translations will only be initalised
// after the filemanager (to get the path to the tranlsations from it)
for(unsigned int i=0; i<m_root_dirs.size(); i++)
@ -302,11 +300,50 @@ void FileManager::discoverPaths()
Log::info("[FileManager]", "User-defined grand prix will be stored in '%s'.",
m_gp_dir.c_str());
// Reset for re-downloading assets if needed
TrackManager::removeTrackSearchDirs();
KartPropertiesManager::removeKartSearchDirs();
/** Now search for the path to all needed subdirectories. */
// ==========================================================
// This must be done here since otherwise translations will not be found.
std::vector<bool> dir_found;
dir_found.resize(ASSET_COUNT, false);
#ifdef MOBILE_STK
assert(!m_root_dirs.empty());
for (unsigned j = ASSET_MIN; j <= BUILTIN_ASSETS; j++)
{
if (!dir_found[j] && fileExists(m_root_dirs[0] + m_subdir_name[j]))
{
dir_found[j] = true;
m_subdir_name[j] = m_root_dirs[0] + m_subdir_name[j] + "/";
}
}
// Use stk-assets-full for karts, tracks, textures..., otherwise in data/
std::string assets_root = ExtractMobileAssets::hasFullAssets() ?
m_stk_assets_download_dir : m_root_dirs[0];
for (unsigned j = LIBRARY; j <= ASSET_MAX; j++)
{
if (!dir_found[j] && fileExists(assets_root + m_subdir_name[j]))
{
dir_found[j] = true;
m_subdir_name[j] = assets_root + m_subdir_name[j] + "/";
}
}
if (fileExists(assets_root + "tracks/"))
TrackManager::addTrackSearchDir(assets_root + "tracks/");
if (fileExists(assets_root + "karts/"))
KartPropertiesManager::addKartSearchDir(assets_root + "karts/");
if (UserConfigParams::m_artist_debug_mode)
{
if (fileExists(assets_root + "wip-tracks/"))
TrackManager::addTrackSearchDir(assets_root + "wip-tracks/");
if (fileExists(assets_root + "wip-karts/"))
KartPropertiesManager::addKartSearchDir(assets_root + "wip-karts/");
}
#else
for(unsigned int i=0; i<m_root_dirs.size(); i++)
{
if(fileExists(m_root_dirs[i]+"tracks/"))
@ -332,6 +369,7 @@ void FileManager::discoverPaths()
} // !dir_found && file_exist
} // for j=ASSET_MIN; j<=ASSET_MAX
} // for i<m_root_dirs
#endif
bool was_error = false;
for(unsigned int i=ASSET_MIN; i<=ASSET_MAX; i++)
@ -396,6 +434,7 @@ void FileManager::reinitAfterDownloadAssets()
m_texture_search_path.clear();
m_model_search_path.clear();
m_music_search_path.clear();
discoverPaths();
addAssetsSearchPath();
} // reinitAfterDownloadAssets

View File

@ -61,9 +61,8 @@ public:
enum AssetType {ASSET_MIN,
CHALLENGE=ASSET_MIN,
GFX, GRANDPRIX, GUI_ICON, GUI_SCREEN, GUI_DIALOG,
LIBRARY, MODEL, MUSIC, REPLAY,
SCRIPT, SFX, SHADER, SKIN, TEXTURE, TTF,
TRANSLATION, ASSET_MAX = TRANSLATION,
REPLAY, SHADER, SKIN, TTF, TRANSLATION, BUILTIN_ASSETS=TRANSLATION,
LIBRARY, MODEL, MUSIC, SFX, TEXTURE, SCRIPT, ASSET_MAX = SCRIPT,
ASSET_COUNT};
private:
@ -132,6 +131,7 @@ private:
void checkAndCreateGPDir();
void discoverPaths();
void addAssetsSearchPath();
void resetSubdir();
#if !defined(WIN32) && !defined(__CYGWIN__) && !defined(__APPLE__)
std::string checkAndCreateLinuxDir(const char *env_name,
const char *dir_name,

View File

@ -55,6 +55,12 @@ KartPropertiesManager::~KartPropertiesManager()
{
} // ~KartPropertiesManager
//-----------------------------------------------------------------------------
void KartPropertiesManager::removeKartSearchDirs()
{
m_kart_search_path.clear();
} // removeKartSearchDirs
//-----------------------------------------------------------------------------
/** Adds a directory from which karts are loaded. The kart manager checks if
* either this directory itself contains a kart, and if any subdirectory

View File

@ -78,6 +78,7 @@ protected:
public:
KartPropertiesManager();
~KartPropertiesManager();
static void removeKartSearchDirs();
static void addKartSearchDir (const std::string &s);
const KartProperties* getKartById (int i) const;
const KartProperties* getKart(const std::string &ident) const;

View File

@ -234,7 +234,6 @@
#include "race/race_manager.hpp"
#include "replay/replay_play.hpp"
#include "replay/replay_recorder.hpp"
#include "states_screens/download_assets.hpp"
#include "states_screens/main_menu_screen.hpp"
#include "states_screens/online/networking_lobby.hpp"
#include "states_screens/online/register_screen.hpp"
@ -1713,31 +1712,6 @@ void initRest()
input_manager = new InputManager();
// Get into menu mode initially.
input_manager->setMode(InputManager::MENU);
#ifdef MOBILE_STK
if (DownloadAssets::getInstance()->needDownloadAssets())
{
// The screen tell user it will use wifi / cellular data to download already
int prev_state = UserConfigParams::m_internet_status;
UserConfigParams::m_internet_status = Online::RequestManager::IPERM_ALLOWED;
DownloadAssets::getInstance()->push();
main_loop = new MainLoop(0, true/*download_assets*/);
main_loop->run();
delete main_loop;
main_loop = NULL;
// Reset after finish download
UserConfigParams::m_internet_status = prev_state;
if (DownloadAssets::getInstance()->needDownloadAssets())
throw std::runtime_error("User doesn't want to download assets");
else
{
// Clean the download assets screen after downloading
GUIEngine::clear();
GUIEngine::cleanUp();
GUIEngine::clearScreenCache();
GUIEngine::init(device, driver, StateManager::get());
}
}
#endif
stk_config->initMusicFiles();
// This only initialises the non-network part of the add-ons manager. The

View File

@ -46,6 +46,12 @@ TrackManager::~TrackManager()
delete *i;
} // ~TrackManager
//-----------------------------------------------------------------------------
void TrackManager::removeTrackSearchDirs()
{
m_track_search_path.clear();
} // removeTrackSearchDirs
//-----------------------------------------------------------------------------
/** Adds a directory from which tracks are loaded. The track manager checks if
* either this directory itself contains a track, and if any subdirectory

View File

@ -72,7 +72,7 @@ private:
public:
TrackManager();
~TrackManager();
static void removeTrackSearchDirs();
static void addTrackSearchDir(const std::string &dir);
/** Returns a list of all track identifiers. */
std::vector<std::string> getAllTrackIdentifiers();

View File

@ -0,0 +1,73 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2019 SuperTuxKart-Team
//
// This program 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; either version 3
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifdef MOBILE_STK
#include "utils/extract_mobile_assets.hpp"
#include "addons/zip.hpp"
#include "io/file_manager.hpp"
#include "utils/constants.hpp"
#include "utils/log.hpp"
#ifdef ANDROID
#include <android/asset_manager.h>
#include "../../../lib/irrlicht/source/Irrlicht/CIrrDeviceAndroid.h"
extern struct android_app* global_android_app;
#endif
// ----------------------------------------------------------------------------
bool ExtractMobileAssets::hasFullAssets()
{
const std::string& dir = file_manager->getSTKAssetsDownloadDir();
if (dir.empty())
return false;
return file_manager->fileExists(dir + "stk-assets." + STK_VERSION);
} // hasFullAssets
// ----------------------------------------------------------------------------
bool ExtractMobileAssets::extract(const std::string& zip_file,
const std::string& dst)
{
if (!file_manager->fileExists(zip_file))
return false;
bool succeed = false;
// Remove previous stk-assets version and create a new one
file_manager->removeDirectory(dst);
file_manager->checkAndCreateDirectory(dst);
if (extract_zip(zip_file, dst, true/*recursive*/))
{
std::string extract_ok = dst + "stk-assets." + STK_VERSION;
FILE* fp = fopen(extract_ok.c_str(), "wb");
if (!fp)
{
Log::error("ExtractMobileAssets",
"Failed to create extract ok file.");
}
else
{
fclose(fp);
succeed = true;
}
}
if (succeed)
file_manager->reinitAfterDownloadAssets();
file_manager->removeFile(zip_file);
return succeed;
} // extract
#endif

View File

@ -0,0 +1,33 @@
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2019 SuperTuxKart-Team
//
// This program 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; either version 3
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_EXTRACT_MOBILE_ASSETS_HPP
#define HEADER_EXTRACT_MOBILE_ASSETS_HPP
#ifdef MOBILE_STK
#include <string>
namespace ExtractMobileAssets
{
bool hasFullAssets();
bool extract(const std::string& zip_file, const std::string& dst);
} // namespace FileUtils
#endif
#endif