Joe Allen
Joe Allen

Reputation: 389

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
)

Upvotes: 1

Views: 62

Answers (2)

Philip Withnall
Philip Withnall

Reputation: 5705

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);

Upvotes: 3

Oleg Habchak
Oleg Habchak

Reputation: 49

The g_resource_lookup_data() function in GLib is used to retrieve data embedded in a GResource file. If you're encountering a "File not found" error, it could be due to several reasons. Here's a checklist to help you troubleshoot:

  1. Resource XML Configuration: Ensure that the file you're trying to access is properly defined in the .gresource.xml file. For example:

    <gresources>
      <gresource prefix="/com/your/app">
        <file>path/to/your/file</file>
      </gresource>
    </gresources>
    
  2. Correct Path Prefix: When using g_resource_lookup_data(), make sure the path you're using matches the prefix in the XML file. For example:

    g_resource_lookup_data("/com/your/app/path/to/your/file", G_RESOURCE_LOOKUP_FLAGS_NONE, &error);
    
  3. Resource Compilation: Make sure that your resource file (.gresource.xml) has been compiled correctly using glib-compile-resources. The command should generate a .c file that includes the data. For example:

    glib-compile-resources your.gresource.xml
    
  4. Resource Registration: Before you can access resources, you must register the GResource at runtime using g_resources_register(). For example:

    GResource *resource = g_resource_load("your.gresource", &error);
    g_resources_register(resource);
    
  5. Linking the Resource in Build: Ensure that the compiled resource file is linked properly in your build system (e.g., Makefile, Meson, etc.).

  6. Case Sensitivity: Resource paths are case-sensitive. Ensure that the path you provide matches exactly with the file names and the path specified in the XML.

Let me know if any of these steps resolve your issue or if you need further guidance!

Upvotes: 3

Related Questions