Odamex
Setting the Standard in Multiplayer Doom
common/farchive.h
Go to the documentation of this file.
00001 // Emacs style mode select   -*- C++ -*- 
00002 //-----------------------------------------------------------------------------
00003 //
00004 // $Id: farchive.h 1788 2010-08-24 04:42:57Z russellrice $
00005 //
00006 // Copyright (C) 1998-2006 by Randy Heit (ZDoom).
00007 // Copyright (C) 2006-2010 by The Odamex Team.
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // DESCRIPTION:
00020 //      FARCHIVE
00021 //
00022 //-----------------------------------------------------------------------------
00023 
00024 
00025 #ifndef __DARCHIVE_H__
00026 #define __DARCHIVE_H__
00027 
00028 #include "doomtype.h"
00029 #include "dobject.h"
00030 
00031 #include <string>
00032 
00033 class DObject;
00034 
00035 class FFile
00036 {
00037 public:
00038                 enum EOpenMode
00039                 {
00040                         EReading,
00041                         EWriting
00042                 };
00043 
00044                 enum ESeekPos
00045                 {
00046                         ESeekSet,
00047                         ESeekRelative,
00048                         ESeekEnd
00049                 };
00050 
00051 virtual ~FFile () {}
00052 
00053 virtual bool Open (const char *name, EOpenMode mode) = 0;
00054 virtual void Close () = 0;
00055 virtual void Flush () = 0;
00056 virtual EOpenMode Mode () const = 0;
00057 virtual bool IsPersistent () const = 0;
00058 virtual bool IsOpen () const = 0;
00059 
00060 virtual FFile& Write (const void *, unsigned int) = 0;
00061 virtual FFile& Read (void *, unsigned int) = 0;
00062 
00063 virtual unsigned int Tell () const = 0;
00064 virtual FFile& Seek (int, ESeekPos) = 0;
00065 inline  FFile& Seek (unsigned int i, ESeekPos p) { return Seek ((int)i, p); }
00066 };
00067 
00068 class FLZOFile : public FFile
00069 {
00070 public:
00071         FLZOFile ();
00072         FLZOFile (const char *name, EOpenMode mode, bool dontcompress = false);
00073         FLZOFile (FILE *file, EOpenMode mode, bool dontcompress = false);
00074         ~FLZOFile ();
00075 
00076         bool Open (const char *name, EOpenMode mode);
00077         void Close ();
00078         void Flush ();
00079         EOpenMode Mode () const;
00080         bool IsPersistent () const { return true; }
00081         bool IsOpen () const;
00082 
00083         FFile &Write (const void *, unsigned int);
00084         FFile &Read (void *, unsigned int);
00085         unsigned int Tell () const;
00086         FFile &Seek (int, ESeekPos);
00087 
00088 protected:
00089         unsigned int m_Pos;
00090         unsigned int m_BufferSize;
00091         unsigned int m_MaxBufferSize;
00092         unsigned char *m_Buffer;
00093         bool m_NoCompress;
00094         EOpenMode m_Mode;
00095         FILE *m_File;
00096 
00097         void Implode ();
00098         void Explode ();
00099         virtual bool FreeOnExplode () { return true; }
00100 
00101 private:
00102         void BeEmpty ();
00103         void PostOpen ();
00104 };
00105 
00106 class FLZOMemFile : public FLZOFile
00107 {
00108 public:
00109         FLZOMemFile ();
00110         FLZOMemFile (FILE *file);       // Create for reading
00111 
00112         bool Open (const char *name, EOpenMode mode);   // Works for reading only
00113         bool Open (void *memblock);     // Open for reading only
00114         bool Open ();   // Open for writing only
00115         bool Reopen (); // Re-opens imploded file for reading only
00116         void Close ();
00117         bool IsOpen () const;
00118 
00119         void Serialize (FArchive &arc);
00120 
00121 protected:
00122         bool FreeOnExplode () { return !m_SourceFromMem; }
00123 
00124 private:
00125         bool m_SourceFromMem;
00126         unsigned char *m_ImplodedBuffer;
00127 };
00128 
00129 class FArchive
00130 {
00131 public:
00132                 FArchive (FFile &file);
00133                 virtual ~FArchive ();
00134 
00135                 inline bool IsLoading () const { return m_Loading; }
00136                 inline bool IsStoring () const { return m_Storing; }
00137                 inline bool IsPeristent () const { return m_Persistent; }
00138                 
00139                 void SetHubTravel () { m_HubTravel = true; }
00140 
00141                 void Close ();
00142 
00143 virtual void Write (const void *mem, unsigned int len);
00144 virtual void Read (void *mem, unsigned int len);
00145 
00146                 void WriteCount (DWORD count);
00147                 DWORD ReadCount ();
00148 
00149                 FArchive& operator<< (BYTE c);
00150                 FArchive& operator<< (WORD s);
00151                 FArchive& operator<< (DWORD i);
00152                 FArchive& operator<< (QWORD i);
00153                 FArchive& operator<< (float f);
00154                 FArchive& operator<< (double d);
00155                 FArchive& operator<< (const char *str);
00156                 FArchive& operator<< (DObject *obj);
00157 
00158 inline  FArchive& operator<< (char c) { return operator<< ((BYTE)c); }
00159 inline  FArchive& operator<< (SBYTE c) { return operator<< ((BYTE)c); }
00160 inline  FArchive& operator<< (SWORD s) { return operator<< ((WORD)s); }
00161 inline  FArchive& operator<< (SDWORD i) { return operator<< ((DWORD)i); }
00162 inline  FArchive& operator<< (SQWORD i) { return operator<< ((QWORD)i); }
00163 inline  FArchive& operator<< (const unsigned char *str) { return operator<< ((const char *)str); }
00164 inline  FArchive& operator<< (const signed char *str) { return operator<< ((const char *)str); }
00165 inline  FArchive& operator<< (bool b) { return operator<< ((BYTE)b); }
00166 
00167 #ifdef WIN32
00168 inline  FArchive& operator<< (int i) { return operator<< ((SDWORD)i); }
00169 inline  FArchive& operator<< (unsigned int i) { return operator<< ((DWORD)i); }
00170 #endif
00171 
00172                 FArchive& operator>> (BYTE &c);
00173                 FArchive& operator>> (WORD &s);
00174                 FArchive& operator>> (DWORD &i);
00175                 FArchive& operator>> (QWORD &i);
00176                 FArchive& operator>> (float &f);
00177                 FArchive& operator>> (double &d);
00178                 FArchive& operator>> (std::string &s);
00179                 FArchive& ReadObject (DObject *&obj, TypeInfo *wanttype);
00180 
00181 inline  FArchive& operator>> (char &c) { return operator>> ((BYTE &)c); }
00182 inline  FArchive& operator>> (SBYTE &c) { return operator>> ((BYTE &)c); }
00183 inline  FArchive& operator>> (SWORD &s) { return operator>> ((WORD &)s); }
00184 inline  FArchive& operator>> (SDWORD &i) { return operator>> ((DWORD &)i); }
00185 inline  FArchive& operator>> (SQWORD &i) { return operator>> ((QWORD &)i); }
00186 //inline        FArchive& operator>> (unsigned char *&str) { return operator>> ((char *&)str); }
00187 //inline        FArchive& operator>> (signed char *&str) { return operator>> ((char *&)str); }
00188 inline  FArchive& operator>> (bool &b) { return operator>> ((BYTE &)b); }
00189 inline  FArchive& operator>> (DObject* &object) { return ReadObject (object, RUNTIME_CLASS(DObject)); }
00190 
00191 #ifdef WIN32
00192 inline  FArchive& operator>> (int &i) { return operator>> ((DWORD &)i); }
00193 inline  FArchive& operator>> (unsigned int &i) { return operator>> ((DWORD &)i); }
00194 #endif
00195 
00196 protected:
00197                 enum { EObjectHashSize = 137 };
00198 
00199                 DWORD FindObjectIndex (const DObject *obj) const;
00200                 DWORD MapObject (const DObject *obj);
00201                 DWORD WriteClass (const TypeInfo *info);
00202                 const TypeInfo *ReadClass ();
00203                 const TypeInfo *ReadClass (const TypeInfo *wanttype);
00204                 const TypeInfo *ReadStoredClass (const TypeInfo *wanttype);
00205                 DWORD HashObject (const DObject *obj) const;
00206 
00207                 bool m_Persistent;              // meant for persistent storage (disk)?
00208                 bool m_Loading;                 // extracting objects?
00209                 bool m_Storing;                 // inserting objects?
00210                 bool m_HubTravel;               // travelling inside a hub?
00211                 FFile *m_File;                  // unerlying file object
00212                 DWORD m_ObjectCount;    // # of objects currently serialized
00213                 DWORD m_MaxObjectCount;
00214                 DWORD m_ClassCount;             // # of unique classes currently serialized
00215                 struct TypeMap
00216                 {
00217                         const TypeInfo *toCurrent;      // maps archive type index to execution type index
00218                         DWORD toArchive;                // maps execution type index to archive type index
00219                 } *m_TypeMap;
00220                 struct ObjectMap
00221                 {
00222                         const DObject *object;
00223                         size_t hashNext;
00224                 } *m_ObjectMap;
00225                 size_t m_ObjectHash[EObjectHashSize];
00226 
00227 private:
00228                 FArchive (const FArchive &src) {}
00229                 void operator= (const FArchive &src) {}
00230 };
00231 
00232 class player_s;
00233 
00234 FArchive &operator<< (FArchive &arc, player_s *p);
00235 FArchive &operator>> (FArchive &arc, player_s *&p);
00236 
00237 #endif //__DARCHIVE_H__
00238 
00239 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends