2017-06-01 21:55:10 +02:00
|
|
|
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
|
2016-10-22 18:06:27 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
|
|
|
*
|
|
|
|
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
|
|
|
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 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.
|
|
|
|
*
|
|
|
|
* A full copy of the GNU General Public License can be found in licence.txt
|
|
|
|
*****************************************************************************/
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
2017-05-20 23:59:51 +02:00
|
|
|
#include "../config/Config.h"
|
2017-01-05 18:51:31 +01:00
|
|
|
#include "../core/Collections.hpp"
|
2016-10-22 18:06:27 +02:00
|
|
|
#include "../core/Console.hpp"
|
2017-01-05 13:55:41 +01:00
|
|
|
#include "../core/File.h"
|
2016-10-22 20:11:37 +02:00
|
|
|
#include "../core/FileScanner.h"
|
2016-10-22 18:06:27 +02:00
|
|
|
#include "../core/FileStream.hpp"
|
|
|
|
#include "../core/Path.hpp"
|
2017-06-14 13:31:27 +02:00
|
|
|
#include "RideGroupManager.h"
|
2016-10-22 18:06:27 +02:00
|
|
|
#include "../core/String.hpp"
|
2017-05-20 23:59:51 +02:00
|
|
|
#include "../object/ObjectRepository.h"
|
|
|
|
#include "../object/RideObject.h"
|
2016-12-12 02:30:27 +01:00
|
|
|
#include "../PlatformEnvironment.h"
|
2016-10-22 18:06:27 +02:00
|
|
|
#include "TrackDesignRepository.h"
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
2016-11-30 12:07:10 +01:00
|
|
|
#include "../rct2.h"
|
2016-10-22 18:06:27 +02:00
|
|
|
#include "track_design.h"
|
|
|
|
}
|
|
|
|
|
2017-06-11 13:53:37 +02:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
2016-10-22 18:06:27 +02:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct TrackRepositoryHeader
|
|
|
|
{
|
|
|
|
uint32 MagicNumber;
|
|
|
|
uint16 Version;
|
2016-10-23 02:12:17 +02:00
|
|
|
uint32 TotalFiles;
|
|
|
|
uint64 TotalFileSize;
|
|
|
|
uint32 FileDateModifiedChecksum;
|
|
|
|
uint32 PathChecksum;
|
2016-10-22 18:06:27 +02:00
|
|
|
uint32 NumItems;
|
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
2016-10-22 19:06:52 +02:00
|
|
|
struct TrackRepositoryItem
|
|
|
|
{
|
|
|
|
std::string Name;
|
|
|
|
std::string Path;
|
|
|
|
uint8 RideType = 0;
|
|
|
|
std::string ObjectEntry;
|
2016-10-22 19:15:48 +02:00
|
|
|
uint32 Flags;
|
2016-10-22 19:06:52 +02:00
|
|
|
};
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2016-10-22 19:16:23 +02:00
|
|
|
constexpr uint32 TRACK_REPOSITORY_MAGIC_NUMBER = 0x58444954;
|
|
|
|
constexpr uint16 TRACK_REPOSITORY_VERSION = 1;
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2016-10-22 19:15:48 +02:00
|
|
|
enum TRACK_REPO_ITEM_FLAGS
|
|
|
|
{
|
|
|
|
TRIF_READ_ONLY = (1 << 0),
|
|
|
|
};
|
|
|
|
|
2017-01-13 12:12:50 +01:00
|
|
|
class TrackDesignRepository final : public ITrackDesignRepository
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
private:
|
2017-01-05 18:51:31 +01:00
|
|
|
static constexpr const utf8 * TD_FILE_PATTERN = "*.td4;*.td6";
|
|
|
|
|
2016-12-12 02:30:27 +01:00
|
|
|
IPlatformEnvironment * _env;
|
|
|
|
|
2016-10-22 19:06:52 +02:00
|
|
|
std::vector<TrackRepositoryItem> _items;
|
2016-12-19 23:59:03 +01:00
|
|
|
QueryDirectoryResult _directoryQueryResult = { 0 };
|
2016-10-22 18:06:27 +02:00
|
|
|
|
|
|
|
public:
|
2016-12-12 02:30:27 +01:00
|
|
|
TrackDesignRepository(IPlatformEnvironment * env)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
Guard::ArgumentNotNull(env);
|
|
|
|
|
|
|
|
_env = env;
|
|
|
|
}
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2017-01-13 12:12:50 +01:00
|
|
|
virtual ~TrackDesignRepository() final
|
2016-12-12 02:30:27 +01:00
|
|
|
{
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetCount() const override
|
|
|
|
{
|
|
|
|
return _items.size();
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:59:51 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param rideType
|
|
|
|
* @param entry The entry name to count the track list of. Leave empty to count track list for the non-separated types (e.g. Hyper-Twister, Car Ride)
|
|
|
|
* @return
|
|
|
|
*/
|
2017-01-05 18:51:31 +01:00
|
|
|
size_t GetCountForObjectEntry(uint8 rideType, const std::string &entry) const override
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
2017-06-14 13:31:27 +02:00
|
|
|
const IObjectRepository * repo = GetObjectRepository();
|
2017-05-20 23:59:51 +02:00
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
for (const auto &item : _items)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-05-20 23:59:51 +02:00
|
|
|
if (item.RideType != rideType)
|
2017-06-14 13:31:27 +02:00
|
|
|
{
|
2017-05-20 23:59:51 +02:00
|
|
|
continue;
|
2017-06-14 13:31:27 +02:00
|
|
|
}
|
2017-05-20 23:59:51 +02:00
|
|
|
|
|
|
|
bool entryIsNotSeparate = false;
|
|
|
|
if (entry.empty())
|
|
|
|
{
|
|
|
|
const ObjectRepositoryItem * ori = repo->FindObject(item.ObjectEntry.c_str());
|
|
|
|
|
|
|
|
if (gConfigInterface.select_by_track_type || !(ori->RideFlags & ORI_RIDE_FLAG_SEPARATE))
|
|
|
|
entryIsNotSeparate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entryIsNotSeparate || String::Equals(item.ObjectEntry, entry, true))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
size_t GetCountForRideGroup(uint8 rideType, const ride_group * rideGroup) const override
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
const IObjectRepository * repo = GetObjectRepository();
|
|
|
|
|
|
|
|
for (const auto &item : _items)
|
|
|
|
{
|
|
|
|
if (item.RideType != rideType)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ObjectRepositoryItem * ori = repo->FindObject(item.ObjectEntry.c_str());
|
|
|
|
ride_group * itemRideGroup = ride_group_find(rideType, ori->RideGroupIndex);
|
|
|
|
|
|
|
|
if (itemRideGroup != NULL && ride_groups_are_equal(itemRideGroup, rideGroup))
|
|
|
|
{
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2017-05-20 23:59:51 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param outRefs
|
|
|
|
* @param rideType
|
|
|
|
* @param entry The entry name to build a track list for. Leave empty to build track list for the non-separated types (e.g. Hyper-Twister, Car Ride)
|
|
|
|
* @return
|
|
|
|
*/
|
2017-01-05 18:51:31 +01:00
|
|
|
size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs, uint8 rideType, const std::string &entry) const override
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
std::vector<track_design_file_ref> refs;
|
2017-06-14 13:31:27 +02:00
|
|
|
const IObjectRepository * repo = GetObjectRepository();
|
2017-05-20 23:59:51 +02:00
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
for (const auto &item : _items)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-05-20 23:59:51 +02:00
|
|
|
if (item.RideType != rideType)
|
2017-06-14 13:31:27 +02:00
|
|
|
{
|
2017-05-20 23:59:51 +02:00
|
|
|
continue;
|
2017-06-14 13:31:27 +02:00
|
|
|
}
|
2017-05-20 23:59:51 +02:00
|
|
|
|
|
|
|
bool entryIsNotSeparate = false;
|
|
|
|
if (entry.empty())
|
|
|
|
{
|
|
|
|
const ObjectRepositoryItem * ori = repo->FindObject(item.ObjectEntry.c_str());
|
|
|
|
|
|
|
|
if (gConfigInterface.select_by_track_type || !(ori->RideFlags & ORI_RIDE_FLAG_SEPARATE))
|
|
|
|
entryIsNotSeparate = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entryIsNotSeparate || String::Equals(item.ObjectEntry, entry, true))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
track_design_file_ref ref;
|
2017-01-05 14:13:22 +01:00
|
|
|
ref.name = String::Duplicate(GetNameFromTrackPath(item.Path));
|
|
|
|
ref.path = String::Duplicate(item.Path);
|
2016-10-22 18:06:27 +02:00
|
|
|
refs.push_back(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
*outRefs = Collections::ToArray(refs);
|
2016-10-22 18:06:27 +02:00
|
|
|
return refs.size();
|
|
|
|
}
|
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
size_t GetItemsForRideGroup(track_design_file_ref **outRefs, uint8 rideType, const ride_group * rideGroup) const override
|
|
|
|
{
|
|
|
|
std::vector<track_design_file_ref> refs;
|
|
|
|
const IObjectRepository * repo = GetObjectRepository();
|
|
|
|
|
|
|
|
for (const auto &item : _items)
|
|
|
|
{
|
|
|
|
if (item.RideType != rideType)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ObjectRepositoryItem * ori = repo->FindObject(item.ObjectEntry.c_str());
|
|
|
|
ride_group * itemRideGroup = ride_group_find(rideType, ori->RideGroupIndex);
|
|
|
|
|
|
|
|
if (itemRideGroup != NULL && ride_groups_are_equal(itemRideGroup, rideGroup))
|
|
|
|
{
|
|
|
|
track_design_file_ref ref;
|
|
|
|
ref.name = String::Duplicate(GetNameFromTrackPath(item.Path));
|
|
|
|
ref.path = String::Duplicate(item.Path);
|
|
|
|
refs.push_back(ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*outRefs = Collections::ToArray(refs);
|
|
|
|
return refs.size();
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:06:27 +02:00
|
|
|
void Scan() override
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
std::string rct2Directory = _env->GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK);
|
2016-12-12 03:06:20 +01:00
|
|
|
std::string userDirectory = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2016-10-23 02:12:17 +02:00
|
|
|
_items.clear();
|
|
|
|
_directoryQueryResult = { 0 };
|
|
|
|
Query(rct2Directory);
|
|
|
|
Query(userDirectory);
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2016-10-23 02:12:17 +02:00
|
|
|
if (!Load())
|
|
|
|
{
|
|
|
|
Scan(rct2Directory, TRIF_READ_ONLY);
|
|
|
|
Scan(userDirectory);
|
|
|
|
SortItems();
|
|
|
|
Save();
|
|
|
|
}
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
bool Delete(const std::string &path) override
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
bool result = false;
|
2016-10-22 19:15:48 +02:00
|
|
|
size_t index = GetTrackIndex(path);
|
|
|
|
if (index != SIZE_MAX)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-10-22 19:15:48 +02:00
|
|
|
const TrackRepositoryItem * item = &_items[index];
|
|
|
|
if (!(item->Flags & TRIF_READ_ONLY))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 19:13:48 +01:00
|
|
|
if (File::Delete(path))
|
2016-10-22 19:15:48 +02:00
|
|
|
{
|
|
|
|
_items.erase(_items.begin() + index);
|
2016-10-23 01:42:22 +02:00
|
|
|
result = true;
|
2016-10-22 19:15:48 +02:00
|
|
|
}
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string Rename(const std::string &path, const std::string &newName) override
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string result;
|
2016-10-22 18:06:27 +02:00
|
|
|
size_t index = GetTrackIndex(path);
|
|
|
|
if (index != SIZE_MAX)
|
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
TrackRepositoryItem * item = &_items[index];
|
2016-10-22 23:35:12 +02:00
|
|
|
if (!(item->Flags & TRIF_READ_ONLY))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string directory = Path::GetDirectory(path);
|
|
|
|
std::string newPath = Path::Combine(directory, newName + Path::GetExtension(path));
|
2017-01-05 19:13:48 +01:00
|
|
|
if (File::Move(path, newPath))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
item->Name = newName;
|
|
|
|
item->Path = newPath;
|
2016-10-22 19:15:48 +02:00
|
|
|
SortItems();
|
2017-01-05 18:51:31 +01:00
|
|
|
result = newPath;
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string Install(const std::string &path) override
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string result;
|
|
|
|
std::string fileName = Path::GetFileName(path);
|
2016-12-12 02:30:27 +01:00
|
|
|
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string newPath = Path::Combine(installDir, fileName);
|
2017-01-05 19:13:48 +01:00
|
|
|
if (File::Copy(path, newPath, false))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
AddTrack(path);
|
|
|
|
SortItems();
|
2017-01-05 18:51:31 +01:00
|
|
|
result = path;
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-12-12 02:30:27 +01:00
|
|
|
void Query(const std::string &directory)
|
2016-10-23 02:12:17 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
|
2016-10-23 02:12:17 +02:00
|
|
|
Path::QueryDirectory(&_directoryQueryResult, pattern);
|
|
|
|
}
|
|
|
|
|
2016-12-12 02:30:27 +01:00
|
|
|
void Scan(const std::string &directory, uint32 flags = 0)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
|
2016-10-22 20:10:16 +02:00
|
|
|
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
|
|
|
|
while (scanner->Next())
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-10-22 20:10:16 +02:00
|
|
|
const utf8 * path = scanner->GetPath();
|
2016-10-22 19:15:48 +02:00
|
|
|
AddTrack(path, flags);
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
2016-10-22 20:10:16 +02:00
|
|
|
delete scanner;
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-05 14:13:22 +01:00
|
|
|
void AddTrack(const std::string path, uint32 flags = 0)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-01-05 14:13:22 +01:00
|
|
|
rct_track_td6 * td6 = track_design_open(path.c_str());
|
2016-10-22 18:06:27 +02:00
|
|
|
if (td6 != nullptr)
|
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
TrackRepositoryItem item;
|
2017-01-05 14:13:22 +01:00
|
|
|
item.Name = GetNameFromTrackPath(path);
|
|
|
|
item.Path = path;
|
2016-10-22 19:06:52 +02:00
|
|
|
item.RideType = td6->type;
|
|
|
|
item.ObjectEntry = std::string(td6->vehicle_object.name, 8);
|
2016-10-22 19:15:48 +02:00
|
|
|
item.Flags = flags;
|
2016-10-22 19:06:52 +02:00
|
|
|
_items.push_back(item);
|
2016-10-22 18:06:27 +02:00
|
|
|
track_design_dispose(td6);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SortItems()
|
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
std::sort(_items.begin(), _items.end(), [](const TrackRepositoryItem &a,
|
|
|
|
const TrackRepositoryItem &b) -> bool
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
if (a.RideType != b.RideType)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
return a.RideType < b.RideType;
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
2017-01-05 14:13:22 +01:00
|
|
|
return String::Compare(a.Name, b.Name) < 0;
|
2016-10-22 18:06:27 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-23 02:12:17 +02:00
|
|
|
bool Load()
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
std::string path = _env->GetFilePath(PATHID::CACHE_TRACKS);
|
2016-10-23 02:12:17 +02:00
|
|
|
bool result = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(path, FILE_MODE_OPEN);
|
|
|
|
|
|
|
|
// Read header, check if we need to re-scan
|
|
|
|
auto header = fs.ReadValue<TrackRepositoryHeader>();
|
|
|
|
if (header.MagicNumber == TRACK_REPOSITORY_MAGIC_NUMBER &&
|
|
|
|
header.Version == TRACK_REPOSITORY_VERSION &&
|
|
|
|
header.TotalFiles == _directoryQueryResult.TotalFiles &&
|
|
|
|
header.TotalFileSize == _directoryQueryResult.TotalFileSize &&
|
|
|
|
header.FileDateModifiedChecksum == _directoryQueryResult.FileDateModifiedChecksum &&
|
|
|
|
header.PathChecksum == _directoryQueryResult.PathChecksum)
|
|
|
|
{
|
|
|
|
// Directory is the same, just read the saved items
|
|
|
|
for (uint32 i = 0; i < header.NumItems; i++)
|
|
|
|
{
|
|
|
|
TrackRepositoryItem item;
|
2016-10-23 22:27:44 +02:00
|
|
|
item.Name = fs.ReadStdString();
|
|
|
|
item.Path = fs.ReadStdString();
|
2016-10-23 02:12:17 +02:00
|
|
|
item.RideType = fs.ReadValue<uint8>();
|
2016-10-23 22:27:44 +02:00
|
|
|
item.ObjectEntry = fs.ReadStdString();
|
2016-10-23 02:12:17 +02:00
|
|
|
item.Flags = fs.ReadValue<uint32>();
|
|
|
|
_items.push_back(item);
|
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 23:32:47 +01:00
|
|
|
catch (const Exception &)
|
2016-10-23 02:12:17 +02:00
|
|
|
{
|
|
|
|
Console::Error::WriteLine("Unable to write object repository index.");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:06:27 +02:00
|
|
|
void Save() const
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
std::string path = _env->GetFilePath(PATHID::CACHE_TRACKS);
|
2016-10-22 18:06:27 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
auto fs = FileStream(path, FILE_MODE_WRITE);
|
|
|
|
|
|
|
|
// Write header
|
|
|
|
TrackRepositoryHeader header = { 0 };
|
2016-10-22 19:16:23 +02:00
|
|
|
header.MagicNumber = TRACK_REPOSITORY_MAGIC_NUMBER;
|
|
|
|
header.Version = TRACK_REPOSITORY_VERSION;
|
2016-10-23 02:12:17 +02:00
|
|
|
header.TotalFiles = _directoryQueryResult.TotalFiles;
|
|
|
|
header.TotalFileSize = _directoryQueryResult.TotalFileSize;
|
|
|
|
header.FileDateModifiedChecksum = _directoryQueryResult.FileDateModifiedChecksum;
|
|
|
|
header.PathChecksum = _directoryQueryResult.PathChecksum;
|
2016-10-22 18:06:27 +02:00
|
|
|
header.NumItems = (uint32)_items.size();
|
|
|
|
fs.WriteValue(header);
|
|
|
|
|
|
|
|
// Write items
|
2016-10-22 19:06:52 +02:00
|
|
|
for (const auto item : _items)
|
|
|
|
{
|
|
|
|
fs.WriteString(item.Name);
|
|
|
|
fs.WriteString(item.Path);
|
|
|
|
fs.WriteValue(item.RideType);
|
|
|
|
fs.WriteString(item.ObjectEntry);
|
2016-10-22 19:15:48 +02:00
|
|
|
fs.WriteValue(item.Flags);
|
2016-10-22 19:06:52 +02:00
|
|
|
}
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
2016-11-30 23:32:47 +01:00
|
|
|
catch (const Exception &)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
Console::Error::WriteLine("Unable to write object repository index.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
size_t GetTrackIndex(const std::string &path) const
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _items.size(); i++)
|
|
|
|
{
|
2017-01-05 18:51:31 +01:00
|
|
|
if (Path::Equals(_items[i].Path, path))
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
TrackRepositoryItem * GetTrackItem(const std::string &path)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-10-22 19:06:52 +02:00
|
|
|
TrackRepositoryItem * result = nullptr;
|
2016-10-22 18:06:27 +02:00
|
|
|
size_t index = GetTrackIndex(path);
|
|
|
|
if (index != SIZE_MAX)
|
|
|
|
{
|
|
|
|
result = &_items[index];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-01-05 14:13:22 +01:00
|
|
|
static std::string GetNameFromTrackPath(const std::string &path)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2017-04-23 16:42:08 +02:00
|
|
|
std::string name = Path::GetFileNameWithoutExtension(path);
|
|
|
|
//The track name should be the file name until the first instance of a dot
|
|
|
|
name = name.substr(0, name.find_first_of("."));
|
|
|
|
return name;
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-12 02:30:27 +01:00
|
|
|
static std::unique_ptr<TrackDesignRepository> _trackDesignRepository;
|
2016-10-22 18:06:27 +02:00
|
|
|
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
_trackDesignRepository = std::unique_ptr<TrackDesignRepository>(new TrackDesignRepository(env));
|
|
|
|
return _trackDesignRepository.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
ITrackDesignRepository * GetTrackDesignRepository()
|
|
|
|
{
|
|
|
|
return _trackDesignRepository.get();
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
void track_repository_scan()
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2016-10-22 18:06:27 +02:00
|
|
|
repo->Scan();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t track_repository_get_count_for_ride(uint8 rideType, const utf8 * entry)
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2017-01-05 19:19:20 +01:00
|
|
|
return repo->GetCountForObjectEntry(rideType, String::ToStd(entry));
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
size_t track_repository_get_count_for_ride_group(uint8 rideType, const ride_group * rideGroup)
|
|
|
|
{
|
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
|
|
|
return repo->GetCountForRideGroup(rideType, rideGroup);
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:06:27 +02:00
|
|
|
size_t track_repository_get_items_for_ride(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry)
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2017-01-05 19:19:20 +01:00
|
|
|
return repo->GetItemsForObjectEntry(outRefs, rideType, String::ToStd(entry));
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:31:27 +02:00
|
|
|
size_t track_repository_get_items_for_ride_group(track_design_file_ref * * outRefs, uint8 rideType, const ride_group * rideGroup)
|
|
|
|
{
|
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
|
|
|
return repo->GetItemsForRideGroup(outRefs, rideType, rideGroup);
|
|
|
|
}
|
|
|
|
|
2016-10-22 18:06:27 +02:00
|
|
|
bool track_repository_delete(const utf8 * path)
|
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2016-10-22 18:06:27 +02:00
|
|
|
return repo->Delete(path);
|
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
bool track_repository_rename(const utf8 * path, const utf8 * newName)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string newPath = repo->Rename(path, newName);
|
|
|
|
return !newPath.empty();
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
2017-01-05 18:51:31 +01:00
|
|
|
bool track_repository_install(const utf8 * srcPath)
|
2016-10-22 18:06:27 +02:00
|
|
|
{
|
2016-12-12 02:30:27 +01:00
|
|
|
ITrackDesignRepository * repo = GetTrackDesignRepository();
|
2017-01-05 18:51:31 +01:00
|
|
|
std::string newPath = repo->Install(srcPath);
|
|
|
|
return !newPath.empty();
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
utf8 * track_repository_get_name_from_path(const utf8 * path)
|
|
|
|
{
|
2017-01-05 14:13:22 +01:00
|
|
|
return String::Duplicate(TrackDesignRepository::GetNameFromTrackPath(path));
|
2016-10-22 18:06:27 +02:00
|
|
|
}
|
|
|
|
}
|