Archive 17/01/2023.

Load Resource from memory at runtime

CodeCrafty

I really need to add a model to my scene that I download from our server at runtime.
Getting the file over the net is easy… HTTP Get etc.

Now how do I get the resourcecache to load it??

Tried lots of stuff with MemoryBuffer but no luck 8(
Also tried writing to mass storage but again no Bueno…

Might have found a happy path… I’ll post the results for all once confirmed and cleaned up.

Modanung

Maybe Resource::LoadFile and then ResourceCache::AddManualResource?
I have not tried this.

SirNate0

Make sure you set the (file?) name on the resource before adding it as a manual resource. What’s happening when you try using the MemoryBuffer?

TheComet

Probably stating the obvious here but the standard way to do this is through Scene::AddRequiredPackageFile and Network::SetPackageCacheDir.

Is this not possible in your situation?

CodeCrafty

That sounds like what I should explore… but here’s our hacky ‘working’ version as we spike this functionality:

        public Model LoadModel()
        {
            var _model = scene.GetComponent<StaticModel>(true);

            try
            {
                // This mimics the bytes we will get from the server
                var file = ResourceCache.GetFile("Models/Sphere.mdl");
                byte[] fileBytes = new byte[file.Size];
                file.Read(fileBytes, file.Size);

                //This is stuffing it into the Urho system as if we got it off the network etc...
                MemoryBuffer buff = new MemoryBuffer(fileBytes);
                _model.Model = new Model();
                _model.Model.Load(buff);
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
            }

            return _model.Model;
        }

You’ll notice it’s C# and I’ve already read that this is not the C# forum but there is no other documentation for us-- it is just a thin wrapper to the same bits covered in this forum.
So it’s what I have to work with, ya know?
Someday you guys may need to do a Xamarin thing, I’ll be glad to help with the various gotchas I have encountered.

Modanung