Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- using NLua;
- using ConsoleApp3;
- using System.Collections.Generic;
- using System.Reflection;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System.Linq;
- namespace GetLuaTable
- {
- class Program
- {
- public static Lua netLua;
- static void Main(string[] args)
- {
- netLua = new Lua();
- CShaprFunction cShapr = new CShaprFunction();
- netLua.RegisterFunction("CShaprConsoleLine", cShapr, cShapr.GetType().GetMethod("CShaprConsoleLine"));
- netLua.RegisterFunction("CSharpGetTableFromStr", cShapr, cShapr.GetType().GetMethod("CSharpGetTableFromStr"));
- netLua.DoString(@"
- function main()
- CShaprConsoleLine(""Start"")
- local tmptable = CSharpGetTableFromStr(""stats"")
- CShaprConsoleLine(type(tmptable))
- CShaprConsoleLine(""end"")
- end
- ");
- netLua.GetFunction("main").Call();
- Console.ReadKey();
- }
- }
- class CShaprFunction
- {
- public static List<player> players = new List<player>();
- public static Lua lua = Program.netLua;
- public void CShaprConsoleLine(object obj)
- {
- Console.WriteLine(obj);
- }
- public LuaTable CSharpGetTableFromStr(string name)
- {
- string text = System.IO.File.ReadAllText(@"C:\Users\erwin\Documents\CombatFlight\SlmodStats.lua");
- for (int i = 0; i < 1000; i++)
- {
- string newInex = string.Format("[\"{0}\"]", i);
- text = text.Replace("[" + i + "]", newInex);
- }
- lua.DoString(text);
- LuaTable tab = lua.GetTable(name);
- Dictionary<object, object> dict = lua.GetTableDict(tab);
- foreach (KeyValuePair<object, object> de in dict)
- {
- player player = new player();
- player.id = de.Key.ToString();
- players.Add(player);
- getNames(player,"stats." + player.id + ".names");
- getPvpKills(player, "stats." + player.id + ".PvP");
- getFriendlyKills(player, "stats." + player.id + ".friendlyKills");
- }
- return null;
- }
- //Returns the names of a player.
- public void getNames(player player ,string path)
- {
- LuaTable taba = lua.GetTable(path);
- foreach (KeyValuePair<object, object> names in taba)
- {
- if (player != null && player.names != null)
- {
- player.names.Add(names.Value.ToString());
- }
- }
- }
- public void getPvpKills(player player, string path)
- {
- LuaTable taba = lua.GetTable(path);
- int i = 0;
- foreach (KeyValuePair<object, object> kills in taba)
- {
- if (player != null && player.names != null)
- {
- if (i == 0)
- {
- player.pvpWins = Convert.ToInt16(kills.Value.ToString());
- i++;
- }
- else if(i == 1)
- {
- player.pvpLoses = Convert.ToInt16(kills.Value.ToString());
- }
- }
- }
- }
- public void getFriendlyKills(player player,string path)
- {
- LuaTable taba = lua.GetTable(path);
- var friendlyKills = new Dictionary<int, Dictionary<string, string>>();
- foreach (KeyValuePair<object, object> kills in taba)
- {
- if (player != null && player.friendlyKills != null)
- {
- friendlyKills.Add(Convert.ToInt16(kills.Key), new Dictionary<string, string>());
- }
- player.friendlyKills = friendlyKills;
- }
- if (friendlyKills.Count > 0)
- {
- Dictionary<string, string> values = new Dictionary<string, string>();
- foreach (int index in friendlyKills.Keys)
- {
- path = "stats." + player.id + ".friendlyKills.1";
- LuaTable tab = lua.GetTable(path);
- foreach (KeyValuePair<object, object> details in tab)
- {
- player.friendlyKills[index].Add(details.Key.ToString(), details.Value.ToString());
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement