Tileson  1.3.0
A helpful json parser for Tiled maps
Project.hpp
Go to the documentation of this file.
1 //
2 // Created by robin on 01.08.2020.
3 //
4 
5 #ifndef TILESON_PROJECT_HPP
6 #define TILESON_PROJECT_HPP
7 
8 #include <fstream>
9 #include <sstream>
10 #include <memory>
11 #include "World.hpp"
12 #include "../objects/ProjectFolder.hpp"
13 #include "../objects/ProjectData.hpp"
14 
15 namespace tson
16 {
17  class Project
18  {
19  public:
20  #ifdef JSON11_IS_DEFINED
21  inline explicit Project(std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>()) : m_json {std::move(jsonParser)}
22  {
23 
24  }
25  inline explicit Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>());
26  #else
27  inline explicit Project(std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
28  {
29 
30  }
31  inline explicit Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser);
32  #endif
33  inline bool parse(const fs::path &path);
34 
35  [[nodiscard]] inline const ProjectData &getData() const;
36  [[nodiscard]] inline const fs::path &getPath() const;
37  [[nodiscard]] inline const std::vector<ProjectFolder> &getFolders() const;
38 
39  private:
40  inline void parseJson(IJson &json);
41  fs::path m_path;
42  std::vector<ProjectFolder> m_folders;
43  ProjectData m_data;
44  std::unique_ptr<IJson> m_json = nullptr;
45  };
46 
47  Project::Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
48  {
49  parse(path);
50  }
51 
52  bool Project::parse(const fs::path &path)
53  {
54  m_path = path;
55  std::ifstream i(m_path.u8string());
56 
57  try
58  {
59  if(!m_json->parse(path))
60  return false;
61  }
62  catch(const std::exception &error)
63  {
64  std::string message = "Parse error: ";
65  message += std::string(error.what());
66  message += std::string("\n");
67  return false;
68  }
69  parseJson(*m_json);
70  return true;
71  }
72 
74  {
75  return m_data;
76  }
77 
78  void Project::parseJson(IJson &json)
79  {
80  m_data.basePath = m_path.parent_path(); //The directory of the project file
81 
82  if(json.count("automappingRulesFile") > 0) m_data.automappingRulesFile = json["automappingRulesFile"].get<std::string>();
83  if(json.count("commands") > 0)
84  {
85  m_data.commands.clear();
86  auto &commands = json.array("commands");
87  std::for_each(commands.begin(), commands.end(), [&](std::unique_ptr<IJson> &item)
88  {
89  m_data.commands.emplace_back(item->get<std::string>());
90  });
91  }
92  if(json.count("extensionsPath") > 0) m_data.extensionsPath = json["extensionsPath"].get<std::string>();
93  if(json.count("folders") > 0)
94  {
95  m_data.folders.clear();
96  m_data.folderPaths.clear();
97  auto &folders = json.array("folders");
98  std::for_each(folders.begin(), folders.end(), [&](std::unique_ptr<IJson> &item)
99  {
100  std::string folder = item->get<std::string>();
101  m_data.folders.emplace_back(folder);
102  m_data.folderPaths.emplace_back(m_data.basePath / folder);
103  m_folders.emplace_back(m_data.basePath / folder);
104  });
105  }
106  if(json.count("objectTypesFile") > 0) m_data.objectTypesFile = json["objectTypesFile"].get<std::string>();
107 
108  }
109 
110  const fs::path &Project::getPath() const
111  {
112  return m_path;
113  }
114 
115  const std::vector<ProjectFolder> &Project::getFolders() const
116  {
117  return m_folders;
118  }
119 
120 
121 }
122 
123 #endif //TILESON_PROJECT_HPP
Definition: IJson.hpp:11
T get(std::string_view key)
Definition: IJson.hpp:72
virtual size_t count(std::string_view key) const =0
virtual std::vector< std::unique_ptr< IJson > > array()=0
Definition: ProjectData.hpp:11
std::string automappingRulesFile
Definition: ProjectData.hpp:14
std::vector< std::string > folders
Definition: ProjectData.hpp:17
fs::path basePath
Definition: ProjectData.hpp:21
std::string objectTypesFile
Definition: ProjectData.hpp:18
std::vector< std::string > commands
Definition: ProjectData.hpp:15
std::string extensionsPath
Definition: ProjectData.hpp:16
std::vector< tson::ProjectFolder > folderPaths
Definition: ProjectData.hpp:22
Definition: Project.hpp:18
const ProjectData & getData() const
Definition: Project.hpp:73
const std::vector< ProjectFolder > & getFolders() const
Definition: Project.hpp:115
const fs::path & getPath() const
Definition: Project.hpp:110
Project(std::unique_ptr< tson::IJson > jsonParser)
Definition: Project.hpp:27
bool parse(const fs::path &path)
Definition: Project.hpp:52
Definition: Base64.hpp:12