From 3d302f5c7a60b43e49a3a68758b61ab350c55ce3 Mon Sep 17 00:00:00 2001 From: GaryOderNichts <12049776+GaryOderNichts@users.noreply.github.com> Date: Thu, 22 Oct 2020 15:03:38 +0200 Subject: [PATCH] wiiu: Endian and other fixes now loads up to point where it's converting textures and crashes --- src/audio/sampman_oal.cpp | 10 ++--- src/core/ControllerConfig.h | 3 -- src/core/FileLoader.cpp | 87 +++++++++++++++++++++++++++++++++++++ src/core/Streaming.cpp | 4 ++ src/core/common.h | 12 +++++ src/fakerw/fake.cpp | 2 + src/rw/TexRead.cpp | 2 +- src/skel/crossplatform.cpp | 1 + src/skel/wiiu/wiiu.cpp | 5 +-- src/text/Text.cpp | 2 +- 10 files changed, 114 insertions(+), 14 deletions(-) diff --git a/src/audio/sampman_oal.cpp b/src/audio/sampman_oal.cpp index e828ca4d..dc7b3c95 100644 --- a/src/audio/sampman_oal.cpp +++ b/src/audio/sampman_oal.cpp @@ -1448,11 +1448,11 @@ cSampleManager::InitialiseSampleBanks(void) // fix endianess for (int i = 0; i < TOTAL_AUDIO_SAMPLES; i++) { - m_aSamples[i].nOffset = __builtin_bswap32(m_aSamples[i].nOffset); - m_aSamples[i].nSize = __builtin_bswap32(m_aSamples[i].nSize); - m_aSamples[i].nFrequency = __builtin_bswap32(m_aSamples[i].nFrequency); - m_aSamples[i].nLoopStart = __builtin_bswap32(m_aSamples[i].nLoopStart); - m_aSamples[i].nLoopEnd = __builtin_bswap32(m_aSamples[i].nLoopEnd); + m_aSamples[i].nOffset = BSWAP32(m_aSamples[i].nOffset); + m_aSamples[i].nSize = BSWAP32(m_aSamples[i].nSize); + m_aSamples[i].nFrequency = BSWAP32(m_aSamples[i].nFrequency); + m_aSamples[i].nLoopStart = BSWAP32(m_aSamples[i].nLoopStart); + m_aSamples[i].nLoopEnd = BSWAP32(m_aSamples[i].nLoopEnd); } #endif #ifdef AUDIO_OPUS diff --git a/src/core/ControllerConfig.h b/src/core/ControllerConfig.h index ed3ff8f6..16d67104 100644 --- a/src/core/ControllerConfig.h +++ b/src/core/ControllerConfig.h @@ -119,10 +119,7 @@ struct GlfwJoyState { #ifdef __WIIU__ struct WiiUJoyState { - int8 id; - bool isGamepad; VPADStatus status; - bool mappedButtons[17]; }; #endif diff --git a/src/core/FileLoader.cpp b/src/core/FileLoader.cpp index b4da1a5e..fb989d9d 100644 --- a/src/core/FileLoader.cpp +++ b/src/core/FileLoader.cpp @@ -193,6 +193,9 @@ CFileLoader::LoadCollisionFile(const char *filename) while(CFileMgr::Read(fd, (char*)&header, sizeof(header))){ assert(strncmp(header.ident, "COLL", 4) == 0); +#ifdef BIGENDIAN + header.size = BSWAP32(header.size); +#endif CFileMgr::Read(fd, (char*)work_buff, header.size); memcpy(modelname, work_buff, 24); @@ -218,6 +221,7 @@ CFileLoader::LoadCollisionModel(uint8 *buf, CColModel &model, char *modelname) { int i; +#ifndef BIGENDIAN model.boundingSphere.radius = *(float*)(buf); model.boundingSphere.center.x = *(float*)(buf+4); model.boundingSphere.center.y = *(float*)(buf+8); @@ -229,59 +233,142 @@ CFileLoader::LoadCollisionModel(uint8 *buf, CColModel &model, char *modelname) model.boundingBox.max.y = *(float*)(buf+32); model.boundingBox.max.z = *(float*)(buf+36); model.numSpheres = *(int16*)(buf+40); +#else + // TODO make a macro for those buf to float bswaps + // wtf is 0 | 128? + model.boundingSphere.radius = FLOATSWAP32(*(float*)(buf)); + model.boundingSphere.center.x = FLOATSWAP32(*(float*)(buf+4)); + model.boundingSphere.center.y = FLOATSWAP32(*(float*)(buf+8)); + model.boundingSphere.center.z = FLOATSWAP32(*(float*)(buf+12)); + model.boundingBox.min.x = FLOATSWAP32(*(float*)(buf+16)); + model.boundingBox.min.y = FLOATSWAP32(*(float*)(buf+20)); + model.boundingBox.min.z = FLOATSWAP32(*(float*)(buf+24)); + model.boundingBox.max.x = FLOATSWAP32(*(float*)(buf+28)); + model.boundingBox.max.y = FLOATSWAP32(*(float*)(buf+32)); + model.boundingBox.max.z = FLOATSWAP32(*(float*)(buf+36)); + model.numSpheres = (int16) BSWAP16(*(uint16*)(buf+40)); +#endif + buf += 44; if(model.numSpheres > 0){ model.spheres = (CColSphere*)RwMalloc(model.numSpheres*sizeof(CColSphere)); for(i = 0; i < model.numSpheres; i++){ +#ifndef BIGENDIAN model.spheres[i].Set(*(float*)buf, *(CVector*)(buf+4), buf[16], buf[17]); +#else + float radius = FLOATSWAP32(*(float*)buf); + CVector center = *(CVector*)(buf+4); + center.x = FLOATSWAP32(center.x); + center.y = FLOATSWAP32(center.y); + center.z = FLOATSWAP32(center.z); + model.spheres[i].Set(radius, center, buf[16], buf[17]); +#endif buf += 20; } }else model.spheres = nil; +#ifndef BIGENDIAN model.numLines = *(int16*)buf; +#else + model.numLines = (int16) BSWAP16(*(uint16*)buf); +#endif buf += 4; if(model.numLines > 0){ model.lines = (CColLine*)RwMalloc(model.numLines*sizeof(CColLine)); for(i = 0; i < model.numLines; i++){ +#ifndef BIGENDIAN model.lines[i].Set(*(CVector*)buf, *(CVector*)(buf+12)); +#else + CVector p0 = *(CVector*)buf; + p0.x = FLOATSWAP32(p0.x); + p0.y = FLOATSWAP32(p0.y); + p0.z = FLOATSWAP32(p0.z); + CVector p1 = *(CVector*)(buf+12); + p1.x = FLOATSWAP32(p1.x); + p1.y = FLOATSWAP32(p1.y); + p1.z = FLOATSWAP32(p1.z); + model.lines[i].Set(p0, p1); +#endif buf += 24; } }else model.lines = nil; +#ifndef BIGENDIAN model.numBoxes = *(int16*)buf; +#else + model.numBoxes = (int16) BSWAP16(*(uint16*)buf); +#endif buf += 4; if(model.numBoxes > 0){ model.boxes = (CColBox*)RwMalloc(model.numBoxes*sizeof(CColBox)); for(i = 0; i < model.numBoxes; i++){ +#ifndef BIGENDIAN model.boxes[i].Set(*(CVector*)buf, *(CVector*)(buf+12), buf[24], buf[25]); +#else + CVector min = *(CVector*)buf; + min.x = FLOATSWAP32(min.x); + min.y = FLOATSWAP32(min.y); + min.z = FLOATSWAP32(min.z); + CVector max = *(CVector*)(buf+12); + max.x = FLOATSWAP32(max.x); + max.y = FLOATSWAP32(max.y); + max.z = FLOATSWAP32(max.z); + model.boxes[i].Set(min, max, buf[24], buf[25]); +#endif buf += 28; } }else model.boxes = nil; +#ifndef BIGENDIAN int32 numVertices = *(int16*)buf; +#else + int32 numVertices = (int16) BSWAP16(*(uint16*)buf); +#endif buf += 4; if(numVertices > 0){ model.vertices = (CompressedVector*)RwMalloc(numVertices*sizeof(CompressedVector)); for(i = 0; i < numVertices; i++){ +#ifndef BIGENDIAN model.vertices[i].Set(*(float*)buf, *(float*)(buf+4), *(float*)(buf+8)); if(Abs(*(float*)buf) >= 256.0f || Abs(*(float*)(buf+4)) >= 256.0f || Abs(*(float*)(buf+8)) >= 256.0f) printf("%s:Collision volume too big\n", modelname); +#else + float x = FLOATSWAP32(*(float*)buf); + float y = FLOATSWAP32(*(float*)(buf+4)); + float z = FLOATSWAP32(*(float*)(buf+8)); + model.vertices[i].Set(x, y, z); + if(Abs(x) >= 256.0f || + Abs(y) >= 256.0f || + Abs(z) >= 256.0f) + printf("%s:Collision volume too big\n", modelname); +#endif buf += 12; } }else model.vertices = nil; +#ifndef BIGENDIAN model.numTriangles = *(int16*)buf; +#else + model.numTriangles = (int16) BSWAP16(*(uint16*)buf); +#endif buf += 4; if(model.numTriangles > 0){ model.triangles = (CColTriangle*)RwMalloc(model.numTriangles*sizeof(CColTriangle)); for(i = 0; i < model.numTriangles; i++){ +#ifndef BIGENDIAN model.triangles[i].Set(model.vertices, *(int32*)buf, *(int32*)(buf+4), *(int32*)(buf+8), buf[12], buf[13]); +#else + int32 a = (int32) BSWAP32(*(uint32*)buf); + int32 b = (int32) BSWAP32(*(uint32*)(buf+4)); + int32 c = (int32) BSWAP32(*(uint32*)(buf+8)); + model.triangles[i].Set(model.vertices, a, b, c, buf[12], buf[13]); +#endif buf += 16; } }else diff --git a/src/core/Streaming.cpp b/src/core/Streaming.cpp index 507815fa..c0a8d1f6 100644 --- a/src/core/Streaming.cpp +++ b/src/core/Streaming.cpp @@ -381,6 +381,10 @@ CStreaming::LoadCdDirectory(const char *dirname, int n) imgSelector = n<<24; assert(sizeof(direntry) == 32); while(CFileMgr::Read(fd, (char*)&direntry, sizeof(direntry))){ +#ifdef BIGENDIAN + direntry.offset = BSWAP32(direntry.offset); + direntry.size = BSWAP32(direntry.size); +#endif dot = strchr(direntry.name, '.'); if(dot) *dot = '\0'; if(direntry.size > (uint32)ms_streamingBufferSize) diff --git a/src/core/common.h b/src/core/common.h index 3f2412be..768dfffc 100644 --- a/src/core/common.h +++ b/src/core/common.h @@ -78,6 +78,18 @@ typedef int64_t int64; // hardcode ucs-2 typedef uint16_t wchar; +#ifdef BIGENDIAN +inline float _floatswap32(float f) +{ + uint32_t _swapval = __builtin_bswap32(*(uint32_t*)&f); + return *(float*)&_swapval; +} + +#define BSWAP32(x) __builtin_bswap32(x) +#define BSWAP16(x) __builtin_bswap16(x) +#define FLOATSWAP32(x) _floatswap32(x) +#endif + #ifndef nil #define nil NULL #endif diff --git a/src/fakerw/fake.cpp b/src/fakerw/fake.cpp index b9ff0144..2c57fc98 100644 --- a/src/fakerw/fake.cpp +++ b/src/fakerw/fake.cpp @@ -913,6 +913,8 @@ RwInt32 _rwD3D8FindCorrectRasterFormat(RwRasterType type, RwInt32 flags) { #ifdef RW_GL3 return '3LGO'; +#elif __WIIU__ + return 'UIIW'; #endif return flags & 0xF00; } diff --git a/src/rw/TexRead.cpp b/src/rw/TexRead.cpp index c29d0044..3c94762d 100644 --- a/src/rw/TexRead.cpp +++ b/src/rw/TexRead.cpp @@ -71,7 +71,7 @@ RwTexDictionaryGtaStreamRead(RwStream *stream) return nil; #ifdef BIGENDIAN - numTextures = __builtin_bswap32(numTextures); + numTextures = BSWAP32(numTextures); #endif texDict = RwTexDictionaryCreate(); diff --git a/src/skel/crossplatform.cpp b/src/skel/crossplatform.cpp index 6afe88b8..f0988648 100644 --- a/src/skel/crossplatform.cpp +++ b/src/skel/crossplatform.cpp @@ -174,6 +174,7 @@ char* casepath(char const* path, bool checkPathFirst) char* c; while (c = strsep(&p, "/\\")) { + debug("sep string from %s is %s", p, c); // May be trailing slash(allow), slash at the start(avoid), or multiple slashes(avoid) if (*c == '\0') { diff --git a/src/skel/wiiu/wiiu.cpp b/src/skel/wiiu/wiiu.cpp index fe5f4a3b..4c08b9fe 100644 --- a/src/skel/wiiu/wiiu.cpp +++ b/src/skel/wiiu/wiiu.cpp @@ -284,7 +284,7 @@ psInitialize(void) #endif // TODO: Is there a way to get free ram on wiiu? - _dwMemAvailPhys = 1024; //systemInfo.freeram; + _dwMemAvailPhys = 1024*1024*170; //systemInfo.freeram; _dwOperatingSystemVersion = OS_WINXP; // To fool other classes // debug("Physical memory size %u\n", systemInfo.totalram); @@ -1382,9 +1382,6 @@ void CapturePad(RwInt32 padID) memcpy(&ControlsManager.m_NewState.status, &status, sizeof(VPADStatus)); - ControlsManager.m_NewState.id = 0; - ControlsManager.m_NewState.isGamepad = true; - // if (ControlsManager.m_NewState.isGamepad) // { // memcpy(&ControlsManager.m_NewState.mappedButtons, gamepadState.buttons, sizeof(gamepadState.buttons)); diff --git a/src/text/Text.cpp b/src/text/Text.cpp index 680e40f4..4ec90807 100644 --- a/src/text/Text.cpp +++ b/src/text/Text.cpp @@ -71,7 +71,7 @@ CText::Load(void) sectlen = (int)filedata[offset+3]<<24 | (int)filedata[offset+2]<<16 | (int)filedata[offset+1]<<8 | (int)filedata[offset+0]; #ifdef BIGENDIAN - sectlen = __builtin_bswap32(sectlen); + sectlen = BSWAP32(sectlen); #endif offset += 4; if(sectlen != 0){