diff --git a/src/core/Zip.cpp b/src/core/Zip.cpp index 29d123f356..5a9d88b3e8 100644 --- a/src/core/Zip.cpp +++ b/src/core/Zip.cpp @@ -111,7 +111,6 @@ public: { zip_replace(_zip, index, source); } - zip_source_free(source); } }; diff --git a/src/core/Zip.h b/src/core/Zip.h index db89785215..af62cd4f4a 100644 --- a/src/core/Zip.h +++ b/src/core/Zip.h @@ -30,6 +30,13 @@ interface IZipArchive virtual const uint64 GetFileSize(size_t index) const abstract; virtual void * GetFileData(const utf8 * path, size_t * outSize) const abstract; + /** + * Creates or overwrites a file within the zip archive to the given data buffer. + * @param path The path of the file within the zip. + * @param data The data to write, this buffer must not be mutated or disposed until + * the zip archive has been disposed. + * @param dataSize The size of the data in bytes. + */ virtual void SetFileData(const utf8 * path, void * data, size_t dataSize) abstract; }; diff --git a/src/title/TitleSequence.cpp b/src/title/TitleSequence.cpp index 724defe78b..fc09b9ec68 100644 --- a/src/title/TitleSequence.cpp +++ b/src/title/TitleSequence.cpp @@ -39,6 +39,13 @@ static utf8 * LegacyScriptWrite(TitleSequence * seq); extern "C" { + TitleSequence * CreateTitleSequence() + { + TitleSequence * seq = Memory::Allocate(); + Memory::Set(seq, 0, sizeof(TitleSequence)); + return seq; + } + TitleSequence * LoadTitleSequence(const utf8 * path) { size_t scriptLength; @@ -87,7 +94,7 @@ extern "C" std::vector commands = LegacyScriptRead(script, scriptLength, saves); - TitleSequence * seq = Memory::Allocate(); + TitleSequence * seq = CreateTitleSequence(); seq->Name = Path::GetFileNameWithoutExtension(path); seq->Path = String::Duplicate(path); seq->NumSaves = saves.size(); diff --git a/src/title/TitleSequence.h b/src/title/TitleSequence.h index 6d5fbbc9f1..77be35e7e3 100644 --- a/src/title/TitleSequence.h +++ b/src/title/TitleSequence.h @@ -84,6 +84,7 @@ constexpr const utf8 * TITLE_SEQUENCE_EXTENSION = ".parkseq"; extern "C" { #endif + TitleSequence * CreateTitleSequence(); TitleSequence * LoadTitleSequence(const utf8 * path); void FreeTitleSequence(TitleSequence * seq); diff --git a/src/title/TitleSequenceManager.cpp b/src/title/TitleSequenceManager.cpp index bed29349cf..5344b29eb8 100644 --- a/src/title/TitleSequenceManager.cpp +++ b/src/title/TitleSequenceManager.cpp @@ -142,6 +142,31 @@ namespace TitleSequenceManager return index; } + size_t CreateItem(const utf8 * name) + { + utf8 path[MAX_PATH]; + GetUserSequencesPath(path, sizeof(path)); + Path::Append(path, sizeof(path), name); + String::Append(path, sizeof(path), TITLE_SEQUENCE_EXTENSION); + + TitleSequence * seq = CreateTitleSequence(); + seq->Name = String::Duplicate(name); + seq->Path = String::Duplicate(path); + seq->IsZip = true; + + bool success = TileSequenceSave(seq); + FreeTitleSequence(seq); + + size_t index = SIZE_MAX; + if (success) + { + AddSequence(path); + SortSequences(); + index = FindItemIndexByPath(path); + } + return success; + } + static const uint16 GetPredefinedIndex(const std::string &path) { const utf8 * filename = Path::GetFileName(path.c_str()); @@ -346,4 +371,9 @@ extern "C" { return TitleSequenceManager::DuplicateItem(i, name); } + + size_t title_sequence_manager_create(const utf8 * name) + { + return TitleSequenceManager::CreateItem(name); + } } diff --git a/src/title/TitleSequenceManager.h b/src/title/TitleSequenceManager.h index 57d04119c5..f789425378 100644 --- a/src/title/TitleSequenceManager.h +++ b/src/title/TitleSequenceManager.h @@ -37,6 +37,7 @@ namespace TitleSequenceManager void DeleteItem(size_t i); size_t RenameItem(size_t i, const utf8 * name); size_t DuplicateItem(size_t i, const utf8 * name); + size_t CreateItem(const utf8 * name); } constexpr uint16 PREDEFINED_INDEX_CUSTOM = UINT16_MAX; @@ -55,6 +56,7 @@ extern "C" { void title_sequence_manager_delete(size_t i); size_t title_sequence_manager_rename(size_t i, const utf8 * name); size_t title_sequence_manager_duplicate(size_t i, const utf8 * name); + size_t title_sequence_manager_create(const utf8 * name); #ifdef __cplusplus } diff --git a/src/windows/title_editor.c b/src/windows/title_editor.c index e48debe268..b03be184e5 100644 --- a/src/windows/title_editor.c +++ b/src/windows/title_editor.c @@ -609,7 +609,8 @@ static void window_title_editor_textinput(rct_window *w, int widgetIndex, char * if (filename_valid_characters(text)) { if (title_sequence_manager_get_index_for_name(text) == SIZE_MAX) { if (widgetIndex == WIDX_TITLE_EDITOR_NEW_BUTTON) { - title_sequence_create_preset(text); + size_t newIndex = title_sequence_manager_create(text); + window_title_editor_load_sequence(newIndex); } else if (widgetIndex == WIDX_TITLE_EDITOR_DUPLICATE_BUTTON) { size_t newIndex = title_sequence_manager_duplicate(_selectedTitleSequence, text); window_title_editor_load_sequence(newIndex);