Jelajahi Sumber

Added a fix for the installer: Single line IFs in field maps were not being closed with END.

Iñigo Valentin 3 tahun lalu
induk
melakukan
bfd3154fdd

+ 0 - 1
V-Gears-Installer/src/ff7DataInstaller.cpp

@@ -768,7 +768,6 @@ static void FF7PcFieldToQGearsField(
         }
 
         // Get lines. Add them to a list, so they aren't processed later as regular entities.
-        std::cout << "Processing lines..." << std::endl;
         std::vector<std::string> line_entities;
         for (SUDM::FF7::Field::Line line : decompiled.lines){
             std::unique_ptr<TiXmlElement> xml_entity_trigger(new TiXmlElement("entity_trigger"));

+ 16 - 12
lib/SUDM/decompiler/ff7_field/ff7_field_codegen.cpp

@@ -73,6 +73,7 @@ void FF7::FF7SimpleCodeGenerator::generate(InstVec& insts, const Graph& /*g*/){
       function != functions_with_bodies.end();
       ++ function
     ){
+
         onBeforeStartFunction(function->first);
         auto signature = constructFuncSignature(function->first);
         addOutputLine(signature, false, true);
@@ -94,11 +95,13 @@ void FF7::FF7SimpleCodeGenerator::generate(InstVec& insts, const Graph& /*g*/){
         }
 
         // Implemented instructions.
+        bool end_needed = false;
         for (
           auto instruction = function->second.begin();
           instruction != function->second.end();
           ++instruction
         ){
+
             auto label = labels.find((*instruction)->_address);
             if (label != labels.end()){
                 bool needs_label = false;
@@ -110,7 +113,6 @@ void FF7::FF7SimpleCodeGenerator::generate(InstVec& insts, const Graph& /*g*/){
                     }
                     else needs_label = true;
                 }
-
                 if (needs_new_line) addOutputLine("");
                 if (needs_label)
                     addOutputLine((boost::format("::label_0x%1$X::") % label->first).str());
@@ -119,15 +121,21 @@ void FF7::FF7SimpleCodeGenerator::generate(InstVec& insts, const Graph& /*g*/){
             ValueStack stack;
             (*instruction)->processInst(function->first, stack, _engine, this);
 
+            if (end_needed){
+                addOutputLine("end -- end if", true, false);
+                end_needed = false;
+            }
+
             if ((*instruction)->isCondJump()){
                 addOutputLine(
                   (boost::format("if (%s) then") % stack.pop()->getString()).str(), false, true
                 );
 
-                // If there are no more instructions then ensure end is outputted.
-                if (instruction + 1 == std::end(function->second)){
-                    addOutputLine("end", true, false);
-                }
+                // If the next instruction is the last in the function, mark the next pass to
+                // add an 'end' after the instruction to clode the if.
+                if ((*(instruction + 1))->_address == (*(function->second.back()))._address)
+                    end_needed = true;
+
             }
             else if ((*instruction)->isUncondJump()){
                 // If destination address is outside the functions, turn goto into a return.
@@ -146,26 +154,22 @@ void FF7::FF7SimpleCodeGenerator::generate(InstVec& insts, const Graph& /*g*/){
                     );
                 }
             }
+
             // Else, already output'd.
         }
 
+        // Add missing return:
         if (
           "return 0" != mLines.at(mLines.size() - 1)._line
           && "do return 0 end" != mLines.at(mLines.size() - 1)._line
         ){
-            /*if (function->first._name == "Init"){
-                addOutputLine("--do return 0 end -- INIT return, omit.", false, false);
-            }
-            addOutputLine("-- " + function->first._name + ": Missing original RET", false, false);
-            addOutputLine("--LAST --" + mLines.at(mLines.size() - 1)._line + "--", false, false);*/
             addOutputLine("do return 0 end", false, false);
         }
         onEndFunction(function->first);
     }
 
     for (auto i = mLines.begin(); i != mLines.end(); ++i){
-        if (i->_unindentBefore){
-            assert(_indentLevel > 0);
+        if (i->_unindentBefore && _indentLevel > 0){
             _indentLevel --;
         }
         _output << indentString(i->_line) << std::endl;

+ 2 - 6
lib/SUDM/decompiler/ff7_field/ff7_field_engine.cpp

@@ -461,13 +461,9 @@ void FF7::FF7ControlFlowInstruction::processInst(Function& func, ValueStack&, En
         //     'main' return is kept, the code of 'main' is never executed.
         //     There are safeguards forfunctions without a return in the end,
         //     so it's OK not to include it here.
-        if (func._name != "on_start")
+        if (func._name != "on_start"){
             codeGen->addOutputLine("do return 0 end");
-        // TODO: Lua won't allow return if not followed by end;
-        //codeGen->addOutputLine("--RET : " + func._name); // Seems all our functions must return zero
-        //codeGen->addOutputLine("do return 0 end"); // Seems all our functions must return zero
-        //codeGen->addOutputLine("do return end");
-        //codeGen->addOutputLine("-- do return end");
+        }
         break;
 
     case eOpcodes::REQ: