Esempio n. 1
0
        /// <summary>
        /// Copies and translates the contents of the specified config file into the specified table
        /// </summary>
        /// <param name="config"></param>
        /// <param name="lua"></param>
        /// <returns></returns>
        public static LuaTable TableFromConfig(DynamicConfigFile config, NLua.Lua lua)
        {
            // Make a table
            lua.NewTable("tmp");
            LuaTable tbl = lua["tmp"] as LuaTable;
            lua["tmp"] = null;

            // Loop each item in config
            foreach (var pair in config)
            {
                CreateFullPath(pair.Key, tbl, lua);
                // Translate and set on table
                tbl[pair.Key] = TranslateConfigItemToLuaItem(lua, pair.Value);
            }

            // Return
            return tbl;
        }
Esempio n. 2
0
        /// <summary>
        /// Translates a single object from its C# form to its Lua form
        /// </summary>
        /// <param name="lua"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        private static object TranslateConfigItemToLuaItem(NLua.Lua lua, object item)
        {
            // Switch on the object type
            if (item is int || item is float || item is double)
                return Convert.ToDouble(item);
            else if (item is bool)
                return Convert.ToBoolean(item);
            else if (item is string)
                return item;
            else if (item is List<object>)
            {
                lua.NewTable("tmplist");
                LuaTable tbl = lua["tmplist"] as LuaTable;
                lua["tmplist"] = null;

                List<object> list = item as List<object>;
                for (int i = 0; i < list.Count; i++)
                {
                    tbl[i + 1] = TranslateConfigItemToLuaItem(lua, list[i]);
                }

                return tbl;
            }
            else if (item is Dictionary<string, object>)
            {
                lua.NewTable("tmpdict");
                LuaTable tbl = lua["tmpdict"] as LuaTable;
                lua["tmpdict"] = null;

                Dictionary<string, object> dict = item as Dictionary<string, object>;
                foreach (var pair in dict)
                {
                    CreateFullPath(pair.Key, tbl, lua);
                    tbl[pair.Key] = TranslateConfigItemToLuaItem(lua, pair.Value);
                }

                return tbl;
            }
            else
                return null;
        }
Esempio n. 3
0
 private static void CreateFullPath(string fullPath, LuaTable tbl, NLua.Lua lua)
 {
     var path = fullPath.Split('.');
     for (var i = 0; i < path.Length - 1; i++)
     {
         if (tbl[path[i]] == null)
         {
             lua.NewTable("tmp");
             var table = (LuaTable) lua["tmp"];
             tbl[path[i]] = table;
             lua["tmp"] = null;
             tbl = table;
         }
     }
 }