Converted FileHandler into a C header

This commit is contained in:
Tony Bark 2024-04-28 09:04:43 -04:00
parent ea46c7a037
commit d83abea13c
5 changed files with 212 additions and 169 deletions

View file

@ -8,6 +8,14 @@ Although an existing and established reimplementation project already exists, i
The problem is C is, well, C. ¯\\\_(ツ)\_/¯ Accessing NioTSO's code in safer languages like Go or Rust has been historically been difficult. While Zig is still in its infancy, it has proven to be perfectly capable of doing this without jumping through hoops. Making it well worth giving it a try. The problem is C is, well, C. ¯\\\_(ツ)\_/¯ Accessing NioTSO's code in safer languages like Go or Rust has been historically been difficult. While Zig is still in its infancy, it has proven to be perfectly capable of doing this without jumping through hoops. Making it well worth giving it a try.
Will this succeed? *I have no idea*. I'm not much of a game developer, but that hasn't stopped me from dreaming.
## To do
- [ ] Rewrite header files
- [ ] Write Vitaboy renderer in Zig
## Requirements ## Requirements
- The Sims Online - The Sims Online

View file

@ -26,62 +26,58 @@
#include <string.h> #include <string.h>
#ifdef WIN32 #ifdef WIN32
#define fhexport __declspec(dllexport) #define fhexport __declspec(dllexport);
#else #else
#define fhexport __attribute__((visibility ("default"))) #define fhexport __attribute__((visibility("default")));
#endif #endif
struct Asset_t { #ifdef __cplusplus
uint32_t Group; extern "C"
uint32_t File; {
uint32_t Type; #endif
};
enum FErr { struct Asset_t
FERR_NOT_FOUND, {
FERR_OPEN, uint32_t Group;
FERR_BLANK, uint32_t File;
FERR_MEMORY, uint32_t Type;
FERR_READ, };
FERR_UNRECOGNIZED,
FERR_INVALIDDATA
};
enum ImageFormat_t { enum FErr
FIMG_BGR24, {
FIMG_BGRA32 FERR_NOT_FOUND,
}; FERR_OPEN,
FERR_BLANK,
FERR_MEMORY,
FERR_READ,
FERR_UNRECOGNIZED,
FERR_INVALIDDATA
};
struct Image_t { enum ImageFormat_t
unsigned Width, Height; {
ImageFormat_t Format; FIMG_BGR24,
uint8_t * Data; FIMG_BGRA32
}; };
struct Sound_t { struct Image_t
unsigned Channels; {
unsigned SamplingRate; unsigned Width, Height;
unsigned BitDepth; ImageFormat_t Format;
unsigned Duration; uint8_t *Data;
uint8_t * Data; };
};
namespace File { struct Sound_t
{
unsigned Channels;
unsigned SamplingRate;
unsigned BitDepth;
unsigned Duration;
uint8_t *Data;
};
inline size_t GetFileSize(FILE * hFile){ #ifdef __cplusplus
fseek(hFile, 0, SEEK_END); } // extern "C"
size_t FileSize = ftell(hFile); #endif
fseek(hFile, 0, SEEK_SET);
return FileSize;
}
fhexport extern int Error;
fhexport extern size_t FileSize;
fhexport uint8_t * ReadFile(const char * Filename);
fhexport Image_t * ReadImageFile(const char * Filename);
fhexport Sound_t * ReadSoundFile(const char * Filename);
}
#endif #endif

View file

@ -23,81 +23,98 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <FileHandler.hpp> #include <filehandler.h>
/**** /****
** Bytestream ** Bytestream
*/ */
class VBFile_t { class VBFile_t
private: {
private:
const uint8_t *Buffer, *Position; const uint8_t *Buffer, *Position;
unsigned Size; unsigned Size;
public: public:
inline void set(const void *_Buffer, unsigned _Size){ inline void set(const void *_Buffer, unsigned _Size)
Buffer = (const uint8_t*) _Buffer; {
Position = (const uint8_t*) _Buffer; Buffer = (const uint8_t *)_Buffer;
Position = (const uint8_t *)_Buffer;
Size = _Size; Size = _Size;
} }
inline unsigned getpos(){ inline unsigned getpos()
return Position-Buffer; {
return Position - Buffer;
} }
inline void seekto(unsigned offset){ inline void seekto(unsigned offset)
Position = Buffer+offset; {
Position = Buffer + offset;
} }
inline void seekahead(unsigned count){ inline void seekahead(unsigned count)
{
Position += count; Position += count;
} }
inline void seekback(unsigned count){ inline void seekback(unsigned count)
{
Position -= count; Position -= count;
} }
inline uint32_t readint32(){ inline uint32_t readint32()
uint32_t value = (uint32_t)((Position[0]<<(8*3)) | (Position[1]<<(8*2)) | (Position[2]<<(8*1)) | (Position[3]<<(8*0))); {
uint32_t value = (uint32_t)((Position[0] << (8 * 3)) | (Position[1] << (8 * 2)) | (Position[2] << (8 * 1)) | (Position[3] << (8 * 0)));
Position += 4; Position += 4;
return value; return value;
} }
inline uint32_t readint16(){ inline uint32_t readint16()
uint16_t value = (uint16_t)((Position[0]<<(8*1)) | (Position[1]<<(8*0))); {
uint16_t value = (uint16_t)((Position[0] << (8 * 1)) | (Position[1] << (8 * 0)));
Position += 2; Position += 2;
return value; return value;
} }
inline uint32_t readint8(){ inline uint32_t readint8()
uint8_t value = (uint8_t)((Position[0]<<(8*0))); {
uint8_t value = (uint8_t)((Position[0] << (8 * 0)));
Position += 1; Position += 1;
return value; return value;
} }
inline float readfloat(){ inline float readfloat()
union { uint32_t i; float f; } value; {
value.i = (uint32_t)((Position[0]<<(8*0)) | (Position[1]<<(8*1)) | (Position[2]<<(8*2)) | (Position[3]<<(8*3))); union
{
uint32_t i;
float f;
} value;
value.i = (uint32_t)((Position[0] << (8 * 0)) | (Position[1] << (8 * 1)) | (Position[2] << (8 * 2)) | (Position[3] << (8 * 3)));
Position += 4; Position += 4;
return value.f; return value.f;
} }
inline void readbytes(void* Destination, unsigned length){ inline void readbytes(void *Destination, unsigned length)
{
memcpy(Destination, Position, length); memcpy(Destination, Position, length);
Position += length; Position += length;
} }
inline char* readstring(){ inline char *readstring()
//Read a Pascal string with 1 length byte {
// Read a Pascal string with 1 length byte
unsigned length = readint8(); unsigned length = readint8();
char *string = (char*) malloc(length+1); char *string = (char *)malloc(length + 1);
readbytes(string, length); readbytes(string, length);
string[length] = '\0'; string[length] = '\0';
return string; return string;
} }
inline char* readstring2(){ inline char *readstring2()
//Read a Pascal string with 2 length bytes {
// Read a Pascal string with 2 length bytes
unsigned length = readint16(); unsigned length = readint16();
char *string = (char*) malloc(length+1); char *string = (char *)malloc(length + 1);
readbytes(string, length); readbytes(string, length);
string[length] = '\0'; string[length] = '\0';
return string; return string;
@ -110,83 +127,92 @@ extern VBFile_t VBFile;
** Common ** Common
*/ */
enum ReadGroup { enum ReadGroup
{
NOGROUP, NOGROUP,
READGROUP READGROUP
}; };
struct Translation_t { struct Translation_t
{
float x, y, z; float x, y, z;
}; };
struct Rotation_t { struct Rotation_t
{
float x, y, z, w; float x, y, z, w;
}; };
struct KeyValuePair_t { struct KeyValuePair_t
char * Key; {
char * Value; char *Key;
char *Value;
}; };
struct Prop_t { struct Prop_t
{
uint32_t EntriesCount; uint32_t EntriesCount;
KeyValuePair_t * Entries; KeyValuePair_t *Entries;
}; };
struct PropsList_t { struct PropsList_t
{
uint32_t PropsCount; uint32_t PropsCount;
Prop_t * Props; Prop_t *Props;
}; };
void ReadAsset(Asset_t& Asset, bool ReadGroup); void ReadAsset(Asset_t &Asset, bool ReadGroup);
void ReadPropEntry(KeyValuePair_t& Entry); void ReadPropEntry(KeyValuePair_t &Entry);
void ReadPropEntries(Prop_t& Prop); void ReadPropEntries(Prop_t &Prop);
void ReadPropsList(PropsList_t& PropsList); void ReadPropsList(PropsList_t &PropsList);
float DotProduct(Rotation_t * q1, Rotation_t * q2); float DotProduct(Rotation_t *q1, Rotation_t *q2);
void Normalize(Rotation_t * q); void Normalize(Rotation_t *q);
void CombineQuaternions(Rotation_t * Destination, Rotation_t * Source); void CombineQuaternions(Rotation_t *Destination, Rotation_t *Source);
void FindQuaternionMatrix(float * Matrix, Rotation_t * Quaternion); void FindQuaternionMatrix(float *Matrix, Rotation_t *Quaternion);
/**** /****
** Animation (*.anim) ** Animation (*.anim)
*/ */
struct TimeProp_t { struct TimeProp_t
{
uint32_t ID; uint32_t ID;
PropsList_t PropsList; PropsList_t PropsList;
}; };
struct TimePropsList_t { struct TimePropsList_t
{
uint32_t TimePropsCount; uint32_t TimePropsCount;
TimeProp_t * TimeProps; TimeProp_t *TimeProps;
}; };
struct Motion_t { struct Motion_t
{
uint32_t Unknown; uint32_t Unknown;
char * BoneName; char *BoneName;
uint32_t FrameCount; uint32_t FrameCount;
float Duration; //Converted to seconds float Duration; // Converted to seconds
uint8_t HasTranslation; uint8_t HasTranslation;
uint8_t HasRotation; uint8_t HasRotation;
uint32_t FirstTranslation; uint32_t FirstTranslation;
uint32_t FirstRotation; uint32_t FirstRotation;
Translation_t * Translations; Translation_t *Translations;
Rotation_t * Rotations; Rotation_t *Rotations;
uint8_t HasPropsLists; uint8_t HasPropsLists;
uint32_t PropsListsCount; uint32_t PropsListsCount;
PropsList_t * PropsLists; PropsList_t *PropsLists;
uint8_t HasTimePropsLists; uint8_t HasTimePropsLists;
uint32_t TimePropsListsCount; uint32_t TimePropsListsCount;
TimePropsList_t * TimePropsLists; TimePropsList_t *TimePropsLists;
}; };
struct Animation_t { struct Animation_t
{
uint32_t Version; uint32_t Version;
char * Name; char *Name;
float Duration; //Converted to seconds float Duration; // Converted to seconds
float Distance; float Distance;
uint8_t IsMoving; uint8_t IsMoving;
uint32_t TranslationsCount; uint32_t TranslationsCount;
@ -196,101 +222,107 @@ struct Animation_t {
unsigned TranslationsOffset; unsigned TranslationsOffset;
unsigned RotationsOffset; unsigned RotationsOffset;
Motion_t * Motions; Motion_t *Motions;
}; };
void ReadAnimation(Animation_t& Animation); void ReadAnimation(Animation_t &Animation);
void ReadMotion(Animation_t& Animation, Motion_t& Motion); void ReadMotion(Animation_t &Animation, Motion_t &Motion);
void ReadPropsLists(Motion_t& Motion); void ReadPropsLists(Motion_t &Motion);
void ReadTimePropsList(TimePropsList_t& TimePropsList); void ReadTimePropsList(TimePropsList_t &TimePropsList);
void ReadTimePropsLists(Motion_t& Motion); void ReadTimePropsLists(Motion_t &Motion);
/**** /****
** Appearance (*.apr) ** Appearance (*.apr)
*/ */
struct Appearance_t { struct Appearance_t
{
uint32_t Version; uint32_t Version;
Asset_t Thumbnail; Asset_t Thumbnail;
uint32_t BindingCount; uint32_t BindingCount;
Asset_t * Bindings; Asset_t *Bindings;
}; };
void ReadAppearance(Appearance_t& Appearance); void ReadAppearance(Appearance_t &Appearance);
/**** /****
** Binding (*.bnd) ** Binding (*.bnd)
*/ */
struct Binding_t { struct Binding_t
{
uint32_t Version; uint32_t Version;
char * BoneName; char *BoneName;
uint32_t MeshDef; uint32_t MeshDef;
Asset_t Mesh; Asset_t Mesh;
uint32_t AppearanceDef; uint32_t AppearanceDef;
Asset_t Appearance; Asset_t Appearance;
}; };
void ReadBinding(Binding_t& Binding); void ReadBinding(Binding_t &Binding);
/**** /****
** Collection (*.col) ** Collection (*.col)
*/ */
struct PODef_t { struct PODef_t
{
uint32_t Index; uint32_t Index;
Asset_t PO; Asset_t PO;
}; };
struct Collection_t { struct Collection_t
{
uint32_t POCount; uint32_t POCount;
PODef_t * PurchasableOutfits; PODef_t *PurchasableOutfits;
}; };
void ReadCollection(Collection_t& Collection); void ReadCollection(Collection_t &Collection);
/**** /****
** Hand Group (*.hag) ** Hand Group (*.hag)
*/ */
struct HandGroup_t { struct HandGroup_t
{
uint32_t Version; uint32_t Version;
Asset_t HandAppearances[18]; Asset_t HandAppearances[18];
}; };
void ReadHandGroup(HandGroup_t& HandGroup); void ReadHandGroup(HandGroup_t &HandGroup);
/**** /****
** Mesh (*.mesh) ** Mesh (*.mesh)
*/ */
struct TextureVertex_t { struct TextureVertex_t
{
float u, v; float u, v;
}; };
struct Coord_t { struct Coord_t
{
float x, y, z; float x, y, z;
}; };
struct TextureCoord_t { struct TextureCoord_t
{
float u, v; float u, v;
}; };
struct NormalCoord_t { struct NormalCoord_t
{
float x, y, z; float x, y, z;
}; };
struct BlendData_t { struct BlendData_t
{
float Weight; float Weight;
unsigned OtherVertex; unsigned OtherVertex;
}; };
struct Vertex_t { struct Vertex_t
{
Coord_t Coord; Coord_t Coord;
TextureCoord_t TextureCoord; TextureCoord_t TextureCoord;
NormalCoord_t NormalCoord; NormalCoord_t NormalCoord;
@ -299,11 +331,13 @@ struct Vertex_t {
BlendData_t BlendData; BlendData_t BlendData;
}; };
struct Face_t { struct Face_t
{
unsigned VertexA, VertexB, VertexC; unsigned VertexA, VertexB, VertexC;
}; };
struct BoneBinding_t { struct BoneBinding_t
{
unsigned BoneIndex; unsigned BoneIndex;
unsigned FirstRealVertex; unsigned FirstRealVertex;
unsigned RealVertexCount; unsigned RealVertexCount;
@ -311,40 +345,43 @@ struct BoneBinding_t {
unsigned BlendVertexCount; unsigned BlendVertexCount;
}; };
struct Mesh_t { struct Mesh_t
{
uint32_t Version; uint32_t Version;
uint32_t BoneCount; uint32_t BoneCount;
char ** BoneNames; char **BoneNames;
uint32_t FaceCount; uint32_t FaceCount;
Face_t * FaceData; Face_t *FaceData;
uint32_t BindingCount; uint32_t BindingCount;
BoneBinding_t * BoneBindings; BoneBinding_t *BoneBindings;
uint32_t RealVertexCount; uint32_t RealVertexCount;
uint32_t BlendVertexCount; uint32_t BlendVertexCount;
uint32_t TotalVertexCount; uint32_t TotalVertexCount;
Vertex_t * VertexData; Vertex_t *VertexData;
Vertex_t * TransformedVertexData; Vertex_t *TransformedVertexData;
}; };
void ReadMesh(Mesh_t& Mesh); void ReadMesh(Mesh_t &Mesh);
/**** /****
** Outfit (*.oft) ** Outfit (*.oft)
*/ */
enum OutfitColor { enum OutfitColor
{
OutfitColor_Light, OutfitColor_Light,
OutfitColor_Medium, OutfitColor_Medium,
OutfitColor_Dark OutfitColor_Dark
}; };
enum OutfitRegion { enum OutfitRegion
{
OutfitRegion_Head = 0, OutfitRegion_Head = 0,
OutfitRegion_Body = 18 OutfitRegion_Body = 18
}; };
struct Outfit_t { struct Outfit_t
{
uint32_t Version; uint32_t Version;
uint32_t Unknown; uint32_t Unknown;
Asset_t Appearance[3]; Asset_t Appearance[3];
@ -352,14 +389,14 @@ struct Outfit_t {
uint32_t Region; uint32_t Region;
}; };
void ReadOutfit(Outfit_t& Outfit); void ReadOutfit(Outfit_t &Outfit);
/**** /****
** Purchasable Outfit (*.po) ** Purchasable Outfit (*.po)
*/ */
struct PurchasableOutfit_t { struct PurchasableOutfit_t
{
uint32_t Version; uint32_t Version;
uint32_t Unknown; uint32_t Unknown;
uint32_t OutfitDef; uint32_t OutfitDef;
@ -368,17 +405,17 @@ struct PurchasableOutfit_t {
Asset_t Collection; Asset_t Collection;
}; };
void ReadPurchasableOutfit(PurchasableOutfit_t& PurchasableOutfit); void ReadPurchasableOutfit(PurchasableOutfit_t &PurchasableOutfit);
/**** /****
** Skeleton (*.skel) ** Skeleton (*.skel)
*/ */
struct Bone_t { struct Bone_t
{
uint32_t Unknown; uint32_t Unknown;
char * Name; char *Name;
char * ParentsName; char *ParentsName;
uint8_t HasProps; uint8_t HasProps;
PropsList_t PropsList; PropsList_t PropsList;
Translation_t Translation; Translation_t Translation;
@ -390,18 +427,19 @@ struct Bone_t {
float WigglePower; float WigglePower;
unsigned ChildrenCount; unsigned ChildrenCount;
Bone_t ** Children; Bone_t **Children;
}; };
struct Skeleton_t { struct Skeleton_t
{
uint32_t Version; uint32_t Version;
char * Name; char *Name;
uint16_t BoneCount; uint16_t BoneCount;
Bone_t * Bones; Bone_t *Bones;
}; };
void ReadSkeleton(Skeleton_t& Bone); void ReadSkeleton(Skeleton_t &Bone);
void ReadBone(Skeleton_t& Skeleton, Bone_t& Bone, unsigned Index); void ReadBone(Skeleton_t &Skeleton, Bone_t &Bone, unsigned Index);
unsigned FindBone(Skeleton_t& Skeleton, const char * BoneName, unsigned Count); unsigned FindBone(Skeleton_t &Skeleton, const char *BoneName, unsigned Count);
#endif #endif

View file

@ -1,7 +1,4 @@
const std = @import("std"); const std = @import("std");
const iff = @cImport({
@cInclude("./iff/iff.h");
});
pub fn main() !void { pub fn main() !void {

4
src/vitaboy.zig Normal file
View file

@ -0,0 +1,4 @@
const std = @import("std");
// const iff = @cImport({
// @cInclude("./filehandler.h");
// });