| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- #include "ff7_world_engine.h"
- #include "ff7_world_disassembler.h"
- #include "ff7_world_codegen.h"
- #include <iostream>
- #include <sstream>
- #include <boost/format.hpp>
- #define GET(vertex) (boost::get(boost::vertex_name, g, vertex))
- std::unique_ptr<Disassembler> FF7::FF7WorldEngine::GetDisassembler(InstVec &insts)
- {
- return std::make_unique<FF7WorldDisassembler>(this, insts, mScriptNumber);
- }
- std::unique_ptr<CodeGenerator> FF7::FF7WorldEngine::GetCodeGenerator(const InstVec& /*insts*/, std::ostream &output)
- {
- return std::make_unique<FF7WorldCodeGenerator>(this, output);
- }
- void FF7::FF7WorldEngine::PostCFG(InstVec&, Graph g)
- {
- /*
- VertexRange vr = boost::vertices(g);
- for (VertexIterator v = vr.first; v != vr.second; ++v)
- {
- GroupPtr gr = GET(*v);
- // If this group is the last instruction and its an unconditional jump
- if ((*gr->_start)->_address == insts.back()->_address && insts.back()->IsUncondJump())
- {
- // Then assume its an infinite do { } while(true) loop that wraps part of the script
- gr->_type = kDoWhileCondGroupType;
- }
- }*/
- }
- void FF7::FF7WorldEngine::getVariants(std::vector<std::string>&) const
- {
- }
- void FF7::FF7WorldLoadBankInstruction::ProcessInst(Function&, ValueStack &stack, Engine*, CodeGenerator*)
- {
- stack.push(new BankValue("Read(" + _params[0]->getString() + ")"));
- }
- void FF7::FF7WorldLoadInstruction::ProcessInst(Function&, ValueStack &stack, Engine*, CodeGenerator*)
- {
- stack.push(new VarValue(_params[0]->getString()));
- }
- void FF7::FF7SubStackInstruction::ProcessInst(Function&, ValueStack &stack, Engine*, CodeGenerator*)
- {
- std::string op;
- switch (_opcode)
- {
- case 0x41:
- op = "-";
- break;
- case 0x40:
- op = "+";
- break;
- case 0x80:
- op = "&&";
- break;
- case 0xa0:
- op = "||";
- break;
- case 0xc0:
- op = "|";
- break;
- case 0x15: // neg
- case 0x17: // not
- stack.push(stack.pop()->negate());
- return;
- case 0x30:
- op = "*";
- break;
- case 0x60:
- op = "<";
- break;
- case 0x61:
- op = ">";
- break;
- case 0x62:
- op = "<=";
- break;
- case 0x63:
- op = ">=";
- break;
- case 0x0b0:
- op = "&";
- break;
- case 0x51:
- op = "<<";
- break;
- default:
- op = "unknown_operation";
- }
- std::string strValue = stack.pop()->getString() + " " + op + " " + stack.pop()->getString();
- stack.push(new VarValue(strValue));
- }
- void FF7::FF7WorldStoreInstruction::ProcessInst(Function&, ValueStack &stack, Engine*, CodeGenerator *codeGen)
- {
- std::string strValue = stack.pop()->getString();
- // If the bank address is from a load bank instruction, then we only want
- // the bank address, not whats *at* the bank address
- ValuePtr bankValue = stack.pop();
- std::string bankAddr = bankValue->getString();
- codeGen->AddOutputLine("Write(" + bankAddr + ", " + strValue + ");");
- }
- void FF7::FF7WorldStackInstruction::ProcessInst(Function&, ValueStack&, Engine*, CodeGenerator*)
- {
- }
- void FF7::FF7WorldCondJumpInstruction::ProcessInst(Function&, ValueStack&, Engine*, CodeGenerator*)
- {
- }
- uint32 FF7::FF7WorldCondJumpInstruction::GetDestAddress() const
- {
- return 0x200 + _params[0]->getUnsigned();
- }
- std::ostream& FF7::FF7WorldCondJumpInstruction::print(std::ostream &output) const
- {
- Instruction::print(output);
- return output;
- }
- bool FF7::FF7WorldUncondJumpInstruction::IsFuncCall() const
- {
- return _isCall;
- }
- bool FF7::FF7WorldUncondJumpInstruction::IsUncondJump() const
- {
- return !_isCall;
- }
- uint32 FF7::FF7WorldUncondJumpInstruction::GetDestAddress() const
- {
- // the world map while loops are incorrect without +1'in this, but
- // doing that will break some if elses.. hmm
- return 0x200 + _params[0]->getUnsigned();// +1; // TODO: +1 to skip param?
- }
- std::ostream& FF7::FF7WorldUncondJumpInstruction::print(std::ostream &output) const
- {
- Instruction::print(output);
- // output << " (Jump target address: 0x" << std::hex << GetDestAddress() << std::dec << ")";
- return output;
- }
- void FF7::FF7WorldUncondJumpInstruction::ProcessInst(Function& , ValueStack&, Engine*, CodeGenerator*)
- {
- }
- void FF7::FF7WorldKernelCallInstruction::ProcessInst(Function& , ValueStack &stack, Engine*, CodeGenerator *codeGen)
- {
- std::string strFunc;
- switch (_opcode)
- {
- case 0x203:
- strFunc = "return;";
- break;
- case 0x317:
- strFunc = "TriggerBattle(" + stack.pop()->getString() + ");";
- break;
- case 0x324:
- // x, y, w, h
- strFunc = "SetWindowDimensions(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x32D:
- strFunc = "WaitForWindowReady();";
- break;
- case 0x325:
- strFunc = "SetWindowMessage(" + stack.pop()->getString() + ");";
- break;
- case 0x333:
- strFunc = "Unknown333(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x308:
- strFunc = "SetActiveEntityMeshCoordinates(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x309:
- strFunc = "SetActiveEntityMeshCoordinatesInMesh(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x32e:
- strFunc = "WaitForMessageAcknowledge();";
- break;
- case 0x32c:
- // Mode, Permanency
- strFunc = "SetWindowParameters(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x318:
- strFunc = "EnterFieldScene(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x348:
- strFunc = "FadeIn(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x33b:
- strFunc = "FadeOut(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x310:
- strFunc = "SetActivePoint(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x311:
- strFunc = "SetLightMeshCoordinates(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x312:
- strFunc = "SetLightMeshCoordinatesInMesh(" +
- stack.pop()->getString() + ", " +
- stack.pop()->getString() + ");";
- break;
- case 0x31D:
- strFunc = "PlaySoundEffect(" + stack.pop()->getString() + ");";
- break;
- case 0x328:
- strFunc = "SetActiveEntityDirection(" + stack.pop()->getString() + ");";
- break;
- case 0x336: // honours walk mesh
- case 0x303:
- strFunc = "SetActiveEntityMovespeed(" + stack.pop()->getString() + ");";
- break;
- case 0x304:
- strFunc = "SetActiveEntityDirectionAndFacing(" + stack.pop()->getString() + ");";
- break;
- case 0x32b:
- strFunc = "SetBattleLock(" + stack.pop()->getString() + ");";
- break;
- case 0x305:
- strFunc = "SetWaitFrames(" + stack.pop()->getString() + ");";
- break;
- case 0x33e:
- strFunc = "Unknown_AKAO(" + stack.pop()->getString() + ");";
- break;
- case 0x306:
- strFunc = "Wait();";
- break;
- case 0x350:
- strFunc = "SetMeteorTexture(" + stack.pop()->getString() + ");";
- break;
- case 0x34b:
- {
- std::string type = stack.pop()->getString();
- switch (std::stoi(type))
- {
- case 0:
- type = "yellow";
- break;
- case 1:
- type = "green";
- break;
- case 2:
- type = "blue";
- break;
- case 3:
- type = "black";
- break;
- case 4:
- type = "gold";
- break;
- }
- strFunc = "SetChocoboType(" + type + ");";
- }
- break;
- case 0x34c:
- {
- std::string type = stack.pop()->getString();
- switch (std::stoi(type))
- {
- case 0:
- type = "red";
- break;
- case 1:
- type = "blue";
- break;
- // Hack - not really supported, but works
- case -1:
- type = "gold";
- break;
- case -2:
- type = "black";
- break;
- }
- strFunc = "SetSubmarineColor(" + type + ");";
- }
- break;
- case 0x349:
- {
- std::string type = stack.pop()->getString();
- std::string comment = "// ";
- switch (std::stoi(type))
- {
- case 0:
- comment += "before temple of the ancients,";
- break;
- case 1:
- comment += "after temple of the ancients";
- break;
- case 2:
- comment += " after ultimate weapon appears,";
- break;
- case 3:
- comment += "after mideel";
- break;
- case 4:
- comment += "after ultimate weapon killed";
- break;
- default:
- comment += "??";
- break;
- }
- strFunc = "SetWorldProgress(" + type + "); " +comment;
- }
- break;
- case 0x300:
- {
- std::string type = stack.pop()->getString();
- std::string comment = "// ";
- try
- {
- switch (std::stoi(type))
- {
- case 0: comment += "Cloud"; break;
- case 1: comment += "Tifa?"; break;
- case 2: comment += "Cid?"; break;
- case 3: comment += "Ultimate Weapon"; break;
- case 4: comment += "Unknown"; break;
- case 5: comment += "Unknown"; break;
- case 6: comment += "Unknown"; break;
- case 7: comment += "Unknown"; break;
- case 8: comment += "Unknown"; break;
- case 9: comment += "Unknown"; break;
- case 10: comment += "Unknown"; break;
- case 11: comment += "Highwind"; break;
- case 12: comment += "Unknown"; break;
- case 13: comment += "Submarine"; break;
- case 14: comment += "Unknown"; break;
- case 15: comment += "Unknown"; break;
- case 16: comment += "Unknown"; break;
- case 17: comment += "Unknown"; break;
- case 18: comment += "Unknown"; break;
- case 19: comment += "Chocobo"; break;
- case 20: comment += "Unknown"; break;
- case 21: comment += "Unknown"; break;
- case 22: comment += "Unknown"; break;
- case 23: comment += "Unknown"; break;
- case 24: comment += "Unknown"; break;
- case 25: comment += "Unknown"; break;
- default:
- comment += "??";
- break;
- }
- }
- catch (std::invalid_argument&)
- {
- }
- strFunc = "LoadModel(" + type + "); " + comment;
- }
- break;
- case 0x307:
- strFunc = "SetControlLock(" + stack.pop()->getString() + ");";
- break;
- case 0x30c:
- strFunc = "EnterVehicle();";
- break;
- default:
- strFunc = "kernel_unknown_" + AddressValue(_opcode).getString() + "();";
- break;
- }
- codeGen->AddOutputLine(strFunc);
- }
- void FF7::FF7WorldNoOutputInstruction::ProcessInst(Function&, ValueStack&, Engine*, CodeGenerator*)
- {
- }
|