[Plugins] Updated the SDCafiine plugin

- Now using the FSWrapper lib
- Added the modpack selector
- Still only support for games with nativ SD Access
This commit is contained in:
Maschell 2018-02-12 20:50:27 +01:00
parent b9af38c7ff
commit 48e5ea7e4c
25 changed files with 253 additions and 1814 deletions

View File

@ -35,8 +35,8 @@ TARGET := $(notdir $(CURDIR)).mod
BUILD := build BUILD := build
SOURCES := src \ SOURCES := src \
src/common \ src/common \
src/fs_wrapper \ src/myutils \
src/utils src/myfs
DATA := DATA :=
@ -71,7 +71,7 @@ CFLAGS += $(COMMON_CFLAGS) -x c -std=c11
# -x c: compile as c++ code # -x c: compile as c++ code
# -std=gnu++11: use the c++11 standard # -std=gnu++11: use the c++11 standard
CXXFLAGS += $(COMMON_CFLAGS) -x c++ -std=gnu++11 CXXFLAGS += $(COMMON_CFLAGS) -x c++ -std=gnu++11 -D_GNU_SOURCE
ifeq ($(DO_LOGGING), 1) ifeq ($(DO_LOGGING), 1)
CFLAGS += -D__LOGGING__ CFLAGS += -D__LOGGING__
@ -99,7 +99,7 @@ MAKEFLAGS += --no-print-directory
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project # any extra libraries we wish to link with the project
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
LIBS := -lutils -ldynamiclibs -liosuhax LIBS := -liosuhax -lfswrapper -lutils -ldynamiclibs
# #
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
@ -150,7 +150,8 @@ export OFILES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD) -I$(PORTLIBS)/include \ -I$(CURDIR)/$(BUILD) -I$(PORTLIBS)/include \
-I$(PORTLIBS)/include/libutils -I$(WUPSDIR)/include -I$(PORTLIBS)/include/libutils -I$(PORTLIBS)/include/libfswrapper \
-I$(WUPSDIR)/include
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# build a list of library paths # build a list of library paths

View File

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="sdcafiine" />
<Option pch_mode="2" />
<Option compiler="ppc-gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/sdcafiine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="ppc-gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/sdcafiine" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="ppc-gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Extensions>
<code_completion />
<envvars />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

View File

@ -9,6 +9,3 @@ char gModFolder[FS_MAX_ENTNAME_SIZE] __attribute__((section(".data")));
void * ntfs_mounts __attribute__((section(".data"))) = NULL; void * ntfs_mounts __attribute__((section(".data"))) = NULL;
int ntfs_mount_count __attribute__((section(".data"))) = 0; int ntfs_mount_count __attribute__((section(".data"))) = 0;
vc_vector* g_dirhandles = NULL;
vc_vector* g_filehandles = NULL;

View File

@ -2,7 +2,6 @@
#define RETAINS_VARS_H_ #define RETAINS_VARS_H_
#include <dynamic_libs/fs_defs.h> #include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/os_types.h> #include <dynamic_libs/os_types.h>
#include "utils/vc_vector.h"
#define ASYNC_RESULT_CACHE_SIZE 50 #define ASYNC_RESULT_CACHE_SIZE 50
#define FS_QUEUE_MESSAGE_COUNT 5 #define FS_QUEUE_MESSAGE_COUNT 5
@ -17,7 +16,4 @@ extern char gModFolder[FS_MAX_ENTNAME_SIZE];
extern void * ntfs_mounts; extern void * ntfs_mounts;
extern int ntfs_mount_count; extern int ntfs_mount_count;
extern vc_vector* g_dirhandles;
extern vc_vector* g_filehandles;
#endif // RETAINS_VARS_H_ #endif // RETAINS_VARS_H_

View File

@ -1,98 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 "FileReplacerUtils.h"
#include "fs_async_wrapper.h"
#include "common/retain_vars.h"
OSMessageQueue lfFSQueue __attribute__((section(".data")));
OSMessage lfFSQueueMessages[FS_QUEUE_MESSAGE_COUNT] __attribute__((section(".data")));
FSAsyncResult lfAsyncResultCache[ASYNC_RESULT_CACHE_SIZE] __attribute__((section(".data")));
u8 lfAsyncResultCacheLock __attribute__((section(".data"))) = 0;
u8 lfAsyncResultCacheCur __attribute__((section(".data"))) = 0;
int setErrorFlag(int error){
int result = error;
if(error == -1){
result = CHECKED_WITH_ALL_ERRORS;
}else{
result |= CHECKED_MASK;
}
return result;
}
int checkErrorFlag(int * error){
if(*error == CHECKED_WITH_ALL_ERRORS){
*error = -1;
return true;
}else if ((*error & CHECKED_MASK) == CHECKED_MASK){
*error &= ~CHECKED_MASK;
return true;
}
return false;
}
void addFileHandleInternal(int handle){
vc_vector_push_back(g_filehandles, &handle);
}
void removeFileHandleInternal(int handle){
int index = 0;
for (void* i = vc_vector_begin(g_filehandles);i != vc_vector_end(g_filehandles); i = vc_vector_next(g_filehandles, i)) {
int val = *(int*)i;
if(val == handle){
vc_vector_erase(g_filehandles,index);
return;
}
index++;
}
}
bool hasFileHandleInternal(int handle){
for (void* i = vc_vector_begin(g_filehandles);i != vc_vector_end(g_filehandles); i = vc_vector_next(g_filehandles, i)) {
if(*(int*)i == handle){
return true;
}
}
return false;
}
void addDirHandleInternal(int handle){
vc_vector_push_back(g_dirhandles, &handle);
}
void removeDirHandleInternal(int handle){
int index = 0;
for (void* i = vc_vector_begin(g_dirhandles);i != vc_vector_end(g_dirhandles); i = vc_vector_next(g_dirhandles, i)) {
int val = *(int*)i;
if(val == handle){
vc_vector_erase(g_dirhandles,index);
return;
}
index++;
}
}
bool hasDirHandleInternal(int handle){
for (void* i = vc_vector_begin(g_dirhandles);i != vc_vector_end(g_dirhandles); i = vc_vector_next(g_dirhandles, i)) {
if(*(int*)i == handle){
return true;
}
}
return false;
}

View File

@ -1,64 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef __FILE_REPLACER_UTILS_H_
#define __FILE_REPLACER_UTILS_H_
#include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/os_types.h>
#include <system/CThread.h>
#include "utils/vc_vector.h"
#include "fs_retain_vars.h"
#define CHECKED_WITH_ALL_ERRORS 0x10000
#define CHECKED_MASK 0x01000
#define FS_WRAPPER_DEBUG_LOG 0
#define USE_OS_FS_FUNCTION -1337
/**
Returns a modified error flag.
This will be used to save the information if a file/handle was already
tried to be patched.
The non-async function internally call the async functions, and this way
we avoid testing it twice.
If the result contains our mask, we just straight to the OS functions.
**/
int setErrorFlag(int error);
/**
Check if we already checked the file/handle.
Returns true if it was already checked (+ revert the error)
Return false if it should be (tried) to be patched.
**/
int checkErrorFlag(int * error);
void addFileHandleInternal(int handle);
void removeFileHandleInternal(int handle);
bool hasFileHandleInternal(int handle);
void addDirHandleInternal(int handle);
void removeDirHandleInternal(int handle);
bool hasDirHandleInternal(int handle);
#endif // __FILE_REPLACER_UTILS_H_

View File

@ -1,217 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utils/logger.h>
#include "fs_async_wrapper.h"
//Wii U fails to allocate memory if we do the functions async. =/
#define DO_REAL_ASYNC 0
static int doFallback(CustomAsyncParamWrapper params){
if(params.fallbackFunction != NULL){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Calling the fallback function %08X\n",params.fallbackFunction); }
return ((FSAsyncFallback)params.fallbackFunction)(params.fallbackParams);
}
if(params.params.needToFreePath){
free((void*)params.params.params.path);
}
DEBUG_FUNCTION_LINE("No fallback function provided\n");
//OSFatal("No fallback given."); //We don't need to free stuff, because we'll (want to) crash anyway.
return FS_STATUS_FATAL_ERROR;
}
static int sendAsyncResult(CustomAsyncParamWrapper in, int result){
//FileReplacerUtils::sendAsyncCommand(in.params.params.pClient, in.params.params.pCmd, in.params.params.asyncParams, result);
if(in.params.needToFreePath){
free((void*)in.params.params.path);
}
return FS_STATUS_OK;
}
static int fs_wrapper_async_template(CustomAsyncParam params,FSAsyncCustomCallback callback, FSAsyncFallback fallback, void * fallbackParams){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called!\n"); }
CustomAsyncParamWrapper wrapper;
memset(&wrapper,0,sizeof(CustomAsyncParamWrapper));
wrapper.params = params;
wrapper.fallbackFunction = fallback;
wrapper.fallbackParams = (void*)fallbackParams;
if(!DO_REAL_ASYNC){
return callback(wrapper);
}
//TODO:!
/*OSMessage message;
message.message = (u32) callback;
message.data0 = (u32) &wrapper;
//FileReplacerUtils::addFSQueueMSG(&message);*/
return FS_STATUS_OK;
}
int fs_wrapper_FSCloseFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSCloseFile(p->handle)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSGetPosFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSGetPosFile(p->handle,p->posPtr)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSGetStatAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSGetStat(p->path,p->stats)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSGetStatFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSGetStatFile(p->handle,p->stats)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSIsEofAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSIsEof(p->handle)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSOpenFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSOpenFile(p->path,p->mode,p->handlePtr)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSReadFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSReadFile(p->handle,p->buffer,p->size,p->count)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSReadFileWithPosAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSReadFileWithPos(p->buffer,p->size,p->count,p->pos,p->handle)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
int fs_wrapper_FSSetPosFileAsyncCallback(CustomAsyncParamWrapper params){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! params: %08X \n",params); }
//if(params == NULL){ DEBUG_FUNCTION_LINE("!!!WARNING: Given parameter was NULL\n"); }
OSAsyncParamWrapper * p = &(params.params.params);
int result = USE_OS_FS_FUNCTION;
if((result = fs_wrapper_FSSetPosFile(p->handle,p->pos)) != USE_OS_FS_FUNCTION){
return sendAsyncResult(params,result);
}
return doFallback(params);
}
DEFINE_FS_WRAPPER(FSCloseFileAsync)
DEFINE_FS_WRAPPER(FSGetStatAsync)
DEFINE_FS_WRAPPER(FSGetStatFileAsync)
DEFINE_FS_WRAPPER(FSGetPosFileAsync)
DEFINE_FS_WRAPPER(FSIsEofAsync)
DEFINE_FS_WRAPPER(FSOpenFileAsync)
DEFINE_FS_WRAPPER(FSReadFileAsync)
DEFINE_FS_WRAPPER(FSReadFileWithPosAsync)
DEFINE_FS_WRAPPER(FSSetPosFileAsync)

View File

@ -1,124 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef __FS_ASYNC_WRAPPER_H_
#define __FS_ASYNC_WRAPPER_H_
#include "FileReplacerUtils.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/fs_functions.h>
#include <unistd.h>
#include "fs_sync_wrapper.h"
typedef int (*FSAsyncFallback)(void * params);
typedef struct OSAsyncParamWrapper_{
FSClient *pClient;
FSCmdBlock *pCmd;
const char *path;
const char *mode;
FSStat * stats;
void * buffer;
int handle;
int size;
int count;
int pos;
int flag;
int *handlePtr;
int *posPtr;
int error;
FSAsyncParams * asyncParams;
} OSAsyncParamWrapper;
typedef struct FallbackParamWrapper_{
OSAsyncParamWrapper params;
void * realFunctionAddress;
} FallbackParamWrapper;
typedef struct CustomAsyncParam_{
OSAsyncParamWrapper params;
bool needToFreePath;
} CustomAsyncParam;
typedef struct CustomAsyncParamWrapper_{
CustomAsyncParam params;
FSAsyncFallback fallbackFunction;
void * fallbackParams;
} CustomAsyncParamWrapper;
typedef int (*FSAsyncCustomCallback)(CustomAsyncParamWrapper params);
#define DEFINE_FS_WRAPPER(name) \
int fs_wrapper_##name##Ex(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams){\
return fs_wrapper_async_template(params,fs_wrapper_##name##Callback,fallback,fallbackParams);\
}\
int fs_wrapper_##name(CustomAsyncParam params){ \
return fs_wrapper_##name##Ex(params,NULL,NULL);\
}
#define DECLARE_FS_WRAPPER(name) \
int fs_wrapper_##name##Ex(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);\
int fs_wrapper_##name(CustomAsyncParam params);
int fs_wrapper_FSCloseFileAsync(CustomAsyncParam params);
int fs_wrapper_FSCloseFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSGetStatAsync(CustomAsyncParam params);
int fs_wrapper_FSGetStatAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSGetStatFileAsync(CustomAsyncParam params);
int fs_wrapper_FSGetStatFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSGetPosFileAsync(CustomAsyncParam params);
int fs_wrapper_FSGetPosFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSIsEofAsync(CustomAsyncParam params);
int fs_wrapper_FSIsEofAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSOpenFileAsync(CustomAsyncParam params);
int fs_wrapper_FSOpenFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSReadFileAsync(CustomAsyncParam params);
int fs_wrapper_FSReadFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSReadFileWithPosAsync(CustomAsyncParam params);
int fs_wrapper_FSReadFileWithPosAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
int fs_wrapper_FSSetPosFileAsync(CustomAsyncParam params);
int fs_wrapper_FSSetPosFileAsyncEx(CustomAsyncParam params, FSAsyncFallback fallback, void * fallbackParams);
#ifdef __cplusplus
}
#endif
#endif // __FS_ASYNC_WRAPPER_H_

View File

@ -1,220 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 <utils/logger.h>
#include "fs_default_os_wrapper.h"
int fs_default_os_wrapper_FSCloseFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int error, FSAsyncParams * asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.handle = fd;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSCloseFileAsyncEx(params,&fallbackFSCloseFileAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSGetPosFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int *pos, int error, FSAsyncParams * asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.handle = fd;
paramWrapper.params.posPtr = pos;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSGetPosFileAsyncEx(params,fallbackFSGetPosFileAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSGetStatAsync(const char *libPath, FSClient *pClient, FSCmdBlock *pCmd, const char *path, FSStat *stats, int error, FSAsyncParams * asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.path = (char*)path;
paramWrapper.params.stats = stats;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
params.params.path = libPath; // Use modified path.
params.needToFreePath = 1;
return fs_wrapper_FSGetPosFileAsyncEx(params,fallbackFSGetStatAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSGetStatFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, FSStat * stats, int error, FSAsyncParams * asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.handle = fd;
paramWrapper.params.stats = stats;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSGetStatFileAsyncEx(params,fallbackFSGetStatFileAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSIsEofAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int error, FSAsyncParams * asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.handle = fd;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSIsEofAsyncEx(params,fallbackFSIsEofAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSOpenFileAsync(const char *libpath, FSClient *pClient, FSCmdBlock *pCmd, const char *path, const char *mode, int *handle, int error, FSAsyncParams *asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.path = path;
paramWrapper.params.mode = mode;
paramWrapper.params.handlePtr = handle;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
params.params.path = libpath; // Use modified path.
params.needToFreePath = 1;
return fs_wrapper_FSOpenFileAsyncEx(params,fallbackFSOpenFileAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSReadFileAsync(FSClient *pClient, FSCmdBlock *pCmd, void *buffer, int size, int count, int fd, int flag, int error, FSAsyncParams *asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.buffer = buffer;
paramWrapper.params.size = size;
paramWrapper.params.count = count;
paramWrapper.params.handle = fd;
paramWrapper.params.flag = flag;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSReadFileAsyncEx(params,fallbackFSReadFileAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSReadFileWithPosAsync(FSClient *pClient, FSCmdBlock *pCmd, void *buffer, int size, int count, u32 pos, int fd, int flag, int error, FSAsyncParams *asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.buffer = buffer;
paramWrapper.params.size = size;
paramWrapper.params.count = count;
paramWrapper.params.pos = pos;
paramWrapper.params.handle = fd;
paramWrapper.params.flag = flag;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSReadFileWithPosAsyncEx(params,fallbackFSReadFileWithPosAsync,&paramWrapper);
}
int fs_default_os_wrapper_FSSetPosFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int handle, u32 pos, int error, FSAsyncParams *asyncParams, void * realAddress){
FallbackParamWrapper paramWrapper;
memset(&paramWrapper,0,sizeof(FallbackParamWrapper));
CustomAsyncParam params;
memset(&params,0,sizeof(CustomAsyncParam));
paramWrapper.params.pClient = pClient;
paramWrapper.params.pCmd = pCmd;
paramWrapper.params.handle = handle;
paramWrapper.params.pos = pos;
paramWrapper.params.error = error;
paramWrapper.params.asyncParams = asyncParams;
paramWrapper.realFunctionAddress = (void*) realAddress;
params.params = paramWrapper.params;
return fs_wrapper_FSSetPosFileAsyncEx(params,fallbackFSSetPosFileAsync,&paramWrapper);
}

View File

@ -1,43 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef __FS_DEFAULT_OS_WRAPPER_H_
#define __FS_DEFAULT_OS_WRAPPER_H_
#include "fs_async_wrapper.h"
#include "fs_sync_wrapper.h"
#include "fs_wrapper_utils.h"
#ifdef __cplusplus
extern "C" {
#endif
int fs_default_os_wrapper_FSCloseFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int error, FSAsyncParams * asyncParams, void * realAddress);
int fs_default_os_wrapper_FSGetPosFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int *pos, int error, FSAsyncParams * asyncParams, void * realAddress);
int fs_default_os_wrapper_FSGetStatAsync(const char *libpath, FSClient *pClient, FSCmdBlock *pCmd, const char *path, FSStat *stats, int error, FSAsyncParams * asyncParams, void * realAddress);
int fs_default_os_wrapper_FSGetStatFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, FSStat * stats, int error, FSAsyncParams * asyncParams, void * realAddress);
int fs_default_os_wrapper_FSIsEofAsync(FSClient *pClient, FSCmdBlock *pCmd, int fd, int error, FSAsyncParams * asyncParams, void * realAddress);
int fs_default_os_wrapper_FSOpenFileAsync(const char *libpath, FSClient *pClient, FSCmdBlock *pCmd, const char *path, const char *mode, int *handle, int error, FSAsyncParams *asyncParams, void * realAddress);
int fs_default_os_wrapper_FSReadFileAsync(FSClient *pClient, FSCmdBlock *pCmd, void *buffer, int size, int count, int fd, int flag, int error, FSAsyncParams *asyncParams, void * realAddress);
int fs_default_os_wrapper_FSReadFileWithPosAsync(FSClient *pClient, FSCmdBlock *pCmd, void *buffer, int size, int count, u32 pos, int fd, int flag, int error, FSAsyncParams *asyncParams, void * realAddress);
int fs_default_os_wrapper_FSSetPosFileAsync(FSClient *pClient, FSCmdBlock *pCmd, int handle, u32 pos, int error, FSAsyncParams *asyncParams, void * realAddress);
#ifdef __cplusplus
}
#endif
#endif // __FS_DEFAULT_OS_WRAPPER_H_

View File

@ -1,28 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 <dynamic_libs/os_types.h>
#include "fs_retain_vars.h"
OSMessageQueue fsFSQueue __attribute__((section(".data")));
OSMessage fsFSQueueMessages[FS_QUEUE_MESSAGE_COUNT] __attribute__((section(".data")));
FSAsyncResult fsAsyncResultCache[ASYNC_RESULT_CACHE_SIZE] __attribute__((section(".data")));
u8 fsAsyncResultCacheLock __attribute__((section(".data"))) = 0;
u8 fsAsyncResultCacheCur __attribute__((section(".data"))) = 0;
u32 global_owner_id __attribute__((section(".data"))) = 0;
u32 global_group_id __attribute__((section(".data"))) = 0;

View File

@ -1,38 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef FS_RETAINS_VARS_H_
#define FS_RETAINS_VARS_H_
#include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/os_types.h>
#define ASYNC_RESULT_CACHE_SIZE 50
#define FS_QUEUE_MESSAGE_COUNT 5
extern OSMessageQueue fsFSQueue __attribute__((section(".data")));
extern OSMessage fsFSQueueMessages[FS_QUEUE_MESSAGE_COUNT] __attribute__((section(".data")));
extern FSAsyncResult fsAsyncResultCache[ASYNC_RESULT_CACHE_SIZE];
extern u8 fsAsyncResultCacheLock;
extern u8 fsAsyncResultCacheCur;
extern u32 global_owner_id;
extern u32 global_group_id;
#endif // FS_RETAINS_VARS_H_

View File

@ -1,226 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <utils/logger.h>
#include "fs_sync_wrapper.h"
#include "fs_retain_vars.h"
int fs_wrapper_FSCloseFile(int handle){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! for handle: %08X \n",handle); }
if(hasFileHandleInternal(handle)){
removeFileHandleInternal(handle);
close(handle);
DEBUG_FUNCTION_LINE("closed handle %08X\n",handle);
return FS_STATUS_OK;
}
return USE_OS_FS_FUNCTION;
}
int fs_wrapper_FSGetPosFile(int handle,int * pos){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! for handle: %08X \n",handle); }
if(hasFileHandleInternal(handle)){
off_t currentPos = lseek(handle, (off_t)0, SEEK_CUR);
*pos = currentPos;
DEBUG_FUNCTION_LINE("pos %08X for handle %08X\n",*pos,handle);
return FS_STATUS_OK;
}
return USE_OS_FS_FUNCTION;
}
void setCommonStats(FSStat * stats){
if(stats == NULL){
DEBUG_FUNCTION_LINE("STATS EMPTY\n");
}
stats->permission = 0x00000400;
stats->owner_id = global_owner_id;
stats->group_id = global_group_id;
stats->flag |= 0x08000000;
stats->flag |= 0x04000000;
stats->ctime = 0x0003E8C3E677A740L;
stats->mtime = 0x0003F8AABBEAFBC0L;
}
int fs_wrapper_FSGetStat(const char * path, FSStat * stats){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called!\n"); }
int result = USE_OS_FS_FUNCTION;
if(path != NULL){
DEBUG_FUNCTION_LINE("Searching for path %s\n",path);
struct stat path_stat;
if(stat(path, &path_stat) < 0){
result = USE_OS_FS_FUNCTION;
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("failed for path %s\n",path); }
}else{
DEBUG_FUNCTION_LINE("success! path %s\n",path);
stats->flag = 0;
if(S_ISDIR(path_stat.st_mode)){
stats->flag |= 0x80000000;
DEBUG_FUNCTION_LINE("set stats->flag: DIR\n");
}else{
stats->size = path_stat.st_size;
DEBUG_FUNCTION_LINE("stats->size: %08X\n",stats->size);
}
setCommonStats(stats);
result = FS_STATUS_OK;
}
}
return result;
}
int fs_wrapper_FSGetStatFile(int handle, FSStat * stats){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! for handle: %08X\n",handle); }
if(hasFileHandleInternal(handle)){
struct stat path_stat;
if(fstat(handle, &path_stat) < 0){
DEBUG_FUNCTION_LINE("failed! handle: %08X\n",handle);
return -1;
}
stats->size = path_stat.st_size;
stats->flag = 0;
setCommonStats(stats);
DEBUG_FUNCTION_LINE("success! handle: %08X size: %08X\n",handle,stats->size);
return FS_STATUS_OK;
}
return USE_OS_FS_FUNCTION;
}
int fs_wrapper_FSIsEof(int handle){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! handle: %08X\n",handle); }
int result = USE_OS_FS_FUNCTION;
if(hasFileHandleInternal(handle)){
off_t currentPos = lseek(handle, (off_t) 0, SEEK_CUR);
off_t endPos = lseek(handle, (off_t) 0, SEEK_END);
if(currentPos == endPos){
result = FS_STATUS_EOF;
}else{
lseek(handle, currentPos, SEEK_CUR);
result = FS_STATUS_OK;
}
DEBUG_FUNCTION_LINE("handle: %08X result: %08X\n",handle,result);
}
return result;
}
int fs_wrapper_FSOpenFile(const char * path, const char * mode, int * handle){
int result = USE_OS_FS_FUNCTION;
if(path != NULL){
result = USE_OS_FS_FUNCTION;
DEBUG_FUNCTION_LINE("Searching for path %s\n",path);
int fd = open(path,O_RDONLY); //TODO: remove hardcoded mode.
if(fd != -1){
DEBUG_FUNCTION_LINE("opened path: %s handle: %08X\n",path,fd);
addFileHandleInternal(fd);
*handle = fd;
result = FS_STATUS_OK;
}else{
if(FS_WRAPPER_DEBUG_LOG){DEBUG_FUNCTION_LINE("failed path: %s\n",path);}
}
}
return result;
}
#define MAXIMUM_READ_CHUNK 1024*1024
static int readIntoBuffer(int handle,void *buffer,size_t size, size_t count){
int sizeToRead = size*count;
void * newBuffer = buffer;
int curResult = -1;
int totalSize = 0;
int toRead = 0;
while(sizeToRead > 0){
if(sizeToRead < MAXIMUM_READ_CHUNK){
toRead = sizeToRead;
}else{
toRead = MAXIMUM_READ_CHUNK;
}
curResult = read(handle, newBuffer,toRead);
if(curResult < 0){
DEBUG_FUNCTION_LINE("Error: Reading %08X bytes from handle %08X. result %08X \n",size*count,handle,curResult);
return -1;
}
if(curResult == 0 ){
//EOF
break;
}
newBuffer = (void*)(((u32)newBuffer) + curResult);
totalSize += curResult;
sizeToRead -= curResult;
if(sizeToRead > 0){
DEBUG_FUNCTION_LINE("Reading. missing %08X bytes\n",sizeToRead);
}
}
DEBUG_FUNCTION_LINE("Success: Read %08X bytes from handle %08X. result %08X \n",size*count,handle,totalSize);
return totalSize;
}
int fs_wrapper_FSReadFile(int handle,void *buffer,size_t size, size_t count){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! for handle: %08X \n",handle); }
int result = USE_OS_FS_FUNCTION;
if(hasFileHandleInternal(handle)){
result = readIntoBuffer(handle,buffer,size,count);
}
return result;
}
int fs_wrapper_FSReadFileWithPos(void *buffer, size_t size, size_t count, u32 pos, int handle){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! \n"); }
int result = USE_OS_FS_FUNCTION;
if(hasFileHandleInternal(handle)){
off_t newOffset = -1;
newOffset = lseek(handle, (off_t)pos, SEEK_SET);
if(newOffset == (off_t)pos){
result = readIntoBuffer(handle, buffer,size,count);
DEBUG_FUNCTION_LINE("Reading %08X bytes from handle %08X at pos %08X. result %08X \n",size*count,handle,pos,result);
}else{
return -1;
}
}
return result;
}
int fs_wrapper_FSSetPosFile(int handle,u32 pos){
if(FS_WRAPPER_DEBUG_LOG){ DEBUG_FUNCTION_LINE("Called! \n"); }
int result = USE_OS_FS_FUNCTION;
if(hasFileHandleInternal(handle)){
off_t newOffset = -1;
result = -1;
newOffset = lseek(handle, (off_t)pos, SEEK_SET);
if(newOffset == (off_t)pos){
result = FS_STATUS_OK;
DEBUG_FUNCTION_LINE("Set position to %08X for handle %08X\n",pos,handle);
}else{
DEBUG_FUNCTION_LINE("Failed set position to %08X for handle %08X\n",pos,handle);
return FS_STATUS_OK;
}
}
return result;
}

View File

@ -1,54 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef __FS_SYNC_WRAPPER_H_
#define __FS_SYNC_WRAPPER_H_
#include "FileReplacerUtils.h"
#include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/fs_functions.h>
#include <unistd.h>
#ifdef __cplusplus
extern "C" {
#endif
int fs_wrapper_FSCloseFile(int handle);
int fs_wrapper_FSGetPosFile(int handle,int * pos);
int fs_wrapper_FSGetStat(const char * path, FSStat * stats);
int fs_wrapper_FSGetStatFile(int handle, FSStat * stats);
int fs_wrapper_FSIsEof(int handle);
int fs_wrapper_FSOpenFile(const char * path, const char * mode, int * handle);
int fs_wrapper_FSReadFile(int handle,void *buffer,size_t size,size_t count);
int fs_wrapper_FSReadFileWithPos(void *buffer, size_t size, size_t count, u32 pos, int handle);
int fs_wrapper_FSSetPosFile(int handle,u32 pos);
#ifdef __cplusplus
}
#endif
#endif // __FS_SYNC_WRAPPER_H_

View File

@ -1,108 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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 <utils/logger.h>
#include "fs_wrapper_utils.h"
#include "fs_async_wrapper.h"
typedef int (*FallbackHelperInternal)(FallbackParamWrapper*, OSAsyncParamWrapper *);
static int fallbackCaller(FallbackHelperInternal _function, void * fallbackparams){
FallbackParamWrapper * paramsWrapper = (FallbackParamWrapper *)fallbackparams;
OSAsyncParamWrapper * params = &(paramsWrapper->params);
if(paramsWrapper->realFunctionAddress == NULL){ return FS_STATUS_FATAL_ERROR; } // OSFatal("paramsWrapper->realFunctionAddress was NULL;");}
return _function(paramsWrapper,params);
}
static int fallbackFSCloseFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSCloseFileAsyncCallback)(FSClient *, FSCmdBlock *, int, int, FSAsyncParams *);
real_FSCloseFileAsyncCallback = (int(*)(FSClient *, FSCmdBlock *, int, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSCloseFileAsyncCallback(params->pClient, params->pCmd, params->handle, params->error,params->asyncParams);
return res;
}
static int fallbackFSGetPosFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSGetPosFileAsyncFunc)(FSClient *, FSCmdBlock *, int, int *, int, FSAsyncParams *);
real_FSGetPosFileAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, int, int *, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSGetPosFileAsyncFunc(params->pClient, params->pCmd, params->handle, params->posPtr, params->error,params->asyncParams);
return res;
}
static int fallbackFSGetStatAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSGetStatAsyncFunc)(FSClient *, FSCmdBlock *, const char *, FSStat *, int, FSAsyncParams *);
real_FSGetStatAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, const char *, FSStat *, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSGetStatAsyncFunc(params->pClient, params->pCmd, params->path, params->stats, params->error,params->asyncParams);
return res;
}
static int fallbackFSGetStatFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSGetStatFileAsyncFunc)(FSClient *, FSCmdBlock *, int, FSStat *, int, FSAsyncParams *);
real_FSGetStatFileAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, int, FSStat *, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSGetStatFileAsyncFunc(params->pClient, params->pCmd, params->handle, params->stats, params->error,params->asyncParams);
return res;
}
static int fallbackFSIsEofAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSIsEofAsyncFunc)(FSClient *, FSCmdBlock *, int, int, FSAsyncParams *);
real_FSIsEofAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, int, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSIsEofAsyncFunc(params->pClient, params->pCmd, params->handle, params->error,params->asyncParams);
return res;
}
static int fallbackFSOpenFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSOpenFileAsyncCallback)(FSClient *, FSCmdBlock *, const char *, const char *, int *, int, FSAsyncParams *);
real_FSOpenFileAsyncCallback = (int(*)(FSClient *, FSCmdBlock *, const char *, const char *, int *, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSOpenFileAsyncCallback(params->pClient, params->pCmd, params->path, params->mode, params->handlePtr, params->error, params->asyncParams);
return res;
}
static int fallbackFSReadFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSReadFileAsyncFunc)(FSClient *, FSCmdBlock *, void *, int, int, int, int, int, FSAsyncParams *);
real_FSReadFileAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, void *, int, int, int, int, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSReadFileAsyncFunc(params->pClient, params->pCmd, params->buffer, params->size, params->count, params->handle, params->flag, params->error, params->asyncParams);
return res;
}
static int fallbackFSReadFileWithPosAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSReadFileWithPosAsyncFunc)(FSClient *, FSCmdBlock *, void *, int, int, u32, int, int, int, FSAsyncParams *);
real_FSReadFileWithPosAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, void *, int, int, u32, int, int, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSReadFileWithPosAsyncFunc(params->pClient, params->pCmd, params->buffer, params->size, params->count, params->pos, params->handle, params->flag, params->error, params->asyncParams);
return res;
}
static int fallbackFSSetPosFileAsyncInternal(FallbackParamWrapper * paramsWrapper, OSAsyncParamWrapper * params){
int (*real_FSSetPosFileAsyncFunc)(FSClient *, FSCmdBlock *, int, u32, int, FSAsyncParams *);
real_FSSetPosFileAsyncFunc = (int(*)(FSClient *, FSCmdBlock *, int, u32, int, FSAsyncParams *)) paramsWrapper->realFunctionAddress;
int res = real_FSSetPosFileAsyncFunc(params->pClient, params->pCmd, params->handle, params->pos, params->error, params->asyncParams);
return res;
}
#define DEFINE_FS_FALLBACK_CALLER(name) \
int fallback##name(void * fallbackparams){\
return fallbackCaller(fallback##name##Internal,fallbackparams);\
}
DEFINE_FS_FALLBACK_CALLER(FSCloseFileAsync)
DEFINE_FS_FALLBACK_CALLER(FSGetStatAsync)
DEFINE_FS_FALLBACK_CALLER(FSGetStatFileAsync)
DEFINE_FS_FALLBACK_CALLER(FSGetPosFileAsync)
DEFINE_FS_FALLBACK_CALLER(FSIsEofAsync)
DEFINE_FS_FALLBACK_CALLER(FSOpenFileAsync)
DEFINE_FS_FALLBACK_CALLER(FSReadFileAsync)
DEFINE_FS_FALLBACK_CALLER(FSReadFileWithPosAsync)
DEFINE_FS_FALLBACK_CALLER(FSSetPosFileAsync)

View File

@ -1,53 +0,0 @@
/****************************************************************************
* Copyright (C) 2017 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/>.
****************************************************************************/
#ifndef __FS_WRAPPER_UTILS_H_
#define __FS_WRAPPER_UTILS_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <dynamic_libs/fs_defs.h>
#include <dynamic_libs/fs_functions.h>
#include <unistd.h>
int fallbackFSCloseFileAsync(void * fallbackparams);
int fallbackFSGetPosFileAsync(void * fallbackparams);
int fallbackFSGetStatAsync(void * fallbackparams);
int fallbackFSGetStatFileAsync(void * fallbackparams);
int fallbackFSFSGetPosFileAsync(void * fallbackparams);
int fallbackFSIsEofAsync(void * fallbackparams);
int fallbackFSOpenFileAsync(void * fallbackparams);
int fallbackFSReadFileAsync(void * fallbackparams);
int fallbackFSReadFileWithPosAsync(void * fallbackparams);
int fallbackFSSetPosFileAsync(void * fallbackparams);
#ifdef __cplusplus
}
#endif
#endif // __FS_WRAPPER_UTILS_H_

View File

@ -8,22 +8,21 @@
#include <dynamic_libs/socket_functions.h> #include <dynamic_libs/socket_functions.h>
#include <fs/sd_fat_devoptab.h> #include <fs/sd_fat_devoptab.h>
#include <utils/logger.h> #include <utils/logger.h>
#include "utils/fs_utils.h"
#include "common/retain_vars.h" #include "common/retain_vars.h"
#include <iosuhax.h> #include <iosuhax.h>
#include "common/retain_vars.h" #include <fswrapper/fs_retain_vars.h>
#include "fs_wrapper/fs_retain_vars.h" #include <fswrapper/FileReplacerUtils.h>
#include "common/common.h" #include "common/common.h"
#include "main.h" #include "main.h"
#include "modpackSelector.h"
#define DEBUG_LOG 1 #define DEBUG_LOG 1
WUPS_MODULE_NAME("SDCaffiine"); WUPS_MODULE_NAME("SDCaffiine lite");
WUPS_MODULE_VERSION("v1.0"); WUPS_MODULE_VERSION("v1.0");
WUPS_MODULE_AUTHOR("Maschell"); WUPS_MODULE_AUTHOR("Maschell");
WUPS_MODULE_LICENSE("GPL"); WUPS_MODULE_LICENSE("GPL");
static bool setGroupAndOwnerID();
void Init_SD_USB(); void Init_SD_USB();
INITIALIZE(){ INITIALIZE(){
@ -41,39 +40,20 @@ INITIALIZE(){
log_init(); log_init();
gSDInitDone = 0; gSDInitDone = 0;
snprintf(gModFolder, FS_MAX_ENTNAME_SIZE, "sd:/sdcafiine/%016llX",OSGetTitleID());
DEBUG_FUNCTION_LINE("Folder: %s\n",gModFolder);
DEBUG_FUNCTION_LINE("Mount SD partition\n"); DEBUG_FUNCTION_LINE("Mount SD partition\n");
Init_SD_USB(); Init_SD_USB();
setGroupAndOwnerID(); if(FileReplacerUtils::setGroupAndOwnerID()){
DEBUG_FUNCTION_LINE("SUCCESS\n");
}
g_dirhandles = vc_vector_create(0, sizeof(int), NULL); HandleMultiModPacks(OSGetTitleID());
g_filehandles = vc_vector_create(0, sizeof(int), NULL);
log_print("Init of sd_cafiine!\n"); log_print("Init of sd_cafiine!\n");
} }
static bool setGroupAndOwnerID(){
int mcpHandle = MCP_Open();
if(mcpHandle != 0)
{
unsigned char titleInfo[0x80];
memset(titleInfo, 0, sizeof(titleInfo));
MCP_GetOwnTitleInfo(mcpHandle, titleInfo);
MCP_Close(mcpHandle);
u32 * test = (u32*)titleInfo;
global_owner_id = test[1];
global_group_id = test[2];
DEBUG_FUNCTION_LINE("Set group_id to %08X and owner_id to %08X\n",global_group_id,global_owner_id);
return true;
}
return false;
}
void Init_SD_USB() { void Init_SD_USB() {
//int res = IOSUHAX_Open(NULL); //int res = IOSUHAX_Open(NULL);
//if(res < 0){ //if(res < 0){
@ -109,6 +89,7 @@ void Init_SD_USB() {
DEBUG_FUNCTION_LINE("%08X\n",gSDInitDone); DEBUG_FUNCTION_LINE("%08X\n",gSDInitDone);
} }
void deInit_SD_USB(){ void deInit_SD_USB(){
DEBUG_FUNCTION_LINE("Called this function.\n"); DEBUG_FUNCTION_LINE("Called this function.\n");
@ -152,3 +133,10 @@ void deInit_SD_USB(){
} }
DEBUG_FUNCTION_LINE("Function end.\n"); DEBUG_FUNCTION_LINE("Function end.\n");
} }
void deInitApplication(){
//FileReplacerUtils::getInstance()->StopAsyncThread();
FileReplacerUtils::destroyInstance();
deInit_SD_USB();
}

View File

@ -9,7 +9,7 @@
extern "C" { extern "C" {
#endif #endif
void deInit_SD_USB(); void deInitApplication();
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -0,0 +1,210 @@
#include <string>
#include <map>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <malloc.h>
#include "modpackSelector.h"
#include "common/common.h"
#include <dynamic_libs/os_functions.h>
#include <dynamic_libs/vpad_functions.h>
#include <utils/logger.h>
#include <utils/StringTools.h>
#include <fs/FSUtils.h>
#include <fs/CFile.hpp>
#include <fs/DirList.h>
#include "common/retain_vars.h"
#define TEXT_SEL(x, text1, text2) ((x) ? (text1) : (text2))
void HandleMultiModPacks(u64 titleID/*,bool showMenu*/) {
gModFolder[0] = 0;
char TitleIDString[FS_MAX_FULLPATH_SIZE];
snprintf(TitleIDString,FS_MAX_FULLPATH_SIZE,"%016llX",titleID);
std::map<std::string,std::string> modTitlePath;
std::map<std::string,std::string> mounting_points;
if(gSDInitDone & SDUSB_MOUNTED_OS_SD){ mounting_points[std::string(SD_PATH)] = std::string(NAME_PREFIX_SD); }
if(gSDInitDone & SD_MOUNTED_LIBFAT){ mounting_points[std::string(SD_PATH)] = std::string(NAME_PREFIX_SD); }
if(gSDInitDone & USB_MOUNTED_LIBFAT){ mounting_points[std::string(USB_PATH)] = std::string(NAME_PREFIX_USB); }
int i = 0;
for (i = 0; i < ntfs_mount_count; i++){
//mounting_points[std::string(((ntfs_md *)ntfs_mounts)[i].name) + ":"] = StringTools::strfmt("%s:(%s)",((ntfs_md *)ntfs_mounts)[i].name, ntfsGetVolumeName(((ntfs_md *)ntfs_mounts)[i].name));
}
for (std::map<std::string,std::string>::iterator it=mounting_points.begin(); it!=mounting_points.end(); ++it){
std::string curMount = it->first;
std::string curMountName = it->second;
//DEBUG_FUNCTION_LINE("%s %s \n",curMount.c_str(),curMountName.c_str());
std::string modTitleIDPath = curMount + GAME_MOD_FOLDER + "/" + TitleIDString;
//DEBUG_FUNCTION_LINE("modTitleIDPath %s \n",modTitleIDPath.c_str());
DirList modTitleDirList(modTitleIDPath.c_str(), NULL, DirList::Dirs);
modTitleDirList.SortList();
for(int i = 0; i < modTitleDirList.GetFilecount(); i++) {
std::string curFile = modTitleDirList.GetFilename(i);
//DEBUG_FUNCTION_LINE("curFile %s \n",curFile.c_str());
if(curFile.compare(".") == 0 || curFile.compare("..") == 0)
continue;
if(curFile.compare(CONTENT_FOLDER) == 0 || curFile.compare(AOC_FOLDER) == 0/* || curFile.compare(META_FOLDER) == 0*/) {
std::string packageName = curMountName + " " + DEFAULT_NAME_PACKAGE;
modTitlePath[packageName] = modTitleIDPath;
DEBUG_FUNCTION_LINE("found %s \n",packageName.c_str());
}else{
std::string packageName = curMountName + " " + curFile;
modTitlePath[packageName] = modTitleIDPath + "/" + curFile;
DEBUG_FUNCTION_LINE("found %s \n",packageName.c_str());
}
}
}
//DEBUG_FUNCTION_LINE("Iteration done\n");
int modPackListSize =modTitlePath.size();
if(modPackListSize == 0) return;
if(modPackListSize == 1/* || !showMenu*/){
for (std::map<std::string,std::string>::iterator it=modTitlePath.begin(); it!=modTitlePath.end(); ++it){
snprintf(gModFolder, FS_MAX_ENTNAME_SIZE, "%s", it->second.c_str());
break;
}
return;
}
int selected = 0;
int initScreen = 1;
int x_offset = -2;
VPADData vpad;
s32 vpadError;
OSScreenInit();
u32 screen_buf0_size = OSScreenGetBufferSizeEx(0);
u32 screen_buf1_size = OSScreenGetBufferSizeEx(1);
u32 * screenbuffers = (u32*)memalign(0x100, screen_buf0_size + screen_buf1_size);
OSScreenSetBufferEx(0, (void *)screenbuffers);
OSScreenSetBufferEx(1, (void *)(screenbuffers + screen_buf0_size));
OSScreenEnableEx(0, 1);
OSScreenEnableEx(1, 1);
// Clear screens
OSScreenClearBufferEx(0, 0);
OSScreenClearBufferEx(1, 0);
// Flip buffers
OSScreenFlipBuffersEx(0);
OSScreenFlipBuffersEx(1);
int wantToExit = 0;
int page = 0;
int per_page = 13;
int max_pages = (modPackListSize / per_page) + 1;
while(1){
vpadError = -1;
VPADRead(0, &vpad, 1, &vpadError);
if(vpadError == 0) {
if(vpad.btns_d & VPAD_BUTTON_A) {
wantToExit = 1;
initScreen = 1;
} else if(vpad.btns_d & VPAD_BUTTON_DOWN) {
selected++;
initScreen = 1;
} else if(vpad.btns_d & VPAD_BUTTON_UP) {
selected--;
initScreen = 1;
} else if(vpad.btns_d & VPAD_BUTTON_L) {
selected -= per_page;
initScreen = 1;
} else if(vpad.btns_d & VPAD_BUTTON_R) {
selected += per_page;
initScreen = 1;
}
if(selected < 0) selected = 0;
if(selected >= modPackListSize) selected = modPackListSize-1;
page = selected / per_page;
}
if(initScreen) {
OSScreenClearBufferEx(0, 0);
OSScreenClearBufferEx(1, 0);
console_print_pos(x_offset, -1, " -- SDCafiine lite by Maschell --");
console_print_pos(x_offset, 1, "Select your options and press A to launch.");
int y_offset = 3;
int cur_ = 0;
for (std::map<std::string,std::string>::iterator it=modTitlePath.begin(); it!=modTitlePath.end(); ++it){
std::string key = it->first;
std::string value = it->second;
if(wantToExit && cur_ == selected){
snprintf(gModFolder, FS_MAX_ENTNAME_SIZE, "%s", value.c_str());
break;
}
if(cur_ >= (page*per_page) && cur_ < ((page+1)*per_page)){
console_print_pos(x_offset, y_offset++, "%s %s",TEXT_SEL((selected == cur_), "--->", " "), key.c_str());
}
cur_++;
}
if(wantToExit){ //just in case.
break;
}
if(max_pages > 0) {
console_print_pos(x_offset, 17, "Page %02d/%02d. Press L/R to change page.", page + 1,max_pages);
}
// Flip buffers
OSScreenFlipBuffersEx(0);
OSScreenFlipBuffersEx(1);
initScreen = 0;
}
os_usleep(20000);
}
OSScreenClearBufferEx(0, 0);
OSScreenClearBufferEx(1, 0);
// Flip buffers
OSScreenFlipBuffersEx(0);
OSScreenFlipBuffersEx(1);
free(screenbuffers);
return;
}
void console_print_pos(int x, int y, const char *format, ...){
char * tmp = NULL;
va_list va;
va_start(va, format);
if((vasprintf(&tmp, format, va) >= 0) && tmp)
{
if(strlen(tmp) > 79)
tmp[79] = 0;
OSScreenPutFontEx(0, x, y, tmp);
OSScreenPutFontEx(1, x, y, tmp);
}
va_end(va);
if(tmp)
free(tmp);
}

View File

@ -0,0 +1,16 @@
#ifndef _MODPACK_SELECTOR_H_
#define _MODPACK_SELECTOR_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <dynamic_libs/os_types.h>
void HandleMultiModPacks(u64 titleid/*,bool showMenu = true*/);
void console_print_pos(int x, int y, const char *format, ...);
#ifdef __cplusplus
}
#endif
#endif //_MODPACK_SELECTOR_H_

View File

@ -5,10 +5,10 @@
#include <dynamic_libs/sys_functions.h> #include <dynamic_libs/sys_functions.h>
#include <dynamic_libs/proc_ui_functions.h> #include <dynamic_libs/proc_ui_functions.h>
#include <dynamic_libs/socket_functions.h> #include <dynamic_libs/socket_functions.h>
#include "fs_wrapper/FileReplacerUtils.h" #include <fswrapper/FileReplacerUtils.h>
#include "fs_wrapper/fs_default_os_wrapper.h" #include <fswrapper/fs_default_os_wrapper.h>
#include <myfs/fs_utils.h>
#include <utils/logger.h> #include <utils/logger.h>
#include "utils/fs_utils.h"
#include "common/retain_vars.h" #include "common/retain_vars.h"
#include "common/common.h" #include "common/common.h"
#include "main.h" #include "main.h"
@ -18,7 +18,7 @@
DECL_FUNCTION(void, __PPCExit, void){ DECL_FUNCTION(void, __PPCExit, void){
DEBUG_FUNCTION_LINE("__PPCExit\n"); DEBUG_FUNCTION_LINE("__PPCExit\n");
deInit_SD_USB(); deInitApplication();
real___PPCExit(); real___PPCExit();
} }

View File

@ -1,329 +0,0 @@
#include "vc_vector.h"
#include <stdlib.h>
#include <string.h>
#define GROWTH_FACTOR 1.5
#define DEFAULT_COUNT_OF_ELEMENETS 8
#define MINIMUM_COUNT_OF_ELEMENTS 2
// ----------------------------------------------------------------------------
// vc_vector structure
struct vc_vector {
size_t count;
size_t element_size;
size_t reserved_size;
char* data;
vc_vector_deleter* deleter;
};
// ----------------------------------------------------------------------------
// auxillary methods
bool vc_vector_realloc(vc_vector* vector, size_t new_count) {
const size_t new_size = new_count * vector->element_size;
char* new_data = (char*)realloc(vector->data, new_size);
if (!new_data) {
return false;
}
vector->reserved_size = new_size;
vector->data = new_data;
return true;
}
// [first_index, last_index)
void vc_vector_call_deleter(vc_vector* vector, size_t first_index, size_t last_index) {
for (size_t i = first_index; i < last_index; ++i) {
vector->deleter(vc_vector_at(vector, i));
}
}
void vc_vector_call_deleter_all(vc_vector* vector) {
vc_vector_call_deleter(vector, 0, vc_vector_count(vector));
}
// ----------------------------------------------------------------------------
// Contol
vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter) {
vc_vector* v = (vc_vector*)malloc(sizeof(vc_vector));
if (v != NULL) {
v->data = NULL;
v->count = 0;
v->element_size = size_of_element;
v->deleter = deleter;
if (count_elements < MINIMUM_COUNT_OF_ELEMENTS) {
count_elements = DEFAULT_COUNT_OF_ELEMENETS;
}
if (size_of_element < 1 ||
!vc_vector_realloc(v, count_elements)) {
free(v);
v = NULL;
}
}
return v;
}
vc_vector* vc_vector_create_copy(const vc_vector* vector) {
vc_vector* new_vector = vc_vector_create(vector->reserved_size / vector->count,
vector->element_size,
vector->deleter);
if (!new_vector) {
return new_vector;
}
if (memcpy(vector->data,
new_vector->data,
new_vector->element_size * vector->count) == NULL) {
vc_vector_release(new_vector);
new_vector = NULL;
return new_vector;
}
new_vector->count = vector->count;
return new_vector;
}
void vc_vector_release(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
if (vector->reserved_size != 0) {
free(vector->data);
}
free(vector);
}
bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2) {
const size_t size_vector1 = vc_vector_size(vector1);
if (size_vector1 != vc_vector_size(vector2)) {
return false;
}
return memcmp(vector1->data, vector2->data, size_vector1) == 0;
}
float vc_vector_get_growth_factor() {
return GROWTH_FACTOR;
}
size_t vc_vector_get_default_count_of_elements() {
return DEFAULT_COUNT_OF_ELEMENETS;
}
size_t vc_vector_struct_size() {
return sizeof(vc_vector);
}
// ----------------------------------------------------------------------------
// Element access
void* vc_vector_at(vc_vector* vector, size_t index) {
return vector->data + index * vector->element_size;
}
void* vc_vector_front(vc_vector* vector) {
return vector->data;
}
void* vc_vector_back(vc_vector* vector) {
return vector->data + (vector->count - 1) * vector->element_size;
}
void* vc_vector_data(vc_vector* vector) {
return vector->data;
}
// ----------------------------------------------------------------------------
// Iterators
void* vc_vector_begin(vc_vector* vector) {
return vector->data;
}
void* vc_vector_end(vc_vector* vector) {
return vector->data + vector->element_size * vector->count;
}
void* vc_vector_next(vc_vector* vector, void* i) {
return i + vector->element_size;
}
// ----------------------------------------------------------------------------
// Capacity
bool vc_vector_empty(vc_vector* vector) {
return vector->count == 0;
}
size_t vc_vector_count(const vc_vector* vector) {
return vector->count;
}
size_t vc_vector_size(const vc_vector* vector) {
return vector->count * vector->element_size;
}
size_t vc_vector_max_count(const vc_vector* vector) {
return vector->reserved_size / vector->element_size;
}
size_t vc_vector_max_size(const vc_vector* vector) {
return vector->reserved_size;
}
bool vc_vector_reserve_count(vc_vector* vector, size_t new_count) {
if (new_count < vector->count) {
return false;
}
size_t new_size = vector->element_size * new_count;
if (new_size == vector->reserved_size) {
return true;
}
return vc_vector_realloc(vector, new_count);
}
bool vc_vector_reserve_size(vc_vector* vector, size_t new_size) {
return vc_vector_reserve_count(vector, new_size / vector->element_size);
}
// ----------------------------------------------------------------------------
// Modifiers
void vc_vector_clear(vc_vector* vector) {
if (vector->deleter != NULL) {
vc_vector_call_deleter_all(vector);
}
vector->count = 0;
}
bool vc_vector_insert(vc_vector* vector, size_t index, const void* value) {
if (vc_vector_max_count(vector) < vector->count + 1) {
if (!vc_vector_realloc(vector, vc_vector_max_count(vector) * GROWTH_FACTOR)) {
return false;
}
}
if (!memmove(vc_vector_at(vector, index + 1),
vc_vector_at(vector, index),
vector->element_size * (vector->count - index))) {
return false;
}
if (memcpy(vc_vector_at(vector, index),
value,
vector->element_size) == NULL) {
return false;
}
++vector->count;
return true;
}
bool vc_vector_erase(vc_vector* vector, size_t index) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
if (!memmove(vc_vector_at(vector, index),
vc_vector_at(vector, index + 1),
vector->element_size * (vector->count - index))) {
return false;
}
vector->count--;
return true;
}
bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, first_index, last_index);
}
if (!memmove(vc_vector_at(vector, first_index),
vc_vector_at(vector, last_index),
vector->element_size * (vector->count - last_index))) {
return false;
}
vector->count -= last_index - first_index;
return true;
}
bool vc_vector_append(vc_vector* vector, const void* values, size_t count) {
const size_t count_new = count + vc_vector_count(vector);
if (vc_vector_max_count(vector) < count_new) {
size_t max_count_to_reserved = vc_vector_max_count(vector) * GROWTH_FACTOR;
while (count_new > max_count_to_reserved) {
max_count_to_reserved *= GROWTH_FACTOR;
}
if (!vc_vector_realloc(vector, max_count_to_reserved)) {
return false;
}
}
if (memcpy(vector->data + vector->count * vector->element_size,
values,
vector->element_size * count) == NULL) {
return false;
}
vector->count = count_new;
return true;
}
bool vc_vector_push_back(vc_vector* vector, const void* value) {
if (!vc_vector_append(vector, value, 1)) {
return false;
}
return true;
}
bool vc_vector_pop_back(vc_vector* vector) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_back(vector));
}
vector->count--;
return true;
}
bool vc_vector_replace(vc_vector* vector, size_t index, const void* value) {
if (vector->deleter != NULL) {
vector->deleter(vc_vector_at(vector, index));
}
return memcpy(vc_vector_at(vector, index),
value,
vector->element_size) != NULL;
}
bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count) {
if (vector->deleter != NULL) {
vc_vector_call_deleter(vector, index, index + count);
}
return memcpy(vc_vector_at(vector, index),
values,
vector->element_size * count) != NULL;
}

View File

@ -1,130 +0,0 @@
#ifndef VCVECTOR_H
#define VCVECTOR_H
#include <stdbool.h>
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct vc_vector vc_vector;
typedef void (vc_vector_deleter)(void *);
// ----------------------------------------------------------------------------
// Control
// ----------------------------------------------------------------------------
// Constructs an empty vector with an reserver size for count_elements.
vc_vector* vc_vector_create(size_t count_elements, size_t size_of_element, vc_vector_deleter* deleter);
// Constructs a copy of an existing vector.
vc_vector* vc_vector_create_copy(const vc_vector* vector);
// Releases the vector.
void vc_vector_release(vc_vector* vector);
// Compares vector content
bool vc_vector_is_equals(vc_vector* vector1, vc_vector* vector2);
// Returns constant value of the vector growth factor.
float vc_vector_get_growth_factor();
// Returns constant value of the vector default count of elements.
size_t vc_vector_get_default_count_of_elements();
// Returns constant value of the vector struct size.
size_t vc_vector_struct_size();
// ----------------------------------------------------------------------------
// Element access
// ----------------------------------------------------------------------------
// Returns the item at index position in the vector.
void* vc_vector_at(vc_vector* vector, size_t index);
// Returns the first item in the vector.
void* vc_vector_front(vc_vector* vector);
// Returns the last item in the vector.
void* vc_vector_back(vc_vector* vector);
// Returns a pointer to the data stored in the vector. The pointer can be used to access and modify the items in the vector.
void* vc_vector_data(vc_vector* vector);
// ----------------------------------------------------------------------------
// Iterators
// ----------------------------------------------------------------------------
// Returns a pointer to the first item in the vector.
void* vc_vector_begin(vc_vector* vector);
// Returns a pointer to the imaginary item after the last item in the vector.
void* vc_vector_end(vc_vector* vector);
// Returns a pointer to the next element of vector after 'i'.
void* vc_vector_next(vc_vector* vector, void* i);
// ----------------------------------------------------------------------------
// Capacity
// ----------------------------------------------------------------------------
// Returns true if the vector is empty; otherwise returns false.
bool vc_vector_empty(vc_vector* vector);
// Returns the number of elements in the vector.
size_t vc_vector_count(const vc_vector* vector);
// Returns the size (in bytes) of occurrences of value in the vector.
size_t vc_vector_size(const vc_vector* vector);
// Returns the maximum number of elements that the vector can hold.
size_t vc_vector_max_count(const vc_vector* vector);
// Returns the maximum size (in bytes) that the vector can hold.
size_t vc_vector_max_size(const vc_vector* vector);
// Resizes the container so that it contains n elements.
bool vc_vector_reserve_count(vc_vector* vector, size_t new_count);
// Resizes the container so that it contains new_size / element_size elements.
bool vc_vector_reserve_size(vc_vector* vector, size_t new_size);
// ----------------------------------------------------------------------------
// Modifiers
// ----------------------------------------------------------------------------
// Removes all elements from the vector (without reallocation).
void vc_vector_clear(vc_vector* vector);
// The container is extended by inserting a new element at position.
bool vc_vector_insert(vc_vector* vector, size_t index, const void* value);
// Removes from the vector a single element by 'index'
bool vc_vector_erase(vc_vector* vector, size_t index);
// Removes from the vector a range of elements '[first_index, last_index)'.
bool vc_vector_erase_range(vc_vector* vector, size_t first_index, size_t last_index);
// Inserts multiple values at the end of the vector.
bool vc_vector_append(vc_vector* vector, const void* values, size_t count);
// Inserts value at the end of the vector.
bool vc_vector_push_back(vc_vector* vector, const void* value);
// Removes the last item in the vector.
bool vc_vector_pop_back(vc_vector* vector);
// Replace value by index in the vector.
bool vc_vector_replace(vc_vector* vector, size_t index, const void* value);
// Replace multiple values by index in the vector.
bool vc_vector_replace_multiple(vc_vector* vector, size_t index, const void* values, size_t count);
#ifdef __cplusplus
}
#endif
#endif // VCVECTOR_H