Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using NLua;
  5. using ConsoleApp3;
  6. using System.Collections.Generic;
  7. using System.Reflection;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using System.Linq;
  11.  
  12. namespace GetLuaTable
  13. {
  14. class Program
  15. {
  16. public static Lua netLua;
  17.  
  18. static void Main(string[] args)
  19. {
  20. netLua = new Lua();
  21. CShaprFunction cShapr = new CShaprFunction();
  22. netLua.RegisterFunction("CShaprConsoleLine", cShapr, cShapr.GetType().GetMethod("CShaprConsoleLine"));
  23. netLua.RegisterFunction("CSharpGetTableFromStr", cShapr, cShapr.GetType().GetMethod("CSharpGetTableFromStr"));
  24. netLua.DoString(@"
  25. function main()
  26. CShaprConsoleLine(""Start"")
  27. local tmptable = CSharpGetTableFromStr(""stats"")
  28. CShaprConsoleLine(type(tmptable))
  29. CShaprConsoleLine(""end"")
  30. end
  31. ");
  32. netLua.GetFunction("main").Call();
  33. Console.ReadKey();
  34. }
  35. }
  36. class CShaprFunction
  37. {
  38. public static List<player> players = new List<player>();
  39. public static Lua lua = Program.netLua;
  40.  
  41. public void CShaprConsoleLine(object obj)
  42. {
  43. Console.WriteLine(obj);
  44. }
  45.  
  46. public LuaTable CSharpGetTableFromStr(string name)
  47. {
  48. string text = System.IO.File.ReadAllText(@"C:\Users\erwin\Documents\CombatFlight\SlmodStats.lua");
  49. for (int i = 0; i < 1000; i++)
  50. {
  51. string newInex = string.Format("[\"{0}\"]", i);
  52. text = text.Replace("[" + i + "]", newInex);
  53. }
  54.  
  55. lua.DoString(text);
  56.  
  57. LuaTable tab = lua.GetTable(name);
  58. Dictionary<object, object> dict = lua.GetTableDict(tab);
  59.  
  60. foreach (KeyValuePair<object, object> de in dict)
  61. {
  62. player player = new player();
  63. player.id = de.Key.ToString();
  64.  
  65. players.Add(player);
  66. getNames(player,"stats." + player.id + ".names");
  67. getPvpKills(player, "stats." + player.id + ".PvP");
  68. getFriendlyKills(player, "stats." + player.id + ".friendlyKills");
  69. }
  70.  
  71. return null;
  72. }
  73.  
  74. //Returns the names of a player.
  75. public void getNames(player player ,string path)
  76. {
  77. LuaTable taba = lua.GetTable(path);
  78. foreach (KeyValuePair<object, object> names in taba)
  79. {
  80. if (player != null && player.names != null)
  81. {
  82. player.names.Add(names.Value.ToString());
  83. }
  84. }
  85. }
  86. public void getPvpKills(player player, string path)
  87. {
  88. LuaTable taba = lua.GetTable(path);
  89. int i = 0;
  90.  
  91. foreach (KeyValuePair<object, object> kills in taba)
  92. {
  93. if (player != null && player.names != null)
  94. {
  95. if (i == 0)
  96. {
  97. player.pvpWins = Convert.ToInt16(kills.Value.ToString());
  98. i++;
  99. }
  100. else if(i == 1)
  101. {
  102. player.pvpLoses = Convert.ToInt16(kills.Value.ToString());
  103. }
  104. }
  105. }
  106. }
  107. public void getFriendlyKills(player player,string path)
  108. {
  109. LuaTable taba = lua.GetTable(path);
  110.  
  111. var friendlyKills = new Dictionary<int, Dictionary<string, string>>();
  112.  
  113. foreach (KeyValuePair<object, object> kills in taba)
  114. {
  115. if (player != null && player.friendlyKills != null)
  116. {
  117. friendlyKills.Add(Convert.ToInt16(kills.Key), new Dictionary<string, string>());
  118. }
  119. player.friendlyKills = friendlyKills;
  120. }
  121.  
  122. if (friendlyKills.Count > 0)
  123. {
  124. Dictionary<string, string> values = new Dictionary<string, string>();
  125. foreach (int index in friendlyKills.Keys)
  126. {
  127. path = "stats." + player.id + ".friendlyKills.1";
  128. LuaTable tab = lua.GetTable(path);
  129.  
  130. foreach (KeyValuePair<object, object> details in tab)
  131. {
  132. player.friendlyKills[index].Add(details.Key.ToString(), details.Value.ToString());
  133. }
  134. }
  135. }
  136. }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement