OpenRCT2/src/openrct2/ride/TrackDesignRepository.cpp

527 lines
16 KiB
C++
Raw Normal View History

#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>
#include "../config/Config.h"
#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"
#include "RideGroupManager.h"
2016-10-22 18:06:27 +02:00
#include "../core/String.hpp"
#include "../object/ObjectRepository.h"
#include "../object/RideObject.h"
#include "../PlatformEnvironment.h"
2016-10-22 18:06:27 +02:00
#include "TrackDesignRepository.h"
extern "C"
{
#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)
struct TrackRepositoryItem
{
std::string Name;
std::string Path;
uint8 RideType = 0;
std::string ObjectEntry;
uint32 Flags;
};
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
enum TRACK_REPO_ITEM_FLAGS
{
TRIF_READ_ONLY = (1 << 0),
};
class TrackDesignRepository final : public ITrackDesignRepository
2016-10-22 18:06:27 +02:00
{
private:
static constexpr const utf8 * TD_FILE_PATTERN = "*.td4;*.td6";
IPlatformEnvironment * _env;
std::vector<TrackRepositoryItem> _items;
2016-12-19 23:59:03 +01:00
QueryDirectoryResult _directoryQueryResult = { 0 };
2016-10-22 18:06:27 +02:00
public:
TrackDesignRepository(IPlatformEnvironment * env)
2016-10-22 18:06:27 +02:00
{
Guard::ArgumentNotNull(env);
_env = env;
}
2016-10-22 18:06:27 +02:00
virtual ~TrackDesignRepository() final
{
2016-10-22 18:06:27 +02:00
}
size_t GetCount() const override
{
return _items.size();
}
/**
*
* @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
*/
size_t GetCountForObjectEntry(uint8 rideType, const std::string &entry) const override
2016-10-22 18:06:27 +02:00
{
size_t count = 0;
const IObjectRepository * repo = GetObjectRepository();
for (const auto &item : _items)
2016-10-22 18:06:27 +02:00
{
if (item.RideType != rideType)
{
continue;
}
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;
}
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;
}
/**
*
* @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
*/
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;
const IObjectRepository * repo = GetObjectRepository();
for (const auto &item : _items)
2016-10-22 18:06:27 +02:00
{
if (item.RideType != rideType)
{
continue;
}
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;
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);
}
}
*outRefs = Collections::ToArray(refs);
2016-10-22 18:06:27 +02:00
return refs.size();
}
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
{
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
}
bool Delete(const std::string &path) override
2016-10-22 18:06:27 +02:00
{
bool result = false;
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
2016-10-22 18:06:27 +02:00
{
const TrackRepositoryItem * item = &_items[index];
if (!(item->Flags & TRIF_READ_ONLY))
2016-10-22 18:06:27 +02:00
{
if (File::Delete(path))
{
_items.erase(_items.begin() + index);
2016-10-23 01:42:22 +02:00
result = true;
}
2016-10-22 18:06:27 +02:00
}
}
return result;
}
std::string Rename(const std::string &path, const std::string &newName) override
2016-10-22 18:06:27 +02:00
{
std::string result;
2016-10-22 18:06:27 +02:00
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
{
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
{
std::string directory = Path::GetDirectory(path);
std::string newPath = Path::Combine(directory, newName + Path::GetExtension(path));
if (File::Move(path, newPath))
2016-10-22 18:06:27 +02:00
{
item->Name = newName;
item->Path = newPath;
SortItems();
result = newPath;
2016-10-22 18:06:27 +02:00
}
}
}
return result;
}
std::string Install(const std::string &path) override
2016-10-22 18:06:27 +02:00
{
std::string result;
std::string fileName = Path::GetFileName(path);
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
2016-10-22 18:06:27 +02:00
std::string newPath = Path::Combine(installDir, fileName);
if (File::Copy(path, newPath, false))
2016-10-22 18:06:27 +02:00
{
AddTrack(path);
SortItems();
result = path;
2016-10-22 18:06:27 +02:00
}
return result;
}
private:
void Query(const std::string &directory)
2016-10-23 02:12:17 +02:00
{
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
2016-10-23 02:12:17 +02:00
Path::QueryDirectory(&_directoryQueryResult, pattern);
}
void Scan(const std::string &directory, uint32 flags = 0)
2016-10-22 18:06:27 +02: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();
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
}
void AddTrack(const std::string path, uint32 flags = 0)
2016-10-22 18:06:27 +02:00
{
rct_track_td6 * td6 = track_design_open(path.c_str());
2016-10-22 18:06:27 +02:00
if (td6 != nullptr)
{
TrackRepositoryItem item;
item.Name = GetNameFromTrackPath(path);
item.Path = path;
item.RideType = td6->type;
item.ObjectEntry = std::string(td6->vehicle_object.name, 8);
item.Flags = flags;
_items.push_back(item);
2016-10-22 18:06:27 +02:00
track_design_dispose(td6);
}
}
void SortItems()
{
std::sort(_items.begin(), _items.end(), [](const TrackRepositoryItem &a,
const TrackRepositoryItem &b) -> bool
2016-10-22 18:06:27 +02:00
{
if (a.RideType != b.RideType)
2016-10-22 18:06:27 +02:00
{
return a.RideType < b.RideType;
2016-10-22 18:06:27 +02: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()
{
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;
item.Name = fs.ReadStdString();
item.Path = fs.ReadStdString();
2016-10-23 02:12:17 +02:00
item.RideType = fs.ReadValue<uint8>();
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
{
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
for (const auto item : _items)
{
fs.WriteString(item.Name);
fs.WriteString(item.Path);
fs.WriteValue(item.RideType);
fs.WriteString(item.ObjectEntry);
fs.WriteValue(item.Flags);
}
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.");
}
}
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++)
{
if (Path::Equals(_items[i].Path, path))
2016-10-22 18:06:27 +02:00
{
return i;
}
}
return SIZE_MAX;
}
TrackRepositoryItem * GetTrackItem(const std::string &path)
2016-10-22 18:06:27 +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:
static std::string GetNameFromTrackPath(const std::string &path)
2016-10-22 18:06:27 +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
}
};
static std::unique_ptr<TrackDesignRepository> _trackDesignRepository;
2016-10-22 18:06:27 +02:00
ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env)
2016-10-22 18:06:27 +02: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()
{
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)
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->GetCountForObjectEntry(rideType, String::ToStd(entry));
2016-10-22 18:06: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)
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->GetItemsForObjectEntry(outRefs, rideType, String::ToStd(entry));
2016-10-22 18:06: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)
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
2016-10-22 18:06:27 +02:00
return repo->Delete(path);
}
bool track_repository_rename(const utf8 * path, const utf8 * newName)
2016-10-22 18:06:27 +02:00
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
std::string newPath = repo->Rename(path, newName);
return !newPath.empty();
2016-10-22 18:06:27 +02:00
}
bool track_repository_install(const utf8 * srcPath)
2016-10-22 18:06:27 +02:00
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
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)
{
return String::Duplicate(TrackDesignRepository::GetNameFromTrackPath(path));
2016-10-22 18:06:27 +02:00
}
}