Embedded Resources

This commit is contained in:
Isuru Samarathunga
2025-11-01 11:13:04 +05:30
parent dd2d2e1ae8
commit ab484b4016
11 changed files with 248 additions and 18 deletions

View File

@ -16,3 +16,117 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import sys
import glob
from AddHeaderFile import add_header_file
from AddSourceFile import add_source_file
class Resource:
def __init__(self) -> None:
self.name = ""
self.path = ""
self.data = b''
self.Type = "" # Binary, Text, Shader
def load_resources() -> list[Resource]:
result = []
imagePaths = glob.glob("Resources/Images/*.*", recursive=True)
for t in imagePaths:
r = Resource()
r.path = t.replace('\\', '/')
r.path = r.path[r.path.find('/') + 1:]
r.name = r.path.split('/')[-1].upper().replace('.', '_')
r.Type = "Binary"
with open(t, 'rb') as f:
r.data = f.read()
result.append(r)
fontPaths = glob.glob("Resources/Fonts/*.ttf", recursive=True)
for t in fontPaths:
r = Resource()
r.path = t.replace('\\', '/')
r.path = r.path[r.path.find('/') + 1:]
r.name = r.path.split('/')[-1].upper().replace('.', '_')
r.Type = "Binary"
with open(t, 'rb') as f:
r.data = f.read()
result.append(r)
shaderPaths = glob.glob("Resources/Shaders/*.*", recursive=True)
for t in shaderPaths:
r = Resource()
r.path = t.replace('\\', '/')
r.path = r.path[r.path.find('/') + 1:]
r.name = r.path.split('/')[-1].upper().replace('.', '_')
r.Type = "Binary"
os.system(f"glslc {t} -o {t}.spv")
with open(f"{t}.spv", 'rb') as f:
r.data = f.read()
os.remove(f"{t}.spv")
result.append(r)
return result
def main():
resoruces = load_resources()
add_header_file("EmbeddedResources.hpp", "Src/IAEngine/imp/hpp", """ class EmbeddedResources
{
public:
STATIC VOID Initialize();
STATIC VOID Terminate();
STATIC CONST Vector<UINT8>& GetResource(IN CONST String& name);
private:
STATIC Map<String, Vector<UINT8>> s_resources;
};""")
resourceData = []
resourceInit = []
for r in resoruces:
d = "\t\t"
for b in r.data:
d += f"{hex(b)},"
resourceData.append(f"\tCONST Vector<UINT8> DATA_{r.name} = {{")
resourceData.append(d)
resourceData.append("\t};\n")
resourceInit.append(f"\t\ts_resources[\"{r.path}\"] = DATA_{r.name};")
add_source_file("EmbeddedResources.cpp", "Src/IAEngine/imp/cpp", f"""#include <EmbeddedResources.hpp>
namespace ia::iae
{{
{'\n'.join(resourceData)}
}}
namespace ia::iae
{{
Map<String, Vector<UINT8>> EmbeddedResources::s_resources;
VOID EmbeddedResources::Initialize()
{{
{'\n'.join(resourceInit)}
}}
VOID EmbeddedResources::Terminate()
{{
for(auto& t: s_resources)
t->Value.reset();
}}
CONST Vector<UINT8>& EmbeddedResources::GetResource(IN CONST String& name)
{{
return s_resources[name];
}}
}}
""")
if __name__ == "__main__":
main()