/// <summary> /// Update the list of bound types on the LuaBindings object. /// </summary> protected static void PopulateBoundTypes(LuaBindings luaBindings, SerializedObject so) { // Use a temp HashSet to store the list of types. // The final list is stored as a list of type strings. HashSet <System.Type> typeSet = new HashSet <System.Type>(); foreach (BoundObject boundObject in luaBindings.BoundObjects) { if (boundObject.obj == null) { continue; } AddAllSubTypes(typeSet, boundObject.obj.GetType()); if (boundObject.component != null) { AddAllSubTypes(typeSet, boundObject.component.GetType()); } } // Store the final list of types in the luaBindings object SerializedProperty boundTypesProp = so.FindProperty("boundTypes"); boundTypesProp.ClearArray(); int index = 0; foreach (System.Type t in typeSet) { boundTypesProp.InsertArrayElementAtIndex(index); SerializedProperty element = boundTypesProp.GetArrayElementAtIndex(index); element.stringValue = t.AssemblyQualifiedName; index++; } }
/// <summary> /// Returns a new binding key that is guaranteed to be a valid Lua variable name and /// not to clash with any existing binding in the list. /// </summary> protected virtual string GetUniqueKey(LuaBindings luaBindings, string originalKey, int ignoreIndex = -1) { string baseKey = originalKey; // Only letters and digits allowed char[] arr = baseKey.Where(c => (char.IsLetterOrDigit(c) || c == '_')).ToArray(); baseKey = new string(arr); // No leading digits allowed baseKey = baseKey.TrimStart('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'); // No empty keys allowed if (baseKey.Length == 0) { baseKey = "object"; } // Build a hash of all keys currently in use HashSet <string> keyhash = new HashSet <string>(); for (int i = 0; i < luaBindings.BoundObjects.Count; ++i) { if (i == ignoreIndex) { continue; } keyhash.Add(luaBindings.BoundObjects[i].key); } // Append a suffix to make the key unique string key = baseKey; int suffix = 0; while (keyhash.Contains(key)) { suffix++; key = baseKey + suffix; } return(key); }