Reading a .json file inside my own package

mrmr32

Well-known member
Messages
124
Reactions
325
Points
63
Website
github.com
Patreon
mrmr32
Hi, I have to read a .json with information that I'll include before building the plugin.

I've read files before, but they were physical files; not files inside a package.
I've tried to do something similar to the .cslist syntax and I've come with this solution: `SuperController.singleton.GetFilesAtPath("mrmr32.UVSwapper." + PLUGIN_VERSION + ":/" + path)`, but it always return an empty array (but at least it doesn't throw any error).
Anyone has done it before, or knows a plugin that does it?

Thanks.
 
SPQR's code should do.

However, to automatically get the "SPQR.SPQRAlive.34:/" part, I recommend using this helpful method from MacGruber_Utils.cs
C#:
string text = FileManagerSecure.ReadAllText(Utils.GetPackagePath(this) + "Saves/scene/Alive/Alive Demo.json");
It autodetects the package you are in, so you don't have to worry about version numbers increasing. It also returns just an empty string, if you are just a local plugin not (yet) in a VAR package. So, it still works in that case, too.

If you don't wanna depend on any of my packages or don't want make your plugin CC-BY-SA to be able to include a copy of MacGruber_Utils.cs in your own VAR, you can also use this snippet under CC-BY conditions (so, without the "SA" part):
C#:
        // Get path prefix of the package that contains our plugin.
        private string GetPackagePath()
        {              
            string id = name.Substring(0, name.IndexOf('_'));
            string filename = manager.GetJSON()["plugins"][id].Value;
            int idx = filename.IndexOf(":/");
            if (idx >= 0)
                return filename.Substring(0, idx+2);
            else
                return string.Empty;
        }
 
Back
Top Bottom