internal void AddCallbackFunc(string[] path, int index, CallbackFunc func) { if (index + 1 >= path.Length) { //At lowest level, add callback func if (callbackTables.ContainsKey(path[index])) { throw new Exception($"Cannot add {string.Join(".",path)} ({func.Name}), a Table with that key exists"); } else if (callbackFunctions.ContainsKey(path[index])) { throw new Exception($"Cannot add {string.Join(".", path)} ({func.Name}), a Function with that key exists"); } callbackFunctions[path[index]] = func; } else { if (callbackFunctions.ContainsKey(path[index])) { throw new Exception($"Cannot add {string.Join(".", path)} ({func.Name}), a Function with the key ({path[index]}) exists in the path"); } CallbackTable nextTable; if (callbackTables.TryGetValue(path[index], out nextTable)) { nextTable.AddCallbackFunc(path, index + 1, func); } else { nextTable = new CallbackTable(path[index]); callbackTables.Add(path[index], nextTable); nextTable.AddCallbackFunc(path, index + 1, func); } } }
/// <summary> /// Automatically create tables, etc /// </summary> /// <param name="dict"></param> /// <param name="pathString"></param> public static void CreateCallbackItem(Dictionary <string, CallbackItem> dict, string pathString, Delegate callback, string documentation, string example) { if (string.IsNullOrWhiteSpace(pathString)) { throw new Exception($"Path cannot be null, empty, or whitespace for path [{pathString}] MethodInfo: ({callback.Method.Name})"); } var path = pathString.Split('.'); string root = path[0]; CallbackFunc func = new CallbackFunc(path[path.Length - 1], callback, documentation, example); if (func.IsYieldable) { func.GenerateYieldableString(pathString); } if (path.Length == 1) { //Simple global function if (dict.ContainsKey(root)) { throw new Exception($"Cannot add {pathString} ({callback.Method.Name}), a {(dict[root].GetType() == typeof(CallbackFunc) ? "Function" : "Table" )} with that key already exists"); } dict[root] = func; } else { //Recursion time if (dict.TryGetValue(root, out CallbackItem item)) { if (item is CallbackTable t) { t.AddCallbackFunc(path, 1, func); t.GenerateYieldableString(); //Bake the yieldable string } else { throw new Exception($"Cannot add {pathString} ({callback.Method.Name}), One or more keys in the path is assigned to a function"); } } else { //Create new CallbackTable t = new CallbackTable(root); dict[root] = t; t.AddCallbackFunc(path, 1, func); t.GenerateYieldableString(); //Bake the yieldable string } } }