Skip to content

Commit

Permalink
[BOX32][WRAPPER] Added 32bits wrapping for SDL2_image
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Sep 30, 2024
1 parent c88cf63 commit 2b0c8d4
Show file tree
Hide file tree
Showing 14 changed files with 316 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ if(BOX32)
"${BOX64_ROOT}/src/wrapped32/wrappedlibglu.c"
"${BOX64_ROOT}/src/wrapped32/wrappedsdl1.c"
"${BOX64_ROOT}/src/wrapped32/wrappedsdl2.c"
"${BOX64_ROOT}/src/wrapped32/wrappedsdl2image.c"
"${BOX64_ROOT}/src/wrapped32/wrappedlibasound.c"
"${BOX64_ROOT}/src/wrapped32/wrappedopenal.c"
"${BOX64_ROOT}/src/wrapped32/wrappedfreetype.c"
Expand Down
4 changes: 2 additions & 2 deletions src/include/sdl2align32.h
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,14 @@ typedef struct my_SDL2_RWops_s {
void* hidden[3];
} my_SDL2_RWops_t;

typedef struct __attribute__((packed)) my_SDL2_RWops_32_s {
typedef struct my_SDL2_RWops_32_s {
ptr_t size;
ptr_t seek;
ptr_t read;
ptr_t write;
ptr_t close;
uint32_t type;
void* hidden[3]; // not converting hidden, just moving it
ptr_t hidden[3]; // not converting hidden, just moving it
} my_SDL2_RWops_32_t;

void inplace_SDL2_DisplayMode_to_64(void* a);
Expand Down
6 changes: 6 additions & 0 deletions src/include/sdl2rwops.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define __SDL2RWOPS_H__

typedef struct SDL2_RWops_s SDL2_RWops_t; // the actual SDL2 SDL_RWops
#ifdef BOX32
typedef struct my_SDL2_RWops_32_s my_SDL2_RWops_32_t;
#endif
typedef struct x64emu_s x64emu_t;

typedef SDL2_RWops_t* (*sdl2_allocrw)(void);
Expand All @@ -23,6 +26,9 @@ SDL2_RWops_t* AddNativeRW2(x64emu_t* emu, SDL2_RWops_t* ops);
SDL2_RWops_t* RWNativeStart2(x64emu_t* emu, SDL2_RWops_t* ops); // put native RW functions, wrapping the emulated (callback style) ones if needed
void RWNativeEnd2(SDL2_RWops_t* ops); // put emulated function back in place
int isRWops(SDL2_RWops_t* ops); // 1 if ops seems to be a valid RWops, 0 if not
#ifdef BOX32
int isRWops32(my_SDL2_RWops_32_t* ops);
#endif

int64_t RWNativeSeek2(SDL2_RWops_t *ops, int64_t offset, int32_t whence);
int64_t RWNativeSize2(SDL2_RWops_t *ops);
Expand Down
2 changes: 2 additions & 0 deletions src/library_list_32.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ GO("libGL.so.1", libgl)
GO("libGLU.so.1", libglu)
GO("libSDL-1.2.so.0", sdl1)
GO("libSDL2-2.0.so.0", sdl2)
GO("libSDL2_image-2.0.so.0", sdl2image)
GO("libSDL2_image.so", sdl2image)
GO("libasound.so.2", libasound)
GO("libasound.so", libasound)
GO("libfreetype.so.6", freetype)
Expand Down
20 changes: 20 additions & 0 deletions src/libtools/sdl2rwops.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ int isRWops(SDL2_RWops_t* ops)
return 0;
return 1;
}
#ifdef BOX32
int isRWops32(my_SDL2_RWops_32_t* ops)
{
if(!ops)
return 0;
#define GO(A, W) \
if(!ops->A || (uintptr_t)ops->A < 0x1000) return 0;

SUPER()

#undef GO
// check if all then hidden content is just full of 0
if(ops->hidden[0]==0 && ops->hidden[1]==0 && ops->hidden[2]==0)
return 0;
// check the type (not sure it's a good check here)
if (ops->type>5 && ops->type!=BOX64RW)
return 0;
return 1;
}
#endif

int64_t RWNativeSeek2(SDL2_RWops_t *ops, int64_t offset, int32_t whence)
{
Expand Down
27 changes: 27 additions & 0 deletions src/wrapped32/generated/functions_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@
#() LFXCii -> LFXCii
#() LFXLpi -> LFXLpi
#() pFEupp -> pFEupp
#() pFEpip -> pFEpip
#() pFEppi -> pFEppi
#() pFEppp -> pFEppp
#() pFEXpL -> pFEXpL
Expand Down Expand Up @@ -770,6 +771,7 @@
#() LFpLppa -> LFpLppa
#() LFXLuuu -> LFXLuuu
#() LFXLpuu -> LFXLpuu
#() pFEppip -> pFEppip
#() pFEXipp -> pFEXipp
#() pFuiupp -> pFuiupp
#() pFpiiuu -> pFpiiuu
Expand Down Expand Up @@ -1756,6 +1758,31 @@ wrappedsdl2:
- pFuiiiuuuu:
- SDL_CreateRGBSurface
% JFEi -> pFEpi
wrappedsdl2image:
- pFp:
- IMG_LoadBMP_RW
- IMG_LoadCUR_RW
- IMG_LoadGIF_RW
- IMG_LoadICO_RW
- IMG_LoadJPG_RW
- IMG_LoadLBM_RW
- IMG_LoadPCX_RW
- IMG_LoadPNG_RW
- IMG_LoadPNM_RW
- IMG_LoadTGA_RW
- IMG_LoadTIF_RW
- IMG_LoadWEBP_RW
- IMG_LoadXCF_RW
- IMG_LoadXPM_RW
- IMG_LoadXV_RW
- pFpi:
- IMG_Load_RW
- pFpip:
- IMG_LoadTyped_RW
- pFppi:
- IMG_LoadTexture_RW
- pFppip:
- IMG_LoadTextureTyped_RW
wrappedtcmallocminimal:
- pFp:
- iFpL:
Expand Down
8 changes: 8 additions & 0 deletions src/wrapped32/generated/wrappedsdl2imagedefs32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*********************************************************************
* File automatically generated by rebuild_wrappers_32.py (v0.0.2.2) *
*********************************************************************/
#ifndef __wrappedsdl2imageDEFS32_H_
#define __wrappedsdl2imageDEFS32_H_


#endif // __wrappedsdl2imageDEFS32_H_
41 changes: 41 additions & 0 deletions src/wrapped32/generated/wrappedsdl2imagetypes32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*********************************************************************
* File automatically generated by rebuild_wrappers_32.py (v0.0.2.2) *
*********************************************************************/
#ifndef __wrappedsdl2imageTYPES32_H_
#define __wrappedsdl2imageTYPES32_H_

#ifndef LIBNAME
#error You should only #include this file inside a wrapped*.c file
#endif
#ifndef ADDED_FUNCTIONS
#define ADDED_FUNCTIONS()
#endif

typedef void* (*pFp_t)(void*);
typedef void* (*pFpi_t)(void*, int32_t);
typedef void* (*pFpip_t)(void*, int32_t, void*);
typedef void* (*pFppi_t)(void*, void*, int32_t);
typedef void* (*pFppip_t)(void*, void*, int32_t, void*);

#define SUPER() ADDED_FUNCTIONS() \
GO(IMG_LoadBMP_RW, pFp_t) \
GO(IMG_LoadCUR_RW, pFp_t) \
GO(IMG_LoadGIF_RW, pFp_t) \
GO(IMG_LoadICO_RW, pFp_t) \
GO(IMG_LoadJPG_RW, pFp_t) \
GO(IMG_LoadLBM_RW, pFp_t) \
GO(IMG_LoadPCX_RW, pFp_t) \
GO(IMG_LoadPNG_RW, pFp_t) \
GO(IMG_LoadPNM_RW, pFp_t) \
GO(IMG_LoadTGA_RW, pFp_t) \
GO(IMG_LoadTIF_RW, pFp_t) \
GO(IMG_LoadWEBP_RW, pFp_t) \
GO(IMG_LoadXCF_RW, pFp_t) \
GO(IMG_LoadXPM_RW, pFp_t) \
GO(IMG_LoadXV_RW, pFp_t) \
GO(IMG_Load_RW, pFpi_t) \
GO(IMG_LoadTyped_RW, pFpip_t) \
GO(IMG_LoadTexture_RW, pFppi_t) \
GO(IMG_LoadTextureTyped_RW, pFppip_t)

#endif // __wrappedsdl2imageTYPES32_H_
8 changes: 8 additions & 0 deletions src/wrapped32/generated/wrappedsdl2imageundefs32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*********************************************************************
* File automatically generated by rebuild_wrappers_32.py (v0.0.2.2) *
*********************************************************************/
#ifndef __wrappedsdl2imageUNDEFS32_H_
#define __wrappedsdl2imageUNDEFS32_H_


#endif // __wrappedsdl2imageUNDEFS32_H_
4 changes: 4 additions & 0 deletions src/wrapped32/generated/wrapper32.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ typedef uintptr_t (*LFppLa_t)(void*, void*, uintptr_t, void*);
typedef uintptr_t (*LFXCii_t)(void*, uint8_t, int32_t, int32_t);
typedef uintptr_t (*LFXLpi_t)(void*, uintptr_t, void*, int32_t);
typedef void* (*pFEupp_t)(x64emu_t*, uint32_t, void*, void*);
typedef void* (*pFEpip_t)(x64emu_t*, void*, int32_t, void*);
typedef void* (*pFEppi_t)(x64emu_t*, void*, void*, int32_t);
typedef void* (*pFEppp_t)(x64emu_t*, void*, void*, void*);
typedef void* (*pFEXpL_t)(x64emu_t*, void*, void*, uintptr_t);
Expand Down Expand Up @@ -860,6 +861,7 @@ typedef uintptr_t (*LFEppLL_t)(x64emu_t*, void*, void*, uintptr_t, uintptr_t);
typedef uintptr_t (*LFpLppa_t)(void*, uintptr_t, void*, void*, void*);
typedef uintptr_t (*LFXLuuu_t)(void*, uintptr_t, uint32_t, uint32_t, uint32_t);
typedef uintptr_t (*LFXLpuu_t)(void*, uintptr_t, void*, uint32_t, uint32_t);
typedef void* (*pFEppip_t)(x64emu_t*, void*, void*, int32_t, void*);
typedef void* (*pFEXipp_t)(x64emu_t*, void*, int32_t, void*, void*);
typedef void* (*pFuiupp_t)(uint32_t, int32_t, uint32_t, void*, void*);
typedef void* (*pFpiiuu_t)(void*, int32_t, int32_t, uint32_t, uint32_t);
Expand Down Expand Up @@ -1800,6 +1802,7 @@ void LFppLa_32(x64emu_t *emu, uintptr_t fcn) { LFppLa_t fn = (LFppLa_t)fcn; R_EA
void LFXCii_32(x64emu_t *emu, uintptr_t fcn) { LFXCii_t fn = (LFXCii_t)fcn; R_EAX = to_ulong(fn(getDisplay(from_ptriv(R_ESP + 4)), from_ptri(uint8_t, R_ESP + 8), from_ptri(int32_t, R_ESP + 12), from_ptri(int32_t, R_ESP + 16))); }
void LFXLpi_32(x64emu_t *emu, uintptr_t fcn) { LFXLpi_t fn = (LFXLpi_t)fcn; R_EAX = to_ulong(fn(getDisplay(from_ptriv(R_ESP + 4)), to_ulong(from_ptri(ulong_t, R_ESP + 8)), from_ptriv(R_ESP + 12), from_ptri(int32_t, R_ESP + 16))); }
void pFEupp_32(x64emu_t *emu, uintptr_t fcn) { pFEupp_t fn = (pFEupp_t)fcn; R_EAX = to_ptrv(fn(emu, from_ptri(uint32_t, R_ESP + 4), from_ptriv(R_ESP + 8), from_ptriv(R_ESP + 12))); }
void pFEpip_32(x64emu_t *emu, uintptr_t fcn) { pFEpip_t fn = (pFEpip_t)fcn; R_EAX = to_ptrv(fn(emu, from_ptriv(R_ESP + 4), from_ptri(int32_t, R_ESP + 8), from_ptriv(R_ESP + 12))); }
void pFEppi_32(x64emu_t *emu, uintptr_t fcn) { pFEppi_t fn = (pFEppi_t)fcn; R_EAX = to_ptrv(fn(emu, from_ptriv(R_ESP + 4), from_ptriv(R_ESP + 8), from_ptri(int32_t, R_ESP + 12))); }
void pFEppp_32(x64emu_t *emu, uintptr_t fcn) { pFEppp_t fn = (pFEppp_t)fcn; R_EAX = to_ptrv(fn(emu, from_ptriv(R_ESP + 4), from_ptriv(R_ESP + 8), from_ptriv(R_ESP + 12))); }
void pFEXpL_32(x64emu_t *emu, uintptr_t fcn) { pFEXpL_t fn = (pFEXpL_t)fcn; R_EAX = to_ptrv(fn(emu, getDisplay(from_ptriv(R_ESP + 4)), from_ptriv(R_ESP + 8), to_ulong(from_ptri(ulong_t, R_ESP + 12)))); }
Expand Down Expand Up @@ -1960,6 +1963,7 @@ void LFEppLL_32(x64emu_t *emu, uintptr_t fcn) { LFEppLL_t fn = (LFEppLL_t)fcn; R
void LFpLppa_32(x64emu_t *emu, uintptr_t fcn) { LFpLppa_t fn = (LFpLppa_t)fcn; R_EAX = to_ulong(fn(from_ptriv(R_ESP + 4), to_ulong(from_ptri(ulong_t, R_ESP + 8)), from_ptriv(R_ESP + 12), from_ptriv(R_ESP + 16), from_locale(from_ptri(ptr_t, R_ESP + 20)))); }
void LFXLuuu_32(x64emu_t *emu, uintptr_t fcn) { LFXLuuu_t fn = (LFXLuuu_t)fcn; R_EAX = to_ulong(fn(getDisplay(from_ptriv(R_ESP + 4)), to_ulong(from_ptri(ulong_t, R_ESP + 8)), from_ptri(uint32_t, R_ESP + 12), from_ptri(uint32_t, R_ESP + 16), from_ptri(uint32_t, R_ESP + 20))); }
void LFXLpuu_32(x64emu_t *emu, uintptr_t fcn) { LFXLpuu_t fn = (LFXLpuu_t)fcn; R_EAX = to_ulong(fn(getDisplay(from_ptriv(R_ESP + 4)), to_ulong(from_ptri(ulong_t, R_ESP + 8)), from_ptriv(R_ESP + 12), from_ptri(uint32_t, R_ESP + 16), from_ptri(uint32_t, R_ESP + 20))); }
void pFEppip_32(x64emu_t *emu, uintptr_t fcn) { pFEppip_t fn = (pFEppip_t)fcn; R_EAX = to_ptrv(fn(emu, from_ptriv(R_ESP + 4), from_ptriv(R_ESP + 8), from_ptri(int32_t, R_ESP + 12), from_ptriv(R_ESP + 16))); }
void pFEXipp_32(x64emu_t *emu, uintptr_t fcn) { pFEXipp_t fn = (pFEXipp_t)fcn; R_EAX = to_ptrv(fn(emu, getDisplay(from_ptriv(R_ESP + 4)), from_ptri(int32_t, R_ESP + 8), from_ptriv(R_ESP + 12), from_ptriv(R_ESP + 16))); }
void pFuiupp_32(x64emu_t *emu, uintptr_t fcn) { pFuiupp_t fn = (pFuiupp_t)fcn; R_EAX = to_ptrv(fn(from_ptri(uint32_t, R_ESP + 4), from_ptri(int32_t, R_ESP + 8), from_ptri(uint32_t, R_ESP + 12), from_ptriv(R_ESP + 16), from_ptriv(R_ESP + 20))); }
void pFpiiuu_32(x64emu_t *emu, uintptr_t fcn) { pFpiiuu_t fn = (pFpiiuu_t)fcn; R_EAX = to_ptrv(fn(from_ptriv(R_ESP + 4), from_ptri(int32_t, R_ESP + 8), from_ptri(int32_t, R_ESP + 12), from_ptri(uint32_t, R_ESP + 16), from_ptri(uint32_t, R_ESP + 20))); }
Expand Down
2 changes: 2 additions & 0 deletions src/wrapped32/generated/wrapper32.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ void LFppLa_32(x64emu_t *emu, uintptr_t fnc);
void LFXCii_32(x64emu_t *emu, uintptr_t fnc);
void LFXLpi_32(x64emu_t *emu, uintptr_t fnc);
void pFEupp_32(x64emu_t *emu, uintptr_t fnc);
void pFEpip_32(x64emu_t *emu, uintptr_t fnc);
void pFEppi_32(x64emu_t *emu, uintptr_t fnc);
void pFEppp_32(x64emu_t *emu, uintptr_t fnc);
void pFEXpL_32(x64emu_t *emu, uintptr_t fnc);
Expand Down Expand Up @@ -811,6 +812,7 @@ void LFEppLL_32(x64emu_t *emu, uintptr_t fnc);
void LFpLppa_32(x64emu_t *emu, uintptr_t fnc);
void LFXLuuu_32(x64emu_t *emu, uintptr_t fnc);
void LFXLpuu_32(x64emu_t *emu, uintptr_t fnc);
void pFEppip_32(x64emu_t *emu, uintptr_t fnc);
void pFEXipp_32(x64emu_t *emu, uintptr_t fnc);
void pFuiupp_32(x64emu_t *emu, uintptr_t fnc);
void pFpiiuu_32(x64emu_t *emu, uintptr_t fnc);
Expand Down
127 changes: 127 additions & 0 deletions src/wrapped32/wrappedsdl2image.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <dlfcn.h>

#include "wrappedlibs.h"

#include "debug.h"
#include "wrapper32.h"
#include "bridge.h"
#include "librarian/library_private.h"
#include "x64emu.h"
#include "emu/x64emu_private.h"
#include "box32context.h"
#include "sdl2rwops.h"
#include "sdl2align32.h"

static const char* sdl2imageName = "libSDL2_image-2.0.so.0";
#define LIBNAME sdl2image

#define ADDED_FUNCTIONS() \

#include "generated/wrappedsdl2imagetypes32.h"

#include "wrappercallback32.h"

#define GO(A) \
EXPORT void *my32_2_##A(x64emu_t* emu, void* a) \
{ \
inplace_SDL2_RWops_to_64(a); \
SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a); \
void* r = my->A(rw); \
RWNativeEnd2(rw); \
inplace_SDL2_RWops_to_32(a); \
inplace_SDL2_Surface_to_32(r); \
return r; \
}
GO(IMG_LoadBMP_RW)
GO(IMG_LoadCUR_RW)
GO(IMG_LoadGIF_RW)
GO(IMG_LoadICO_RW)
GO(IMG_LoadJPG_RW)
GO(IMG_LoadLBM_RW)
GO(IMG_LoadPCX_RW)
GO(IMG_LoadPNG_RW)
GO(IMG_LoadPNM_RW)
GO(IMG_LoadTGA_RW)
GO(IMG_LoadTIF_RW)
GO(IMG_LoadWEBP_RW)
GO(IMG_LoadXCF_RW)
GO(IMG_LoadXPM_RW)
GO(IMG_LoadXV_RW)
#undef GO

EXPORT void *my32_2_IMG_LoadTyped_RW(x64emu_t* emu, void* a, int32_t b, void* c)
{
inplace_SDL2_RWops_to_64(a);
SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a);
void* r = my->IMG_LoadTyped_RW(rw, b, c);
if(b==0) {
RWNativeEnd2(rw);
inplace_SDL2_RWops_to_32(a);
}
inplace_SDL2_Surface_to_32(r);
return r;
}
EXPORT void *my32_2_IMG_Load_RW(x64emu_t* emu, void* a, int32_t b)
{
inplace_SDL2_RWops_to_64(a);
SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a);
void* r = my->IMG_Load_RW(rw, b);
if(b==0) {
RWNativeEnd2(rw);
inplace_SDL2_RWops_to_32(a);
}
inplace_SDL2_Surface_to_32(r);
return r;
}
//EXPORT int32_t my32_2_IMG_SavePNG_RW(x64emu_t* emu, void* s, void* a, int32_t b)
//{
// // some old? fuction signature use IMG_SavePNG_RW(dst, surf, compression) instead of the IMG_SavePNG_RW(surf, dst, freedst)
// // need to try detect if s is in fact a RWops
// int32_t r;
// if(isRWops32((my_SDL2_RWops_32_t*)s) && !isRWops32((my_SDL2_RWops_32_t*)a)) {
// inplace_SDL2_RWops_to_64(s);
// SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)s);
// r = my->IMG_SavePNG_RW(a, rw, 0);
// RWNativeEnd2(rw);
// } else {
// SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a);
// r = my->IMG_SavePNG_RW(s, rw, b);
// if(b==0)
// RWNativeEnd2(rw);
// }
// return r;
//}

EXPORT void* my32_2_IMG_LoadTexture_RW(x64emu_t* emu, void* rend, void* a, int32_t b)
{
inplace_SDL2_RWops_to_64(a);
SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a);
void* r = my->IMG_LoadTexture_RW(rend, rw, b);
if(b==0) {
RWNativeEnd2(rw);
inplace_SDL2_RWops_to_32(a);
}
return r;
}

EXPORT void* my32_2_IMG_LoadTextureTyped_RW(x64emu_t* emu, void* rend, void* a, int32_t b, void* type)
{
inplace_SDL2_RWops_to_64(a);
SDL2_RWops_t *rw = RWNativeStart2(emu, (SDL2_RWops_t*)a);
void* r = my->IMG_LoadTextureTyped_RW(rend, rw, b, type);
if(b==0) {
RWNativeEnd2(rw);
inplace_SDL2_RWops_to_32(a);
}
return r;
}

#define ALTMY my32_2_

#define NEEDED_LIBS "libSDL2-2.0.so.0"

#include "wrappedlib_init32.h"
Loading

0 comments on commit 2b0c8d4

Please sign in to comment.