Browse Source

Fix eals_1 crash, debug text scrolling and line near-once events

- TextHandler: GetDialogText builds the text from all the parts of the node. A dialog that starts with a character-name tag crashed the game .
- DebugDraw: reset the world, view and projection matrices before drawing the screen-space text and quads.
- Decompiler: the check that stops the events of a line while it is off now also covers on_near_once.
- Logging: remove the movie-frame, audio player and scroll diagnostics added while working on movies.

Tests
- TextHandler text extraction (plain, character tag first, other tags) and a  decompiler case for the events of a line that is off.
Iñigo Valentin 12 giờ trước cách đây
mục cha
commit
da848ab75e

+ 5 - 0
src/core/DebugDraw.cpp

@@ -419,6 +419,11 @@ void DebugDraw::renderQueueEnded(
 ){
     if (queueGroupId == Ogre::RENDER_QUEUE_OVERLAY){
         render_system_->clearFrameBuffer(Ogre::FBT_DEPTH);
+        // The text and the quads are given in screen coordinates. The background leaves its scroll
+        // in the view matrix, so without this they would move with it, but not at the same speed.
+        render_system_->_setWorldMatrix(Ogre::Matrix4::IDENTITY);
+        render_system_->_setViewMatrix(Ogre::Matrix4::IDENTITY);
+        render_system_->_setProjectionMatrix(Ogre::Matrix4::IDENTITY);
         auto render_params= render_system_->getFixedFunctionParams(Ogre::TVC_NONE, Ogre::FOG_NONE);
         render_params->setConstant(
           Ogre::GpuProgramParameters::ACT_WORLD_MATRIX, Ogre::Matrix4::IDENTITY

+ 24 - 5
src/core/TextHandler.cpp

@@ -83,15 +83,34 @@ TiXmlNode* TextHandler::GetDialog(const Ogre::String& name, float &width, float&
     return NULL;
 }
 
+std::string TextHandler::ExtractText(
+  const TiXmlNode* node, const std::function<std::string(int)>& character_name
+){
+    std::string text;
+    if (node == nullptr) return text;
+    for (const TiXmlNode* child = node->FirstChild(); child; child = child->NextSibling()){
+        if (child->ToText() != nullptr){
+            text += child->ToText()->Value();
+        }
+        else if (child->ToElement() != nullptr){
+            if (child->ValueStr() == "character"){
+                int id = -1;
+                if (child->ToElement()->QueryIntAttribute("id", &id) == TIXML_SUCCESS)
+                    text += character_name(id);
+            }
+            else text += ExtractText(child, character_name);
+        }
+    }
+    return text;
+}
+
 std::string TextHandler::GetDialogText(const std::string name){
-    std::string text = "";
     for (unsigned int i = 0; i < dialogs_.size(); ++ i){
         if (dialogs_[i].name == name){
-            text = dialogs_[i].node->FirstChild()->ToText()->Value();
-            break;
+            return ExtractText(dialogs_[i].node, [this](int id){return GetCharacterName(id);});
         }
     }
-    return text;
+    return "";
 }
 void TextHandler::UnloadTexts(){
     for (unsigned int i = 0; i < texts_.size(); ++ i) delete texts_[i].node;
@@ -102,7 +121,7 @@ void TextHandler::UnloadTexts(){
 
 std::string TextHandler::GetCharacterName(int id){
     if (id >= 0 && id < 11) return character_names_[id];
-    else return std::string("UNKNOWN_CHAR_" + id);
+    else return "UNKNOWN_CHAR_" + std::to_string(id);
 }
 
 std::string TextHandler::GetPartyCharacterName(int position){

+ 13 - 0
src/core/TextHandler.h

@@ -103,6 +103,19 @@ class TextHandler : public Ogre::Singleton<TextHandler>{
          */
         TiXmlNode* GetDialog(const Ogre::String& name, float &width, float& height) const;
 
+        /**
+         * Gets the plain text of a dialog node. The tags that don't hold text, such as the colour,
+         * are skipped (their content is kept), and the character tags are replaced by the name of
+         * the character.
+         *
+         * @param[in] node The node of the dialog.
+         * @param[in] character_name Gives the name of a character from its ID.
+         * @return The text.
+         */
+        static std::string ExtractText(
+          const TiXmlNode* node, const std::function<std::string(int)>& character_name
+        );
+
         /**
          * Retrieves a dialog text by name.
          *

+ 5 - 2
src/installer/decompiler/field/FieldCodeGenerator.cpp

@@ -282,8 +282,11 @@ void FieldCodeGenerator::OnStartFunction(const Function& func){
         AddOutputLine("end", true, false);
     }
     // For lines, exit if they are off.
-    if (func.name == "on_approach" || func.name == "on_cross" || func.name == "on_near" || func.name == "on_leave"){
-        AddOutputLine("if self.on ~= nill and self.on == false then", false, true);
+    if (
+      func.name == "on_approach" || func.name == "on_cross" || func.name == "on_near"
+      || func.name == "on_near_once" || func.name == "on_leave"
+    ){
+        AddOutputLine("if self.on ~= nil and self.on == false then", false, true);
         AddOutputLine("return 0");
         AddOutputLine("end", true, false);
     }

+ 27 - 0
test/core/TextHandler.cpp

@@ -16,6 +16,7 @@
 #include "../../src/core/TextHandler.h"
 
 #include <boost/test/unit_test.hpp>
+#include <tinyxml.h>
 
 #pragma message("Unimplemented test file: " __FILE__)
 
@@ -25,3 +26,29 @@ BOOST_AUTO_TEST_CASE(TestTextManager){
     BOOST_CHECK(0 == 0);
 }
 
+
+namespace{
+    std::string TextOf(const char* xml){
+        TiXmlDocument document;
+        document.Parse(xml);
+        return TextHandler::ExtractText(
+          document.RootElement(), [](int id){return std::string("Name") + std::to_string(id);}
+        );
+    }
+}
+
+BOOST_AUTO_TEST_CASE(TestTextHandlerExtractTextPlain){
+    BOOST_CHECK_EQUAL(TextOf("<dialog>Sector 5</dialog>"), "Sector 5");
+    BOOST_CHECK_EQUAL(TextOf("<dialog></dialog>"), "");
+}
+
+BOOST_AUTO_TEST_CASE(TestTextHandlerExtractTextStartsWithACharacterName){
+    // The map name of the house of Tifa. It starts with a tag, which crashed the game.
+    BOOST_CHECK_EQUAL(TextOf("<dialog><character id=\"3\" />&apos;s House</dialog>"), "Name3's House");
+}
+
+BOOST_AUTO_TEST_CASE(TestTextHandlerExtractTextKeepsTheContentOfOtherTags){
+    BOOST_CHECK_EQUAL(
+      TextOf("<dialog>A<colour value=\"1 0 0\">[SAVE]</colour>B</dialog>"), "A[SAVE]B"
+    );
+}

+ 32 - 0
test/installer/decompiler/field/FieldDecompiler.cpp

@@ -144,6 +144,38 @@ BOOST_AUTO_TEST_CASE(TestFieldDecompilerNonWaitingAnimationsDontWait){
     BOOST_CHECK(output.find("animation_sync") == std::string::npos);
 }
 
+BOOST_AUTO_TEST_CASE(TestFieldDecompilerEventsOfALineDoNotRunWhileItIsOff){
+    // An entity whose script 5 (on_near_once) is its own code, and the other scripts share another.
+    std::vector<unsigned char> script(104, 0);
+    script[0] = 0x02;
+    script[1] = 0x05;
+    script[2] = 1;
+    script[9] = 2;
+    script[32] = 'D';
+    script[33] = 'o';
+    script[34] = 'o';
+    script[35] = 'r';
+    for (size_t index = 0; index < 32; ++index) script[40 + index * 2] = 104;
+    // The code of each script comes after the previous one. The first one has a LINE, which makes
+    // the entity a line, and its scripts the events of one.
+    script[40 + 5 * 2] = 118;
+    for (size_t index = 6; index < 32; ++index) script[40 + index * 2] = 119;
+    script.push_back(0xD0); // LINE, with six values of two bytes
+    for (int i = 0; i < 12; ++i) script.push_back(0);
+    script.push_back(0); // RET of the first scripts
+    script.push_back(0); // RET of on_near_once
+    script.push_back(0); // RET of the last scripts
+    script[4] = static_cast<unsigned char>(script.size());
+    script[5] = static_cast<unsigned char>(script.size() >> 8);
+    FieldScriptFormatter formatter;
+    const std::string output = FieldDecompiler::Decompile("synthetic", script, formatter).luaScript;
+    const size_t function = output.find("on_near_once = function(self)");
+    BOOST_REQUIRE(function != std::string::npos);
+    const size_t guard = output.find("if self.on ~= nil and self.on == false then", function);
+    const size_t body = output.find("--[[", function);
+    BOOST_CHECK(guard != std::string::npos && guard < body);
+}
+
 BOOST_AUTO_TEST_CASE(TestFieldDecompilerMovaWalksToTheEntityAndWaits){
     FieldScriptFormatter formatter;
     // MOVA 0: the only entity of the synthetic field.