From 16fbc0f93f425d07d4cb21b6b2d04cbde00d7382 Mon Sep 17 00:00:00 2001 From: NovaRain Date: Tue, 16 Jul 2019 12:45:22 +0800 Subject: [PATCH] Changed "add_extra_msg_file" script function (#249) * added auto ID assignment for the message file. --- artifacts/scripting/headers/sfall.h | 2 +- artifacts/scripting/sfall function notes.txt | 9 ++++++--- sfall/Modules/Message.cpp | 13 +++++++++++-- sfall/Modules/Scripting/Handlers/Metarule.cpp | 2 +- sfall/Modules/Scripting/Handlers/Utils.cpp | 13 +++++++++++-- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/artifacts/scripting/headers/sfall.h b/artifacts/scripting/headers/sfall.h index 70d11940..5320b911 100644 --- a/artifacts/scripting/headers/sfall.h +++ b/artifacts/scripting/headers/sfall.h @@ -244,7 +244,7 @@ #define ADD_PERK_MODE_REMOVE (4) // remove from the list of selectable perks // sfall_funcX macros -#define add_extra_msg_file(name, number) sfall_func2("add_extra_msg_file", name, number) +#define add_extra_msg_file(name) sfall_func1("add_extra_msg_file", name) #define add_iface_tag sfall_func0("add_iface_tag") #define art_cache_clear sfall_func0("art_cache_clear") #define attack_is_aimed sfall_func0("attack_is_aimed") diff --git a/artifacts/scripting/sfall function notes.txt b/artifacts/scripting/sfall function notes.txt index fd6deaf7..0da5a1df 100644 --- a/artifacts/scripting/sfall function notes.txt +++ b/artifacts/scripting/sfall function notes.txt @@ -609,10 +609,13 @@ optional arguments: > int sfall_func2("has_fake_trait_npc", object npc, string nameTrait) - these functions are similar to has_fake_*/set_fake_*/set_selectable_perk functions, but apply to the specified party member NPC (including dude_obj) -> void sfall_func2("add_extra_msg_file", string fileName, int fileNumber) -- loads the custom message file and assigns it with the specified ID number +> int sfall_func1("add_extra_msg_file", string fileName) +> int sfall_func2("add_extra_msg_file", string fileName, int fileNumber) +- loads the custom message file, and returns the file ID number assigned to it in range from 0x3000 to 0x3FFF for the message_str_game function to get messages from the file - fileName: the name of the custom message file (including the .msg extension) in "text\\game\" directory -- fileNumber: the file ID number for the message_str_game function to get messages from the file. The available range is from 0x3000 to 0x3FFF +optional argument: +- fileNumber: the file ID number for the message_str_game function. The available range is from 0x2000 to 0x2FFF (see ExtraGameMsgFileList setting in ddraw.ini) + use fileNumber only if you want to add a message file without editing ddraw.ini or existing scripts to support the old way ------------------------ ------ MORE INFO ------- diff --git a/sfall/Modules/Message.cpp b/sfall/Modules/Message.cpp index 7887c106..638a99b5 100644 --- a/sfall/Modules/Message.cpp +++ b/sfall/Modules/Message.cpp @@ -56,6 +56,8 @@ const fo::MessageList* gameMsgFiles[] = { ExtraGameMessageListsMap gExtraGameMsgLists; static std::vector msgFileList; +static long msgNumCounter = 0x3000; + fo::MessageNode *GetMsgNode(fo::MessageList *msgList, int msgRef) { if (msgList != nullptr && msgList->numMsgs > 0) { fo::MessageNode *msgNode = msgList->nodes; @@ -115,16 +117,22 @@ static void ReadExtraGameMsgFiles() { } long Message::AddExtraMsgFile(const char* msgName, long msgNumber) { - if (msgNumber < 0x3000 || msgNumber > 0x3FFF || gExtraGameMsgLists.count(msgNumber)) return -1; + if (msgNumber) { + if (msgNumber < 0x2000 || msgNumber > 0x2FFF) return -1; + if (gExtraGameMsgLists.count(msgNumber)) return 0; // file has already been added + } else if (msgNumCounter > 0x3FFF) return -3; + std::string path("game\\"); path += msgName; fo::MessageList* list = new fo::MessageList(); if (fo::func::message_load(list, path.c_str())) { + if (msgNumber == 0) msgNumber = msgNumCounter++; gExtraGameMsgLists.emplace(msgNumber, list); } else { delete list; + msgNumber = -2; } - return 0; + return msgNumber; } static void ClearScriptAddedExtraGameMsg() { // C++11 @@ -136,6 +144,7 @@ static void ClearScriptAddedExtraGameMsg() { // C++11 ++it; } } + msgNumCounter = 0x3000; } static void ClearReadExtraGameMsgFiles() { diff --git a/sfall/Modules/Scripting/Handlers/Metarule.cpp b/sfall/Modules/Scripting/Handlers/Metarule.cpp index 026f9faf..618e1cc5 100644 --- a/sfall/Modules/Scripting/Handlers/Metarule.cpp +++ b/sfall/Modules/Scripting/Handlers/Metarule.cpp @@ -59,7 +59,7 @@ static MetaruleTableType metaruleTable; - arg1, arg2, ... - argument types for automatic validation */ static const SfallMetarule metarules[] = { - {"add_extra_msg_file", sf_add_extra_msg_file, 2, 2, {ARG_STRING, ARG_INT}}, + {"add_extra_msg_file", sf_add_extra_msg_file, 1, 2, {ARG_STRING, ARG_INT}}, {"add_iface_tag", sf_add_iface_tag, 0, 0}, {"art_cache_clear", sf_art_cache_flush, 0, 0}, {"attack_is_aimed", sf_attack_is_aimed, 0, 0}, diff --git a/sfall/Modules/Scripting/Handlers/Utils.cpp b/sfall/Modules/Scripting/Handlers/Utils.cpp index ddef6a85..1feda9cd 100644 --- a/sfall/Modules/Scripting/Handlers/Utils.cpp +++ b/sfall/Modules/Scripting/Handlers/Utils.cpp @@ -298,8 +298,17 @@ void sf_message_str_game(OpcodeContext& ctx) { } void sf_add_extra_msg_file(OpcodeContext& ctx) { - long result = Message::AddExtraMsgFile(ctx.arg(0).strValue(), ctx.arg(1).rawValue()); - if (result == -1) ctx.printOpcodeError("%s() - cannot add message file with the specified number.", ctx.getMetaruleName()); + long result = Message::AddExtraMsgFile(ctx.arg(0).strValue(), (ctx.numArgs() == 2) ? ctx.arg(1).rawValue() : 0); + switch (result) { + case -1 : + ctx.printOpcodeError("%s() - cannot add message file with the specified number.", ctx.getMetaruleName()); + break; + case -2 : + ctx.printOpcodeError("%s() - error loading message file.", ctx.getMetaruleName()); + break; + case -3 : + ctx.printOpcodeError("%s() - the limit of adding message files has been exceeded.", ctx.getMetaruleName()); + } ctx.setReturn(result); }