2016-11-12 18:12:11 +01:00
|
|
|
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
|
|
|
/*****************************************************************************
|
|
|
|
* 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
|
|
|
|
|
2016-11-12 19:38:30 +01:00
|
|
|
#include <algorithm>
|
2016-11-12 18:12:11 +01:00
|
|
|
#include <vector>
|
2016-11-29 14:22:11 +01:00
|
|
|
#include "../core/Collections.hpp"
|
2016-11-12 18:12:11 +01:00
|
|
|
#include "../core/FileScanner.h"
|
|
|
|
#include "../core/Memory.hpp"
|
|
|
|
#include "../core/Path.hpp"
|
|
|
|
#include "../core/String.hpp"
|
2016-11-12 19:38:30 +01:00
|
|
|
#include "../core/Util.hpp"
|
2016-11-27 17:16:17 +01:00
|
|
|
#include "TitleSequence.h"
|
2016-11-12 18:12:11 +01:00
|
|
|
#include "TitleSequenceManager.h"
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "../localisation/localisation.h"
|
|
|
|
#include "../openrct2.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace TitleSequenceManager
|
|
|
|
{
|
|
|
|
struct PredefinedSequence
|
|
|
|
{
|
|
|
|
const utf8 * ConfigId;
|
|
|
|
const utf8 * Filename;
|
|
|
|
rct_string_id StringId;
|
|
|
|
};
|
|
|
|
|
|
|
|
const PredefinedSequence PredefinedSequences[] =
|
|
|
|
{
|
|
|
|
{ "*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 },
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<TitleSequenceManagerItem> _items;
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
static size_t FindItemIndexByPath(const utf8 * path);
|
2016-11-12 19:53:01 +01:00
|
|
|
static void Scan(const utf8 * directory);
|
2016-11-29 14:22:11 +01:00
|
|
|
static void AddSequence(const utf8 * scanPath);
|
|
|
|
static void SortSequences();
|
2016-11-13 14:15:01 +01:00
|
|
|
static std::string GetNameFromSequencePath(const std::string &path);
|
2016-11-12 19:53:01 +01:00
|
|
|
static void GetDataSequencesPath(utf8 * buffer, size_t bufferSize);
|
|
|
|
static void GetUserSequencesPath(utf8 * buffer, size_t bufferSize);
|
2016-11-12 18:12:11 +01:00
|
|
|
|
|
|
|
size_t GetCount()
|
|
|
|
{
|
|
|
|
return _items.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TitleSequenceManagerItem * GetItem(size_t i)
|
|
|
|
{
|
|
|
|
return &_items[i];
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
static size_t FindItemIndexByPath(const utf8 * path)
|
|
|
|
{
|
|
|
|
size_t index = Collections::IndexOf(_items, [path](const TitleSequenceManagerItem &item) -> bool
|
|
|
|
{
|
|
|
|
return String::Equals(path, item.Path.c_str());
|
|
|
|
});
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:16:17 +01:00
|
|
|
void DeleteItem(size_t i)
|
|
|
|
{
|
|
|
|
auto item = GetItem(i);
|
|
|
|
const utf8 * path = item->Path.c_str();
|
|
|
|
if (item->IsZip)
|
|
|
|
{
|
|
|
|
platform_file_delete(path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_directory_delete(path);
|
|
|
|
}
|
|
|
|
_items.erase(_items.begin() + i);
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
size_t RenameItem(size_t i, const utf8 * newName)
|
2016-11-27 17:16:17 +01:00
|
|
|
{
|
|
|
|
auto item = &_items[i];
|
|
|
|
const utf8 * oldPath = item->Path.c_str();
|
|
|
|
|
|
|
|
utf8 newPath[MAX_PATH];
|
|
|
|
Path::GetDirectory(newPath, sizeof(newPath), oldPath);
|
|
|
|
Path::Append(newPath, sizeof(newPath), newName);
|
|
|
|
if (item->IsZip)
|
|
|
|
{
|
|
|
|
String::Append(newPath, sizeof(newPath), TITLE_SEQUENCE_EXTENSION);
|
|
|
|
platform_file_move(oldPath, newPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
platform_file_move(oldPath, newPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
item->Name = std::string(newName);
|
|
|
|
item->Path = std::string(newPath);
|
2016-11-29 14:22:11 +01:00
|
|
|
|
|
|
|
SortSequences();
|
|
|
|
size_t index = FindItemIndexByPath(newPath);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t DuplicateItem(size_t i, const utf8 * name)
|
|
|
|
{
|
|
|
|
auto item = &_items[i];
|
|
|
|
const utf8 * srcPath = item->Path.c_str();
|
|
|
|
|
|
|
|
utf8 dstPath[MAX_PATH];
|
|
|
|
Path::GetDirectory(dstPath, sizeof(dstPath), srcPath);
|
|
|
|
Path::Append(dstPath, sizeof(dstPath), name);
|
|
|
|
if (item->IsZip)
|
|
|
|
{
|
|
|
|
String::Append(dstPath, sizeof(dstPath), TITLE_SEQUENCE_EXTENSION);
|
|
|
|
}
|
|
|
|
if (!platform_file_copy(srcPath, dstPath, true))
|
|
|
|
{
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddSequence(dstPath);
|
|
|
|
SortSequences();
|
|
|
|
size_t index = FindItemIndexByPath(dstPath);
|
|
|
|
return index;
|
2016-11-27 17:16:17 +01:00
|
|
|
}
|
|
|
|
|
2016-11-13 14:15:01 +01:00
|
|
|
static const uint16 GetPredefinedIndex(const std::string &path)
|
2016-11-12 19:38:30 +01:00
|
|
|
{
|
2016-11-13 14:15:01 +01:00
|
|
|
const utf8 * filename = Path::GetFileName(path.c_str());
|
2016-11-12 19:38:30 +01:00
|
|
|
for (uint16 i = 0; i < Util::CountOf(PredefinedSequences); i++)
|
|
|
|
{
|
|
|
|
if (String::Equals(filename, PredefinedSequences[i].Filename, true))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PREDEFINED_INDEX_CUSTOM;
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
static void SortSequences()
|
2016-11-12 18:12:11 +01:00
|
|
|
{
|
2016-11-12 19:53:01 +01:00
|
|
|
// Sort sequences by predefined index and then name
|
|
|
|
std::sort(_items.begin(), _items.end(), [](const TitleSequenceManagerItem &a,
|
|
|
|
const TitleSequenceManagerItem &b) -> bool
|
|
|
|
{
|
|
|
|
if (a.PredefinedIndex < b.PredefinedIndex)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (a.PredefinedIndex > b.PredefinedIndex)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return _strcmpi(a.Name.c_str(), b.Name.c_str()) < 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
static void Scan()
|
|
|
|
{
|
|
|
|
utf8 path[MAX_PATH];
|
|
|
|
|
|
|
|
_items.clear();
|
|
|
|
|
|
|
|
// Scan data path
|
|
|
|
GetDataSequencesPath(path, sizeof(path));
|
|
|
|
Scan(path);
|
|
|
|
|
|
|
|
// Scan user path
|
|
|
|
GetUserSequencesPath(path, sizeof(path));
|
|
|
|
Scan(path);
|
|
|
|
|
|
|
|
SortSequences();
|
|
|
|
}
|
|
|
|
|
2016-11-12 19:53:01 +01:00
|
|
|
static void Scan(const utf8 * directory)
|
|
|
|
{
|
|
|
|
utf8 pattern[MAX_PATH];
|
|
|
|
String::Set(pattern, sizeof(pattern), directory);
|
2016-11-13 14:15:01 +01:00
|
|
|
Path::Append(pattern, sizeof(pattern), "script.txt;*.parkseq");
|
2016-11-12 19:53:01 +01:00
|
|
|
|
|
|
|
IFileScanner * fileScanner = Path::ScanDirectory(pattern, true);
|
2016-11-12 18:12:11 +01:00
|
|
|
while (fileScanner->Next())
|
|
|
|
{
|
2016-11-29 14:22:11 +01:00
|
|
|
const utf8 * path = fileScanner->GetPath();
|
|
|
|
AddSequence(path);
|
2016-11-12 18:12:11 +01:00
|
|
|
}
|
|
|
|
delete fileScanner;
|
2016-11-12 19:38:30 +01:00
|
|
|
}
|
2016-11-12 18:12:11 +01:00
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
static void AddSequence(const utf8 * scanPath)
|
|
|
|
{
|
|
|
|
std::string path;
|
|
|
|
bool isZip = true;
|
|
|
|
if (String::Equals(Path::GetExtension(scanPath), ".txt", true))
|
|
|
|
{
|
|
|
|
// If we are given a .txt file, set the path to the containing directory
|
|
|
|
path = std::string(Path::GetDirectory(scanPath));
|
|
|
|
isZip = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
path = std::string(scanPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
TitleSequenceManagerItem item;
|
|
|
|
item.PredefinedIndex = GetPredefinedIndex(path);
|
|
|
|
item.Path = path;
|
|
|
|
if (item.PredefinedIndex != PREDEFINED_INDEX_CUSTOM)
|
|
|
|
{
|
|
|
|
rct_string_id stringId = PredefinedSequences[item.PredefinedIndex].StringId;
|
|
|
|
item.Name = String::Duplicate(language_get_string(stringId));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
item.Name = GetNameFromSequencePath(path);
|
|
|
|
}
|
|
|
|
item.IsZip = isZip;
|
|
|
|
_items.push_back(item);
|
|
|
|
}
|
|
|
|
|
2016-11-13 14:15:01 +01:00
|
|
|
static std::string GetNameFromSequencePath(const std::string &path)
|
2016-11-12 19:38:30 +01:00
|
|
|
{
|
2016-11-13 14:15:01 +01:00
|
|
|
utf8 * name = Path::GetFileNameWithoutExtension(path.c_str());
|
2016-11-12 18:12:11 +01:00
|
|
|
std::string result = std::string(name);
|
|
|
|
Memory::Free(name);
|
|
|
|
return result;
|
|
|
|
}
|
2016-11-12 19:53:01 +01:00
|
|
|
|
|
|
|
static void GetDataSequencesPath(utf8 * buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
platform_get_openrct_data_path(buffer, bufferSize);
|
|
|
|
Path::Append(buffer, bufferSize, "title");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void GetUserSequencesPath(utf8 * buffer, size_t bufferSize)
|
|
|
|
{
|
|
|
|
platform_get_user_directory(buffer, "title sequences", bufferSize);
|
|
|
|
}
|
2016-11-12 18:12:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
size_t title_sequence_manager_get_count()
|
|
|
|
{
|
|
|
|
return TitleSequenceManager::GetCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
const utf8 * title_sequence_manager_get_name(size_t index)
|
|
|
|
{
|
|
|
|
auto item = TitleSequenceManager::GetItem(index);
|
|
|
|
const utf8 * name = item->Name.c_str();
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const utf8 * title_sequence_manager_get_path(size_t index)
|
|
|
|
{
|
|
|
|
auto item = TitleSequenceManager::GetItem(index);
|
|
|
|
const utf8 * name = item->Path.c_str();
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const utf8 * title_sequence_manager_get_config_id(size_t index)
|
|
|
|
{
|
|
|
|
auto item = TitleSequenceManager::GetItem(index);
|
|
|
|
const utf8 * name = item->Name.c_str();
|
|
|
|
const utf8 * filename = Path::GetFileName(item->Path.c_str());
|
|
|
|
for (const auto &pseq : TitleSequenceManager::PredefinedSequences)
|
|
|
|
{
|
|
|
|
if (String::Equals(filename, pseq.Filename, true))
|
|
|
|
{
|
|
|
|
return pseq.ConfigId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2016-11-27 15:47:02 +01:00
|
|
|
uint16 title_sequence_manager_get_predefined_index(size_t index)
|
|
|
|
{
|
|
|
|
auto item = TitleSequenceManager::GetItem(index);
|
|
|
|
uint16 predefinedIndex = item->PredefinedIndex;
|
|
|
|
return predefinedIndex;
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:12:11 +01:00
|
|
|
size_t title_sequence_manager_get_index_for_config_id(const utf8 * configId)
|
|
|
|
{
|
|
|
|
size_t count = TitleSequenceManager::GetCount();
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
const utf8 * cid = title_sequence_manager_get_config_id(i);
|
|
|
|
if (String::Equals(cid, configId))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:16:17 +01:00
|
|
|
size_t title_sequence_manager_get_index_for_name(const utf8 * name)
|
|
|
|
{
|
|
|
|
size_t count = TitleSequenceManager::GetCount();
|
|
|
|
for (size_t i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
const utf8 * tn = title_sequence_manager_get_name(i);
|
|
|
|
if (String::Equals(tn, name))
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SIZE_MAX;
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:12:11 +01:00
|
|
|
void title_sequence_manager_scan()
|
|
|
|
{
|
|
|
|
TitleSequenceManager::Scan();
|
|
|
|
}
|
2016-11-27 17:16:17 +01:00
|
|
|
|
|
|
|
void title_sequence_manager_delete(size_t i)
|
|
|
|
{
|
|
|
|
TitleSequenceManager::DeleteItem(i);
|
|
|
|
}
|
|
|
|
|
2016-11-29 14:22:11 +01:00
|
|
|
size_t title_sequence_manager_rename(size_t i, const utf8 * name)
|
|
|
|
{
|
|
|
|
return TitleSequenceManager::RenameItem(i, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t title_sequence_manager_duplicate(size_t i, const utf8 * name)
|
2016-11-27 17:16:17 +01:00
|
|
|
{
|
2016-11-29 14:22:11 +01:00
|
|
|
return TitleSequenceManager::DuplicateItem(i, name);
|
2016-11-27 17:16:17 +01:00
|
|
|
}
|
2016-11-12 18:12:11 +01:00
|
|
|
}
|