1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 20:43:04 +01:00

Use vector for TitleSequence saves

Co-authored-by: Gabriel Guedes <gabriel.guedesaz@gmail.com>
This commit is contained in:
Matt
2020-09-27 16:10:47 +03:00
parent ccb6eb06f8
commit 7245f23422
5 changed files with 49 additions and 68 deletions

View File

@@ -288,10 +288,10 @@ private:
}
if (!loadSuccess)
{
if (_sequence->NumSaves > saveIndex)
if (_sequence->Saves.size() > saveIndex)
{
const utf8* path = _sequence->Saves[saveIndex];
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", path);
const auto& path = _sequence->Saves[saveIndex];
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", path.c_str());
}
return false;
}

View File

@@ -238,7 +238,7 @@ void window_title_command_editor_open(TitleSequence* sequence, int32_t index, bo
switch (command.Type)
{
case TITLE_SCRIPT_LOAD:
if (command.SaveIndex >= _sequence->NumSaves)
if (command.SaveIndex >= _sequence->Saves.size())
command.SaveIndex = SAVE_INDEX_INVALID;
break;
case TITLE_SCRIPT_LOCATION:
@@ -392,11 +392,11 @@ static void window_title_command_editor_mousedown(rct_window* w, rct_widgetindex
}
else if (command.Type == TITLE_SCRIPT_LOAD)
{
int32_t numItems = static_cast<int32_t>(_sequence->NumSaves);
int32_t numItems = static_cast<int32_t>(_sequence->Saves.size());
for (int32_t i = 0; i < numItems; i++)
{
gDropdownItemsFormat[i] = STR_OPTIONS_DROPDOWN_ITEM;
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(_sequence->Saves[i]);
gDropdownItemsArgs[i] = reinterpret_cast<uintptr_t>(_sequence->Saves[i].c_str());
}
window_dropdown_show_text_custom_width(
@@ -466,7 +466,7 @@ static void window_title_command_editor_dropdown(rct_window* w, rct_widgetindex
break;
case TITLE_SCRIPT_LOAD:
command.SaveIndex = 0;
if (command.SaveIndex >= _sequence->NumSaves)
if (command.SaveIndex >= _sequence->Saves.size())
{
command.SaveIndex = 0xFF;
}
@@ -768,7 +768,7 @@ static void window_title_command_editor_paint(rct_window* w, rct_drawpixelinfo*
else
{
auto ft = Formatter::Common();
ft.Add<utf8*>(_sequence->Saves[command.SaveIndex]);
ft.Add<utf8*>(_sequence->Saves[command.SaveIndex].c_str());
DrawTextEllipsised(
dpi, { w->windowPos.x + w->widgets[WIDX_INPUT].left + 1, w->windowPos.y + w->widgets[WIDX_INPUT].top },
w->widgets[WIDX_INPUT_DROPDOWN].left - w->widgets[WIDX_INPUT].left - 4, STR_STRING, ft, w->colours[1]);

View File

@@ -323,7 +323,7 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
if (w->selected_list_item != -1)
{
TitleSequenceRemovePark(*_editingTitleSequence, w->selected_list_item);
if (w->selected_list_item >= static_cast<int16_t>(_editingTitleSequence->NumSaves))
if (w->selected_list_item >= static_cast<int16_t>(_editingTitleSequence->Saves.size()))
{
w->selected_list_item--;
}
@@ -337,12 +337,12 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
{
window_text_input_open(
w, widgetIndex, STR_FILEBROWSER_RENAME_SAVE_TITLE, STR_TITLE_EDITOR_ENTER_NAME_FOR_SAVE, STR_STRING,
reinterpret_cast<uintptr_t>(_editingTitleSequence->Saves[w->selected_list_item]), 52 - 1);
reinterpret_cast<uintptr_t>(_editingTitleSequence->Saves[w->selected_list_item].c_str()), 52 - 1);
}
}
break;
case WIDX_TITLE_EDITOR_LOAD_SAVE:
if (w->selected_list_item >= 0 && w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->NumSaves))
if (w->selected_list_item >= 0 && w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->Saves.size()))
{
auto handle = TitleSequenceGetParkHandle(*_editingTitleSequence, w->selected_list_item);
auto stream = static_cast<OpenRCT2::IStream*>(handle->Stream);
@@ -587,7 +587,7 @@ static void window_title_editor_scrollgetsize(rct_window* w, int32_t scrollIndex
{
size_t lineCount = 1;
if (w->selected_tab == WINDOW_TITLE_EDITOR_TAB_SAVES)
lineCount = _editingTitleSequence->NumSaves;
lineCount = _editingTitleSequence->Saves.size();
else if (w->selected_tab == WINDOW_TITLE_EDITOR_TAB_SCRIPT)
lineCount = _editingTitleSequence->NumCommands;
@@ -614,7 +614,7 @@ static void window_title_editor_scrollmousedown(rct_window* w, int32_t scrollInd
switch (w->selected_tab)
{
case WINDOW_TITLE_EDITOR_TAB_SAVES:
if (index < static_cast<int32_t>(_editingTitleSequence->NumSaves))
if (index < static_cast<int32_t>(_editingTitleSequence->Saves.size()))
{
w->selected_list_item = index;
widget_invalidate(w, WIDX_TITLE_EDITOR_LIST);
@@ -636,7 +636,7 @@ static void window_title_editor_scrollmouseover(rct_window* w, int32_t scrollInd
switch (w->selected_tab)
{
case WINDOW_TITLE_EDITOR_TAB_SAVES:
if (index < static_cast<int32_t>(_editingTitleSequence->NumSaves))
if (index < static_cast<int32_t>(_editingTitleSequence->Saves.size()))
_window_title_editor_highlighted_index = static_cast<int16_t>(index);
break;
case WINDOW_TITLE_EDITOR_TAB_SCRIPT:
@@ -862,7 +862,8 @@ static void window_title_editor_scrollpaint_saves(rct_window* w, rct_drawpixelin
if (_editingTitleSequence == nullptr)
return;
for (int32_t i = 0; i < static_cast<int32_t>(_editingTitleSequence->NumSaves); i++, screenCoords.y += SCROLLABLE_ROW_HEIGHT)
for (int32_t i = 0; i < static_cast<int32_t>(_editingTitleSequence->Saves.size());
i++, screenCoords.y += SCROLLABLE_ROW_HEIGHT)
{
bool selected = false;
bool hover = false;
@@ -885,7 +886,7 @@ static void window_title_editor_scrollpaint_saves(rct_window* w, rct_drawpixelin
char buffer[256];
auto ft = Formatter::Common();
ft.Add<const char*>(_editingTitleSequence->Saves[i]);
ft.Add<const char*>(_editingTitleSequence->Saves[i].c_str());
if (selected || hover)
{
format_string(buffer, 256, STR_STRING, gCommonFormatArgs);
@@ -949,7 +950,7 @@ static void window_title_editor_scrollpaint_commands(rct_window* w, rct_drawpixe
}
else
{
ft.Add<const char*>(_editingTitleSequence->Saves[command->SaveIndex]);
ft.Add<const char*>(_editingTitleSequence->Saves[command->SaveIndex].c_str());
}
break;
case TITLE_SCRIPT_LOCATION:
@@ -1070,7 +1071,10 @@ static void window_title_editor_load_sequence(size_t index)
size_t predefinedIndex = title_sequence_manager_get_predefined_index(index);
_isSequenceReadOnly = (predefinedIndex != SIZE_MAX);
_sequenceName = title_sequence_manager_get_name(index);
FreeTitleSequence(*_editingTitleSequence);
if (_editingTitleSequence != nullptr)
{
FreeTitleSequence(*_editingTitleSequence);
}
_editingTitleSequence = std::move(titleSequence);
window_close_by_class(WC_TITLE_COMMAND_EDITOR);
@@ -1100,11 +1104,11 @@ static bool window_title_editor_check_can_edit()
static bool save_filename_exists(const utf8* filename)
{
auto& seq = _editingTitleSequence;
for (size_t i = 0; i < seq->NumSaves; i++)
for (size_t i = 0; i < seq->Saves.size(); i++)
{
const utf8* savePath = seq->Saves[i];
const auto& savePath = seq->Saves[i];
if (_stricmp(savePath, filename) == 0)
if (_stricmp(savePath.c_str(), filename) == 0)
return true;
}
return false;
@@ -1139,12 +1143,12 @@ static void window_title_editor_rename_park(size_t index, const utf8* name)
return;
}
for (size_t i = 0; i < _editingTitleSequence->NumSaves; i++)
for (size_t i = 0; i < _editingTitleSequence->Saves.size(); i++)
{
if (i != index)
{
const utf8* savePath = _editingTitleSequence->Saves[i];
if (_strcmpi(savePath, name) == 0)
const auto& savePath = _editingTitleSequence->Saves[i];
if (_strcmpi(savePath.c_str(), name) == 0)
{
context_show_error(STR_ERROR_EXISTING_NAME, STR_NONE, {});
return;

View File

@@ -91,8 +91,10 @@ std::unique_ptr<TitleSequence> LoadTitleSequence(const utf8* path)
auto seq = CreateTitleSequence();
seq->Name = Path::GetFileNameWithoutExtension(path);
seq->Path = String::Duplicate(path);
seq->NumSaves = saves.size();
seq->Saves = Collections::ToArray(saves);
for (auto* save : saves)
{
seq->Saves.push_back(save);
}
seq->NumCommands = commands.size();
seq->Commands = Collections::ToArray(commands);
seq->IsZip = isZip;
@@ -104,19 +106,14 @@ void FreeTitleSequence(TitleSequence& seq)
Memory::Free(seq.Name);
Memory::Free(seq.Path);
Memory::Free(seq.Commands);
for (size_t i = 0; i < seq.NumSaves; i++)
{
Memory::Free(seq.Saves[i]);
}
Memory::Free(seq.Saves);
}
TitleSequenceParkHandle* TitleSequenceGetParkHandle(TitleSequence& seq, size_t index)
{
TitleSequenceParkHandle* handle = nullptr;
if (index <= seq.NumSaves)
if (index <= seq.Saves.size())
{
const utf8* filename = seq.Saves[index];
const auto& filename = seq.Saves[index];
if (seq.IsZip)
{
auto zip = std::unique_ptr<IZipArchive>(Zip::TryOpen(seq.Path, ZIP_ACCESS::READ));
@@ -134,14 +131,14 @@ TitleSequenceParkHandle* TitleSequenceGetParkHandle(TitleSequence& seq, size_t i
}
else
{
Console::Error::WriteLine("Failed to open zipped path '%s' from zip '%s'", filename, seq.Path);
Console::Error::WriteLine("Failed to open zipped path '%s' from zip '%s'", filename.c_str(), seq.Path);
}
}
else
{
utf8 absolutePath[MAX_PATH];
String::Set(absolutePath, sizeof(absolutePath), seq.Path);
Path::Append(absolutePath, sizeof(absolutePath), filename);
Path::Append(absolutePath, sizeof(absolutePath), filename.c_str());
OpenRCT2::FileStream* fileStream = nullptr;
try
@@ -201,23 +198,11 @@ bool TitleSequenceSave(TitleSequence& seq)
bool TitleSequenceAddPark(TitleSequence& seq, const utf8* path, const utf8* name)
{
// Get new save index
size_t index = SIZE_MAX;
for (size_t i = 0; i < seq.NumSaves; i++)
auto it = std::find(seq.Saves.begin(), seq.Saves.end(), path);
if (it == seq.Saves.end())
{
if (String::Equals(seq.Saves[i], path, true))
{
index = i;
break;
}
seq.Saves.push_back(name);
}
if (index == SIZE_MAX)
{
seq.Saves = Memory::ReallocateArray(seq.Saves, seq.NumSaves + 1);
Guard::Assert(seq.Saves != nullptr, GUARD_LINE);
index = seq.NumSaves;
seq.NumSaves++;
}
seq.Saves[index] = String::Duplicate(name);
if (seq.IsZip)
{
@@ -254,9 +239,9 @@ bool TitleSequenceAddPark(TitleSequence& seq, const utf8* path, const utf8* name
bool TitleSequenceRenamePark(TitleSequence& seq, size_t index, const utf8* name)
{
Guard::Assert(index < seq.NumSaves, GUARD_LINE);
Guard::Assert(index < seq.Saves.size(), GUARD_LINE);
utf8* oldRelativePath = seq.Saves[index];
auto& oldRelativePath = seq.Saves[index];
if (seq.IsZip)
{
auto zip = Zip::TryOpen(seq.Path, ZIP_ACCESS::WRITE);
@@ -272,7 +257,7 @@ bool TitleSequenceRenamePark(TitleSequence& seq, size_t index, const utf8* name)
utf8 srcPath[MAX_PATH];
utf8 dstPath[MAX_PATH];
String::Set(srcPath, sizeof(srcPath), seq.Path);
Path::Append(srcPath, sizeof(srcPath), oldRelativePath);
Path::Append(srcPath, sizeof(srcPath), oldRelativePath.c_str());
String::Set(dstPath, sizeof(dstPath), seq.Path);
Path::Append(dstPath, sizeof(dstPath), name);
if (!File::Move(srcPath, dstPath))
@@ -281,18 +266,16 @@ bool TitleSequenceRenamePark(TitleSequence& seq, size_t index, const utf8* name)
return false;
}
}
Memory::Free(seq.Saves[index]);
seq.Saves[index] = String::Duplicate(name);
seq.Saves[index] = name;
return true;
}
bool TitleSequenceRemovePark(TitleSequence& seq, size_t index)
{
Guard::Assert(index < seq.NumSaves, GUARD_LINE);
Guard::Assert(index < seq.Saves.size(), GUARD_LINE);
// Delete park file
utf8* relativePath = seq.Saves[index];
auto& relativePath = seq.Saves[index];
if (seq.IsZip)
{
auto zip = Zip::TryOpen(seq.Path, ZIP_ACCESS::WRITE);
@@ -307,7 +290,7 @@ bool TitleSequenceRemovePark(TitleSequence& seq, size_t index)
{
utf8 absolutePath[MAX_PATH];
String::Set(absolutePath, sizeof(absolutePath), seq.Path);
Path::Append(absolutePath, sizeof(absolutePath), relativePath);
Path::Append(absolutePath, sizeof(absolutePath), relativePath.c_str());
if (!File::Delete(absolutePath))
{
Console::Error::WriteLine("Unable to delete '%s'", absolutePath);
@@ -316,12 +299,7 @@ bool TitleSequenceRemovePark(TitleSequence& seq, size_t index)
}
// Remove from sequence
Memory::Free(relativePath);
for (size_t i = index; i < seq.NumSaves - 1; i++)
{
seq.Saves[i] = seq.Saves[i + 1];
}
seq.NumSaves--;
seq.Saves.erase(seq.Saves.begin() + index);
// Update load commands
for (size_t i = 0; i < seq.NumCommands; i++)
@@ -567,7 +545,7 @@ static std::string LegacyScriptWrite(TitleSequence& seq)
else
{
sb.Append("LOAD ");
sb.Append(seq.Saves[command->SaveIndex]);
sb.Append(seq.Saves[command->SaveIndex].c_str());
}
break;
case TITLE_SCRIPT_LOADSC:

View File

@@ -48,8 +48,7 @@ struct TitleSequence
size_t NumCommands = 0;
TitleCommand* Commands = 0;
size_t NumSaves = 0;
utf8** Saves = nullptr;
std::vector<std::string> Saves;
bool IsZip = false;
};