CSEngine
Loading...
Searching...
No Matches
AssetsDef.h
1#pragma once
2
3#include <string>
4#include <fstream>
5#include <vector>
6
7#include "Settings.h"
8#include "MoreString.h"
9
10#ifdef __ANDROID__
11#include <android/asset_manager.h>
12#include <android/asset_manager_jni.h>
13#include <android/log.h>
14#include <Manager/EngineCore.h>
15#include <Manager/ResMgr.h>
16
17#define LOG_TAG "SCENGINE"
18
19#elif IOS
20#include <CoreFoundation/CFURL.h>
21#include <CoreFoundation/CFURLAccess.h>
22#include <CoreFoundation/CFBundle.h>
23#include <CoreFoundation/CFPropertyList.h>
24#endif
25
26namespace CSE {
27 static std::string NativeAssetsPath() {
28 std::string path;
29#if defined(__CSE_EDITOR__)
30 #if defined(MSVC_CMAKE)
31 path.append("../../../../../Assets/");
32 #elif defined(__EMSCRIPTEN__)
33 path = "";
34 #else
35 path.append("../../../../Assets/");
36#endif
37#elif defined(MSVC_CMAKE)
38 path.append("../../../../Assets/");
39#elif defined(IOS)
40 path = "";
41#elif defined(_WIN32) || defined(__linux__) || defined(__APPLE_CC__)
42 path.append("../../../Assets/");
43#endif
44#ifdef __ANDROID__
45 path = "";
46#endif
47 return path;
48 }
49
50 static std::string AssetsPath() {
51 std::string path;
52
53 if (Settings::IsAssetsPacked() == false)
54 path = NativeAssetsPath();
55
56 return path;
57 }
58
59 static std::string OpenNativeAssetsTxtFile(const std::string& path) {
60
61 std::string buf;
62#ifdef __ANDROID__
63 // get lists : https://stackoverflow.com/questions/13317387/how-to-get-file-in-assets-from-android-ndk
64 AAssetManager* assetManager = CORE->GetCore(ResMgr)->GetAssetManager();
65 AAsset* asset = AAssetManager_open(assetManager, path.c_str(), AASSET_MODE_STREAMING);
66 if(asset == nullptr){
67 return "";
68 }
69
70 off_t fileSize = AAsset_getRemainingLength(asset);
71 if(fileSize <= 0){
72 return "";
73 }
74
75 char* strbuf = new char[fileSize + 1]();
76 AAsset_read(asset, strbuf, fileSize);
77 AAsset_close(asset);
78 buf = std::string(strbuf, fileSize);
79 delete[] strbuf;
80#elif defined (_WIN32) || defined(__linux__) || defined(__EMSCRIPTEN__) || defined(__APPLE_CC__)
81#ifdef IOS
82 auto path_split = split(path, '.');
83 CFStringRef cf_name = CFStringCreateWithCString(kCFAllocatorDefault, path_split[0].c_str(), kCFStringEncodingUTF8);
84 CFStringRef cf_ext = CFStringCreateWithCString(kCFAllocatorDefault, path_split[1].c_str(), kCFStringEncodingUTF8);
85
86 CFURLRef manifest_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cf_name, cf_ext, NULL);
87 char manifest_path[1024];
88 CFURLGetFileSystemRepresentation(manifest_url, TRUE, (UInt8*)manifest_path, sizeof(manifest_path));
89 CFRelease(manifest_url);
90 CFRelease(cf_name);
91 CFRelease(cf_ext);
92
93 std::ifstream fin(manifest_path, std::ios::binary);
94#else
95 std::ifstream fin(path, std::ios::binary);
96#endif
97 if (!fin.is_open()) return "";
98
99 fin.seekg(0, std::ios::end);
100 size_t size = fin.tellg();
101 fin.seekg(0);
102 buf = std::string(size, ' ');
103 fin.read(&buf[0], size);
104 fin.close();
105#endif
106 return buf;
107 }
108
109 static bool SaveTxtFile(const std::string& path, std::string data) {
110#if defined(__ANDROID__) || defined(IOS) || defined(__EMSCRIPTEN__)
111 return true;
112#endif
113 std::ofstream fout(path, std::ios::binary | std::ios::trunc);
114 fout << data;
115 return fout.good();
116 }
117}