OpenClonk
C4ComponentHost Class Reference

#include <C4ComponentHost.h>

Inheritance diagram for C4ComponentHost:
[legend]
Collaboration diagram for C4ComponentHost:
[legend]

Public Member Functions

 C4ComponentHost ()=default
 
virtual ~C4ComponentHost ()
 
const char * GetFilePath () const
 
void Clear ()
 
const char * GetData () const
 
const StdStrBufGetDataBuf () const
 
size_t GetDataSize () const
 
bool Load (C4Group &hGroup, const char *szFilename, const char *szLanguage=nullptr)
 
bool Load (C4GroupSet &hGroupSet, const char *szFilename, const char *szLanguage=nullptr)
 
bool GetLanguageString (const char *szLanguage, StdStrBuf &rTarget)
 

Protected Member Functions

virtual void OnLoad ()
 
void CopyFilePathFromGroup (const C4Group &hGroup)
 
void FinishLoad (const StdStrBuf &, C4Group &hGroup)
 

Protected Attributes

StdCopyStrBuf Data
 
StdCopyStrBuf Filename
 
StdCopyStrBuf FilePath
 

Detailed Description

Definition at line 23 of file C4ComponentHost.h.

Constructor & Destructor Documentation

◆ C4ComponentHost()

C4ComponentHost::C4ComponentHost ( )
default

◆ ~C4ComponentHost()

virtual C4ComponentHost::~C4ComponentHost ( )
inlinevirtual

Definition at line 27 of file C4ComponentHost.h.

27 { Clear(); }

References Clear().

Here is the call graph for this function:

Member Function Documentation

◆ Clear()

void C4ComponentHost::Clear ( )
inline

Definition at line 29 of file C4ComponentHost.h.

29 { Data.Clear(); OnLoad(); }
virtual void OnLoad()
StdCopyStrBuf Data
void Clear()
Definition: StdBuf.h:466

References StdStrBuf::Clear(), Data, and OnLoad().

Referenced by C4Game::Clear(), C4Def::Clear(), C4ScriptHost::Clear(), C4Language::ClearLanguage(), C4Def::Default(), Load(), and ~C4ComponentHost().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CopyFilePathFromGroup()

void C4ComponentHost::CopyFilePathFromGroup ( const C4Group hGroup)
protected

Definition at line 120 of file C4ComponentHost.cpp.

121 {
125 }
C4Config Config
Definition: C4Config.cpp:930
StdCopyStrBuf Filename
StdCopyStrBuf FilePath
const char * AtRelativePath(const char *filename)
Definition: C4Config.cpp:741
StdStrBuf GetFullName() const
Definition: C4Group.cpp:2638
const char * getData() const
Definition: StdBuf.h:442
void AppendBackslash()
Definition: StdBuf.cpp:248
void Copy()
Definition: StdBuf.h:467
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519

References StdStrBuf::Append(), StdStrBuf::AppendBackslash(), C4Config::AtRelativePath(), Config, StdStrBuf::Copy(), Filename, FilePath, StdStrBuf::getData(), and C4Group::GetFullName().

Referenced by FinishLoad(), and Load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FinishLoad()

void C4ComponentHost::FinishLoad ( const StdStrBuf name,
C4Group hGroup 
)
protected

Definition at line 99 of file C4ComponentHost.cpp.

100 {
101  // Store actual filename
102  hGroup.FindEntry(name.getData(), &Filename);
103  CopyFilePathFromGroup(hGroup);
104 
105  if (Data.EnsureUnicode())
106  {
107  LogF("WARNING: File is not encoded as UTF-8 (%s)", FilePath.getData());
108  }
109  // Skip those stupid "zero width no-break spaces" (also known as Byte Order Marks)
110  if (Data[0] == '\xEF' && Data[1] == '\xBB' && Data[2] == '\xBF')
111  {
112  Data.Move(3,Data.getSize()-3);
113  Data.Shrink(3);
114  }
115  // Notify
116  OnLoad();
117 }
bool LogF(const char *strMessage,...)
Definition: C4Log.cpp:262
void CopyFilePathFromGroup(const C4Group &hGroup)
bool FindEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr)
Definition: C4Group.cpp:2211
size_t getSize() const
Definition: StdBuf.h:444
void Move(size_t iFrom, size_t inSize, size_t iTo=0)
Definition: StdBuf.h:471
void Shrink(size_t iShrink)
Definition: StdBuf.h:503
bool EnsureUnicode()
Definition: StdBuf.cpp:421

References CopyFilePathFromGroup(), Data, StdStrBuf::EnsureUnicode(), Filename, FilePath, C4Group::FindEntry(), StdStrBuf::getData(), StdStrBuf::getSize(), LogF(), StdStrBuf::Move(), OnLoad(), and StdStrBuf::Shrink().

Referenced by Load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetData()

const char* C4ComponentHost::GetData ( ) const
inline

Definition at line 30 of file C4ComponentHost.h.

30 { return Data.getData(); }

References Data, and StdStrBuf::getData().

Referenced by C4Game::CompileRuntimeData(), C4RankSystem::Load(), C4ScenarioListLoader::Entry::Load(), C4Player::LoadRuntimeData(), C4ObjectInfoList::New(), and C4Game::OpenScenario().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDataBuf()

const StdStrBuf& C4ComponentHost::GetDataBuf ( ) const
inline

Definition at line 31 of file C4ComponentHost.h.

31 { return Data; }

References Data.

Referenced by C4Game::CompileRuntimeData(), C4Language::HasStringTable(), and C4ScriptHost::MakeScript().

Here is the caller graph for this function:

◆ GetDataSize()

size_t C4ComponentHost::GetDataSize ( ) const
inline

Definition at line 32 of file C4ComponentHost.h.

32 { return Data.getLength(); }
size_t getLength() const
Definition: StdBuf.h:445

References Data, and StdStrBuf::getLength().

Referenced by C4RankSystem::Load().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetFilePath()

const char* C4ComponentHost::GetFilePath ( ) const
inline

Definition at line 28 of file C4ComponentHost.h.

28 { return FilePath.getData(); }

References FilePath, and StdStrBuf::getData().

Referenced by C4ScriptHost::Load(), C4Player::LoadRuntimeData(), C4ScriptHost::ReloadScript(), and C4LangStringTable::ReplaceStrings().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetLanguageString()

bool C4ComponentHost::GetLanguageString ( const char *  szLanguage,
StdStrBuf rTarget 
)

Definition at line 127 of file C4ComponentHost.cpp.

128 {
129  const char *cptr;
130  // No good parameters
131  if (!szLanguage || !Data) return false;
132  // Search for two-letter language identifier in text body, i.e. "DE:"
133  char langindex[4] = "";
134  for (int clseg=0; SCopySegment(szLanguage ? szLanguage : "", clseg, langindex, ',', 2); clseg++)
135  {
136  SAppend(":",langindex);
137  if ((cptr = SSearch(Data.getData(),langindex)))
138  {
139  // Return the according string
140  int iEndPos = SCharPos('\r', cptr);
141  if (iEndPos<0) iEndPos = SCharPos('\n', cptr);
142  if (iEndPos<0) iEndPos = strlen(cptr);
143  rTarget.Copy(cptr, iEndPos);
144  return true;
145  }
146  }
147  // Language string not found
148  return false;
149 }
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:369
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:239
bool SCopySegment(const char *szString, int iSegment, char *sTarget, char cSeparator, int iMaxL, bool fSkipWhitespace)
Definition: Standard.cpp:279
void SAppend(const char *szSource, char *szTarget, int iMaxL)
Definition: Standard.cpp:263

References StdStrBuf::Copy(), Data, StdStrBuf::getData(), SAppend(), SCharPos(), SCopySegment(), and SSearch().

Referenced by C4DefList::Load(), C4ScenarioListLoader::Entry::Load(), and C4Game::OpenScenario().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Load() [1/2]

bool C4ComponentHost::Load ( C4Group hGroup,
const char *  szFilename,
const char *  szLanguage = nullptr 
)

Definition at line 23 of file C4ComponentHost.cpp.

26 {
27  // Clear any old stuff
28  Clear();
29  // Store filename
30  if (fname)
31  Filename.Copy(fname);
32  // Load component - try all segmented filenames
33  char strEntry[_MAX_FNAME_LEN];
34  StdStrBuf strEntryWithLanguage;
35  for (int iFilename = 0; SCopySegment(Filename.getData(), iFilename, strEntry, '|'); iFilename++)
36  {
37  // Try to insert all language codes provided into the filename
38  char strCode[3] = "";
39  for (int iLang = 0; SCopySegment(szLanguage ? szLanguage : "", iLang, strCode, ',', 2); iLang++)
40  {
41  // Insert language code
42  strEntryWithLanguage.Format(strEntry, strCode);
43  if (hGroup.LoadEntryString(strEntryWithLanguage, &Data))
44  {
45  FinishLoad(strEntryWithLanguage, hGroup);
46  // Got it
47  return true;
48  }
49  // Couldn't insert language code anyway - no point in trying other languages
50  if (!SSearch(strEntry, "%s")) break;
51  }
52  }
53  // Truncate any additional segments from stored filename
54  SReplaceChar(Filename.getMData(), '|', 0);
55  CopyFilePathFromGroup(hGroup);
56  // Not loaded
57  return false;
58 }
#define _MAX_FNAME_LEN
void SReplaceChar(char *str, char fc, char tc)
Definition: Standard.cpp:354
void FinishLoad(const StdStrBuf &, C4Group &hGroup)
bool LoadEntryString(const char *entry_name, StdStrBuf *buffer)
Definition: C4Group.cpp:2430
char * getMData()
Definition: StdBuf.h:443
void Format(const char *szFmt,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: StdBuf.cpp:174

References _MAX_FNAME_LEN, Clear(), StdStrBuf::Copy(), CopyFilePathFromGroup(), Data, Filename, FinishLoad(), StdStrBuf::Format(), StdStrBuf::getData(), StdStrBuf::getMData(), C4Group::LoadEntryString(), SCopySegment(), SReplaceChar(), and SSearch().

Referenced by C4Game::CompileRuntimeData(), C4Game::InitScriptEngine(), C4ScriptHost::Load(), C4ScenarioListLoader::Entry::Load(), C4Language::LoadComponentHost(), and C4Game::LoadScenarioComponents().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Load() [2/2]

bool C4ComponentHost::Load ( C4GroupSet hGroupSet,
const char *  szFilename,
const char *  szLanguage = nullptr 
)

Definition at line 60 of file C4ComponentHost.cpp.

63 {
64  // Clear any old stuff
65  Clear();
66  // Store filename
67  if (fname)
68  Filename.Copy(fname);
69  // Load component - try all segmented filenames
70  char strEntry[_MAX_FNAME_LEN];
71  StdStrBuf strEntryWithLanguage;
72  for (int iFilename = 0; SCopySegment(Filename.getData(), iFilename, strEntry, '|'); iFilename++)
73  {
74  // Try to insert all language codes provided into the filename
75  char strCode[3] = "";
76  for (int iLang = 0; SCopySegment(szLanguage ? szLanguage : "", iLang, strCode, ',', 2); iLang++)
77  {
78  // Insert language code
79  strEntryWithLanguage.Format(strEntry, strCode);
80  if (hGroupSet.LoadEntryString(strEntryWithLanguage, &Data))
81  {
82  C4Group *pGroup = hGroupSet.FindEntry(strEntryWithLanguage.getData());
83  FinishLoad(strEntryWithLanguage, *pGroup);
84  // Got it
85  return true;
86  }
87  // Couldn't insert language code anyway - no point in trying other languages
88  if (!SSearch(strEntry, "%s")) break;
89  }
90  }
91  // Truncate any additional segments from stored filename
92  SReplaceChar(Filename.getMData(), '|', 0);
93  // for error message purposes, the first group failed to provide the desired file
94  CopyFilePathFromGroup(*hGroupSet.GetGroup(0));
95  // Not loaded
96  return false;
97 }
bool LoadEntryString(const char *szEntryName, StdStrBuf *rBuf)
Definition: C4GroupSet.cpp:225
C4Group * GetGroup(int32_t iIndex)
Definition: C4GroupSet.cpp:259
C4Group * FindEntry(const char *szWildcard, int32_t *pPriority=nullptr, int32_t *pID=nullptr)
Definition: C4GroupSet.cpp:175

References _MAX_FNAME_LEN, Clear(), StdStrBuf::Copy(), CopyFilePathFromGroup(), Data, Filename, C4GroupSet::FindEntry(), FinishLoad(), StdStrBuf::Format(), StdStrBuf::getData(), C4GroupSet::GetGroup(), StdStrBuf::getMData(), C4GroupSet::LoadEntryString(), SCopySegment(), SReplaceChar(), and SSearch().

Here is the call graph for this function:

◆ OnLoad()

virtual void C4ComponentHost::OnLoad ( )
inlineprotectedvirtual

Reimplemented in C4LangStringTable.

Definition at line 39 of file C4ComponentHost.h.

39 {}

Referenced by Clear(), and FinishLoad().

Here is the caller graph for this function:

Member Data Documentation

◆ Data

StdCopyStrBuf C4ComponentHost::Data
protected

◆ Filename

StdCopyStrBuf C4ComponentHost::Filename
protected

Definition at line 42 of file C4ComponentHost.h.

Referenced by CopyFilePathFromGroup(), FinishLoad(), and Load().

◆ FilePath

StdCopyStrBuf C4ComponentHost::FilePath
protected

Definition at line 43 of file C4ComponentHost.h.

Referenced by CopyFilePathFromGroup(), FinishLoad(), and GetFilePath().


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