2

I have this Lua file which contains the following code:

options = {
["VR"] = {
    ["box_mouse_cursor"] = false,
    ["custom_IPD"] = 63.5,
    ["custom_IPD_enable"] = true,
    ["enable"] = false,
    ["hand_controllers"] = false,
    ["pixel_density"] = 2,
    ["prefer_built_in_audio"] = true,
    ["use_mouse"] = true,
    }
}

I am trying to find an assembly that will parse this into something usable by my .Net Framework 4.5.1 C# project. LuaInterface is .NET standard 2.0 project which can't be used in .Net Framework 4.5.1, so that's off the table.

Are there any tools available that can parse this information, and hopefully serialize it back again?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Ross Miller
  • 656
  • 3
  • 9
  • Yes. It's not compatable – Ross Miller Jul 03 '18 at 17:46
  • 2
    Brian's answer to your question is a good one. If you JSON encode your table, you can, at the other end of the pipeline in your C#, decode it. You can use Newtonsoft for this. You can throw the values directly into a class, too, so you just need to decode as type. There's also a handy function in Visual Studio that allows you to `Paste as JSON Class`; allowing you to make a class with the same structure of your JSON in one fell swoop. Look over Brian's answer and look into Newtonsoft; I think you'll love it. :) – James Whyte Jul 04 '18 at 00:03
  • 1
    Thank you. I didn't think of converting into some intermediate format. – Ross Miller Jul 04 '18 at 08:23
  • Why not simply use NLua nuget to your project? – Vinicius Jarina Feb 15 '19 at 21:10

2 Answers2

3

I would consider using Lua to encode the nested table structure that you have into JSON. That would give you a standard format to write to disk and/or easily pass into another code base. There are quite a few options available when you search for that online (lua table encode json) or even here on SO people are trying to pedal their JSON encode/decode Lua libraries when similar questions were asked. Here, for example, is the reverse case.

brianolive
  • 1,573
  • 2
  • 9
  • 19
  • Thanks for this. I'll need to execute a lua script to covert the json data table back to a lua format within the lua interpreter. (I'm using Moonsharp). Is this the correct approach? – Ross Miller Jul 04 '18 at 08:25
2

0) Download LuaInterface 2.0.3
1) Add LuaInterface.dll to C# project, copy lua51.dll to project folder
2) Modify app.config (add property to startup tag)

<startup useLegacyV2RuntimeActivationPolicy="true">

3) Write a C# code to read values you need:

using LuaInterface;

        Lua lua = new Lua();
        string Lua_code = @"
            options = {
                [""VR""] = {
                    [""box_mouse_cursor""] = false,
                    [""custom_IPD""] = 63.5,
                    [""custom_IPD_enable""] = true,
                    [""enable""] = false,
                    [""hand_controllers""] = false,
                    [""pixel_density""] = 2,
                    [""prefer_built_in_audio""] = true,
                    [""use_mouse""] = true,
                }
            }
        ";
        lua.DoString(Lua_code);
        double custom_IPD_value = (double)lua["options.VR.custom_IPD"];
        textBox1.Text = custom_IPD_value.ToString();
Egor Skriptunoff
  • 906
  • 1
  • 8
  • 23