Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
BundlerResource.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <sese/Config.h>
18#include "ResourceStream.h"
19
20#if defined(SESE_PLATFORM_WINDOWS)
21#include <map>
22#elif defined(SESE_PLATFORM_APPLE)
23// ref https://stackoverflow.com/questions/77640444/what-is-the-first-argument-to-apples-getsectiondata-function
24#include <dlfcn.h>
25#include <mach-o/getsect.h>
26#endif
27
28namespace sese::res {
29
30template<class R>
32public:
33 using BinaryIds = typename R::Binaries;
34
35 explicit BundlerResource();
36
38
40
41private:
42#if defined(SESE_PLATFORM_WINDOWS)
43 struct RC {
44 HRSRC hResInfo;
45 LPVOID pResData;
46 DWORD dwSize;
47 };
48 std::map<BinaryIds, RC> binariesMap;
49
50 HINSTANCE hInstance;
51#elif defined(SESE_PLATFORM_APPLE)
52 Dl_info img_info{};
53#endif
54};
55
56#if defined(SESE_PLATFORM_WINDOWS)
57
58template<class R>
60 hInstance = nullptr;
61}
62
63template<class R>
65
66template<class R>
68 if (auto iter = binariesMap.find(id); iter != binariesMap.end()) {
69 return std::make_unique<ResourceStream>(iter->second.pResData, iter->second.dwSize);
70 }
71 HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(static_cast<int>(id)), RT_RCDATA);
72 assert(hResInfo);
73 HGLOBAL hGlobal = LoadResource(hInstance, hResInfo);
74 DWORD dwSize = SizeofResource(hInstance, hResInfo);
75 LPVOID _RESOURCE_HEADER = LockResource(hResInfo);
76 binariesMap[id] = {hResInfo, hGlobal, dwSize};
77 return std::make_unique<ResourceStream>(static_cast<const char *>(hGlobal), dwSize);
78}
79
80#elif defined(SESE_PLATFORM_LINUX)
81
82template<class R>
84
85template<class R>
87
88template<class R>
90 auto res = R::syms[static_cast<int>(id)];
91 return std::make_unique<ResourceStream>(res.start, res.size);
92}
93
94#elif defined(SESE_PLATFORM_APPLE)
95
96static int rcs_addr_handle = 0;
97
98template<class R>
100 auto index = dladdr(&rcs_addr_handle, &img_info);
101 assert(index != 0);
102}
103
104template<class R>
106
107template<class R>
109 auto name = R::syms[static_cast<int>(id)];
110 auto header = static_cast<struct mach_header_64 *>(img_info.dli_fbase);
111 unsigned long size;
112 auto start = getsectiondata(header, "__DATA", name, &size);
113 return std::make_unique<ResourceStream>(start, size);
114}
115
116#endif
117
118} // namespace sese::res