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(MSVC_CMAKE)
30 path.append("../../../../Assets/");
31#elif defined(IOS)
32 path = "";
33#elif defined(_WIN32) || defined(__linux__) || defined(__APPLE_CC__)
34 path.append("../../../Assets/");
35#endif
36#ifdef __ANDROID__
37 path = "";
38#endif
39 return path;
40 }
41
42 static std::string AssetsPath() {
43 std::string path;
44
45 if (Settings::IsAssetsPacked() == false)
46 path = NativeAssetsPath();
47
48 return path;
49 }
50
51 static std::string OpenNativeAssetsTxtFile(const std::string& path) {
52
53 std::string buf;
54#ifdef __ANDROID__
55 // get lists : https://stackoverflow.com/questions/13317387/how-to-get-file-in-assets-from-android-ndk
56 AAssetManager* assetManager = CORE->GetCore(ResMgr)->GetAssetManager();
57 AAsset* asset = AAssetManager_open(assetManager, path.c_str(), AASSET_MODE_STREAMING);
58 if(asset == nullptr){
59 return "";
60 }
61
62 off_t fileSize = AAsset_getRemainingLength(asset);
63 if(fileSize <= 0){
64 return "";
65 }
66
67 char* strbuf = new char[fileSize + 1]();
68 AAsset_read(asset, strbuf, fileSize);
69 AAsset_close(asset);
70 buf = std::string(strbuf, fileSize);
71 delete[] strbuf;
72#elif defined (_WIN32) || defined(__linux__) || defined(__EMSCRIPTEN__) || defined(__APPLE_CC__)
73#ifdef IOS
74 auto path_split = split(path, '.');
75 CFStringRef cf_name = CFStringCreateWithCString(kCFAllocatorDefault, path_split[0].c_str(), kCFStringEncodingUTF8);
76 CFStringRef cf_ext = CFStringCreateWithCString(kCFAllocatorDefault, path_split[1].c_str(), kCFStringEncodingUTF8);
77
78 CFURLRef manifest_url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), cf_name, cf_ext, NULL);
79 char manifest_path[1024];
80 CFURLGetFileSystemRepresentation(manifest_url, TRUE, (UInt8*)manifest_path, sizeof(manifest_path));
81 CFRelease(manifest_url);
82 CFRelease(cf_name);
83 CFRelease(cf_ext);
84
85 std::ifstream fin(manifest_path, std::ios::binary);
86#else
87 std::ifstream fin(path, std::ios::binary);
88#endif
89 if (!fin.is_open()) return "";
90
91 fin.seekg(0, std::ios::end);
92 size_t size = fin.tellg();
93 fin.seekg(0);
94 buf = std::string(size, ' ');
95 fin.read(&buf[0], size);
96 fin.close();
97#endif
98 return buf;
99 }
100
101 static bool SaveTxtFile(const std::string& path, std::string data) {
102#if defined(__ANDROID__) || defined(IOS) || defined(__EMSCRIPTEN__)
103 return true;
104#endif
105 std::ofstream fout(path, std::ios::binary | std::ios::trunc);
106 fout << data;
107 return fout.good();
108 }
109}