Files
OpenRCT2-Web/src/openrct2/ride/TrackDesignRepository.cpp
T

373 lines
11 KiB
C++
Raw Normal View History

2016-10-22 17:06:27 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
2016-10-22 17:06:27 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2016-10-22 17:06:27 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-10-22 17:06:27 +01:00
*****************************************************************************/
2018-06-22 23:14:18 +02:00
#include "TrackDesignRepository.h"
#include "../Context.h"
2018-06-22 23:14:18 +02:00
#include "../PlatformEnvironment.h"
#include "../config/Config.h"
#include "../core/Collections.hpp"
2016-10-22 17:06:27 +01:00
#include "../core/Console.hpp"
2017-01-05 12:55:41 +00:00
#include "../core/File.h"
#include "../core/FileIndex.hpp"
2016-10-22 17:06:27 +01:00
#include "../core/FileStream.hpp"
#include "../core/Path.hpp"
#include "../core/String.hpp"
2018-04-26 23:48:25 +01:00
#include "../localisation/LocalisationService.h"
#include "../object/ObjectRepository.h"
#include "../object/RideObject.h"
#include "../ride/RideData.h"
#include "TrackDesign.h"
2016-10-22 17:06:27 +01:00
2018-06-22 23:14:18 +02:00
#include <algorithm>
#include <memory>
#include <mutex>
2018-06-22 23:14:18 +02:00
#include <vector>
2017-06-11 12:53:37 +01:00
using namespace OpenRCT2;
2016-10-22 18:06:52 +01:00
struct TrackRepositoryItem
{
std::string Name;
std::string Path;
uint8_t RideType = RIDE_TYPE_NULL;
2016-10-22 18:06:52 +01:00
std::string ObjectEntry;
uint32_t Flags = 0;
2016-10-22 18:06:52 +01:00
};
2016-10-22 17:06:27 +01:00
2016-10-22 18:15:48 +01:00
enum TRACK_REPO_ITEM_FLAGS
{
TRIF_READ_ONLY = (1 << 0),
};
2018-06-22 23:14:18 +02:00
std::string GetNameFromTrackPath(const std::string& path)
{
std::string name = Path::GetFileNameWithoutExtension(path);
2018-06-22 23:14:18 +02:00
// The track name should be the file name until the first instance of a dot
2017-12-03 23:45:43 +01:00
name = name.substr(0, name.find_first_of('.'));
return name;
}
static std::mutex _objectLookupMutex;
class TrackDesignFileIndex final : public FileIndex<TrackRepositoryItem>
{
private:
static constexpr uint32_t MAGIC_NUMBER = 0x58444954; // TIDX
static constexpr uint16_t VERSION = 3;
static constexpr auto PATTERN = "*.td4;*.td6";
public:
2018-06-22 23:14:18 +02:00
explicit TrackDesignFileIndex(const IPlatformEnvironment& env)
: FileIndex(
2019-11-04 14:02:30 +01:00
"track design index", MAGIC_NUMBER, VERSION, env.GetFilePath(PATHID::CACHE_TRACKS), std::string(PATTERN),
std::vector<std::string>({
env.GetDirectoryPath(DIRBASE::RCT1, DIRID::TRACK),
env.GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK),
env.GetDirectoryPath(DIRBASE::USER, DIRID::TRACK),
}))
{
}
public:
2018-06-22 23:14:18 +02:00
std::tuple<bool, TrackRepositoryItem> Create(int32_t, const std::string& path) const override
{
auto td6 = track_design_open(path.c_str());
if (td6 != nullptr)
{
ObjectEntryIndex rideType = td6->type;
if (RCT2RideTypeNeedsConversion(td6->type))
{
std::scoped_lock<std::mutex> lock(_objectLookupMutex);
auto* rawObject = object_repository_load_object(&td6->vehicle_object);
const auto* rideEntry = static_cast<const rct_ride_entry*>(
static_cast<RideObject*>(rawObject)->GetLegacyData());
rideType = RCT2RideTypeToOpenRCT2RideType(td6->type, rideEntry);
object_delete(rawObject);
}
TrackRepositoryItem item;
item.Name = GetNameFromTrackPath(path);
item.Path = path;
item.RideType = rideType;
item.ObjectEntry = std::string(td6->vehicle_object.name, 8);
item.Flags = 0;
if (IsTrackReadOnly(path))
{
item.Flags |= TRIF_READ_ONLY;
}
return std::make_tuple(true, item);
}
else
{
return std::make_tuple(true, TrackRepositoryItem());
}
}
protected:
2018-06-22 23:14:18 +02:00
void Serialise(IStream* stream, const TrackRepositoryItem& item) const override
{
2017-08-30 21:58:43 +01:00
stream->WriteString(item.Name);
stream->WriteString(item.Path);
stream->WriteValue(item.RideType);
stream->WriteString(item.ObjectEntry);
stream->WriteValue(item.Flags);
}
2018-06-22 23:14:18 +02:00
TrackRepositoryItem Deserialise(IStream* stream) const override
{
2017-08-30 21:58:43 +01:00
TrackRepositoryItem item;
item.Name = stream->ReadStdString();
item.Path = stream->ReadStdString();
item.RideType = stream->ReadValue<uint8_t>();
2017-08-30 21:58:43 +01:00
item.ObjectEntry = stream->ReadStdString();
item.Flags = stream->ReadValue<uint32_t>();
2017-08-30 21:58:43 +01:00
return item;
}
private:
2018-06-22 23:14:18 +02:00
bool IsTrackReadOnly(const std::string& path) const
{
2018-06-22 23:14:18 +02:00
return String::StartsWith(path, SearchPaths[0]) || String::StartsWith(path, SearchPaths[1]);
}
};
class TrackDesignRepository final : public ITrackDesignRepository
2016-10-22 17:06:27 +01:00
{
private:
2018-04-20 19:58:59 +01:00
std::shared_ptr<IPlatformEnvironment> const _env;
TrackDesignFileIndex const _fileIndex;
2016-10-22 18:06:52 +01:00
std::vector<TrackRepositoryItem> _items;
2016-10-22 17:06:27 +01:00
public:
2018-04-27 18:47:57 +01:00
explicit TrackDesignRepository(const std::shared_ptr<IPlatformEnvironment>& env)
2018-06-22 23:14:18 +02:00
: _env(env)
, _fileIndex(*env)
{
Guard::ArgumentNotNull(env);
}
2016-10-22 17:06:27 +01:00
size_t GetCount() const override
{
return _items.size();
}
/**
*
2018-06-22 23:14:18 +02:00
* @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)
*/
2018-06-22 23:14:18 +02:00
size_t GetCountForObjectEntry(uint8_t rideType, const std::string& entry) const override
2016-10-22 17:06:27 +01:00
{
size_t count = 0;
2018-08-29 00:52:10 +02:00
const auto& repo = GetContext()->GetObjectRepository();
2018-06-22 23:14:18 +02:00
for (const auto& item : _items)
2016-10-22 17:06:27 +01:00
{
if (item.RideType != rideType)
{
continue;
}
bool entryIsNotSeparate = false;
if (entry.empty())
{
2018-08-29 00:52:10 +02:00
const ObjectRepositoryItem* ori = repo.FindObject(item.ObjectEntry.c_str());
if (ori == nullptr || !RideTypeDescriptors[rideType].HasFlag(RIDE_TYPE_FLAG_LIST_VEHICLES_SEPARATELY))
entryIsNotSeparate = true;
}
if (entryIsNotSeparate || String::Equals(item.ObjectEntry, entry, true))
2016-10-22 17:06:27 +01:00
{
count++;
}
}
return count;
}
/**
*
2018-06-22 23:14:18 +02:00
* @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)
*/
2018-06-22 23:14:18 +02:00
std::vector<track_design_file_ref> GetItemsForObjectEntry(uint8_t rideType, const std::string& entry) const override
2016-10-22 17:06:27 +01:00
{
std::vector<track_design_file_ref> refs;
2018-08-29 00:52:10 +02:00
const auto& repo = GetContext()->GetObjectRepository();
2018-06-22 23:14:18 +02:00
for (const auto& item : _items)
2016-10-22 17:06:27 +01:00
{
if (item.RideType != rideType)
{
continue;
}
bool entryIsNotSeparate = false;
if (entry.empty())
{
2018-08-29 00:52:10 +02:00
const ObjectRepositoryItem* ori = repo.FindObject(item.ObjectEntry.c_str());
if (ori == nullptr || !RideTypeDescriptors[rideType].HasFlag(RIDE_TYPE_FLAG_LIST_VEHICLES_SEPARATELY))
entryIsNotSeparate = true;
}
if (entryIsNotSeparate || String::Equals(item.ObjectEntry, entry, true))
2016-10-22 17:06:27 +01:00
{
track_design_file_ref ref;
2017-01-05 13:13:22 +00:00
ref.name = String::Duplicate(GetNameFromTrackPath(item.Path));
ref.path = String::Duplicate(item.Path);
2016-10-22 17:06:27 +01:00
refs.push_back(ref);
}
}
2018-01-19 00:32:53 +00:00
return refs;
2016-10-22 17:06:27 +01:00
}
void Scan(int32_t language) override
2016-10-22 17:06:27 +01:00
{
2016-10-23 01:12:17 +01:00
_items.clear();
2018-04-26 23:48:25 +01:00
auto trackDesigns = _fileIndex.LoadOrBuild(language);
2018-06-22 23:14:18 +02:00
for (const auto& td : trackDesigns)
2016-10-23 01:12:17 +01:00
{
_items.push_back(td);
2016-10-23 01:12:17 +01:00
}
SortItems();
2016-10-22 17:06:27 +01:00
}
2018-06-22 23:14:18 +02:00
bool Delete(const std::string& path) override
2016-10-22 17:06:27 +01:00
{
bool result = false;
2016-10-22 18:15:48 +01:00
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
2016-10-22 17:06:27 +01:00
{
2018-06-22 23:14:18 +02:00
const TrackRepositoryItem* item = &_items[index];
2016-10-22 18:15:48 +01:00
if (!(item->Flags & TRIF_READ_ONLY))
2016-10-22 17:06:27 +01:00
{
2017-01-05 18:13:48 +00:00
if (File::Delete(path))
2016-10-22 18:15:48 +01:00
{
_items.erase(_items.begin() + index);
2016-10-23 00:42:22 +01:00
result = true;
2016-10-22 18:15:48 +01:00
}
2016-10-22 17:06:27 +01:00
}
}
return result;
}
2018-06-22 23:14:18 +02:00
std::string Rename(const std::string& path, const std::string& newName) override
2016-10-22 17:06:27 +01:00
{
std::string result;
2016-10-22 17:06:27 +01:00
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
{
2018-06-22 23:14:18 +02:00
TrackRepositoryItem* item = &_items[index];
2016-10-22 22:35:12 +01:00
if (!(item->Flags & TRIF_READ_ONLY))
2016-10-22 17:06:27 +01:00
{
std::string directory = Path::GetDirectory(path);
std::string newPath = Path::Combine(directory, newName + Path::GetExtension(path));
2017-01-05 18:13:48 +00:00
if (File::Move(path, newPath))
2016-10-22 17:06:27 +01:00
{
item->Name = newName;
item->Path = newPath;
2016-10-22 18:15:48 +01:00
SortItems();
result = newPath;
2016-10-22 17:06:27 +01:00
}
}
}
return result;
}
2018-06-22 23:14:18 +02:00
std::string Install(const std::string& path) override
2016-10-22 17:06:27 +01:00
{
std::string result;
std::string fileName = Path::GetFileName(path);
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
2016-10-22 17:06:27 +01:00
std::string newPath = Path::Combine(installDir, fileName);
2017-01-05 18:13:48 +00:00
if (File::Copy(path, newPath, false))
2016-10-22 17:06:27 +01:00
{
2018-04-27 18:23:39 +01:00
auto language = LocalisationService_GetCurrentLanguage();
auto td = _fileIndex.Create(language, path);
if (std::get<0>(td))
{
_items.push_back(std::get<1>(td));
SortItems();
result = path;
}
2016-10-22 17:06:27 +01:00
}
return result;
}
private:
void SortItems()
{
2018-06-22 23:14:18 +02:00
std::sort(_items.begin(), _items.end(), [](const TrackRepositoryItem& a, const TrackRepositoryItem& b) -> bool {
if (a.RideType != b.RideType)
2016-10-22 17:06:27 +01:00
{
2018-06-22 23:14:18 +02:00
return a.RideType < b.RideType;
}
return String::Compare(a.Name, b.Name) < 0;
});
2016-10-22 17:06:27 +01:00
}
2018-06-22 23:14:18 +02:00
size_t GetTrackIndex(const std::string& path) const
2016-10-22 17:06:27 +01:00
{
for (size_t i = 0; i < _items.size(); i++)
{
if (Path::Equals(_items[i].Path, path))
2016-10-22 17:06:27 +01:00
{
return i;
}
}
return SIZE_MAX;
}
2018-06-22 23:14:18 +02:00
TrackRepositoryItem* GetTrackItem(const std::string& path)
2016-10-22 17:06:27 +01:00
{
2018-06-22 23:14:18 +02:00
TrackRepositoryItem* result = nullptr;
2016-10-22 17:06:27 +01:00
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
{
result = &_items[index];
}
return result;
}
};
std::unique_ptr<ITrackDesignRepository> CreateTrackDesignRepository(const std::shared_ptr<IPlatformEnvironment>& env)
2016-10-22 17:06:27 +01:00
{
return std::make_unique<TrackDesignRepository>(env);
2016-10-22 17:06:27 +01:00
}
2018-02-01 18:49:14 +01:00
void track_repository_scan()
2016-10-22 17:06:27 +01:00
{
2018-06-22 23:14:18 +02:00
ITrackDesignRepository* repo = GetContext()->GetTrackDesignRepository();
2018-04-26 23:48:25 +01:00
repo->Scan(LocalisationService_GetCurrentLanguage());
2016-10-22 17:06:27 +01:00
}
2018-02-01 18:49:14 +01:00
2018-06-22 23:14:18 +02:00
bool track_repository_delete(const utf8* path)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:14:18 +02:00
ITrackDesignRepository* repo = GetContext()->GetTrackDesignRepository();
2018-02-01 18:49:14 +01:00
return repo->Delete(path);
}
2018-06-22 23:14:18 +02:00
bool track_repository_rename(const utf8* path, const utf8* newName)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:14:18 +02:00
ITrackDesignRepository* repo = GetContext()->GetTrackDesignRepository();
2018-02-01 18:49:14 +01:00
std::string newPath = repo->Rename(path, newName);
return !newPath.empty();
}
2018-06-22 23:14:18 +02:00
bool track_repository_install(const utf8* srcPath)
2018-02-01 18:49:14 +01:00
{
2018-06-22 23:14:18 +02:00
ITrackDesignRepository* repo = GetContext()->GetTrackDesignRepository();
2018-02-01 18:49:14 +01:00
std::string newPath = repo->Install(srcPath);
return !newPath.empty();
}