Skip to content

Commit

Permalink
Formatting and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Sep 25, 2021
1 parent 13ed348 commit aa90e24
Show file tree
Hide file tree
Showing 38 changed files with 368 additions and 396 deletions.
66 changes: 33 additions & 33 deletions source/PluginManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
#include "plugin/PluginMetaInformationFactory.h"
#include "plugin/PluginInformationFactory.h"

#include "utils/logger.h"
#include "utils/ElfUtils.h"
#include "PluginManagement.h"
#include "hooks.h"
#include "globals.h"

bool PluginManagement::doRelocation(const std::vector<RelocationData> &relocData, relocation_trampolin_entry_t *tramp_data, uint32_t tramp_length, uint32_t trampolinID) {
std::map<std::string, OSDynLoad_Module> moduleHandleCache;
for (auto const &cur : relocData) {
for (auto const &cur: relocData) {
uint32_t functionAddress = 0;
const std::string &functionName = cur.getName();

Expand Down Expand Up @@ -65,7 +64,7 @@ bool PluginManagement::doRelocation(const std::vector<RelocationData> &relocData


void PluginManagement::doRelocations(const std::vector<PluginContainer> &plugins, relocation_trampolin_entry_t *trampData, uint32_t tramp_size) {
for (auto &pluginContainer : plugins) {
for (auto &pluginContainer: plugins) {
DEBUG_FUNCTION_LINE_VERBOSE("Doing relocations for plugin: %s", pluginContainer.getMetaInformation().getName().c_str());

if (!PluginManagement::doRelocation(pluginContainer.getPluginInformation().getRelocationDataList(), trampData, tramp_size, pluginContainer.getPluginInformation().getTrampolinId())) {
Expand All @@ -75,7 +74,7 @@ void PluginManagement::doRelocations(const std::vector<PluginContainer> &plugins
}

void PluginManagement::memsetBSS(const std::vector<PluginContainer> &plugins) {
for (auto &pluginContainer : plugins) {
for (auto &pluginContainer: plugins) {
auto sbssSection = pluginContainer.getPluginInformation().getSectionInfo(".sbss");
if (sbssSection) {
DEBUG_FUNCTION_LINE_VERBOSE("memset .sbss %08X (%d)", sbssSection->getAddress(), sbssSection->getSize());
Expand All @@ -98,22 +97,22 @@ void PluginManagement::RestorePatches(plugin_information_t *pluginInformation, B
}
}

void PluginManagement::unloadPlugins(plugin_information_t *gPluginInformation, MEMHeapHandle pluginHeap, BOOL freePluginData) {
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_MALLOC);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_NEWLIB);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_STDCPP);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_INIT_WUT_SOCKETS);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_DEINIT_PLUGIN);
// CallHook(gPluginInformation, WUPS_LOADER_HOOK_FINI_WUT_SOCKETS); To keep network alive we skip this.
CallHook(gPluginInformation, WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_FINI_WUT_STDCPP);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_FINI_WUT_NEWLIB);
CallHook(gPluginInformation, WUPS_LOADER_HOOK_FINI_WUT_MALLOC);

RestorePatches(gPluginInformation, true);
for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) {
plugin_information_single_t *plugin = &(gPluginInformation->plugin_data[plugin_index]);
void PluginManagement::unloadPlugins(plugin_information_t *pluginInformation, MEMHeapHandle pluginHeap, BOOL freePluginData) {
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_MALLOC);
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_NEWLIB);
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_STDCPP);
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB);
CallHook(pluginInformation, WUPS_LOADER_HOOK_INIT_WUT_SOCKETS);
CallHook(pluginInformation, WUPS_LOADER_HOOK_DEINIT_PLUGIN);
// CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_SOCKETS); To keep network alive we skip this.
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB);
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_STDCPP);
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_NEWLIB);
CallHook(pluginInformation, WUPS_LOADER_HOOK_FINI_WUT_MALLOC);

RestorePatches(pluginInformation, true);
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
plugin_information_single_t *plugin = &(pluginInformation->plugin_data[plugin_index]);
if (freePluginData) {
if (plugin->data.buffer != nullptr) {
if (plugin->data.memoryType == eMemTypeMEM2) {
Expand Down Expand Up @@ -151,7 +150,7 @@ void PluginManagement::unloadPlugins(plugin_information_t *gPluginInformation, M
}
}

memset((void *) gPluginInformation, 0, sizeof(plugin_information_t));
memset((void *) pluginInformation, 0, sizeof(plugin_information_t));
}

void PluginManagement::callInitHooks(plugin_information_t *pluginInformation) {
Expand All @@ -165,32 +164,33 @@ void module_callback(OSDynLoad_Module module,
OSDynLoad_NotifyReason reason,
OSDynLoad_NotifyData *infos) {
if (reason == OS_DYNLOAD_NOTIFY_LOADED) {
auto *gPluginInformation = (plugin_information_t *) userContext;
for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) {
FunctionPatcherPatchFunction(gPluginInformation->plugin_data[plugin_index].info.functions, gPluginInformation->plugin_data[plugin_index].info.number_used_functions);
auto *pluginInformation = (plugin_information_t *) userContext;
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
FunctionPatcherPatchFunction(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
}
}
}

void PluginManagement::PatchFunctionsAndCallHooks(plugin_information_t *gPluginInformation) {
void PluginManagement::PatchFunctionsAndCallHooks(plugin_information_t *pluginInformation) {
DEBUG_FUNCTION_LINE_VERBOSE("Patching functions");
FunctionPatcherPatchFunction(method_hooks_hooks_static, method_hooks_size_hooks_static);

DCFlushRange((void *) 0x00800000, 0x00800000);
ICInvalidateRange((void *) 0x00800000, 0x00800000);

for (int32_t plugin_index = 0; plugin_index < gPluginInformation->number_used_plugins; plugin_index++) {
CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_APPLICATION_STARTS, plugin_index);
FunctionPatcherPatchFunction(gPluginInformation->plugin_data[plugin_index].info.functions, gPluginInformation->plugin_data[plugin_index].info.number_used_functions);
CallHookEx(gPluginInformation, WUPS_LOADER_HOOK_FUNCTIONS_PATCHED, plugin_index);
for (int32_t plugin_index = 0; plugin_index < pluginInformation->number_used_plugins; plugin_index++) {
CallHookEx(pluginInformation, WUPS_LOADER_HOOK_APPLICATION_STARTS, plugin_index);
FunctionPatcherPatchFunction(pluginInformation->plugin_data[plugin_index].info.functions, pluginInformation->plugin_data[plugin_index].info.number_used_functions);
CallHookEx(pluginInformation, WUPS_LOADER_HOOK_FUNCTIONS_PATCHED, plugin_index);
}
OSDynLoad_AddNotifyCallback(module_callback, gPluginInformation);
OSDynLoad_AddNotifyCallback(module_callback, pluginInformation);
}

std::vector<PluginContainer> PluginManagement::loadPlugins(const std::vector<PluginData> &pluginList, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) {
std::vector<PluginContainer>
PluginManagement::loadPlugins(const std::vector<PluginData> &pluginList, MEMHeapHandle heapHandle, relocation_trampolin_entry_t *trampolin_data, uint32_t trampolin_data_length) {
std::vector<PluginContainer> plugins;

for (auto &pluginData : pluginList) {
for (auto &pluginData: pluginList) {
DEBUG_FUNCTION_LINE_VERBOSE("Load meta information");
auto metaInfo = PluginMetaInformationFactory::loadPlugin(pluginData);
if (metaInfo) {
Expand All @@ -203,7 +203,7 @@ std::vector<PluginContainer> PluginManagement::loadPlugins(const std::vector<Plu
}
}
uint32_t trampolineID = 0;
for (auto &pluginContainer : plugins) {
for (auto &pluginContainer: plugins) {
std::optional<PluginInformation> info = PluginInformationFactory::load(pluginContainer.getPluginData(), heapHandle, trampolin_data, trampolin_data_length, trampolineID++);
if (!info) {
DEBUG_FUNCTION_LINE("Failed to load Plugin %s", pluginContainer.getMetaInformation().getName().c_str());
Expand Down
1 change: 0 additions & 1 deletion source/PluginManagement.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once


#include <common/plugin_defines.h>
#include <vector>

Expand Down
30 changes: 15 additions & 15 deletions source/common/plugin_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#pragma once

#include <stdint.h>
#include <stddef.h>
#include <cstdint>
#include <cstddef>
#include <wums/defines/dynamic_linking_defines.h>
#include <wums/defines/export_defines.h>
#include <wums/defines/relocation_defines.h>
Expand All @@ -45,8 +45,8 @@ extern "C" {

struct plugin_section_info_t {
char name[MAXIMUM_PLUGIN_SECTION_NAME_LENGTH] = "";
uint32_t addr;
uint32_t size;
uint32_t addr{};
uint32_t size{};
};

struct plugin_meta_info_t {
Expand All @@ -57,28 +57,28 @@ struct plugin_meta_info_t {
char buildTimestamp[MAXIMUM_PLUGIN_META_FIELD_LENGTH] = "";
char descripion[MAXIMUM_PLUGIN_DESCRIPTION_LENGTH] = "";
char id[MAXIMUM_PLUGIN_META_FIELD_LENGTH] = "";
uint32_t size;
uint32_t size{};
};

struct replacement_data_hook_t {
void * func_pointer = NULL; /* [will be filled] */
wups_loader_hook_type_t type; /* [will be filled] */
void * func_pointer = nullptr; /* [will be filled] */
wups_loader_hook_type_t type{}; /* [will be filled] */
};

struct plugin_info_t {
dyn_linking_relocation_entry_t linking_entries[PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH];
dyn_linking_relocation_entry_t linking_entries[PLUGIN_DYN_LINK_RELOCATION_LIST_LENGTH]{};
plugin_section_info_t sectionInfos[MAXIMUM_PLUGIN_SECTION_LENGTH];
uint32_t number_used_functions; // Number of used function. Maximum is MAXIMUM_FUNCTION_PER_PLUGIN
function_replacement_data_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]; // Replacement information for each function.
uint32_t number_used_hooks; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN
uint32_t number_used_functions{}; // Number of used function. Maximum is MAXIMUM_FUNCTION_PER_PLUGIN
function_replacement_data_t functions[MAXIMUM_FUNCTION_PER_PLUGIN]{}; // Replacement information for each function.
uint32_t number_used_hooks{}; // Number of used hooks. Maximum is MAXIMUM_HOOKS_PER_PLUGIN
replacement_data_hook_t hooks[MAXIMUM_HOOKS_PER_PLUGIN]; // Replacement information for each function.
uint8_t trampolinId;
uint8_t trampolinId{};
void * allocatedTextMemoryAddress = nullptr;
void * allocatedDataMemoryAddress = nullptr;
};

struct plugin_data_t {
char * buffer = NULL;
char * buffer = nullptr;
size_t bufferLength = 0;
int memoryType = 0;
int heapHandle = 0;
Expand All @@ -88,15 +88,15 @@ struct plugin_information_single_t {
plugin_meta_info_t meta;
plugin_info_t info;
plugin_data_t data;
int32_t priority; // Priority of this plugin
int32_t priority{}; // Priority of this plugin
};

#define MAXIMUM_PLUGINS 32

struct plugin_information_t {
int32_t number_used_plugins = 0; // Number of used plugins. Maximum is MAXIMUM_PLUGINS
plugin_information_single_t plugin_data[MAXIMUM_PLUGINS];
dyn_linking_relocation_data_t linking_data; // RPL and function name list
dyn_linking_relocation_data_t linking_data{}; // RPL and function name list
};

struct plugin_information_on_reload_t {
Expand Down
4 changes: 2 additions & 2 deletions source/config/WUPSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class WUPSConfig {
}

~WUPSConfig() {
for (auto &element : categories) {
for (auto &element: categories) {
delete element;
}
}
Expand Down Expand Up @@ -69,7 +69,7 @@ class WUPSConfig {
\param category: The category that will be added to this config.
\return On success, the inserted category will be returned.
On error NULL will be returned. In this case the caller still has the responsibility
On error nullptr will be returned. In this case the caller still has the responsibility
for deleting the WUPSConfigCategory instance.
**/
WUPSConfigCategory *addCategory(WUPSConfigCategory *category) {
Expand Down
4 changes: 2 additions & 2 deletions source/config/WUPSConfigCategory.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class WUPSConfigCategory {
}

~WUPSConfigCategory() {
for (auto &element : items) {
for (auto &element: items) {
delete element;
}
}
Expand All @@ -53,7 +53,7 @@ class WUPSConfigCategory {
for deleting the WUPSConfigItem instance.
**/
[[nodiscard]] bool addItem(WUPSConfigItem *item) {
if(item != nullptr){
if (item != nullptr) {
items.push_back(item);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion source/config/WUPSConfigExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int32_t WUPSConfig_AddCategoryByName(WUPSConfigHandle handle, const char *catego
if (res.has_value()) {
if (out != nullptr) {
*out = reinterpret_cast<WUPSConfigCategoryHandle>(res.value());
}else{
} else {
return -3;
}
return 0;
Expand Down
2 changes: 1 addition & 1 deletion source/config/WUPSConfigItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class WUPSConfigItem {
This function will be called whenever this item should call it's (optional) given
callback with the current value.
Returns true if a valid callback could be called
Returns false if no callback was called (e.g. callback was NULL)
Returns false if no callback was called (e.g. callback was nullptr)
**/
bool callCallback() {
if (this->callbacks.callCallback != nullptr) {
Expand Down
10 changes: 5 additions & 5 deletions source/config/WUPSConfigItemExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

typedef uint32_t WUPSConfigItemHandle;

int32_t WUPSConfigItem_Create(WUPSConfigItemHandle *out, const char *configID, const char *displayName, WUPSConfigCallbacks_t callbacks, void* context) {
int32_t WUPSConfigItem_Create(WUPSConfigItemHandle *out, const char *configID, const char *displayName, WUPSConfigCallbacks_t callbacks, void *context) {
if (configID == nullptr || displayName == nullptr) {
return -1;
}
Expand All @@ -14,7 +14,7 @@ int32_t WUPSConfigItem_Create(WUPSConfigItemHandle *out, const char *configID, c
return 0;
}
return -2;
};
}

int32_t WUPSConfigItem_Destroy(WUPSConfigItemHandle handle) {
if (handle == 0) {
Expand All @@ -24,7 +24,7 @@ int32_t WUPSConfigItem_Destroy(WUPSConfigItemHandle handle) {
auto *config = reinterpret_cast<WUPSConfigItem *>(handle);
delete config;
return 0;
};
}

int32_t WUPSConfigItem_SetDisplayName(WUPSConfigItemHandle handle, const char *displayName) {
if (displayName == nullptr) {
Expand All @@ -34,7 +34,7 @@ int32_t WUPSConfigItem_SetDisplayName(WUPSConfigItemHandle handle, const char *d
auto *config = reinterpret_cast<WUPSConfigItem *>(handle);
config->setDisplayName(displayName);
return 0;
};
}

int32_t WUPSConfigItem_GetDisplayName(WUPSConfigItemHandle handle, char *out_buf, int32_t out_len) {
if (out_buf == nullptr) {
Expand All @@ -53,7 +53,7 @@ int32_t WUPSConfigItem_SetConfigID(WUPSConfigItemHandle handle, const char *conf
auto *config = reinterpret_cast<WUPSConfigItem *>(handle);
config->setConfigID(configID);
return 0;
};
}

int32_t WUPSConfigItem_GetConfigID(WUPSConfigItemHandle handle, char *out_buf, int32_t out_len) {
if (out_buf == nullptr) {
Expand Down
13 changes: 6 additions & 7 deletions source/fs/CFile.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <cstdarg>
#include <cstdio>
#include <strings.h>
#include <fs/CFile.hpp>

CFile::CFile() {
iFd = -1;
mem_file = NULL;
mem_file = nullptr;
filesize = 0;
pos = 0;
}
Expand Down Expand Up @@ -78,7 +77,7 @@ void CFile::close() {
::close(iFd);

iFd = -1;
mem_file = NULL;
mem_file = nullptr;
filesize = 0;
pos = 0;
}
Expand All @@ -99,7 +98,7 @@ int32_t CFile::read(uint8_t *ptr, size_t size) {
if (readsize <= 0)
return readsize;

if (mem_file != NULL) {
if (mem_file != nullptr) {
memcpy(ptr, mem_file + pos, readsize);
pos += readsize;
return readsize;
Expand Down Expand Up @@ -147,7 +146,7 @@ int32_t CFile::seek(long int offset, int32_t origin) {
if (iFd >= 0)
ret = ::lseek(iFd, pos, SEEK_SET);

if (mem_file != NULL) {
if (mem_file != nullptr) {
if (pos > filesize) {
pos = filesize;
}
Expand Down
Loading

0 comments on commit aa90e24

Please sign in to comment.