-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathResourceLoader.cpp
141 lines (106 loc) · 3.9 KB
/
ResourceLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// Created by per-arne on 02.04.17.
//
#include <fstream>
#include <iostream>
#include "util/String.h"
#include "ResourceLoader.h"
#include <cstdio>
#include <string>
#ifdef _WIN32
#include <direct.h>
#define GetCurrentDir _getcwd
#elif defined __linux__ || defined __APPLE__
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include <cmrc/cmrc.hpp>
CMRC_DECLARE(DeepRTSAssets);
using namespace DeepRTS;
std::string GetCurrentWorkingDir()
{
char buff[FILENAME_MAX];
GetCurrentDir(buff, FILENAME_MAX);
std::string current_working_dir(buff);
return current_working_dir;
}
std::string ResourceLoader::getFilePath(const std::string& fileName) {
std::string workingDir = GetCurrentWorkingDir();
std::string assetLocation = "python";
if (workingDir.find(assetLocation) != std::string::npos) {
// asset location is included (happens in python runtime)
return workingDir + "/assets/" + fileName;
} else {
// Did not find asset location in Working directory
return workingDir + "/" + assetLocation + "/assets/" + fileName;
}
}
void ResourceLoader::loadMapJSON(const std::string& mapFile) {
// If map is already loaded
if(mapLoaded) {
return;
}
try{
auto fs = cmrc::DeepRTSAssets::get_filesystem();
std::string fileName = "assets/maps/" + mapFile;
auto tilePropertiesFile = fs.open(fileName);
std::string tilePropertiesData = std::string(tilePropertiesFile.begin(), tilePropertiesFile.size());
try{
mapJSON = nlohmann::json::parse(tilePropertiesData);
}catch(const nlohmann::json::exception&){
throw std::runtime_error("Failed to parse: " + mapFile + " (" + fileName + ").");
}
mapLoaded = true;
}catch(const std::exception& err){
// Retrieve map file location
auto mapFilePath = getFilePath("maps/" + mapFile);
// Read Map data
std::ifstream map(mapFilePath);
if (map.is_open()) {
try{
mapJSON = nlohmann::json::parse(map);
}catch(const nlohmann::json::exception&){
throw std::runtime_error("Failed to parse: " + mapFile + " (" + mapFilePath + ").");
}
mapLoaded = true;
}
else {
throw std::runtime_error("File Error: Could not find: " + mapFile + " (" + mapFilePath + ").\nEnsure that the data directory exists!");
}
}
}
TilePropertyData ResourceLoader::loadMAPJson(const nlohmann::json& tJSON){
TilePropertyData data;
// Parse tile types
for(auto &item : tJSON["tile_types"].items()){
for(auto &ele : item.value()){
if(ele.is_string()){
auto range = StringUtil::split(ele.get<std::string>(), "-");
for(auto i = std::stoi(range[0]); i <= std::stoi(range[1]); i++){
data.tileID2Type.insert(std::make_pair(i, item.key()));
}
}else if(ele.is_number_integer()){
data.tileID2Type.insert(std::make_pair(ele.get<int>(), item.key()));
}
}
}
// Parse tile data
for(auto &item : tJSON["tile_metadata"].items()){
//std::cout << item.key() << std::endl;
data.tileData.insert(std::make_pair(item.key(), item.value()));
}
return data;
}
void ResourceLoader::loadTileJSON() {
auto fs = cmrc::DeepRTSAssets::get_filesystem();
std::string fileName = "assets/tile_properties.json";
auto tilePropertiesFile = fs.open(fileName);
std::string tilePropertiesData = std::string(tilePropertiesFile.begin(), tilePropertiesFile.size());
try{
tileJSON = nlohmann::json::parse(tilePropertiesData);
}catch(const nlohmann::json::exception&){
throw std::runtime_error("Failed to parse: " + fileName + ".");
}
tileData = ResourceLoader::loadMAPJson(tileJSON);
tilesLoaded = true;
}