TerraForge3D  2.3.1
3D Terrain And Landscape Generator
ProjectData.cpp
1#include "Data/ProjectData.h"
2#include "Data/ApplicationState.h"
3#include "Utils/Utils.h"
4#include "Platform.h"
5
6#include <json/json.hpp>
7
8
9ProjectManager *ProjectManager::s_ProjectManager;
10
11ProjectManager *ProjectManager::Get()
12{
13 return s_ProjectManager;
14}
15
16ProjectManager::~ProjectManager()
17{
18}
19
20ProjectManager::ProjectManager(ApplicationState *as)
21{
22 appState = as;
23 s_ProjectManager = this;
24}
25
26void ProjectManager::SetId(std::string id)
27{
28 projectID = id;
29}
30
31std::string ProjectManager::GetId()
32{
33 return projectID;
34}
35
36nlohmann::json ProjectManager::GetDatabase()
37{
38 return projectDatase;
39}
40
41void ProjectManager::RegisterAsset(std::string uid, std::string path)
42{
43 projectDatase[uid] = path;
44}
45
46std::string ProjectManager::GetAsset(std::string id)
47{
48 if(projectDatase.find(id) != projectDatase.end())
49 {
50 std::string path = projectDatase[id];
51#ifndef TERR3D_WIN32
52 // replace \ with /
53 for(int i = 0; i < path.size(); i++)
54 {
55 if(path[i] == '\\')
56 {
57 path[i] = '/';
58 }
59 }
60#endif
61 return path;
62 }
63
64 return "";
65}
66
67bool ProjectManager::AssetExists(std::string id)
68{
69 return (projectDatase.find(id) != projectDatase.end());
70}
71
72void ProjectManager::SetDatabase(nlohmann::json db)
73{
74 projectDatase = db;
75}
76
77void ProjectManager::SaveDatabase()
78{
79 if (!PathExist(GetResourcePath()))
80 {
81 MkDir(GetResourcePath());
82 }
83
84 SaveToFile(GetResourcePath() + PATH_SEPARATOR "project_database.terr3d", projectDatase.dump());
85}
86
87std::string ProjectManager::GetResourcePath()
88{
89 return GetExecutableDir() + PATH_SEPARATOR "Data" PATH_SEPARATOR "cache" PATH_SEPARATOR "project_data" PATH_SEPARATOR "project_" + GetId();
90}
91
92std::string ProjectManager::SaveTexture(Texture2D *texture)
93{
94 return SaveResource("textures", texture->GetPath());
95}
96
97std::string ProjectManager::SaveResource(std::string folder, std::string path)
98{
99 if (path.size() <= 3)
100 {
101 return "";
102 }
103
104 std::string hash = MD5File(path).ToString();
105
106 if (GetAsset(hash).size() <= 0)
107 {
108 MkDir(GetResourcePath() + PATH_SEPARATOR + folder);
109 CopyFileData(path, GetResourcePath() + PATH_SEPARATOR + folder + PATH_SEPARATOR + hash);
110 RegisterAsset(hash, folder + PATH_SEPARATOR + hash);
111 }
112
113 return hash;
114}
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition: json.hpp:20117
iterator end() noexcept
returns an iterator to one past the last element
Definition: json.hpp:22448
iterator find(KeyT &&key)
find an element in a JSON object
Definition: json.hpp:22223
a class to store JSON values
Definition: json.hpp:17860
std::size_t hash(const BasicJsonType &j)
hash a JSON value
Definition: json.hpp:5240