V-Gears 0
Free Final Fantasy VII engine.
FieldDisassembler Class Reference

A disassebler for field maps. More...

#include <FieldDisassembler.h>

Inheritance diagram for FieldDisassembler:
Collaboration diagram for FieldDisassembler:

Classes

struct  InstructionRecord
 Information about an instruction. More...
 
struct  ScriptHeader
 A script header. More...
 
struct  ScriptInfo
 Info about a script. More...
 

Public Member Functions

 FieldDisassembler (FieldScriptFormatter &formatter, FieldEngine *engine, InstVec &insts)
 Constructor. More...
 
 FieldDisassembler (FieldScriptFormatter &formatter, FieldEngine *engine, InstVec &insts, const std::vector< unsigned char > &raw_script_data)
 Constructor. More...
 
 ~FieldDisassembler ()
 Destructor. More...
 
float GetScaleFactor () const
 Retrieves the scale factor for the field. More...
 
virtual void DoDisassemble () override
 Disassembles the instructions. More...
 
- Public Member Functions inherited from Disassembler
 Disassembler (InstVec &insts)
 Constructor for Disassembler. More...
 
virtual ~Disassembler ()=default
 Destructor. More...
 
virtual void Open (const char *filename)
 Open a file for disassembly. More...
 
void Disassemble ()
 Request disassembled instructions. More...
 
void DumpDisassembly (std::ostream &output)
 Outputs the disassembled code. More...
 

Static Public Member Functions

static std::map< std::string, const InstructionRecord * > FieldInstructions ()
 Generates a map of control flow instructions. More...
 
static int FindId (uint32 start_addr, uint32 end_addr, const InstVec &insts)
 Finds the ID of the character a function refers to. More...
 

Static Public Attributes

static InstructionRecord FLOW_OPCODES []
 A list of flow control opcodes needed by the disassembler. More...
 

Private Types

enum  SECTIONS {
  SCRIPT = 0 , WALKMESH = 1 , TILE_MAP = 2 , CAMERA_MATRIX = 3 ,
  TRIGGERS = 4 , ENCOUNTERS = 5 , MODELS = 6
}
 The sections of a field. More...
 

Private Member Functions

virtual void Open (const char *filename) override
 Open a file for disassembly. More...
 
void DisassembleIndivdualScript (std::string entity_name, size_t entity_index, size_t script_index, size_t script_entry_point, uint32 next_script_entry_point, bool is_start, bool is_end)
 Disassembles a script (function). More...
 
void AddFunc (std::string entity_name, size_t entity_index, size_t script_index, uint32 next_script_entry_point, const bool is_start, const bool is_end, bool to_return_only, std::string func_name)
 Adds a function to the engine. More...
 
bool ReadOpCodesToPositionOrReturn (size_t end_pos, std::vector< float > &point_a, std::vector< float > &point_b)
 Reads opcodes from a script, and detects lines. More...
 
std::unique_ptr< FunctionStartFunction (size_t script_index)
 Initializes a function for an entity. More...
 
void ReadHeader ()
 Reads the header. More...
 
void ReadHeader (BinaryReader &reader)
 Reads the header. More...
 
uint32 GetEndOfScriptOffset (uint16 cur_entry_point, size_t entity_index, size_t script_index)
 Retrieves the offset of the end of a script. More...
 
template<typename T >
void ParseOpcode (int opcode, std::string name, T instruction, int stack_change, const char *argument_format)
 Parses an opcode. More...
 

Private Attributes

FieldEngineengine_
 The engine. More...
 
uint32 header_end_position_ = 0
 Last address of the header. More...
 
std::array< uint32, 7 > sections_
 The list of sections. More...
 
int16 section_pointers_size_
 Size of the pointers to sections. More...
 
ScriptHeader header_
 The field header. More...
 
bool loaded_from_raw_data_ = false
 Indicates if the field has been loaded from raw data. More...
 
FieldScriptFormatterformatter_
 The formatter. More...
 
float scale_factor_ = 1.0f
 The scale factor of the field. More...
 

Static Private Attributes

static const int MAGIC
 Field file identification magic number. More...
 
static const int NUM_SECTIONS
 The number of sections in a field. More...
 

Additional Inherited Members

- Protected Member Functions inherited from Disassembler
virtual void DoDisassemble ()=0
 Performs disassembly. More...
 
virtual void DoDumpDisassembly (std::ostream &output)
 Outputs the disassembled code. More...
 
void ReadParams (InstPtr inst, const char *types)
 Read parameters and associate them with an instruction. More...
 
void ReadParams (InstPtr inst, const char *types, const std::vector< std::string > &params)
 Read parameters but it doesn't associate them with an instruction. More...
 
virtual ValuePtr ReadParameter (InstPtr inst, std::string type)
 Reads data for a single parameter. More...
 
- Protected Attributes inherited from Disassembler
std::unique_ptr< BinaryReaderstream_
 Used to perform file I/O. More...
 
InstVecinsts_
 List of disassembled instructions. More...
 
uint32 address_base_
 Base address where the script starts. More...
 
uint32 address_
 The current address. More...
 

Detailed Description

A disassebler for field maps.

Member Enumeration Documentation

◆ SECTIONS

The sections of a field.

Enumerator
SCRIPT 

The scripts section.

WALKMESH 

The walkmesh section.

TILE_MAP 

The tiles section.

CAMERA_MATRIX 

The camera section.

TRIGGERS 

The triggers section.

ENCOUNTERS 

Encounter data section.

MODELS 

Field models section.

Constructor & Destructor Documentation

◆ FieldDisassembler() [1/2]

FieldDisassembler::FieldDisassembler ( FieldScriptFormatter formatter,
FieldEngine engine,
InstVec insts 
)

Constructor.

Parameters
[in]formatterThe code formatter.
[in]engineThe engine to use to disassemble.
[in]instsThe list of instructions.

◆ FieldDisassembler() [2/2]

FieldDisassembler::FieldDisassembler ( FieldScriptFormatter formatter,
FieldEngine engine,
InstVec insts,
const std::vector< unsigned char > &  raw_script_data 
)

Constructor.

Parameters
[in]formatterThe code formatter.
[in]engineThe engine to use to disassemble.
[in]instsThe list of instructions.
[in]raw_script_data
Todo:
Understand and document.

◆ ~FieldDisassembler()

FieldDisassembler::~FieldDisassembler ( )

Destructor.

Member Function Documentation

◆ AddFunc()

void FieldDisassembler::AddFunc ( std::string  entity_name,
size_t  entity_index,
size_t  script_index,
uint32  next_script_entry_point,
const bool  is_start,
const bool  is_end,
bool  to_return_only,
std::string  func_name 
)
private

Adds a function to the engine.

Parameters
[in]entity_nameName of the entity that owns the function.
[in]entity_indexIndex of the entity that owns the function.
[in]script_indexIndex of the script within the entity.
[in]next_script_entry_pointStart position of the next script (or the ending position of this one, plus one).
[in]is_start
Todo:
Understand and document.
Parameters
[in]is_end
Todo:
Understand and document.
Parameters
[in]to_return_onlyTrue to read the script only until the first return.
[in]func_nameName of the function. If the entity is a line, the provided name will be overridden and set to the standard line entity script names.

◆ DisassembleIndivdualScript()

void FieldDisassembler::DisassembleIndivdualScript ( std::string  entity_name,
size_t  entity_index,
size_t  script_index,
size_t  script_entry_point,
uint32  next_script_entry_point,
bool  is_start,
bool  is_end 
)
private

Disassembles a script (function).

Parameters
[in]entity_nameName of the entity the script belongs to.
[in]entity_indexIndex of the entity the script belongs to.
[in]script_indexIndex of the script, relative to the entity scripts.
[in]script_entry_pointAddress of the first instruction of the script to disassemble.
[in]next_script_entry_pointAddress of the first instruction of the script next to the one to disassemble (i.e. the last address of the script to disassemble, plus one).
[in]is_startIndicates if the script is the first one for it's entity.
[in]is_endIndicates if the script is the last one for it's entity.
Exceptions
DecompilerExceptionfor malformed scripts.

◆ DoDisassemble()

void FieldDisassembler::DoDisassemble ( )
overridevirtual

Disassembles the instructions.

Implements Disassembler.

◆ FieldInstructions()

std::map< std::string, const FieldDisassembler::InstructionRecord * > FieldDisassembler::FieldInstructions ( )
static

Generates a map of control flow instructions.

Returns
A map of control flow instructions, by opcode name.

◆ FindId()

int FieldDisassembler::FindId ( uint32  start_addr,
uint32  end_addr,
const InstVec insts 
)
static

Finds the ID of the character a function refers to.

Parameters
[in]start_addrThe starting address of the function.
[in]end_addrThe ending address of the instruction.
[in]instsThe list of instructions to search.
Returns
The ID of the character the function refers to, or -1 if the function doesn't belong to a character.

◆ GetEndOfScriptOffset()

uint32 FieldDisassembler::GetEndOfScriptOffset ( uint16  cur_entry_point,
size_t  entity_index,
size_t  script_index 
)
private

Retrieves the offset of the end of a script.

Parameters
[in]cur_entry_point
Todo:
Understand and document.
Parameters
[in]entity_indexIndex of the entity the script belongs to.
[in]script_indexThe script index.
Returns
The address of the end of the script.

◆ GetScaleFactor()

float FieldDisassembler::GetScaleFactor ( ) const

Retrieves the scale factor for the field.

◆ Open()

void FieldDisassembler::Open ( const char *  filename)
overrideprivatevirtual

Open a file for disassembly.

Parameters
[in]filenameThe file to disassemble.
Deprecated:
Do not use. Load the instructions from the constructor.

Reimplemented from Disassembler.

◆ ParseOpcode()

template<typename T >
void FieldDisassembler::ParseOpcode ( int  opcode,
std::string  name,
instruction,
int  stack_change,
const char *  argument_format 
)
inlineprivate

Parses an opcode.

Adds properties to an instruction, and adds the instruction to the list.

Parameters
[in]opcodeThe opcode code.
[in]namethe opcode name.
[in]instructionA newly created instruction of the type the opcode belongs to.
[in]stack_changeIndicates how much the instruction changes the stack pointer by.
[in]argument_formatThe opcode argument format.

◆ ReadHeader() [1/2]

void FieldDisassembler::ReadHeader ( )
private

Reads the header.

◆ ReadHeader() [2/2]

void FieldDisassembler::ReadHeader ( BinaryReader reader)
private

Reads the header.

Parameters
[in]readerReader to use to read.

◆ ReadOpCodesToPositionOrReturn()

bool FieldDisassembler::ReadOpCodesToPositionOrReturn ( size_t  end_pos,
std::vector< float > &  point_a,
std::vector< float > &  point_b 
)
private

Reads opcodes from a script, and detects lines.

It stops when it reaches the ending position or a RET opcode, whatever comes first. It also detects if one of the read opcodes LINE, which means that the entity is a line

Parameters
[in]end_posThe last position to read. Opcodes will be read until this, or until a RET is found.
[out]point_aIf a LINE opcode is found, the first point will be saved here.
[out]point_bIf a LINE opcode is found, the second point will be saved here.
Returns
True if LINE opcode found, false if not.

◆ StartFunction()

std::unique_ptr< Function > FieldDisassembler::StartFunction ( size_t  script_index)
private

Initializes a function for an entity.

Parameters
[in]script_indexIndex of the script.

Member Data Documentation

◆ engine_

FieldEngine* FieldDisassembler::engine_
private

The engine.

◆ FLOW_OPCODES

FieldDisassembler::InstructionRecord FieldDisassembler::FLOW_OPCODES
static
Initial value:
={
}
@ REQSW
Definition: FieldDisassembler.h:31
@ REQEW
Definition: FieldDisassembler.h:32
@ RET
Definition: FieldDisassembler.h:29
@ REQ
Definition: FieldDisassembler.h:30
@ IFUB
Definition: FieldDisassembler.h:47
@ NOP
Definition: FieldDisassembler.h:112
static InstPtr Create()
Create a FieldControlFlowInstruction.
Definition: FieldControlFlowInstruction.cpp:27
static InstPtr Create()
Generates a instruction that does nothing.
Definition: FieldNoOperationInstruction.h:35

A list of flow control opcodes needed by the disassembler.

◆ formatter_

FieldScriptFormatter& FieldDisassembler::formatter_
private

The formatter.

◆ header_

ScriptHeader FieldDisassembler::header_
private

The field header.

◆ header_end_position_

uint32 FieldDisassembler::header_end_position_ = 0
private

Last address of the header.

◆ loaded_from_raw_data_

bool FieldDisassembler::loaded_from_raw_data_ = false
private

Indicates if the field has been loaded from raw data.

◆ MAGIC

const int FieldDisassembler::MAGIC
staticprivate

Field file identification magic number.

◆ NUM_SECTIONS

const int FieldDisassembler::NUM_SECTIONS
staticprivate

The number of sections in a field.

◆ scale_factor_

float FieldDisassembler::scale_factor_ = 1.0f
private

The scale factor of the field.

◆ section_pointers_size_

int16 FieldDisassembler::section_pointers_size_
private

Size of the pointers to sections.

Todo:
Understand and document.

◆ sections_

std::array<uint32, 7> FieldDisassembler::sections_
private

The list of sections.


The documentation for this class was generated from the following files: