// Copyright (c) Pixel Crushers. All rights reserved. #if !(USE_NLUA || OVERRIDE_LUA) using UnityEngine; using System.Collections.Generic; using Language.Lua; namespace PixelCrushers.DialogueSystem { /// /// This is a Lua table wrapper for LuaInterpreter. It isolates the /// underlying LuaInterpreter implementation from the rest of the /// Dialogue System. /// public class LuaTableWrapper { /// /// The LuaInterpreter Lua table. /// public Language.Lua.LuaTable luaTable = null; /// /// Initializes a new instance of the class. /// /// Lua table. public LuaTableWrapper(LuaTable luaTable) { this.luaTable = luaTable; } /// /// Indicates whether the wrapper points to a valid Lua table. /// /// true if this instance is valid; otherwise, false. public bool isValid { get { return (luaTable != null); } } /// /// Gets the number of elements in the table. /// /// The count. public int count { get { return (luaTable != null) ? Mathf.Max(luaTable.Length, luaTable.Count) : 0; } } /// /// Gets the keys as strings.If the table is a one-dimensional /// array, this returns the indices as strings. /// /// The keys. public IEnumerable keys { get { if ((luaTable != null) && (count > 0)) { foreach (LuaValue key in luaTable.Keys) { yield return key.ToString(); } } } } /// /// Gets the values. If a value is a table, this returns a /// LuaTableWrapper around it. /// /// The values. public IEnumerable values { get { if (luaTable != null) { if (luaTable.Length > 0) { // Get list values: foreach (LuaValue value in luaTable.ListValues) { yield return (value is LuaTable) ? new LuaTableWrapper(value as LuaTable) : LuaInterpreterExtensions.LuaValueToObject(value); } } else if (luaTable.Count > 0) { // Get dictionary values: foreach (KeyValuePair kvp in luaTable.KeyValuePairs) { yield return (kvp.Value is LuaTable) ? new LuaTableWrapper(kvp.Value as LuaTable) : LuaInterpreterExtensions.LuaValueToObject(kvp.Value); } } } } } /// @cond FOR_V1_COMPATIBILITY public bool IsValid { get { return isValid; } } public int Count { get { return count; } } public IEnumerable Keys { get { if ((luaTable != null) && (count > 0)) { foreach (LuaValue key in luaTable.Keys) { yield return key.ToString(); } } } } public IEnumerable Values { get { if (luaTable != null) { if (luaTable.Length > 0) { // Get list values: foreach (LuaValue value in luaTable.ListValues) { yield return (value is LuaTable) ? new LuaTableWrapper(value as LuaTable) : LuaInterpreterExtensions.LuaValueToObject(value); } } else if (luaTable.Count > 0) { // Get dictionary values: foreach (KeyValuePair kvp in luaTable.KeyValuePairs) { yield return (kvp.Value is LuaTable) ? new LuaTableWrapper(kvp.Value as LuaTable) : LuaInterpreterExtensions.LuaValueToObject(kvp.Value); } } } } } /// @endcond /// /// Gets the value with the specified string key. Returns a standard type such as /// string, float, bool, or null. If the value /// is a table, it returns a LuaTableWrapper around it. /// /// Key. public object this[string key] { get { if (luaTable == null) { if (DialogueDebug.logErrors) Debug.LogError(string.Format("{0}: Lua table is null; lookup[{1}] failed", new object[] { DialogueDebug.Prefix, key })); return null; } LuaValue luaValue = LuaNil.Nil; if (luaTable.Length > 0) { // Get list value: luaValue = luaTable.GetValue(Tools.StringToInt(key)); } else { // Get dictionary value: LuaValue luaValueKey = luaTable.GetKey(key); if (luaValueKey == LuaNil.Nil) { //--- Suppressed: if (DialogueDebug.LogErrors) Debug.LogError(string.Format("{0}: Lua table does not contain key [{1}]", new string[] { DialogueDebug.Prefix, key })); return null; } luaValue = luaTable.GetValue(key); } if (luaValue is LuaTable) { return new LuaTableWrapper(luaValue as LuaTable); } else { return LuaInterpreterExtensions.LuaValueToObject(luaValue); } } } /// /// Gets the value with the specified int key. Returns a standard type such as /// string, float, bool, or null. If the value /// is a table, it returns a LuaTableWrapper around it. /// /// Key. public object this[int key] { get { if (luaTable == null) { if (DialogueDebug.logErrors) Debug.LogError(string.Format("{0}: Lua table is null; lookup[{1}] failed", new object[] { DialogueDebug.Prefix, key })); return null; } LuaValue luaValue = LuaNil.Nil; if (luaTable.Length > 0) { // Get list value: luaValue = luaTable.GetValue(key); } else { // Get dictionary value: LuaValue luaValueKey = luaTable.GetKey(key.ToString()); if (luaValueKey == LuaNil.Nil) { //--- Suppressed: if (DialogueDebug.LogErrors) Debug.LogError(string.Format("{0}: Lua table does not contain key [{1}]", new string[] { DialogueDebug.Prefix, key })); return null; } luaValue = luaTable.GetValue(key); } if (luaValue is LuaTable) { return new LuaTableWrapper(luaValue as LuaTable); } else { return LuaInterpreterExtensions.LuaValueToObject(luaValue); } } } } } #endif