diff --git a/distribution/openrct2.d.ts b/distribution/openrct2.d.ts index f944c78d78..701aa934af 100644 --- a/distribution/openrct2.d.ts +++ b/distribution/openrct2.d.ts @@ -2198,13 +2198,15 @@ declare global { /** * The parks stored within this title sequence. */ - parks: string[]; + readonly parks: TitleSequencePark[]; /** * The commands that describe how to play the title sequence. */ commands: TitleSequenceCommand[]; + addPark(path: string, fileName: string): void; + /** * Creates a new title sequence identical to this one. * @param name The name of the new title sequence. @@ -2217,6 +2219,18 @@ declare global { delete(): void; } + interface TitleSequencePark { + /** + * The file name of the park. + */ + fileName: string; + + /** + * Deletes this park from the title sequence. + */ + delete(): void; + } + type TitleSequenceCommandType = 'load' | 'loadsc' | diff --git a/src/openrct2-ui/scripting/ScTitleSequence.hpp b/src/openrct2-ui/scripting/ScTitleSequence.hpp index 054153228f..7e5d0da5e3 100644 --- a/src/openrct2-ui/scripting/ScTitleSequence.hpp +++ b/src/openrct2-ui/scripting/ScTitleSequence.hpp @@ -13,10 +13,87 @@ # include # include +# include # include namespace OpenRCT2::Scripting { + class ScTitleSequencePark + { + private: + std::string _titleSequencePath; + std::string _fileName; + + public: + ScTitleSequencePark(std::string_view path, std::string_view fileName) + : _titleSequencePath(path) + , _fileName(fileName) + { + } + + private: + std::string fileName_get() const + { + return _fileName; + } + + void fileName_set(const std::string& value) + { + if (value == _fileName) + return; + + auto seq = LoadTitleSequence(_titleSequencePath); + if (seq != nullptr) + { + // Check if name already in use + auto index = GetIndex(*seq, value); + if (!index) + { + index = GetIndex(*seq, _fileName); + if (index) + { + TitleSequenceRenamePark(*seq, *index, value.c_str()); + TitleSequenceSave(*seq); + } + } + } + } + + void delete_() + { + auto seq = LoadTitleSequence(_titleSequencePath); + if (seq != nullptr) + { + auto index = GetIndex(*seq, _fileName); + if (index) + { + TitleSequenceRemovePark(*seq, *index); + TitleSequenceSave(*seq); + } + } + } + + public: + static void Register(duk_context* ctx) + { + dukglue_register_property(ctx, &ScTitleSequencePark::fileName_get, &ScTitleSequencePark::fileName_set, "fileName"); + dukglue_register_method(ctx, &ScTitleSequencePark::delete_, "delete"); + } + + private: + static std::optional GetIndex(const TitleSequence& seq, const std::string_view needle) + { + for (size_t i = 0; i < seq.Saves.size(); i++) + { + if (seq.Saves[i] == needle) + { + return i; + } + } + return {}; + } + }; + class ScTitleSequence { private: @@ -82,6 +159,20 @@ namespace OpenRCT2::Scripting return {}; } + std::vector> parks_get() const + { + std::vector> result; + auto titleSeq = LoadTitleSequence(_path); + if (titleSeq != nullptr) + { + for (size_t i = 0; i < titleSeq->Saves.size(); i++) + { + result.push_back(std::make_shared(_path, titleSeq->Saves[i])); + } + } + return result; + } + std::shared_ptr clone(const std::string& name) const { auto copyIndex = GetManagerIndex(); @@ -114,6 +205,7 @@ namespace OpenRCT2::Scripting dukglue_register_property(ctx, &ScTitleSequence::path_get, nullptr, "path"); dukglue_register_property(ctx, &ScTitleSequence::isDirectory_get, nullptr, "isDirectory"); dukglue_register_property(ctx, &ScTitleSequence::isReadOnly_get, nullptr, "isReadOnly"); + dukglue_register_property(ctx, &ScTitleSequence::parks_get, nullptr, "parks"); dukglue_register_method(ctx, &ScTitleSequence::clone, "clone"); dukglue_register_method(ctx, &ScTitleSequence::delete_, "delete"); } diff --git a/src/openrct2-ui/scripting/UiExtensions.cpp b/src/openrct2-ui/scripting/UiExtensions.cpp index f97c58f653..316b9af0b2 100644 --- a/src/openrct2-ui/scripting/UiExtensions.cpp +++ b/src/openrct2-ui/scripting/UiExtensions.cpp @@ -48,6 +48,7 @@ void UiScriptExtensions::Extend(ScriptEngine& scriptEngine) ScTitleSequence::Register(ctx); ScTitleSequenceManager::Register(ctx); + ScTitleSequencePark::Register(ctx); ScWindow::Register(ctx); InitialiseCustomMenuItems(scriptEngine);