OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Group.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Handles group files */
19 
20 #ifndef INC_C4Group
21 #define INC_C4Group
22 
23 #ifdef HAVE_IO_H
24 #include <io.h>
25 #endif
26 #include "c4group/CStdFile.h"
27 #include "lib/StdBuf.h"
28 
29 // C4Group-Rewind-warning:
30 // The current C4Group-implementation cannot handle random file access very well,
31 // because all files are written within a single zlib-stream.
32 // For every out-of-order-file accessed a group-rewind must be performed, and every
33 // single file up to the accessed file unpacked. As a workaround, all C4Groups are
34 // packed in a file order matching the reading order of the engine.
35 // If the reading order doesn't match the packing order, and a rewind has to be performed,
36 // a warning is issued in Debug-builds of the engine. But since some components require
37 // random access because they are loaded on-demand at runtime (e.g. global sounds), the
38 // warning may be temp disabled for those files using C4GRP_DISABLE_REWINDWARN and
39 // C4GRP_ENABLE_REWINDWARN. A ref counter keeps track of nested calls to those functions.
40 //
41 // If you add any new components to scenario or definition files, remember to adjust the
42 // sort order lists in C4Components.h accordingly, and enforce a reading order for that
43 // component.
44 //
45 // Maybe some day, someone will write a C4Group-implementation that is probably capable of
46 // random access...
47 #ifdef _DEBUG
48 extern int iC4GroupRewindFilePtrNoWarn;
49 #define C4GRP_DISABLE_REWINDWARN ++iC4GroupRewindFilePtrNoWarn;
50 #define C4GRP_ENABLE_REWINDWARN --iC4GroupRewindFilePtrNoWarn;
51 #else
52 #define C4GRP_DISABLE_REWINDWARN ;
53 #define C4GRP_ENABLE_REWINDWARN ;
54 #endif
55 
57 
58 const int C4GroupMaxError = 100;
59 
60 const int32_t C4GroupSwapThreshold = 10 * 1024 * 1024;
61 
62 #define C4GroupFileID "RedWolf Design GrpFolder"
63 
64 bool C4Group_TestIgnore(const char *szFilename);
65 void C4Group_SetTempPath(const char *szPath);
66 const char* C4Group_GetTempPath();
67 void C4Group_SetSortList(const char **ppSortList);
68 void C4Group_SetProcessCallback(bool (*fnCallback)(const char *, int));
69 bool C4Group_IsGroup(const char *szFilename);
70 bool C4Group_CopyItem(const char *szSource, const char *szTarget, bool fNoSort=false, bool fResetAttributes=false);
71 bool C4Group_MoveItem(const char *szSource, const char *szTarget, bool fNoSort=false);
72 bool C4Group_DeleteItem(const char *szItem, bool fRecycle=false);
73 bool C4Group_PackDirectoryTo(const char *szFilename, const char *szFilenameTo);
74 bool C4Group_PackDirectory(const char *szFilename);
75 bool C4Group_UnpackDirectory(const char *szFilename);
76 bool C4Group_ExplodeDirectory(const char *szFilename);
77 bool C4Group_ReadFile(const char *szFilename, char **pData, size_t *iSize);
78 
79 extern const char *C4CFN_FLS[];
80 
81 #pragma pack (push, 1)
82 
84 {
85  char id[24+4] = C4GroupFileID;
88  int Entries = 0;
89  char reserved[164] = { 0 };
90 };
91 
93 {
94  char FileName[260] = { 0 };
95  int32_t Packed = 0, ChildGroup = 0;
96  int32_t Size = 0, reserved1 = 0, Offset = 0;
97  int32_t reserved2 = 0;
98  char reserved3 = '\0';
99  unsigned int reserved4 = 0;
100  char Executable = '\0';
101  BYTE fbuf[26] = { 0 };
102 };
103 
104 #pragma pack (pop)
105 
107 {
108 public:
109  ~C4GroupEntry();
110 
112  {
117  };
118 
119 public:
120  char DiskPath[_MAX_PATH + 1] = { 0 };
122  bool DeleteOnDisk = false;
123  bool HoldBuffer = false;
124  bool BufferIsStdbuf = false;
125  bool NoSort = false;
128 public:
129  void Set(const DirectoryIterator & iter, const char * szPath);
130 };
131 
132 class C4Group : public CStdStream
133 {
134  struct P;
135  std::unique_ptr<P> p;
136 public:
137  C4Group();
138  ~C4Group();
139  C4Group(C4Group &&) = default;
140  C4Group &operator=(C4Group &&) = default;
141 
142 protected:
143  // C4Update requires these to be available by a subclass (C4GroupEx)
145  C4GroupEntry *GetEntry(const char *szName);
146  void Clear();
147 
148 public:
149  bool Open(const char *szGroupName, bool fCreate=false);
150  bool Close();
151  bool Save(bool fReOpen);
152  bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false);
153  bool OpenChild(const char* strEntry);
154  bool OpenMother();
155  bool Add(const char *szFile, const char *szAddAs);
156  bool Add(const char *szName, void *pBuffer, int iSize, bool fChild = false, bool fHoldBuffer = false, bool fExecutable = false);
157  bool Add(const char *szName, StdBuf &pBuffer, bool fChild = false, bool fHoldBuffer = false, bool fExecutable = false);
158  bool Add(const char *szName, StdStrBuf &pBuffer, bool fChild = false, bool fHoldBuffer = false, bool fExecutable = false);
159  bool Merge(const char *szFolders);
160  bool Move(const char *szFile, const char *szAddAs);
161  bool Extract(const char *szFiles, const char *szExtractTo=nullptr, const char *szExclude=nullptr);
162  bool ExtractEntry(const char *szFilename, const char *szExtractTo=nullptr);
163  bool Delete(const char *szFiles, bool fRecursive = false);
164  bool DeleteEntry(const char *szFilename, bool fRecycle=false);
165  bool Rename(const char *szFile, const char *szNewName);
166  bool Sort(const char *szSortList);
167  bool SortByList(const char **ppSortList, const char *szFilename=nullptr);
168  bool AccessEntry(const char *szWildCard,
169  size_t *iSize=nullptr, char *sFileName=nullptr,
170  bool NeedsToBeAGroup = false);
171  bool AccessNextEntry(const char *szWildCard,
172  size_t *iSize=nullptr, char *sFileName=nullptr,
173  bool fStartAtFilename=false);
174  bool LoadEntry(const char *szEntryName, char **lpbpBuf,
175  size_t *ipSize=nullptr, int iAppendZeros=0);
176  bool LoadEntry(const char *szEntryName, StdBuf * Buf);
177  bool LoadEntry(const StdStrBuf & name, StdBuf * Buf) { return LoadEntry(name.getData(), Buf); }
178  bool LoadEntryString(const char *szEntryName, StdStrBuf * Buf);
179  bool LoadEntryString(const StdStrBuf & name, StdStrBuf * Buf) { return LoadEntryString(name.getData(), Buf); }
180  bool FindEntry(const char *szWildCard,
181  StdStrBuf *sFileName=nullptr,
182  size_t *iSize=nullptr);
183  bool FindEntry(const char *szWildCard,
184  char *sFileName)
185  {
186  StdStrBuf name;
187  bool r = FindEntry(szWildCard, &name);
188  if(sFileName) SCopy(name.getData(),sFileName);
189  return r;
190  }
191  bool FindNextEntry(const char *szWildCard,
192  StdStrBuf *sFileName=nullptr,
193  size_t *iSize=nullptr,
194  bool fStartAtFilename=false);
195  bool FindNextEntry(const char *szWildCard,
196  char *sFileName,
197  size_t *iSize=nullptr,
198  bool fStartAtFilename=false)
199  {
200  StdStrBuf name(fStartAtFilename ? sFileName : "");
201  bool r = FindNextEntry(szWildCard, &name, iSize, fStartAtFilename);
202  if (r && sFileName) SCopy(name.getData(),sFileName);
203  return r;
204  }
205  bool Read(void *pBuffer, size_t iSize) override;
206  bool Advance(int iOffset) override;
207  void SetStdOutput(bool fStatus);
208  void ResetSearch(bool reload_contents=false); // reset search pointer so calls to FindNextEntry find first entry again. if reload_contents is set, the file list for directories is also refreshed.
209  const char *GetError();
210  const char *GetName() const;
211  StdStrBuf GetFullName() const;
212  int EntryCount(const char *szWildCard=nullptr);
213  size_t EntrySize(const char *szWildCard=nullptr);
214  size_t AccessedEntrySize() const override; // retrieve size of last accessed entry
215  unsigned int EntryCRC32(const char *szWildCard=nullptr);
216  bool IsOpen() const;
217  C4Group *GetMother();
218  bool IsPacked() const;
219  bool HasPackedMother() const;
220  bool SetNoSort(bool fNoSort);
221  int PreCacheEntries(const char *szSearchPattern, bool cache_previous=false); // pre-load entries to memory. return number of loaded entries.
222 
223  const C4GroupHeader &GetHeader() const;
224  const C4GroupEntry *GetFirstEntry() const;
225 
226 private:
227  void Init();
228  bool EnsureChildFilePtr(C4Group *pChild);
229  bool CloseExclusiveMother();
230  bool Error(const char *szStatus);
231  bool OpenReal(const char *szGroupName);
232  bool OpenRealGrpFile();
233  bool SetFilePtr(int iOffset);
234  bool RewindFilePtr();
235  bool AdvanceFilePtr(int iOffset);
236  bool AddEntry(C4GroupEntry::EntryStatus status,
237  bool childgroup,
238  const char *fname,
239  long size,
240  const char *entryname = nullptr,
241  BYTE *membuf = nullptr,
242  bool fDeleteOnDisk = false,
243  bool fHoldBuffer = false,
244  bool fExecutable = false,
245  bool fBufferIsStdbuf = false);
246  bool AddEntryOnDisk(const char *szFilename, const char *szAddAs=nullptr, bool fMove=false);
247  bool SetFilePtr2Entry(const char *szName, bool NeedsToBeAGroup = false);
248  bool AppendEntry2StdFile(C4GroupEntry *centry, CStdFile &stdfile);
249  C4GroupEntry *SearchNextEntry(const char *szName);
250  C4GroupEntry *GetNextFolderEntry();
251  uint32_t CalcCRC32(C4GroupEntry *pEntry);
252  void PreCacheEntry(C4GroupEntry * p);
253 };
254 
255 #endif
bool SetNoSort(bool fNoSort)
Definition: C4Group.cpp:2013
const char * getData() const
Definition: StdBuf.h:450
bool BufferIsStdbuf
Definition: C4Group.h:124
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
bool C4Group_MoveItem(const char *szSource, const char *szTarget, bool fNoSort=false)
Definition: C4Group.cpp:144
Definition: StdBuf.h:37
const int C4GroupFileVer2
Definition: C4Group.h:56
Definition: C4Group.h:116
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:129
int EntryCount(const char *szWildCard=nullptr)
Definition: C4Group.cpp:1850
const int C4GroupMaxError
Definition: C4Group.h:58
bool HoldBuffer
Definition: C4Group.h:123
C4Group * GetMother()
Definition: C4Group.cpp:2004
char Executable
Definition: C4Group.h:100
bool Merge(const char *szFolders)
Definition: C4Group.cpp:1229
unsigned int reserved4
Definition: C4Group.h:99
bool AccessEntry(const char *szWildCard, size_t *iSize=nullptr, char *sFileName=nullptr, bool NeedsToBeAGroup=false)
Definition: C4Group.cpp:1695
void C4Group_SetProcessCallback(bool(*fnCallback)(const char *, int))
Definition: C4Group.cpp:64
void C4Group_SetSortList(const char **ppSortList)
Definition: C4Group.cpp:69
BYTE fbuf[26]
Definition: C4Group.h:101
bool AccessNextEntry(const char *szWildCard, size_t *iSize=nullptr, char *sFileName=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1719
char FileName[260]
Definition: C4Group.h:94
bool C4Group_CopyItem(const char *szSource, const char *szTarget, bool fNoSort=false, bool fResetAttributes=false)
Definition: C4Group.cpp:100
int32_t reserved1
Definition: C4Group.h:96
bool LoadEntry(const char *szEntryName, char **lpbpBuf, size_t *ipSize=nullptr, int iAppendZeros=0)
Definition: C4Group.cpp:1893
uint8_t BYTE
bool Rename(const char *szFile, const char *szNewName)
Definition: C4Group.cpp:1424
bool Delete(const char *szFiles, bool fRecursive=false)
Definition: C4Group.cpp:1334
#define _MAX_PATH
Definition: C4Group.h:92
bool C4Group_ExplodeDirectory(const char *szFilename)
Definition: C4Group.cpp:351
const C4GroupEntry * GetFirstEntry() const
Definition: C4Group.cpp:2247
EntryStatus
Definition: C4Group.h:111
const int C4GroupFileVer1
Definition: C4Group.h:56
C4GroupEntry * Next
Definition: C4Group.h:127
int32_t reserved2
Definition: C4Group.h:97
void C4Group_SetTempPath(const char *szPath)
Definition: C4Group.cpp:74
bool Save(bool fReOpen)
Definition: C4Group.cpp:797
~C4Group()
Definition: C4Group.cpp:493
bool OpenMother()
Definition: C4Group.cpp:2197
int32_t Packed
Definition: C4Group.h:95
void Set(const DirectoryIterator &iter, const char *szPath)
Definition: C4Group.cpp:462
bool SortByList(const char **ppSortList, const char *szFilename=nullptr)
Definition: C4Group.cpp:2027
bool IsOpen() const
Definition: C4Group.cpp:1891
#define C4GroupFileID
Definition: C4Group.h:62
const char * GetName() const
Definition: C4Group.cpp:1845
bool C4Group_PackDirectory(const char *szFilename)
Definition: C4Group.cpp:284
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2078
bool C4Group_TestIgnore(const char *szFilename)
Definition: C4Group.cpp:85
bool Move(const char *szFile, const char *szAddAs)
Definition: C4Group.cpp:1325
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
void SetStdOutput(bool fStatus)
Definition: C4Group.cpp:509
const C4GroupHeader & GetHeader() const
Definition: C4Group.cpp:2245
unsigned int EntryCRC32(const char *szWildCard=nullptr)
Definition: C4Group.cpp:1877
bool fRecursive
Definition: C4GroupMain.cpp:34
size_t AccessedEntrySize() const override
Definition: C4Group.cpp:1875
bool Close()
Definition: C4Group.cpp:755
int32_t Offset
Definition: C4Group.h:96
const char * C4CFN_FLS[]
Definition: C4Group.cpp:32
~C4GroupEntry()
Definition: C4Group.cpp:450
int32_t ChildGroup
Definition: C4Group.h:95
bool OpenChild(const char *strEntry)
Definition: C4Group.cpp:2161
int32_t Size
Definition: C4Group.h:96
C4GroupEntry * GetEntry(const char *szName)
Definition: C4Group.cpp:744
bool DeleteOnDisk
Definition: C4Group.h:122
C4GroupHeader Head
Definition: C4Group.h:144
bool LoadEntry(const StdStrBuf &name, StdBuf *Buf)
Definition: C4Group.h:177
const char * GetError()
Definition: C4Group.cpp:504
Definition: C4Group.h:113
int PreCacheEntries(const char *szSearchPattern, bool cache_previous=false)
Definition: C4Group.cpp:2222
bool DeleteEntry(const char *szFilename, bool fRecycle=false)
Definition: C4Group.cpp:1384
Definition: C4Group.h:114
C4Group & operator=(C4Group &&)=default
bool NoSort
Definition: C4Group.h:125
bool LoadEntryString(const char *szEntryName, StdStrBuf *Buf)
Definition: C4Group.cpp:1932
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
bool C4Group_IsGroup(const char *szFilename)
Definition: C4Group.cpp:94
bool HasPackedMother() const
Definition: C4Group.cpp:2011
bool C4Group_DeleteItem(const char *szItem, bool fRecycle=false)
Definition: C4Group.cpp:194
Definition: C4Group.h:115
bool LoadEntryString(const StdStrBuf &name, StdStrBuf *Buf)
Definition: C4Group.h:179
const int32_t C4GroupSwapThreshold
Definition: C4Group.h:60
bool ExtractEntry(const char *szFilename, const char *szExtractTo=nullptr)
Definition: C4Group.cpp:1518
bool Add(const char *szFile, const char *szAddAs)
Definition: C4Group.cpp:1316
bool C4Group_ReadFile(const char *szFilename, char **pData, size_t *iSize)
Definition: C4Group.cpp:366
bool IsPacked() const
Definition: C4Group.cpp:2009
size_t EntrySize(const char *szWildCard=nullptr)
Definition: C4Group.cpp:1862
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1013
bool FindNextEntry(const char *szWildCard, char *sFileName, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.h:195
bool Sort(const char *szSortList)
Definition: C4Group.cpp:1963
EntryStatus Status
Definition: C4Group.h:121
char DiskPath[_MAX_PATH+1]
Definition: C4Group.h:120
void Clear()
Definition: C4Group.cpp:898
bool Extract(const char *szFiles, const char *szExtractTo=nullptr, const char *szExclude=nullptr)
Definition: C4Group.cpp:1471
const char * C4Group_GetTempPath()
Definition: C4Group.cpp:80
bool C4Group_UnpackDirectory(const char *szFilename)
Definition: C4Group.cpp:306
char reserved[164]
Definition: C4Group.h:89
bool FindEntry(const char *szWildCard, char *sFileName)
Definition: C4Group.h:183
int Entries
Definition: C4Group.h:88
bool Advance(int iOffset) override
Definition: C4Group.cpp:1105
bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false)
Definition: C4Group.cpp:1585
C4Group()
Definition: C4Group.cpp:477
Definition: C4Group.h:106
int iSize
Definition: TstC4NetIO.cpp:35
bool C4Group_PackDirectoryTo(const char *szFilename, const char *szFilenameTo)
Definition: C4Group.cpp:221
BYTE * bpMemBuf
Definition: C4Group.h:126
char reserved3
Definition: C4Group.h:98
bool Read(void *pBuffer, size_t iSize) override
Definition: C4Group.cpp:1125