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 CreateAssetFolder(path, name);
114 ReadDirectory(path + name +
'/');
118 AssetReference* asset = CreateAsset(path, name);
119 if (asset ==
nullptr)
continue;
120 std::cout <<
"[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
128 WIN32_FIND_DATA data;
129 HANDLE hFind = FindFirstFile(std::string(path +
'*').c_str(), &data);
130 if (hFind != INVALID_HANDLE_VALUE) {
132 std::string name = data.cFileName;
134 if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
135 if (name ==
"." || name ==
"..")
continue;
137 CreateAssetFolder(path, name);
139 ReadDirectory(path + name +
'/');
143 AssetReference* asset = CreateAsset(path, name);
144 if (asset ==
nullptr)
continue;
145 std::cout <<
"[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
147 }
while (FindNextFile(hFind, &data) != 0);
153void AssetMgr::ReadPackage(
const std::string& path) {
154#if defined(__ANDROID__) || defined(IOS)
155 if(m_zip ==
nullptr) {
156 SafeLog::Log(path.c_str());
157 m_package_raw = OpenNativeAssetsTxtFile(path);
158 m_zip =
zip_stream_open(m_package_raw.c_str(), m_package_raw.length(), 0,
'r');
161 if (m_zip ==
nullptr) m_zip =
zip_open(path.c_str(), 0,
'r');
164 for (
int i = 0; i < zipSize; ++i) {
170 std::string name_str = name;
171 if(name_str.find(
"__MACOSX/") != std::string::npos)
continue;
172 if(isdir != 0) name_str = name_str.substr(0, name_str.size() - 1);
173 auto rFindIndex = name_str.rfind(
'/');
174 std::string name_cropped = name_str;
175 std::string path_str =
"";
176 if (rFindIndex != std::string::npos) {
177 name_cropped = name_str.substr(rFindIndex + 1);
178 path_str = name_str.substr(0, rFindIndex + 1);
182 CreateAssetFolder(path_str, name_cropped);
186 AssetReference* asset = CreateAsset(path_str, name_cropped);
187 if (asset ==
nullptr)
continue;
188 std::cout <<
"(Packed)[pkg] " << asset->name <<
" (" << asset->extension <<
")\n";
195AssetMgr::CreateAsset(
const std::string& path,
const std::string& name_full, std::string name) {
196 auto asset =
new AssetReference();
198 asset->name_path = path + name_full;
199 asset->id =
"File:" + asset->name_path.substr(CSE::AssetsPath().size());
200 asset->name_full = name_full;
204 auto name_strs = split(name,
'.');
205 asset->extension = name_strs[name_strs.size() - 1];
206 asset->name = name.substr(0, name.rfind(
'.'));
212 if (asset->extension ==
"meta") {
217 asset->hash = GetAssetHash(asset->name_path);
219 m_assets.insert(std::pair<std::string, AssetReference*>(asset->hash, asset));
220 m_assetsList.push_back(asset);
226 auto asset =
new AssetReference();
228 asset->name_path = path + name_full;
229 asset->extension =
"/\\?folder";
230 asset->id =
"Folder:" + asset->name_path.substr(CSE::AssetsPath().size());
231 asset->name_full = name_full;
232 asset->name = name_full;
234 asset->hash =
"FOLDER" + GetRandomHash(10);
236 m_assets.insert(std::pair<std::string, AssetReference*>(asset->hash, asset));
237 m_assetsList.push_back(asset);
242void AssetMgr::SetType() {
244 for (
const auto asset: m_assetsList) {
245 std::string type_str = asset->extension;
246 make_lower(type_str);
250 if (type_str ==
"jpg" || type_str ==
"png" || type_str ==
"dds" || type_str ==
"hdr") {
251 asset->type = TEX_2D;
252 asset->name +=
".texture";
253 asset->class_type =
"STexture";
258 if (type_str ==
"cbmap") {
259 asset->type = TEX_CUBEMAP;
260 asset->name +=
".textureCubeMap";
261 asset->class_type =
"STexture";
266 if (type_str ==
"framebuffer") {
267 asset->type = FRAMEBUFFER;
268 asset->name +=
".frameBuffer";
269 asset->class_type =
"SFrameBuffer";
274 if (type_str ==
"mat") {
275 asset->type = MATERIAL;
276 asset->name +=
".material";
277 asset->class_type =
"SMaterial";
282 if (type_str ==
"dae") {
284 asset->name +=
".prefab";
285 asset->class_type =
"SPrefab";
287 const auto& ani = AppendSubName(CreateAsset(asset->path, asset->name_full, asset->name),
289 const auto& ske = AppendSubName(CreateAsset(asset->path, asset->name_full, asset->name),
291 const auto& mes = AppendSubName(CreateAsset(asset->path, asset->name_full, asset->name),
"mesh");
296 ani->class_type =
"Animation";
297 ske->class_type =
"Skeleton";
298 mes->class_type =
"MeshSurface";
304 if (type_str ==
"nut") {
305 asset->type = SCRIPT;
306 asset->name +=
".script";
307 asset->class_type =
"SScriptObject";
312 if (type_str ==
"vert" || type_str ==
"vs") {
313 asset->type = SHADER;
314 asset->name +=
".vert";
315 asset->class_type =
"GLProgramHandle";
318 if (type_str ==
"frag" || type_str ==
"fs") {
319 asset->type = SHADER;
320 asset->name +=
".frag";
321 asset->class_type =
"GLProgramHandle";
326 if (type_str ==
"shader") {
327 asset->type = SHADER_HANDLE;
328 asset->name +=
".shader";
329 asset->class_type =
"SShaderGroup";
334 if (type_str ==
"prefab") {
335 asset->type = PREFAB;
336 asset->name +=
".prefab";
337 asset->class_type =
"SPrefab";
342 if (type_str ==
"scene") {
344 asset->name +=
".scene";
349 if (type_str ==
"ini") {
351 asset->name +=
".ini";
355 if (asset->type == NONE) {
358 asset->name +=
".text";
366 std::string sub =
'?' + sub_name;
372std::list<AssetMgr::AssetReference*> AssetMgr::GetAssets(TYPE type)
const {
373 std::list<AssetReference*> result;
375 for (
const auto& asset: m_assetsList) {
376 if (asset->type == type) {
377 result.push_back(asset);
384std::string AssetMgr::LoadAssetFile(
const std::string& path) {
385 if (Settings::IsAssetsPacked() ==
false)
386 return OpenNativeAssetsTxtFile(path);
389 std::string path_convert;
390 const auto& zip = CORE->GetResMgrCore()->m_assetManager->m_zip;
391 if (path.find(
"/../") != std::string::npos) {
392 auto path_split = split(path,
'/');
393 std::stack<std::string> path_stack;
394 for (
const auto& part: path_split) {
399 path_stack.push(part);
401 while (!path_stack.empty()) {
402 path_convert = path_stack.top() +
'/' + path_convert;
405 path_convert = path_convert.substr(0, path_convert.size() - 1);
415 result = std::string(buf, bufsize);
422std::string AssetMgr::GetAssetHash(
const std::string& path) {
423 std::string meta = LoadAssetFile(path +
".meta");
424 std::string&& hash =
"";
426 const XNode* root =
XFILE().loadBuffer(std::move(meta));
427 const auto& hashData = root->getNode(
"hash-data");
428 hash = hashData.getAttribute(
"hash").value;
431 hash = GetRandomHash(16);
432 std::string meta =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
433 "<CSEMETA version=\"1.0.0\">\n"
434 "<hash-data hash=\"" + hash +
"\">\n"
435 "\n</hash-data>\n</CSEMETA>";
436 if (!Settings::IsAssetsPacked())
437 SaveTxtFile(path +
".meta", meta);
445void AssetMgr::SetAssetManager(AAssetManager* obj) {
446 m_assetManager = obj;
450AAssetManager * AssetMgr::GetAssetManager() {
451 return m_assetManager;
454void AssetMgr::SetEnv(JNIEnv *obj) {
458JNIEnv *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)