OpenMW
components/esm/esmcommon.hpp
Go to the documentation of this file.
00001 #ifndef OPENMW_ESM_COMMON_H
00002 #define OPENMW_ESM_COMMON_H
00003 
00004 #include <string>
00005 #include <cstring>
00006 
00007 #include <libs/platform/stdint.h>
00008 #include <libs/platform/string.h>
00009 
00010 namespace ESM
00011 {
00012 enum Version
00013   {
00014     VER_12 = 0x3f99999a,
00015     VER_13 = 0x3fa66666
00016   };
00017 
00018 /* A structure used for holding fixed-length strings. In the case of
00019    LEN=4, it can be more efficient to match the string as a 32 bit
00020    number, therefore the struct is implemented as a union with an int.
00021  */
00022 template <int LEN>
00023 union NAME_T
00024 {
00025     char name[LEN];
00026     int32_t val;
00027 
00028   bool operator==(const char *str) const
00029   {
00030     for(int i=0; i<LEN; i++)
00031       if(name[i] != str[i]) return false;
00032       else if(name[i] == 0) return true;
00033     return str[LEN] == 0;
00034   }
00035   bool operator!=(const char *str) const { return !((*this)==str); }
00036 
00037   bool operator==(const std::string &str) const
00038   {
00039     return (*this) == str.c_str();
00040   }
00041   bool operator!=(const std::string &str) const { return !((*this)==str); }
00042 
00043   bool operator==(int v) const { return v == val; }
00044   bool operator!=(int v) const { return v != val; }
00045 
00046   std::string toString() const { return std::string(name, strnlen(name, LEN)); }
00047 
00048   void assign (const std::string& value) { std::strncpy (name, value.c_str(), LEN); }
00049 };
00050 
00051 typedef NAME_T<4> NAME;
00052 typedef NAME_T<32> NAME32;
00053 typedef NAME_T<64> NAME64;
00054 typedef NAME_T<256> NAME256;
00055 
00056 #pragma pack(push)
00057 #pragma pack(1)
00058 // Data that is only present in save game files
00059 struct SaveData
00060 {
00061   float pos[6];     // Player position and rotation
00062   NAME64 cell;      // Cell name
00063   float unk2;       // Unknown value - possibly game time?
00064   NAME32 player;    // Player name
00065 };
00066 #pragma pack(pop)
00067 
00068 /* This struct defines a file 'context' which can be saved and later
00069    restored by an ESMReader instance. It will save the position within
00070    a file, and when restored will let you read from that position as
00071    if you never left it.
00072  */
00073 struct ESM_Context
00074 {
00075   std::string filename;
00076   uint32_t leftRec, leftSub;
00077   size_t leftFile;
00078   NAME recName, subName;
00079   // When working with multiple esX files, we will generate lists of all files that
00080   //  actually contribute to a specific cell. Therefore, we need to store the index
00081   //  of the file belonging to this contest. See CellStore::(list/load)refs for details.
00082   int index;
00083 
00084   // True if subName has been read but not used.
00085   bool subCached;
00086 
00087   // File position. Only used for stored contexts, not regularly
00088   // updated within the reader itself.
00089   size_t filePos;
00090 };
00091 
00092 }
00093 
00094 #endif