Example of method for converting Lua table to c# dictionary

Time:2021-10-19

Table property

Table is an “associative array”. The index of the array can be a number or a string. All index values need to be enclosed by “[” and “]”; If it is a string, you can also remove quotation marks and brackets; That is, if it is not enclosed by [], it is considered as a string index

The default initial index of table generally starts with 1. If the index is not written, the index will be considered as a number and will be automatically compiled from 1 in order;

The variable of table is only an address reference and will not have data impact on the operation of table

The length of the table will not be fixed. When new data is inserted, the length will increase automatically

The data stored in table can be of any type, including function and table;

All elements of table are always separated by commas “,”;

introduction

Because doing web background development in a game company often involves reading and writing various operations from the database of the game server.

Yesterday afternoon, the server asked me to read a configuration display to the background, so that the operation bosses can configure game parameters in the web background.

I thought it was a simple thing, but I read out the binary field in this format

?
1
{1=0,2=0,3=0,4=2,5={},6=0,7={1=118,s010GameConfig={s008wPayType=0,s009wCostType=0,s015dwReservedRule3=3,s015dwReservedRule2=0,s006ClubId=0,s010wCostValue=0,s010wCellScore=1,s014wPlayCountRule=10,s013wHadPlayCount=0,s010dwPlayRule=0,s010wSubGameID=114,s009wMaxScore=0,s015dwReservedRule1=0,s015sPrivateTableID=0}

The server brother told me that this is Lua’s table… It’s simple, that’s it… (hundreds of words are omitted here)

Then I

Then I searched the Internet for any ready-made wheels left by senior leaders… The result is either to load a virtual machine Library of lua VM, or some solutions to all kinds of troubles….

The blogger is also a person with four years + working experience. If there is no suitable one, write it yourself. Just exercise yourself

Create a new project with a nice name sharpluatable

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class SharpluaTable
 {
 string luatable = "";
 //Parse from {
 //Then it is divided by the equal sign, and the front and back of the equal sign are a key value pair
 //After the comma is another key value pair
 //If {is encountered halfway, it will be parsed until}, which is a value
 Dictionary<string, string> dic = new Dictionary<string, string>();
 
 
 public Dictionary<string, string> Parse(string luatable)
 {
  this.luatable = luatable;
  //Parse the 0 bit and the last bit to determine whether it is luatable format
 
  if (luatable[0] != '{')
  {
  throw new Exception("Failed to parse Lua, format error");
  }
 
  if (luatable[luatable.Length - 1] != '}')
  {
  throw new Exception("Failed to parse Lua, format error");
  }
  string luaKey = string.Empty, LuaValue = string.Empty;
  //Indicates whether to resolve key or value. If true, it will be resolved to key. If false, it will be resolved to value
  bool iskey = true;
 
 
  for (int i = 1; i < luatable.Length; i++)
  {          //If it is the last key value pair, it is over directly   if (i+1==luatable.Length&&luatable[i]=='}')   {    dic.Add(luaKey, LuaValue);    luaKey = string.Empty;    LuaValue = string.Empty;      break;   }
  //If it is a comma, the current key value is stored and the current character parsing is skipped
  if (luatable[i] == ',')
  {
   dic.Add(luaKey, LuaValue);
   luaKey = string.Empty;
   LuaValue = string.Empty;
   iskey = true; //Skip a comma and continue to resolve to key
   continue;
  }
  else
  {
   if (luatable[i] == '=')
   {
   iskey = false; //If it is an equal sign, it resolves to key and skips the current
   continue;
   }
   //If it is a secondary {, resolve to} and move the current value of I to the subscript position of}
   if (luatable[i] == '{')
   {
   //LuaValue += luatable[i];
   int kuohaoCount = 0;
   for (int j = i; j < luatable.Length; j++)
   {
    LuaValue += luatable[j];
    if (luatable[j]=='{')
    {
    kuohaoCount += 1;
    }
    if (luatable[j]=='}')
    {
    kuohaoCount -= 1;
 
    if (kuohaoCount==0)
    {
     i = j;
     break;
    }
    else
    {
     //kuohaoCount -= 1;
    }
    }
   }
   }
   else
   {
   if (iskey)
   {
    luaKey += luatable[i];
   }
   else
   {
    LuaValue += luatable[i];
   }
   }
  }
  }
  return dic;
 }

At present, the previous parsing class only does one level of parsing. If the value has multiple levels, I will parse it into value by default

Test it

?
1
2
3
4
5
6
7
string luatable = "{1=0,2=0,3=0,4=2,5={},6=0,7={1=118,s010GameConfig={s008wPayType=0,s009wCostType=0,s015dwReservedRule3=3,s015dwReservedRule2=0,s006ClubId=0,s010wCostValue=0,s010wCellScore=1,s014wPlayCountRule=10,s013wHadPlayCount=0,s010dwPlayRule=0,s010wSubGameID=114,s009wMaxScore=0,s015dwReservedRule1=0,s015sPrivateTableID=0},s006GameID=114},8=1}";
 
   SharpluaTable lua = new SharpluaTable();
   var dic = lua.Parse(luatable);
   Console.WriteLine(dic["7"]);
   SharpluaTable luaitem = new SharpluaTable();
    var items = luaitem.Parse(dic["7"]);

Example of method for converting Lua table to c# dictionary

If there are multiple levels, just create a new sharpluatable object and parse it again

It has been submitted to nuget. If necessary, you can use the big brother command directly

?
1
Install-Package LuaTableToCSharp -Version 1.0.3

The code is submitted to GitHub: https://github.com/QingChengCoder/LuaTableToCSharp

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message. Thank you for your support for developepper.