2014-07-19 09:57:36 +02:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
2014-07-21 01:34:28 +02:00
|
|
|
|
|
|
|
#include <script/SCMFile.hpp>
|
|
|
|
#include <script/ScriptMachine.hpp>
|
2015-05-03 19:09:25 +02:00
|
|
|
#include <script/ScriptDisassembly.hpp>
|
|
|
|
#include <script/modules/VMModule.hpp>
|
|
|
|
#include <script/modules/GameModule.hpp>
|
|
|
|
#include <script/modules/ObjectModule.hpp>
|
2014-07-19 09:57:36 +02:00
|
|
|
|
|
|
|
#define FIELD_DESC_WIDTH 30
|
|
|
|
#define FIELD_PARAM_WIDTH 8
|
|
|
|
|
|
|
|
void printUsage();
|
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
void dumpModels(SCMFile* file)
|
2014-07-19 09:57:36 +02:00
|
|
|
{
|
2014-07-21 01:34:28 +02:00
|
|
|
std::cout << "model count: " << std::dec << file->getModels().size() << std::endl;
|
|
|
|
int i = 0;
|
|
|
|
for( auto& m : file->getModels() ) {
|
|
|
|
std::cout << std::dec << (i++) << ": " << m << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
void dumpCodeSizes(SCMFile* file)
|
2014-07-19 21:57:45 +02:00
|
|
|
{
|
2014-07-21 01:34:28 +02:00
|
|
|
std::cout << "main size: " << std::hex <<
|
|
|
|
file->getMainSize() << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
std::cout << "largest mission size: " << std::hex <<
|
|
|
|
file->getLargestMissionSize() << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
std::cout << "mission count: " << std::dec <<
|
|
|
|
file->getMissionOffsets().size() << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
int i = 0;
|
|
|
|
for(auto& m : file->getMissionOffsets()) {
|
|
|
|
std::cout << std::dec << (i++) << ": " << std::hex << m << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
void dumpOpcodes(SCMFile* scm, SCMOpcodes* codes, unsigned int offset, unsigned int size)
|
2014-07-19 21:57:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "Offs Opcd " << std::setw(FIELD_DESC_WIDTH) << std::left
|
2014-07-21 01:34:28 +02:00
|
|
|
<< "Description" << "Parameters" << std::endl;
|
2014-07-19 21:57:45 +02:00
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
ScriptDisassembly disassembly(codes, scm);
|
2014-07-19 09:57:36 +02:00
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
disassembly.disassemble(offset);
|
|
|
|
}
|
|
|
|
catch( IllegalInstruction& ex )
|
|
|
|
{
|
|
|
|
std::cerr << "Error during disassembly: \n"
|
|
|
|
<< ex.what() << std::endl;
|
|
|
|
}
|
2014-07-19 09:57:36 +02:00
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
for( auto& inst : disassembly.getInstructions() )
|
|
|
|
{
|
|
|
|
ScriptFunctionMeta* code;
|
|
|
|
if(! codes->findOpcode(inst.second.opcode, &code) )
|
|
|
|
{
|
|
|
|
std::cerr << "Invalid opcode in disassembly (" << inst.second.opcode << ")" << std::endl;
|
2014-07-19 09:57:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::hex << std::setfill('0') << std::right <<
|
2015-05-03 19:09:25 +02:00
|
|
|
std::setw(4) << inst.first << ":" <<
|
|
|
|
std::setw(4) << inst.second.opcode << " " <<
|
|
|
|
std::setw(FIELD_DESC_WIDTH) << std::setfill(' ') <<
|
|
|
|
std::left << code->signature << std::right << "(";
|
|
|
|
|
|
|
|
for( SCMOpcodeParameter& param : inst.second.parameters )
|
|
|
|
{
|
|
|
|
switch( param.type )
|
|
|
|
{
|
|
|
|
case TInt8:
|
|
|
|
std::cout << " i8: " << param.integer;
|
|
|
|
break;
|
|
|
|
case TInt16:
|
|
|
|
std::cout << " i16: " << param.integer;
|
|
|
|
break;
|
|
|
|
case TInt32:
|
|
|
|
std::cout << " i32: " << param.integer;
|
|
|
|
break;
|
|
|
|
case TFloat16:
|
|
|
|
std::cout << " f16: " << param.real;
|
|
|
|
break;
|
|
|
|
case TString:
|
|
|
|
std::cout << " str: " << param.string;
|
|
|
|
break;
|
|
|
|
case TGlobal:
|
|
|
|
std::cout << " g: " << param.globalPtr;
|
|
|
|
break;
|
|
|
|
case TLocal:
|
|
|
|
std::cout << " l: " << param.globalPtr;
|
|
|
|
break;
|
2014-07-19 09:57:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
std::cout << " )\n";
|
2014-07-19 09:57:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
void disassemble(const std::string& scmname)
|
2014-07-19 09:57:36 +02:00
|
|
|
{
|
2016-06-08 20:37:00 +02:00
|
|
|
std::ifstream scmfile(scmname.c_str(), std::ios_base::binary);
|
2014-07-19 09:57:36 +02:00
|
|
|
|
|
|
|
if( !scmfile.is_open() ) {
|
|
|
|
std::cerr << "Failed to open " << scmname << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scmfile.seekg(0, std::ios_base::end);
|
|
|
|
int size = scmfile.tellg();
|
|
|
|
scmfile.seekg(0);
|
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
SCMByte* byff = new SCMByte[size];
|
|
|
|
scmfile.read(byff, size);
|
|
|
|
SCMFile scm;
|
|
|
|
scm.loadFile(byff, size);
|
|
|
|
delete byff;
|
2014-07-19 09:57:36 +02:00
|
|
|
|
|
|
|
try {
|
2014-07-21 01:34:28 +02:00
|
|
|
std::cout << "section globals: " << std::hex <<
|
|
|
|
scm.getGlobalSection() << std::endl;
|
|
|
|
std::cout << "section models: " << std::hex <<
|
|
|
|
scm.getModelSection() << std::endl;
|
|
|
|
std::cout << "section sizes: " << std::hex <<
|
|
|
|
scm.getMissionSection() << std::endl;
|
|
|
|
std::cout << "section main: " << std::hex <<
|
|
|
|
scm.getCodeSection() << std::endl;
|
2014-07-19 09:57:36 +02:00
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
dumpModels(&scm);
|
2014-07-19 09:57:36 +02:00
|
|
|
|
2014-07-21 01:34:28 +02:00
|
|
|
dumpCodeSizes(&scm);
|
2015-05-03 19:09:25 +02:00
|
|
|
|
|
|
|
SCMOpcodes* opcodes = new SCMOpcodes;
|
|
|
|
opcodes->modules.push_back(new VMModule);
|
|
|
|
opcodes->modules.push_back(new GameModule);
|
|
|
|
opcodes->modules.push_back(new ObjectModule);
|
|
|
|
|
|
|
|
dumpOpcodes(&scm, opcodes, scm.getCodeSection(), size);
|
2014-07-19 09:57:36 +02:00
|
|
|
}
|
|
|
|
catch (SCMException& ex) {
|
|
|
|
std::cerr << ex.what() << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
if( argc < 2 ) {
|
|
|
|
std::cerr << "Missing argument" << std::endl;
|
|
|
|
printUsage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-03 19:09:25 +02:00
|
|
|
disassemble(std::string(argv[1]));
|
2014-07-19 09:57:36 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void printUsage() {
|
|
|
|
std::cout << "Usage:" << std::endl;
|
|
|
|
std::cout << " scripttool scmfile" << std::endl;
|
|
|
|
}
|