Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Long needed update for USB-Botbase
  • Loading branch information
fishguy6564 authored Aug 21, 2020
1 parent f172c65 commit 91abc4b
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 265 deletions.
127 changes: 104 additions & 23 deletions sys-botbase/source/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,97 @@ u64 buttonClickSleepTime = 50;
DmntCheatProcessMetadata metaData;

void attach()
{
Result rc = dmntchtInitialize();
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);;
rc = dmntchtForceOpenCheatProcess();
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);;
rc = dmntchtGetCheatProcessMetadata(&metaData);
{
u64 pid = 0;
Result rc = pmdmntGetApplicationProcessId(&pid);

if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);;
fatalThrow(rc);
if (debughandle != 0)
svcCloseHandle(debughandle);
rc = svcDebugActiveProcess(&debughandle, pid);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);
}

void detach(){
if (debughandle != 0) svcCloseHandle(debughandle);
}

u64 getMainNsoBase(u64 pid){
LoaderModuleInfo proc_modules[2];
s32 numModules = 0;
Result rc = ldrDmntGetProcessModuleInfo(pid, proc_modules, 2, &numModules);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);

LoaderModuleInfo *proc_module = 0;
if(numModules == 2){
proc_module = &proc_modules[1];
}else{
proc_module = &proc_modules[0];
}
return proc_module->base_address;
}

u64 getHeapBase(Handle handle){
MemoryInfo meminfo;
memset(&meminfo, 0, sizeof(MemoryInfo));
u64 heap_base = 0;
u64 lastaddr = 0;
do
{
lastaddr = meminfo.addr;
u32 pageinfo;
svcQueryDebugProcessMemory(&meminfo, &pageinfo, handle, meminfo.addr + meminfo.size);
if((meminfo.type & MemType_Heap) == MemType_Heap){
heap_base = meminfo.addr;
break;
}
} while (lastaddr < meminfo.addr + meminfo.size);

return heap_base;
}

u64 getTitleId(u64 pid){
u64 titleId = 0;
Result rc = pminfoGetProgramId(&titleId, pid);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);
return titleId;
}

void getBuildID(MetaData* meta, u64 pid){
LoaderModuleInfo proc_modules[2];
s32 numModules = 0;
Result rc = ldrDmntGetProcessModuleInfo(pid, proc_modules, 2, &numModules);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);

LoaderModuleInfo *proc_module = 0;
if(numModules == 2){
proc_module = &proc_modules[1];
}else{
proc_module = &proc_modules[0];
}
memcpy(meta->buildID, proc_module->build_id, 0x20);
}

MetaData getMetaData(){
MetaData meta;
attach();
u64 pid = 0;
Result rc = pmdmntGetApplicationProcessId(&pid);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);

meta.main_nso_base = getMainNsoBase(pid);
meta.heap_base = getHeapBase(debughandle);
meta.titleID = getTitleId(pid);
getBuildID(&meta, pid);

detach();
return meta;
}

void initController()
Expand Down Expand Up @@ -67,20 +148,20 @@ void initController()


void poke(u64 offset, u64 size, u8* val)
{
Result rc = dmntchtWriteCheatProcessMemory(offset, val, size);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);
}

u8* peek(u64 offset, u64 size)
{
u8 out[size];
Result rc = dmntchtReadCheatProcessMemory(offset, &out, size);
{
attach();
Result rc = svcWriteDebugProcessMemory(debughandle, val, offset, size);
if (R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);

return out;
fatalThrow(rc);
detach();
}

void peek(u8 outData[], u64 offset, u64 size){
attach();
Result rc = svcReadDebugProcessMemory(outData, debughandle, offset, size);
if(R_FAILED(rc) && debugResultCodes)
fatalThrow(rc);
detach();
}

void click(HidControllerKeys btn)
Expand Down Expand Up @@ -110,4 +191,4 @@ void setStickState(int side, int dxVal, int dyVal)
controllerState.joysticks[side].dx = dxVal;
controllerState.joysticks[side].dy = dyVal;
hiddbgSetHdlsState(controllerHandle, &controllerState);
}
}
50 changes: 30 additions & 20 deletions sys-botbase/source/commands.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
#include <switch.h>
#include "dmntcht.h"

extern Handle debughandle;
bool bControllerIsInitialised;
u64 controllerHandle;
HiddbgHdlsDeviceInfo controllerDevice;
HiddbgHdlsState controllerState;
extern u64 buttonClickSleepTime;

extern DmntCheatProcessMetadata metaData;

void attach();

void poke(u64 offset, u64 size, u8* val);
u8* peek(u64 offset, u64 size);
void click(HidControllerKeys btn);
void press(HidControllerKeys btn);
void release(HidControllerKeys btn);
void setStickState(int side, int dxVal, int dyVal);
#include <switch.h>

extern Handle debughandle;
bool bControllerIsInitialised;
u64 controllerHandle;
HiddbgHdlsDeviceInfo controllerDevice;
HiddbgHdlsState controllerState;
extern u64 buttonClickSleepTime;

typedef struct {
u64 main_nso_base;
u64 heap_base;
u64 titleID;
u8 buildID[0x20];
} MetaData;

void attach();
void detach();
u64 getMainNsoBase(u64 pid);
u64 getHeapBase(Handle handle);
u64 getTitleId(u64 pid);
void getBuildID(MetaData* meta, u64 pid);
MetaData getMetaData(void);

void poke(u64 offset, u64 size, u8* val);
void peek(u8* outData, u64 offset, u64 size);
void click(HidControllerKeys btn);
void press(HidControllerKeys btn);
void release(HidControllerKeys btn);
void setStickState(int side, int dxVal, int dyVal);
Loading

0 comments on commit 91abc4b

Please sign in to comment.