示例#1
0
        internal void AddBindUserType(string[] path, int index, BindUserType bindType)
        {
            if (index + 1 >= path.Length)
            {
                //At lowest level, add enum
                if (bindTables.ContainsKey(path[index]))
                {
                    throw new Exception($"Cannot add {string.Join(".", path)} ({bindType.Name}), a Table with that key already exists");
                }
                else
                {
                    CheckConflictingItems(path, index, bindType);
                }

                bindTypes[path[index]] = bindType;
            }
            else
            {
                CheckConflictingItems(path, index, bindType);

                BindTable nextTable;
                if (bindTables.TryGetValue(path[index], out nextTable))
                {
                    nextTable.AddBindUserType(path, index + 1, bindType);
                }
                else
                {
                    //Create new table
                    nextTable = new BindTable(path[index]);
                    bindTables.Add(path[index], nextTable);
                    nextTable.AddBindUserType(path, index + 1, bindType);
                }
            }
        }
        /// <summary>
        /// Automatically create tables, etc
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="pathString"></param>
        internal static BindUserObject CreateBindUserObject(Dictionary <string, BindItem> dict, string pathString, object obj)
        {
            if (string.IsNullOrWhiteSpace(pathString))
            {
                throw new Exception($"Path cannot be null, empty, or whitespace for path [{pathString}]");
            }
            var            path    = pathString.Split('.');
            string         root    = path[0];
            BindUserObject bindObj = new BindUserObject(path[path.Length - 1], obj);

            var doc = obj.GetType().GetCustomAttribute <LunarBindDocumentationAttribute>()?.Data ?? "";
            var ex  = obj.GetType().GetCustomAttribute <LunarBindExampleAttribute>()?.Data ?? "";

            bindObj.Documentation = doc;
            bindObj.Example       = ex;


            if (path.Length == 1)
            {
                //Simple global function
                if (dict.ContainsKey(root))
                {
                    throw new Exception($"Cannot add {pathString} (global object), a {GetItemTypeStr(dict[root])} with that key already exists");
                }
                dict[root] = bindObj;
            }
            else
            {
                //Recursion time
                if (dict.TryGetValue(root, out BindItem item))
                {
                    if (item is BindTable t)
                    {
                        t.AddBindUserObject(path, 1, bindObj);
                        t.GenerateWrappedYieldString(); //Bake the yieldable string
                    }
                    else
                    {
                        throw new Exception($"Cannot add {pathString} (global object), One or more keys in the path is already assigned to an item");
                    }
                }
                else
                {
                    //Create new table
                    BindTable t = new BindTable(root);
                    dict[root] = t;
                    t.AddBindUserObject(path, 1, bindObj);
                }
            }

            return(bindObj);
        }
        //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);
        }
        /// <summary>
        /// Automatically create tables, etc for bind enums
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="pathString"></param>
        internal static BindEnum CreateBindEnum(Dictionary <string, BindItem> dict, string pathString, Type enumType)
        {
            if (string.IsNullOrWhiteSpace(pathString))
            {
                throw new Exception($"Path cannot be null, empty, or whitespace for path [{pathString}] Enum Type: ({enumType.Name})");
            }
            var path = pathString.Split('.');

            string   root     = path[0];
            BindEnum bindEnum = new BindEnum(path[path.Length - 1], enumType);


            if (path.Length == 1)
            {
                //Simple global function
                if (dict.ContainsKey(root))
                {
                    throw new Exception($"Cannot add {pathString} ({enumType.Name}), a {GetItemTypeStr(dict[root])} with that key already exists");
                }
                dict[root] = bindEnum;
            }
            else
            {
                //Recursion time
                if (dict.TryGetValue(root, out BindItem item))
                {
                    if (item is BindTable t)
                    {
                        t.AddBindEnum(path, 1, bindEnum);
                    }
                    else
                    {
                        throw new Exception($"Cannot add {pathString} ({enumType.Name}), The root key is a {GetItemTypeStr(dict[root])}");
                    }
                }
                else
                {
                    //Create new table
                    BindTable t = new BindTable(root);
                    dict[root] = t;
                    t.AddBindEnum(path, 1, bindEnum);
                }
            }

            return(bindEnum);
        }