OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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+1]; *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 
char * GetFilename(char *szPath)
Definition: StdFile.cpp:55
const char * getData() const
Definition: StdBuf.h:450
bool FindEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr)
Definition: C4Group.cpp:1774
C4Config Config
Definition: C4Config.cpp:837
C4ScenarioSection(const char *szName)
C4Game Game
Definition: C4Globals.cpp:52
bool CreatePath(const std::string &path)
Definition: StdFile.cpp:642
C4AulScriptEngine ScriptEngine
Definition: C4Globals.cpp:43
C4ConfigGeneral General
Definition: C4Config.h:252
bool SEqualNoCase(const char *szStr1, const char *szStr2, int iLen)
Definition: Standard.cpp:177
#define C4FLS_Section
Definition: C4Components.h:189
class C4ScenarioObjectsScriptHost * pScenarioObjectsScript
Definition: C4Game.h:92
bool Delete(const char *szFiles, bool fRecursive=false)
Definition: C4Group.cpp:1334
StdCopyStrBuf filename
Definition: C4Scenario.h:260
void Reg2List(C4AulScriptEngine *pEngine)
C4LangStringTable ScenarioLangStringTable
Definition: C4Game.h:82
#define C4FLS_SectionLandscape
Definition: C4Components.h:190
const char * C4ScenSect_Main
#define C4CFN_ScenarioObjectsScript
Definition: C4Components.h:74
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
C4Group ScenarioFile
Definition: C4Game.h:88
C4Group * GetGroupfile(C4Group &rGrp)
C4ScenarioSection * pScenarioSections
Definition: C4Game.h:93
bool Close()
Definition: C4Group.cpp:755
bool ScenarioLoad(const char *szFilename, bool is_temp_file)
#define _MAX_FNAME
char LanguageEx[CFG_MaxString+1]
Definition: C4Config.h:38
#define C4FLS_SectionObjects
Definition: C4Components.h:191
bool EnsureTempStore(bool fExtractLandscape, bool fExtractObjects)
StdCopyStrBuf name
Definition: C4Scenario.h:258
bool FindNextEntry(const char *szWildCard, StdStrBuf *sFileName=nullptr, size_t *iSize=nullptr, bool fStartAtFilename=false)
Definition: C4Group.cpp:1780
bool EraseItem(const char *szItemName)
Definition: StdFile.cpp:819
bool WildcardMatch(const char *szWildcard, const char *szString)
Definition: StdFile.cpp:384
bool ExtractEntry(const char *szFilename, const char *szExtractTo=nullptr)
Definition: C4Group.cpp:1518
class C4ScenarioObjectsScriptHost * pObjectScripts
Definition: C4Scenario.h:262
bool IsPacked() const
Definition: C4Group.cpp:2009
void ResetSearch(bool reload_contents=false)
Definition: C4Group.cpp:1013
const char * AtTempPath(const char *szFilename)
Definition: C4Config.cpp:544
void MakeTempFilename(char *szFilename)
Definition: StdFile.cpp:333
C4ScenarioSection * pNext
Definition: C4Scenario.h:264
virtual bool Load(C4Group &hGroup, const char *szFilename, const char *szLanguage, C4LangStringTable *pLocalTable)
void Copy()
Definition: StdBuf.h:475
StdCopyStrBuf temp_filename
Definition: C4Scenario.h:259
bool OpenAsChild(C4Group *pMother, const char *szEntryName, bool fExclusive=false, bool fCreate=false)
Definition: C4Group.cpp:1585