Skip to content

Commit

Permalink
phoenix::func
Browse files Browse the repository at this point in the history
  • Loading branch information
Try committed Aug 6, 2023
1 parent ce3dbf2 commit e6f5687
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 88 deletions.
42 changes: 12 additions & 30 deletions game/game/compatibility/ikarus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ Ikarus::Ikarus(GameScript& /*owner*/, phoenix::vm& vm) : vm(vm) {
vm.override_function("MEMINT_ReplaceSlowFunctions", [ ](){ });
vm.override_function("MEM_GetAddress_Init", [this](){ mem_getaddress_init(); });
vm.override_function("MEM_PrintStackTrace", [this](){ mem_printstacktrace_implementation(); });
vm.override_function("MEM_GetFuncOffset", [this](int func){ return mem_getfuncoffset(func); });
vm.override_function("MEM_GetFuncID", [this](int sym) { return mem_getfuncid(sym); });
vm.override_function("MEM_GetFuncOffset", [this](phoenix::func func){ return mem_getfuncoffset(func); });
vm.override_function("MEM_GetFuncID", [this](phoenix::func sym) { return mem_getfuncid(sym); });
vm.override_function("MEM_CallByID", [this](int sym) { return mem_callbyid(sym); });
vm.override_function("MEM_GetFuncPtr", [this](int sym) { return mem_getfuncptr(sym); });
vm.override_function("MEM_ReplaceFunc", [this](int dest, int func){ mem_replacefunc(dest, func); });
vm.override_function("MEM_ReplaceFunc", [this](phoenix::func dest, phoenix::func func){ mem_replacefunc(dest, func); });
vm.override_function("MEM_GetFuncIdByOffset", [this](int off) { return mem_getfuncidbyoffset(off); });
vm.override_function("MEM_AssignInst", [this](int sym, int ptr) { mem_assigninst(sym, ptr); });

Expand Down Expand Up @@ -189,20 +189,11 @@ void Ikarus::mem_sendtospy(int cat, std::string_view msg) {

void Ikarus::mem_getaddress_init() { /* nop */ }

void Ikarus::mem_replacefunc(int dest, int func) {
auto* sf = vm.find_symbol_by_index(uint32_t(func));
auto* sd = vm.find_symbol_by_index(uint32_t(dest));

if(sf == nullptr || sf->type() != phoenix::datatype::function) {
Log::e("mem_replacefunc: invalid function ptr");
return;
}
if(sd == nullptr || sd->type() != phoenix::datatype::function) {
Log::e("mem_replacefunc: invalid function ptr");
return;
}

void Ikarus::mem_replacefunc(phoenix::func dest, phoenix::func func) {
auto* sf = func.value;
auto* sd = dest.value;
Log::d("mem_replacefunc: ",sd->name()," -> ",sf->name());

//auto& bin = vm.getDATFile().rawCode();
//bin[sd.address]->op = EParOp_Jump;
//bin[sd.address]->address = func;
Expand All @@ -228,26 +219,17 @@ void Ikarus::mem_printstacktrace_implementation() {
Log::e("[end of stacktrace]");
}

int Ikarus::mem_getfuncoffset(int func) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
if(sym == nullptr || sym->type() != phoenix::datatype::function) {
int Ikarus::mem_getfuncoffset(phoenix::func func) {
auto* sym = func.value;
if(sym == nullptr) {
Log::e("mem_getfuncptr: invalid function ptr");
return 0;
}
return int(sym->address());
}

int Ikarus::mem_getfuncid(int func) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
return func;
int Ikarus::mem_getfuncid(phoenix::func func) {
return int(func.value->index());
}

void Ikarus::mem_callbyid(int symbId) {
Expand Down
6 changes: 3 additions & 3 deletions game/game/compatibility/ikarus.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class Ikarus : public ScriptPlugin {
void mem_setupexceptionhandler ();
void mem_getaddress_init ();
void mem_printstacktrace_implementation();
int mem_getfuncoffset (int func);
int mem_getfuncid (int func);
int mem_getfuncoffset (phoenix::func func);
int mem_getfuncid (phoenix::func func);
void mem_callbyid (int symbId);
int mem_getfuncptr (int symbId);
void mem_replacefunc (int dest, int func);
void mem_replacefunc (phoenix::func dest, phoenix::func func);
int mem_getfuncidbyoffset (int off);
void mem_assigninst (int sym, int ptr);

Expand Down
76 changes: 28 additions & 48 deletions game/game/compatibility/lego.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,37 @@ LeGo::LeGo(GameScript& owner, Ikarus& ikarus, phoenix::vm& vm_) : owner(owner),
Log::i("DMA mod detected: LeGo");

// ## FrameFunctions
vm.override_function("_FF_Create", [this](int function, int delay, int cycles, int hasData, int data, bool gametime) {
vm.override_function("_FF_Create", [this](phoenix::func function, int delay, int cycles,
int hasData, int data, bool gametime) {
return _FF_Create(function, delay, cycles, hasData, data, gametime);
});
vm.override_function("FF_RemoveData", [this](int function, int data){
vm.override_function("FF_RemoveData", [this](phoenix::func function, int data){
return FF_RemoveData(function, data);
});
vm.override_function("FF_ActiveData", [this](int function, int data){
vm.override_function("FF_ActiveData", [this](phoenix::func function, int data){
return FF_ActiveData(function, data);
});
vm.override_function("FF_Active", [this](int function){
vm.override_function("FF_Active", [this](phoenix::func function){
return FF_Active(function);
});

// HookEngine
vm.override_function("HookEngineF", [this](int address, int oldInstr, int function) {
auto sym = vm.find_symbol_by_index(uint32_t(function));
vm.override_function("HookEngineF", [](int address, int oldInstr, phoenix::func function) {
auto sym = function.value;
auto name = sym==nullptr ? "" : sym->name().c_str();
Log::e("not implemented call [HookEngineF] (", reinterpret_cast<void*>(uint64_t(address)),
" -> ", name, ")");
});
vm.override_function("HookEngineI", [this](int address, int oldInstr, int function){
auto sym = vm.find_symbol_by_index(uint32_t(function));
vm.override_function("HookEngineI", [](int address, int oldInstr, phoenix::func function){
auto sym = function.value;
auto name = sym==nullptr ? "" : sym->name().c_str();
Log::e("not implemented call [HookEngineI] (", reinterpret_cast<void*>(uint64_t(address)),
" -> ", name, ")");
});

// console commands
vm.override_function("CC_Register", [this](int func, std::string_view prefix, std::string_view desc){
auto sym = vm.find_symbol_by_index(uint32_t(func));
vm.override_function("CC_Register", [](phoenix::func func, std::string_view prefix, std::string_view desc){
auto sym = func.value;
auto name = sym==nullptr ? "" : sym->name().c_str();
Log::e("not implemented call [CC_Register] (", prefix, " -> ", name, ")");
});
Expand Down Expand Up @@ -119,18 +120,9 @@ void LeGo::tick(uint64_t dt) {
}
}

void LeGo::_FF_Create(int func, int delay, int cycles, int hasData, int data, bool gametime) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
if(sym == nullptr || sym->type() != phoenix::datatype::function) {
Log::e("_FF_Create: invalid function ptr");
return;
}
void LeGo::_FF_Create(phoenix::func func, int delay, int cycles, int hasData, int data, bool gametime) {
FFItem itm;
itm.fncID = sym->index();
itm.fncID = func.value->index();
itm.cycles = cycles;
itm.delay = std::max(delay, 0);
itm.data = data;
Expand All @@ -146,66 +138,54 @@ void LeGo::_FF_Create(int func, int delay, int cycles, int hasData, int data, bo
frameFunc.emplace_back(itm);
}

void LeGo::FF_Remove(int function) {
void LeGo::FF_Remove(phoenix::func function) {

}

void LeGo::FF_RemoveAll(int function) {
void LeGo::FF_RemoveAll(phoenix::func function) {

}

void LeGo::FF_RemoveData(int func, int data) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
if(sym == nullptr || sym->type() != phoenix::datatype::function) {
void LeGo::FF_RemoveData(phoenix::func func, int data) {
auto* sym = func.value;
if(sym == nullptr) {
Log::e("FF_RemoveData: invalid function ptr");
return;
}

size_t nsz = 0;
for(size_t i=0; i<frameFunc.size(); ++i) {
if(frameFunc[i].fncID==uint32_t(func) && frameFunc[i].data==data)
if(frameFunc[i].fncID==sym->index() && frameFunc[i].data==data)
continue;
frameFunc[nsz] = frameFunc[i];
++func;
++nsz;
}
frameFunc.resize(nsz);
}

bool LeGo::FF_ActiveData(int func, int data) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
if(sym == nullptr || sym->type() != phoenix::datatype::function) {
bool LeGo::FF_ActiveData(phoenix::func func, int data) {
auto* sym = func.value;
if(sym == nullptr) {
Log::e("FF_ActiveData: invalid function ptr");
return false;
}

for(auto& f:frameFunc) {
if(f.fncID==uint32_t(func) && f.data==data)
if(f.fncID==sym->index() && f.data==data)
return true;
}
return false;
}

bool LeGo::FF_Active(int func) {
auto* sym = vm.find_symbol_by_index(uint32_t(func));
while(sym!=nullptr && !sym->is_const()) {
func = sym->get_int();
sym = vm.find_symbol_by_index(uint32_t(func));
}
if(sym == nullptr || sym->type() != phoenix::datatype::function) {
bool LeGo::FF_Active(phoenix::func func) {
auto* sym = func.value;
if(sym == nullptr) {
Log::e("FF_Active: invalid function ptr");
return false;
}

for(auto& f:frameFunc) {
if(f.fncID==uint32_t(func))
if(f.fncID==sym->index())
return true;
}
return false;
Expand Down
12 changes: 6 additions & 6 deletions game/game/compatibility/lego.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class LeGo : public ScriptPlugin {
void tick(uint64_t dt) override;

// ## FRAMEFUNCTIONS
void _FF_Create (int function, int delay, int cycles, int hasData, int data, bool gametime);
void FF_Remove (int function);
void FF_RemoveAll (int function);
void FF_RemoveData(int function, int data);
bool FF_ActiveData(int function, int data);
bool FF_Active (int function);
void _FF_Create (phoenix::func function, int delay, int cycles, int hasData, int data, bool gametime);
void FF_Remove (phoenix::func function);
void FF_RemoveAll (phoenix::func function);
void FF_RemoveData(phoenix::func function, int data);
bool FF_ActiveData(phoenix::func function, int data);
bool FF_Active (phoenix::func function);
struct FFItem {
uint32_t fncID = 0;
uint64_t next = 0;
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix

0 comments on commit e6f5687

Please sign in to comment.