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

#include <C4ScenarioParameters.h>

Public Member Functions

 C4ScenarioParameters ()
 
 ~C4ScenarioParameters ()
 
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 111 of file C4ScenarioParameters.h.

Constructor & Destructor Documentation

C4ScenarioParameters::C4ScenarioParameters ( )
inline

Definition at line 116 of file C4ScenarioParameters.h.

116 {}
C4ScenarioParameters::~C4ScenarioParameters ( )
inline

Definition at line 117 of file C4ScenarioParameters.h.

117 {}

Member Function Documentation

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

Definition at line 265 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().

266 {
267  // composes an ID string that contains both the relevant part of the filename and the ID
268  // we care for .oc* folders only
269  StdStrBuf sResult, sSource(filename, true), sPart;
271  size_t idx=0;
272  while (sSource.GetSection(idx++, &sPart, DirectorySeparator))
273  {
274  size_t len = sPart.getLength();
275  if (len > 4 && SEqual2NoCase(sPart.getPtr(len - 4), ".oc", 3))
276  {
277  // .oc* folders separated by underscores
278  sResult.Append(sPart.getData(), len - 4);
279  sResult.AppendChar('_');
280  }
281  }
282  sResult.Append(id);
283  return sResult;
284 }
const char * getData() const
Definition: StdBuf.h:450
void AppendChar(char cChar)
Definition: StdBuf.h:596
#define AltDirectorySeparator
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:527
int ReplaceChar(char cOld, char cNew)
Definition: StdBuf.cpp:343
bool SEqual2NoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:190
const char * getPtr(size_t i) const
Definition: StdBuf.h:456
size_t getLength() const
Definition: StdBuf.h:453
#define DirectorySeparator

Here is the call graph for this function:

Here is the caller graph for this function:

void C4ScenarioParameters::Clear ( )

Definition at line 169 of file C4ScenarioParameters.cpp.

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

170 {
171  Parameters.clear();
172 }

Here is the caller graph for this function:

void C4ScenarioParameters::CompileFunc ( StdCompiler pComp)

Definition at line 209 of file C4ScenarioParameters.cpp.

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

210 {
211  // Unfortunately, StdCompiler cannot save std::map yet
212  if (pComp->isDeserializer())
213  {
214  Parameters.clear();
215  if (pComp->hasNaming())
216  {
217  // load from INI
218  size_t name_count = pComp->NameCount();
219  for (size_t i=0; i<name_count; ++i)
220  {
221  int32_t v=0;
222  const char *name = pComp->GetNameByIndex(0); // always get name index 0, because names are removed after values have been extracted for them
223  StdCopyStrBuf sName(name);
224  if (!name) continue;
225  pComp->Value(mkNamingAdapt(v, sName.getData(), 0));
226  Parameters[sName] = v;
227  }
228  }
229  else
230  {
231  // load from binary
232  int32_t name_count=0;
233  pComp->Value(name_count);
234  for (int32_t i=0; i<name_count; ++i)
235  {
236  StdCopyStrBuf name; int32_t v;
237  pComp->Value(name);
238  pComp->Value(v);
239  Parameters[name] = v;
240  }
241  }
242  }
243  else
244  {
245  if (pComp->hasNaming())
246  {
247  // save to INI
248  for (auto i = Parameters.begin(); i != Parameters.end(); ++i)
249  pComp->Value(mkNamingAdapt(i->second, i->first.getData()));
250  }
251  else
252  {
253  // save to binary
254  int32_t name_count=Parameters.size();
255  pComp->Value(name_count);
256  for (auto i = Parameters.begin(); i != Parameters.end(); ++i)
257  {
258  pComp->Value(const_cast<StdCopyStrBuf &>(i->first));
259  pComp->Value(i->second);
260  }
261  }
262  }
263 }
virtual bool hasNaming()
Definition: StdCompiler.h:68
StdNamingAdapt< T > mkNamingAdapt(T &&rValue, const char *szName)
Definition: StdAdaptors.h:93
virtual const char * GetNameByIndex(size_t idx) const
Definition: StdCompiler.h:89
void Value(const T &rStruct)
Definition: StdCompiler.h:170
virtual bool isDeserializer()
Definition: StdCompiler.h:63
virtual int NameCount(const char *szName=nullptr)
Definition: StdCompiler.h:101

Here is the call graph for this function:

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

Definition at line 188 of file C4ScenarioParameters.cpp.

Referenced by C4ScenarioParameterDefs::RegisterScriptConstants().

189 {
190  // return map value if name is in map. Otherwise, return default value.
191  auto i = Parameters.find(StdStrBuf(id));
192  if (i != Parameters.end()) return i->second; else return default_value;
193 }

Here is the caller graph for this function:

void C4ScenarioParameters::Merge ( const C4ScenarioParameters other)

Definition at line 174 of file C4ScenarioParameters.cpp.

175 {
176  // Merge lists and keep larger value
177  for (auto i = other.Parameters.cbegin(); i != other.Parameters.cend(); ++i)
178  {
179  auto j = Parameters.find(i->first);
180  if (j != Parameters.end())
181  if (j->second >= i->second)
182  continue; // existing value is same or larger - keep old
183  // update to new value from other list
184  Parameters[i->first] = i->second;
185  }
186 }
void C4ScenarioParameters::SetValue ( const char *  id,
int32_t  value,
bool  only_if_larger 
)

Definition at line 195 of file C4ScenarioParameters.cpp.

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

196 {
197  if (only_if_larger)
198  {
199  auto i = Parameters.find(StdStrBuf(id));
200  if (i != Parameters.end())
201  if (i->second >= value)
202  // could become smaller. don't set.
203  return;
204  }
205  // just update map
206  Parameters[StdCopyStrBuf(id)] = value;
207 }

Here is the caller graph for this function:


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