Skip to content

Commit 91abc4b

Browse files
authored
Update
Long needed update for USB-Botbase
1 parent f172c65 commit 91abc4b

File tree

5 files changed

+282
-265
lines changed

5 files changed

+282
-265
lines changed

sys-botbase/source/commands.c

Lines changed: 104 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,97 @@ u64 buttonClickSleepTime = 50;
2121
DmntCheatProcessMetadata metaData;
2222

2323
void attach()
24-
{
25-
Result rc = dmntchtInitialize();
26-
if (R_FAILED(rc) && debugResultCodes)
27-
fatalThrow(rc);;
28-
rc = dmntchtForceOpenCheatProcess();
29-
if (R_FAILED(rc) && debugResultCodes)
30-
fatalThrow(rc);;
31-
rc = dmntchtGetCheatProcessMetadata(&metaData);
24+
{
25+
u64 pid = 0;
26+
Result rc = pmdmntGetApplicationProcessId(&pid);
27+
3228
if (R_FAILED(rc) && debugResultCodes)
33-
fatalThrow(rc);;
29+
fatalThrow(rc);
30+
if (debughandle != 0)
31+
svcCloseHandle(debughandle);
32+
rc = svcDebugActiveProcess(&debughandle, pid);
33+
if (R_FAILED(rc) && debugResultCodes)
34+
fatalThrow(rc);
35+
}
36+
37+
void detach(){
38+
if (debughandle != 0) svcCloseHandle(debughandle);
39+
}
40+
41+
u64 getMainNsoBase(u64 pid){
42+
LoaderModuleInfo proc_modules[2];
43+
s32 numModules = 0;
44+
Result rc = ldrDmntGetProcessModuleInfo(pid, proc_modules, 2, &numModules);
45+
if (R_FAILED(rc) && debugResultCodes)
46+
fatalThrow(rc);
47+
48+
LoaderModuleInfo *proc_module = 0;
49+
if(numModules == 2){
50+
proc_module = &proc_modules[1];
51+
}else{
52+
proc_module = &proc_modules[0];
53+
}
54+
return proc_module->base_address;
55+
}
56+
57+
u64 getHeapBase(Handle handle){
58+
MemoryInfo meminfo;
59+
memset(&meminfo, 0, sizeof(MemoryInfo));
60+
u64 heap_base = 0;
61+
u64 lastaddr = 0;
62+
do
63+
{
64+
lastaddr = meminfo.addr;
65+
u32 pageinfo;
66+
svcQueryDebugProcessMemory(&meminfo, &pageinfo, handle, meminfo.addr + meminfo.size);
67+
if((meminfo.type & MemType_Heap) == MemType_Heap){
68+
heap_base = meminfo.addr;
69+
break;
70+
}
71+
} while (lastaddr < meminfo.addr + meminfo.size);
72+
73+
return heap_base;
74+
}
75+
76+
u64 getTitleId(u64 pid){
77+
u64 titleId = 0;
78+
Result rc = pminfoGetProgramId(&titleId, pid);
79+
if (R_FAILED(rc) && debugResultCodes)
80+
fatalThrow(rc);
81+
return titleId;
82+
}
83+
84+
void getBuildID(MetaData* meta, u64 pid){
85+
LoaderModuleInfo proc_modules[2];
86+
s32 numModules = 0;
87+
Result rc = ldrDmntGetProcessModuleInfo(pid, proc_modules, 2, &numModules);
88+
if (R_FAILED(rc) && debugResultCodes)
89+
fatalThrow(rc);
90+
91+
LoaderModuleInfo *proc_module = 0;
92+
if(numModules == 2){
93+
proc_module = &proc_modules[1];
94+
}else{
95+
proc_module = &proc_modules[0];
96+
}
97+
memcpy(meta->buildID, proc_module->build_id, 0x20);
98+
}
99+
100+
MetaData getMetaData(){
101+
MetaData meta;
102+
attach();
103+
u64 pid = 0;
104+
Result rc = pmdmntGetApplicationProcessId(&pid);
105+
if (R_FAILED(rc) && debugResultCodes)
106+
fatalThrow(rc);
107+
108+
meta.main_nso_base = getMainNsoBase(pid);
109+
meta.heap_base = getHeapBase(debughandle);
110+
meta.titleID = getTitleId(pid);
111+
getBuildID(&meta, pid);
112+
113+
detach();
114+
return meta;
34115
}
35116

36117
void initController()
@@ -67,20 +148,20 @@ void initController()
67148

68149

69150
void poke(u64 offset, u64 size, u8* val)
70-
{
71-
Result rc = dmntchtWriteCheatProcessMemory(offset, val, size);
72-
if (R_FAILED(rc) && debugResultCodes)
73-
fatalThrow(rc);
74-
}
75-
76-
u8* peek(u64 offset, u64 size)
77-
{
78-
u8 out[size];
79-
Result rc = dmntchtReadCheatProcessMemory(offset, &out, size);
151+
{
152+
attach();
153+
Result rc = svcWriteDebugProcessMemory(debughandle, val, offset, size);
80154
if (R_FAILED(rc) && debugResultCodes)
81-
fatalThrow(rc);
82-
83-
return out;
155+
fatalThrow(rc);
156+
detach();
157+
}
158+
159+
void peek(u8 outData[], u64 offset, u64 size){
160+
attach();
161+
Result rc = svcReadDebugProcessMemory(outData, debughandle, offset, size);
162+
if(R_FAILED(rc) && debugResultCodes)
163+
fatalThrow(rc);
164+
detach();
84165
}
85166

86167
void click(HidControllerKeys btn)
@@ -110,4 +191,4 @@ void setStickState(int side, int dxVal, int dyVal)
110191
controllerState.joysticks[side].dx = dxVal;
111192
controllerState.joysticks[side].dy = dyVal;
112193
hiddbgSetHdlsState(controllerHandle, &controllerState);
113-
}
194+
}

sys-botbase/source/commands.h

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
#include <switch.h>
2-
#include "dmntcht.h"
3-
4-
extern Handle debughandle;
5-
bool bControllerIsInitialised;
6-
u64 controllerHandle;
7-
HiddbgHdlsDeviceInfo controllerDevice;
8-
HiddbgHdlsState controllerState;
9-
extern u64 buttonClickSleepTime;
10-
11-
extern DmntCheatProcessMetadata metaData;
12-
13-
void attach();
14-
15-
void poke(u64 offset, u64 size, u8* val);
16-
u8* peek(u64 offset, u64 size);
17-
void click(HidControllerKeys btn);
18-
void press(HidControllerKeys btn);
19-
void release(HidControllerKeys btn);
20-
void setStickState(int side, int dxVal, int dyVal);
1+
#include <switch.h>
2+
3+
extern Handle debughandle;
4+
bool bControllerIsInitialised;
5+
u64 controllerHandle;
6+
HiddbgHdlsDeviceInfo controllerDevice;
7+
HiddbgHdlsState controllerState;
8+
extern u64 buttonClickSleepTime;
9+
10+
typedef struct {
11+
u64 main_nso_base;
12+
u64 heap_base;
13+
u64 titleID;
14+
u8 buildID[0x20];
15+
} MetaData;
16+
17+
void attach();
18+
void detach();
19+
u64 getMainNsoBase(u64 pid);
20+
u64 getHeapBase(Handle handle);
21+
u64 getTitleId(u64 pid);
22+
void getBuildID(MetaData* meta, u64 pid);
23+
MetaData getMetaData(void);
24+
25+
void poke(u64 offset, u64 size, u8* val);
26+
void peek(u8* outData, u64 offset, u64 size);
27+
void click(HidControllerKeys btn);
28+
void press(HidControllerKeys btn);
29+
void release(HidControllerKeys btn);
30+
void setStickState(int side, int dxVal, int dyVal);

0 commit comments

Comments
 (0)