From 067d36400296286d79bd460cbebb99306c00eb70 Mon Sep 17 00:00:00 2001 From: Ted John Date: Wed, 7 Dec 2016 12:00:02 +0000 Subject: [PATCH] Fix rename park in sequence --- src/core/File.cpp | 5 +++++ src/core/File.h | 1 + src/core/Zip.cpp | 6 ++++++ src/core/Zip.h | 1 + src/title/TitleSequence.cpp | 38 +++++++++++++++++++++++++++++++++++- src/title/TitleSequence.h | 1 + src/windows/title_editor.c | 39 +++++++++++++++++++++---------------- 7 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/core/File.cpp b/src/core/File.cpp index 42e5c53a11..1a12d746bb 100644 --- a/src/core/File.cpp +++ b/src/core/File.cpp @@ -38,6 +38,11 @@ namespace File return platform_file_delete(path); } + bool Move(const utf8 * srcPath, const utf8 * dstPath) + { + return platform_file_move(srcPath, dstPath); + } + void * ReadAllBytes(const utf8 * path, size_t * length) { void * result = nullptr; diff --git a/src/core/File.h b/src/core/File.h index 284d9e2adf..dd935c52d4 100644 --- a/src/core/File.h +++ b/src/core/File.h @@ -22,5 +22,6 @@ namespace File { bool Copy(const utf8 * srcPath, const utf8 * dstPath, bool overwrite); bool Delete(const utf8 * path); + bool Move(const utf8 * srcPath, const utf8 * dstPath); void * ReadAllBytes(const utf8 * path, size_t * length); } diff --git a/src/core/Zip.cpp b/src/core/Zip.cpp index 7ff0103b1c..7ce770e518 100644 --- a/src/core/Zip.cpp +++ b/src/core/Zip.cpp @@ -118,6 +118,12 @@ public: uint64 index = zip_name_locate(_zip, path, 0); zip_delete(_zip, index); } + + void RenameFile(const utf8 * path, const utf8 * newPath) override + { + uint64 index = zip_name_locate(_zip, path, 0); + zip_file_rename(_zip, index, newPath, ZIP_FL_ENC_GUESS); + } }; namespace Zip diff --git a/src/core/Zip.h b/src/core/Zip.h index 77f1cb1685..88a24698d3 100644 --- a/src/core/Zip.h +++ b/src/core/Zip.h @@ -40,6 +40,7 @@ interface IZipArchive virtual void SetFileData(const utf8 * path, void * data, size_t dataSize) abstract; virtual void DeleteFile(const utf8 * path) abstract; + virtual void RenameFile(const utf8 * path, const utf8 * newPath) abstract; }; enum ZIP_ACCESS diff --git a/src/title/TitleSequence.cpp b/src/title/TitleSequence.cpp index 861e73f6f7..3908588431 100644 --- a/src/title/TitleSequence.cpp +++ b/src/title/TitleSequence.cpp @@ -258,9 +258,45 @@ extern "C" return true; } + bool TileSequenceRenamePark(TitleSequence * seq, size_t index, const utf8 * name) + { + Guard::Assert(index < seq->NumSaves, GUARD_LINE); + + utf8 * oldRelativePath = seq->Saves[index]; + if (seq->IsZip) + { + IZipArchive * zip = Zip::TryOpen(seq->Path, ZIP_ACCESS_WRITE); + if (zip == nullptr) + { + Console::Error::WriteLine("Unable to open '%s'", seq->Path); + return false; + } + zip->RenameFile(oldRelativePath, name); + delete zip; + } + else + { + utf8 srcPath[MAX_PATH]; + utf8 dstPath[MAX_PATH]; + String::Set(srcPath, sizeof(srcPath), seq->Path); + Path::Append(srcPath, sizeof(srcPath), oldRelativePath); + String::Set(dstPath, sizeof(dstPath), seq->Path); + Path::Append(dstPath, sizeof(dstPath), name); + if (!File::Move(srcPath, dstPath)) + { + Console::Error::WriteLine("Unable to move '%s' to '%s'", srcPath, dstPath); + return false; + } + } + + Memory::Free(seq->Saves[index]); + seq->Saves[index] = String::Duplicate(name); + return true; + } + bool TitleSequenceRemovePark(TitleSequence * seq, size_t index) { - Guard::Assert(seq->NumSaves > index, GUARD_LINE); + Guard::Assert(index < seq->NumSaves, GUARD_LINE); // Delete park file utf8 * relativePath = seq->Saves[index]; diff --git a/src/title/TitleSequence.h b/src/title/TitleSequence.h index 77c195f0a0..c6237cb492 100644 --- a/src/title/TitleSequence.h +++ b/src/title/TitleSequence.h @@ -95,6 +95,7 @@ extern "C" void TitleSequenceCloseParkHandle(TitleSequenceParkHandle * handle); bool TileSequenceSave(TitleSequence * seq); bool TileSequenceAddPark(TitleSequence * seq, const utf8 * path, const utf8 * name); + bool TileSequenceRenamePark(TitleSequence * seq, size_t index, const utf8 * name); bool TitleSequenceRemovePark(TitleSequence * seq, size_t index); bool TitleSequenceIsLoadCommand(const TitleCommand * command); diff --git a/src/windows/title_editor.c b/src/windows/title_editor.c index 08188570f7..c8748c7b32 100644 --- a/src/windows/title_editor.c +++ b/src/windows/title_editor.c @@ -65,7 +65,7 @@ static void window_title_editor_load_sequence(size_t index); static ITitleSequencePlayer * window_title_editor_get_player(); static bool window_title_editor_check_can_edit(); static void window_title_editor_add_park_callback(int result, const utf8 * path); -static void window_title_editor_save_sequence(); +static void window_title_editor_rename_park(size_t index, const utf8 * name); static rct_window_event_list window_title_editor_events = { window_title_editor_close, @@ -399,7 +399,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex) if (w->selected_list_item >= (sint16)_editingTitleSequence->NumCommands) { w->selected_list_item--; } - window_title_editor_save_sequence(); + TileSequenceSave(_editingTitleSequence); } } break; @@ -422,7 +422,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex) *a = *b; *b = tmp; w->selected_list_item++; - window_title_editor_save_sequence(); + TileSequenceSave(_editingTitleSequence); } } break; @@ -435,7 +435,7 @@ static void window_title_editor_mouseup(rct_window *w, int widgetIndex) *b = *a; *a = tmp; w->selected_list_item--; - window_title_editor_save_sequence(); + TileSequenceSave(_editingTitleSequence); } } break; @@ -641,17 +641,7 @@ static void window_title_editor_textinput(rct_window *w, int widgetIndex, char * } break; case WIDX_TITLE_EDITOR_RENAME_SAVE: - // if (filename_valid_characters(text)) { - // if (!title_sequence_save_exists(_selectedTitleSequence, text)) { - // title_sequence_rename_save(_selectedTitleSequence, w->selected_list_item, text); - // TileSequenceSave(_editingTitleSequence); - // window_invalidate(w); - // } else { - // window_error_open(STR_ERROR_EXISTING_NAME, STR_NONE); - // } - // } else { - // window_error_open(STR_ERROR_INVALID_CHARACTERS, STR_NONE); - // } + window_title_editor_rename_park(w->selected_list_item, text); break; } } @@ -1024,9 +1014,24 @@ static void window_title_editor_add_park_callback(int result, const utf8 * path) TileSequenceAddPark(_editingTitleSequence, path, filename); } -static void window_title_editor_save_sequence() +static void window_title_editor_rename_park(size_t index, const utf8 * name) { - if (_editingTitleSequence != NULL) { + if (!filename_valid_characters(name)) { + window_error_open(STR_ERROR_INVALID_CHARACTERS, STR_NONE); + return; + } + + for (size_t i = 0; i < _editingTitleSequence->NumSaves; i++) { + if (i != index) { + const utf8 * savePath = _editingTitleSequence->Saves[i]; + if (_strcmpi(savePath, name) == 0) { + window_error_open(STR_ERROR_EXISTING_NAME, STR_NONE); + return; + } + } + } + + if (TileSequenceRenamePark(_editingTitleSequence, index, name)) { TileSequenceSave(_editingTitleSequence); } }