OpenClonk
C4ScenarioSection.cpp
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 #include "C4Include.h"
19 #include "landscape/C4Scenario.h"
20 #include "c4group/C4Components.h"
21 #include "script/C4ScriptHost.h"
22 
23 // scenario sections
24 
25 const char *C4ScenSect_Main = "main";
26 
28 {
29  // copy name
30  name.Copy(szName);
31  // zero fields
32  fModified = false;
33  pObjectScripts = nullptr;
34  // link into main list
36  Game.pScenarioSections = this;
37 }
38 
40 {
41  // del following scenario sections
42  while (pNext)
43  {
44  C4ScenarioSection *pDel = pNext;
45  pNext = pNext->pNext;
46  pDel->pNext = nullptr;
47  delete pDel;
48  }
49  // del temp file
50  if (temp_filename.getData())
51  {
53  }
54 }
55 
56 bool C4ScenarioSection::ScenarioLoad(const char *szFilename, bool is_temp_file)
57 {
58  // safety
59  if (filename.getData() || !szFilename) return false;
60  // store name
61  filename.Copy(szFilename);
62  if (is_temp_file)
63  {
64  // temp: Remember temp filename to be deleted on destruction
65  temp_filename.Copy(szFilename);
66  }
67  else
68  {
69  // non-temp: extract if it's not an open folder
70  if (Game.ScenarioFile.IsPacked()) if (!EnsureTempStore(true, true)) return false;
71  }
72  // load section object script
74  {
75  C4Group GrpSect, *pGrp;
76  if ((pGrp = GetGroupfile(GrpSect)) && pGrp->FindEntry(C4CFN_ScenarioObjectsScript))
77  {
81  }
82  else
83  {
84  pObjectScripts = nullptr;
85  }
86  }
87  else
88  {
89  // Object script for main section
91  }
92  // donce, success
93  return true;
94 }
95 
97 {
98  // check temp filename
99  if (temp_filename.getData())
100  {
101  if (rGrp.Open(temp_filename.getData())) return &rGrp;
102  else return nullptr;
103  }
104  // check filename within scenario
105  if (filename.getData())
106  {
107  if (rGrp.OpenAsChild(&Game.ScenarioFile, filename.getData())) return &rGrp;
108  else return nullptr;
109  }
110  // unmodified main section: return main group
112  // failure
113  return nullptr;
114 }
115 
116 bool C4ScenarioSection::EnsureTempStore(bool fExtractLandscape, bool fExtractObjects)
117 {
118  // if it's temp store already, don't do anything
119  if (temp_filename.getData()) return true;
120  // make temp filename
121  char *szTmp = const_cast<char *>(Config.AtTempPath(filename.getData() ? GetFilename(filename.getData()) : name.getData()));
122  MakeTempFilename(szTmp);
123  // main section: extract section files from main scenario group (create group as open dir)
124  if (!filename.getData())
125  {
126  if (!CreatePath(szTmp)) return false;
127  C4Group hGroup;
128  if (!hGroup.Open(szTmp, true)) { EraseItem(szTmp); return false; }
129  // extract all desired section files
131  char fn[_MAX_FNAME_LEN]; *fn=0;
133  if (fExtractLandscape || !WildcardMatch(C4FLS_SectionLandscape, fn))
134  if (fExtractObjects || !WildcardMatch(C4FLS_SectionObjects, fn))
135  Game.ScenarioFile.ExtractEntry(fn, szTmp);
136  hGroup.Close();
137  }
138  else
139  {
140  // subsection: simply extract section from main group
141  if (!Game.ScenarioFile.ExtractEntry(filename.getData(), szTmp)) return false;
142  // delete undesired landscape/object files
143  if (!fExtractLandscape || !fExtractObjects)
144  {
145  C4Group hGroup;
146  if (hGroup.Open(filename.getData()))
147  {
148  if (!fExtractLandscape) hGroup.Delete(C4FLS_SectionLandscape);
149  if (!fExtractObjects) hGroup.Delete(C4FLS_SectionObjects);
150  }
151  }
152  }
153  // copy temp filename
154  temp_filename.Copy(szTmp);
155  // done, success
156  return true;
157 }
158 
#define C4CFN_ScenarioObjectsScript
Definition: C4Components.h:74
#define C4FLS_Section
Definition: C4Components.h:190
#define C4FLS_SectionObjects
Definition: C4Components.h:192
#define C4FLS_SectionLandscape
Definition: C4Components.h:191
C4Config Config
Definition: C4Config.cpp:930
C4Game Game
Definition: C4Globals.cpp:52
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
const char * C4ScenSect_Main
#define _MAX_FNAME_LEN
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:213
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:833
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:396
void MakeTempFilename(char *szFilename)
Definition: StdFile.cpp:320
char * GetFilename(char *szPath)
Definition: StdFile.cpp:42
bool CreatePath(const std::string &path)
Definition: StdFile.cpp:656
char LanguageEx[CFG_MaxString+1]
Definition: C4Config.h:38
C4ConfigGeneral General
Definition: C4Config.h:255
const char * AtTempPath(const char *filename)
Definition: C4Config.cpp:600
class C4ScenarioObjectsScriptHost * pScenarioObjectsScript
Definition: C4Game.h:90
C4LangStringTable ScenarioLangStringTable
Definition: C4Game.h:80
C4ScenarioSection * pScenarioSections
Definition: C4Game.h:91
C4Group ScenarioFile
Definition: C4Game.h:86
bool FindNextEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr, bool start_at_filename=false)
Definition: C4Group.cpp:2217
bool IsPacked() const
Definition: C4Group.cpp:2541
bool ExtractEntry(const char *filename, const char *destination=nullptr)
Definition: C4Group.cpp:1870
bool OpenAsChild(C4Group *mother, const char *entry_name, bool is_exclusive=false, bool do_create=false)
Definition: C4Group.cpp:1952
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1316
bool Close()
Definition: C4Group.cpp:971
bool Delete(const char *files, bool recursive=false)
Definition: C4Group.cpp:1645
bool FindEntry(const char *wildcard, StdStrBuf *filename=nullptr, size_t *size=nullptr)
Definition: C4Group.cpp:2211
bool Open(const char *group_name, bool do_create=false)
Definition: C4Group.cpp:660
bool ScenarioLoad(const char *szFilename, bool is_temp_file)
bool EnsureTempStore(bool fExtractLandscape, bool fExtractObjects)
StdCopyStrBuf name
Definition: C4Scenario.h:260
class C4ScenarioObjectsScriptHost * pObjectScripts
Definition: C4Scenario.h:264
StdCopyStrBuf filename
Definition: C4Scenario.h:262
C4ScenarioSection * pNext
Definition: C4Scenario.h:266
StdCopyStrBuf temp_filename
Definition: C4Scenario.h:261
C4Group * GetGroupfile(C4Group &rGrp)
C4ScenarioSection(const char *szName)
virtual bool Load(C4Group &hGroup, const char *szFilename, const char *szLanguage, C4LangStringTable *pLocalTable)
void Reg2List(C4AulScriptEngine *pEngine)
const char * getData() const
Definition: StdBuf.h:442
void Copy()
Definition: StdBuf.h:467