6#include "../MacroDef.h"
7#include "../Util/AssetsDef.h"
8#include "../Util/MoreString.h"
9#include "../Util/SafeLog.h"
10#include "../Util/Loader/XML/XML.h"
11#include "EngineCore.h"
16#if defined(__linux__) || defined(__APPLE_CC__)
27#include <android/asset_manager.h>
28#include <android/asset_manager_jni.h>
29#include <android/log.h>
30#include <Manager/ResMgr.h>
31#include <Util/SafeLog.h>
36AssetMgr::AssetMgr() =
default;
38AssetMgr::~AssetMgr() {
43void AssetMgr::Exterminate() {
46 for (
auto& asset : m_assetsList) {
51 SAFE_DELETE(m_assetManager);
58void AssetMgr::LoadAssets(
bool isPacked) {
61 ReadDirectory(CSE::NativeAssetsPath());
63 ReadPackage(CSE::NativeAssetsPath() +
"Assets.zip");
70 if (m_assets.count(name) > 0)
return m_assets.at(name);
72 std::string lowerName = name;
73 make_lower(lowerName);
74 for (
const auto& asset : m_assetsList) {
75 if (make_lower_copy(asset->name) == lowerName)
return asset;
76 if (make_lower_copy(asset->id) == lowerName)
return asset;
77 if (make_lower_copy(asset->name_path) == lowerName)
return asset;
78 if (make_lower_copy(asset->name_full) == lowerName)
return asset;
80 auto errorLog =
"[Assets Warning] " + name +
" does not exist.";
81 SafeLog::Log(errorLog.c_str());
85void AssetMgr::ReadDirectory(
const std::string& path) {
88 AAssetDir* assetDir = AAssetManager_openDir(m_assetManager,
"");
89 const char* filename = (
const char*)NULL;
90 SafeLog::Log(
"ReadDirectory");
91 while ((filename = AAssetDir_getNextFileName(assetDir)) != NULL) {
92 AAsset* asset = AAssetManager_open(m_assetManager, filename, AASSET_MODE_STREAMING);
94 std::string name = filename;
95 SafeLog::Log(name.c_str());
96 AssetReference* assetReference = CreateAsset(path, name);
100 AAssetDir_close(assetDir);
101#elif defined(__linux__) || defined(__APPLE_CC__)
103 DIR* dirp = opendir(path.c_str());
105 while ((dp = readdir(dirp)) !=
nullptr) {
107 std::string name = dp->d_name;
109 if (dp->d_type == DT_DIR) {
110 if (name ==
"." || name ==
"..")
continue;
112 ReadDirectory(path + name +
'/');
116 AssetReference* asset = CreateAsset(path, name);
117 if (asset ==
nullptr)
continue;
118 std::cout <<
"[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
126 WIN32_FIND_DATA data;
127 HANDLE hFind = FindFirstFile(std::string(path +
'*').c_str(), &data);
128 if (hFind != INVALID_HANDLE_VALUE) {
130 std::string name = data.cFileName;
132 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
133 if (name ==
"." || name ==
"..")
continue;
135 ReadDirectory(path + name +
'/');
139 AssetReference* asset = CreateAsset(path, name);
140 if (asset ==
nullptr)
continue;
141 std::cout <<
"[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
143 }
while (FindNextFile(hFind, &data) != 0);
149void AssetMgr::ReadPackage(
const std::string& path) {
150#if defined(__ANDROID__) || defined(IOS)
151 if(m_zip ==
nullptr) {
152 SafeLog::Log(path.c_str());
153 m_package_raw = OpenNativeAssetsTxtFile(path);
154 m_zip =
zip_stream_open(m_package_raw.c_str(), m_package_raw.length(), 0,
'r');
157 if (m_zip ==
nullptr) m_zip =
zip_open(path.c_str(), 0,
'r');
160 for (
int i = 0; i < zipSize; ++i) {
164 std::string name_str = name;
165 auto rFindIndex = name_str.rfind(
'/');
166 std::string name_cropped = name_str;
167 std::string path_str =
"";
168 if (rFindIndex != std::string::npos) {
169 name_cropped = name_str.substr(rFindIndex + 1);
170 path_str = name_str.substr(0, rFindIndex + 1);
173 if (isdir != 0)
continue;
174 AssetReference* asset = CreateAsset(path_str, name_cropped);
175 if (asset ==
nullptr)
continue;
176 std::cout <<
"(Packed)[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
183AssetMgr::CreateAsset(
const std::string& path,
const std::string& name_full, std::string name) {
184 auto asset =
new AssetReference();
186 asset->name_path = path + name_full;
187 asset->id =
"File:" + asset->name_path.substr(CSE::AssetsPath().size());
188 asset->name_full = name_full;
192 auto name_strs = split(name,
'.');
193 asset->extension = name_strs[name_strs.size() - 1];
194 asset->name = name.substr(0, name.rfind(
'.'));
200 if (asset->extension ==
"meta") {
205 asset->hash = GetAssetHash(asset->name_path);
207 m_assets.insert(std::pair<std::string, AssetReference*>(asset->hash, asset));
208 m_assetsList.push_back(asset);
213void AssetMgr::SetType() {
215 for (
const auto asset : m_assetsList) {
216 std::string type_str = asset->extension;
217 make_lower(type_str);
221 if (type_str ==
"jpg" || type_str ==
"png" || type_str ==
"dds" || type_str ==
"hdr") {
222 asset->type = TEX_2D;
223 asset->name +=
".texture";
228 if (type_str ==
"cbmap") {
229 asset->type = TEX_CUBEMAP;
230 asset->name +=
".textureCubeMap";
235 if (type_str ==
"framebuffer") {
236 asset->type = FRAMEBUFFER;
237 asset->name +=
".frameBuffer";
242 if (type_str ==
"mat") {
243 asset->type = MATERIAL;
244 asset->name +=
".material";
249 if (type_str ==
"dae") {
251 asset->name +=
".prefab";
253 AppendSubName(CreateAsset(asset->name_path, asset->name_full, asset->name),
"animation")->type = DAE;
254 AppendSubName(CreateAsset(asset->name_path, asset->name_full, asset->name),
"skeleton")->type = DAE;
255 AppendSubName(CreateAsset(asset->name_path, asset->name_full, asset->name),
"mesh")->type = DAE;
261 if (type_str ==
"nut") {
262 asset->type = SCRIPT;
263 asset->name +=
".script";
268 if (type_str ==
"vert" || type_str ==
"vs") {
269 asset->type = SHADER;
270 asset->name +=
".vert";
273 if (type_str ==
"frag" || type_str ==
"fs") {
274 asset->type = SHADER;
275 asset->name +=
".frag";
280 if (type_str ==
"shader") {
281 asset->type = SHADER_HANDLE;
282 asset->name +=
".shader";
287 if (type_str ==
"prefab") {
288 asset->type = PREFAB;
289 asset->name +=
".prefab";
294 if (type_str ==
"scene") {
296 asset->name +=
".scene";
301 if (type_str ==
"ini") {
303 asset->name +=
".ini";
307 if (asset->type == NONE) {
310 asset->name +=
".text";
318 std::string sub =
'?' + sub_name;
324std::list<AssetMgr::AssetReference*> AssetMgr::GetAssets(TYPE type)
const {
325 std::list<AssetReference*> result;
327 for (
const auto& asset : m_assetsList) {
328 if (asset->type == type) {
329 result.push_back(asset);
336std::string AssetMgr::LoadAssetFile(
const std::string& path) {
337 if (Settings::IsAssetsPacked() ==
false)
338 return OpenNativeAssetsTxtFile(path);
341 std::string path_convert;
342 const auto& zip = CORE->GetResMgrCore()->m_assetManager->m_zip;
343 if (path.find(
"/../") != std::string::npos) {
344 auto path_split = split(path,
'/');
345 std::stack<std::string> path_stack;
346 for (
const auto& part : path_split) {
351 path_stack.push(part);
353 while (!path_stack.empty()) {
354 path_convert = path_stack.top() +
'/' + path_convert;
357 path_convert = path_convert.substr(0, path_convert.size() - 1);
368 result = std::string(buf, bufsize);
375std::string AssetMgr::GetAssetHash(
const std::string& path) {
376 std::string meta = LoadAssetFile(path +
".meta");
377 std::string&& hash =
"";
379 const XNode* root =
XFILE().loadBuffer(std::move(meta));
380 const auto& hashData = root->getNode(
"hash-data");
381 hash = hashData.getAttribute(
"hash").value;
384 hash = GetRandomHash(16);
385 std::string meta =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
386 "<CSEMETA version=\"1.0.0\">\n"
387 "<hash-data hash=\"" + hash +
"\">\n"
388 "\n</hash-data>\n</CSEMETA>";
389 if (!Settings::IsAssetsPacked())
390 SaveTxtFile(path +
".meta", meta);
398void AssetMgr::SetAssetManager(AAssetManager* obj) {
399 m_assetManager = obj;
403AAssetManager * AssetMgr::GetAssetManager() {
404 return m_assetManager;
407void AssetMgr::SetEnv(JNIEnv *obj) {
411JNIEnv *AssetMgr::GetEnv() {
int zip_entry_isdir(struct zip_t *zip)
const char * zip_entry_name(struct zip_t *zip)
struct zip_t * zip_open(const char *zipname, int level, char mode)
struct zip_t * zip_stream_open(const char *stream, size_t size, int level, char mode)
void zip_close(struct zip_t *zip)
int zip_entry_open(struct zip_t *zip, const char *entryname)
int zip_entry_openbyindex(struct zip_t *zip, int index)
int zip_entries_total(struct zip_t *zip)
int zip_entry_close(struct zip_t *zip)
ssize_t zip_entry_read(struct zip_t *zip, void **buf, size_t *bufsize)