1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-25 07:44:38 +01:00

Refactor more of TrackDesignRepository to std::string

This commit is contained in:
Ted John
2017-01-05 17:51:31 +00:00
parent be035dcdb2
commit 11477c5869
8 changed files with 101 additions and 75 deletions

View File

@@ -17,6 +17,7 @@
#include <algorithm>
#include <memory>
#include <vector>
#include "../core/Collections.hpp"
#include "../core/Console.hpp"
#include "../core/File.h"
#include "../core/FileScanner.h"
@@ -65,6 +66,8 @@ enum TRACK_REPO_ITEM_FLAGS
class TrackDesignRepository : public ITrackDesignRepository
{
private:
static constexpr const utf8 * TD_FILE_PATTERN = "*.td4;*.td6";
IPlatformEnvironment * _env;
std::vector<TrackRepositoryItem> _items;
@@ -87,13 +90,13 @@ public:
return _items.size();
}
size_t GetCountForObjectEntry(uint8 rideType, const utf8 * entry) const override
size_t GetCountForObjectEntry(uint8 rideType, const std::string &entry) const override
{
size_t count = 0;
for (const auto item : _items)
{
if (item.RideType == rideType &&
(entry == nullptr || String::Equals(item.ObjectEntry.c_str(), entry, true)))
(entry.empty() || String::Equals(item.ObjectEntry, entry, true)))
{
count++;
}
@@ -101,13 +104,13 @@ public:
return count;
}
size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs, uint8 rideType, const utf8 * entry) const override
size_t GetItemsForObjectEntry(track_design_file_ref * * outRefs, uint8 rideType, const std::string &entry) const override
{
std::vector<track_design_file_ref> refs;
for (const auto item : _items)
{
if (item.RideType == rideType &&
(entry == nullptr || String::Equals(item.ObjectEntry.c_str(), entry, true)))
(entry.empty() || String::Equals(item.ObjectEntry, entry, true)))
{
track_design_file_ref ref;
ref.name = String::Duplicate(GetNameFromTrackPath(item.Path));
@@ -116,11 +119,7 @@ public:
}
}
*outRefs = nullptr;
if (refs.size() != 0)
{
*outRefs = Memory::DuplicateArray(refs.data(), refs.size());
}
*outRefs = Collections::ToArray(refs);
return refs.size();
}
@@ -143,7 +142,7 @@ public:
}
}
bool Delete(const utf8 * path) override
bool Delete(const std::string &path) override
{
bool result = false;
size_t index = GetTrackIndex(path);
@@ -152,7 +151,7 @@ public:
const TrackRepositoryItem * item = &_items[index];
if (!(item->Flags & TRIF_READ_ONLY))
{
if (File::Delete(path))
if (File::Delete(path.c_str()))
{
_items.erase(_items.begin() + index);
result = true;
@@ -162,57 +161,41 @@ public:
return result;
}
const utf8 * Rename(const utf8 * path, const utf8 * newName) override
std::string Rename(const std::string &path, const std::string &newName) override
{
const utf8 * result = nullptr;
std::string result;
size_t index = GetTrackIndex(path);
if (index != SIZE_MAX)
{
TrackRepositoryItem * item = &_items[index];
if (!(item->Flags & TRIF_READ_ONLY))
{
utf8 newPath[MAX_PATH];
Path::GetDirectory(newPath, sizeof(newPath), path);
Path::Append(newPath, sizeof(newPath), newName);
String::Append(newPath, sizeof(newPath), Path::GetExtension(path));
if (File::Move(path, newPath))
std::string directory = Path::GetDirectory(path);
std::string newPath = Path::Combine(directory, newName + Path::GetExtension(path));
if (File::Move(path.c_str(), newPath.c_str()))
{
item->Name = std::string(newName);
item->Path = std::string(newPath);
item->Name = newName;
item->Path = newPath;
SortItems();
item = GetTrackItem(newPath);
if (item != nullptr)
{
result = item->Path.c_str();
}
result = newPath;
}
}
}
return result;
}
const utf8 * Install(const utf8 * path) override
std::string Install(const std::string &path) override
{
const utf8 * result = nullptr;
const utf8 * fileName = Path::GetFileName(path);
std::string result;
std::string fileName = Path::GetFileName(path);
std::string installDir = _env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK);
utf8 newPath[MAX_PATH];
String::Set(newPath, sizeof(newPath), installDir.c_str());
Path::Append(newPath, sizeof(newPath), fileName);
if (File::Copy(path, newPath, false))
std::string newPath = Path::Combine(installDir, fileName);
if (File::Copy(path.c_str(), newPath.c_str(), false))
{
AddTrack(path);
SortItems();
const TrackRepositoryItem * item = GetTrackItem(path);
if (item != nullptr)
{
result = item->Path.c_str();
}
result = path;
}
return result;
}
@@ -220,15 +203,13 @@ public:
private:
void Query(const std::string &directory)
{
utf8 pattern[MAX_PATH];
String::Set(pattern, sizeof(pattern), directory.c_str());
Path::Append(pattern, sizeof(pattern), "*.td4;*.td6");
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
Path::QueryDirectory(&_directoryQueryResult, pattern);
}
void Scan(const std::string &directory, uint32 flags = 0)
{
std::string pattern = Path::Append(directory, "*.td4;*.td6");
std::string pattern = Path::Combine(directory, TD_FILE_PATTERN);
IFileScanner * scanner = Path::ScanDirectory(pattern, true);
while (scanner->Next())
{
@@ -339,11 +320,11 @@ private:
}
}
size_t GetTrackIndex(const utf8 * path) const
size_t GetTrackIndex(const std::string &path) const
{
for (size_t i = 0; i < _items.size(); i++)
{
if (Path::Equals(_items[i].Path.c_str(), path))
if (Path::Equals(_items[i].Path, path))
{
return i;
}
@@ -351,7 +332,7 @@ private:
return SIZE_MAX;
}
TrackRepositoryItem * GetTrackItem(const utf8 * path)
TrackRepositoryItem * GetTrackItem(const std::string &path)
{
TrackRepositoryItem * result = nullptr;
size_t index = GetTrackIndex(path);
@@ -408,16 +389,18 @@ extern "C"
return repo->Delete(path);
}
const utf8 * track_repository_rename(const utf8 * path, const utf8 * newName)
bool track_repository_rename(const utf8 * path, const utf8 * newName)
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->Rename(path, newName);
std::string newPath = repo->Rename(path, newName);
return !newPath.empty();
}
const utf8 * track_repository_install(const utf8 * srcPath)
bool track_repository_install(const utf8 * srcPath)
{
ITrackDesignRepository * repo = GetTrackDesignRepository();
return repo->Install(srcPath);
std::string newPath = repo->Install(srcPath);
return !newPath.empty();
}
utf8 * track_repository_get_name_from_path(const utf8 * path)