sudm.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "sudm.h"
  2. #include "decompiler/ff7_field/ff7_field_engine.h"
  3. #include "decompiler/ff7_field/ff7_field_disassembler.h"
  4. #include "decompiler/ff7_field/ff7_field_codegen.h"
  5. #include "decompiler/control_flow.h"
  6. namespace SUDM
  7. {
  8. namespace FF7
  9. {
  10. namespace Animation
  11. {
  12. // TODO
  13. }
  14. namespace AI
  15. {
  16. // TODO
  17. }
  18. namespace World
  19. {
  20. // TODO
  21. }
  22. namespace Field
  23. {
  24. float ScaleFactor(const std::vector<unsigned char>& scriptBytes)
  25. {
  26. // Could be cleaner, but just does enough work to pull out the fields scale
  27. IScriptFormatter formatter;
  28. ::FF7::FF7FieldEngine engine(formatter, "Unused");
  29. InstVec insts;
  30. engine.getDisassembler(insts, scriptBytes);
  31. return engine.ScaleFactor();
  32. }
  33. DecompiledScript Decompile(std::string scriptName,
  34. const std::vector<unsigned char>& scriptBytes,
  35. IScriptFormatter& formatter,
  36. std::string textToAppend,
  37. std::string textToPrepend)
  38. {
  39. // Disassemble the script
  40. ::FF7::FF7FieldEngine engine(formatter, scriptName);
  41. InstVec insts;
  42. auto disassembler = engine.getDisassembler(insts, scriptBytes);
  43. disassembler->disassemble();
  44. //disassembler->dumpDisassembly(std::cout);
  45. // Check if the script contains the opcode LINE.
  46. /*bool is_line = false;
  47. for(InstPtr inst : insts){
  48. if ("LINE" == inst->_name){
  49. std::cout << " INSTRUCTION: " << inst->_name << " (" << scriptName << ")" << std::endl;
  50. disassembler->dumpDisassembly(std::cout);
  51. std::cout << " INSTRUCTION END" << std::endl;
  52. is_line = true;
  53. break;
  54. }
  55. }*/
  56. // Create CFG
  57. auto controlFlow = std::make_unique<ControlFlow>(insts, engine);
  58. controlFlow->createGroups();
  59. // Decompile/analyze
  60. //Graph graph = controlFlow->analyze();
  61. //engine.postCFG(insts, graph);
  62. Graph graph;
  63. DecompiledScript ds;
  64. // Generate code and return it
  65. std::stringstream output;
  66. auto cg = engine.getCodeGenerator(insts, output);
  67. cg->generate(insts, graph);
  68. ds.luaScript = textToPrepend + output.str() + textToAppend;
  69. ds.entities = engine.GetEntities();
  70. for (auto line : engine.GetLineList()){
  71. ds.lines.push_back(line);
  72. }
  73. ds.lines = engine.GetLineList();
  74. return ds;
  75. }
  76. }
  77. }
  78. namespace FF8
  79. {
  80. namespace Field
  81. {
  82. // TODO
  83. }
  84. }
  85. namespace FF9
  86. {
  87. namespace Field
  88. {
  89. // TODO
  90. }
  91. }
  92. }