Skip to content

Commit

Permalink
Big endian fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BSzili committed Aug 2, 2023
1 parent c565624 commit f5d9c7c
Show file tree
Hide file tree
Showing 15 changed files with 282 additions and 92 deletions.
10 changes: 10 additions & 0 deletions TheForceEngine/TFE_Archive/gobArchive.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstring>

#include <TFE_System/system.h>
#include <TFE_System/endian.h>
#include "gobArchive.h"
#include <assert.h>
#include <algorithm>
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions TheForceEngine/TFE_Archive/labArchive.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstring>

#include <TFE_System/system.h>
#include <TFE_System/endian.h>
#include "labArchive.h"
#include <assert.h>
#include <algorithm>
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions TheForceEngine/TFE_Archive/lfdArchive.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <cstring>

#include <TFE_System/system.h>
#include <TFE_System/endian.h>
#include "lfdArchive.h"
#include <assert.h>
#include <algorithm>
Expand Down Expand Up @@ -39,13 +40,15 @@ 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];

s32 IX = sizeof(LFD_Entry_t) + root.LENGTH;
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 };
Expand Down
6 changes: 3 additions & 3 deletions TheForceEngine/TFE_Asset/modelAsset_jedi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
92 changes: 86 additions & 6 deletions TheForceEngine/TFE_Asset/spriteAsset_Jedi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "spriteAsset_Jedi.h"
#include <TFE_System/system.h>
#include <TFE_System/endian.h>
#include <TFE_FileSystem/filestream.h>
#include <TFE_FileSystem/paths.h>
#include <TFE_Asset/assetSystem.h>
Expand Down Expand Up @@ -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.
Expand All @@ -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
{
Expand Down Expand Up @@ -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])
Expand All @@ -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<u32> swappedAnims;
std::vector<u32> swappedViews;
std::vector<u32> swappedFrames;
std::vector<u32> 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);
Expand Down
18 changes: 13 additions & 5 deletions TheForceEngine/TFE_DarkForces/GameUI/delt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <cstring>

#include "delt.h"
#include <TFE_System/endian.h>
#include <TFE_Game/igame.h>
#include <TFE_FileSystem/paths.h>
#include <TFE_FileSystem/filestream.h>
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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++;

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit f5d9c7c

Please sign in to comment.