Beispiel #1
0
        internal void AddBindFunc(string[] path, int index, BindFunc bindFunc)
        {
            if (index + 1 >= path.Length)
            {
                //At lowest level, add callback func
                if (bindTables.ContainsKey(path[index]))
                {
                    throw new Exception($"Cannot add {string.Join(".",path)} ({bindFunc.Name}), a Table with that key already exists");
                }
                else
                {
                    CheckConflictingItems(path, index, bindFunc);
                }

                bindFunctions[path[index]] = bindFunc;
            }
            else
            {
                CheckConflictingItems(path, index, bindFunc);

                BindTable nextTable;
                if (bindTables.TryGetValue(path[index], out nextTable))
                {
                    nextTable.AddBindFunc(path, index + 1, bindFunc);
                }
                else
                {
                    nextTable = new BindTable(path[index]);
                    bindTables.Add(path[index], nextTable);
                    nextTable.AddBindFunc(path, index + 1, bindFunc);
                }
            }
        }
        //TODO: get the attributes here
        /// <summary>
        /// Automatically create tables, etc
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="pathString"></param>
        internal static BindFunc CreateBindFunction(Dictionary <string, BindItem> dict, string pathString, Delegate callback, bool autoYield = true, 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];
            BindFunc func = new BindFunc(path[path.Length - 1], callback, autoYield, documentation, example);

            if (autoYield && func.IsYieldable && GlobalScriptBindings.AutoYield)
            {
                func.GenerateYieldableString(pathString);
            }

            if (path.Length == 1)
            {
                //Simple global function
                if (dict.ContainsKey(root))
                {
                    throw new Exception($"Cannot add {pathString} ({callback.Method.Name}), a {GetItemTypeStr(dict[root])} with that key already exists");
                }
                dict[root] = func;
            }
            else
            {
                //Recursion time
                if (dict.TryGetValue(root, out BindItem item))
                {
                    if (item is BindTable t)
                    {
                        t.AddBindFunc(path, 1, func);
                        t.GenerateWrappedYieldString(); //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 table
                    BindTable t = new BindTable(root);
                    dict[root] = t;
                    t.AddBindFunc(path, 1, func);
                    t.GenerateWrappedYieldString(); //Bake the yieldable string
                }
            }

            return(func);
        }