diff --git a/TheForceEngine/TFE_Archive/gobArchive.cpp b/TheForceEngine/TFE_Archive/gobArchive.cpp index 031cf663d..bc1ed8293 100644 --- a/TheForceEngine/TFE_Archive/gobArchive.cpp +++ b/TheForceEngine/TFE_Archive/gobArchive.cpp @@ -1,6 +1,7 @@ #include #include +#include #include "gobArchive.h" #include #include @@ -53,8 +54,10 @@ bool GobArchive::validate(const char *archivePath, s32 minFileCount) file.close(); return false; } + header.MASTERX = TFE_Endian::swapLE32(header.MASTERX); file.seek(header.MASTERX); file.readBuffer(&MASTERN, sizeof(long)); + MASTERN = TFE_Endian::swapLE32(MASTERN); file.close(); return MASTERN >= minFileCount; @@ -68,11 +71,18 @@ bool GobArchive::open(const char *archivePath) // Read the directory. m_file.readBuffer(&m_header, sizeof(GOB_Header_t)); + m_header.MASTERX = TFE_Endian::swapLE32(m_header.MASTERX); m_file.seek(m_header.MASTERX); m_file.readBuffer(&m_fileList.MASTERN, sizeof(u32)); + m_fileList.MASTERN = TFE_Endian::swapLE32(m_fileList.MASTERN); m_fileList.entries = new GOB_Entry_t[m_fileList.MASTERN]; m_file.readBuffer(m_fileList.entries, sizeof(GOB_Entry_t), m_fileList.MASTERN); + for (s32 i = 0; i < m_fileList.MASTERN; i++) + { + m_fileList.entries[i].IX = TFE_Endian::swapLE32(m_fileList.entries[i].IX); + m_fileList.entries[i].LEN = TFE_Endian::swapLE32(m_fileList.entries[i].LEN); + } strcpy(m_archivePath, archivePath); m_file.close(); diff --git a/TheForceEngine/TFE_Archive/labArchive.cpp b/TheForceEngine/TFE_Archive/labArchive.cpp index e31a7ee5a..d2686d20a 100644 --- a/TheForceEngine/TFE_Archive/labArchive.cpp +++ b/TheForceEngine/TFE_Archive/labArchive.cpp @@ -1,6 +1,7 @@ #include #include +#include #include "labArchive.h" #include #include @@ -25,11 +26,20 @@ bool LabArchive::open(const char *archivePath) // Read the directory. m_file.readBuffer(&m_header, sizeof(LAB_Header_t)); + m_header.version = TFE_Endian::swapLE32(m_header.version); + m_header.fileCount = TFE_Endian::swapLE32(m_header.fileCount); + m_header.stringTableSize = TFE_Endian::swapLE32(m_header.stringTableSize); m_stringTable = new char[m_header.stringTableSize + 1]; m_entries = new LAB_Entry_t[m_header.fileCount]; // Read the file entries. m_file.readBuffer(m_entries, sizeof(LAB_Entry_t), m_header.fileCount); + for (u32 i = 0; i < m_header.fileCount; i++) + { + m_entries[i].nameOffset = TFE_Endian::swapLE32(m_entries[i].nameOffset); + m_entries[i].dataOffset = TFE_Endian::swapLE32(m_entries[i].dataOffset); + m_entries[i].len = TFE_Endian::swapLE32(m_entries[i].len); + } // Read string table. m_file.readBuffer(m_stringTable, m_header.stringTableSize); diff --git a/TheForceEngine/TFE_Archive/lfdArchive.cpp b/TheForceEngine/TFE_Archive/lfdArchive.cpp index 6366a5dfb..9bb5ed2ed 100644 --- a/TheForceEngine/TFE_Archive/lfdArchive.cpp +++ b/TheForceEngine/TFE_Archive/lfdArchive.cpp @@ -1,6 +1,7 @@ #include #include +#include #include "lfdArchive.h" #include #include @@ -39,6 +40,7 @@ bool LfdArchive::open(const char *archivePath) // Read the directory. LFD_Entry_t root, entry; m_file.readBuffer(&root, sizeof(LFD_Entry_t)); + root.LENGTH = TFE_Endian::swapLE32(root.LENGTH); m_fileList.MASTERN = root.LENGTH / sizeof(LFD_Entry_t); m_fileList.entries = new LFD_EntryFinal_t[m_fileList.MASTERN]; @@ -46,6 +48,7 @@ bool LfdArchive::open(const char *archivePath) for (s32 i = 0; i < m_fileList.MASTERN; i++) { m_file.readBuffer(&entry, sizeof(LFD_Entry_t)); + entry.LENGTH = TFE_Endian::swapLE32(entry.LENGTH); char name[9] = { 0 }; char ext[5] = { 0 }; diff --git a/TheForceEngine/TFE_Asset/modelAsset_jedi.cpp b/TheForceEngine/TFE_Asset/modelAsset_jedi.cpp index bb7b43999..ed4feb2e8 100644 --- a/TheForceEngine/TFE_Asset/modelAsset_jedi.cpp +++ b/TheForceEngine/TFE_Asset/modelAsset_jedi.cpp @@ -402,14 +402,14 @@ namespace TFE_Model_Jedi const char* buffer = parser.readLine(bufferPos, true); if (!buffer) { return false; } - f32 version; - if (sscanf(buffer, "3DO %f", &version) != 1) + s32 versionMajor, versionMinor; + if (sscanf(buffer, "3DO %d.%d", &versionMajor, &versionMinor) != 2) { TFE_System::logWrite(LOG_ERROR, "Object3D_Load", "'%s' has no valid name.", name); assert(0); return false; } - if (version < 1.2f) + if (versionMajor < 1 || (versionMajor == 1 && versionMinor < 2)) { TFE_System::logWrite(LOG_ERROR, "Object3D_Load", "'%s' has inavlid version", name); assert(0); diff --git a/TheForceEngine/TFE_Asset/spriteAsset_Jedi.cpp b/TheForceEngine/TFE_Asset/spriteAsset_Jedi.cpp index 7b7045d4c..6e5071a14 100644 --- a/TheForceEngine/TFE_Asset/spriteAsset_Jedi.cpp +++ b/TheForceEngine/TFE_Asset/spriteAsset_Jedi.cpp @@ -2,6 +2,7 @@ #include "spriteAsset_Jedi.h" #include +#include #include #include #include @@ -62,8 +63,20 @@ namespace TFE_Sprite_Jedi const u8* data = s_buffer.data(); // Determine ahead of time how much we need to allocate. - const WaxFrame* base_frame = (WaxFrame*)data; - const WaxCell* base_cell = WAX_CellPtr(data, base_frame); + WaxFrame* base_frame = (WaxFrame*)data; + base_frame->offsetX = TFE_Endian::swapLE32(base_frame->offsetX); + base_frame->offsetY = TFE_Endian::swapLE32(base_frame->offsetY); + base_frame->flip = TFE_Endian::swapLE32(base_frame->flip); + base_frame->cellOffset = TFE_Endian::swapLE32(base_frame->cellOffset); + base_frame->widthWS = TFE_Endian::swapLE32(base_frame->widthWS); + base_frame->heightWS = TFE_Endian::swapLE32(base_frame->heightWS); + + WaxCell* base_cell = WAX_CellPtr(data, base_frame); + base_cell->sizeX = TFE_Endian::swapLE32(base_cell->sizeX); + base_cell->sizeY = TFE_Endian::swapLE32(base_cell->sizeY); + base_cell->compressed = TFE_Endian::swapLE32(base_cell->compressed); + base_cell->dataSize = TFE_Endian::swapLE32(base_cell->dataSize); + base_cell->columnOffset = TFE_Endian::swapLE32(base_cell->columnOffset); const u32 columnSize = base_cell->sizeX * sizeof(u32); // This is a "load in place" format in the original code. @@ -90,6 +103,11 @@ namespace TFE_Sprite_Jedi { // Update the column offset, it starts right after the cell data. cell->columnOffset = frame->cellOffset + sizeof(WaxCell); + u32* columns = (u32*)((u8*)asset + cell->columnOffset); + for (s32 c = 0; c < cell->sizeX; c++) + { + columns[c] = TFE_Endian::swapLE32(columns[c]); + } } else { @@ -211,7 +229,18 @@ namespace TFE_Sprite_Jedi file.close(); const u8* data = s_buffer.data(); - const Wax* srcWax = (Wax*)data; + Wax* srcWax = (Wax*)data; + srcWax->version = TFE_Endian::swapLE32(srcWax->version); + srcWax->animCount = TFE_Endian::swapLE32(srcWax->animCount); + srcWax->frameCount = TFE_Endian::swapLE32(srcWax->frameCount); + srcWax->cellCount = TFE_Endian::swapLE32(srcWax->cellCount); + srcWax->xScale = TFE_Endian::swapLE32(srcWax->xScale); + srcWax->yScale = TFE_Endian::swapLE32(srcWax->yScale); + srcWax->xtraLight = TFE_Endian::swapLE32(srcWax->xtraLight); + for (s32 animIdx = 0; animIdx < WAX_MAX_ANIM; animIdx++) + { + srcWax->animOffsets[animIdx] = TFE_Endian::swapLE32(srcWax->animOffsets[animIdx]); + } // every animation is filled out until the end, so no animations = no wax. if (!srcWax->animOffsets[0]) @@ -221,20 +250,71 @@ namespace TFE_Sprite_Jedi s_cellOffsets.clear(); // First determine the size to allocate (note that this will overallocate a bit because cells are shared). + std::vector swappedAnims; + std::vector swappedViews; + std::vector swappedFrames; + std::vector swappedCells; u32 sizeToAlloc = sizeof(JediWax) + (u32)s_buffer.size(); const s32* animOffset = srcWax->animOffsets; for (s32 animIdx = 0; animIdx < 32 && animOffset[animIdx]; animIdx++) { WaxAnim* anim = (WaxAnim*)(data + animOffset[animIdx]); + if (animOffset[animIdx] && std::find(swappedAnims.begin(), swappedAnims.end(), animOffset[animIdx]) == swappedAnims.end()) + { + swappedAnims.push_back(animOffset[animIdx]); + anim->worldWidth = TFE_Endian::swapLE32(anim->worldWidth); + anim->worldHeight = TFE_Endian::swapLE32(anim->worldHeight); + anim->frameRate = TFE_Endian::swapLE32(anim->frameRate); + anim->frameCount = TFE_Endian::swapLE32(anim->frameCount); + for (s32 v = 0; v < WAX_MAX_VIEWS; v++) + { + anim->viewOffsets[v] = TFE_Endian::swapLE32(anim->viewOffsets[v]); + } + } const s32* viewOffsets = anim->viewOffsets; for (s32 v = 0; v < 32; v++) { - const WaxView* view = (WaxView*)(data + viewOffsets[v]); + WaxView* view = (WaxView*)(data + viewOffsets[v]); + if (viewOffsets[v] && std::find(swappedViews.begin(), swappedViews.end(), viewOffsets[v]) == swappedViews.end()) + { + swappedViews.push_back(viewOffsets[v]); + for (s32 f = 0; f < WAX_MAX_FRAMES; f++) + { + view->frameOffsets[f] = TFE_Endian::swapLE32(view->frameOffsets[f]); + } + } const s32* frameOffset = view->frameOffsets; for (s32 f = 0; f < 32 && frameOffset[f]; f++) { - const WaxFrame* frame = (WaxFrame*)(data + frameOffset[f]); - const WaxCell* cell = frame->cellOffset ? (WaxCell*)(data + frame->cellOffset) : nullptr; + WaxFrame* frame = (WaxFrame*)(data + frameOffset[f]); + if (frameOffset[f] && std::find(swappedFrames.begin(), swappedFrames.end(), frameOffset[f]) == swappedFrames.end()) + { + swappedFrames.push_back(frameOffset[f]); + frame->offsetX = TFE_Endian::swapLE32(frame->offsetX); + frame->offsetY = TFE_Endian::swapLE32(frame->offsetY); + frame->flip = TFE_Endian::swapLE32(frame->flip); + frame->cellOffset = TFE_Endian::swapLE32(frame->cellOffset); + frame->widthWS = TFE_Endian::swapLE32(frame->widthWS); + frame->heightWS = TFE_Endian::swapLE32(frame->heightWS); + } + WaxCell* cell = frame->cellOffset ? (WaxCell*)(data + frame->cellOffset) : nullptr; + if (frame->cellOffset && std::find(swappedCells.begin(), swappedCells.end(), frame->cellOffset) == swappedCells.end()) + { + swappedCells.push_back(frame->cellOffset); + cell->sizeX = TFE_Endian::swapLE32(cell->sizeX); + cell->sizeY = TFE_Endian::swapLE32(cell->sizeY); + cell->compressed = TFE_Endian::swapLE32(cell->compressed); + cell->dataSize = TFE_Endian::swapLE32(cell->dataSize); + cell->columnOffset = TFE_Endian::swapLE32(cell->columnOffset); + if (cell->compressed == 1) + { + u32* columns = (u32*)(data + frame->cellOffset + sizeof(WaxCell)); + for (s32 c = 0; c < cell->sizeX; c++) + { + columns[c] = TFE_Endian::swapLE32(columns[c]); + } + } + } if (cell && cell->compressed == 0 && isUniqueCell(frame->cellOffset)) { sizeToAlloc += cell->sizeX * sizeof(u32); diff --git a/TheForceEngine/TFE_DarkForces/GameUI/delt.cpp b/TheForceEngine/TFE_DarkForces/GameUI/delt.cpp index 64c6bf0eb..886b65038 100644 --- a/TheForceEngine/TFE_DarkForces/GameUI/delt.cpp +++ b/TheForceEngine/TFE_DarkForces/GameUI/delt.cpp @@ -2,6 +2,7 @@ #include #include "delt.h" +#include #include #include #include @@ -93,7 +94,9 @@ namespace TFE_DarkForces file.readBuffer(buffer, (u32)size); file.close(); - const s16 frameCount = *((s16*)buffer); + s16 frameCount = *((s16*)buffer); + frameCount = TFE_Endian::swapLE16(frameCount); + const u8* frames = buffer + 2; *outFrames = (DeltFrame*)game_alloc(sizeof(DeltFrame) * frameCount); @@ -102,6 +105,7 @@ namespace TFE_DarkForces for (s32 i = 0; i < frameCount; i++) { u32 size = *((u32*)frames); + size = TFE_Endian::swapLE32(size); frames += 4; loadDeltIntoFrame(&outFramePtr[i], frames, size); @@ -226,6 +230,10 @@ namespace TFE_DarkForces void loadDeltIntoFrame(DeltFrame* frame, const u8* buffer, u32 size) { DeltHeader header = *((DeltHeader*)buffer); + header.offsetX = TFE_Endian::swapLE16(header.offsetX); + header.offsetY = TFE_Endian::swapLE16(header.offsetY); + header.sizeX = TFE_Endian::swapLE16(header.sizeX); + header.sizeY = TFE_Endian::swapLE16(header.sizeY); header.sizeX++; header.sizeY++; @@ -255,10 +263,10 @@ namespace TFE_DarkForces data += sizeof(DeltLine); - const s32 startX = line->xStart; - const s32 startY = line->yStart; - const bool rle = (line->sizeAndType & 1) ? true : false; - s32 pixelCount = (line->sizeAndType >> 1) & 0x3FFF; + const s32 startX = TFE_Endian::swapLE16(line->xStart); + const s32 startY = TFE_Endian::swapLE16(line->yStart); + const bool rle = (TFE_Endian::swapLE16(line->sizeAndType) & 1) ? true : false; + s32 pixelCount = (TFE_Endian::swapLE16(line->sizeAndType) >> 1) & 0x3FFF; u8* image = frame->texture.image + startX + startY * header.sizeX; while (pixelCount > 0) diff --git a/TheForceEngine/TFE_DarkForces/Landru/cutscene_film.cpp b/TheForceEngine/TFE_DarkForces/Landru/cutscene_film.cpp index 774a5ddea..e6c69935c 100644 --- a/TheForceEngine/TFE_DarkForces/Landru/cutscene_film.cpp +++ b/TheForceEngine/TFE_DarkForces/Landru/cutscene_film.cpp @@ -9,6 +9,7 @@ #include "time.h" #include #include +#include #include #include #include @@ -128,19 +129,13 @@ namespace TFE_DarkForces } } - u32 swapEndian(u32 x) - { - u8* buffer = (u8*)&x; - return u32(buffer[3]) | (u32(buffer[2])<<8) | (u32(buffer[1])<<16) | (u32(buffer[0])<<24); - } - JBool cutsceneFilm_loadResources(FileStream* file, u8** array, s16 arraySize) { for (s32 i = 0; i < arraySize; i++) { s32 type; file->read(&type); - type = swapEndian(type); + type = TFE_Endian::swapBE32(type); char name[32]; file->readBuffer(name, 8); @@ -152,6 +147,10 @@ namespace TFE_DarkForces file->read(&id); file->read(&chunks); file->read(&used); + size = TFE_Endian::swapLE32(size); + id = TFE_Endian::swapLE16(id); + chunks = TFE_Endian::swapLE16(chunks); + used = TFE_Endian::swapLE16(used); // Allocate space for the object. FilmObject* obj = (FilmObject*)landru_alloc(sizeof(FilmObject) + used); @@ -377,6 +376,9 @@ namespace TFE_DarkForces file.read(&version); file.read(&cellCount); file.read(&arraySize); + version = TFE_Endian::swapLE16(version); + cellCount = TFE_Endian::swapLE16(cellCount); + arraySize = TFE_Endian::swapLE16(arraySize); if (version != CF_VERSION) { @@ -460,7 +462,7 @@ namespace TFE_DarkForces JBool cutsceneFilm_isTimeStamp(Film* film, FilmObject* filmObj, u8* data) { s16* chunk = (s16*)(data + filmObj->offset); - if (chunk[1] == CF_CMD_TIMESTAMP && chunk[2] == (s16)film->curCell) + if (TFE_Endian::swapLE16(chunk[1]) == CF_CMD_TIMESTAMP && TFE_Endian::swapLE16(chunk[2]) == (s16)film->curCell) { return JTRUE; } @@ -596,13 +598,13 @@ namespace TFE_DarkForces void cutsceneFilm_stepFilmFrame(FilmObject* filmObj, u8* data) { s16* chunk = (s16*)(data + filmObj->offset); - filmObj->offset += chunk[0]; + filmObj->offset += TFE_Endian::swapLE16(chunk[0]); } JBool cutsceneFilm_isNextFrame(FilmObject* filmObj, u8* data) { s16* chunk = (s16*)(data + filmObj->offset); - if (chunk[1] == CF_CMD_TIMESTAMP || chunk[1] == CF_CMD_END) + if (TFE_Endian::swapLE16(chunk[1]) == CF_CMD_TIMESTAMP || TFE_Endian::swapLE16(chunk[1]) == CF_CMD_END) { return JTRUE; } @@ -612,55 +614,55 @@ namespace TFE_DarkForces void cutsceneFilm_setActorToFilm(FilmObject* filmObj, u8* data) { s16* chunk = (s16*)(data + filmObj->offset); - u32 type = chunk[1]; + u32 type = TFE_Endian::swapLE16(chunk[1]); LActor* actor = (LActor*)filmObj->data; chunk += 2; switch (type) { case CF_CMD_ACTOR_POS: - lactor_setPos(actor, chunk[0], chunk[1], chunk[2], chunk[3]); + lactor_setPos(actor, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1]), TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); break; case CF_CMD_ACTOR_VEL: - lactor_setSpeed(actor, chunk[0], chunk[1], chunk[2], chunk[3]); + lactor_setSpeed(actor, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1]), TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); break; case CF_CMD_ACTOR_Z: - lactor_setZPlane(actor, chunk[0]); + lactor_setZPlane(actor, TFE_Endian::swapLE16(chunk[0])); break; case CF_CMD_ACTOR_STATE: - lactor_setState(actor, chunk[0], chunk[1]); + lactor_setState(actor, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1])); break; case CF_CMD_ACTOR_STATEV: - lactor_setStateSpeed(actor, chunk[0], chunk[1]); + lactor_setStateSpeed(actor, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1])); break; case CF_CMD_ACTOR_VAR1: - actor->var1 = chunk[0]; + actor->var1 = TFE_Endian::swapLE16(chunk[0]); break; case CF_CMD_ACTOR_VAR2: - actor->var2 = chunk[0]; + actor->var2 = TFE_Endian::swapLE16(chunk[0]); break; case CF_CMD_ACTOR_CLIP: - lrect_set(&actor->frame, chunk[0], chunk[1], chunk[2], chunk[3]); + lrect_set(&actor->frame, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1]), TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); break; case CF_CMD_ACTOR_CLIPV: - lrect_set(&actor->frameVelocity, chunk[0], chunk[1], chunk[2], chunk[3]); + lrect_set(&actor->frameVelocity, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1]), TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); break; case CF_CMD_ACTOR_SHOW: - if (chunk[0]) { lactor_show(actor); } + if (TFE_Endian::swapLE16(chunk[0])) { lactor_show(actor); } else { lactor_hide(actor); } break; case CF_CMD_ACTOR_FLIP: - lactor_setFlip(actor, chunk[0], chunk[1]); + lactor_setFlip(actor, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1])); break; } } @@ -669,7 +671,7 @@ namespace TFE_DarkForces { s16* chunk = (s16*)(data + filmObj->offset); u8* byteChunk = (u8*)chunk; - u16 type = chunk[1]; + u16 type = TFE_Endian::swapLE16(chunk[1]); switch (type) { @@ -693,8 +695,8 @@ namespace TFE_DarkForces } break; case CF_CMD_VIEW_FADE: { - s16 fade = chunk[2]; - s16 colorFade = chunk[3]; + s16 fade = TFE_Endian::swapLE16(chunk[2]); + s16 colorFade = TFE_Endian::swapLE16(chunk[3]); cutsceneFilm_setFade(fade, colorFade); } break; } @@ -703,7 +705,7 @@ namespace TFE_DarkForces void cutsceneFilm_setPaletteToFilm(Film* film, FilmObject* filmObj, u8* data) { s16* chunk = (s16*)(data + filmObj->offset); - if (chunk[1] == CF_CMD_PALETTE_SET) + if (TFE_Endian::swapLE16(chunk[1]) == CF_CMD_PALETTE_SET) { lpalette_setDstPal((LPalette*)filmObj->data); } @@ -713,7 +715,7 @@ namespace TFE_DarkForces { LSound* sound = (LSound*)filmObj->data; s16* chunk = (s16*)(data + filmObj->offset); - u16 type = chunk[1]; + u16 type = TFE_Endian::swapLE16(chunk[1]); chunk += 2; switch (type) @@ -735,15 +737,15 @@ namespace TFE_DarkForces } break; case CF_CMD_SOUND_VOLUME: { - setSoundVolume(sound, chunk[0]); + setSoundVolume(sound, TFE_Endian::swapLE16(chunk[0])); } break; case CF_CMD_SOUND_FADE: { - setSoundFade(sound, chunk[0], chunk[1]); + setSoundFade(sound, TFE_Endian::swapLE16(chunk[0]), TFE_Endian::swapLE16(chunk[1])); } break; case CF_CMD_SOUND_VAR1: { - sound->var1 = chunk[0]; + sound->var1 = TFE_Endian::swapLE16(chunk[0]); if (sound->var1 == 1) { setSoundKeep(sound); @@ -751,11 +753,11 @@ namespace TFE_DarkForces } break; case CF_CMD_SOUND_VAR2: { - sound->var2 = chunk[0]; + sound->var2 = TFE_Endian::swapLE16(chunk[0]); } break; case CF_CMD_SOUND_CMD: { - if (chunk[0]) + if (TFE_Endian::swapLE16(chunk[0])) { if (sound->var2 == 1) { @@ -767,15 +769,15 @@ namespace TFE_DarkForces } } - if (chunk[1]) { setSoundVolume(sound, chunk[1]); } - if (chunk[2] || chunk[3]) + if (TFE_Endian::swapLE16(chunk[1])) { setSoundVolume(sound, TFE_Endian::swapLE16(chunk[1])); } + if (TFE_Endian::swapLE16(chunk[2]) || TFE_Endian::swapLE16(chunk[3])) { - setSoundFade(sound, chunk[2], chunk[3]); + setSoundFade(sound, TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); } } break; case CF_CMD_SOUND_CMD2: { - if (chunk[0]) + if (TFE_Endian::swapLE16(chunk[0])) { if (sound->var2 == 1) { @@ -787,14 +789,14 @@ namespace TFE_DarkForces } } - if (chunk[1]) { setSoundVolume(sound, chunk[1]); } - if (chunk[2] || chunk[3]) + if (TFE_Endian::swapLE16(chunk[1])) { setSoundVolume(sound, TFE_Endian::swapLE16(chunk[1])); } + if (TFE_Endian::swapLE16(chunk[2]) || TFE_Endian::swapLE16(chunk[3])) { - setSoundFade(sound, chunk[2], chunk[3]); + setSoundFade(sound, TFE_Endian::swapLE16(chunk[2]), TFE_Endian::swapLE16(chunk[3])); } - if (chunk[4]) { setSoundPan(sound, chunk[4]); } - if (chunk[5] || chunk[6]) { setSoundPanFade(sound, chunk[5], chunk[6]); } + if (TFE_Endian::swapLE16(chunk[4])) { setSoundPan(sound, TFE_Endian::swapLE16(chunk[4])); } + if (TFE_Endian::swapLE16(chunk[5]) || TFE_Endian::swapLE16(chunk[6])) { setSoundPanFade(sound, TFE_Endian::swapLE16(chunk[5]), TFE_Endian::swapLE16(chunk[6])); } } break; } } diff --git a/TheForceEngine/TFE_DarkForces/Landru/lactorAnim.cpp b/TheForceEngine/TFE_DarkForces/Landru/lactorAnim.cpp index 8a3c4993f..378826e68 100644 --- a/TheForceEngine/TFE_DarkForces/Landru/lactorAnim.cpp +++ b/TheForceEngine/TFE_DarkForces/Landru/lactorAnim.cpp @@ -4,6 +4,7 @@ #include "cutscene_film.h" #include "lview.h" #include "ltimer.h" +#include #include #include @@ -41,7 +42,7 @@ namespace TFE_DarkForces s16* data = (s16*)lactor_getArrayData(actor, actor->state); if (data && data[0] != -1 && data[1] != -1) { - lrect_set(rect, data[0], data[1], data[2] + 1, data[3] + 1); + lrect_set(rect, TFE_Endian::swapLE16(data[0]), TFE_Endian::swapLE16(data[1]), TFE_Endian::swapLE16(data[2]) + 1, TFE_Endian::swapLE16(data[3]) + 1); JBool xFlip, yFlip; lactor_getFlip(actor, &xFlip, &yFlip); lrect_flip(rect, &actor->bounds, xFlip, yFlip); @@ -89,6 +90,7 @@ namespace TFE_DarkForces s16 animCount; file.read(&animCount); + animCount = TFE_Endian::swapLE16(animCount); u8** array = (u8**)landru_alloc(sizeof(u8*) * animCount); if (array) { @@ -96,6 +98,7 @@ namespace TFE_DarkForces { s32 deltaSize; file.read(&deltaSize); + deltaSize = TFE_Endian::swapLE32(deltaSize); if (deltaSize <= 0) { array[i] = nullptr; continue; } array[i] = (u8*)landru_alloc(deltaSize); diff --git a/TheForceEngine/TFE_DarkForces/Landru/lactorDelt.cpp b/TheForceEngine/TFE_DarkForces/Landru/lactorDelt.cpp index 8527cb070..28f80d23e 100644 --- a/TheForceEngine/TFE_DarkForces/Landru/lactorDelt.cpp +++ b/TheForceEngine/TFE_DarkForces/Landru/lactorDelt.cpp @@ -5,6 +5,7 @@ #include "lview.h" #include "ldraw.h" #include "ltimer.h" +#include #include #include @@ -42,7 +43,7 @@ namespace TFE_DarkForces s16* data = (s16*)actor->data; if (data) { - lrect_set(rect, data[0], data[1], data[2], data[3]); + lrect_set(rect, TFE_Endian::swapLE16(data[0]), TFE_Endian::swapLE16(data[1]), TFE_Endian::swapLE16(data[2]), TFE_Endian::swapLE16(data[3])); JBool hFlip, vFlip; lactor_getFlip(actor, &hFlip, &vFlip); lrect_flip(rect, &actor->bounds, hFlip, JFALSE); @@ -153,11 +154,11 @@ namespace TFE_DarkForces JBool lactorDelt_drawClipped(u8* data, s16 x, s16 y, JBool dirty) { s16* data16 = (s16*)data; - s16 sx = data16[0] + x; - s16 sy = data16[1] + y; + s16 sx = TFE_Endian::swapLE16(data16[0]) + x; + s16 sy = TFE_Endian::swapLE16(data16[1]) + y; - s16 ex = data16[2] + x; - s16 ey = data16[3] + y; + s16 ex = TFE_Endian::swapLE16(data16[2]) + x; + s16 ey = TFE_Endian::swapLE16(data16[3]) + y; data16 += 4; LRect drect; @@ -197,17 +198,17 @@ namespace TFE_DarkForces s16 ex, ey; if (hFlip) { - ex = w - data16[0] + x; - sy = data16[1] + y; - sx = w - data16[2] + x; - ey = data16[3] + y; + ex = w - TFE_Endian::swapLE16(data16[0]) + x; + sy = TFE_Endian::swapLE16(data16[1]) + y; + sx = w - TFE_Endian::swapLE16(data16[2]) + x; + ey = TFE_Endian::swapLE16(data16[3]) + y; } else { - sx = data16[0] + x; - sy = data16[1] + y; - ex = data16[2] + x; - ey = data16[3] + y; + sx = TFE_Endian::swapLE16(data16[0]) + x; + sy = TFE_Endian::swapLE16(data16[1]) + y; + ex = TFE_Endian::swapLE16(data16[2]) + x; + ey = TFE_Endian::swapLE16(data16[3]) + y; } data16 += 4; diff --git a/TheForceEngine/TFE_DarkForces/Landru/ldraw.cpp b/TheForceEngine/TFE_DarkForces/Landru/ldraw.cpp index d916ad9be..091367f69 100644 --- a/TheForceEngine/TFE_DarkForces/Landru/ldraw.cpp +++ b/TheForceEngine/TFE_DarkForces/Landru/ldraw.cpp @@ -2,6 +2,7 @@ #include "lcanvas.h" #include "lsystem.h" #include +#include #include #include #include @@ -75,14 +76,14 @@ namespace TFE_DarkForces while (1) { const s16* deltaLine = (s16*)srcImage; - s16 sizeAndType = deltaLine[0]; + s16 sizeAndType = TFE_Endian::swapLE16(deltaLine[0]); if (sizeAndType == 0) { break; } - s16 xStart = deltaLine[1] + x; - s16 yStart = deltaLine[2] + y; + s16 xStart = TFE_Endian::swapLE16(deltaLine[1]) + x; + s16 yStart = TFE_Endian::swapLE16(deltaLine[2]) + y; // Size of the Delta Line structure. srcImage += sizeof(s16) * 3; @@ -169,9 +170,9 @@ namespace TFE_DarkForces while (1) { const s16* deltaLine = (s16*)srcImage; - s16 sizeAndType = deltaLine[0]; - s16 xStart = deltaLine[1] + x; - s16 yStart = deltaLine[2] + y; + s16 sizeAndType = TFE_Endian::swapLE16(deltaLine[0]); + s16 xStart = TFE_Endian::swapLE16(deltaLine[1]) + x; + s16 yStart = TFE_Endian::swapLE16(deltaLine[2]) + y; srcImage += 3 * sizeof(s16); if (sizeAndType == 0) { break; } @@ -240,14 +241,14 @@ namespace TFE_DarkForces while (1) { const s16* deltaLine = (s16*)srcImage; - s16 sizeAndType = deltaLine[0]; + s16 sizeAndType = TFE_Endian::swapLE16(deltaLine[0]); if (sizeAndType == 0) { break; } - s16 xCur = w - deltaLine[1] + x; - s16 yStart = deltaLine[2] + y; + s16 xCur = w - TFE_Endian::swapLE16(deltaLine[1]) + x; + s16 yStart = TFE_Endian::swapLE16(deltaLine[2]) + y; // Size of the Delta Line structure. srcImage += sizeof(s16) * 3; @@ -305,9 +306,9 @@ namespace TFE_DarkForces while (1) { const s16* deltaLine = (s16*)srcImage; - s16 sizeAndType = deltaLine[0]; - s16 xStart = deltaLine[1] + x; - s16 yStart = deltaLine[2] + y; + s16 sizeAndType = TFE_Endian::swapLE16(deltaLine[0]); + s16 xStart = TFE_Endian::swapLE16(deltaLine[1]) + x; + s16 yStart = TFE_Endian::swapLE16(deltaLine[2]) + y; srcImage += 3 * sizeof(s16); if (sizeAndType == 0) { break; } @@ -316,7 +317,7 @@ namespace TFE_DarkForces s32 pixelCount = (sizeAndType >> 1) & 0x3fff; u8* dstImage = &framebuffer[yStart*stride]; - s16 xCur = w - deltaLine[1] + x; + s16 xCur = w - TFE_Endian::swapLE16(deltaLine[1]) + x; s16 yCur = yStart; JBool writeRow = (yCur >= clipRect.top && yCur < clipRect.bottom) ? JTRUE : JFALSE; diff --git a/TheForceEngine/TFE_DarkForces/Landru/lfont.cpp b/TheForceEngine/TFE_DarkForces/Landru/lfont.cpp index ab0350ae2..8477dc024 100644 --- a/TheForceEngine/TFE_DarkForces/Landru/lfont.cpp +++ b/TheForceEngine/TFE_DarkForces/Landru/lfont.cpp @@ -4,6 +4,7 @@ #include "lpalette.h" #include "lsystem.h" #include "ltimer.h" +#include #include #include #include @@ -160,6 +161,10 @@ namespace TFE_DarkForces file.read(&font->numChars); file.read(&font->width); file.read(&font->height); + font->firstChar = TFE_Endian::swapLE16(font->firstChar); + font->numChars = TFE_Endian::swapLE16(font->numChars); + font->width = TFE_Endian::swapLE16(font->width); + font->height = TFE_Endian::swapLE16(font->height); font->charSize = (font->width >> 3) * font->height; if (font->height > MAX_FONT_HEIGHT) @@ -171,6 +176,8 @@ namespace TFE_DarkForces file.read(&font->baseline); file.read(&font->isColor); + font->baseline = TFE_Endian::swapLE16(font->baseline); + font->isColor = TFE_Endian::swapLE16(font->isColor); font->widthArray = (u8*)landru_alloc(256); memset(font->widthArray, 0, 256); diff --git a/TheForceEngine/TFE_DarkForces/agent.cpp b/TheForceEngine/TFE_DarkForces/agent.cpp index 34fb26de5..c8b5bfe33 100644 --- a/TheForceEngine/TFE_DarkForces/agent.cpp +++ b/TheForceEngine/TFE_DarkForces/agent.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -434,16 +435,28 @@ namespace TFE_DarkForces { return JFALSE; } + saveData->agentData.selectedMission = TFE_Endian::swapLE32(saveData->agentData.selectedMission); + saveData->agentData.nextMission = TFE_Endian::swapLE32(saveData->agentData.nextMission); + for (s32 i = 0; i < 140; i++) + { + saveData->ammo[i] = TFE_Endian::swapLE32(saveData->ammo[i]); + } return JTRUE; } - JBool agent_writeAgentConfigData(FileStream* file, s32 agentId, const LevelSaveData* saveData) + JBool agent_writeAgentConfigData(FileStream* file, s32 agentId, LevelSaveData* saveData) { s32 fileOffset = agentId*sizeof(LevelSaveData) + s_headerSize; if (!file->seek(fileOffset)) { return JFALSE; } + saveData->agentData.selectedMission = TFE_Endian::swapLE32(saveData->agentData.selectedMission); + saveData->agentData.nextMission = TFE_Endian::swapLE32(saveData->agentData.nextMission); + for (s32 i = 0; i < 140; i++) + { + saveData->ammo[i] = TFE_Endian::swapLE32(saveData->ammo[i]); + } file->writeBuffer(saveData, sizeof(LevelSaveData)); return JTRUE; } diff --git a/TheForceEngine/TFE_DarkForces/agent.h b/TheForceEngine/TFE_DarkForces/agent.h index e1ef49ac0..128eee15f 100644 --- a/TheForceEngine/TFE_DarkForces/agent.h +++ b/TheForceEngine/TFE_DarkForces/agent.h @@ -54,7 +54,7 @@ namespace TFE_DarkForces void agent_levelEndTask(); JBool agent_readConfigData(FileStream* file, s32 agentId, LevelSaveData* saveData); - JBool agent_writeAgentConfigData(FileStream* file, s32 agentId, const LevelSaveData* saveData); + JBool agent_writeAgentConfigData(FileStream* file, s32 agentId, LevelSaveData* saveData); void agent_readSavedDataForLevel(s32 agentId, s32 levelIndex); void agent_saveLevelCompletion(u8 diff, s32 levelIndex); s32 agent_saveInventory(s32 agentId, s32 nextLevel); diff --git a/TheForceEngine/TFE_Jedi/Level/rtexture.cpp b/TheForceEngine/TFE_Jedi/Level/rtexture.cpp index 75bbc67b7..0b7f3711b 100644 --- a/TheForceEngine/TFE_Jedi/Level/rtexture.cpp +++ b/TheForceEngine/TFE_Jedi/Level/rtexture.cpp @@ -3,6 +3,7 @@ #include "rtexture.h" #include #include +#include #include #include #include @@ -60,21 +61,21 @@ namespace TFE_Jedi { s16 res = *((s16*)data); data += 2; - return res; + return TFE_Endian::swapLE16(res); } u16 readUShort(const u8*& data) { u16 res = *((u16*)data); data += 2; - return res; + return TFE_Endian::swapLE16(res); } s32 readInt(const u8*& data) { s32 res = *((s32*)data); data += 4; - return res; + return TFE_Endian::swapLE32(res); } void bitmap_setupAnimationTask() @@ -280,7 +281,7 @@ namespace TFE_Jedi u8* dst = texture->image; for (s32 i = 0; i < texture->width; i++, dst += texture->height) { - const u8* src = &inBuffer[columns[i]]; + const u8* src = &inBuffer[TFE_Endian::swapLE32(columns[i])]; decompressColumn_Type1(src, dst, texture->height); } } @@ -289,7 +290,7 @@ namespace TFE_Jedi u8* dst = texture->image; for (s32 i = 0; i < texture->width; i++, dst += texture->height) { - const u8* src = &inBuffer[columns[i]]; + const u8* src = &inBuffer[TFE_Endian::swapLE32(columns[i])]; decompressColumn_Type2(src, dst, texture->height); } } @@ -481,11 +482,15 @@ namespace TFE_Jedi const u8* base = tex->image + 2; for (s32 i = 0; i < anim->count; i++) { - const TextureData* frame = (TextureData*)(base + textureOffsets[i]); + const TextureData* frame = (TextureData*)(base + TFE_Endian::swapLE32(textureOffsets[i])); outFrames[i] = *frame; - + outFrames[i].width = TFE_Endian::swapLE16(outFrames[i].width); + outFrames[i].height = TFE_Endian::swapLE16(outFrames[i].height); + outFrames[i].uvWidth = TFE_Endian::swapLE16(outFrames[i].uvWidth); + outFrames[i].uvHeight = TFE_Endian::swapLE16(outFrames[i].uvHeight); + // Somehow this doesn't crash in DOS... - if (frame->width >= 16384 || frame->height >= 16384) + if (outFrames[i].width >= 16384 || outFrames[i].height >= 16384) { outFrames[i] = outFrames[0]; } diff --git a/TheForceEngine/TFE_System/endian.h b/TheForceEngine/TFE_System/endian.h new file mode 100644 index 000000000..acc48df71 --- /dev/null +++ b/TheForceEngine/TFE_System/endian.h @@ -0,0 +1,47 @@ +#pragma once + +#include "types.h" +#include + +namespace TFE_Endian +{ + inline u16 swapLE16(u16 x) + { + return SDL_SwapLE16(x); + } + + inline u32 swapLE32(u32 x) + { + return SDL_SwapLE32(x); + } + + inline u64 swap64LE(u64 x) + { + return SDL_SwapLE64(x); + } + + inline f32 swapFloatLE(f32 x) + { + return SDL_SwapFloatLE(x); + } + + inline u16 swapBE16(u16 x) + { + return SDL_SwapBE16(x); + } + + inline u32 swapBE32(u32 x) + { + return SDL_SwapBE32(x); + } + + inline u64 swap64BE(u64 x) + { + return SDL_SwapBE64(x); + } + + inline f32 swapFloatBE(f32 x) + { + return SDL_SwapFloatBE(x); + } +}