OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ScenarioParameters Class Reference

#include <C4ScenarioParameters.h>

Public Member Functions

 C4ScenarioParameters ()=default
 
 ~C4ScenarioParameters ()=default
 
void Clear ()
 
void Merge (const C4ScenarioParameters &other)
 
int32_t GetValueByID (const char *id, int32_t default_value) const
 
void SetValue (const char *id, int32_t value, bool only_if_larger)
 
void CompileFunc (StdCompiler *pComp)
 

Static Public Member Functions

static StdStrBuf AddFilename2ID (const char *filename, const char *id)
 

Detailed Description

Definition at line 94 of file C4ScenarioParameters.h.

Constructor & Destructor Documentation

C4ScenarioParameters::C4ScenarioParameters ( )
default
C4ScenarioParameters::~C4ScenarioParameters ( )
default

Member Function Documentation

StdStrBuf C4ScenarioParameters::AddFilename2ID ( const char *  filename,
const char *  id 
)
static

Definition at line 208 of file C4ScenarioParameters.cpp.

References AltDirectorySeparator, StdStrBuf::Append(), StdStrBuf::AppendChar(), DirectorySeparator, StdStrBuf::getData(), StdStrBuf::getLength(), StdStrBuf::getPtr(), StdStrBuf::ReplaceChar(), and SEqual2NoCase().

Referenced by C4Player::GainScenarioAchievement(), and C4ScenarioListLoader::Scenario::LoadCustomPre().

209 {
210  // composes an ID string that contains both the relevant part of the filename and the ID
211  // we care for .oc* folders only
212  StdStrBuf sResult, sSource(filename, true), sPart;
214  size_t idx=0;
215  while (sSource.GetSection(idx++, &sPart, DirectorySeparator))
216  {
217  size_t len = sPart.getLength();
218  if (len > 4 && SEqual2NoCase(sPart.getPtr(len - 4), ".oc", 3))
219  {
220  // .oc* folders separated by underscores
221  sResult.Append(sPart.getData(), len - 4);
222  sResult.AppendChar('_');
223  }
224  }
225  sResult.Append(id);
226  return sResult;
227 }
const char * getData() const
Definition: StdBuf.h:442
void AppendChar(char cChar)
Definition: StdBuf.h:588
#define AltDirectorySeparator
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
int ReplaceChar(char cOld, char cNew)
Definition: StdBuf.cpp:336
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:198
const char * getPtr(size_t i) const
Definition: StdBuf.h:448
size_t getLength() const
Definition: StdBuf.h:445
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ScenarioParameters::Clear ( )

Definition at line 112 of file C4ScenarioParameters.cpp.

Referenced by C4Game::Clear(), C4GameParameters::Clear(), and C4StartupScenSelDlg::StartScenario().

113 {
114  Parameters.clear();
115 }

Here is the caller graph for this function:

void C4ScenarioParameters::CompileFunc ( StdCompiler pComp)

Definition at line 152 of file C4ScenarioParameters.cpp.

References StdStrBuf::getData(), StdCompiler::GetNameByIndex(), StdCompiler::hasNaming(), StdCompiler::isDeserializer(), mkNamingAdapt(), StdCompiler::NameCount(), and StdCompiler::Value().

153 {
154  // Unfortunately, StdCompiler cannot save std::map yet
155  if (pComp->isDeserializer())
156  {
157  Parameters.clear();
158  if (pComp->hasNaming())
159  {
160  // load from INI
161  size_t name_count = pComp->NameCount();
162  for (size_t i=0; i<name_count; ++i)
163  {
164  int32_t v=0;
165  const char *name = pComp->GetNameByIndex(0); // always get name index 0, because names are removed after values have been extracted for them
166  StdCopyStrBuf sName(name);
167  if (!name) continue;
168  pComp->Value(mkNamingAdapt(v, sName.getData(), 0));
169  Parameters[sName] = v;
170  }
171  }
172  else
173  {
174  // load from binary
175  int32_t name_count=0;
176  pComp->Value(name_count);
177  for (int32_t i=0; i<name_count; ++i)
178  {
179  StdCopyStrBuf name; int32_t v;
180  pComp->Value(name);
181  pComp->Value(v);
182  Parameters[name] = v;
183  }
184  }
185  }
186  else
187  {
188  if (pComp->hasNaming())
189  {
190  // save to INI
191  for (auto & Parameter : Parameters)
192  pComp->Value(mkNamingAdapt(Parameter.second, Parameter.first.getData()));
193  }
194  else
195  {
196  // save to binary
197  int32_t name_count=Parameters.size();
198  pComp->Value(name_count);
199  for (auto & Parameter : Parameters)
200  {
201  pComp->Value(const_cast<StdCopyStrBuf &>(Parameter.first));
202  pComp->Value(Parameter.second);
203  }
204  }
205  }
206 }
virtual bool hasNaming()
Definition: StdCompiler.h:58
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:92
virtual const char * GetNameByIndex(size_t idx) const
Definition: StdCompiler.h:79
void Value(const T &rStruct)
Definition: StdCompiler.h:161
virtual bool isDeserializer()
Definition: StdCompiler.h:53
virtual int NameCount(const char *szName=nullptr)
Definition: StdCompiler.h:91

Here is the call graph for this function:

int32_t C4ScenarioParameters::GetValueByID ( const char *  id,
int32_t  default_value 
) const

Definition at line 131 of file C4ScenarioParameters.cpp.

Referenced by C4ScenarioParameterDefs::RegisterScriptConstants().

132 {
133  // return map value if name is in map. Otherwise, return default value.
134  auto i = Parameters.find(StdStrBuf(id));
135  if (i != Parameters.end()) return i->second; else return default_value;
136 }

Here is the caller graph for this function:

void C4ScenarioParameters::Merge ( const C4ScenarioParameters other)

Definition at line 117 of file C4ScenarioParameters.cpp.

118 {
119  // Merge lists and keep larger value
120  for (const auto & Parameter : other.Parameters)
121  {
122  auto j = Parameters.find(Parameter.first);
123  if (j != Parameters.end())
124  if (j->second >= Parameter.second)
125  continue; // existing value is same or larger - keep old
126  // update to new value from other list
127  Parameters[Parameter.first] = Parameter.second;
128  }
129 }
void C4ScenarioParameters::SetValue ( const char *  id,
int32_t  value,
bool  only_if_larger 
)

Definition at line 138 of file C4ScenarioParameters.cpp.

Referenced by C4GameParameters::EnforceLeagueRules(), C4Player::GainScenarioAchievement(), C4GameLobby::MainDlg::HandlePacket(), and C4Application::ParseCommandLine().

139 {
140  if (only_if_larger)
141  {
142  auto i = Parameters.find(StdStrBuf(id));
143  if (i != Parameters.end())
144  if (i->second >= value)
145  // could become smaller. don't set.
146  return;
147  }
148  // just update map
149  Parameters[StdCopyStrBuf(id)] = value;
150 }

Here is the caller graph for this function:


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