c++meson-buildgtk4gio

File not found when using g_resource_lookup_data()


I am attempting to access a file that has been compiled into a GResouce using gnome.compile_resources() in meson. I successfully get the generated source files and #include "shader-resources.h" works, along with no errors from shader_resources_get_resource(). However, upon trying to lookup any data g_resource_lookup_data gives the error: "The resource at “org/lyra/core/default.vert” does not exist".

Am I using an incorrect format for the file path or is there something else that I am missing?

Relevant c++ code: (shader_resources is a static GResource* defined in a separate header)

// check if shader_resources exists, create if not
if(shader_resources == nullptr) {
    shader_resources = shader_resources_get_resource();

    // output default.vert contents
    GError* err = NULL;
    GBytes* data = g_resource_lookup_data(shader_resources, "org/lyra/core/default.vert", G_RESOURCE_LOOKUP_FLAGS_NONE, &err);
    if (err != NULL) {
        g_assert (data == NULL);
        fprintf (stderr, "Unable to read file: %s\n", err->message);
        g_error_free (err);
    }
    std::printf("%s", data);
    g_bytes_unref(data);
}

org.lyra.core.gresource.xml:

<?xml version="1.0" encoding="UTF-8"?>
<gresources>
  <gresource prefix="/org/lyra/core">
  
    <file compressed="true">default.vert</file> 
    <file compressed="true">default.vert.spv</file> 
    <file compressed="true">default.frag.spv</file> 
    
  </gresource>
</gresources>

In my meson.build:

shader_resources = gnome.compile_resources(
    'shader-resources',
    'org.lyra.core.gresource.xml', 
    c_name:'shader_resources',
    dependencies:shader_sources
)

Solution

  • It looks like you’re missing a leading slash from the lookup path:

    GBytes* data = g_resource_lookup_data(shader_resources, "/org/lyra/core/default.vert", G_RESOURCE_LOOKUP_FLAGS_NONE, &err);
    

    rather than

    GBytes* data = g_resource_lookup_data(shader_resources, "org/lyra/core/default.vert", G_RESOURCE_LOOKUP_FLAGS_NONE, &err);