OpenRCT2/src/openrct2/scenes/title/TitleSequenceManager.cpp

389 lines
10 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
2018-06-22 23:15:14 +02:00
#include "TitleSequenceManager.h"
#include "../../Context.h"
#include "../../OpenRCT2.h"
#include "../../PlatformEnvironment.h"
#include "../../core/Collections.hpp"
#include "../../core/File.h"
#include "../../core/FileScanner.h"
#include "../../core/Memory.hpp"
#include "../../core/Path.hpp"
#include "../../core/String.hpp"
#include "../../localisation/Localisation.h"
#include "../../platform/Platform.h"
2018-06-22 23:15:14 +02:00
#include "TitleSequence.h"
#include <algorithm>
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:15:14 +02:00
#include <vector>
namespace TitleSequenceManager
{
struct PredefinedSequence
{
2018-06-22 23:15:14 +02:00
const utf8* ConfigId;
const utf8* Filename;
2022-07-31 15:04:08 +02:00
::StringId StringId;
};
static constexpr PredefinedSequence PredefinedSequences[] = {
2018-06-22 23:15:14 +02:00
{ "*RCT1", "rct1.parkseq", STR_TITLE_SEQUENCE_RCT1 },
{ "*RCT1AA", "rct1aa.parkseq", STR_TITLE_SEQUENCE_RCT1_AA },
{ "*RCT1AALL", "rct1aall.parkseq", STR_TITLE_SEQUENCE_RCT1_AA_LL },
{ "*RCT2", "rct2.parkseq", STR_TITLE_SEQUENCE_RCT2 },
{ "*OPENRCT2", "openrct2.parkseq", STR_TITLE_SEQUENCE_OPENRCT2 },
};
static std::vector<TitleSequenceManagerItem> _items;
2018-06-22 23:15:14 +02:00
static std::string GetNewTitleSequencePath(const std::string& name, bool isZip);
static size_t FindItemIndexByPath(const std::string& path);
static void Scan(const std::string& directory);
static void AddSequence(const std::string& scanPath);
2016-11-29 14:22:11 +01:00
static void SortSequences();
2018-06-22 23:15:14 +02:00
static std::string GetNameFromSequencePath(const std::string& path);
static std::string GetDataSequencesPath();
static std::string GetUserSequencesPath();
2018-06-22 23:15:14 +02:00
static bool IsNameReserved(const std::string& name);
size_t GetCount()
{
return _items.size();
}
2018-06-22 23:15:14 +02:00
const TitleSequenceManagerItem* GetItem(size_t i)
{
if (i >= _items.size())
{
return nullptr;
}
return &_items[i];
}
static size_t FindItemIndexByPath(const std::string& path)
2016-11-29 14:22:11 +01:00
{
2018-06-22 23:15:14 +02:00
size_t index = Collections::IndexOf(
_items, [path](const TitleSequenceManagerItem& item) -> bool { return path == item.Path; });
2016-11-29 14:22:11 +01:00
return index;
}
2016-11-27 17:16:17 +01:00
void DeleteItem(size_t i)
{
auto item = GetItem(i);
if (item == nullptr)
{
return;
}
2018-06-22 23:15:14 +02:00
const utf8* path = item->Path.c_str();
2016-11-27 17:16:17 +01:00
if (item->IsZip)
{
2022-01-08 13:57:29 +01:00
File::Delete(path);
2016-11-27 17:16:17 +01:00
}
else
{
Path::DeleteDirectory(path);
2016-11-27 17:16:17 +01:00
}
_items.erase(_items.begin() + i);
}
2018-06-22 23:15:14 +02:00
size_t RenameItem(size_t i, const utf8* newName)
2016-11-27 17:16:17 +01:00
{
auto item = &_items[i];
const auto& oldPath = item->Path;
2016-11-27 17:16:17 +01:00
auto newPath = Path::Combine(Path::GetDirectory(oldPath), newName);
2016-11-27 17:16:17 +01:00
if (item->IsZip)
{
newPath += OpenRCT2::Title::TITLE_SEQUENCE_EXTENSION;
2022-01-08 13:57:29 +01:00
File::Move(oldPath, newPath);
2016-11-27 17:16:17 +01:00
}
else
{
2022-01-08 13:57:29 +01:00
File::Move(oldPath, newPath);
2016-11-27 17:16:17 +01:00
}
item->Name = newName;
item->Path = newPath;
2016-11-29 14:22:11 +01:00
SortSequences();
size_t index = FindItemIndexByPath(newPath);
2016-11-29 14:22:11 +01:00
return index;
}
2018-06-22 23:15:14 +02:00
size_t DuplicateItem(size_t i, const utf8* name)
2016-11-29 14:22:11 +01:00
{
auto item = &_items[i];
const auto& srcPath = item->Path;
2016-11-29 14:22:11 +01:00
std::string dstPath = GetNewTitleSequencePath(std::string(name), item->IsZip);
2022-01-08 13:57:29 +01:00
if (!File::Copy(srcPath, dstPath, true))
2016-11-29 14:22:11 +01:00
{
return SIZE_MAX;
}
AddSequence(dstPath);
2016-11-29 14:22:11 +01:00
SortSequences();
size_t index = FindItemIndexByPath(dstPath);
2016-11-29 14:22:11 +01:00
return index;
2016-11-27 17:16:17 +01:00
}
2018-06-22 23:15:14 +02:00
size_t CreateItem(const utf8* name)
2016-11-29 14:42:24 +01:00
{
auto seq = OpenRCT2::Title::CreateTitleSequence();
seq->Name = name;
seq->Path = GetNewTitleSequencePath(seq->Name, true);
2016-11-29 14:42:24 +01:00
seq->IsZip = true;
size_t index = SIZE_MAX;
if (TitleSequenceSave(*seq))
2016-11-29 14:42:24 +01:00
{
AddSequence(seq->Path);
2016-11-29 14:42:24 +01:00
SortSequences();
index = FindItemIndexByPath(seq->Path);
2016-11-29 14:42:24 +01:00
}
2016-11-29 18:54:57 +01:00
return index;
2016-11-29 14:42:24 +01:00
}
2018-06-22 23:15:14 +02:00
static std::string GetNewTitleSequencePath(const std::string& name, bool isZip)
{
auto path = Path::Combine(GetUserSequencesPath(), name);
if (isZip)
{
path += OpenRCT2::Title::TITLE_SEQUENCE_EXTENSION;
}
return path;
}
2018-06-22 23:15:14 +02:00
static size_t GetPredefinedIndex(const std::string& path)
2016-11-12 19:38:30 +01:00
{
auto filename = Path::GetFileName(path);
2018-11-21 23:16:04 +01:00
for (size_t i = 0; i < std::size(PredefinedSequences); i++)
2016-11-12 19:38:30 +01:00
{
if (String::IEquals(filename, PredefinedSequences[i].Filename))
2016-11-12 19:38:30 +01:00
{
return i;
}
}
return PREDEFINED_INDEX_CUSTOM;
}
2016-11-29 14:22:11 +01:00
static void SortSequences()
{
2016-11-12 19:53:01 +01:00
// Sort sequences by predefined index and then name
2018-06-22 23:15:14 +02:00
std::sort(
_items.begin(), _items.end(), [](const TitleSequenceManagerItem& a, const TitleSequenceManagerItem& b) -> bool {
2022-07-03 16:20:23 +02:00
if (a.PredefinedIndex != b.PredefinedIndex)
2018-06-22 23:15:14 +02:00
{
2022-07-03 16:20:23 +02:00
return a.PredefinedIndex < b.PredefinedIndex;
2018-06-22 23:15:14 +02:00
}
return String::Compare(a.Name, b.Name, true) < 0;
2018-06-22 23:15:14 +02:00
});
2016-11-12 19:53:01 +01:00
}
2016-12-14 13:07:48 +01:00
void Scan()
2016-11-29 14:22:11 +01:00
{
_items.clear();
// Scan data path
Scan(GetDataSequencesPath());
2016-11-29 14:22:11 +01:00
// Scan user path
Scan(GetUserSequencesPath());
2016-11-29 14:22:11 +01:00
SortSequences();
}
static void Scan(const std::string& directory)
2016-11-12 19:53:01 +01:00
{
auto pattern = Path::Combine(directory, u8"script.txt;*.parkseq");
auto fileScanner = Path::ScanDirectory(pattern, true);
while (fileScanner->Next())
{
AddSequence(fileScanner->GetPath());
}
2016-11-12 19:38:30 +01:00
}
static void AddSequence(const std::string& scanPath)
2016-11-29 14:22:11 +01:00
{
TitleSequenceManagerItem item{};
if (String::IEquals(Path::GetExtension(scanPath), u8".txt"))
2016-11-29 14:22:11 +01:00
{
// If we are given a .txt file, set the path to the containing directory
item.Path = Path::GetDirectory(scanPath);
item.Name = Path::GetFileName(item.Path);
item.IsZip = false;
2016-11-29 14:22:11 +01:00
}
else
{
item.Path = scanPath;
item.Name = GetNameFromSequencePath(item.Path);
item.IsZip = true;
2016-11-29 14:22:11 +01:00
}
item.PredefinedIndex = GetPredefinedIndex(item.Path);
2016-11-29 14:22:11 +01:00
if (item.PredefinedIndex != PREDEFINED_INDEX_CUSTOM)
{
2022-07-31 14:22:58 +02:00
StringId stringId = PredefinedSequences[item.PredefinedIndex].StringId;
item.Name = LanguageGetString(stringId);
2016-11-29 14:22:11 +01:00
}
else if (IsNameReserved(item.Name))
{
// Reserved names are not allowed because they map to the
// actual predefined names and also prevent editing
return;
}
_items.push_back(std::move(item));
2016-11-29 14:22:11 +01:00
}
2018-06-22 23:15:14 +02:00
static std::string GetNameFromSequencePath(const std::string& path)
2016-11-12 19:38:30 +01:00
{
auto name = Path::GetFileNameWithoutExtension(path);
return name;
}
2016-11-12 19:53:01 +01:00
static std::string GetDataSequencesPath()
2016-11-12 19:53:01 +01:00
{
auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
return env->GetDirectoryPath(OpenRCT2::DIRBASE::OPENRCT2, OpenRCT2::DIRID::SEQUENCE);
2016-11-12 19:53:01 +01:00
}
static std::string GetUserSequencesPath()
2016-11-12 19:53:01 +01:00
{
auto env = OpenRCT2::GetContext()->GetPlatformEnvironment();
return env->GetDirectoryPath(OpenRCT2::DIRBASE::USER, OpenRCT2::DIRID::SEQUENCE);
2016-11-12 19:53:01 +01:00
}
2018-06-22 23:15:14 +02:00
static bool IsNameReserved(const std::string& name)
{
2018-06-22 23:15:14 +02:00
for (const auto& pseq : TitleSequenceManager::PredefinedSequences)
{
if (String::IEquals(name, pseq.ConfigId))
{
return true;
}
}
return false;
}
2018-05-04 22:40:09 +02:00
} // namespace TitleSequenceManager
size_t TitleSequenceManagerGetCount()
{
2018-02-01 18:49:14 +01:00
return TitleSequenceManager::GetCount();
}
const utf8* TitleSequenceManagerGetName(size_t index)
2018-02-01 18:49:14 +01:00
{
auto item = TitleSequenceManager::GetItem(index);
if (item == nullptr)
{
2018-02-01 18:49:14 +01:00
return nullptr;
}
return item->Name.c_str();
2018-02-01 18:49:14 +01:00
}
const utf8* TitleSequenceManagerGetPath(size_t index)
2018-02-01 18:49:14 +01:00
{
auto item = TitleSequenceManager::GetItem(index);
2018-06-22 23:15:14 +02:00
if (item == nullptr)
{
2018-02-01 18:49:14 +01:00
return nullptr;
}
return item->Path.c_str();
2018-02-01 18:49:14 +01:00
}
const utf8* TitleSequenceManagerGetConfigID(size_t index)
2018-02-01 18:49:14 +01:00
{
auto item = TitleSequenceManager::GetItem(index);
2018-06-22 23:15:14 +02:00
if (item == nullptr)
{
2018-02-01 18:49:14 +01:00
return nullptr;
}
const auto& name = item->Name;
const auto filename = Path::GetFileName(item->Path);
2018-06-22 23:15:14 +02:00
for (const auto& pseq : TitleSequenceManager::PredefinedSequences)
{
if (String::IEquals(filename, pseq.Filename))
{
2018-02-01 18:49:14 +01:00
return pseq.ConfigId;
}
}
return name.c_str();
2018-02-01 18:49:14 +01:00
}
size_t TitleSequenceManagerGetPredefinedIndex(size_t index)
2018-02-01 18:49:14 +01:00
{
auto item = TitleSequenceManager::GetItem(index);
2018-06-22 23:15:14 +02:00
if (item == nullptr)
{
2018-02-01 18:49:14 +01:00
return 0;
2016-11-27 15:47:02 +01:00
}
2018-02-01 18:49:14 +01:00
size_t predefinedIndex = item->PredefinedIndex;
return predefinedIndex;
}
2016-11-27 15:47:02 +01:00
size_t TitleSequenceManagerGetIndexForConfigID(const utf8* configId)
2018-02-01 18:49:14 +01:00
{
size_t count = TitleSequenceManager::GetCount();
for (size_t i = 0; i < count; i++)
{
const utf8* cid = TitleSequenceManagerGetConfigID(i);
2018-02-01 18:49:14 +01:00
if (String::Equals(cid, configId))
{
2018-02-01 18:49:14 +01:00
return i;
}
}
2018-02-01 18:49:14 +01:00
return SIZE_MAX;
}
size_t TitleSequenceManagerGetIndexForName(const utf8* name)
2018-02-01 18:49:14 +01:00
{
size_t count = TitleSequenceManager::GetCount();
for (size_t i = 0; i < count; i++)
2016-11-27 17:16:17 +01:00
{
const utf8* tn = TitleSequenceManagerGetName(i);
2018-02-01 18:49:14 +01:00
if (String::Equals(tn, name))
2016-11-27 17:16:17 +01:00
{
2018-02-01 18:49:14 +01:00
return i;
2016-11-27 17:16:17 +01:00
}
}
2018-02-01 18:49:14 +01:00
return SIZE_MAX;
}
2016-11-27 17:16:17 +01:00
void TitleSequenceManagerScan()
2018-02-01 18:49:14 +01:00
{
TitleSequenceManager::Scan();
}
2016-11-27 17:16:17 +01:00
void TitleSequenceManagerDelete(size_t i)
2018-02-01 18:49:14 +01:00
{
TitleSequenceManager::DeleteItem(i);
}
2016-11-27 17:16:17 +01:00
size_t TitleSequenceManagerRename(size_t i, const utf8* name)
2018-02-01 18:49:14 +01:00
{
return TitleSequenceManager::RenameItem(i, name);
}
2016-11-29 14:22:11 +01:00
size_t TitleSequenceManagerDuplicate(size_t i, const utf8* name)
2018-02-01 18:49:14 +01:00
{
return TitleSequenceManager::DuplicateItem(i, name);
}
2016-11-29 14:42:24 +01:00
size_t TitleSequenceManagerCreate(const utf8* name)
2018-02-01 18:49:14 +01:00
{
return TitleSequenceManager::CreateItem(name);
}