OpenClonk
C4GameResList Class Reference

#include <C4GameParameters.h>

Public Member Functions

 C4GameResList ()=default
 
 ~C4GameResList ()
 
C4GameResListoperator= (const C4GameResList &List)
 
int32_t getResCount () const
 
C4GameResiterRes (C4GameRes *pLast, C4Network2ResType eType=NRT_Null)
 
void Clear ()
 
bool Load (C4Group &hGroup, C4Scenario *pScenario, const char *szDefinitionFilenames)
 
C4GameResCreateByFile (C4Network2ResType eType, const char *szFile)
 
C4GameResCreateByNetRes (C4Network2Res::Ref pNetRes)
 
bool InitNetwork (C4Network2ResList *pNetResList)
 
void CalcHashes ()
 
bool RetrieveFiles ()
 
void CompileFunc (StdCompiler *pComp)
 

Protected Member Functions

void Add (C4GameRes *pRes)
 
void LoadFoldersWithLocalDefs (const char *szPath)
 

Detailed Description

Definition at line 64 of file C4GameParameters.h.

Constructor & Destructor Documentation

◆ C4GameResList()

C4GameResList::C4GameResList ( )
default

◆ ~C4GameResList()

C4GameResList::~C4GameResList ( )
inline

Definition at line 72 of file C4GameParameters.h.

References C4GameRes::Clear(), and C4GameRes::operator=().

72 { Clear(); }
Here is the call graph for this function:

Member Function Documentation

◆ Add()

void C4GameResList::Add ( C4GameRes pRes)
protected

Definition at line 341 of file C4GameParameters.cpp.

342 {
343  // Enlarge
344  if (iResCount >= iResCapacity)
345  {
346  iResCapacity += 10;
347  C4GameRes **pnResList = new C4GameRes *[iResCapacity];
348  for (int i = 0; i < iResCount; i++)
349  pnResList[i] = pResList[i];
350  pResList = pnResList;
351  }
352  // Add
353  pResList[iResCount++] = pRes;
354 }

◆ CalcHashes()

void C4GameResList::CalcHashes ( )

Definition at line 322 of file C4GameParameters.cpp.

References C4GameRes::CalcHash().

323 {
324  for (int32_t i = 0; i < iResCount; i++)
325  pResList[i]->CalcHash();
326 }
Here is the call graph for this function:

◆ Clear()

void C4GameResList::Clear ( )

Definition at line 205 of file C4GameParameters.cpp.

206 {
207  // clear them
208  for (int32_t i = 0; i < iResCount; i++)
209  delete pResList[i];
210  delete [] pResList;
211  pResList = nullptr;
212  iResCount = iResCapacity = 0;
213 }

◆ CompileFunc()

void C4GameResList::CompileFunc ( StdCompiler pComp)

Definition at line 356 of file C4GameParameters.cpp.

References C4GameParameters::C4GameParameters(), C4GameRes::Clear(), StdCompiler::isDeserializer(), mkArrayAdaptMap(), mkNamingAdapt(), mkNamingCountAdapt(), StdCompiler::Value(), ZeroMem(), and C4GameParameters::~C4GameParameters().

357 {
358  bool deserializing = pComp->isDeserializer();
359  // Clear previous data
360  if (deserializing) Clear();
361  // Compile resource count
362  pComp->Value(mkNamingCountAdapt(iResCount, "Resource"));
363  // Create list
364  if (deserializing)
365  {
366  pResList = new C4GameRes *[iResCapacity = iResCount];
367  ZeroMem(pResList, sizeof(*pResList) * iResCount);
368  }
369  // Compile list
370  pComp->Value(
372  mkArrayAdaptMap(pResList, iResCount, mkPtrAdaptNoNull<C4GameRes>),
373  "Resource"));
374  mkPtrAdaptNoNull<C4GameRes>(*pResList);
375 }
StdNamingCountAdapt< int_t > mkNamingCountAdapt(int_t &iCount, const char *szName)
Definition: StdAdaptors.h:994
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
std::enable_if< std::is_pod< T >::value >::type ZeroMem(T *lpMem, size_t dwSize)
Definition: Standard.h:60
StdArrayAdapt< T, M > mkArrayAdaptMap(T *pArray, int iSize, M &&map)
Definition: StdAdaptors.h:331
Here is the call graph for this function:

◆ CreateByFile()

C4GameRes * C4GameResList::CreateByFile ( C4Network2ResType  eType,
const char *  szFile 
)

Definition at line 292 of file C4GameParameters.cpp.

References C4GameRes::C4GameRes(), and C4GameRes::SetFile().

293 {
294  // Create & set
295  C4GameRes *pRes = new C4GameRes();
296  pRes->SetFile(eType, szFile);
297  // Add to list
298  Add(pRes);
299  return pRes;
300 }
void Add(C4GameRes *pRes)
void SetFile(C4Network2ResType eType, const char *szFile)
Here is the call graph for this function:

◆ CreateByNetRes()

C4GameRes * C4GameResList::CreateByNetRes ( C4Network2Res::Ref  pNetRes)

Definition at line 302 of file C4GameParameters.cpp.

References C4GameRes::C4GameRes(), and C4GameRes::SetNetRes().

303 {
304  // Create & set
305  C4GameRes *pRes = new C4GameRes();
306  pRes->SetNetRes(pNetRes);
307  // Add to list
308  Add(pRes);
309  return pRes;
310 }
void SetNetRes(C4Network2Res::Ref pRes)
void Add(C4GameRes *pRes)
Here is the call graph for this function:

◆ getResCount()

int32_t C4GameResList::getResCount ( ) const
inline

Definition at line 76 of file C4GameParameters.h.

References C4GameRes::Clear(), C4GameRes::CompileFunc(), C4GameRes::InitNetwork(), C4GameRes::Load(), and NRT_Null.

76 { return iResCount; }
Here is the call graph for this function:

◆ InitNetwork()

bool C4GameResList::InitNetwork ( C4Network2ResList pNetResList)

Definition at line 312 of file C4GameParameters.cpp.

References C4GameRes::InitNetwork().

Referenced by C4Network2::HandleJoinData().

313 {
314  // Check all resources without attached network resource object
315  for (int i = 0; i < iResCount; i++)
316  if (!pResList[i]->InitNetwork(pNetResList))
317  return false;
318  // Success
319  return true;
320 }
bool InitNetwork(C4Network2ResList *pNetResList)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ iterRes()

C4GameRes * C4GameResList::iterRes ( C4GameRes pLast,
C4Network2ResType  eType = NRT_Null 
)

Definition at line 192 of file C4GameParameters.cpp.

References C4GameRes::getType(), and NRT_Null.

Referenced by C4Game::InitDefs(), and C4Game::InitMaterialTexture().

193 {
194  for (int i = 0; i < iResCount; i++)
195  if (!pLast)
196  {
197  if (eType == NRT_Null || pResList[i]->getType() == eType)
198  return pResList[i];
199  }
200  else if (pLast == pResList[i])
201  pLast = nullptr;
202  return nullptr;
203 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Load()

bool C4GameResList::Load ( C4Group hGroup,
C4Scenario pScenario,
const char *  szDefinitionFilenames 
)

Definition at line 260 of file C4GameParameters.cpp.

References _MAX_PATH, C4CFN_Material, C4CFN_System, C4GSCnt_Material, C4GameRes::Clear(), DirSep, C4GroupSet::FindGroup(), Game, StdStrBuf::getData(), C4Group::GetFullName(), C4Game::GroupSet, C4Scenario::Head, NRT_Definitions, NRT_Material, NRT_System, C4SHead::Origin, C4Game::ScenarioFile, and SCopySegment().

261 {
262  // clear any prev
263  Clear();
264  // no defs to be added? that's OK (LocalOnly)
265  if (szDefinitionFilenames && *szDefinitionFilenames)
266  {
267  // add them
268  char szSegment[_MAX_PATH+1];
269  for (int32_t cseg=0; SCopySegment(szDefinitionFilenames,cseg,szSegment,';',_MAX_PATH); ++cseg)
270  if (*szSegment)
271  CreateByFile(NRT_Definitions, szSegment);
272  }
273 
274  LoadFoldersWithLocalDefs(pScenario->Head.Origin ? pScenario->Head.Origin.getData() : hGroup.GetFullName().getData());
275 
276  // add System.ocg
278  // add all instances of Material.ocg, except those inside the scenario file
279  C4Group *pMatParentGrp = nullptr;
280  while ((pMatParentGrp = Game.GroupSet.FindGroup(C4GSCnt_Material, pMatParentGrp)))
281  if (pMatParentGrp != &Game.ScenarioFile)
282  {
283  StdStrBuf MaterialPath = pMatParentGrp->GetFullName() + DirSep C4CFN_Material;
284  CreateByFile(NRT_Material, (pMatParentGrp->GetFullName() + DirSep C4CFN_Material).getData());
285  }
286  // add global Material.ocg
287  CreateByFile(NRT_Material, C4CFN_Material);
288  // done; success
289  return true;
290 }
StdCopyStrBuf Origin
Definition: C4Scenario.h:80
C4Game Game
Definition: C4Globals.cpp:52
C4GroupSet GroupSet
Definition: C4Game.h:87
#define C4CFN_Material
Definition: C4Components.h:25
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:273
#define C4GSCnt_Material
Definition: C4GroupSet.h:36
#define _MAX_PATH
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2078
C4GameRes * CreateByFile(C4Network2ResType eType, const char *szFile)
C4Group ScenarioFile
Definition: C4Game.h:86
const char * getData() const
Definition: StdBuf.h:442
C4Group * FindGroup(int32_t Contents, C4Group *pAfter=nullptr, bool fSamePrio=false)
Definition: C4GroupSet.cpp:155
C4SHead Head
Definition: C4Scenario.h:232
void LoadFoldersWithLocalDefs(const char *szPath)
#define C4CFN_System
Definition: C4Components.h:29
#define DirSep
Here is the call graph for this function:

◆ LoadFoldersWithLocalDefs()

void C4GameResList::LoadFoldersWithLocalDefs ( const char *  szPath)
protected

Definition at line 215 of file C4GameParameters.cpp.

References _MAX_PATH, AltDirectorySeparator, C4GSCnt_Definitions, C4GroupSet::CheckGroupContents(), C4Group::Close(), DirectorySeparator, Game, GetExtension(), C4Game::GroupSet, LogF(), NRT_Definitions, C4Reloc::Open(), Reloc, SCharPos(), SCopy(), and SEqualNoCase().

216 {
217  // Scan path for folder names
218  int32_t iBackslash;
219  char szFoldername[_MAX_PATH+1];
220  C4Group hGroup;
221 #ifdef _WIN32
222  // Allow both backward and forward slashes when searching because the path
223  // may be given with forward slashes. We would skip loading some definitions
224  // if we didn't handle this properly and the user would have no clue what was
225  // going on. See also http://forum.openclonk.org/topic_show.pl?tid=905.
226  char control[3] = { DirectorySeparator, AltDirectorySeparator, '\0' };
227  const int32_t len = (int32_t)strlen(szPath);
228  for (int32_t iPrev=0; (iBackslash = strcspn(szPath+iPrev, control) + iPrev) < len; iPrev = iBackslash + 1)
229 #else
230  for (int32_t cnt=0; (iBackslash=SCharPos(DirectorySeparator,szPath,cnt)) > -1; cnt++)
231 #endif
232  {
233  // Get folder name
234  SCopy(szPath,szFoldername,iBackslash);
235  // Open folder
236  if (SEqualNoCase(GetExtension(szFoldername),"ocf"))
237  {
238  if (Reloc.Open(hGroup, szFoldername))
239  {
240  // Check for contained defs
241  // do not, however, add them to the group set:
242  // parent folders are added by OpenScenario already!
243  int32_t iContents;
244  if ((iContents = Game.GroupSet.CheckGroupContents(hGroup, C4GSCnt_Definitions)))
245  {
246  // Add folder to list
247  CreateByFile(NRT_Definitions, szFoldername);
248  }
249  // Close folder
250  hGroup.Close();
251  }
252  else
253  {
254  LogF("Internal WARNING: Could not inspect folder %s for definitions.", szFoldername);
255  }
256  }
257  }
258 }
void SCopy(const char *szSource, char *sTarget, size_t iMaxL)
Definition: Standard.cpp:152
C4Game Game
Definition: C4Globals.cpp:52
C4GroupSet GroupSet
Definition: C4Game.h:87
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:207
#define C4GSCnt_Definitions
Definition: C4GroupSet.h:38
#define _MAX_PATH
#define AltDirectorySeparator
C4GameRes * CreateByFile(C4Network2ResType eType, const char *szFile)
bool Close()
Definition: C4Group.cpp:755
char * GetExtension(char *szFilename)
Definition: StdFile.cpp:121
C4Reloc Reloc
Definition: C4Reloc.cpp:21
static int32_t CheckGroupContents(C4Group &rGroup, int32_t Contents)
Definition: C4GroupSet.cpp:106
bool Open(C4Group &hGroup, const char *filename) const
Definition: C4Reloc.cpp:82
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:233
#define DirectorySeparator
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:260
Here is the call graph for this function:

◆ operator=()

C4GameResList & C4GameResList::operator= ( const C4GameResList List)

Definition at line 181 of file C4GameParameters.cpp.

References C4GameRes::C4GameRes(), and C4GameRes::Clear().

182 {
183  Clear();
184  // Copy the list
185  iResCount = iResCapacity = List.iResCount;
186  pResList = new C4GameRes *[iResCapacity];
187  for (int i = 0; i < iResCount; i++)
188  pResList[i] = new C4GameRes(*List.pResList[i]);
189  return *this;
190 }
Here is the call graph for this function:

◆ RetrieveFiles()

bool C4GameResList::RetrieveFiles ( )

Definition at line 328 of file C4GameParameters.cpp.

References C4NetResRetrieveTimeout, FormatString(), StdStrBuf::getData(), GetFilename(), C4Network2ResCore::getFileName(), LoadResStr(), Network, and C4Network2::RetrieveRes().

Referenced by C4Game::Init().

329 {
330  // wait for all resources
331  for (int32_t i = 0; i < iResCount; i++)
332  {
333  const C4Network2ResCore &Core = *pResList[i]->getResCore();
334  StdStrBuf ResNameBuf = FormatString("%s: %s", LoadResStr("IDS_DLG_DEFINITION"), GetFilename(Core.getFileName()));
335  if (!::Network.RetrieveRes(Core, C4NetResRetrieveTimeout, ResNameBuf.getData()))
336  return false;
337  }
338  return true;
339 }
char * GetFilename(char *szPath)
Definition: StdFile.cpp:45
const char * LoadResStr(const char *id)
Definition: C4Language.h:83
C4Network2 Network
Definition: C4Globals.cpp:53
C4Network2Res::Ref RetrieveRes(const C4Network2ResCore &Core, int32_t iTimeout, const char *szResName, bool fWaitForCore=false)
const char * getData() const
Definition: StdBuf.h:442
const int C4NetResRetrieveTimeout
Definition: C4Network2.h:37
const C4Network2ResCore * getResCore() const
StdStrBuf FormatString(const char *szFmt,...)
Definition: StdBuf.cpp:270
const char * getFileName() const
Definition: C4Network2Res.h:94
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: