Skip to content

Commit

Permalink
formatting all the files
Browse files Browse the repository at this point in the history
  • Loading branch information
hamhub7 committed May 24, 2021
1 parent 358efd4 commit 4ebf1e8
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 60 deletions.
17 changes: 10 additions & 7 deletions source/format.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
#include <memory>
#include <string>

template<typename ... Args>
std::string string_format( const std::string& format, Args ... args )
template <typename... Args>
std::string string_format(const std::string &format, Args... args)
{
int size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
if( size <= 0 ){ throw std::runtime_error( "Error during formatting." ); }
std::unique_ptr<char[]> buf( new char[ size ] );
snprintf( buf.get(), size, format.c_str(), args ... );
return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size <= 0)
{
throw std::runtime_error("Error during formatting.");
}
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), size, format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
2 changes: 1 addition & 1 deletion source/lua_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool lua_hid_KeyboardDown(HidKeyboardKey key)
return keyboard.keyDown(key);
}

void registerHID(sol::state& lua)
void registerHID(sol::state &lua)
{
lua.set_function("hid_ScanInput", lua_hid_ScanInput);
lua.set_function("hid_KeyboardDown", lua_hid_KeyboardDown);
Expand Down
2 changes: 1 addition & 1 deletion source/lua_hid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
#include <sol/sol.hpp>
#include "keyPad.hpp"

void registerHID(sol::state& lua);
void registerHID(sol::state &lua);
35 changes: 20 additions & 15 deletions source/lua_hiddbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Returns a pro controller connected using bluetooth, accepting arguments for the bodyColor then buttonsColor then gripLColor then gripRcolor
Controller lua_hiddbg_AttachController(u32 bodyColor, u32 buttonsColor, u32 gripLColor, u32 gripRColor)
{
HiddbgHdlsDeviceInfo device = { 0 };
HiddbgHdlsDeviceInfo device = {0};
device.deviceType = HidDeviceType_FullKey3;
device.npadInterfaceType = HidNpadInterfaceType_Bluetooth;
device.singleColorBody = bodyColor;
Expand All @@ -12,7 +12,7 @@ Controller lua_hiddbg_AttachController(u32 bodyColor, u32 buttonsColor, u32 grip
device.colorRightGrip = gripRColor;

Controller controller = {0};

Result rc = hiddbgAttachHdlsVirtualDevice(&controller.handle, &device);
if (R_FAILED(rc))
{
Expand All @@ -22,7 +22,7 @@ Controller lua_hiddbg_AttachController(u32 bodyColor, u32 buttonsColor, u32 grip
controller.state.battery_level = 4;

rc = hiddbgSetHdlsState(controller.handle, &controller.state);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error setting controller state: %#x", rc);
}
Expand All @@ -31,7 +31,7 @@ Controller lua_hiddbg_AttachController(u32 bodyColor, u32 buttonsColor, u32 grip
}

// Takes a controller userdata and detaches it
void lua_hiddbg_DetachController(Controller* controller)
void lua_hiddbg_DetachController(Controller *controller)
{
Result rc = hiddbgDetachHdlsVirtualDevice(controller->handle);
if (R_FAILED(rc))
Expand All @@ -43,12 +43,12 @@ void lua_hiddbg_DetachController(Controller* controller)
}

// Takes a controller userdata and returns whether the handle is connected
bool lua_hiddbg_IsControllerAttached(Controller* controller)
bool lua_hiddbg_IsControllerAttached(Controller *controller)
{
bool isAttached;

Result rc = hiddbgIsHdlsVirtualDeviceAttached(controller->handle, &isAttached);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error checking if controller attached: %#x", rc);
}
Expand All @@ -57,12 +57,12 @@ bool lua_hiddbg_IsControllerAttached(Controller* controller)
}

// Takes a controller userdata then a button field and updates the buttons
void lua_hiddbg_SetButtons(Controller* controller, u64 buttons)
void lua_hiddbg_SetButtons(Controller *controller, u64 buttons)
{
controller->state.buttons = buttons;

Result rc = hiddbgSetHdlsState(controller->handle, &controller->state);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error setting button state: %#x", rc);
}
Expand All @@ -71,30 +71,35 @@ void lua_hiddbg_SetButtons(Controller* controller, u64 buttons)
}

// Takes a controller userdata then a joystick index then an x position and y position and updates the joystick in question
void lua_hiddbg_SetJoystick(Controller* controller, int stickIndex, s32 x, s32 y)
void lua_hiddbg_SetJoystick(Controller *controller, int stickIndex, s32 x, s32 y)
{
HidAnalogStickState* stick = nullptr;
if(stickIndex == 1) {
HidAnalogStickState *stick = nullptr;
if (stickIndex == 1)
{
stick = &controller->state.analog_stick_l;
} else if (stickIndex == 2) {
}
else if (stickIndex == 2)
{
stick = &controller->state.analog_stick_r;
} else {
}
else
{
throw "Invalid joystick specified";
}

stick->x = x;
stick->y = y;

Result rc = hiddbgSetHdlsState(controller->handle, &controller->state);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error setting joystick state: %#x", rc);
}

return;
}

void registerHIDDBG(sol::state& lua)
void registerHIDDBG(sol::state &lua)
{
lua.set_function("hiddbg_AttachController", lua_hiddbg_AttachController);
lua.set_function("hiddbg_DetachController", lua_hiddbg_DetachController);
Expand Down
2 changes: 1 addition & 1 deletion source/lua_hiddbg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ typedef struct
HiddbgHdlsState state;
} Controller;

void registerHIDDBG(sol::state& lua);
void registerHIDDBG(sol::state &lua);
40 changes: 20 additions & 20 deletions source/lua_svc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,39 @@ std::vector<u8> lua_svc_ReadMemory(uintptr_t mainAddr, std::vector<size_t> offse
{
u64 pid;
Result rc = pmdmntGetApplicationProcessId(&pid);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error getting process id: %#x", rc);
}

Handle debugHandle;
rc = svcDebugActiveProcess(&debugHandle, pid);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error debugging process: %#x", rc);
}

uintptr_t current = mainAddr;
for(size_t i = 0; i < offsets.size() - 1; i++)
for (size_t i = 0; i < offsets.size() - 1; i++)
{
u64 readSize = sizeof(uintptr_t); // Size of an address

rc = svcReadDebugProcessMemory(&current, debugHandle, current + offsets[i], readSize);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error reading memory addr %#lx: %#x", current, rc);
throw string_format("Error reading memory addr %#lx: %#x", current, rc);
}
}

u8 buffer[size];
rc = svcReadDebugProcessMemory(buffer, debugHandle, current + offsets.back(), size);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error reading memory addr %#lx: %#x", current, rc);
throw string_format("Error reading memory addr %#lx: %#x", current, rc);
}

rc = svcCloseHandle(debugHandle);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error closing debug handle: %#x", rc);
}
Expand All @@ -55,14 +55,14 @@ std::vector<u8> lua_svc_ReadMemory(uintptr_t mainAddr, std::vector<size_t> offse
}

// Returns some userdata in the form of a hooked address that can be used to read process memory using another command
int lua_svc_MapProcessMemory(lua_State* L)
int lua_svc_MapProcessMemory(lua_State *L)
{
u64 pid;
Result rc = pmdmntGetApplicationProcessId(&pid);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
std::size_t len = std::snprintf(nullptr, 0, "Error getting process id: %#x", rc);
char error[len+1];
char error[len + 1];
std::sprintf(error, "Error getting process id: %#x", rc);
lua_pushstring(L, error);
lua_error(L);
Expand All @@ -73,21 +73,21 @@ int lua_svc_MapProcessMemory(lua_State* L)
CfgOverrideStatus status;

rc = pmdmntAtmosphereGetProcessInfo(&processHandle, &location, &status, pid);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
std::size_t len = std::snprintf(nullptr, 0, "Error getting process info: %#x", rc);
char error[len+1];
char error[len + 1];
std::sprintf(error, "Error getting process info: %#x", rc);
lua_pushstring(L, error);
lua_error(L);
}

void* bruh = nullptr;
void *bruh = nullptr;
rc = svcMapProcessMemory(bruh, processHandle, 0, 0);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
std::size_t len = std::snprintf(nullptr, 0, "Error mapping process memory: %#x", rc);
char error[len+1];
char error[len + 1];
std::sprintf(error, "Error mapping process memory: %#x", rc);
lua_pushstring(L, error);
lua_error(L);
Expand All @@ -101,20 +101,20 @@ u64 lua_svc_GetMainAddr()
{
u64 pid;
Result rc = pmdmntGetApplicationProcessId(&pid);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
string_format("Error getting process id: %#x", rc);
}

LoaderModuleInfo procModules[2];
s32 numModules = 0;
rc = ldrDmntGetProcessModuleInfo(pid, procModules, 2, &numModules);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error getting process module info: %#x", rc);
}

if(numModules == 2)
if (numModules == 2)
{
return procModules[1].base_address;
}
Expand All @@ -124,7 +124,7 @@ u64 lua_svc_GetMainAddr()
}
}

void registerSVC(sol::state& lua)
void registerSVC(sol::state &lua)
{
lua.set_function("svc_SleepThread", lua_svc_SleepThread);
lua.set_function("svc_ReadMemory", lua_svc_ReadMemory);
Expand Down
2 changes: 1 addition & 1 deletion source/lua_svc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
#include <sol/sol.hpp>
#include "format.hpp"

void registerSVC(sol::state& lua);
void registerSVC(sol::state &lua);
12 changes: 6 additions & 6 deletions source/lua_switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ void lua_FatalThrow(u32 errorCode)
}

// Takes an event and a timeout and waits for it for the specific timeout
void lua_EventWait(Event* event, u64 timeout)
void lua_EventWait(Event *event, u64 timeout)
{
Result rc = eventWait(event, timeout);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error waiting for event: %#x", rc);
}
Expand All @@ -21,10 +21,10 @@ void lua_EventWait(Event* event, u64 timeout)
}

// Takes an event and waits for it for the maximum timeout
void lua_EventWaitMax(Event* event)
void lua_EventWaitMax(Event *event)
{
Result rc = eventWait(event, UINT64_MAX);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error waiting for event(max): %#x", rc);
}
Expand All @@ -33,14 +33,14 @@ void lua_EventWaitMax(Event* event)
}

// Closes the event specified
void lua_EventClose(Event* event)
void lua_EventClose(Event *event)
{
eventClose(event);

return;
}

void registerSwitch(sol::state& lua)
void registerSwitch(sol::state &lua)
{
lua.set_function("FatalThrow", lua_FatalThrow);
lua.set_function("EventWait", lua_EventWait);
Expand Down
2 changes: 1 addition & 1 deletion source/lua_switch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#include <sol/sol.hpp>
#include "format.hpp"

void registerSwitch(sol::state& lua);
void registerSwitch(sol::state &lua);
12 changes: 6 additions & 6 deletions source/lua_vi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ViDisplay lua_vi_OpenDefaultDisplay()
ViDisplay disp;

Result rc = viOpenDefaultDisplay(&disp);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error opening default display: %#x", rc);
}
Expand All @@ -15,10 +15,10 @@ ViDisplay lua_vi_OpenDefaultDisplay()
}

// Closes the display provided
void lua_vi_CloseDisplay(ViDisplay* disp)
void lua_vi_CloseDisplay(ViDisplay *disp)
{
Result rc = viCloseDisplay(disp);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error closing display: %#x", rc);
}
Expand All @@ -27,20 +27,20 @@ void lua_vi_CloseDisplay(ViDisplay* disp)
}

// Takes a display and returns a vsync event from that display
Event lua_vi_GetDisplayVsyncEvent(ViDisplay* disp)
Event lua_vi_GetDisplayVsyncEvent(ViDisplay *disp)
{
Event vsync_event;

Result rc = viGetDisplayVsyncEvent(disp, &vsync_event);
if(R_FAILED(rc))
if (R_FAILED(rc))
{
throw string_format("Error getting vsync event: %#x", rc);
}

return vsync_event;
}

void registerVI(sol::state& lua)
void registerVI(sol::state &lua)
{
lua.set_function("vi_OpenDefaultDisplay", lua_vi_OpenDefaultDisplay);
lua.set_function("vi_CloseDisplay", lua_vi_CloseDisplay);
Expand Down
2 changes: 1 addition & 1 deletion source/lua_vi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
#include <sol/sol.hpp>
#include "format.hpp"

void registerVI(sol::state& lua);
void registerVI(sol::state &lua);

0 comments on commit 4ebf1e8

Please sign in to comment.