diff --git a/source/format.hpp b/source/format.hpp index f168439..4399b2d 100644 --- a/source/format.hpp +++ b/source/format.hpp @@ -3,12 +3,15 @@ #include #include -template -std::string string_format( const std::string& format, Args ... args ) +template +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 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 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 } diff --git a/source/lua_hid.cpp b/source/lua_hid.cpp index 97eb00c..6be20a0 100644 --- a/source/lua_hid.cpp +++ b/source/lua_hid.cpp @@ -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); diff --git a/source/lua_hid.hpp b/source/lua_hid.hpp index eee81ad..cddd200 100644 --- a/source/lua_hid.hpp +++ b/source/lua_hid.hpp @@ -4,4 +4,4 @@ #include #include "keyPad.hpp" -void registerHID(sol::state& lua); \ No newline at end of file +void registerHID(sol::state &lua); \ No newline at end of file diff --git a/source/lua_hiddbg.cpp b/source/lua_hiddbg.cpp index 219044f..ad3bb73 100644 --- a/source/lua_hiddbg.cpp +++ b/source/lua_hiddbg.cpp @@ -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; @@ -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)) { @@ -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); } @@ -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)) @@ -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); } @@ -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); } @@ -71,14 +71,19 @@ 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"; } @@ -86,7 +91,7 @@ void lua_hiddbg_SetJoystick(Controller* controller, int stickIndex, s32 x, s32 y 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); } @@ -94,7 +99,7 @@ void lua_hiddbg_SetJoystick(Controller* controller, int stickIndex, s32 x, s32 y 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); diff --git a/source/lua_hiddbg.hpp b/source/lua_hiddbg.hpp index 006e58a..a53e4a8 100644 --- a/source/lua_hiddbg.hpp +++ b/source/lua_hiddbg.hpp @@ -13,4 +13,4 @@ typedef struct HiddbgHdlsState state; } Controller; -void registerHIDDBG(sol::state& lua); +void registerHIDDBG(sol::state &lua); diff --git a/source/lua_svc.cpp b/source/lua_svc.cpp index c0820df..356cd0f 100644 --- a/source/lua_svc.cpp +++ b/source/lua_svc.cpp @@ -13,39 +13,39 @@ std::vector lua_svc_ReadMemory(uintptr_t mainAddr, std::vector 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(¤t, 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); } @@ -55,14 +55,14 @@ std::vector lua_svc_ReadMemory(uintptr_t mainAddr, std::vector 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); @@ -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); @@ -101,7 +101,7 @@ 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); } @@ -109,12 +109,12 @@ u64 lua_svc_GetMainAddr() 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; } @@ -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); diff --git a/source/lua_svc.hpp b/source/lua_svc.hpp index fb15465..82cdc05 100644 --- a/source/lua_svc.hpp +++ b/source/lua_svc.hpp @@ -8,4 +8,4 @@ #include #include "format.hpp" -void registerSVC(sol::state& lua); \ No newline at end of file +void registerSVC(sol::state &lua); \ No newline at end of file diff --git a/source/lua_switch.cpp b/source/lua_switch.cpp index 5121a30..d8a24ad 100644 --- a/source/lua_switch.cpp +++ b/source/lua_switch.cpp @@ -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); } @@ -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); } @@ -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); diff --git a/source/lua_switch.hpp b/source/lua_switch.hpp index a9d56a7..4403e4d 100644 --- a/source/lua_switch.hpp +++ b/source/lua_switch.hpp @@ -5,4 +5,4 @@ #include #include "format.hpp" -void registerSwitch(sol::state& lua); +void registerSwitch(sol::state &lua); diff --git a/source/lua_vi.cpp b/source/lua_vi.cpp index bcf490f..ccc7d66 100644 --- a/source/lua_vi.cpp +++ b/source/lua_vi.cpp @@ -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); } @@ -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); } @@ -27,12 +27,12 @@ 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); } @@ -40,7 +40,7 @@ Event lua_vi_GetDisplayVsyncEvent(ViDisplay* disp) 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); diff --git a/source/lua_vi.hpp b/source/lua_vi.hpp index 7008d63..8a53522 100644 --- a/source/lua_vi.hpp +++ b/source/lua_vi.hpp @@ -5,4 +5,4 @@ #include #include "format.hpp" -void registerVI(sol::state& lua); +void registerVI(sol::state &lua);