first commit

This commit is contained in:
Maschell 2020-05-17 20:26:31 +02:00
commit b7eb531c05
11 changed files with 705 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
/*.a
/build
*.bz2
release/
lib/
CMakeLists.txt
.idea/
cmake-build-debug/

157
Makefile Normal file
View File

@ -0,0 +1,157 @@
#-------------------------------------------------------------------------------
.SUFFIXES:
#-------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPRO)),)
$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITPRO)/wut/share/wut_rules
export VER_MAJOR := 1
export VER_MINOR := 0
export VER_PATCH := 0
VERSION := $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH)
#-------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
#-------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := source \
include \
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
CFLAGS := -Wall -Werror -save-temps \
-ffunction-sections -fdata-sections \
$(MACHDEP) \
$(BUILD_CFLAGS)
CFLAGS += $(INCLUDE) -D__WIIU__
CXXFLAGS := $(CFLAGS) -std=gnu++17
ASFLAGS := $(MACHDEP)
LDFLAGS = $(ARCH) -Wl,--gc-sections
LIBS :=
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(PORTLIBS) $(WUT_ROOT)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
DEFFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.def)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SRC := $(DEFFILES:.def=.o) $(SFILES:.s=.o) $(CFILES:.c=.o) $(CPPFILES:.cpp=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SRC)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES)))
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I.
.PHONY: all dist-bin dist-src dist install clean
#---------------------------------------------------------------------------------
all: lib/libwupsbackend.a
dist-bin: all
@tar --exclude=*~ -cjf libwupsbackend-$(VERSION).tar.bz2 include lib
dist-src:
@tar --exclude=*~ -cjf libwupsbackend-src-$(VERSION).tar.bz2 include source Makefile
dist: dist-src dist-bin
install: dist-bin
mkdir -p $(DESTDIR)$(DEVKITPRO)/wups
bzip2 -cd libwupsbackend-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/wups
lib:
@[ -d $@ ] || mkdir -p $@
release:
@[ -d $@ ] || mkdir -p $@
lib/libwupsbackend.a :$(SOURCES) $(INCLUDES) | lib release
@$(MAKE) BUILD=release OUTPUT=$(CURDIR)/$@ \
BUILD_CFLAGS="-DNDEBUG=1 -O2 -s" \
DEPSDIR=$(CURDIR)/release \
--no-print-directory -C release \
-f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -rf release lib
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT) : $(OFILES)
$(OFILES_SRC) : $(HFILES)
#---------------------------------------------------------------------------------
%_bin.h %.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

View File

@ -0,0 +1,45 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#include "PluginMetaInformation.h"
#include "PluginData.h"
class PluginContainer {
public:
PluginContainer(const PluginData &data, const PluginMetaInformation &metaInfo, uint32_t handle) : pluginData(data), metaInformation(metaInfo) {
this->handle = handle;
}
uint32_t getHandle() const {
return this->handle;
}
const PluginMetaInformation &getMetaInformation() const {
return this->metaInformation;
}
const PluginData &getPluginData() const {
return pluginData;
}
const PluginData pluginData;
const PluginMetaInformation metaInformation;
uint32_t handle = 0;
};

View File

@ -0,0 +1,29 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
class PluginData {
public:
PluginData(uint32_t handle);
uint32_t getHandle() const {
return handle;
}
uint32_t handle;
};

View File

@ -0,0 +1,101 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#include <string>
#include <vector>
#include <optional>
class PluginMetaInformation {
public:
const std::string getName() const {
return name;
}
const std::string getAuthor() const {
return this->author;
}
const std::string getVersion() const {
return this->version;
}
const std::string getLicense() const {
return this->license;
}
const std::string getBuildTimestamp() const {
return this->buildtimestamp;
}
const std::string getDescription() const {
return this->description;
}
const size_t getSize() const {
return this->size;
}
PluginMetaInformation(std::string name,
std::string author,
std::string version,
std::string license,
std::string buildtimestamp,
std::string description,
size_t size);
private:
PluginMetaInformation() {
}
void setName(const std::string &name) {
this->name = name;
}
void setAuthor(const std::string &author) {
this->author = author;
}
void setVersion(const std::string &version) {
this->version = version;
}
void setLicense(const std::string &license) {
this->license = license;
}
void setBuildTimestamp(const std::string &buildtimestamp) {
this->buildtimestamp = buildtimestamp;
}
void setDescription(const std::string &description) {
this->description = description;
}
void setSize(size_t size) {
this->size = size;
}
std::string name;
std::string author;
std::string version;
std::string license;
std::string buildtimestamp;
std::string description;
size_t size;
};

View File

@ -0,0 +1,40 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#include <optional>
#include "PluginMetaInformation.h"
#include "PluginContainer.h"
class PluginUtils {
public:
static std::optional<PluginMetaInformation> getMetaInformationForBuffer(char *buffer, size_t size);
static std::optional<PluginMetaInformation> getMetaInformationForPath(const std::string &path);
static std::vector<PluginContainer> getLoadedPlugins(uint32_t maxSize);
static std::optional<PluginContainer> getPluginForPath(const std::string &path);
static std::optional<PluginContainer> getPluginForBuffer(char *buffer, size_t size);
static void destroyPluginContainer(PluginContainer &plugin);
static void destroyPluginContainer(std::vector<PluginContainer> &vector);
static int32_t LoadAndLinkOnRestart(std::vector<PluginContainer> &plugins);
};

23
source/PluginData.cpp Normal file
View File

@ -0,0 +1,23 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <cstdint>
#include "wups_backend/PluginData.h"
PluginData::PluginData(uint32_t handle) {
this->handle = handle;
}

View File

@ -0,0 +1,37 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include "wups_backend/PluginMetaInformation.h"
#include <optional>
#include <cstring>
PluginMetaInformation::PluginMetaInformation(std::string name,
std::string author,
std::string version,
std::string license,
std::string buildtimestamp,
std::string description,
size_t size) {
this->name = name;
this->author = author;
this->size = size;
this->buildtimestamp = buildtimestamp;
this->description = description;
this->license = license;
this->version = version;
}

181
source/PluginUtils.cpp Normal file
View File

@ -0,0 +1,181 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <cstring>
#include "wups_backend/PluginUtils.h"
#include "wups_backend/PluginMetaInformation.h"
#include "wups_backend/PluginContainer.h"
#include "imports.h"
std::optional<PluginMetaInformation> PluginUtils::getMetaInformationForBuffer(char *buffer, size_t size) {
plugin_information info;
memset(&info, 0, sizeof(info));
if (WUPSGetPluginMetaInformationByBuffer(&info, buffer, size)) {
// DEBUG_FUNCTION_LINE("Failed to load meta infos for buffer %08X with size %08X\n", buffer, size);
return std::nullopt;
}
PluginMetaInformation metaInfo(info.name,
info.author,
info.version,
info.license,
info.buildTimestamp,
info.description,
info.size);
return metaInfo;
}
std::optional<PluginMetaInformation> PluginUtils::getMetaInformationForPath(const std::string &path) {
plugin_information info;
memset(&info, 0, sizeof(info));
if (WUPSGetPluginMetaInformationByPath(&info, path.c_str()) != 0) {
// DEBUG_FUNCTION_LINE("Failed to load meta infos for %s\n", path.c_str());
return std::nullopt;
}
PluginMetaInformation metaInfo(info.name,
info.author,
info.version,
info.license,
info.buildTimestamp,
info.description,
info.size);
return metaInfo;
}
std::optional<PluginContainer> PluginUtils::getPluginForPath(const std::string &path) {
auto metaInfoOpt = PluginUtils::getMetaInformationForPath(path);
if (!metaInfoOpt) {
return std::nullopt;
}
plugin_data_handle dataHandle;
if (WUPSLoadPluginAsDataByPath(&dataHandle, path.c_str()) != 0) {
// DEBUG_FUNCTION_LINE("Failed to load data");
return std::nullopt;
}
return PluginContainer(PluginData(dataHandle), metaInfoOpt.value(), 0);
}
std::optional<PluginContainer> PluginUtils::getPluginForBuffer(char *buffer, size_t size) {
auto metaInfoOpt = PluginUtils::getMetaInformationForBuffer(buffer, size);
if (!metaInfoOpt) {
return std::nullopt;
}
plugin_data_handle dataHandle;
if (WUPSLoadPluginAsDataByBuffer(&dataHandle, buffer, size) != 0) {
// DEBUG_FUNCTION_LINE("Failed to load data");
return std::nullopt;
}
return PluginContainer(PluginData(dataHandle), metaInfoOpt.value(), 0);
}
std::vector<PluginContainer> PluginUtils::getLoadedPlugins(uint32_t maxSize) {
std::vector<PluginContainer> result;
plugin_container_handle handles[maxSize];
uint32_t realSize = 0;
if (WUPSGetLoadedPlugins(handles, maxSize, &realSize) != 0) {
// DEBUG_FUNCTION_LINE("Failed");
return result;
}
if (realSize == 0) {
// DEBUG_FUNCTION_LINE("realsize is 0");
return result;
}
plugin_data_handle dataHandles[realSize];
if (WUPSGetPluginDataForContainerHandles(handles, dataHandles, realSize) != 0) {
// DEBUG_FUNCTION_LINE("Failed to get plugin data");
return result;
}
plugin_information information[realSize];
if (WUPSGetMetaInformation(handles, information, realSize) != 0) {
// DEBUG_FUNCTION_LINE("Failed to get meta information for handles");
return result;
}
for (uint32_t i = 0; i < realSize; i++) {
PluginMetaInformation metaInfo(information[i].name,
information[i].author,
information[i].version,
information[i].license,
information[i].buildTimestamp,
information[i].description,
information[i].size);
PluginData pluginData((uint32_t) dataHandles[i]);
result.push_back(PluginContainer(pluginData, metaInfo, handles[i]));
}
return result;
}
void PluginUtils::destroyPluginContainer(PluginContainer &plugin) {
std::vector<PluginContainer> list;
list.push_back(plugin);
return destroyPluginContainer(list);
}
void PluginUtils::destroyPluginContainer(std::vector<PluginContainer> &plugins) {
uint32_t containerSize = plugins.size();
uint32_t dataSize = containerSize;
plugin_container_handle container_handles[containerSize];
plugin_data_handle data_handles[dataSize];
uint32_t cntC = 0;
uint32_t cntD = 0;
for (auto &plugin : plugins) {
if (plugin.getHandle() != 0) {
container_handles[cntC] = plugin.getHandle();
cntC++;
} else {
containerSize--;
}
if (plugin.pluginData.getHandle() != 0) {
data_handles[cntD] = plugin.pluginData.getHandle();
cntD++;
} else {
dataSize--;
}
}
WUPSDeletePluginContainer(container_handles, containerSize);
WUPSDeletePluginData(data_handles, dataSize);
}
int32_t PluginUtils::LoadAndLinkOnRestart(std::vector<PluginContainer> &plugins) {
uint32_t dataSize = plugins.size();
plugin_data_handle handles[dataSize];
int i = 0;
for (auto &plugin:plugins) {
plugin_data_handle handle = plugin.getPluginData().getHandle();
if (handle == 0) {
dataSize--;
} else {
handles[i] = handle;
i++;
}
}
int res = WUPSLoadAndLinkByDataHandle(handles, dataSize);
// DEBUG_FUNCTION_LINE("%d", res);
return res;
}

69
source/imports.h Normal file
View File

@ -0,0 +1,69 @@
/****************************************************************************
* Copyright (C) 2019,2020 Maschell
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef enum GetPluginInformationInputType {
PLUGIN_INFORMATION_INPUT_TYPE_PATH = 0,
PLUGIN_INFORMATION_INPUT_TYPE_BUFFER = 1,
} GetPluginInformationInputType;
typedef uint32_t plugin_container_handle;
typedef uint32_t plugin_data_handle;
/* plugin_information message */
typedef struct __attribute__((__packed__)) plugin_information {
char name[256];
char author[256];
char buildTimestamp[256];
char description[256];
char license[256];
char version[256];
size_t size;
} plugin_information;
int32_t WUPSLoadAndLinkByDataHandle(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size);
int32_t WUPSDeletePluginContainer(const plugin_container_handle *handle_list, uint32_t handle_list_size);
int32_t WUPSDeletePluginData(const plugin_data_handle *plugin_data_handle_list, uint32_t plugin_data_handle_list_size);
int32_t WUPSLoadPluginAsData(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_data_handle *out);
int32_t WUPSLoadPluginAsDataByPath(plugin_data_handle *output, const char *path);
int32_t WUPSLoadPluginAsDataByBuffer(plugin_data_handle *output, char *buffer, size_t size);
int32_t WUPSGetPluginMetaInformation(GetPluginInformationInputType inputType, const char *path, char *buffer, size_t size, plugin_information *output);
int32_t WUPSGetPluginMetaInformationByPath(plugin_information *output, const char *path);
int32_t WUPSGetPluginMetaInformationByBuffer(plugin_information *output, char *buffer, size_t size);
int32_t WUPSGetPluginDataForContainerHandles(const plugin_container_handle *plugin_container_handle_list, const plugin_data_handle *plugin_data_list, uint32_t buffer_size);
int32_t WUPSGetMetaInformation(const plugin_container_handle *plugin_container_handle_list, plugin_information *plugin_information_list, uint32_t buffer_size);
int32_t WUPSGetLoadedPlugins(const plugin_container_handle *io_handles, uint32_t buffer_size, uint32_t *outSize);
#ifdef __cplusplus
}
#endif

15
source/wupsbackend.def Normal file
View File

@ -0,0 +1,15 @@
:NAME homebrew_wupsbackend
:TEXT
WUPSLoadPluginAsDataByPath
WUPSLoadPluginAsDataByBuffer
WUPSLoadPluginAsData
WUPSLoadAndLinkByDataHandle
WUPSDeletePluginContainer
WUPSDeletePluginData
WUPSGetPluginMetaInformation
WUPSGetPluginMetaInformationByPath
WUPSGetPluginMetaInformationByBuffer
WUPSGetMetaInformation
WUPSGetLoadedPlugins
WUPSGetPluginDataForContainerHandles