Пример #1
0
        public string TosToString(bool popStack, bool noExpand, Dictionary <IntPtr, bool> printedTables)
        {
            LuaType type = BBLua.lua_type(this.L, -1);
            string  result;

            switch (type)
            {
            case LuaType.Nil:
                result = "nil";
                break;

            case LuaType.Boolean:
                result = BBLua.lua_toboolean(this.L, -1).ToString();
                break;

            case LuaType.Number:
                result = BBLua.lua_tonumber(this.L, -1).ToString();
                break;

            case LuaType.String:
                result = "\"" + BBLua.lua_tostring(this.L, -1) + "\"";
                break;

            case LuaType.Function:
                result = GetTosFunctionInfo();
                break;

            case LuaType.Table:
                if (noExpand)
                {
                    goto default;
                }

                IntPtr tblPtr = BBLua.lua_topointer(this.L, -1);
                if (printedTables.ContainsKey(tblPtr))
                {
                    result = "<Table, recursion>";
                    break;
                }

                result = "{";
                BBLua.lua_pushnil(this.L);
                while (BBLua.lua_next(this.L, -2) != 0)
                {
                    printedTables.Add(tblPtr, true);
                    string val = IndentMultiLine(TosToString(true, false, printedTables));
                    string key = TosToString(false, true, printedTables);
                    printedTables.Remove(tblPtr);

                    if (key[0] == '\"')
                    {
                        string rawString = key.Substring(1, key.Length - 2);
                        if (alphaNumeric.IsMatch(rawString))
                        {
                            result += "\n    " + rawString + " = " + val + ",";
                        }
                        else
                        {
                            result += "\n    [" + key + "] = " + val + ",";
                        }
                    }
                    else
                    {
                        result += "\n    [" + key + "] = " + val + ",";
                    }
                }
                result += "\n}";
                break;

            case LuaType.UserData:
            case LuaType.LightUserData:
                IntPtr ptr = BBLua.lua_touserdata(this.L, -1);
                result = "<" + type.ToString() + ", at 0x" + ptr.ToInt32().ToString("X") + ">";
                break;

            default:
                result = "<" + type.ToString() + ">";
                break;
            }

            if (popStack)
            {
                BBLua.lua_settop(this.L, -2);
            }
            return(result);
        }