Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
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/ProjectPropertyTypes.hpp"
13#include "../objects/ProjectFolder.hpp"
14#include "../objects/ProjectData.hpp"
15
16
17namespace tson
18{
19 class Project
20 {
21 public:
22 #ifdef JSON11_IS_DEFINED
23 inline explicit Project(std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>()) : m_json {std::move(jsonParser)}
24 {
25
26 }
27 inline explicit Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser = std::make_unique<tson::Json11>());
28 #else
29 inline explicit Project(std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
30 {
31
32 }
33 inline explicit Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser);
34 #endif
35 inline bool parse(const fs::path &path);
36 inline void parse();
37
38 [[nodiscard]] inline const ProjectData &getData() const;
39 [[nodiscard]] inline const fs::path &getPath() const;
40 [[nodiscard]] inline const std::vector<ProjectFolder> &getFolders() const;
41 [[nodiscard]] inline tson::EnumDefinition* getEnumDefinition(std::string_view name);
42 [[nodiscard]] inline tson::TiledClass* getClass(std::string_view name);
43
44
45 private:
46 inline void parseJson(IJson &json);
47 fs::path m_path;
48 std::vector<ProjectFolder> m_folders;
49 ProjectData m_data;
50 std::unique_ptr<IJson> m_json = nullptr;
51 };
52
53 Project::Project(const fs::path &path, std::unique_ptr<tson::IJson> jsonParser) : m_json {std::move(jsonParser)}
54 {
55 parse(path);
56 }
57
58 bool Project::parse(const fs::path &path)
59 {
60 m_path = path;
61 std::ifstream i(m_path.generic_string());
62
63 try
64 {
65 if(!m_json->parse(path))
66 return false;
67 }
68 catch(const std::exception &error)
69 {
70 std::string message = "Parse error: ";
71 message += std::string(error.what());
72 message += std::string("\n");
73 return false;
74 }
75 parseJson(*m_json);
76 return true;
77 }
78
80 {
81 return m_data;
82 }
83
84 void Project::parseJson(IJson &json)
85 {
86 m_data.basePath = (m_path.empty()) ? fs::path() : m_path.parent_path(); //The directory of the project file
87
88 //Make sure these property types are read before any map is, so they can be resolved.
89 if(json.count("propertyTypes") > 0)
90 {
91 m_data.projectPropertyTypes.parse(json, this);
92 }
93
94 if(json.count("automappingRulesFile") > 0) m_data.automappingRulesFile = json["automappingRulesFile"].get<std::string>();
95 if(json.count("commands") > 0)
96 {
97 m_data.commands.clear();
98 auto &commands = json.array("commands");
99 std::for_each(commands.begin(), commands.end(), [&](std::unique_ptr<IJson> &item)
100 {
101 m_data.commands.emplace_back(item->get<std::string>());
102 });
103 }
104 if(json.count("extensionsPath") > 0) m_data.extensionsPath = json["extensionsPath"].get<std::string>();
105 if(json.count("folders") > 0)
106 {
107 m_data.folders.clear();
108 m_data.folderPaths.clear();
109 auto &folders = json.array("folders");
110 std::for_each(folders.begin(), folders.end(), [&](std::unique_ptr<IJson> &item)
111 {
112 std::string folder = item->get<std::string>();
113 m_data.folders.emplace_back(folder);
114 m_data.folderPaths.emplace_back(m_data.basePath / folder);
115 m_folders.emplace_back(m_data.basePath / folder);
116 });
117 }
118 if(json.count("objectTypesFile") > 0) m_data.objectTypesFile = json["objectTypesFile"].get<std::string>();
119 }
120
121 const fs::path &Project::getPath() const
122 {
123 return m_path;
124 }
125
126 const std::vector<ProjectFolder> &Project::getFolders() const
127 {
128 return m_folders;
129 }
130
132 {
133 return m_data.projectPropertyTypes.getEnumDefinition(name);
134 }
135
136 tson::TiledClass *Project::getClass(std::string_view name)
137 {
138 return m_data.projectPropertyTypes.getClass(name);
139 }
140
146 {
147 parseJson(*m_json);
148 }
149
150}
151
152#endif //TILESON_PROJECT_HPP
Definition TiledEnum.hpp:11
Definition IJson.hpp:11
T get(std::string_view key)
Definition IJson.hpp:82
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
ProjectPropertyTypes projectPropertyTypes
Definition ProjectData.hpp:19
std::vector< std::string > folders
Definition ProjectData.hpp:17
fs::path basePath
Definition ProjectData.hpp:22
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:23
tson::EnumDefinition * getEnumDefinition(std::string_view name)
Definition ProjectPropertyTypes.hpp:86
tson::TiledClass * getClass(std::string_view name)
Definition ProjectPropertyTypes.hpp:99
bool parse(IJson &json, tson::Project *project)
Definition ProjectPropertyTypes.hpp:29
Definition Project.hpp:20
const ProjectData & getData() const
Definition Project.hpp:79
tson::EnumDefinition * getEnumDefinition(std::string_view name)
Definition Project.hpp:131
void parse()
Definition Project.hpp:145
const std::vector< ProjectFolder > & getFolders() const
Definition Project.hpp:126
tson::TiledClass * getClass(std::string_view name)
Definition Project.hpp:136
const fs::path & getPath() const
Definition Project.hpp:121
Project(std::unique_ptr< tson::IJson > jsonParser)
Definition Project.hpp:29
Definition TiledClass.hpp:11
Definition Base64.hpp:12