Use PlatformEnvironment for track design repository

This commit is contained in:
Ted John 2016-12-12 01:30:27 +00:00
parent cc227b7761
commit 7811987ad1
3 changed files with 45 additions and 46 deletions

View File

@ -173,14 +173,18 @@ extern "C"
OpenRCT2::SetupEnvironment(); OpenRCT2::SetupEnvironment();
IObjectRepository * objRepo = CreateObjectRepository(OpenRCT2::_env); IObjectRepository * objRepo = CreateObjectRepository(OpenRCT2::_env);
// TODO Ideally we want to delay this until we show the title so that we can // TODO Ideally we want to delay this until we show the title so that we can
// still open the game window and draw a progress screen for the creation // still open the game window and draw a progress screen for the creation
// of the object cache. // of the object cache.
objRepo->LoadOrConstruct(); objRepo->LoadOrConstruct();
CreateScenarioRepository(OpenRCT2::_env); CreateScenarioRepository(OpenRCT2::_env);
GetTrackRepository()->Scan();
ITrackDesignRepository * tdRepo = CreateTrackDesignRepository(OpenRCT2::_env);
// TODO Like objects, this can take a while if there are a lot of track designs
// its also really something really we might want to do in the background
// as its not required until the player wants to place a new ride.
tdRepo->Scan();
if (!gOpenRCT2Headless) { if (!gOpenRCT2Headless) {
audio_init(); audio_init();

View File

@ -22,6 +22,7 @@
#include "../core/FileStream.hpp" #include "../core/FileStream.hpp"
#include "../core/Path.hpp" #include "../core/Path.hpp"
#include "../core/String.hpp" #include "../core/String.hpp"
#include "../PlatformEnvironment.h"
#include "TrackDesignRepository.h" #include "TrackDesignRepository.h"
extern "C" extern "C"
@ -64,13 +65,21 @@ enum TRACK_REPO_ITEM_FLAGS
class TrackDesignRepository : public ITrackDesignRepository class TrackDesignRepository : public ITrackDesignRepository
{ {
private: private:
IPlatformEnvironment * _env;
std::vector<TrackRepositoryItem> _items; std::vector<TrackRepositoryItem> _items;
QueryDirectoryResult _directoryQueryResult; QueryDirectoryResult _directoryQueryResult;
public: public:
TrackDesignRepository(IPlatformEnvironment * env)
{
Guard::ArgumentNotNull(env);
_env = env;
}
virtual ~TrackDesignRepository() virtual ~TrackDesignRepository()
{ {
} }
size_t GetCount() const override size_t GetCount() const override
@ -117,11 +126,8 @@ public:
void Scan() override void Scan() override
{ {
utf8 rct2Directory[MAX_PATH]; std::string rct2Directory = _env->GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK);
utf8 userDirectory[MAX_PATH]; std::string userDirectory = _env->GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::TRACK);
GetRCT2Directory(rct2Directory, sizeof(rct2Directory));
GetUserDirectory(userDirectory, sizeof(userDirectory));
_items.clear(); _items.clear();
_directoryQueryResult = { 0 }; _directoryQueryResult = { 0 };
@ -191,9 +197,10 @@ public:
{ {
const utf8 * result = nullptr; const utf8 * result = nullptr;
const utf8 * fileName = Path::GetFileName(path); const utf8 * fileName = Path::GetFileName(path);
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
utf8 newPath[MAX_PATH]; utf8 newPath[MAX_PATH];
GetUserDirectory(newPath, sizeof(newPath)); String::Set(newPath, sizeof(newPath), installDir.c_str());
Path::Append(newPath, sizeof(newPath), fileName); Path::Append(newPath, sizeof(newPath), fileName);
if (platform_file_copy(path, newPath, false)) if (platform_file_copy(path, newPath, false))
@ -211,18 +218,18 @@ public:
} }
private: private:
void Query(const utf8 * directory) void Query(const std::string &directory)
{ {
utf8 pattern[MAX_PATH]; utf8 pattern[MAX_PATH];
String::Set(pattern, sizeof(pattern), directory); String::Set(pattern, sizeof(pattern), directory.c_str());
Path::Append(pattern, sizeof(pattern), "*.td4;*.td6"); Path::Append(pattern, sizeof(pattern), "*.td4;*.td6");
Path::QueryDirectory(&_directoryQueryResult, pattern); Path::QueryDirectory(&_directoryQueryResult, pattern);
} }
void Scan(const utf8 * directory, uint32 flags = 0) void Scan(const std::string &directory, uint32 flags = 0)
{ {
utf8 pattern[MAX_PATH]; utf8 pattern[MAX_PATH];
String::Set(pattern, sizeof(pattern), directory); String::Set(pattern, sizeof(pattern), directory.c_str());
Path::Append(pattern, sizeof(pattern), "*.td4;*.td6"); Path::Append(pattern, sizeof(pattern), "*.td4;*.td6");
IFileScanner * scanner = Path::ScanDirectory(pattern, true); IFileScanner * scanner = Path::ScanDirectory(pattern, true);
@ -268,9 +275,7 @@ private:
bool Load() bool Load()
{ {
utf8 path[MAX_PATH]; std::string path = _env->GetFilePath(PATHID::CACHE_TRACKS);
GetRepositoryPath(path, sizeof(path));
bool result = false; bool result = false;
try try
{ {
@ -308,9 +313,7 @@ private:
void Save() const void Save() const
{ {
utf8 path[MAX_PATH]; std::string path = _env->GetFilePath(PATHID::CACHE_TRACKS);
GetRepositoryPath(path, sizeof(path));
try try
{ {
auto fs = FileStream(path, FILE_MODE_WRITE); auto fs = FileStream(path, FILE_MODE_WRITE);
@ -372,19 +375,6 @@ private:
return buffer; return buffer;
} }
utf8 * GetUserDirectory(utf8 * buffer, size_t bufferSize) const
{
platform_get_user_directory(buffer, "track", bufferSize);
return buffer;
}
utf8 * GetRepositoryPath(utf8 * buffer, size_t bufferSize) const
{
platform_get_user_directory(buffer, nullptr, bufferSize);
Path::Append(buffer, bufferSize, "tracks.idx");
return buffer;
}
public: public:
static utf8 * GetNameFromTrackPath(const utf8 * path) static utf8 * GetNameFromTrackPath(const utf8 * path)
{ {
@ -395,52 +385,54 @@ public:
} }
}; };
static std::unique_ptr<TrackDesignRepository> _trackRepository; static std::unique_ptr<TrackDesignRepository> _trackDesignRepository;
ITrackDesignRepository * GetTrackRepository() ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env)
{ {
if (_trackRepository == nullptr) _trackDesignRepository = std::unique_ptr<TrackDesignRepository>(new TrackDesignRepository(env));
{ return _trackDesignRepository.get();
_trackRepository = std::unique_ptr<TrackDesignRepository>(new TrackDesignRepository()); }
}
return _trackRepository.get(); ITrackDesignRepository * GetTrackDesignRepository()
{
return _trackDesignRepository.get();
} }
extern "C" extern "C"
{ {
void track_repository_scan() void track_repository_scan()
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
repo->Scan(); repo->Scan();
} }
size_t track_repository_get_count_for_ride(uint8 rideType, const utf8 * entry) size_t track_repository_get_count_for_ride(uint8 rideType, const utf8 * entry)
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->GetCountForObjectEntry(rideType, entry); return repo->GetCountForObjectEntry(rideType, entry);
} }
size_t track_repository_get_items_for_ride(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry) size_t track_repository_get_items_for_ride(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry)
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->GetItemsForObjectEntry(outRefs, rideType, entry); return repo->GetItemsForObjectEntry(outRefs, rideType, entry);
} }
bool track_repository_delete(const utf8 * path) bool track_repository_delete(const utf8 * path)
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->Delete(path); return repo->Delete(path);
} }
const utf8 * track_repository_rename(const utf8 * path, const utf8 * newName) const utf8 * track_repository_rename(const utf8 * path, const utf8 * newName)
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->Rename(path, newName); return repo->Rename(path, newName);
} }
const utf8 * track_repository_install(const utf8 * srcPath) const utf8 * track_repository_install(const utf8 * srcPath)
{ {
ITrackDesignRepository * repo = GetTrackRepository(); ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->Install(srcPath); return repo->Install(srcPath);
} }

View File

@ -26,6 +26,8 @@ typedef struct track_design_file_ref
#ifdef __cplusplus #ifdef __cplusplus
interface IPlatformEnvironment;
interface ITrackDesignRepository interface ITrackDesignRepository
{ {
virtual ~ITrackDesignRepository() = default; virtual ~ITrackDesignRepository() = default;
@ -42,7 +44,8 @@ interface ITrackDesignRepository
virtual const utf8 * Install(const utf8 * path) abstract; virtual const utf8 * Install(const utf8 * path) abstract;
}; };
ITrackDesignRepository * GetTrackRepository(); ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env);
ITrackDesignRepository * GetTrackDesignRepository();
#endif #endif