📄 chunkfile.h
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
#ifndef _CORE_CHUNKFILE_H_
#define _CORE_CHUNKFILE_H_
#include "core/bitStream.h"
#include "core/resManager.h"
#include "console/simBase.h"
#ifdef TGE_RPG
// class, fourcc, rev
#define DECLARE_CHUNK(className, fourcc, rev) \
static ConcreteClassRep<className> dynClassRep; \
static AbstractClassRep* getParentStaticClassRep(); \
static AbstractClassRep* getStaticClassRep(); \
virtual AbstractClassRep* getClassRep() const; \
static const U32 getFourCC() { return makeFourCCTag fourcc; } \
static const U32 getChunkVersion() { return rev; }\
static className::FourCCToAcr ms_simChunkAux;
#define IMPLEMENT_CHUNK(className) \
AbstractClassRep* className::getClassRep() const { return &className::dynClassRep; } \
AbstractClassRep* className::getStaticClassRep() { return &dynClassRep; } \
AbstractClassRep* className::getParentStaticClassRep() { return Parent::getStaticClassRep(); } \
ConcreteClassRep<className> className::dynClassRep(#className, 0, -1, 0, className::getParentStaticClassRep());\
className::FourCCToAcr className::ms_simChunkAux(getFourCC(),&dynClassRep)
#else
// class, fourcc, rev
#define DECLARE_CHUNK(className, fourcc, rev) \
static ConcreteClassRep<className> dynClassRep; \
static AbstractClassRep* getParentStaticClassRep(); \
static AbstractClassRep* getStaticClassRep(); \
virtual AbstractClassRep* getClassRep() const; \
virtual const U32 getFourCC() { return makeFourCCTag fourcc; } \
virtual const U32 getChunkVersion() { return rev; }
#endif
class SimChunk : public SimGroup
{
typedef SimGroup Parent;
public:
/// Called post console init to set up the 4cc->class mappings.
struct FourCCToAcr
{
U32 fourCC;
AbstractClassRep *acr;
#ifdef TGE_RPG
FourCCToAcr(U32 fourCC, AbstractClassRep *acr);
#endif
};
DECLARE_CHUNK(SimChunk, ('S','C','H','K'), 1);
/// Called to read the chunk's data. We are provided with all the
/// information that we got from the file, just in case it's relevant.
virtual void readChunk(BitStream &s, const U32 length, const U32 version, const U32 crc, const U32 fourCC);
/// Write a chunk. Length is defined by the size of the data we write out,
/// version by the DECLARE_CHUNK macro, crc by the data we write, and
/// FourCC by the macro. So all we have to do is write data!
virtual void writeChunk(BitStream &s);
#ifdef TGE_RPG
static const char *getFourCCString()
#else
virtual const char *getFourCCString()
#endif
{
U8 tmp[5];
U32 cc = getFourCC();
tmp[0] = (cc >> 0) & 0xFF;
tmp[1] = (cc >> 8) & 0xFF;
tmp[2] = (cc >> 16) & 0xFF;
tmp[3] = (cc >> 24) & 0xFF;
tmp[4] = 0;
return StringTable->insert((const char*)tmp, true);
}
static Vector<FourCCToAcr*> smFourCCList;
static void initChunkMappings();
static SimChunk *createChunkFromFourCC(U32 fourCC);
#ifdef TGE_RPG
//static void addFourCCToAcr(U32 fourCC, AbstractClassRep *acr);
static void destoryChunkMappings();
static void consoleInit();
#endif
};
/// This is a special purpose chunk we use to deal with situations where
/// we have an unknown FourCC. Basically, it loads everything into a buffer,
/// then writes it back out again, masquerading as the unknown chunk.
///
/// This means we have to manually do some of what DECLARE_CHUNK does, ew.
class UnknownChunk : public SimChunk
{
typedef SimChunk Parent;
#ifdef TGE_RPG
//static U32 mChunkFourCC;
//static U8 mChunkFourCCString[4];
//static U32 mChunkVersion;
#else
U32 mChunkFourCC;
U8 mChunkFourCCString[4];
U32 mChunkVersion;
#endif
U32 mChunkCRC;
U32 mDataLength;
U8 *mDataBuffer;
public:
#ifdef TGE_RPG
DECLARE_CHUNK(UnknownChunk, ('S','C','H','K'), 1);
#else
DECLARE_CONOBJECT(UnknownChunk);
#endif
UnknownChunk();
~UnknownChunk();
#ifdef TGE_RPG
//static void consoleInit();
//static U32 getFourCC() { return mChunkFourCC; }
static const char *getFourCCString() { return Parent::getFourCCString(); }
//static U32 getChunkVersion() { return mChunkVersion; }
#else
virtual const U32 getFourCC() const { return mChunkFourCC; }
virtual const U8 *getFourCCString() const { return &mChunkFourCCString[0]; }
virtual const U32 getChunkVersion() const { return mChunkVersion; }
#endif
virtual void readChunk(BitStream &s, const U32 length, const U32 version, const U32 crc, const U32 fourCC);
virtual void writeChunk(BitStream &s);
};
class TextChunk : public SimChunk
{
typedef SimChunk Parent;
protected:
const char * mText;
public:
DECLARE_CHUNK(TextChunk, ('T','E','X','T'), 2);
TextChunk()
{
mText = StringTable->insert("");
}
~TextChunk()
{
// No deletion, it's a string table entry.
}
static void initPersistFields();
void readChunk(BitStream &s, const U32 length, const U32 version, const U32 crc, const U32 fourCC);
void writeChunk(BitStream &s);
};
// Allow only SimChunk and derivatives to be added to a SimChunk (override addObject)
// write/readChunk must call Parent::'s versions
class ChunkFile : ResourceInstance
{
private:
/// Root of the chunk hierarchy for this file.
SimObjectPtr<SimChunk> mRoot;
/// Helper function, saves out a chunk and its children...
bool saveInner(Stream &s, SimChunk *c);
/// Helper function, loads up a chunk and its children...
SimChunk *loadInner(Stream &s, U32 childCount=1);
static const U32 csmFileFourCC;
static const U32 csmFileVersion;
public:
/// Generic chunk file loader.
static ResourceInstance *constructChunkFile(Stream &stream);
/// Return a pointer to the root chunk.
SimChunk * getRoot() const { return mRoot; };
void setRoot( SimChunk * c) { mRoot = c; };
/// Serialize!
bool save(const char * filename);
/// Deserialize!
bool load(Stream &s);
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -