Просмотр исходного кода

All of the old SUDM code refactored, reworked and simplified.

Iñigo Valentin 3 лет назад
Родитель
Сommit
c465070196
23 измененных файлов с 577 добавлено и 392 удалено
  1. 7 2
      V-Gears-Installer/CMakeLists.txt
  2. 24 0
      V-Gears-Installer/include/decompiler/Decompiler.h
  3. 90 0
      V-Gears-Installer/include/decompiler/ScriptFormatter.h
  4. 7 7
      V-Gears-Installer/include/decompiler/field/FieldCodeGenerator.h
  5. 126 0
      V-Gears-Installer/include/decompiler/field/FieldDecompiler.h
  6. 4 4
      V-Gears-Installer/include/decompiler/field/FieldDisassembler.h
  7. 7 6
      V-Gears-Installer/include/decompiler/field/FieldEngine.h
  8. 76 0
      V-Gears-Installer/include/decompiler/field/FieldScriptFormatter.h
  9. 0 244
      V-Gears-Installer/include/decompiler/sudm.h
  10. 2 2
      V-Gears-Installer/include/decompiler/test/ff7_field_dummy_formatter.h
  11. 1 1
      V-Gears-Installer/include/decompiler/test/main.cpp
  12. 79 0
      V-Gears-Installer/src/decompiler/ScriptFormatter.cpp
  13. 6 5
      V-Gears-Installer/src/decompiler/field/FieldCodeGenerator.cpp
  14. 57 0
      V-Gears-Installer/src/decompiler/field/FieldDecompiler.cpp
  15. 3 3
      V-Gears-Installer/src/decompiler/field/FieldDisassembler.cpp
  16. 7 7
      V-Gears-Installer/src/decompiler/field/FieldEngine.cpp
  17. 48 0
      V-Gears-Installer/src/decompiler/field/FieldScriptFormatter.cpp
  18. 3 3
      V-Gears-Installer/src/decompiler/field/instruction/FieldControlFlowInstruction.cpp
  19. 6 6
      V-Gears-Installer/src/decompiler/field/instruction/FieldModelInstruction.cpp
  20. 2 2
      V-Gears-Installer/src/decompiler/field/instruction/FieldModuleInstruction.cpp
  21. 3 3
      V-Gears-Installer/src/decompiler/field/instruction/FieldPartyInstruction.cpp
  22. 0 80
      V-Gears-Installer/src/decompiler/sudm.cpp
  23. 19 17
      V-Gears-Installer/src/ff7DataInstaller.cpp

+ 7 - 2
V-Gears-Installer/CMakeLists.txt

@@ -21,11 +21,12 @@ set(HEADER_FILES
     include/common/Vram.h
     include/common/DrawSkeleton.h
     include/common/Logger.h
-    include/decompiler/sudm.h
+    include/decompiler/ScriptFormatter.h
     include/decompiler/LuaLanguage.h
     include/decompiler/CodeGenerator.h
     include/decompiler/ControlFlow.h
     include/decompiler/Disassembler.h
+    include/decompiler/Decompiler.h
     include/decompiler/Engine.h
     include/decompiler/Graph.h
     include/decompiler/Function.h
@@ -71,6 +72,8 @@ set(HEADER_FILES
     include/decompiler/field/FieldDisassembler.h
     include/decompiler/field/FieldEngine.h
     include/decompiler/field/FieldCodeGenerator.h
+    include/decompiler/field/FieldDecompiler.h
+    include/decompiler/field/FieldScriptFormatter.h
     include/decompiler/world/instruction/WorldBinaryEqualStackInstruction.h
     include/decompiler/world/instruction/WorldCondJumpInstruction.h
     include/decompiler/world/instruction/WorldKernelCallInstruction.h
@@ -101,7 +104,7 @@ set(SOURCE_FILES
     src/common/Vram.cpp
     src/common/OgreBase.cpp
     src/VGearsUtility.cpp
-    src/decompiler/sudm.cpp
+    src/decompiler/ScriptFormatter.cpp
     src/decompiler/LuaLanguage.cpp
     src/decompiler/CodeGenerator.cpp
     src/decompiler/ControlFlow.cpp
@@ -142,6 +145,8 @@ set(SOURCE_FILES
     src/decompiler/field/FieldDisassembler.cpp
     src/decompiler/field/FieldEngine.cpp
     src/decompiler/field/FieldCodeGenerator.cpp
+    src/decompiler/field/FieldDecompiler.cpp
+    src/decompiler/field/FieldScriptFormatter.cpp
     src/decompiler/world/instruction/WorldBinaryEqualStackInstruction.cpp
     src/decompiler/world/instruction/WorldCondJumpInstruction.cpp
     src/decompiler/world/instruction/WorldKernelCallInstruction.cpp

+ 24 - 0
V-Gears-Installer/include/decompiler/Decompiler.h

@@ -0,0 +1,24 @@
+/*
+ * V-Gears
+ * Copyright (C) 2022 V-Gears Team
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+/**
+ * Base class for decompilers.
+ */
+class Decompiler{};

+ 90 - 0
V-Gears-Installer/include/decompiler/ScriptFormatter.h

@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <map>
+#include <vector>
+#include <string>
+
+#include "DecompilerException.h"
+
+class ScriptFormatter{
+
+    public:
+
+        virtual ~ScriptFormatter() = default;
+
+        /**
+         * Retrieves a friendly name for a variable.
+         *
+         * @param bank[in] Variable memory bank.
+         * @param address[in] Variable memory address.
+         * @return Friendly name assigned to the variable, or an empty
+         * string if there is none.
+         */
+        virtual std::string GetFriendlyVarName(unsigned int bank, unsigned int addr);
+
+        /**
+         * Retrieves a friendly name for an entity.
+         *
+         * @param entity_name[in] Name of the entity.
+         * @return Friendly name assigned to the entity, or <entity_name>
+         * if there is none.
+         */
+        virtual std::string GetFriendlyEntityName(const std::string& entity_name);
+
+        /**
+         * Retrieves a friendly name for an animation.
+         *
+         * @param animation_id[in] ID of the animation.
+         * @return Friendly name assigned to the animation. If there is
+         * no one, the ID in string format.
+         */
+        virtual std::string GetFriendlyAnimationName(int animation_id, int id);
+
+        /**
+         * Retrieves a friendly name for a character.
+         *
+         * @param char_id[in] ID of the character.
+         * @return Friendly name assigned to the character. If there is
+         * no one, the ID in string format.
+         */
+        virtual std::string GetFriendlyCharName(int char_id);
+
+        /**
+         * Retrieves a friendly name for a function.
+         *
+         * @param entity_name[in] Name of the entity.
+         * @param function_name[in] Name of the function.
+         * @return Friendly name assigned to the entity, or
+         * <function_name> if there is none.
+         */
+        virtual std::string GetFriendlyFunctionName(
+          const std::string& entity_name, const std::string& function_name
+        );
+
+        /**
+         * Retrieves the header comment for a function in an entity.
+         *
+         * @param entity_name[in] Name of the entity.
+         * @param function_name[in] Name of the function.
+         * @return The function comment. An empty string if the entity or
+         * the function don't exist.
+         */
+        virtual std::string GetFunctionComment(
+          const std::string& entity_name, const std::string& function_name
+        );
+};

+ 7 - 7
V-Gears-Installer/include/decompiler/field/FieldCodeGenerator.h

@@ -19,8 +19,8 @@
 #include <deque>
 #include <unordered_map>
 
+#include "FieldScriptFormatter.h"
 #include "decompiler/CodeGenerator.h"
-#include "decompiler/sudm.h"
 
 namespace FF7{
 
@@ -252,7 +252,7 @@ namespace FF7{
              * address.
              */
             template<typename TValue>static const std::string FormatValueOrVariable(
-              SUDM::IScriptFormatter& formatter, uint32 bank, TValue value_or_address,
+              FieldScriptFormatter& formatter, uint32 bank, TValue value_or_address,
               ValueType type = ValueType::Integer, float scale = 1.0f
             ){
                 switch (bank){
@@ -270,7 +270,7 @@ namespace FF7{
                     case 15:
                         {
                             const auto address = static_cast<uint32>(value_or_address) & 0xFF;
-                            const auto friendly_name = formatter.VarName(bank, value_or_address);
+                            const auto friendly_name = formatter.GetFriendlyVarName(bank, value_or_address);
                             if (friendly_name.empty())
                                 return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
                             return (boost::format("FFVII.Data.%1%") % friendly_name).str();
@@ -279,7 +279,7 @@ namespace FF7{
                     case 6:
                         {
                             const auto address = static_cast<uint32>(value_or_address)& 0xFF;
-                            const  auto friendly_name = formatter.VarName(bank, address);
+                            const  auto friendly_name = formatter.GetFriendlyVarName(bank, address);
                             if (friendly_name.empty())
                                 return (boost::format("FFVII.Banks[%1%][%2%]") % bank % address).str();
                             return "FFVII.Data." + friendly_name;
@@ -303,7 +303,7 @@ namespace FF7{
              */
             FieldCodeGenerator(
               Engine *engine, const InstVec& insts, std::ostream &output,
-              SUDM::IScriptFormatter& formatter
+              FieldScriptFormatter& formatter
             ) :
               CodeGenerator(engine, output, FIFO_ARGUMENT_ORDER, LIFO_ARGUMENT_ORDER),
               insts_(insts), formatter_(formatter)
@@ -340,7 +340,7 @@ namespace FF7{
             /**
              * Retrieves the formatter.
              */
-            SUDM::IScriptFormatter& GetFormatter();
+            FieldScriptFormatter& GetFormatter();
 
 
 
@@ -412,7 +412,7 @@ namespace FF7{
             /**
              * The formatter.
              */
-            SUDM::IScriptFormatter& formatter_;
+            FieldScriptFormatter& formatter_;
 
     };
 }

+ 126 - 0
V-Gears-Installer/include/decompiler/field/FieldDecompiler.h

@@ -0,0 +1,126 @@
+/*
+ * V-Gears
+ * Copyright (C) 2022 V-Gears Team
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <string>
+#include "decompiler/Decompiler.h"
+#include "FieldScriptFormatter.h"
+
+/**
+ * Decompiler for field maps.
+ */
+class FieldDecompiler: public Decompiler{
+
+    public:
+
+        /**
+         * A line.
+         */
+        struct Line{
+
+            /**
+             * Name of the line entity.
+             */
+            std::string name;
+
+            /**
+             * First point of the line.
+             */
+            std::vector<float> point_a;
+
+            /**
+             * Second point of the line.
+             */
+            std::vector<float> point_b;
+        };
+
+        /**
+         * An entity.
+         */
+        struct FieldEntity{
+
+            /**
+             * Character identifier of the entity.
+             */
+            uint char_id;
+
+            /**
+             * Index of the entity in the field.
+             */
+            uint index;
+
+            /**
+             * Name of the entity.
+             */
+            std::string name;
+        };
+
+        /**
+         * The field decompiled script.
+         */
+        struct DecompiledScript{
+
+            /**
+             * The LUA script for the field.
+             */
+            std::string luaScript;
+
+            /**
+             * The field entities.
+             *
+             * Lines are not included.
+             */
+            std::vector<FieldEntity> entities;
+
+            /**
+             * Lines in the field.
+             */
+            std::vector<Line> lines;
+        };
+
+        /**
+         * Retrieves the scale factor of a field.
+         *
+         * @param script_bytes[in] Vector of raw byte data that makes up
+         * the script.
+         * @return The scale fctor.
+         */
+        static float ScaleFactor(const std::vector<unsigned char>& script_bytes);
+
+        /**
+         * Decompiles a field script.
+         *
+         * @param script_name[in] Name of the script to be converted,
+         * should match file name.
+         * @param script_bytes[in] Vector of raw byte data that makes up
+         * the script.
+         * @param formatter[in] Formatter used to rename variables, drop
+         * functions...
+         * @param text_after[in] Raw text that is added at to the end of
+         * the decompiled output.
+         * @param text_before[in] Raw text that is added at to the start of
+         * the decompiled output.
+         * @return A string with the decompiled script.
+         * @throws DecompilerException on failure.
+         */
+        static DecompiledScript Decompile(
+          std::string script_name, const std::vector<unsigned char>& script_bytes,
+          FieldScriptFormatter& formatter, std::string text_after = "", std::string text_before = ""
+        );
+};

+ 4 - 4
V-Gears-Installer/include/decompiler/field/FieldDisassembler.h

@@ -16,8 +16,8 @@
 #pragma once
 
 #include "decompiler/Disassembler.h"
-#include "decompiler/sudm.h"
 #include <array>
+#include "FieldScriptFormatter.h"
 
 class BinaryReader;
 class Function;
@@ -393,7 +393,7 @@ namespace FF7{
              * @param insts[in] The list of instructions.
              */
             FieldDisassembler(
-              SUDM::IScriptFormatter& formatter, FieldEngine* engine, InstVec& insts
+              FieldScriptFormatter& formatter, FieldEngine* engine, InstVec& insts
             );
 
             /**
@@ -405,7 +405,7 @@ namespace FF7{
              * @param raw_script_data[in] @todo Understand and document.
              */
             FieldDisassembler(
-              SUDM::IScriptFormatter& formatter, FieldEngine* engine,
+              FieldScriptFormatter& formatter, FieldEngine* engine,
               InstVec& insts, const std::vector<unsigned char>& raw_script_data
             );
 
@@ -778,7 +778,7 @@ namespace FF7{
             /**
              * The formatter.
              */
-            SUDM::IScriptFormatter& formatter_;
+            FieldScriptFormatter& formatter_;
 
             /**
              * The scale factor of the field.

+ 7 - 6
V-Gears-Installer/include/decompiler/field/FieldEngine.h

@@ -21,8 +21,9 @@
 #include <string>
 #include <vector>
 
-#include "../Engine.h"
-#include "decompiler/sudm.h"
+#include "FieldScriptFormatter.h"
+#include "decompiler/Engine.h"
+#include "decompiler/field/FieldDecompiler.h"
 
 namespace FF7{
 
@@ -188,7 +189,7 @@ namespace FF7{
              * @param formatter[in] The formatter to be used by the engine.
              * @param script_name[in] The script name.
              */
-            FieldEngine(SUDM::IScriptFormatter& formatter, std::string script_name);
+            FieldEngine(FieldScriptFormatter& formatter, std::string script_name);
 
             /**
              * Copy constructor, disabled.
@@ -264,14 +265,14 @@ namespace FF7{
              *
              * @return A list of non-line entities.
              */
-            std::vector<SUDM::FF7::Field::FieldEntity> GetEntityList() const;
+            std::vector<FieldDecompiler::FieldEntity> GetEntityList() const;
 
             /**
              * Retrieves all line entities in the map.
              *
              * @param A list of line entities.
              */
-            std::vector<SUDM::FF7::Field::Line> GetLineList() const;
+            std::vector<FieldDecompiler::Line> GetLineList() const;
 
             /**
              * Retrieves all entities in the map.
@@ -383,7 +384,7 @@ namespace FF7{
             /**
              * The script formatter.
              */
-            SUDM::IScriptFormatter& formatter_;
+            FieldScriptFormatter& formatter_;
 
             /**
              * The entity index map for the field.

+ 76 - 0
V-Gears-Installer/include/decompiler/field/FieldScriptFormatter.h

@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#pragma once
+
+#include <map>
+#include <vector>
+#include <string>
+
+#include "decompiler/ScriptFormatter.h"
+
+/**
+ * Formatter for field scripts.
+ */
+class FieldScriptFormatter: public ScriptFormatter{
+
+    public:
+
+        /**
+         * Adds an spawn point.
+         *
+         * If the spawn is new, a new record will be added to the spawn point
+         * database. If it was already there, update the record to add the
+         * origin.
+         *
+         * @param map_id[in] The target map ID.
+         * @param entity[in] The entity that acts as the spawn point.
+         * @param function_name[in] The spawn function name.
+         * @param address[in] @todo Understand and document.
+         * @param x[in] X coordinate of the field at which to spawn.
+         * @param y[in] Y coordinate of the field at which to spawn.
+         * @param z[in] Z coordinate of the field at which to spawn.
+         * @param angle[in] Orientation at which to spawn.
+         */
+        virtual void AddSpawnPoint(
+          unsigned int map_id, const std::string& entity, const std::string& function_name,
+          unsigned int address, int x, int y, int triangle_id, int angle
+        );
+
+        /**
+         * Retrieves the name of a spawn point.
+         *
+         * @param map_id[in] The target map ID.
+         * @param entity[in] The entity that acts as the spawn point.
+         * @param function_name[in] The spawn function name.
+         * @param address[in] @todo Understand and document.
+         * @return The name of the spawn point.
+         */
+        virtual std::string GetSpawnPointName(
+          unsigned int map_id, const std::string& entity,
+          const std::string& function_name, unsigned int address
+        );
+
+        /**
+         * Retrieves the name of a map.
+         *
+         * The name of a map is usually it's ID.
+         *
+         * @param map_id[in] The map ID.
+         * @return The map name.
+         */
+        virtual std::string GetMapName(unsigned int map_id);
+
+};

+ 0 - 244
V-Gears-Installer/include/decompiler/sudm.h

@@ -1,244 +0,0 @@
-/*
- * Copyright (C) 2022 The V-Gears Team
- *
- * This file is part of V-Gears
- *
- * V-Gears is free software: you can redistribute it and/or modify it under
- * terms of the GNU General Public License as published by the Free Software
- * Foundation, version 3.0 (GPLv3) of the License.
- *
- * V-Gears is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
-#pragma once
-
-#include <map>
-#include <vector>
-#include <string>
-
-#include "DecompilerException.h"
-
-namespace SUDM{
-
-    class IScriptFormatter{
-
-        public:
-
-            virtual ~IScriptFormatter() = default;
-
-            /**
-             * Adds an spawn point.
-             *
-             * If the spawn is new, a new record will be added to the spawn
-             * point database. If it was already there, update the record to
-             * add the origin.
-             *
-             * @param map_id[in] The target map ID.
-             * @param entity[in] The entity that acts as the spawn point.
-             * @param function_name[in] The spawn function name.
-             * @param address[in] @todo Understand and document.
-             * @param x[in] X coordinate of the field at which to spawn.
-             * @param y[in] Y coordinate of the field at which to spawn.
-             * @param z[in] Z coordinate of the field at which to spawn.
-             * @param angle[in] Orientation at which to spawn.
-             */
-            virtual void AddSpawnPoint(
-              unsigned int map_id, const std::string& entity, const std::string& function_name,
-              unsigned int address, int x, int y, int triangle_id, int angle
-            ){}
-
-            /**
-             * Retrieves the name of a spawn point.
-             *
-             * @param map_id[in] The target map ID.
-             * @param entity[in] The entity that acts as the spawn point.
-             * @param function_name[in] The spawn function name.
-             * @param address[in] @todo Understand and document.
-             * @return The name of the spawn point.
-             */
-            virtual std::string SpawnPointName(
-              unsigned int map_id, const std::string& entity,
-              const std::string& function_name, unsigned int address
-            ){
-                return MapName(map_id)
-                  + "_" + entity + "_" + function_name + "_" + std::to_string(address);
-            }
-
-            /**
-             * Retrieves the name of a map.
-             *
-             * The name of a map is usually it's ID.
-             *
-             * @param map_id[in] The map ID.
-             * @return The map name.
-             */
-            virtual std::string MapName(unsigned int map_id) { return std::to_string(map_id);}
-
-            /**
-             * Retrieves a friendly name for a variable.
-             *
-             * @param bank[in] Variable memory bank.
-             * @param address[in] Variable memory address.
-             * @return Friendly name assigned to the variable, or an empty
-             * string if there is none.
-             */
-            virtual std::string VarName(unsigned int bank, unsigned int addr){return "";}
-
-            /**
-             * Retrieves a friendly name for an entity.
-             *
-             * @param entity_name[in] Name of the entity.
-             * @return Friendly name assigned to the entity, or <entity_name>
-             * if there is none.
-             */
-            virtual std::string EntityName(const std::string& entity_name){return entity_name;}
-
-            /**
-             * Retrieves a friendly name for an animation.
-             *
-             * @param animation_id[in] ID of the animation.
-             * @return Friendly name assigned to the animation. If there is
-             * no one, the ID in string format.
-             */
-            virtual std::string AnimationName(int animation_id, int id){
-                return std::to_string(animation_id);
-            }
-
-            /**
-             * Retrieves a friendly name for a character.
-             *
-             * @param char_id[in] ID of the character.
-             * @return Friendly name assigned to the character. If there is
-             * no one, the ID in string format.
-             */
-            virtual std::string CharName(int char_id){return std::to_string(char_id);}
-
-            /**
-             * Retrieves a friendly name for a function.
-             *
-             * @param entity_name[in] Name of the entity.
-             * @param function_name[in] Name of the function.
-             * @return Friendly name assigned to the entity, or
-             * <function_name> if there is none.
-             */
-            virtual std::string FunctionName(
-              const std::string& entity_name, const std::string& function_name
-            ){return function_name;}
-
-            /**
-             * Retrieves the header comment for a function in an entity.
-             *
-             * @param entity_name[in] Name of the entity.
-             * @param function_name[in] Name of the function.
-             * @return The function comment. An empty string if the entity or
-             * the function don't exist.
-             */
-            virtual std::string FunctionComment(
-              const std::string& entity_name, const std::string& function_name
-            ){return "";}
-        };
-
-    namespace FF7{
-
-        namespace Field{
-
-            /**
-             * A line.
-             */
-            struct Line{
-
-                /**
-                 * Name of the line entity.
-                 */
-                std::string name;
-
-                /**
-                 * First point of the line.
-                 */
-                std::vector<float> point_a;
-
-                /**
-                 * Second point of the line.
-                 */
-                std::vector<float> point_b;
-            };
-
-            /**
-             * An entity.
-             */
-            struct FieldEntity{
-
-                /**
-                 * Character identifier of the entity.
-                 */
-                uint char_id;
-
-                /**
-                 * Index of the entity in the field.
-                 */
-                uint index;
-
-                /**
-                 * Name of the entity.
-                 */
-                std::string name;
-            };
-
-            /**
-             * The field decompiled script.
-             */
-            struct DecompiledScript{
-
-                /**
-                 * The LUA script for the field.
-                 */
-                std::string luaScript;
-
-                /**
-                 * The field entities.
-                 *
-                 * Lines are not included.
-                 */
-                std::vector<FieldEntity> entities;
-
-                /**
-                 * Lines in the field.
-                 */
-                std::vector<Line> lines;
-            };
-
-            /**
-             * Retrieves the scale factor of a field.
-             *
-             * @param script_bytes[in] Vector of raw byte data that makes up
-             * the script.
-             * @return The scale fctor.
-             */
-            float ScaleFactor(const std::vector<unsigned char>& script_bytes);
-
-            /**
-             * Decompiles a field script.
-             *
-             * @param script_name[in] Name of the script to be converted,
-             * should match file name.
-             * @param script_bytes[in] Vector of raw byte data that makes up
-             * the script.
-             * @param formatter[in] Formatter used to rename variables, drop
-             * functions...
-             * @param text_after[in] Raw text that is added at to the end of
-             * the decompiled output.
-             * @param text_before[in] Raw text that is added at to the start of
-             * the decompiled output.
-             * @return A string with the decompiled script.
-             * @throws DecompilerException on failure.
-             */
-            DecompiledScript Decompile(
-              std::string script_name, const std::vector<unsigned char>& script_bytes,
-              IScriptFormatter& formatter, std::string text_after = "", std::string text_before = ""
-            );
-        }
-    }
-}

+ 2 - 2
V-Gears-Installer/include/decompiler/test/ff7_field_dummy_formatter.h

@@ -1,6 +1,6 @@
-#include "sudm.h"
+#include "../ScriptFormatter.h"
 
-class DummyFormatter : public SUDM::IScriptFormatter
+class DummyFormatter : public ScriptFormatter
 {
 public:
     // Renames a variable, return empty string for generated name

+ 1 - 1
V-Gears-Installer/include/decompiler/test/main.cpp

@@ -3,6 +3,7 @@
 #include "../../common/Lzs.h"
 #include "../ControlFlow.h"
 #include "../Graph.h"
+#include "../ScriptFormatter.h"
 #include "decompiler/ff7_field/ff7_field_disassembler.h"
 #include "decompiler/ff7_field/ff7_field_engine.h"
 #include "decompiler/ff7_field/ff7_field_codegen.h"
@@ -11,7 +12,6 @@
 #include "decompiler/ff7_world/ff7_world_engine.h"
 
 #include "util.h"
-#include "sudm.h"
 #include "ff7_field_dummy_formatter.h"
 
 #define GET(vertex) (boost::get(boost::vertex_name, g, vertex))

+ 79 - 0
V-Gears-Installer/src/decompiler/ScriptFormatter.cpp

@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "decompiler/ScriptFormatter.h"
+
+/**
+ * Retrieves a friendly name for a variable.
+ *
+ * @param bank[in] Variable memory bank.
+ * @param address[in] Variable memory address.
+ * @return Friendly name assigned to the variable, or an empty
+ * string if there is none.
+ */
+std::string ScriptFormatter::GetFriendlyVarName(unsigned int bank, unsigned int addr){return "";}
+
+/**
+ * Retrieves a friendly name for an entity.
+ *
+ * @param entity_name[in] Name of the entity.
+ * @return Friendly name assigned to the entity, or <entity_name>
+ * if there is none.
+ */
+std::string ScriptFormatter::GetFriendlyEntityName(const std::string& entity_name){return entity_name;}
+
+/**
+ * Retrieves a friendly name for an animation.
+ *
+ * @param animation_id[in] ID of the animation.
+ * @return Friendly name assigned to the animation. If there is
+ * no one, the ID in string format.
+ */
+std::string ScriptFormatter::GetFriendlyAnimationName(int animation_id, int id){
+    return std::to_string(animation_id);
+}
+
+/**
+ * Retrieves a friendly name for a character.
+ *
+ * @param char_id[in] ID of the character.
+ * @return Friendly name assigned to the character. If there is
+ * no one, the ID in string format.
+ */
+std::string ScriptFormatter::GetFriendlyCharName(int char_id){return std::to_string(char_id);}
+
+/**
+ * Retrieves a friendly name for a function.
+ *
+ * @param entity_name[in] Name of the entity.
+ * @param function_name[in] Name of the function.
+ * @return Friendly name assigned to the entity, or
+ * <function_name> if there is none.
+ */
+std::string ScriptFormatter::GetFriendlyFunctionName(
+  const std::string& entity_name, const std::string& function_name
+){return function_name;}
+
+/**
+ * Retrieves the header comment for a function in an entity.
+ *
+ * @param entity_name[in] Name of the entity.
+ * @param function_name[in] Name of the function.
+ * @return The function comment. An empty string if the entity or
+ * the function don't exist.
+ */
+std::string ScriptFormatter::GetFunctionComment(
+  const std::string& entity_name, const std::string& function_name
+){return "";}

+ 6 - 5
V-Gears-Installer/src/decompiler/field/FieldCodeGenerator.cpp

@@ -223,7 +223,7 @@ void FF7::FieldCodeGenerator::OnBeforeStartFunction(const Function& function){
         if (meta_data.GetCharacterId() != -1)
             AddOutputLine(meta_data.GetEntityName() + " = nil,\n");
     }
-    const auto comment = formatter_.FunctionComment(meta_data.GetEntityName(), function.name);
+    const auto comment = formatter_.GetFunctionComment(meta_data.GetEntityName(), function.name);
     if (!comment.empty()) AddOutputLine("-- " + comment);
 }
 
@@ -237,9 +237,9 @@ void FF7::FieldCodeGenerator::OnStartFunction(const Function& func){
         }
     }
     AddOutputLine("]]\n");
-    // TODO: If this hack is needed, maybe it can just be added to the "Direcor" entity.
+    // TODO: If this hack is needed, maybe it can just be added to the "Director" entity.
     if (func.name == "on_start" || func.name == "init"){
-        AddOutputLine("-- HACK ensure camera follows cloud, fix in engine properly later");
+        AddOutputLine("-- HACK ensure camera follows cloud, fix in engine.");
         AddOutputLine("background2d:autoscroll_to_entity(entity_manager:get_entity(\"Cloud\"))");
     }
 }
@@ -256,12 +256,13 @@ void FF7::FieldCodeGenerator::OnEndFunction(const Function& function){
 std::string FF7::FieldCodeGenerator::ConstructFuncSignature(const Function &function){
     // Generate name
     FunctionMetaData meta_data(function.metadata);
-    return formatter_.FunctionName(meta_data.GetEntityName(), function.name) + " = function(self)";
+    return formatter_.GetFriendlyFunctionName(meta_data.GetEntityName(), function.name)
+      + " = function(self)";
 }
 
 bool FF7::FieldCodeGenerator::OutputOnlyRequiredLabels() const{return true;}
 
-SUDM::IScriptFormatter& FF7::FieldCodeGenerator::GetFormatter(){return formatter_;}
+FieldScriptFormatter& FF7::FieldCodeGenerator::GetFormatter(){return formatter_;}
 
 const std::string FF7::FieldCodeGenerator::FormatInstructionNotImplemented(
   const std::string& entity, uint32 address, uint32 opcode

+ 57 - 0
V-Gears-Installer/src/decompiler/field/FieldDecompiler.cpp

@@ -0,0 +1,57 @@
+/*
+ * V-Gears
+ * Copyright (C) 2022 V-Gears Team
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "decompiler/field/FieldDecompiler.h"
+#include "decompiler/field/FieldEngine.h"
+#include "decompiler/ControlFlow.h"
+
+float FieldDecompiler::ScaleFactor(const std::vector<unsigned char>& script_bytes){
+    // Could be cleaner, but just does enough work to pull out the fields scale.
+    FieldScriptFormatter formatter;
+    ::FF7::FieldEngine engine(formatter, "Unused");
+    InstVec insts;
+    engine.GetDisassembler(insts, script_bytes);
+    return engine.GetScaleFactor();
+}
+
+FieldDecompiler::DecompiledScript FieldDecompiler::Decompile(
+  std::string script_name, const std::vector<unsigned char>& script_bytes,
+  FieldScriptFormatter& formatter, std::string text_after, std::string text_before
+){
+    // Disassemble the script.
+    ::FF7::FieldEngine engine(formatter, script_name);
+    InstVec insts;
+    auto disassembler = engine.GetDisassembler(insts, script_bytes);
+    disassembler->Disassemble();
+    // Create control flow group.
+    auto control_flow = std::make_unique<ControlFlow>(insts, engine);
+    control_flow->CreateGroups();
+    // Decompile/analyze
+    //Graph graph = controlFlow->analyze();
+    //engine.PostCFG(insts, graph);
+    Graph graph;
+    DecompiledScript ds;
+    // Generate code and return it.
+    std::stringstream output;
+    auto cg = engine.GetCodeGenerator(insts, output);
+    cg->Generate(insts, graph);
+    ds.luaScript = text_before + output.str() + text_after;
+    for (auto entity : engine.GetEntityList()) ds.entities.push_back(entity);
+    for (auto line : engine.GetLineList()) ds.lines.push_back(line);
+    return ds;
+}

+ 3 - 3
V-Gears-Installer/src/decompiler/field/FieldDisassembler.cpp

@@ -43,7 +43,7 @@ const int FF7::FieldDisassembler::MAGIC(0x0502);
 const int FF7::FieldDisassembler::NUM_SECTIONS(7);
 
 FF7::FieldDisassembler::FieldDisassembler(
-  SUDM::IScriptFormatter& formatter, FieldEngine* engine,
+  FieldScriptFormatter& formatter, FieldEngine* engine,
   InstVec& insts, const std::vector<unsigned char>& raw_script_data
 ): Disassembler(insts), engine_(engine), formatter_(formatter){
     loaded_from_raw_data_ = true;
@@ -55,7 +55,7 @@ FF7::FieldDisassembler::FieldDisassembler(
 }
 
 FF7::FieldDisassembler::FieldDisassembler(
-  SUDM::IScriptFormatter& formatter, FieldEngine *engine, InstVec &insts
+  FieldScriptFormatter& formatter, FieldEngine *engine, InstVec &insts
 ): Disassembler(insts), engine_(engine), formatter_(formatter){
     section_pointers_size_ = (sizeof(uint32) * NUM_SECTIONS);
 }
@@ -123,7 +123,7 @@ void FF7::FieldDisassembler::DoDisassemble(){
         std::string original_name = header_.field_entity_names[entity_number].data();
         // If the entity name was blank in the file then use a consistent generated name
         if (original_name.empty()) original_name = "entity_" + std::to_string(entity_number);
-        const std::string entity_name = formatter_.EntityName(original_name);
+        const std::string entity_name = formatter_.GetFriendlyEntityName(original_name);
         // Only parse each script one.
         std::set<uint16> parsed_scripts;
         std::vector<ScriptInfo> script_info;

+ 7 - 7
V-Gears-Installer/src/decompiler/field/FieldEngine.cpp

@@ -101,7 +101,7 @@ std::vector<float> FF7::FieldEngine::Entity::GetLinePointA(){return point_a_;}
 
 std::vector<float> FF7::FieldEngine::Entity::GetLinePointB(){return point_b_;}
 
-FF7::FieldEngine::FieldEngine(SUDM::IScriptFormatter& formatter, std::string script_name) :
+FF7::FieldEngine::FieldEngine(FieldScriptFormatter& formatter, std::string script_name) :
   formatter_(formatter), script_name_(script_name), scale_factor_(1.0f)
 {SetOutputStackEffect(false);}
 
@@ -161,11 +161,11 @@ std::map<std::string, int> FF7::FieldEngine::GetEntities() const{
     return r;
 }
 
-std::vector<SUDM::FF7::Field::FieldEntity> FF7::FieldEngine::GetEntityList() const{
-    std::vector<SUDM::FF7::Field::FieldEntity> entities;
+std::vector<FieldDecompiler::FieldEntity> FF7::FieldEngine::GetEntityList() const{
+    std::vector<FieldDecompiler::FieldEntity> entities;
     for (auto entity: entity_index_map_){
         if (entity.second.IsLine() == false){
-            SUDM::FF7::Field::FieldEntity ent;
+            FieldDecompiler::FieldEntity ent;
             ent.name = entity.second.GetName();
             ent.index = entity.second.GetIndex();
 
@@ -186,11 +186,11 @@ std::vector<SUDM::FF7::Field::FieldEntity> FF7::FieldEngine::GetEntityList() con
     return entities;
 }
 
-std::vector<SUDM::FF7::Field::Line> FF7::FieldEngine::GetLineList() const{
-    std::vector<SUDM::FF7::Field::Line> lines;
+std::vector<FieldDecompiler::Line> FF7::FieldEngine::GetLineList() const{
+    std::vector<FieldDecompiler::Line> lines;
     for (auto entity: entity_index_map_){
         if (entity.second.IsLine() == true){
-            SUDM::FF7::Field::Line line;
+            FieldDecompiler::Line line;
             line.name = entity.second.GetName();
             line.point_a = entity.second.GetLinePointA();
             line.point_b = entity.second.GetLinePointB();

+ 48 - 0
V-Gears-Installer/src/decompiler/field/FieldScriptFormatter.cpp

@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2022 The V-Gears Team
+ *
+ * This file is part of V-Gears
+ *
+ * V-Gears is free software: you can redistribute it and/or modify it under
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, version 3.0 (GPLv3) of the License.
+ *
+ * V-Gears is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include "decompiler/field/FieldScriptFormatter.h"
+
+void FieldScriptFormatter::AddSpawnPoint(
+  unsigned int map_id, const std::string& entity, const std::string& function_name,
+  unsigned int address, int x, int y, int triangle_id, int angle
+){}
+
+/**
+ * Retrieves the name of a spawn point.
+ *
+ * @param map_id[in] The target map ID.
+ * @param entity[in] The entity that acts as the spawn point.
+ * @param function_name[in] The spawn function name.
+ * @param address[in] @todo Understand and document.
+ * @return The name of the spawn point.
+ */
+std::string FieldScriptFormatter::GetSpawnPointName(
+  unsigned int map_id, const std::string& entity,
+  const std::string& function_name, unsigned int address
+){
+    return GetMapName(map_id)
+      + "_" + entity + "_" + function_name + "_" + std::to_string(address);
+}
+
+/**
+ * Retrieves the name of a map.
+ *
+ * The name of a map is usually it's ID.
+ *
+ * @param map_id[in] The map ID.
+ * @return The map name.
+ */
+std::string FieldScriptFormatter::GetMapName(unsigned int map_id) { return std::to_string(map_id);}

+ 3 - 3
V-Gears-Installer/src/decompiler/field/instruction/FieldControlFlowInstruction.cpp

@@ -61,7 +61,7 @@ void FF7::FF7ControlFlowInstruction::ProcessREQ(
 ){
     FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
     const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
-    const auto& script_name = cg->GetFormatter().FunctionName(
+    const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
       entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
     );
     auto priority = params_[1]->GetUnsigned();
@@ -76,7 +76,7 @@ void FF7::FF7ControlFlowInstruction::ProcessREQSW(
 ){
     FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
     const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
-    const auto& script_name = cg->GetFormatter().FunctionName(
+    const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
       entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
     );
     auto priority = params_[1]->GetUnsigned();
@@ -92,7 +92,7 @@ void FF7::FF7ControlFlowInstruction::ProcessREQEW(
     try{
         FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
         const auto& entity = engine.EntityByIndex(params_[0]->GetSigned());
-        const auto& script_name = cg->GetFormatter().FunctionName(
+        const auto& script_name = cg->GetFormatter().GetFriendlyFunctionName(
           entity.GetName(), entity.FunctionByIndex(params_[2]->GetUnsigned())
         );
         auto priority = params_[1]->GetUnsigned();

+ 6 - 6
V-Gears-Installer/src/decompiler/field/instruction/FieldModelInstruction.cpp

@@ -214,11 +214,11 @@ void FF7::FieldModelInstruction::ProcessDFANM(
     auto speed = 1.0f / params_[1]->GetUnsigned();
     code_gen->AddOutputLine((
       boost::format("self.%1%:set_default_animation(\"%2%\") -- speed %3%")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id) % speed
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id) % speed
     ).str());
     code_gen->AddOutputLine((
       boost::format("self.%1%:play_animation(\"%2%\")")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id)
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id)
     ).str());
 }
 
@@ -232,7 +232,7 @@ void FF7::FieldModelInstruction::ProcessANIME1(
     auto speed = 1.0f / params_[1]->GetUnsigned();
     code_gen->AddOutputLine((
       boost::format("self.%1%:play_animation(\"%2%\") -- speed %3%")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id) % speed
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id) % speed
     ).str());
     code_gen->AddOutputLine((boost::format("self.%1%:animation_sync()") % entity).str());
 }
@@ -359,7 +359,7 @@ void FF7::FieldModelInstruction::ProcessANIM_2(
     auto speed = 1.0f / params_[1]->GetUnsigned();
     code_gen->AddOutputLine((
       boost::format("self.%1%:play_animation_stop(\"%2%\") -- speed %3%")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id) % speed
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id) % speed
     ).str());
     code_gen->AddOutputLine((boost::format("self.%1%:animation_sync()") % entity).str());
 }
@@ -375,7 +375,7 @@ void FF7::FieldModelInstruction::ProcessCANIM2(
     auto speed = 1.0f / params_[3]->GetUnsigned();
     code_gen->AddOutputLine((
       boost::format("self.%1%:play_animation(\"%2%\", %3%, %4%) -- speed %5%")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id)
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id)
       % start_frame % end_frame % speed
     ).str());
     code_gen->AddOutputLine((boost::format("self.%1%:animation_sync()") % entity).str());
@@ -393,7 +393,7 @@ void FF7::FieldModelInstruction::ProcessCANM_2(
     auto speed = 1.0f / params_[3]->GetUnsigned();
     code_gen->AddOutputLine((
       boost::format("self.%1%:play_animation_stop(\"%2%\", %3%, %4%) -- speed %5%")
-      % entity % cg->GetFormatter().AnimationName(char_id, animation_id)
+      % entity % cg->GetFormatter().GetFriendlyAnimationName(char_id, animation_id)
       % start_frame % end_frame % speed
     ).str());
     code_gen->AddOutputLine((boost::format("self.%1%:animation_sync()") % entity).str());

+ 2 - 2
V-Gears-Installer/src/decompiler/field/instruction/FieldModuleInstruction.cpp

@@ -120,7 +120,7 @@ void FF7::FieldModuleInstruction::ProcessMAPJUMP(CodeGenerator* code_gen, Functi
     FieldCodeGenerator* cg = static_cast<FieldCodeGenerator*>(code_gen);
     const auto target_map_id = params_[0]->GetUnsigned();
     FunctionMetaData md(func.metadata);
-    const std::string source_spawn_point_name = cg->GetFormatter().SpawnPointName(
+    const std::string source_spawn_point_name = cg->GetFormatter().GetSpawnPointName(
       target_map_id, md.GetEntityName(), func.name, address_
     );
     cg->GetFormatter().AddSpawnPoint(
@@ -130,7 +130,7 @@ void FF7::FieldModuleInstruction::ProcessMAPJUMP(CodeGenerator* code_gen, Functi
       params_[3]->GetSigned(), // Walk mesh triangle ID
       params_[4]->GetSigned()  // Angle
     );
-    const std::string target_map_name = cg->GetFormatter().MapName(target_map_id);
+    const std::string target_map_name = cg->GetFormatter().GetMapName(target_map_id);
     code_gen->AddOutputLine(
       "load_field_map_request(\"" + target_map_name + "\", \"" + source_spawn_point_name + "\")"
     );

+ 3 - 3
V-Gears-Installer/src/decompiler/field/instruction/FieldPartyInstruction.cpp

@@ -75,11 +75,11 @@ void FF7::FieldPartyInstruction::ProcessSTITM(CodeGenerator* code_gen){
 
 void FF7::FieldPartyInstruction::ProcessPRTYE(CodeGenerator* code_gen){
     FieldCodeGenerator* gc = static_cast<FieldCodeGenerator*>(code_gen);
-    auto char_id_1 = gc->GetFormatter().CharName(params_[0]->GetUnsigned());
+    auto char_id_1 = gc->GetFormatter().GetFriendlyCharName(params_[0]->GetUnsigned());
     char_id_1 = (char_id_1 == "") ? "nil" : ("\"" + char_id_1 + "\"");
-    auto char_id_2 = gc->GetFormatter().CharName(params_[1]->GetUnsigned());
+    auto char_id_2 = gc->GetFormatter().GetFriendlyCharName(params_[1]->GetUnsigned());
     char_id_2 = (char_id_2 == "") ? "nil" : ("\"" + char_id_2 + "\"");
-    auto char_id_3 = gc->GetFormatter().CharName(params_[2]->GetUnsigned());
+    auto char_id_3 = gc->GetFormatter().GetFriendlyCharName(params_[2]->GetUnsigned());
     char_id_3 = (char_id_3 == "") ? "nil" : ("\"" + char_id_3 + "\"");
     code_gen->AddOutputLine((
       boost::format("FFVII.set_party(%1%, %2%, %3%)") % char_id_1 % char_id_2 % char_id_3

+ 0 - 80
V-Gears-Installer/src/decompiler/sudm.cpp

@@ -1,80 +0,0 @@
-/*
- * Copyright (C) 2022 The V-Gears Team
- *
- * This file is part of V-Gears
- *
- * V-Gears is free software: you can redistribute it and/or modify it under
- * terms of the GNU General Public License as published by the Free Software
- * Foundation, version 3.0 (GPLv3) of the License.
- *
- * V-Gears is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
-#include "decompiler/sudm.h"
-
-#include "../../include/decompiler/ControlFlow.h"
-#include "../../include/decompiler/field/FieldCodeGenerator.h"
-#include "../../include/decompiler/field/FieldDisassembler.h"
-#include "../../include/decompiler/field/FieldEngine.h"
-
-namespace SUDM{
-    namespace FF7{
-        namespace Animation{} // TODO
-
-        namespace AI{} // TODO
-
-        namespace World{} // TODO
-
-        namespace Field{
-
-            float ScaleFactor(const std::vector<unsigned char>& script_bytes){
-                // Could be cleaner, but just does enough work to pull out the fields scale.
-                IScriptFormatter formatter;
-                ::FF7::FieldEngine engine(formatter, "Unused");
-                InstVec insts;
-                engine.GetDisassembler(insts, script_bytes);
-                return engine.GetScaleFactor();
-            }
-
-            DecompiledScript Decompile(
-              std::string script_name, const std::vector<unsigned char>& script_bytes,
-              IScriptFormatter& formatter, std::string text_after, std::string text_before
-            ){
-                // Disassemble the script.
-                ::FF7::FieldEngine engine(formatter, script_name);
-                InstVec insts;
-                auto disassembler = engine.GetDisassembler(insts, script_bytes);
-                disassembler->Disassemble();
-                // Create control flow group.
-                auto control_flow = std::make_unique<ControlFlow>(insts, engine);
-                control_flow->CreateGroups();
-                // Decompile/analyze
-                //Graph graph = controlFlow->analyze();
-                //engine.PostCFG(insts, graph);
-                Graph graph;
-                DecompiledScript ds;
-                // Generate code and return it.
-                std::stringstream output;
-                auto cg = engine.GetCodeGenerator(insts, output);
-                cg->Generate(insts, graph);
-                ds.luaScript = text_before + output.str() + text_after;
-                for (auto entity : engine.GetEntityList()) ds.entities.push_back(entity);
-                for (auto line : engine.GetLineList()) ds.lines.push_back(line);
-                return ds;
-            }
-        }
-    }
-
-    namespace FF8{
-
-        namespace Field{} // TODO
-    }
-
-    namespace FF9{
-
-        namespace Field{} // TODO
-    }
-}

+ 19 - 17
V-Gears-Installer/src/ff7DataInstaller.cpp

@@ -48,10 +48,12 @@
 #include "common/FinalFantasy7/FF7NameLookup.h"
 #include "data/VGearsTexCodec.h"
 #include "data/VGearsMapListFile.h"
-#include "decompiler/sudm.h"
 #include <memory>
 #include <QtCore/QDir>
 
+#include "decompiler/field/FieldScriptFormatter.h"
+#include "decompiler/field/FieldDecompiler.h"
+
 float FF7DataInstaller::LINE_SCALE_FACTOR = 0.0078124970964f;
 
 FF7DataInstaller::FF7DataInstaller(
@@ -304,7 +306,7 @@ static std::string FieldMapDir(){return "maps/ffvii/field";}
 /**
  * Utilities for formatting field scripts.
  */
-class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
+class BaseFF7FieldScriptFormatter : public FieldScriptFormatter{
 
     public:
 
@@ -335,7 +337,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @param address[in] @todo Understand and document.
          * @return The composed spawn point name.
          */
-        virtual std::string SpawnPointName(
+        virtual std::string GetSpawnPointName(
           unsigned int map_id, const std::string& entity,
           const std::string& function_name, unsigned int address
         ) override{
@@ -349,7 +351,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @param[in] The map ID.
          * @return The map name.
          */
-        virtual std::string MapName(unsigned int map_id) override{
+        virtual std::string GetMapName(unsigned int map_id) override{
             return FieldName(field_id_to_name_lookup_[map_id]);
         }
 
@@ -361,7 +363,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @return Friendly name for the variable, or an empty string if it
          * doesn't have one assigned.
          */
-        virtual std::string VarName(
+        virtual std::string GetFriendlyVarName(
           unsigned int bank, unsigned int addr
         ) override{
             return VGears::FF7::NameLookup::FieldScriptVarName(bank, addr);
@@ -374,7 +376,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @return Friendly name for the entity, or the current name if it
          * doesn't have one assigned.
          */
-        virtual std::string EntityName(const std::string& entity) override{
+        virtual std::string GetFriendlyEntityName(const std::string& entity) override{
             return VGears::FF7::NameLookup::FieldScriptEntityName(entity);
         }
 
@@ -385,7 +387,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @return Friendly name for the entity, or an empty string if it
          * doesn't have one assigned.
          */
-        virtual std::string CharName(int char_id) override{
+        virtual std::string GetFriendlyCharName(int char_id) override{
             return VGears::FF7::NameLookup::CharName(char_id);
         }
 
@@ -397,7 +399,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @return Friendly name for the function, or the current name if it
          * doesn't have one assigned.
          */
-        virtual std::string FunctionName(
+        virtual std::string GetFriendlyFunctionName(
           const std::string& entity, const std::string& function_name
         ) override{
             return VGears::FF7::NameLookup::FieldScriptFunctionName(
@@ -413,7 +415,7 @@ class BaseFF7FieldScriptFormatter : public SUDM::IScriptFormatter{
          * @return Friendly comment for the function, or an empty string if it
          * doesn't have one assigned.
          */
-        virtual std::string FunctionComment(
+        virtual std::string GetFunctionComment(
           const std::string& entity, const std::string& function_name
         ) override{
             return VGears::FF7::NameLookup::FieldScriptFunctionComment(
@@ -472,7 +474,7 @@ class FF7FieldScriptFormatter : public BaseFF7FieldScriptFormatter{
          * @return The animation name. If there is no name assigned, or it
          * can't be found, a string with the animation ID.
          */
-        virtual std::string AnimationName(int char_id, int id) override{
+        virtual std::string GetFriendlyAnimationName(int char_id, int id) override{
             // Get the animation file name, then look up the friendly name of
             // the "raw" animation.
             if (static_cast<unsigned int>(char_id) >= model_loader_->GetModels().size()){
@@ -625,13 +627,13 @@ static void FF7PcFieldToVGearsField(
         }
     }
     const VGears::ModelListFilePtr& models = field->GetModelList();
-    SUDM::FF7::Field::DecompiledScript decompiled;
+    FieldDecompiler::DecompiledScript decompiled;
     FF7FieldScriptFormatter formatter(field->getName(), models, field_id_to_name_lookup, spawn_map);
     try{
         // Get the raw script bytes.
         const std::vector<u8> raw_field_data = field->GetRawScript();
         // Decompile to LUA.
-        decompiled = SUDM::FF7::Field::Decompile(
+        decompiled = FieldDecompiler::Decompile(
           field->getName(), raw_field_data, formatter, gateway_script_data,
           "EntityContainer = {}\n\n"
         );
@@ -768,7 +770,7 @@ static void FF7PcFieldToVGearsField(
 
         // Get lines. Add them to a list, so they aren't processed later as regular entities.
         std::vector<std::string> line_entities;
-        for (SUDM::FF7::Field::Line line : decompiled.lines){
+        for (FieldDecompiler::Line line : decompiled.lines){
 
             std::unique_ptr<TiXmlElement> xml_entity_trigger(new TiXmlElement("entity_trigger"));
             line_entities.push_back(line.name);
@@ -790,7 +792,7 @@ static void FF7PcFieldToVGearsField(
         }
 
         // Get entities.
-        for (SUDM::FF7::Field::FieldEntity entity : decompiled.entities){
+        for (FieldDecompiler::FieldEntity entity : decompiled.entities){
             // If the entity has been added as a line, skip.
             if (line_entities.size() > 0){
                 if (*std::find(line_entities.begin(), line_entities.end(), entity.name) == entity.name) {
@@ -1098,13 +1100,13 @@ static void CollectSpawnPoints(
     // full list of entries to each field.
     try{
         // Get the raw script bytes.
-        SUDM::FF7::Field::DecompiledScript decompiled;
+        FieldDecompiler::DecompiledScript decompiled;
         const std::vector<u8> raw_field_data = field->GetRawScript();
         // Decompile to LUA.
         FF7FieldScriptGatewayCollector formatter(
           field->getName(), field_id_to_name_lookup, spawn_points, this_field_id
         );
-        decompiled = SUDM::FF7::Field::Decompile(
+        decompiled = FieldDecompiler::Decompile(
           field->getName(), raw_field_data, formatter, "", "EntityContainer = {}\n\n"
         );
     }
@@ -1231,7 +1233,7 @@ static void CollectFieldScaleFactors(
   const std::vector<std::string>& field_id_to_name_lookup
 ){
     scale_factors[FieldId(field->getName(), field_id_to_name_lookup)]
-      = ::SUDM::FF7::Field::ScaleFactor(field->GetRawScript());
+      = FieldDecompiler::ScaleFactor(field->GetRawScript());
 }
 
 void FF7DataInstaller::InitCollectSpawnAndScaleFactors(){