Jump to content

Loading custom model format throws error


SpiderPig
 Share

Go to solution Solved by Josh,

Recommended Posts

I'm working on my own model format with a plugin, and with or without the plugin loaded I get the error - "Seek position outside of file bounds".

If you try loading this file as a model it should return null if it does not recognise shouldn't it?

LoadModel("Test.uga");

Test.zip

Link to comment
Share on other sites

I was creating the file with a buffer but changed to WriteFile but it still throws an error in LoadModel and crashes the application.  This is how I'm creating the model file.

void SaveModel(WString filepath, shared_ptr<Model> model) {

    auto version = 1;
    auto file = WriteFile(filepath);
    auto total_lods = model->lods.size();

    file->WriteInt(version);
    file->WriteInt(total_lods);
    for (auto lod_index = 0; lod_index < total_lods; lod_index++) {

        auto total_meshes = model->lods[lod_index]->meshes.size();
        file->WriteInt(total_meshes);

        for (auto mesh_index = 0; mesh_index < total_meshes; mesh_index++) {
            auto mesh = model->lods[lod_index]->meshes[mesh_index];
            auto total_vertices = mesh->CountVertices();
            auto total_indices = mesh->CountPrimitives();

            //Save Vertices
            file->WriteInt(total_vertices);
            for (auto vert_index = 0; vert_index < total_vertices; vert_index++) {
                auto vertex_position = mesh->GetVertexPosition(vert_index);
                auto vertex_normal = mesh->GetVertexNormal(vert_index);
                auto vertex_texcoords = mesh->GetVertexTexCoords(vert_index);

                file->WriteFloat(vertex_position.x);
                file->WriteFloat(vertex_position.y);
                file->WriteFloat(vertex_position.z);

                file->WriteFloat(vertex_normal.x);
                file->WriteFloat(vertex_normal.y);
                file->WriteFloat(vertex_normal.z);

                file->WriteFloat(vertex_texcoords.x);
                file->WriteFloat(vertex_texcoords.y);
            }

            //Save Indices
            file->WriteInt(total_indices);
            for (auto indice_index = 0; indice_index < total_indices; indice_index++) {
                auto v0 = mesh->GetPrimitiveVertex(indice_index, 0);
                auto v1 = mesh->GetPrimitiveVertex(indice_index, 1);
                auto v2 = mesh->GetPrimitiveVertex(indice_index, 2);

                file->WriteInt(v0);
                file->WriteInt(v1);
                file->WriteInt(v2);
            }
        }
    }

    file->Close();
}

 

Link to comment
Share on other sites

This file causes LoadModel() to hang rather than crash.  Is there some sort of secret header that all model files should have? <_<

To confirm - this is not with any plugin I've made, this is just loading a file that doesn't have much data and is in a format that LoadModel() doesn't recognise.

I've also seen LoadModel hang and rapidly increase RAM usage into the tens of GB.

CausesHang.zip

Link to comment
Share on other sites

On 3/9/2024 at 1:02 AM, SpiderPig said:

This file causes LoadModel() to hang rather than crash.  Is there some sort of secret header that all model files should have? <_<

To confirm - this is not with any plugin I've made, this is just loading a file that doesn't have much data and is in a format that LoadModel() doesn't recognise.

I've also seen LoadModel hang and rapidly increase RAM usage into the tens of GB.

CausesHang.zip 139 B · 0 downloads

This file mimics the beginning of a Leadwerks MDL file. I added some more checks that prevent the loader from trying to load this as such.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Thanks.  I'm now seeing that "Unknown chunk in model file" is spamming the output window for about 20 seconds before it finally fails to load.

Also the first file in this thread gives a "Seek position out of file bounds error" still.

Link to comment
Share on other sites

6 hours ago, Josh said:

This file mimics the beginning of a Leadwerks MDL file. I added some more checks that prevent the loader from trying to load this as such.

Shouldn't it load the file based on the extension type rather than trying to load data from the file first?

Link to comment
Share on other sites

4 hours ago, SpiderPig said:

Shouldn't it load the file based on the extension type rather than trying to load data from the file first?

No, it just loads data from a stream which may not have any file path associated with it at all.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I'm not fully convinced this issue is solved yet.  :)

14 hours ago, SpiderPig said:

Thanks.  I'm now seeing that "Unknown chunk in model file" is spamming the output window for about 20 seconds before it finally fails to load.

Should it really take 20 seconds before finally failing to load?

5 hours ago, Josh said:

No, it just loads data from a stream which may not have any file path associated with it at all.

How then are we to create plugins for new models types, custom or not, if the stream is not looking at the file extension?  At what point does LoadModel look to see if a plugin might have the function it needs to load that extension?

If you could please also try this file as it throws an out of bounds error and crashes the program.

Test.zip

Link to comment
Share on other sites

  • Solution

Typically your file format should start with some sort of identifier like "SPGM" for the first four bytes or so. This is usually how binary file formats are identified.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...