OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Reloc.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
5  *
6  * Distributed under the terms of the ISC license; see accompanying file
7  * "COPYING" for details.
8  *
9  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
10  * See accompanying file "TRADEMARK" for details.
11  *
12  * To redistribute this file separately, substitute the full license texts
13  * for the above references.
14  */
15 
16 #include "C4Include.h"
17 #include "config/C4Reloc.h"
18 
19 #include "game/C4Application.h"
20 
21 C4Reloc Reloc; // singleton
22 
24 {
25  Paths.clear();
26 
27  // The system folder (i.e. installation path) has higher priority than the user path
28  // Although this is counter-intuitive (the user may want to overload system files in the user path),
29  // people had trouble when they downloaded e.g. an Objects.ocd file in a network lobby and that copy permanently
30  // ruined their OpenClonk installation with no obvious way to fix it.
31  // Not even reinstalling would fix the problem because reinstallation does not overwrite user data.
32  // We currently don't have any valid case where overloading system files would make sense so just give higher priority to the system path for now.
33 #ifndef __APPLE__
34  // Add planet subfolder with highest priority because it's used when starting directly from the repository with binaries in the root folder
36  planet.AppendBackslash();
37  planet.Append("planet");
38  if (DirectoryExists(planet.getData()))
39  {
40  // Only add planet if it's a valid contents folder.
41  // Because users may create a folder "planet" in their source repos.
42  StdCopyStrBuf planet_system_check(planet);
43  planet_system_check.AppendBackslash();
44  planet_system_check.Append(C4CFN_System);
45  if (ItemExists(planet_system_check.getData()))
46  {
47  AddPath(planet.getData());
48  }
49  }
50 #endif
51  // Add main system path (unless it's using planet/ anyway, in which we would just slow down scenario enumeration by looking throug hthe whole source folder)
52  if (!Paths.size())
53  {
55  }
56  // Add user path for additional data (player files, user scenarios, etc.)
58 }
59 
60 bool C4Reloc::AddPath(const char* path, PathType pathType)
61 {
62  if(!IsGlobalPath(path))
63  return false;
64 
65  if(std::find(Paths.begin(), Paths.end(), path) != Paths.end())
66  return false;
67 
68  Paths.emplace_back(StdCopyStrBuf(path), pathType);
69  return true;
70 }
71 
73 {
74  return Paths.begin();
75 }
76 
78 {
79  return Paths.end();
80 }
81 
82 bool C4Reloc::Open(C4Group& hGroup, const char* filename) const
83 {
84  if(IsGlobalPath(filename)) return hGroup.Open(filename);
85 
86  for(const auto & iter : *this)
87  if(hGroup.Open((iter.strBuf + DirSep + filename).getData()))
88  return true;
89 
90  return false;
91 }
92 
93 bool C4Reloc::LocateItem(const char* filename, StdStrBuf& str) const
94 {
95  if(IsGlobalPath(filename))
96  {
97  str.Copy(filename);
98  return true;
99  }
100 
101  for(const auto & iter : *this)
102  {
103  str.Copy(iter.strBuf + DirSep + filename);
104  if(ItemExists(str.getData()))
105  return true;
106  }
107 
108  return false;
109 }
const char * getData() const
Definition: StdBuf.h:442
C4Config Config
Definition: C4Config.cpp:833
bool IsGlobalPath(const char *szPath)
Definition: StdFile.cpp:227
C4ConfigGeneral General
Definition: C4Config.h:251
PathList::const_iterator iterator
Definition: C4Reloc.h:36
void AppendBackslash()
Definition: StdBuf.cpp:248
char UserDataPath[CFG_MaxString+1]
Definition: C4Config.h:54
bool Open(const char *szGroupName, bool fCreate=false)
Definition: C4Group.cpp:514
void Append(const char *pnData, size_t iChars)
Definition: StdBuf.h:519
char SystemDataPath[CFG_MaxString+1]
Definition: C4Config.h:55
bool Open(C4Group &hGroup, const char *filename) const
Definition: C4Reloc.cpp:82
iterator begin() const
Definition: C4Reloc.cpp:72
C4Reloc Reloc
Definition: C4Reloc.cpp:21
void Init()
Definition: C4Reloc.cpp:23
bool DirectoryExists(const char *szFilename)
Definition: StdFile.cpp:684
bool AddPath(const char *path, PathType pathType=PATH_Regular)
Definition: C4Reloc.cpp:60
iterator end() const
Definition: C4Reloc.cpp:77
StdCopyStrBuf ExePath
Definition: C4Config.h:52
#define C4CFN_System
Definition: C4Components.h:29
#define DirSep
bool ItemExists(const char *szItemName)
Definition: StdFile.h:75
void Copy()
Definition: StdBuf.h:467
PathType
Definition: C4Reloc.h:22
bool LocateItem(const char *filename, StdStrBuf &str) const
Definition: C4Reloc.cpp:93