SDHotSwapModule/source/exports.cpp
2022-09-03 23:54:57 +02:00

109 lines
2.4 KiB
C++

#include "exports.h"
#include <cstring>
#include <sdutils/sdutils.h>
#include <wums.h>
#define MAX_HANDLERS 16
static SDAttachHandlerFn sHandlers[MAX_HANDLERS] = {nullptr};
static SDCleanUpHandlesHandlerFn sCleanupHandlesHandlers[MAX_HANDLERS] = {nullptr};
void callAttachCallbacks(SDUtilsAttachStatus status) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sHandlers[i]) {
sHandlers[i](status);
}
}
}
void callCleanUpHandlesCallbacks() {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i]) {
sCleanupHandlesHandlers[i]();
}
}
}
void cleanUpAttachCallbacks() {
memset(sHandlers, 0, sizeof(sHandlers));
}
void cleanUpCleanUpHandlesCallbacks() {
memset(sCleanupHandlesHandlers, 0, sizeof(sCleanupHandlesHandlers));
}
bool SDUtilsAddAttachHandler(SDAttachHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sHandlers[i] == fn) {
return true;
}
if (!sHandlers[i]) {
sHandlers[i] = fn;
return true;
}
}
return false;
}
bool SDUtilsRemoveAttachHandler(SDAttachHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sHandlers[i] == fn) {
sHandlers[i] = nullptr;
return true;
}
}
return false;
}
bool SDUtilsAddCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i] == fn) {
return true;
}
if (!sCleanupHandlesHandlers[i]) {
sCleanupHandlesHandlers[i] = fn;
return true;
}
}
return false;
}
bool SDUtilsRemoveCleanUpHandlesHandler(SDCleanUpHandlesHandlerFn fn) {
int i;
for (i = 0; i < MAX_HANDLERS; ++i) {
if (sCleanupHandlesHandlers[i] == fn) {
sCleanupHandlesHandlers[i] = nullptr;
return true;
}
}
return false;
}
SDUtilsStatus SDUtilsGetVersion(SDUtilsVersion *outVersion) {
if (outVersion == nullptr) {
return SDUTILS_RESULT_INVALID_ARGUMENT;
}
*outVersion = 1;
return SDUTILS_RESULT_SUCCESS;
}
WUMS_EXPORT_FUNCTION(SDUtilsGetVersion);
WUMS_EXPORT_FUNCTION(SDUtilsAddAttachHandler);
WUMS_EXPORT_FUNCTION(SDUtilsRemoveAttachHandler);
WUMS_EXPORT_FUNCTION(SDUtilsAddCleanUpHandlesHandler);
WUMS_EXPORT_FUNCTION(SDUtilsRemoveCleanUpHandlesHandler);