1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-03 15:09:39 +00:00

components/esm header refactoring in progress. Refactored:

esm_reader.hpp
loadacti.hpp
loadalch.hpp
loadappa.hpp
loadarmo.hpp
loadbody.hpp
loadbook.hpp
loadbsgn.hpp
loadcell.hpp
loadclas.hpp
loadclot.hpp
loadland.hpp
Updated code style in defs.hpp
This commit is contained in:
Nikolay Kasyanov 2011-04-06 20:11:08 +04:00
parent 4e7a056e9e
commit a2c42ab5a2
26 changed files with 942 additions and 762 deletions

View file

@ -131,6 +131,19 @@ set(ESM
${COMP_DIR}/esm/skill.cpp
${COMP_DIR}/esm/attr.cpp
${COMP_DIR}/esm/class.cpp
${COMP_DIR}/esm/esm_reader.cpp
${COMP_DIR}/esm/loadland.cpp
${COMP_DIR}/esm/loadacti.cpp
${COMP_DIR}/esm/loadalch.cpp
${COMP_DIR}/esm/loadappa.cpp
${COMP_DIR}/esm/loadarmo.cpp
${COMP_DIR}/esm/loadbody.cpp
${COMP_DIR}/esm/loadbook.cpp
${COMP_DIR}/esm/loadbsgn.cpp
${COMP_DIR}/esm/loadcell.cpp
${COMP_DIR}/esm/loadclas.cpp
${COMP_DIR}/esm/loadclot.cpp
)
source_group(components\\esm FILES ${ESM_HEADER} ${ESM})

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
// Pixel color value. Standard four-byte rr,gg,bb,aa format.
typedef int32_t Color;
@ -22,16 +23,12 @@ enum VarType
enum Specialization
{
SPC_Combat = 0,
SPC_Magic = 1,
SPC_Stealth = 2
SPC_Combat = 0, SPC_Magic = 1, SPC_Stealth = 2
};
enum RangeType
{
RT_Self = 0,
RT_Touch = 1,
RT_Target = 2
RT_Self = 0, RT_Touch = 1, RT_Target = 2
};
/** A list of references to spells and spell effects. This is shared

View file

@ -0,0 +1,345 @@
#include "esm_reader.hpp"
namespace ESM
{
ESM_Context ESMReader::getContext()
{
// Update the file position before returning
mCtx.filePos = mEsm->tell();
return mCtx;
}
void ESMReader::restoreContext(const ESM_Context &rc)
{
// Reopen the file if necessary
if (mCtx.filename != rc.filename)
openRaw(rc.filename);
// Copy the data
mCtx = rc;
// Make sure we seek to the right place
mEsm->seek(mCtx.filePos);
}
void ESMReader::close()
{
mEsm.reset();
mCtx.filename.clear();
mCtx.leftFile = 0;
mCtx.leftRec = 0;
mCtx.leftSub = 0;
mCtx.subCached = false;
mCtx.recName.val = 0;
mCtx.subName.val = 0;
}
void ESMReader::openRaw(Mangle::Stream::StreamPtr _esm, const std::string &name)
{
close();
mEsm = _esm;
mCtx.filename = name;
mCtx.leftFile = mEsm->size();
// Flag certain files for special treatment, based on the file
// name.
const char *cstr = mCtx.filename.c_str();
if (iends(cstr, "Morrowind.esm"))
mSpf = SF_Morrowind;
else if (iends(cstr, "Tribunal.esm"))
mSpf = SF_Tribunal;
else if (iends(cstr, "Bloodmoon.esm"))
mSpf = SF_Bloodmoon;
else
mSpf = SF_Other;
}
void ESMReader::open(Mangle::Stream::StreamPtr _esm, const std::string &name)
{
openRaw(_esm, name);
if (getRecName() != "TES3")
fail("Not a valid Morrowind file");
getRecHeader();
// Get the header
getHNT(mCtx.header, "HEDR", 300);
if (mCtx.header.version != VER_12 && mCtx.header.version != VER_13)
fail("Unsupported file format version");
while (isNextSub("MAST"))
{
MasterData m;
m.name = getHString();
m.size = getHNLong("DATA");
mMasters.push_back(m);
}
if (mCtx.header.type == FT_ESS)
{
// Savegame-related data
// Player position etc
getHNT(mSaveData, "GMDT", 124);
/* Image properties, five ints. Is always:
Red-mask: 0xff0000
Blue-mask: 0x00ff00
Green-mask: 0x0000ff
Alpha-mask: 0x000000
Bpp: 32
*/
getSubNameIs("SCRD");
skipHSubSize(20);
/* Savegame screenshot:
128x128 pixels * 4 bytes per pixel
*/
getSubNameIs("SCRS");
skipHSubSize(65536);
}
}
void ESMReader::open(const std::string &file)
{
using namespace Mangle::Stream;
open(StreamPtr(new FileStream(file)), file);
}
void ESMReader::openRaw(const std::string &file)
{
using namespace Mangle::Stream;
openRaw(StreamPtr(new FileStream(file)), file);
}
int64_t ESMReader::getHNLong(const char *name)
{
int64_t val;
getHNT(val, name);
return val;
}
std::string ESMReader::getHNOString(const char* name)
{
if (isNextSub(name))
return getHString();
return "";
}
std::string ESMReader::getHNString(const char* name)
{
getSubNameIs(name);
return getHString();
}
std::string ESMReader::getHString()
{
getSubHeader();
// Hack to make MultiMark.esp load. Zero-length strings do not
// occur in any of the official mods, but MultiMark makes use of
// them. For some reason, they break the rules, and contain a byte
// (value 0) even if the header says there is no data. If
// Morrowind accepts it, so should we.
if (mCtx.leftSub == 0)
{
// Skip the following zero byte
mCtx.leftRec--;
char c;
mEsm->read(&c, 1);
return "";
}
return getString(mCtx.leftSub);
}
void ESMReader::getHExact(void*p, int size)
{
getSubHeader();
if (size != static_cast<int> (mCtx.leftSub))
fail("getHExact() size mismatch");
getExact(p, size);
}
// Read the given number of bytes from a named subrecord
void ESMReader::getHNExact(void*p, int size, const char* name)
{
getSubNameIs(name);
getHExact(p, size);
}
// Get the next subrecord name and check if it matches the parameter
void ESMReader::getSubNameIs(const char* name)
{
getSubName();
if (mCtx.subName != name)
fail(
"Expected subrecord " + std::string(name) + " but got "
+ mCtx.subName.toString());
}
bool ESMReader::isNextSub(const char* name)
{
if (!mCtx.leftRec)
return false;
getSubName();
// If the name didn't match, then mark the it as 'cached' so it's
// available for the next call to getSubName.
mCtx.subCached = (mCtx.subName != name);
// If subCached is false, then subName == name.
return !mCtx.subCached;
}
// Read subrecord name. This gets called a LOT, so I've optimized it
// slightly.
void ESMReader::getSubName()
{
// If the name has already been read, do nothing
if (mCtx.subCached)
{
mCtx.subCached = false;
return;
}
// reading the subrecord data anyway.
mEsm->read(mCtx.subName.name, 4);
mCtx.leftRec -= 4;
}
bool ESMReader::isEmptyOrGetName()
{
if (mCtx.leftRec)
{
mEsm->read(mCtx.subName.name, 4);
mCtx.leftRec -= 4;
return false;
}
return true;
}
void ESMReader::skipHSub()
{
getSubHeader();
skip(mCtx.leftSub);
}
void ESMReader::skipHSubSize(int size)
{
skipHSub();
if (static_cast<int> (mCtx.leftSub) != size)
fail("skipHSubSize() mismatch");
}
void ESMReader::getSubHeader()
{
if (mCtx.leftRec < 4)
fail("End of record while reading sub-record header");
// Get subrecord size
getT(mCtx.leftSub);
// Adjust number of record bytes left
mCtx.leftRec -= mCtx.leftSub + 4;
}
void ESMReader::getSubHeaderIs(int size)
{
getSubHeader();
if (size != static_cast<int> (mCtx.leftSub))
fail("getSubHeaderIs(): Sub header mismatch");
}
NAME ESMReader::getRecName()
{
if (!hasMoreRecs())
fail("No more records, getRecName() failed");
getName(mCtx.recName);
mCtx.leftFile -= 4;
// Make sure we don't carry over any old cached subrecord
// names. This can happen in some cases when we skip parts of a
// record.
mCtx.subCached = false;
return mCtx.recName;
}
void ESMReader::skipRecord()
{
skip(mCtx.leftRec);
mCtx.leftRec = 0;
}
void ESMReader::skipHRecord()
{
if (!mCtx.leftFile)
return;
getRecHeader();
skipRecord();
}
void ESMReader::getRecHeader(uint32_t &flags)
{
// General error checking
if (mCtx.leftFile < 12)
fail("End of file while reading record header");
if (mCtx.leftRec)
fail("Previous record contains unread bytes");
getUint(mCtx.leftRec);
getUint(flags);// This header entry is always zero
getUint(flags);
mCtx.leftFile -= 12;
// Check that sizes add up
if (mCtx.leftFile < mCtx.leftRec)
fail("Record size is larger than rest of file");
// Adjust number of bytes mCtx.left in file
mCtx.leftFile -= mCtx.leftRec;
}
/*************************************************************************
*
* Lowest level data reading and misc methods
*
*************************************************************************/
void ESMReader::getExact(void*x, int size)
{
int t = mEsm->read(x, size);
if (t != size)
fail("Read error");
}
std::string ESMReader::getString(int size)
{
char *ptr = ToUTF8::getBuffer(size);
mEsm->read(ptr, size);
// Convert to UTF8 and return
return ToUTF8::getUtf8(ToUTF8::WINDOWS_1252);
}
void ESMReader::fail(const std::string &msg)
{
using namespace std;
stringstream ss;
ss << "ESM Error: " << msg;
ss << "\n File: " << mCtx.filename;
ss << "\n Record: " << mCtx.recName.toString();
ss << "\n Subrecord: " << mCtx.subName.toString();
if (mEsm != NULL)
ss << "\n Offset: 0x" << hex << mEsm->tell();
throw std::runtime_error(ss.str());
}
}

View file

@ -171,122 +171,27 @@ public:
/** Save the current file position and information in a ESM_Context
struct
*/
ESM_Context getContext()
{
// Update the file position before returning
mCtx.filePos = mEsm->tell();
return mCtx;
}
ESM_Context getContext();
/** Restore a previously saved context */
void restoreContext(const ESM_Context &rc)
{
// Reopen the file if necessary
if(mCtx.filename != rc.filename)
openRaw(rc.filename);
// Copy the data
mCtx = rc;
// Make sure we seek to the right place
mEsm->seek(mCtx.filePos);
}
void restoreContext(const ESM_Context &rc);
/** Close the file, resets all information. After calling close()
the structure may be reused to load a new file.
*/
void close()
{
mEsm.reset();
mCtx.filename.clear();
mCtx.leftFile = 0;
mCtx.leftRec = 0;
mCtx.leftSub = 0;
mCtx.subCached = false;
mCtx.recName.val = 0;
mCtx.subName.val = 0;
}
void close();
/// Raw opening. Opens the file and sets everything up but doesn't
/// parse the header.
void openRaw(Mangle::Stream::StreamPtr _esm, const std::string &name)
{
close();
mEsm = _esm;
mCtx.filename = name;
mCtx.leftFile = mEsm->size();
// Flag certain files for special treatment, based on the file
// name.
const char *cstr = mCtx.filename.c_str();
if(iends(cstr, "Morrowind.esm")) mSpf = SF_Morrowind;
else if(iends(cstr, "Tribunal.esm")) mSpf = SF_Tribunal;
else if(iends(cstr, "Bloodmoon.esm")) mSpf = SF_Bloodmoon;
else mSpf = SF_Other;
}
void openRaw(Mangle::Stream::StreamPtr _esm, const std::string &name);
/// Load ES file from a new stream, parses the header. Closes the
/// currently open file first, if any.
void open(Mangle::Stream::StreamPtr _esm, const std::string &name)
{
openRaw(_esm, name);
void open(Mangle::Stream::StreamPtr _esm, const std::string &name);
if(getRecName() != "TES3")
fail("Not a valid Morrowind file");
void open(const std::string &file);
getRecHeader();
// Get the header
getHNT(mCtx.header, "HEDR", 300);
if(mCtx.header.version != VER_12 &&
mCtx.header.version != VER_13)
fail("Unsupported file format version");
while(isNextSub("MAST"))
{
MasterData m;
m.name = getHString();
m.size = getHNLong("DATA");
mMasters.push_back(m);
}
if(mCtx.header.type == FT_ESS)
{
// Savegame-related data
// Player position etc
getHNT(mSaveData, "GMDT", 124);
/* Image properties, five ints. Is always:
Red-mask: 0xff0000
Blue-mask: 0x00ff00
Green-mask: 0x0000ff
Alpha-mask: 0x000000
Bpp: 32
*/
getSubNameIs("SCRD");
skipHSubSize(20);
/* Savegame screenshot:
128x128 pixels * 4 bytes per pixel
*/
getSubNameIs("SCRS");
skipHSubSize(65536);
}
}
void open(const std::string &file)
{
using namespace Mangle::Stream;
open(StreamPtr(new FileStream(file)), file);
}
void openRaw(const std::string &file)
{
using namespace Mangle::Stream;
openRaw(StreamPtr(new FileStream(file)), file);
}
void openRaw(const std::string &file);
/*************************************************************************
*
@ -320,12 +225,7 @@ public:
getHT(x);
}
int64_t getHNLong(const char *name)
{
int64_t val;
getHNT(val, name);
return val;
}
int64_t getHNLong(const char *name);
// Get data of a given type/size, including subrecord header
template <typename X>
@ -347,57 +247,19 @@ public:
}
// Read a string by the given name if it is the next record.
std::string getHNOString(const char* name)
{
if(isNextSub(name))
return getHString();
return "";
}
std::string getHNOString(const char* name);
// Read a string with the given sub-record name
std::string getHNString(const char* name)
{
getSubNameIs(name);
return getHString();
}
std::string getHNString(const char* name);
// Read a string, including the sub-record header (but not the name)
std::string getHString()
{
getSubHeader();
// Hack to make MultiMark.esp load. Zero-length strings do not
// occur in any of the official mods, but MultiMark makes use of
// them. For some reason, they break the rules, and contain a byte
// (value 0) even if the header says there is no data. If
// Morrowind accepts it, so should we.
if(mCtx.leftSub == 0)
{
// Skip the following zero byte
mCtx.leftRec--;
char c;
mEsm->read(&c,1);
return "";
}
return getString(mCtx.leftSub);
}
std::string getHString();
// Read the given number of bytes from a subrecord
void getHExact(void*p, int size)
{
getSubHeader();
if(size !=static_cast<int> (mCtx.leftSub))
fail("getHExact() size mismatch");
getExact(p,size);
}
void getHExact(void*p, int size);
// Read the given number of bytes from a named subrecord
void getHNExact(void*p, int size, const char* name)
{
getSubNameIs(name);
getHExact(p,size);
}
void getHNExact(void*p, int size, const char* name);
/*************************************************************************
*
@ -406,100 +268,37 @@ public:
*************************************************************************/
// Get the next subrecord name and check if it matches the parameter
void getSubNameIs(const char* name)
{
getSubName();
if(mCtx.subName != name)
fail("Expected subrecord " + std::string(name) + " but got " + mCtx.subName.toString());
}
void getSubNameIs(const char* name);
/** Checks if the next sub record name matches the parameter. If it
does, it is read into 'subName' just as if getSubName() was
called. If not, the read name will still be available for future
calls to getSubName(), isNextSub() and getSubNameIs().
*/
bool isNextSub(const char* name)
{
if(!mCtx.leftRec) return false;
getSubName();
// If the name didn't match, then mark the it as 'cached' so it's
// available for the next call to getSubName.
mCtx.subCached = (mCtx.subName != name);
// If subCached is false, then subName == name.
return !mCtx.subCached;
}
bool isNextSub(const char* name);
// Read subrecord name. This gets called a LOT, so I've optimized it
// slightly.
void getSubName()
{
// If the name has already been read, do nothing
if(mCtx.subCached)
{
mCtx.subCached = false;
return;
}
// Don't bother with error checking, we will catch an EOF upon
// reading the subrecord data anyway.
mEsm->read(mCtx.subName.name, 4);
mCtx.leftRec -= 4;
}
void getSubName();
// This is specially optimized for LoadINFO.
bool isEmptyOrGetName()
{
if(mCtx.leftRec)
{
mEsm->read(mCtx.subName.name, 4);
mCtx.leftRec -= 4;
return false;
}
return true;
}
bool isEmptyOrGetName();
// Skip current sub record, including header (but not including
// name.)
void skipHSub()
{
getSubHeader();
skip(mCtx.leftSub);
}
void skipHSub();
// Skip sub record and check its size
void skipHSubSize(int size)
{
skipHSub();
if(static_cast<int> (mCtx.leftSub) != size)
fail("skipHSubSize() mismatch");
}
void skipHSubSize(int size);
/* Sub-record header. This updates leftRec beyond the current
sub-record as well. leftSub contains size of current sub-record.
*/
void getSubHeader()
{
if(mCtx.leftRec < 4)
fail("End of record while reading sub-record header");
// Get subrecord size
getT(mCtx.leftSub);
// Adjust number of record bytes left
mCtx.leftRec -= mCtx.leftSub + 4;
}
void getSubHeader();
/** Get sub header and check the size
*/
void getSubHeaderIs(int size)
{
getSubHeader();
if(size != static_cast<int> (mCtx.leftSub))
fail("getSubHeaderIs(): Sub header mismatch");
}
void getSubHeaderIs(int size);
/*************************************************************************
*
@ -508,62 +307,21 @@ public:
*************************************************************************/
// Get the next record name
NAME getRecName()
{
if(!hasMoreRecs())
fail("No more records, getRecName() failed");
getName(mCtx.recName);
mCtx.leftFile -= 4;
// Make sure we don't carry over any old cached subrecord
// names. This can happen in some cases when we skip parts of a
// record.
mCtx.subCached = false;
return mCtx.recName;
}
NAME getRecName();
// Skip the rest of this record. Assumes the name and header have
// already been read
void skipRecord()
{
skip(mCtx.leftRec);
mCtx.leftRec = 0;
}
void skipRecord();
// Skip an entire record, including the header (but not the name)
void skipHRecord()
{
if(!mCtx.leftFile) return;
getRecHeader();
skipRecord();
}
void skipHRecord();
/* Read record header. This updatesleftFile BEYOND the data that
follows the header, ie beyond the entire record. You should use
leftRec to orient yourself inside the record itself.
*/
void getRecHeader() { uint32_t u; getRecHeader(u); }
void getRecHeader(uint32_t &flags)
{
// General error checking
if(mCtx.leftFile < 12)
fail("End of file while reading record header");
if(mCtx.leftRec)
fail("Previous record contains unread bytes");
getUint(mCtx.leftRec);
getUint(flags);// This header entry is always zero
getUint(flags);
mCtx.leftFile -= 12;
// Check that sizes add up
if(mCtx.leftFile < mCtx.leftRec)
fail("Record size is larger than rest of file");
// Adjust number of bytes mCtx.left in file
mCtx.leftFile -= mCtx.leftRec;
}
void getRecHeader(uint32_t &flags);
bool hasMoreRecs() { return mCtx.leftFile > 0; }
bool hasMoreSubs() { return mCtx.leftRec > 0; }
@ -578,44 +336,19 @@ public:
template <typename X>
void getT(X &x) { getExact(&x, sizeof(X)); }
void getExact(void*x, int size)
{
int t = mEsm->read(x, size);
if(t != size)
fail("Read error");
}
void getExact(void*x, int size);
void getName(NAME &name) { getT(name); }
void getUint(uint32_t &u) { getT(u); }
// Read the next 'size' bytes and return them as a string. Converts
// them from native encoding to UTF8 in the process.
std::string getString(int size)
{
char *ptr = ToUTF8::getBuffer(size);
mEsm->read(ptr,size);
// Convert to UTF8 and return
return ToUTF8::getUtf8(ToUTF8::WINDOWS_1252);
}
std::string getString(int size);
void skip(int bytes) { mEsm->seek(mEsm->tell()+bytes); }
uint64_t getOffset() { return mEsm->tell(); }
/// Used for error handling
void fail(const std::string &msg)
{
using namespace std;
stringstream ss;
ss << "ESM Error: " << msg;
ss << "\n File: " << mCtx.filename;
ss << "\n Record: " << mCtx.recName.toString();
ss << "\n Subrecord: " << mCtx.subName.toString();
if(mEsm != NULL)
ss << "\n Offset: 0x" << hex << mEsm->tell();
throw std::runtime_error(ss.str());
}
void fail(const std::string &msg);
private:
Mangle::Stream::StreamPtr mEsm;

View file

@ -0,0 +1,11 @@
#include "loadacti.hpp"
namespace ESM
{
void Activator::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
script = esm.getHNOString("SCRI");
}
}

View file

@ -3,18 +3,14 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
struct Activator
{
std::string name, script, model;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
script = esm.getHNOString("SCRI");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,14 @@
#include "loadalch.hpp"
namespace ESM
{
void Potion::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
icon = esm.getHNOString("TEXT"); // not ITEX here for some reason
script = esm.getHNOString("SCRI");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "ALDT", 12);
effects.load(esm);
}
}

View file

@ -4,7 +4,8 @@
#include "esm_reader.hpp"
#include "defs.hpp"
namespace ESM {
namespace ESM
{
/*
* Alchemy item (potions)
@ -23,15 +24,7 @@ struct Potion
std::string name, model, icon, script;
EffectList effects;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
icon = esm.getHNOString("TEXT"); // not ITEX here for some reason
script = esm.getHNOString("SCRI");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "ALDT", 12);
effects.load(esm);
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,13 @@
#include "loadappa.hpp"
namespace ESM
{
void Apparatus::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
esm.getHNT(data, "AADT", 16);
script = esm.getHNOString("SCRI");
icon = esm.getHNString("ITEX");
}
}

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
/*
* Alchemist apparatus
@ -13,10 +14,7 @@ struct Apparatus
{
enum AppaType
{
MortarPestle = 0,
Albemic = 1,
Calcinator = 2,
Retort = 3
MortarPestle = 0, Albemic = 1, Calcinator = 2, Retort = 3
};
struct AADTstruct
@ -30,14 +28,7 @@ struct Apparatus
AADTstruct data;
std::string model, icon, script, name;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
esm.getHNT(data, "AADT", 16);
script = esm.getHNOString("SCRI");
icon = esm.getHNString("ITEX");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,28 @@
#include "loadarmo.hpp"
namespace ESM
{
void PartReferenceList::load(ESMReader &esm)
{
while (esm.isNextSub("INDX"))
{
PartReference pr;
esm.getHT(pr.part); // The INDX byte
pr.male = esm.getHNOString("BNAM");
pr.female = esm.getHNOString("CNAM");
}
}
void Armor::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
script = esm.getHNOString("SCRI");
esm.getHNT(data, "AODT", 24);
icon = esm.getHNOString("ITEX");
parts.load(esm);
enchant = esm.getHNOString("ENAM");
}
}

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
enum PartReferenceType
{
@ -48,16 +49,7 @@ struct PartReferenceList
{
std::vector<PartReference> parts;
void load(ESMReader &esm)
{
while(esm.isNextSub("INDX"))
{
PartReference pr;
esm.getHT(pr.part); // The INDX byte
pr.male = esm.getHNOString("BNAM");
pr.female = esm.getHNOString("CNAM");
}
}
void load(ESMReader &esm);
};
struct Armor
@ -89,16 +81,7 @@ struct Armor
std::string name, model, icon, script, enchant;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
script = esm.getHNOString("SCRI");
esm.getHNT(data, "AODT", 24);
icon = esm.getHNOString("ITEX");
parts.load(esm);
enchant = esm.getHNOString("ENAM");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,13 @@
#include "loadbody.hpp"
namespace ESM
{
void BodyPart::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
esm.getHNT(data, "BYDT", 4);
}
}

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
struct BodyPart
{
@ -28,15 +29,12 @@ struct BodyPart
enum Flags
{
BPF_Female = 1,
BPF_Playable = 2
BPF_Female = 1, BPF_Playable = 2
};
enum MeshType
{
MT_Skin = 0,
MT_Clothing = 1,
MT_Armor = 2
MT_Skin = 0, MT_Clothing = 1, MT_Armor = 2
};
struct BYDTstruct
@ -50,12 +48,7 @@ struct BodyPart
BYDTstruct data;
std::string model, name;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNString("FNAM");
esm.getHNT(data, "BYDT", 4);
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,17 @@
#include "loadbook.hpp"
namespace ESM
{
void Book::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "BKDT", 20);
script = esm.getHNOString("SCRI");
icon = esm.getHNOString("ITEX");
text = esm.getHNOString("TEXT");
enchant = esm.getHNOString("ENAM");
}
}

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
/*
* Books, magic scrolls, notes and so on
@ -20,16 +21,7 @@ struct Book
BKDTstruct data;
std::string name, model, icon, script, enchant, text;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "BKDT", 20);
script = esm.getHNOString("SCRI");
icon = esm.getHNOString("ITEX");
text = esm.getHNOString("TEXT");
enchant = esm.getHNOString("ENAM");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,15 @@
#include "loadbsgn.hpp"
namespace ESM
{
void BirthSign::load(ESMReader &esm)
{
name = esm.getHNString("FNAM");
texture = esm.getHNOString("TNAM");
description = esm.getHNOString("DESC");
powers.load(esm);
}
}

View file

@ -4,7 +4,8 @@
#include "defs.hpp"
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
struct BirthSign
{
@ -13,14 +14,7 @@ struct BirthSign
// List of powers and abilities that come with this birth sign.
SpellList powers;
void load(ESMReader &esm)
{
name = esm.getHNString("FNAM");
texture = esm.getHNOString("TNAM");
description = esm.getHNOString("DESC");
powers.load(esm);
};
void load(ESMReader &esm);
};
}
#endif

113
components/esm/loadcell.cpp Normal file
View file

@ -0,0 +1,113 @@
#include "loadcell.hpp"
namespace ESM
{
void Cell::load(ESMReader &esm)
{
// Ignore this for now, it might mean we should delete the entire
// cell?
if (esm.isNextSub("DELE"))
esm.skipHSub();
esm.getHNT(data, "DATA", 12);
// Water level
water = 0;
if (data.flags & Interior)
{
// Interior cells
if (esm.isNextSub("INTV") || esm.isNextSub("WHGT"))
esm.getHT(water);
// Quasi-exterior cells have a region (which determines the
// weather), pure interior cells have ambient lighting
// instead.
if (data.flags & QuasiEx)
region = esm.getHNOString("RGNN");
else
esm.getHNT(ambi, "AMBI", 16);
}
else
{
// Exterior cells
region = esm.getHNOString("RGNN");
esm.getHNOT(mapColor, "NAM5");
}
// Save position of the cell references and move on
context = esm.getContext();
esm.skipRecord();
}
void Cell::restore(ESMReader &esm) const
{
esm.restoreContext(context);
}
bool Cell::getNextRef(ESMReader &esm, CellRef &ref)
{
if (!esm.hasMoreSubs())
return false;
// Number of references in the cell? Maximum once in each cell,
// but not always at the beginning, and not always right. In other
// words, completely useless.
{
int i;
esm.getHNOT(i, "NAM0");
}
esm.getHNT(ref.refnum, "FRMR");
ref.refID = esm.getHNString("NAME");
// getHNOT will not change the existing value if the subrecord is
// missing
ref.scale = 1.0;
esm.getHNOT(ref.scale, "XSCL");
ref.owner = esm.getHNOString("ANAM");
ref.glob = esm.getHNOString("BNAM");
ref.soul = esm.getHNOString("XSOL");
ref.faction = esm.getHNOString("CNAM");
ref.factIndex = -1;
esm.getHNOT(ref.factIndex, "INDX");
ref.charge = -1.0;
esm.getHNOT(ref.charge, "XCHG");
ref.intv = 0;
ref.nam9 = 0;
esm.getHNOT(ref.intv, "INTV");
esm.getHNOT(ref.nam9, "NAM9");
// Present for doors that teleport you to another cell.
if (esm.isNextSub("DODT"))
{
ref.teleport = true;
esm.getHT(ref.doorDest);
ref.destCell = esm.getHNOString("DNAM");
}
else
ref.teleport = false;
// Integer, despite the name suggesting otherwise
ref.lockLevel = 0;
esm.getHNOT(ref.lockLevel, "FLTV");
ref.key = esm.getHNOString("KNAM");
ref.trap = esm.getHNOString("TNAM");
ref.unam = 0;
ref.fltv = 0;
esm.getHNOT(ref.unam, "UNAM");
esm.getHNOT(ref.fltv, "FLTV");
esm.getHNT(ref.pos, "DATA", 24);
return true;
}
}

View file

@ -117,51 +117,14 @@ struct Cell
int water; // Water level
int mapColor;
void load(ESMReader &esm)
{
// Ignore this for now, it might mean we should delete the entire
// cell?
if(esm.isNextSub("DELE")) esm.skipHSub();
esm.getHNT(data, "DATA", 12);
// Water level
water = 0;
if(data.flags & Interior)
{
// Interior cells
if(esm.isNextSub("INTV") || esm.isNextSub("WHGT"))
esm.getHT(water);
// Quasi-exterior cells have a region (which determines the
// weather), pure interior cells have ambient lighting
// instead.
if(data.flags & QuasiEx)
region = esm.getHNOString("RGNN");
else
esm.getHNT(ambi, "AMBI", 16);
}
else
{
// Exterior cells
region = esm.getHNOString("RGNN");
esm.getHNOT(mapColor, "NAM5");
}
// Save position of the cell references and move on
context = esm.getContext();
esm.skipRecord();
}
void load(ESMReader &esm);
// Restore the given reader to the stored position. Will try to open
// the file matching the stored file name. If you want to read from
// somewhere other than the file system, you need to pre-open the
// ESMReader, and the filename must match the stored filename
// exactly.
void restore(ESMReader &esm) const
{ esm.restoreContext(context); }
void restore(ESMReader &esm) const;
/* Get the next reference in this cell, if any. Returns false when
there are no more references in the cell.
@ -169,66 +132,7 @@ struct Cell
All fields of the CellRef struct are overwritten. You can safely
reuse one memory location without blanking it between calls.
*/
static bool getNextRef(ESMReader &esm, CellRef &ref)
{
if(!esm.hasMoreSubs()) return false;
// Number of references in the cell? Maximum once in each cell,
// but not always at the beginning, and not always right. In other
// words, completely useless.
{
int i;
esm.getHNOT(i, "NAM0");
}
esm.getHNT(ref.refnum, "FRMR");
ref.refID = esm.getHNString("NAME");
// getHNOT will not change the existing value if the subrecord is
// missing
ref.scale = 1.0;
esm.getHNOT(ref.scale, "XSCL");
ref.owner = esm.getHNOString("ANAM");
ref.glob = esm.getHNOString("BNAM");
ref.soul = esm.getHNOString("XSOL");
ref.faction = esm.getHNOString("CNAM");
ref.factIndex = -1;
esm.getHNOT(ref.factIndex, "INDX");
ref.charge = -1.0;
esm.getHNOT(ref.charge, "XCHG");
ref.intv = 0;
ref.nam9 = 0;
esm.getHNOT(ref.intv, "INTV");
esm.getHNOT(ref.nam9, "NAM9");
// Present for doors that teleport you to another cell.
if(esm.isNextSub("DODT"))
{
ref.teleport = true;
esm.getHT(ref.doorDest);
ref.destCell = esm.getHNOString("DNAM");
}
else ref.teleport = false;
// Integer, despite the name suggesting otherwise
ref.lockLevel = 0;
esm.getHNOT(ref.lockLevel, "FLTV");
ref.key = esm.getHNOString("KNAM");
ref.trap = esm.getHNOString("TNAM");
ref.unam = 0;
ref.fltv = 0;
esm.getHNOT(ref.unam, "UNAM");
esm.getHNOT(ref.fltv, "FLTV");
esm.getHNT(ref.pos, "DATA", 24);
return true;
}
static bool getNextRef(ESMReader &esm, CellRef &ref);
};
}
#endif

View file

@ -0,0 +1,17 @@
#include "loadclas.hpp"
namespace ESM
{
void Class::load(ESMReader &esm)
{
name = esm.getHNString("FNAM");
esm.getHNT(data, "CLDT", 60);
if (data.isPlayable > 1)
esm.fail("Unknown bool value");
description = esm.getHNOString("DESC");
}
}

View file

@ -3,7 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
/*
* Character class definitions
@ -37,9 +38,7 @@ struct Class
enum Specialization
{
Combat = 0,
Magic = 1,
Stealth = 2
Combat = 0, Magic = 1, Stealth = 2
};
static const Specialization specializationIds[3];
@ -59,16 +58,7 @@ struct Class
std::string name, description;
CLDTstruct data;
void load(ESMReader &esm)
{
name = esm.getHNString("FNAM");
esm.getHNT(data, "CLDT", 60);
if(data.isPlayable > 1)
esm.fail("Unknown bool value");
description = esm.getHNOString("DESC");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,20 @@
#include "loadclot.hpp"
namespace ESM
{
void Clothing::load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "CTDT", 12);
script = esm.getHNOString("SCRI");
icon = esm.getHNOString("ITEX");
parts.load(esm);
enchant = esm.getHNOString("ENAM");
}
}

View file

@ -4,7 +4,8 @@
#include "esm_reader.hpp"
#include "loadarmo.hpp"
namespace ESM {
namespace ESM
{
/*
* Clothing
@ -39,19 +40,7 @@ struct Clothing
std::string name, model, icon, enchant, script;
void load(ESMReader &esm)
{
model = esm.getHNString("MODL");
name = esm.getHNOString("FNAM");
esm.getHNT(data, "CTDT", 12);
script = esm.getHNOString("SCRI");
icon = esm.getHNOString("ITEX");
parts.load(esm);
enchant = esm.getHNOString("ENAM");
}
void load(ESMReader &esm);
};
}
#endif

View file

@ -0,0 +1,32 @@
#include "loadland.hpp"
namespace ESM
{
void Land::load(ESMReader &esm)
{
// Get the grid location
esm.getSubNameIs("INTV");
esm.getSubHeaderIs(8);
esm.getT<int>(X);
esm.getT<int>(Y);
esm.getHNT(flags, "DATA");
// Store the file position
context = esm.getContext();
hasData = false;
int cnt = 0;
// Skip these here. Load the actual data when the cell is loaded.
if(esm.isNextSub("VNML")) {esm.skipHSubSize(12675);cnt++;}
if(esm.isNextSub("VHGT")) {esm.skipHSubSize(4232);cnt++;}
if(esm.isNextSub("WNAM")) esm.skipHSubSize(81);
if(esm.isNextSub("VCLR")) esm.skipHSubSize(12675);
if(esm.isNextSub("VTEX")) {esm.skipHSubSize(512);cnt++;}
// We need all three of VNML, VHGT and VTEX in order to use the
// landscape.
hasData = (cnt == 3);
}
}

View file

@ -3,8 +3,8 @@
#include "esm_reader.hpp"
namespace ESM {
namespace ESM
{
/*
* Landscape data.
*/
@ -21,33 +21,7 @@ struct Land
bool hasData;
void load(ESMReader &esm)
{
// Get the grid location
esm.getSubNameIs("INTV");
esm.getSubHeaderIs(8);
esm.getT<int>(X);
esm.getT<int>(Y);
esm.getHNT(flags, "DATA");
// Store the file position
context = esm.getContext();
hasData = false;
int cnt = 0;
// Skip these here. Load the actual data when the cell is loaded.
if(esm.isNextSub("VNML")) {esm.skipHSubSize(12675);cnt++;}
if(esm.isNextSub("VHGT")) {esm.skipHSubSize(4232);cnt++;}
if(esm.isNextSub("WNAM")) esm.skipHSubSize(81);
if(esm.isNextSub("VCLR")) esm.skipHSubSize(12675);
if(esm.isNextSub("VTEX")) {esm.skipHSubSize(512);cnt++;}
// We need all three of VNML, VHGT and VTEX in order to use the
// landscape.
hasData = (cnt == 3);
}
void load(ESMReader &esm);
};
}
#endif