Exemplo n.º 1
0
        internal void AddBindUserObject(string[] path, int index, BindUserObject bindObj)
        {
            if (index + 1 >= path.Length)
            {
                //At lowest level, add enum
                if (bindTables.ContainsKey(path[index]))
                {
                    throw new Exception($"Cannot add {string.Join(".", path)} ({bindObj.Name}), a Table with that key already exists");
                }
                else
                {
                    CheckConflictingItems(path, index, bindObj);
                }
                bindObjects[path[index]] = bindObj;
            }
            else
            {
                CheckConflictingItems(path, index, bindObj);

                BindTable nextTable;
                if (bindTables.TryGetValue(path[index], out nextTable))
                {
                    nextTable.AddBindUserObject(path, index + 1, bindObj);
                }
                else
                {
                    //Create new table
                    nextTable = new BindTable(path[index]);
                    bindTables.Add(path[index], nextTable);
                    nextTable.AddBindUserObject(path, index + 1, bindObj);
                }
            }
        }
Exemplo n.º 2
0
        /// <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);
        }