Exemplo n.º 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);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Automatically create tables, etc
        /// </summary>
        /// <param name="dict"></param>
        /// <param name="pathString"></param>
        internal static BindUserType CreateBindType(Dictionary <string, BindItem> dict, string pathString, Type type)
        {
            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];
            BindUserType bindObj = new BindUserType(path[path.Length - 1], type);

            var doc = type.GetCustomAttribute <LunarBindDocumentationAttribute>()?.Data ?? "";
            var ex  = type.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.AddBindUserType(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.AddBindUserType(path, 1, bindObj);
                }
            }
            return(bindObj);
        }