From 029888c5488c83557fcdfdcab392b87c28d71989 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 25 Sep 2024 17:37:07 +0200 Subject: [PATCH] fix: ResourceContainer name field is now a manually allocated string --- src/core/resources.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/core/resources.c b/src/core/resources.c index 7cb07f5..0caeab5 100644 --- a/src/core/resources.c +++ b/src/core/resources.c @@ -15,7 +15,7 @@ typedef enum ResourceType { typedef struct ResourceContainer { char const *path; - char const *name; + char *name; unsigned use_counter; bool is_loaded; ResourceType type; @@ -88,13 +88,15 @@ void Internal_IndexResourceDirectory() { g_resource_map = hash_map_from_types(char const *, ResourceContainer, HashMapHashString); ResourceContainer placeholder; for(size_t i = 0; i < g_resource_files.count; ++i) { + char const *name = GetFileNameWithoutExt(g_resource_files.paths[i]); placeholder = (ResourceContainer){ .is_loaded = false, .use_counter = 0, .path = g_resource_files.paths[i], - .name = GetFileNameWithoutExt(g_resource_files.paths[i]), + .name = malloc(strlen(name) + 1), .type = ResourceGetTypeFromPath(g_resource_files.paths[i]) }; + strcpy(placeholder.name, name); // only index resources that the engine knows how to load // shaders are made up of two files, so their paths are treated differently if(placeholder.type == RESOURCE_SHADER) { @@ -117,9 +119,11 @@ void InitializeResourceSubsystem() { void CleanResourceSubsystem() { List resources = hash_map_values(&g_resource_map); - list_foreach(ResourceContainer **,resource, &resources) + list_foreach(ResourceContainer **,resource, &resources) { if((*resource)->is_loaded) g_unload_functions[(*resource)->type](*resource); + free((*resource)->name); + } hash_map_empty(&g_resource_map); UnloadDirectoryFiles(g_resource_files); } @@ -156,6 +160,7 @@ bool GetShaderResource(char const *path, ShaderResource *out) { *out = ResourceEmpty(Shader); ASSERT_RETURN(container != NULL, false, "GetShaderResource: Resource %s not in index.", path); ASSERT_RETURN(container->type == RESOURCE_SHADER, false, "GetShaderResource: Resource %s is not a Shader.", path); + ASSERT_RETURN(strcmp(container->name, path) == 0, false, "GetShaderResource: Shader resource found name %s instead of %s", container->name, path); *out = (ShaderResource) { .handle = container, .resource = &container->shader @@ -209,6 +214,7 @@ static void Internal_LoadShaderResource(ResourceContainer *resource) { char const *vs = TextFormat("%s/%s.vs", resource->path, resource->name); char const *fs = TextFormat("%s/%s.fs", resource->path, resource->name); + ASSERT_RETURN(resource->type == RESOURCE_SHADER,, "Internal_LoadShaderResource: Attempting to load non-shader resource as shader."); resource->shader = LoadShader(vs, fs); ASSERT_RETURN(IsShaderReady(resource->shader),, "Internal_LoadShaderResource: Shader failed to load."); Internal_OnShaderLoaded((ShaderResource){.resource = &resource->shader, .handle = resource});