Added experimental PS2 target. Implemented a Stream class.

This commit is contained in:
Angelo Papenhoff 2014-12-27 23:18:10 +01:00
parent 9dd859c4d1
commit fd017a69ef
14 changed files with 345 additions and 224 deletions

29
Makefile.ps2 Normal file
View File

@ -0,0 +1,29 @@
CXX=ee-g++
# null, opengl
BUILD=ps2
# e.g. null -> -DRW_NULL
BUILDDEF:=$(shell echo $(BUILD) | tr a-z A-Z | sed 's/^/-DRW_/')
BUILDDIR=build-$(BUILD)
SRCDIR=src
SRC := $(patsubst %.cpp,$(SRCDIR)/%.cpp, rwbase.cpp clump.cpp\
geometry.cpp plugins.cpp ps2.cpp\
ogl.cpp image.cpp)
OBJ := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$(SRC))
DEP := $(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.d,$(SRC))
CFLAGS=-Wall -g $(BUILDDEF) #-Wno-parentheses #-Wconversion
LIB=librw-$(BUILD).a
$(LIB): $(OBJ)
ar scr $@ $(OBJ)
$(BUILDDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CFLAGS) -c $< -o $@
$(BUILDDIR)/%.d: $(SRCDIR)/%.cpp
$(CXX) -MM -MT '$(patsubst $(SRCDIR)/%.cpp,$(BUILDDIR)/%.o,$<)' $(CFLAGS) $< > $@
clean:
rm -f $(BUILDDIR)/*.[od]
-include $(DEP)

View File

@ -3,9 +3,10 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> //#include <iostream>
#include <fstream> //#include <fstream>
#include <list> //#include <list>
#include <new>
#include "rw.h" #include "rw.h"
@ -23,9 +24,11 @@ main(int argc, char *argv[])
registerMeshPlugin(); registerMeshPlugin();
Rw::Clump *c; Rw::Clump *c;
ifstream in(argv[1], ios::binary); // ifstream in(argv[1], ios::binary);
Rw::FindChunk(in, Rw::ID_CLUMP, NULL, NULL); Rw::StreamFile in;
c = Rw::Clump::streamRead(in); in.open(argv[1], "rb");
Rw::FindChunk(&in, Rw::ID_CLUMP, NULL, NULL);
c = Rw::Clump::streamRead(&in);
assert(c != NULL); assert(c != NULL);
in.close(); in.close();
@ -36,8 +39,10 @@ main(int argc, char *argv[])
// for(Rw::int32 i = 0; i < c->numAtomics; i++) // for(Rw::int32 i = 0; i < c->numAtomics; i++)
// Rw::Gl::Instance(c->atomicList[i]); // Rw::Gl::Instance(c->atomicList[i]);
ofstream out(argv[2], ios::binary); // ofstream out(argv[2], ios::binary);
c->streamWrite(out); Rw::StreamFile out;
out.open(argv[2], "wb");
c->streamWrite(&out);
out.close(); out.close();
delete c; delete c;

View File

@ -3,8 +3,7 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> #include <new>
#include <fstream>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
@ -145,14 +144,15 @@ Clump::~Clump(void)
} }
Clump* Clump*
Clump::streamRead(istream &stream) Clump::streamRead(Stream *stream)
{ {
uint32 length, version; uint32 length, version;
int32 buf[3]; int32 buf[3];
Clump *clump; Clump *clump;
printf("- before chunk: %X\n", stream->tell());
assert(FindChunk(stream, ID_STRUCT, &length, &version)); assert(FindChunk(stream, ID_STRUCT, &length, &version));
clump = new Clump; clump = new Clump;
stream.read((char*)buf, length); stream->read(buf, length);
clump->numAtomics = buf[0]; clump->numAtomics = buf[0];
clump->numLights = 0; clump->numLights = 0;
clump->numCameras = 0; clump->numCameras = 0;
@ -161,17 +161,19 @@ Clump::streamRead(istream &stream)
clump->numCameras = buf[2]; clump->numCameras = buf[2];
} }
printf("- frame list: %X\n", stream->tell());
// Frame list // Frame list
Frame **frameList; Frame **frameList;
int32 numFrames; int32 numFrames;
clump->frameListStreamRead(stream, &frameList, &numFrames); clump->frameListStreamRead(stream, &frameList, &numFrames);
clump->parent = (void*)frameList[0]; clump->parent = (void*)frameList[0];
printf("- geometry list: %X\n", stream->tell());
// Geometry list // Geometry list
int32 numGeometries = 0; int32 numGeometries = 0;
assert(FindChunk(stream, ID_GEOMETRYLIST, NULL, NULL)); assert(FindChunk(stream, ID_GEOMETRYLIST, NULL, NULL));
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
numGeometries = readInt32(stream); numGeometries = stream->readI32();
Geometry **geometryList = 0; Geometry **geometryList = 0;
if(numGeometries) if(numGeometries)
geometryList = new Geometry*[numGeometries]; geometryList = new Geometry*[numGeometries];
@ -180,6 +182,7 @@ Clump::streamRead(istream &stream)
geometryList[i] = Geometry::streamRead(stream); geometryList[i] = Geometry::streamRead(stream);
} }
printf("- atomics: %X\n", stream->tell());
// Atomics // Atomics
if(clump->numAtomics) if(clump->numAtomics)
clump->atomicList = new Atomic*[clump->numAtomics]; clump->atomicList = new Atomic*[clump->numAtomics];
@ -196,7 +199,7 @@ Clump::streamRead(istream &stream)
for(int32 i = 0; i < clump->numLights; i++){ for(int32 i = 0; i < clump->numLights; i++){
int32 frm; int32 frm;
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
frm = readInt32(stream); frm = stream->readI32();
assert(FindChunk(stream, ID_LIGHT, NULL, NULL)); assert(FindChunk(stream, ID_LIGHT, NULL, NULL));
clump->lightList[i] = Light::streamRead(stream); clump->lightList[i] = Light::streamRead(stream);
clump->lightList[i]->frame = frameList[frm]; clump->lightList[i]->frame = frameList[frm];
@ -210,14 +213,14 @@ Clump::streamRead(istream &stream)
} }
bool bool
Clump::streamWrite(ostream &stream) Clump::streamWrite(Stream *stream)
{ {
int size = this->streamGetSize(); int size = this->streamGetSize();
WriteChunkHeader(stream, ID_CLUMP, size); WriteChunkHeader(stream, ID_CLUMP, size);
int buf[3] = { this->numAtomics, this->numLights, this->numCameras }; int buf[3] = { this->numAtomics, this->numLights, this->numCameras };
size = Version >= 0x33000 ? 12 : 4; size = Version >= 0x33000 ? 12 : 4;
WriteChunkHeader(stream, ID_STRUCT, size); WriteChunkHeader(stream, ID_STRUCT, size);
stream.write((char*)buf, size); stream->write(buf, size);
int32 numFrames = ((Frame*)this->parent)->count(); int32 numFrames = ((Frame*)this->parent)->count();
Frame **flist = new Frame*[numFrames]; Frame **flist = new Frame*[numFrames];
@ -230,7 +233,7 @@ Clump::streamWrite(ostream &stream)
size += 12 + this->atomicList[i]->geometry->streamGetSize(); size += 12 + this->atomicList[i]->geometry->streamGetSize();
WriteChunkHeader(stream, ID_GEOMETRYLIST, size); WriteChunkHeader(stream, ID_GEOMETRYLIST, size);
WriteChunkHeader(stream, ID_STRUCT, 4); WriteChunkHeader(stream, ID_STRUCT, 4);
writeInt32(this->numAtomics, stream); // same as numGeometries stream->writeI32(this->numAtomics); // same as numGeometries
for(int32 i = 0; i < this->numAtomics; i++) for(int32 i = 0; i < this->numAtomics; i++)
this->atomicList[i]->geometry->streamWrite(stream); this->atomicList[i]->geometry->streamWrite(stream);
@ -243,7 +246,7 @@ Clump::streamWrite(ostream &stream)
if(frm < 0) if(frm < 0)
return false; return false;
WriteChunkHeader(stream, ID_STRUCT, 4); WriteChunkHeader(stream, ID_STRUCT, 4);
writeInt32(frm, stream); stream->writeI32(frm);
l->streamWrite(stream); l->streamWrite(stream);
} }
@ -294,18 +297,18 @@ Clump::streamGetSize(void)
void void
Clump::frameListStreamRead(istream &stream, Frame ***flp, int32 *nf) Clump::frameListStreamRead(Stream *stream, Frame ***flp, int32 *nf)
{ {
FrameStreamData buf; FrameStreamData buf;
int32 numFrames = 0; int32 numFrames = 0;
assert(FindChunk(stream, ID_FRAMELIST, NULL, NULL)); assert(FindChunk(stream, ID_FRAMELIST, NULL, NULL));
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
numFrames = readInt32(stream); numFrames = stream->readI32();
Frame **frameList = new Frame*[numFrames]; Frame **frameList = new Frame*[numFrames];
for(int32 i = 0; i < numFrames; i++){ for(int32 i = 0; i < numFrames; i++){
Frame *f; Frame *f;
frameList[i] = f = new Frame; frameList[i] = f = new Frame;
stream.read((char*)&buf, sizeof(buf)); stream->read(&buf, sizeof(buf));
f->matrix[0] = buf.mat[0]; f->matrix[0] = buf.mat[0];
f->matrix[1] = buf.mat[1]; f->matrix[1] = buf.mat[1];
f->matrix[2] = buf.mat[2]; f->matrix[2] = buf.mat[2];
@ -333,7 +336,7 @@ Clump::frameListStreamRead(istream &stream, Frame ***flp, int32 *nf)
} }
void void
Clump::frameListStreamWrite(ostream &stream, Frame **frameList, int32 numFrames) Clump::frameListStreamWrite(Stream *stream, Frame **frameList, int32 numFrames)
{ {
FrameStreamData buf; FrameStreamData buf;
@ -345,7 +348,7 @@ Clump::frameListStreamWrite(ostream &stream, Frame **frameList, int32 numFrames)
WriteChunkHeader(stream, ID_FRAMELIST, size); WriteChunkHeader(stream, ID_FRAMELIST, size);
WriteChunkHeader(stream, ID_STRUCT, structsize); WriteChunkHeader(stream, ID_STRUCT, structsize);
writeUInt32(numFrames, stream); stream->writeU32(numFrames);
for(int32 i = 0; i < numFrames; i++){ for(int32 i = 0; i < numFrames; i++){
Frame *f = frameList[i]; Frame *f = frameList[i];
buf.mat[0] = f->matrix[0]; buf.mat[0] = f->matrix[0];
@ -362,7 +365,7 @@ Clump::frameListStreamWrite(ostream &stream, Frame **frameList, int32 numFrames)
buf.pos[2] = f->matrix[14]; buf.pos[2] = f->matrix[14];
buf.parent = findPointer((void*)f, (void**)frameList,numFrames); buf.parent = findPointer((void*)f, (void**)frameList,numFrames);
buf.matflag = f->matflag; buf.matflag = f->matflag;
stream.write((char*)&buf, sizeof(buf)); stream->write(&buf, sizeof(buf));
} }
for(int32 i = 0; i < numFrames; i++) for(int32 i = 0; i < numFrames; i++)
frameList[i]->streamWritePlugins(stream); frameList[i]->streamWritePlugins(stream);
@ -389,12 +392,12 @@ Atomic::~Atomic(void)
} }
Atomic* Atomic*
Atomic::streamReadClump(istream &stream, Atomic::streamReadClump(Stream *stream,
Frame **frameList, Geometry **geometryList) Frame **frameList, Geometry **geometryList)
{ {
int32 buf[4]; int32 buf[4];
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
stream.read((char*)buf, 16); stream->read(buf, 16);
Atomic *atomic = new Atomic; Atomic *atomic = new Atomic;
atomic->frame = frameList[buf[0]]; atomic->frame = frameList[buf[0]];
atomic->geometry = geometryList[buf[1]]; atomic->geometry = geometryList[buf[1]];
@ -403,7 +406,7 @@ Atomic::streamReadClump(istream &stream,
} }
bool bool
Atomic::streamWriteClump(ostream &stream, Frame **frameList, int32 numFrames) Atomic::streamWriteClump(Stream *stream, Frame **frameList, int32 numFrames)
{ {
int32 buf[4] = { 0, 0, 5, 0 }; int32 buf[4] = { 0, 0, 5, 0 };
Clump *c = this->clump; Clump *c = this->clump;
@ -420,7 +423,7 @@ Atomic::streamWriteClump(ostream &stream, Frame **frameList, int32 numFrames)
return false; return false;
foundgeo: foundgeo:
stream.write((char*)buf, sizeof(buf)); stream->write(buf, sizeof(buf));
this->streamWritePlugins(stream); this->streamWritePlugins(stream);
return true; return true;
} }
@ -459,12 +462,12 @@ struct LightChunkData
}; };
Light* Light*
Light::streamRead(istream &stream) Light::streamRead(Stream *stream)
{ {
LightChunkData buf; LightChunkData buf;
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
Light *light = new Light; Light *light = new Light;
stream.read((char*)&buf, sizeof(LightChunkData)); stream->read(&buf, sizeof(LightChunkData));
light->radius = buf.radius; light->radius = buf.radius;
light->color[0] = buf.red; light->color[0] = buf.red;
light->color[1] = buf.green; light->color[1] = buf.green;
@ -479,7 +482,7 @@ Light::streamRead(istream &stream)
} }
bool bool
Light::streamWrite(ostream &stream) Light::streamWrite(Stream *stream)
{ {
LightChunkData buf; LightChunkData buf;
WriteChunkHeader(stream, ID_LIGHT, this->streamGetSize()); WriteChunkHeader(stream, ID_LIGHT, this->streamGetSize());
@ -491,7 +494,7 @@ Light::streamWrite(ostream &stream)
buf.minusCosAngle = this->minusCosAngle; buf.minusCosAngle = this->minusCosAngle;
buf.flags = this->flags; buf.flags = this->flags;
buf.type = this->subType; buf.type = this->subType;
stream.write((char*)&buf, sizeof(LightChunkData)); stream->write(&buf, sizeof(LightChunkData));
this->streamWritePlugins(stream); this->streamWritePlugins(stream);
return true; return true;

View File

@ -3,8 +3,7 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> #include <new>
#include <fstream>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
@ -99,44 +98,44 @@ struct GeoStreamData
}; };
Geometry* Geometry*
Geometry::streamRead(istream &stream) Geometry::streamRead(Stream *stream)
{ {
uint32 version; uint32 version;
GeoStreamData buf; GeoStreamData buf;
assert(FindChunk(stream, ID_STRUCT, NULL, &version)); assert(FindChunk(stream, ID_STRUCT, NULL, &version));
stream.read((char*)&buf, sizeof(buf)); stream->read(&buf, sizeof(buf));
Geometry *geo = new Geometry(buf.numVertices, Geometry *geo = new Geometry(buf.numVertices,
buf.numTriangles, buf.flags); buf.numTriangles, buf.flags);
geo->addMorphTargets(buf.numMorphTargets-1); geo->addMorphTargets(buf.numMorphTargets-1);
// skip surface properties // skip surface properties
if(version < 0x34000) if(version < 0x34000)
stream.seekg(12, ios::cur); stream->seek(12);
if(!(geo->geoflags & NATIVE)){ if(!(geo->geoflags & NATIVE)){
if(geo->geoflags & PRELIT) if(geo->geoflags & PRELIT)
stream.read((char*)geo->colors, 4*geo->numVertices); stream->read(geo->colors, 4*geo->numVertices);
for(int32 i = 0; i < geo->numTexCoordSets; i++) for(int32 i = 0; i < geo->numTexCoordSets; i++)
stream.read((char*)geo->texCoords[i], stream->read(geo->texCoords[i],
2*geo->numVertices*4); 2*geo->numVertices*4);
stream.read((char*)geo->triangles, 4*geo->numTriangles*2); stream->read(geo->triangles, 4*geo->numTriangles*2);
} }
for(int32 i = 0; i < geo->numMorphTargets; i++){ for(int32 i = 0; i < geo->numMorphTargets; i++){
MorphTarget *m = &geo->morphTargets[i]; MorphTarget *m = &geo->morphTargets[i];
stream.read((char*)m->boundingSphere, 4*4); stream->read(m->boundingSphere, 4*4);
int32 hasVertices = readInt32(stream); int32 hasVertices = stream->readI32();
int32 hasNormals = readInt32(stream); int32 hasNormals = stream->readI32();
if(hasVertices) if(hasVertices)
stream.read((char*)m->vertices, 3*geo->numVertices*4); stream->read(m->vertices, 3*geo->numVertices*4);
if(hasNormals) if(hasNormals)
stream.read((char*)m->normals, 3*geo->numVertices*4); stream->read(m->normals, 3*geo->numVertices*4);
} }
assert(FindChunk(stream, ID_MATLIST, NULL, NULL)); assert(FindChunk(stream, ID_MATLIST, NULL, NULL));
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
geo->numMaterials = readInt32(stream); geo->numMaterials = stream->readI32();
geo->materialList = new Material*[geo->numMaterials]; geo->materialList = new Material*[geo->numMaterials];
stream.seekg(geo->numMaterials*4, ios::cur); // unused (-1) stream->seek(geo->numMaterials*4); // unused (-1)
for(int32 i = 0; i < geo->numMaterials; i++){ for(int32 i = 0; i < geo->numMaterials; i++){
assert(FindChunk(stream, ID_MATERIAL, NULL, NULL)); assert(FindChunk(stream, ID_MATERIAL, NULL, NULL));
geo->materialList[i] = Material::streamRead(stream); geo->materialList[i] = Material::streamRead(stream);
@ -175,7 +174,7 @@ geoStructSize(Geometry *geo)
} }
bool bool
Geometry::streamWrite(ostream &stream) Geometry::streamWrite(Stream *stream)
{ {
GeoStreamData buf; GeoStreamData buf;
uint32 size; uint32 size;
@ -188,34 +187,34 @@ Geometry::streamWrite(ostream &stream)
buf.numTriangles = this->numTriangles; buf.numTriangles = this->numTriangles;
buf.numVertices = this->numVertices; buf.numVertices = this->numVertices;
buf.numMorphTargets = this->numMorphTargets; buf.numMorphTargets = this->numMorphTargets;
stream.write((char*)&buf, sizeof(buf)); stream->write(&buf, sizeof(buf));
if(Version < 0x34000) if(Version < 0x34000)
stream.write((char*)fbuf, sizeof(fbuf)); stream->write(fbuf, sizeof(fbuf));
if(!(this->geoflags & NATIVE)){ if(!(this->geoflags & NATIVE)){
if(this->geoflags & PRELIT) if(this->geoflags & PRELIT)
stream.write((char*)this->colors, 4*this->numVertices); stream->write(this->colors, 4*this->numVertices);
for(int32 i = 0; i < this->numTexCoordSets; i++) for(int32 i = 0; i < this->numTexCoordSets; i++)
stream.write((char*)this->texCoords[i], stream->write(this->texCoords[i],
2*this->numVertices*4); 2*this->numVertices*4);
stream.write((char*)this->triangles, 4*this->numTriangles*2); stream->write(this->triangles, 4*this->numTriangles*2);
} }
for(int32 i = 0; i < this->numMorphTargets; i++){ for(int32 i = 0; i < this->numMorphTargets; i++){
MorphTarget *m = &this->morphTargets[i]; MorphTarget *m = &this->morphTargets[i];
stream.write((char*)m->boundingSphere, 4*4); stream->write(m->boundingSphere, 4*4);
if(!(this->geoflags & NATIVE)){ if(!(this->geoflags & NATIVE)){
writeInt32(m->vertices != NULL, stream); stream->writeI32(m->vertices != NULL);
writeInt32(m->normals != NULL, stream); stream->writeI32(m->normals != NULL);
if(m->vertices) if(m->vertices)
stream.write((char*)m->vertices, stream->write(m->vertices,
3*this->numVertices*4); 3*this->numVertices*4);
if(m->normals) if(m->normals)
stream.write((char*)m->normals, stream->write(m->normals,
3*this->numVertices*4); 3*this->numVertices*4);
}else{ }else{
writeInt32(0, stream); stream->writeI32(0);
writeInt32(0, stream); stream->writeI32(0);
} }
} }
@ -224,9 +223,9 @@ Geometry::streamWrite(ostream &stream)
size += 4 + 12 + this->materialList[i]->streamGetSize(); size += 4 + 12 + this->materialList[i]->streamGetSize();
WriteChunkHeader(stream, ID_MATLIST, size); WriteChunkHeader(stream, ID_MATLIST, size);
WriteChunkHeader(stream, ID_STRUCT, 4 + this->numMaterials*4); WriteChunkHeader(stream, ID_STRUCT, 4 + this->numMaterials*4);
writeInt32(this->numMaterials, stream); stream->writeI32(this->numMaterials);
for(int32 i = 0; i < this->numMaterials; i++) for(int32 i = 0; i < this->numMaterials; i++)
writeInt32(-1, stream); stream->writeI32(-1);
for(int32 i = 0; i < this->numMaterials; i++) for(int32 i = 0; i < this->numMaterials; i++)
this->materialList[i]->streamWrite(stream); this->materialList[i]->streamWrite(stream);
@ -321,12 +320,12 @@ struct MatStreamData
}; };
Material* Material*
Material::streamRead(istream &stream) Material::streamRead(Stream *stream)
{ {
uint32 length; uint32 length;
MatStreamData buf; MatStreamData buf;
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
stream.read((char*)&buf, sizeof(buf)); stream->read(&buf, sizeof(buf));
Material *mat = new Material; Material *mat = new Material;
mat->color[0] = buf.color[0]; mat->color[0] = buf.color[0];
mat->color[1] = buf.color[1]; mat->color[1] = buf.color[1];
@ -347,7 +346,7 @@ Material::streamRead(istream &stream)
} }
bool bool
Material::streamWrite(ostream &stream) Material::streamWrite(Stream *stream)
{ {
MatStreamData buf; MatStreamData buf;
@ -364,7 +363,7 @@ Material::streamWrite(ostream &stream)
buf.flags = 0; buf.flags = 0;
buf.unused = 0; buf.unused = 0;
buf.textured = this->texture != NULL; buf.textured = this->texture != NULL;
stream.write((char*)&buf, sizeof(buf)); stream->write(&buf, sizeof(buf));
if(this->texture) if(this->texture)
this->texture->streamWrite(stream); this->texture->streamWrite(stream);
@ -409,19 +408,19 @@ Texture::decRef(void)
} }
Texture* Texture*
Texture::streamRead(istream &stream) Texture::streamRead(Stream *stream)
{ {
uint32 length; uint32 length;
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
Texture *tex = new Texture; Texture *tex = new Texture;
tex->filterAddressing = readUInt16(stream); tex->filterAddressing = stream->readU16();
stream.seekg(2, ios::cur); stream->seek(2);
assert(FindChunk(stream, ID_STRING, &length, NULL)); assert(FindChunk(stream, ID_STRING, &length, NULL));
stream.read(tex->name, length); stream->read(tex->name, length);
assert(FindChunk(stream, ID_STRING, &length, NULL)); assert(FindChunk(stream, ID_STRING, &length, NULL));
stream.read(tex->mask, length); stream->read(tex->mask, length);
tex->streamReadPlugins(stream); tex->streamReadPlugins(stream);
@ -429,25 +428,25 @@ Texture::streamRead(istream &stream)
} }
bool bool
Texture::streamWrite(ostream &stream) Texture::streamWrite(Stream *stream)
{ {
int size; int size;
WriteChunkHeader(stream, ID_TEXTURE, this->streamGetSize()); WriteChunkHeader(stream, ID_TEXTURE, this->streamGetSize());
WriteChunkHeader(stream, ID_STRUCT, 4); WriteChunkHeader(stream, ID_STRUCT, 4);
writeUInt32(this->filterAddressing, stream); stream->writeU32(this->filterAddressing);
// TODO: length can't be > 32 // TODO: length can't be > 32
size = strlen(this->name)+3 & ~3; size = strlen(this->name)+3 & ~3;
if(size < 4) if(size < 4)
size = 4; size = 4;
WriteChunkHeader(stream, ID_STRING, size); WriteChunkHeader(stream, ID_STRING, size);
stream.write(this->name, size); stream->write(this->name, size);
size = strlen(this->mask)+3 & ~3; size = strlen(this->mask)+3 & ~3;
if(size < 4) if(size < 4)
size = 4; size = 4;
WriteChunkHeader(stream, ID_STRING, size); WriteChunkHeader(stream, ID_STRING, size);
stream.write(this->mask, size); stream->write(this->mask, size);
this->streamWritePlugins(stream); this->streamWritePlugins(stream);
return true; return true;

View File

@ -14,6 +14,22 @@ using namespace std;
namespace Rw { namespace Rw {
uint8
readUInt8(istream &rw)
{
uint8 tmp;
rw.read((char*)&tmp, sizeof(uint8));
return tmp;
}
uint32
writeUInt8(uint8 tmp, ostream &rw)
{
rw.write((char*)&tmp, sizeof(uint8));
return sizeof(uint8);
}
Image::Image(int32 width, int32 height, int32 depth) Image::Image(int32 width, int32 height, int32 depth)
{ {
this->flags = 0; this->flags = 0;
@ -72,9 +88,9 @@ Image::setPalette(uint8 *palette)
// TGA I/O // TGA I/O
// //
#pragma pack(push) //#pragma pack(push)
#pragma pack(1) //#pragma pack(1)
struct TGAHeader struct __attribute__((__packed__)) TGAHeader
{ {
int8 IDlen; int8 IDlen;
int8 colorMapType; int8 colorMapType;
@ -87,7 +103,7 @@ struct TGAHeader
uint8 depth; uint8 depth;
uint8 descriptor; uint8 descriptor;
}; };
#pragma pack(push) //#pragma pack(push)
Image* Image*
readTGA(const char *filename) readTGA(const char *filename)

View File

@ -3,15 +3,16 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> #include <new>
#include <fstream>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
#include "rwobjects.h" #include "rwobjects.h"
#include "rwogl.h" #include "rwogl.h"
#ifdef RW_OPENGL
#include <GL/glew.h> #include <GL/glew.h>
#endif
using namespace std; using namespace std;
@ -66,7 +67,7 @@ DestroyNativeData(void *object, int32, int32)
} }
void void
ReadNativeData(istream &stream, int32, void *object, int32, int32) ReadNativeData(Stream *stream, int32, void *object, int32, int32)
{ {
Geometry *geometry = (Geometry*)object; Geometry *geometry = (Geometry*)object;
InstanceDataHeader *header = new InstanceDataHeader; InstanceDataHeader *header = new InstanceDataHeader;
@ -74,26 +75,25 @@ ReadNativeData(istream &stream, int32, void *object, int32, int32)
header->platform = PLATFORM_OGL; header->platform = PLATFORM_OGL;
header->vbo = 0; header->vbo = 0;
header->ibo = 0; header->ibo = 0;
header->numAttribs = readUInt32(stream); header->numAttribs = stream->readU32();
header->attribs = new AttribDesc[header->numAttribs]; header->attribs = new AttribDesc[header->numAttribs];
stream.read((char*)header->attribs, stream->read(header->attribs,
header->numAttribs*sizeof(AttribDesc)); header->numAttribs*sizeof(AttribDesc));
header->dataSize = header->attribs[0].stride*geometry->numVertices; header->dataSize = header->attribs[0].stride*geometry->numVertices;
header->data = new uint8[header->dataSize]; header->data = new uint8[header->dataSize];
stream.read((char*)header->data, header->dataSize); stream->read(header->data, header->dataSize);
} }
void void
WriteNativeData(ostream &stream, int32, void *object, int32, int32) WriteNativeData(Stream *stream, int32, void *object, int32, int32)
{ {
Geometry *geometry = (Geometry*)object; Geometry *geometry = (Geometry*)object;
assert(geometry->instData->platform == PLATFORM_OGL); assert(geometry->instData->platform == PLATFORM_OGL);
InstanceDataHeader *header = InstanceDataHeader *header =
(InstanceDataHeader*)geometry->instData; (InstanceDataHeader*)geometry->instData;
writeUInt32(header->numAttribs, stream); stream->writeU32(header->numAttribs);
stream.write((char*)header->attribs, stream->write(header->attribs, header->numAttribs*sizeof(AttribDesc));
header->numAttribs*sizeof(AttribDesc)); stream->write(header->data, header->dataSize);
stream.write((char*)header->data, header->dataSize);
} }
int32 int32

View File

@ -3,8 +3,9 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> //#include <iostream>
#include <fstream> //#include <fstream>
#include <new>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
@ -45,18 +46,18 @@ destroyNodeName(void *object, int32, int32)
} }
static void static void
readNodeName(istream &stream, int32 len, void *object, int32 offset, int32) readNodeName(Stream *stream, int32 len, void *object, int32 offset, int32)
{ {
char *name = PLUGINOFFSET(char, object, offset); char *name = PLUGINOFFSET(char, object, offset);
stream.read(name, len); stream->read(name, len);
name[len] = '\0'; name[len] = '\0';
} }
static void static void
writeNodeName(ostream &stream, int32 len, void *object, int32 offset, int32) writeNodeName(Stream *stream, int32 len, void *object, int32 offset, int32)
{ {
char *name = PLUGINOFFSET(char, object, offset); char *name = PLUGINOFFSET(char, object, offset);
stream.write(name, len); stream->write(name, len);
} }
static int32 static int32
@ -86,12 +87,12 @@ registerNodeNamePlugin(void)
// Mesh // Mesh
static void static void
readMesh(istream &stream, int32 len, void *object, int32, int32) readMesh(Stream *stream, int32 len, void *object, int32, int32)
{ {
Geometry *geo = (Geometry*)object; Geometry *geo = (Geometry*)object;
int32 indbuf[256]; int32 indbuf[256];
uint32 buf[3]; uint32 buf[3];
stream.read((char*)buf, 12); stream->read(buf, 12);
geo->meshHeader = new MeshHeader; geo->meshHeader = new MeshHeader;
geo->meshHeader->flags = buf[0]; geo->meshHeader->flags = buf[0];
geo->meshHeader->numMeshes = buf[1]; geo->meshHeader->numMeshes = buf[1];
@ -100,7 +101,7 @@ readMesh(istream &stream, int32 len, void *object, int32, int32)
Mesh *mesh = geo->meshHeader->mesh; Mesh *mesh = geo->meshHeader->mesh;
bool hasData = len > 12+geo->meshHeader->numMeshes*8; bool hasData = len > 12+geo->meshHeader->numMeshes*8;
for(uint32 i = 0; i < geo->meshHeader->numMeshes; i++){ for(uint32 i = 0; i < geo->meshHeader->numMeshes; i++){
stream.read((char*)buf, 8); stream->read(buf, 8);
mesh->numIndices = buf[0]; mesh->numIndices = buf[0];
mesh->material = geo->materialList[buf[1]]; mesh->material = geo->materialList[buf[1]];
mesh->indices = NULL; mesh->indices = NULL;
@ -108,7 +109,7 @@ readMesh(istream &stream, int32 len, void *object, int32, int32)
// OpenGL stores uint16 indices here // OpenGL stores uint16 indices here
if(hasData){ if(hasData){
mesh->indices = new uint16[mesh->numIndices]; mesh->indices = new uint16[mesh->numIndices];
stream.read((char*)mesh->indices, stream->read(mesh->indices,
mesh->numIndices*2); mesh->numIndices*2);
} }
}else{ }else{
@ -117,7 +118,7 @@ readMesh(istream &stream, int32 len, void *object, int32, int32)
int32 numIndices = mesh->numIndices; int32 numIndices = mesh->numIndices;
for(; numIndices > 0; numIndices -= 256){ for(; numIndices > 0; numIndices -= 256){
int32 n = numIndices < 256 ? numIndices : 256; int32 n = numIndices < 256 ? numIndices : 256;
stream.read((char*)indbuf, n*4); stream->read(indbuf, n*4);
for(int32 j = 0; j < n; j++) for(int32 j = 0; j < n; j++)
ind[j] = indbuf[j]; ind[j] = indbuf[j];
ind += n; ind += n;
@ -128,7 +129,7 @@ readMesh(istream &stream, int32 len, void *object, int32, int32)
} }
static void static void
writeMesh(ostream &stream, int32, void *object, int32, int32) writeMesh(Stream *stream, int32, void *object, int32, int32)
{ {
Geometry *geo = (Geometry*)object; Geometry *geo = (Geometry*)object;
int32 indbuf[256]; int32 indbuf[256];
@ -136,18 +137,18 @@ writeMesh(ostream &stream, int32, void *object, int32, int32)
buf[0] = geo->meshHeader->flags; buf[0] = geo->meshHeader->flags;
buf[1] = geo->meshHeader->numMeshes; buf[1] = geo->meshHeader->numMeshes;
buf[2] = geo->meshHeader->totalIndices; buf[2] = geo->meshHeader->totalIndices;
stream.write((char*)buf, 12); stream->write(buf, 12);
Mesh *mesh = geo->meshHeader->mesh; Mesh *mesh = geo->meshHeader->mesh;
for(uint32 i = 0; i < geo->meshHeader->numMeshes; i++){ for(uint32 i = 0; i < geo->meshHeader->numMeshes; i++){
buf[0] = mesh->numIndices; buf[0] = mesh->numIndices;
buf[1] = findPointer((void*)mesh->material, buf[1] = findPointer((void*)mesh->material,
(void**)geo->materialList, (void**)geo->materialList,
geo->numMaterials); geo->numMaterials);
stream.write((char*)buf, 8); stream->write(buf, 8);
if(geo->geoflags & Geometry::NATIVE){ if(geo->geoflags & Geometry::NATIVE){
assert(geo->instData != NULL); assert(geo->instData != NULL);
if(geo->instData->platform == PLATFORM_OGL) if(geo->instData->platform == PLATFORM_OGL)
stream.write((char*)mesh->indices, stream->write(mesh->indices,
mesh->numIndices*2); mesh->numIndices*2);
}else{ }else{
uint16 *ind = mesh->indices; uint16 *ind = mesh->indices;
@ -156,7 +157,7 @@ writeMesh(ostream &stream, int32, void *object, int32, int32)
int32 n = numIndices < 256 ? numIndices : 256; int32 n = numIndices < 256 ? numIndices : 256;
for(int32 j = 0; j < n; j++) for(int32 j = 0; j < n; j++)
indbuf[j] = ind[j]; indbuf[j] = ind[j];
stream.write((char*)indbuf, n*4); stream->write(indbuf, n*4);
ind += n; ind += n;
} }
} }
@ -207,32 +208,32 @@ destroyNativeData(void *object, int32 offset, int32 size)
} }
static void static void
readNativeData(istream &stream, int32 len, void *object, int32 o, int32 s) readNativeData(Stream *stream, int32 len, void *object, int32 o, int32 s)
{ {
ChunkHeaderInfo header; ChunkHeaderInfo header;
uint32 libid; uint32 libid;
uint32 platform; uint32 platform;
// ugly hack to find out platform // ugly hack to find out platform
stream.seekg(-4, ios::cur); stream->seek(-4);
libid = readUInt32(stream); libid = stream->readU32();
ReadChunkHeaderInfo(stream, &header); ReadChunkHeaderInfo(stream, &header);
if(header.type == ID_STRUCT && if(header.type == ID_STRUCT &&
LibraryIDPack(header.version, header.build) == libid){ LibraryIDPack(header.version, header.build) == libid){
// must be PS2 or Xbox // must be PS2 or Xbox
platform = readUInt32(stream); platform = stream->readU32();
stream.seekg(-16, ios::cur); stream->seek(-16);
if(platform == PLATFORM_PS2) if(platform == PLATFORM_PS2)
ReadNativeDataPS2(stream, len, object, o, s); ReadNativeDataPS2(stream, len, object, o, s);
else if(platform == PLATFORM_XBOX) else if(platform == PLATFORM_XBOX)
stream.seekg(len, ios::cur); stream->seek(len);
}else{ }else{
stream.seekg(-12, ios::cur); stream->seek(-12);
Gl::ReadNativeData(stream, len, object, o, s); Gl::ReadNativeData(stream, len, object, o, s);
} }
} }
static void static void
writeNativeData(ostream &stream, int32 len, void *object, int32 o, int32 s) writeNativeData(Stream *stream, int32 len, void *object, int32 o, int32 s)
{ {
Geometry *geometry = (Geometry*)object; Geometry *geometry = (Geometry*)object;
if(geometry->instData == NULL) if(geometry->instData == NULL)
@ -298,13 +299,13 @@ destroyBreakableModel(void *object, int32 offset, int32)
} }
static void static void
readBreakableModel(istream &stream, int32, void *object, int32 o, int32) readBreakableModel(Stream *stream, int32, void *object, int32 o, int32)
{ {
uint32 header[13]; uint32 header[13];
uint32 hasBreakable = readUInt32(stream); uint32 hasBreakable = stream->readU32();
if(hasBreakable == 0) if(hasBreakable == 0)
return; return;
stream.read((char*)header, 13*4); stream->read(header, 13*4);
uint32 size = header[1]*(12+8+4) + header[5]*(6+2) + uint32 size = header[1]*(12+8+4) + header[5]*(6+2) +
header[8]*(32+32+12); header[8]*(32+32+12);
uint8 *p = new uint8[sizeof(Breakable)+size]; uint8 *p = new uint8[sizeof(Breakable)+size];
@ -315,7 +316,7 @@ readBreakableModel(istream &stream, int32, void *object, int32 o, int32)
breakable->numFaces = header[5]; breakable->numFaces = header[5];
breakable->numMaterials = header[8]; breakable->numMaterials = header[8];
p += sizeof(Breakable); p += sizeof(Breakable);
stream.read((char*)p, size); stream->read(p, size);
breakable->vertices = (float*)p; breakable->vertices = (float*)p;
p += breakable->numVertices*12; p += breakable->numVertices*12;
breakable->texCoords = (float*)p; breakable->texCoords = (float*)p;
@ -334,24 +335,24 @@ readBreakableModel(istream &stream, int32, void *object, int32 o, int32)
} }
static void static void
writeBreakableModel(ostream &stream, int32, void *object, int32 o, int32) writeBreakableModel(Stream *stream, int32, void *object, int32 o, int32)
{ {
uint32 header[13]; uint32 header[13];
Breakable *breakable = *PLUGINOFFSET(Breakable*, object, o); Breakable *breakable = *PLUGINOFFSET(Breakable*, object, o);
uint8 *p = (uint8*)breakable; uint8 *p = (uint8*)breakable;
if(breakable == NULL){ if(breakable == NULL){
writeUInt32(0, stream); stream->writeU32(0);
return; return;
} }
writeUInt32(1, stream); stream->writeU32(1);
memset((char*)header, 0, 13*4); memset((char*)header, 0, 13*4);
header[0] = breakable->position; header[0] = breakable->position;
header[1] = breakable->numVertices; header[1] = breakable->numVertices;
header[5] = breakable->numFaces; header[5] = breakable->numFaces;
header[8] = breakable->numMaterials; header[8] = breakable->numMaterials;
stream.write((char*)header, 13*4); stream->write(header, 13*4);
p += sizeof(Breakable); p += sizeof(Breakable);
stream.write((char*)p, breakable->numVertices*(12+8+4) + stream->write(p, breakable->numVertices*(12+8+4) +
breakable->numFaces*(6+2) + breakable->numFaces*(6+2) +
breakable->numMaterials*(32+32+12)); breakable->numMaterials*(32+32+12));
} }

View File

@ -3,8 +3,7 @@
#include <cstring> #include <cstring>
#include <cassert> #include <cassert>
#include <iostream> #include <new>
#include <fstream>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
@ -30,11 +29,11 @@ DestroyNativeDataPS2(void *object, int32, int32)
} }
void void
ReadNativeDataPS2(istream &stream, int32, void *object, int32, int32) ReadNativeDataPS2(Stream *stream, int32, void *object, int32, int32)
{ {
Geometry *geometry = (Geometry*)object; Geometry *geometry = (Geometry*)object;
assert(FindChunk(stream, ID_STRUCT, NULL, NULL)); assert(FindChunk(stream, ID_STRUCT, NULL, NULL));
assert(readUInt32(stream) == PLATFORM_PS2); assert(stream->readU32() == PLATFORM_PS2);
PS2InstanceDataHeader *header = new PS2InstanceDataHeader; PS2InstanceDataHeader *header = new PS2InstanceDataHeader;
geometry->instData = header; geometry->instData = header;
header->platform = PLATFORM_PS2; header->platform = PLATFORM_PS2;
@ -44,21 +43,21 @@ ReadNativeDataPS2(istream &stream, int32, void *object, int32, int32)
for(uint32 i = 0; i < header->numMeshes; i++){ for(uint32 i = 0; i < header->numMeshes; i++){
PS2InstanceData *instance = &header->instanceMeshes[i]; PS2InstanceData *instance = &header->instanceMeshes[i];
uint32 buf[2]; uint32 buf[2];
stream.read((char*)buf, 8); stream->read(buf, 8);
instance->dataSize = buf[0]; instance->dataSize = buf[0];
instance->noRefChain = buf[1]; instance->noRefChain = buf[1];
instance->data = new uint8[instance->dataSize]; instance->data = new uint8[instance->dataSize];
stream.read((char*)instance->data, instance->dataSize); stream->read(instance->data, instance->dataSize);
} }
} }
void void
WriteNativeDataPS2(ostream &stream, int32 len, void *object, int32, int32) WriteNativeDataPS2(Stream *stream, int32 len, void *object, int32, int32)
{ {
Geometry *geometry = (Geometry*)object; Geometry *geometry = (Geometry*)object;
WriteChunkHeader(stream, ID_STRUCT, len-12); WriteChunkHeader(stream, ID_STRUCT, len-12);
assert(geometry->instData->platform == PLATFORM_PS2); assert(geometry->instData->platform == PLATFORM_PS2);
writeUInt32(PLATFORM_PS2, stream); stream->writeU32(PLATFORM_PS2);
assert(geometry->instData != NULL); assert(geometry->instData != NULL);
PS2InstanceDataHeader *header = PS2InstanceDataHeader *header =
(PS2InstanceDataHeader*)geometry->instData; (PS2InstanceDataHeader*)geometry->instData;
@ -67,8 +66,8 @@ WriteNativeDataPS2(ostream &stream, int32 len, void *object, int32, int32)
uint32 buf[2]; uint32 buf[2];
buf[0] = instance->dataSize; buf[0] = instance->dataSize;
buf[1] = instance->noRefChain; buf[1] = instance->noRefChain;
stream.write((char*)buf, 8); stream->write(buf, 8);
stream.write((char*)instance->data, instance->dataSize); stream->write(instance->data, instance->dataSize);
} }
} }

View File

@ -2,8 +2,7 @@
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
#include <iostream> #include <new>
#include <fstream>
#include "rwbase.h" #include "rwbase.h"
#include "rwplugin.h" #include "rwplugin.h"
@ -15,123 +14,169 @@ namespace Rw {
int Version = 0x36003; int Version = 0x36003;
int Build = 0xFFFF; int Build = 0xFFFF;
uint32 int32
writeInt8(int8 tmp, ostream &rw) Stream::writeI8(int8 val)
{ {
rw.write((char*)&tmp, sizeof(int8)); return write(&val, sizeof(int8));
return sizeof(int8);
} }
uint32 int32
writeUInt8(uint8 tmp, ostream &rw) Stream::writeU8(uint8 val)
{ {
rw.write((char*)&tmp, sizeof(uint8)); return write(&val, sizeof(uint8));
return sizeof(uint8);
} }
uint32 int32
writeInt16(int16 tmp, ostream &rw) Stream::writeI16(int16 val)
{ {
rw.write((char*)&tmp, sizeof(int16)); return write(&val, sizeof(int16));
return sizeof(int16);
} }
uint32 int32
writeUInt16(uint16 tmp, ostream &rw) Stream::writeU16(uint16 val)
{ {
rw.write((char*)&tmp, sizeof(uint16)); return write(&val, sizeof(uint16));
return sizeof(uint16);
} }
uint32 int32
writeInt32(int32 tmp, ostream &rw) Stream::writeI32(int32 val)
{ {
rw.write((char*)&tmp, sizeof(int32)); return write(&val, sizeof(int32));
return sizeof(int32);
} }
uint32 int32
writeUInt32(uint32 tmp, ostream &rw) Stream::writeU32(uint32 val)
{ {
rw.write((char*)&tmp, sizeof(uint32)); return write(&val, sizeof(uint32));
return sizeof(uint32);
} }
uint32 int32
writeFloat32(float32 tmp, ostream &rw) Stream::writeF32(float32 val)
{ {
rw.write((char*)&tmp, sizeof(float32)); return write(&val, sizeof(float32));
return sizeof(float32); }
int8
Stream::readI8(void)
{
int8 tmp;
read(&tmp, sizeof(int8));
return tmp;
} }
uint8 uint8
readUInt8(istream &rw) Stream::readU8(void)
{ {
uint8 tmp; uint8 tmp;
rw.read((char*)&tmp, sizeof(uint8)); read(&tmp, sizeof(uint8));
return tmp; return tmp;
} }
int16 int16
readInt16(istream &rw) Stream::readI16(void)
{ {
int16 tmp; int16 tmp;
rw.read((char*)&tmp, sizeof(int16)); read(&tmp, sizeof(int16));
return tmp; return tmp;
} }
uint16 uint16
readUInt16(istream &rw) Stream::readU16(void)
{ {
uint16 tmp; uint16 tmp;
rw.read((char*)&tmp, sizeof(uint16)); read(&tmp, sizeof(uint16));
return tmp; return tmp;
} }
int32 int32
readInt32(istream &rw) Stream::readI32(void)
{ {
int32 tmp; int32 tmp;
rw.read((char*)&tmp, sizeof(int32)); read(&tmp, sizeof(int32));
return tmp; return tmp;
} }
uint32 uint32
readUInt32(istream &rw) Stream::readU32(void)
{ {
uint32 tmp; uint32 tmp;
rw.read((char*)&tmp, sizeof(uint32)); read(&tmp, sizeof(uint32));
return tmp; return tmp;
} }
float32 float32
readFloat32(istream &rw) Stream::readF32(void)
{ {
float32 tmp; float32 tmp;
rw.read((char*)&tmp, sizeof(float32)); read(&tmp, sizeof(float32));
return tmp; return tmp;
} }
StreamFile*
StreamFile::open(const char *path, const char *mode)
{
this->file = fopen(path, mode);
return this->file ? this : NULL;
}
void
StreamFile::close(void)
{
fclose(this->file);
}
uint32
StreamFile::write(const void *data, uint32 length)
{
return fwrite(data, length, 1, this->file);
}
uint32
StreamFile::read(void *data, uint32 length)
{
printf("read %d bytes @ %x\n", length, this->tell());
return fread(data, length, 1, this->file);
}
void
StreamFile::seek(int32 offset, int32 whence)
{
fseek(this->file, offset, whence);
}
uint32
StreamFile::tell(void)
{
return ftell(this->file);
}
bool bool
WriteChunkHeader(ostream &s, int32 type, int32 size) StreamFile::eof(void)
{
return feof(this->file);
}
bool
WriteChunkHeader(Stream *s, int32 type, int32 size)
{ {
struct { struct {
int32 type, size; int32 type, size;
uint32 id; uint32 id;
} buf = { type, size, LibraryIDPack(Version, Build) }; } buf = { type, size, LibraryIDPack(Version, Build) };
s.write((char*)&buf, 12); s->write(&buf, 12);
return true; return true;
} }
bool bool
ReadChunkHeaderInfo(istream &s, ChunkHeaderInfo *header) ReadChunkHeaderInfo(Stream *s, ChunkHeaderInfo *header)
{ {
struct { struct {
int32 type, size; int32 type, size;
uint32 id; uint32 id;
} buf; } buf;
s.read((char*)&buf, 12); s->read(&buf, 12);
if(s.eof()) if(s->eof())
return false; return false;
assert(header != NULL); assert(header != NULL);
header->type = buf.type; header->type = buf.type;
@ -142,7 +187,7 @@ ReadChunkHeaderInfo(istream &s, ChunkHeaderInfo *header)
} }
bool bool
FindChunk(istream &s, uint32 type, uint32 *length, uint32 *version) FindChunk(Stream *s, uint32 type, uint32 *length, uint32 *version)
{ {
ChunkHeaderInfo header; ChunkHeaderInfo header;
while(ReadChunkHeaderInfo(s, &header)){ while(ReadChunkHeaderInfo(s, &header)){
@ -155,7 +200,7 @@ FindChunk(istream &s, uint32 type, uint32 *length, uint32 *version)
*version = header.version; *version = header.version;
return true; return true;
} }
s.seekg(header.length, ios::cur); s->seek(header.length);
} }
return false; return false;
} }

View File

@ -13,20 +13,43 @@ typedef int32 bool32;
typedef uint8 byte; typedef uint8 byte;
typedef uint32 uint; typedef uint32 uint;
uint32 writeInt8(int8 tmp, std::ostream &rw); class Stream
uint32 writeUInt8(uint8 tmp, std::ostream &rw); {
uint32 writeInt16(int16 tmp, std::ostream &rw); public:
uint32 writeUInt16(uint16 tmp, std::ostream &rw); virtual void close(void) = 0;
uint32 writeInt32(int32 tmp, std::ostream &rw); virtual uint32 write(const void *data, uint32 length) = 0;
uint32 writeUInt32(uint32 tmp, std::ostream &rw); virtual uint32 read(void *data, uint32 length) = 0;
uint32 writeFloat32(float32 tmp, std::ostream &rw); virtual void seek(int32 offset, int32 whence = 1) = 0;
int8 readInt8(std::istream &rw); virtual uint32 tell(void) = 0;
uint8 readUInt8(std::istream &rw); virtual bool eof(void) = 0;
int16 readInt16(std::istream &rw); int32 writeI8(int8 val);
uint16 readUInt16(std::istream &rw); int32 writeU8(uint8 val);
int32 readInt32(std::istream &rw); int32 writeI16(int16 val);
uint32 readUInt32(std::istream &rw); int32 writeU16(uint16 val);
float32 readFloat32(std::istream &rw); int32 writeI32(int32 val);
int32 writeU32(uint32 val);
int32 writeF32(float32 val);
int8 readI8(void);
uint8 readU8(void);
int16 readI16(void);
uint16 readU16(void);
int32 readI32(void);
uint32 readU32(void);
float32 readF32(void);
};
class StreamFile : public Stream
{
FILE *file;
public:
void close(void);
uint32 write(const void *data, uint32 length);
uint32 read(void *data, uint32 length);
void seek(int32 offset, int32 whence);
uint32 tell(void);
bool eof(void);
StreamFile *open(const char *path, const char *mode);
};
enum Platform enum Platform
{ {
@ -72,6 +95,7 @@ extern int Build;
inline uint32 inline uint32
LibraryIDPack(int version, int build) LibraryIDPack(int version, int build)
{ {
// TODO: check version in if statement
if(build){ if(build){
version -= 0x30000; version -= 0x30000;
return (version&0xFFC0) << 14 | (version&0x3F) << 16 | return (version&0xFFC0) << 14 | (version&0x3F) << 16 |
@ -108,9 +132,9 @@ struct ChunkHeaderInfo
}; };
// TODO?: make these methods of ChunkHeaderInfo? // TODO?: make these methods of ChunkHeaderInfo?
bool WriteChunkHeader(std::ostream &s, int32 type, int32 size); bool WriteChunkHeader(Stream *s, int32 type, int32 size);
bool ReadChunkHeaderInfo(std::istream &s, ChunkHeaderInfo *header); bool ReadChunkHeaderInfo(Stream *s, ChunkHeaderInfo *header);
bool FindChunk(std::istream &s, uint32 type, uint32 *length, uint32 *version); bool FindChunk(Stream *s, uint32 type, uint32 *length, uint32 *version);
int32 findPointer(void *p, void **list, int32 num); int32 findPointer(void *p, void **list, int32 num);
} }

View File

@ -38,8 +38,8 @@ struct Texture : PluginBase<Texture>
Texture(void); Texture(void);
~Texture(void); ~Texture(void);
void decRef(void); void decRef(void);
static Texture *streamRead(std::istream &stream); static Texture *streamRead(Stream *stream);
bool streamWrite(std::ostream &stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
enum FilterMode { enum FilterMode {
@ -69,8 +69,8 @@ struct Material : PluginBase<Material>
Material(Material *m); Material(Material *m);
void decRef(void); void decRef(void);
~Material(void); ~Material(void);
static Material *streamRead(std::istream &stream); static Material *streamRead(Stream *stream);
bool streamWrite(std::ostream &stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
}; };
@ -128,8 +128,8 @@ struct Geometry : PluginBase<Geometry>, Object
Geometry(int32 numVerts, int32 numTris, uint32 flags); Geometry(int32 numVerts, int32 numTris, uint32 flags);
void decRef(void); void decRef(void);
~Geometry(void); ~Geometry(void);
static Geometry *streamRead(std::istream &stream); static Geometry *streamRead(Stream *stream);
bool streamWrite(std::ostream &stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
void addMorphTargets(int32 n); void addMorphTargets(int32 n);
@ -183,8 +183,8 @@ struct Light : PluginBase<Light>, Object
Light(void); Light(void);
Light(Light *l); Light(Light *l);
~Light(void); ~Light(void);
static Light *streamRead(std::istream &stream); static Light *streamRead(Stream *stream);
bool streamWrite(std::ostream &stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
}; };
@ -197,9 +197,9 @@ struct Atomic : PluginBase<Atomic>, Object
Atomic(void); Atomic(void);
Atomic(Atomic *a); Atomic(Atomic *a);
~Atomic(void); ~Atomic(void);
static Atomic *streamReadClump(std::istream &stream, static Atomic *streamReadClump(Stream *stream,
Frame **frameList, Geometry **geometryList); Frame **frameList, Geometry **geometryList);
bool streamWriteClump(std::ostream &stream, bool streamWriteClump(Stream *stream,
Frame **frameList, int32 numFrames); Frame **frameList, int32 numFrames);
uint32 streamGetSize(void); uint32 streamGetSize(void);
}; };
@ -216,13 +216,13 @@ struct Clump : PluginBase<Clump>, Object
Clump(void); Clump(void);
Clump(Clump *c); Clump(Clump *c);
~Clump(void); ~Clump(void);
static Clump *streamRead(std::istream &stream); static Clump *streamRead(Stream *stream);
bool streamWrite(std::ostream &stream); bool streamWrite(Stream *stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
private: private:
void frameListStreamRead(std::istream &stream, Frame ***flp, int32 *nf); void frameListStreamRead(Stream *stream, Frame ***flp, int32 *nf);
void frameListStreamWrite(std::ostream &stream, Frame **flp, int32 nf); void frameListStreamWrite(Stream *stream, Frame **flp, int32 nf);
}; };
} }

View File

@ -27,8 +27,8 @@ struct InstanceDataHeader : Rw::InstanceDataHeader
}; };
void *DestroyNativeData(void *object, int32, int32); void *DestroyNativeData(void *object, int32, int32);
void ReadNativeData(std::istream &stream, int32 len, void *object, int32, int32); void ReadNativeData(Stream *stream, int32 len, void *object, int32, int32);
void WriteNativeData(std::ostream &stream, int32 len, void *object, int32, int32); void WriteNativeData(Stream *stream, int32 len, void *object, int32, int32);
int32 GetSizeNativeData(void *object, int32, int32); int32 GetSizeNativeData(void *object, int32, int32);
void Instance(Atomic *atomic); void Instance(Atomic *atomic);

View File

@ -6,8 +6,8 @@ namespace Rw {
typedef void *(*Constructor)(void *object, int32 offset, int32 size); typedef void *(*Constructor)(void *object, int32 offset, int32 size);
typedef void *(*Destructor)(void *object, int32 offset, int32 size); typedef void *(*Destructor)(void *object, int32 offset, int32 size);
typedef void *(*CopyConstructor)(void *dst, void *src, int32 offset, int32 size); typedef void *(*CopyConstructor)(void *dst, void *src, int32 offset, int32 size);
typedef void (*StreamRead)(std::istream &stream, int32 length, void *object, int32 offset, int32 size); typedef void (*StreamRead)(Stream *stream, int32 length, void *object, int32 offset, int32 size);
typedef void (*StreamWrite)(std::ostream &stream, int32 length, void *object, int32 offset, int32 size); typedef void (*StreamWrite)(Stream *stream, int32 length, void *object, int32 offset, int32 size);
typedef int32 (*StreamGetSize)(void *object, int32 offset, int32 size); typedef int32 (*StreamGetSize)(void *object, int32 offset, int32 size);
struct Plugin struct Plugin
@ -34,8 +34,8 @@ struct PluginBase
void constructPlugins(void); void constructPlugins(void);
void destructPlugins(void); void destructPlugins(void);
void copyPlugins(T *t); void copyPlugins(T *t);
void streamReadPlugins(std::istream &stream); void streamReadPlugins(Stream *stream);
void streamWritePlugins(std::ostream &stream); void streamWritePlugins(Stream *stream);
int streamGetPluginSize(void); int streamGetPluginSize(void);
static int registerPlugin(int size, uint id, static int registerPlugin(int size, uint id,
@ -78,7 +78,7 @@ PluginBase<T>::copyPlugins(T *t)
} }
template <typename T> void template <typename T> void
PluginBase<T>::streamReadPlugins(std::istream &stream) PluginBase<T>::streamReadPlugins(Stream *stream)
{ {
int32 length; int32 length;
Rw::ChunkHeaderInfo header; Rw::ChunkHeaderInfo header;
@ -93,14 +93,14 @@ PluginBase<T>::streamReadPlugins(std::istream &stream)
(void*)this, p->offset, p->size); (void*)this, p->offset, p->size);
goto cont; goto cont;
} }
stream.seekg(header.length, std::ios::cur); stream->seek(header.length);
cont: cont:
length -= header.length; length -= header.length;
} }
} }
template <typename T> void template <typename T> void
PluginBase<T>::streamWritePlugins(std::ostream &stream) PluginBase<T>::streamWritePlugins(Stream *stream)
{ {
int size = this->streamGetPluginSize(); int size = this->streamGetPluginSize();
Rw::WriteChunkHeader(stream, Rw::ID_EXTENSION, size); Rw::WriteChunkHeader(stream, Rw::ID_EXTENSION, size);

View File

@ -15,8 +15,8 @@ struct PS2InstanceDataHeader : InstanceDataHeader
}; };
void *DestroyNativeDataPS2(void *object, int32, int32); void *DestroyNativeDataPS2(void *object, int32, int32);
void ReadNativeDataPS2(std::istream &stream, int32 len, void *object, int32, int32); void ReadNativeDataPS2(Stream *stream, int32 len, void *object, int32, int32);
void WriteNativeDataPS2(std::ostream &stream, int32 len, void *object, int32, int32); void WriteNativeDataPS2(Stream *stream, int32 len, void *object, int32, int32);
int32 GetSizeNativeDataPS2(void *object, int32, int32); int32 GetSizeNativeDataPS2(void *object, int32, int32);
} }