sudm.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "sudm.h"
  16. #include "decompiler/ff7_field/ff7_field_engine.h"
  17. #include "decompiler/ff7_field/ff7_field_disassembler.h"
  18. #include "decompiler/ff7_field/ff7_field_codegen.h"
  19. #include "decompiler/control_flow.h"
  20. namespace SUDM{
  21. namespace FF7{
  22. namespace Animation{} // TODO
  23. namespace AI{} // TODO
  24. namespace World{} // TODO
  25. namespace Field{
  26. float ScaleFactor(const std::vector<unsigned char>& script_bytes){
  27. // Could be cleaner, but just does enough work to pull out the fields scale.
  28. IScriptFormatter formatter;
  29. ::FF7::FF7FieldEngine engine(formatter, "Unused");
  30. InstVec insts;
  31. engine.getDisassembler(insts, script_bytes);
  32. return engine.ScaleFactor();
  33. }
  34. DecompiledScript Decompile(
  35. std::string script_name, const std::vector<unsigned char>& script_bytes,
  36. IScriptFormatter& formatter, std::string text_after, std::string text_before
  37. ){
  38. // Disassemble the script.
  39. ::FF7::FF7FieldEngine engine(formatter, script_name);
  40. InstVec insts;
  41. auto disassembler = engine.getDisassembler(insts, script_bytes);
  42. disassembler->disassemble();
  43. // Create control flow group.
  44. auto control_flow = std::make_unique<ControlFlow>(insts, engine);
  45. control_flow->createGroups();
  46. // Decompile/analyze
  47. //Graph graph = controlFlow->analyze();
  48. //engine.postCFG(insts, graph);
  49. Graph graph;
  50. DecompiledScript ds;
  51. // Generate code and return it.
  52. std::stringstream output;
  53. auto cg = engine.getCodeGenerator(insts, output);
  54. cg->generate(insts, graph);
  55. ds.luaScript = text_before + output.str() + text_after;
  56. for (auto entity : engine.GetEntityList()) ds.entities.push_back(entity);
  57. for (auto line : engine.GetLineList()) ds.lines.push_back(line);
  58. return ds;
  59. }
  60. }
  61. }
  62. namespace FF8{
  63. namespace Field{} // TODO
  64. }
  65. namespace FF9{
  66. namespace Field{} // TODO
  67. }
  68. }