public static void RunWithImaginaryHierarchyObject(Assembly[] userAssemblies,
                                                           ImaginaryHierarchyObject rootHierarchyObject, bool raiseStartEvent = true)
        {
            if (IsRunning)
            {
                throw new Exception("Already running!");
            }

            #region Creating

            HierarchyManager.AddHierarchy(rootHierarchyObject.Name, (HierarchyObject)rootHierarchyObject.CreateInstance());

            #endregion

            Run(userAssemblies, raiseStartEvent);
        }
        private void CreateTreeForHierarchyObject(ImaginaryHierarchyObject hierarchyObject)
        {
            if (ImGui.TreeNodeEx(hierarchyObject.Name, ReferenceEquals(CurrentSelectedHierarchyObject, hierarchyObject) ? ImGuiTreeNodeFlags.Selected : ImGuiTreeNodeFlags.None) || ReferenceEquals(CurrentSelectedHierarchyObject, hierarchyObject))
            {
                ImGui.SameLine();

                if (ImGui.Button("Select"))
                {
                    CurrentSelectedHierarchyObject = hierarchyObject;
                }

                foreach (var hierarchyObjectChild in hierarchyObject.LocalHierarchy)
                {
                    CreateTreeForHierarchyObject(hierarchyObject.LocalHierarchy[hierarchyObjectChild.Key]);
                }
                ImGui.TreePop();
            }
        }
        public static void Run(ImaginaryHierarchyObject rootHierarchyObject, Assembly userGeneratedCode)
        {
            #region Running
            RuntimeInformation.UserAssemblies = new System.Collections.Generic.HashSet <Assembly>
            {
                typeof(ScriptObject).Assembly,
                typeof(Script).Assembly,
                userGeneratedCode,
            };

            RuntimeInformation.UserAssemblies.UnionWith(new[] { Assembly.GetExecutingAssembly(), Assembly.GetAssembly(typeof(HierarchyObject)), Assembly.GetAssembly(typeof(ScriptObject)) });

            RuntimeMain.RunWithImaginaryHierarchyObject(RuntimeInformation.UserAssembliesArray, rootHierarchyObject);

            RuntimeMain.WaitForStop(10);

            RuntimeInformation.UserAssemblies = null;
            #endregion
        }
            static ImaginaryHierarchyObject CreateImaginaryHierarchyObject(ref ImaginaryHierarchyObject currentSelectedHierarchyObject, Type ofType)
            {
                if (ofType.IsEditable())
                {
                    EditorData editorData = EditorData.GetEmpty();
                    EditableSystem.OpenEditor(ofType, ref editorData);
                    return(new ImaginaryHierarchyObject(currentSelectedHierarchyObject,
                                                        new ImaginaryEditableObject(ofType, editorData)));
                }

                if (ofType.GetConstructors().Length > 0)
                {
                    return(new ImaginaryHierarchyObject(currentSelectedHierarchyObject,
                                                        new ImaginaryConstructableObject(ofType, GetConstructorParameters(ofType))));
                }

                return(new ImaginaryHierarchyObject(currentSelectedHierarchyObject,
                                                    new ImaginaryConstructableObject(ofType)));
            }
#pragma warning restore CA1031 // Do not catch general exception types
#endif
            static void Modify(ref ImaginaryHierarchyObject currentSelectedHierarchyObject, string toModify = null)
            {
                ImaginaryHierarchyObject hierarchyObjectToModify;

                if (string.IsNullOrEmpty(toModify))
                {
                    hierarchyObjectToModify = currentSelectedHierarchyObject;
                }
                else
                {
                    if (!currentSelectedHierarchyObject.LocalHierarchy.ContainsKey(toModify))
                    {
                        Output.ErrorLog($"command error: no HierarchyObject named {toModify} can be found!");
                        return;
                    }

                    hierarchyObjectToModify = currentSelectedHierarchyObject.LocalHierarchy[toModify];
                }

                if (AskYOrNQuestion(
                        $"Do you want to change the name of the HierarchyObject? Name = {GetName(hierarchyObjectToModify)}"))
                {
                    SetName(hierarchyObjectToModify, AskQuestion("Write the new name"));
                }

                if (hierarchyObjectToModify.ImaginaryObjectBase is ImaginaryEditableObject imaginaryEditableObject)
                {
                    EditableSystem.OpenEditor(imaginaryEditableObject.TypeData.GetConstructionType(),
                                              ref ((ImaginaryEditableObject)hierarchyObjectToModify.ImaginaryObjectBase).EditorData);
                }
                else if (hierarchyObjectToModify.ImaginaryObjectBase is ImaginaryConstructableObject
                         imaginaryConstructableObject)
                {
                    imaginaryConstructableObject.ImaginaryConstructionParameters =
                        GetConstructorParameters(imaginaryConstructableObject.TypeData.GetConstructionType());
                }
                else
                {
                    Output.ErrorLog(
                        $"{hierarchyObjectToModify.ImaginaryObjectBase.GetType()} cannot be modified using this tool.");
                }
            }
            // Lists all HierarchyObjects in the current HierarchyObject's local Hierarchy.
            static void List(ref ImaginaryHierarchyObject currentSelectedHierarchyObject, string toList = null)
            {
                ImaginaryHierarchyObject hierarchyObjectToList;

                if (string.IsNullOrEmpty(toList))
                {
                    hierarchyObjectToList = currentSelectedHierarchyObject;
                }
                else
                {
                    hierarchyObjectToList = currentSelectedHierarchyObject.LocalHierarchy[toList];
                }

                foreach (var name in currentSelectedHierarchyObject.LocalHierarchy.Keys)
                {
                    Output.Log(name);
                }

                Output.Log("A total of " + hierarchyObjectToList.LocalHierarchy.Count +
                           " HierarchyObjects in the local hierarchy.");
            }
        private static void SetUpEditor()
        {
            {
                FileInfo[] files = CurrentProject.ScriptsDirectory.GetFiles("*.cs");

                CodeFilePaths = new string[files.Length];

                for (var i = 0; i < files.Length; i++)
                {
                    CodeFilePaths[i] = files[i].FullName;
                }
            }

            // Compile the code.
            UserAssembly = MainClass.Compile();

            MainClass.Analyze(UserAssembly);

            var fileSystemWatcher = new FileSystemWatcher(CurrentProject.ScriptsDirectory.FullName, "*.cs");

            fileSystemWatcher.Changed += (_, _1) =>
            {
                Output.Log("Code change detected, recompiling.");
                CodeFilePaths = Directory.GetFiles(CurrentProject.ScriptsDirectory.FullName, "*.cs");
                // TODO: something to wait until the file is ready.
                Thread.Sleep(100);
                MainClass.Compile();
                MainClass.Analyze(UserAssembly);
            };
            fileSystemWatcher.EnableRaisingEvents = true;

            RootHierarchyObject =
                new ImaginaryHierarchyObject(null, new ImaginaryConstructableObject(typeof(HierarchyRoot)));

            CurrentSelectedHierarchyObject = RootHierarchyObject;

            EditorWindowTypes = ScanningSystem.ScanningSystem.FindSubclasses <EditorWindow>(ScanningSystem.ScanningSystem.TypesInAssemblies((IEnumerable <Assembly>) new[] { Assembly.GetExecutingAssembly(), UserAssembly }));
        }
        private static void Main()
        {
            #region Project Selection

            Output.Log("Please open or create a new project:");
ProjectSelection:
            switch (Console.ReadLine())
            {
            case "new":
                ProjectInfo.NewProject(AskQuestion("Pick a path for the new project"),
                                       AskQuestion("Pick a name for the new project"));
                break;

            case "open":
                try
                {
                    ProjectInfo.OpenProject(AskQuestion("Enter the path of the project"));
                }
                catch (ArgumentException)
                {
                    goto ProjectSelection;
                }

                break;

            default:
                Output.ErrorLog("command error: unknown command");
                goto ProjectSelection;
            }
            #endregion

            #region Compilation
            // TODO: should update this when a new project is loaded.
            // TODO: make this into a property in ProjectInfo.

            {
                FileInfo[] files = CurrentProject.ScriptsDirectory.GetFiles("*.cs");

                CodeFilePaths = new string[files.Length];

                for (var i = 0; i < files.Length; i++)
                {
                    CodeFilePaths[i] = files[i].FullName;
                }
            }

            // Compile the code.
            var compiledAssembly = Compile();

            Analyze(compiledAssembly);

            // TODO: update this when a new ProjectInfo is used.
            var fileSystemWatcher = new FileSystemWatcher(CurrentProject.ScriptsDirectory.FullName, "*.cs");
            fileSystemWatcher.Changed += (_, _1) =>
            {
                Output.Log("Code change detected, recompiling.");
                CodeFilePaths = Directory.GetFiles(CurrentProject.ScriptsDirectory.FullName, "*.cs");
                // TODO: something to wait until the file is ready.
                Thread.Sleep(100);                 // OTHER THAN THIS LOL
                Compile();
                Analyze(compiledAssembly);
            };
            fileSystemWatcher.EnableRaisingEvents = true;

            #endregion

            #region Editor loop

            // Very basic editor.

            var rootHierarchyObject =
                new ImaginaryHierarchyObject(null, new ImaginaryConstructableObject(typeof(HierarchyRoot)));

            ImaginaryHierarchyObject currentSelectedHierarchyObject = rootHierarchyObject;

            while (true)
            {
                Output.Log();

                var line = Console.ReadLine();

                ParseCommand(line, ref rootHierarchyObject, ref currentSelectedHierarchyObject, compiledAssembly);
            }
            #endregion
        }
            // TODO: add forwardsteps. A selection can be done like this to traverse multiple layers <<< MyFolder > MySubfolder > MyObject
            static void Select(ref ImaginaryHierarchyObject currentSelectedHierarchyObject, string editorObjectSelectQuery = null)
            {
                // Store the status of the currently selected HierarchyObject so we can revert back here.
                ImaginaryHierarchyObject initiallySelected = currentSelectedHierarchyObject;

                // Add selections when the query is empty.
                if (string.IsNullOrEmpty(editorObjectSelectQuery))
                {
                    currentSelectedHierarchyObject = SelectItem(currentSelectedHierarchyObject.LocalHierarchy.Values);
                    return;
                }

                // Does this query start with a backstep?
                if (editorObjectSelectQuery.StartsWith("<"))
                {
                    // Get the count of backsteps.
                    var backStepCount = editorObjectSelectQuery.TakeWhile(c => c == '<').Count();

                    // Backstep.
                    for (var i = 0; i < backStepCount; i++)
                    {
                        // Check if the HierarchyObject actually has a parent.
                        if (currentSelectedHierarchyObject.Parent is null)
                        {
                            Output.ErrorLog(
                                $"error: {currentSelectedHierarchyObject} does not have a parent. Reverting the select.");
                            currentSelectedHierarchyObject = initiallySelected;
                            return;
                        }

                        // Perform the backstep by going back to the parent of currentEditorHierarchyObject.
                        currentSelectedHierarchyObject = currentSelectedHierarchyObject.Parent;
                    }

                    // Was the whole query a backstep?
                    if (editorObjectSelectQuery.EndsWith("<"))
                    {
                        // Then we don't need to do anything else.
                        return;
                    }

                    // Remove the backstep characters from the query, since they have already been counted.
                    editorObjectSelectQuery = editorObjectSelectQuery.Remove(0, backStepCount);
                }
                // Does the query contain backsteps in another location than the start of the string? That's illegal.
                else if (editorObjectSelectQuery.Contains('<'))
                {
                    Output.ErrorLog(
                        "error: backsteps ('<') cannot be located anywhere else in the query other than at the start.");
                }

                if (!currentSelectedHierarchyObject.LocalHierarchy.ContainsKey(editorObjectSelectQuery))
                {
                    Output.ErrorLog(
                        $"error: the requested HierarchyObject doesn't exist. Name = {editorObjectSelectQuery}");
                    currentSelectedHierarchyObject = initiallySelected;
                    return;
                }

                currentSelectedHierarchyObject = currentSelectedHierarchyObject.LocalHierarchy[editorObjectSelectQuery];
            }
Example #10
0
            static void Details(ref ImaginaryHierarchyObject currentSelectedHierarchyObject, string toDetail = null)
            {
                ImaginaryHierarchyObject hierarchyObjectToViewDetailsOf;

                if (string.IsNullOrEmpty(toDetail))
                {
                    hierarchyObjectToViewDetailsOf = currentSelectedHierarchyObject;
                    if (hierarchyObjectToViewDetailsOf.Parent is not null)
                    {
                        toDetail = GetName(hierarchyObjectToViewDetailsOf);
                    }
                }
                else
                {
                    if (!currentSelectedHierarchyObject.LocalHierarchy.ContainsKey(toDetail))
                    {
                        Output.ErrorLog($"command error: no HierarchyObject named {toDetail} can be found!");
                        return;
                    }

                    hierarchyObjectToViewDetailsOf = currentSelectedHierarchyObject.LocalHierarchy[toDetail];
                }

                Output.Log($"Details for {hierarchyObjectToViewDetailsOf}:");

                Output.Log($"Name: {toDetail ?? "\b\b is unknown. Presumably the HierarchyObject is root."}");

                Output.Log($"Type: {hierarchyObjectToViewDetailsOf}");

                if (hierarchyObjectToViewDetailsOf.ImaginaryObjectBase is ImaginaryConstructableObject
                    imaginaryConstructable)
                {
                    Output.Log("This HierarchyObject uses constructor parameters to be created.");

                    Output.Log($"Parameter count: {imaginaryConstructable.ImaginaryConstructionParameters.Length}");

                    Console.Write("Parameters: (");
                    var first = true;
                    foreach (ImaginaryObject parameter in imaginaryConstructable.ImaginaryConstructionParameters)
                    {
                        // Put commas after every parameter if unless it's the first parameter.
                        if (!first)
                        {
                            Console.Write(", ");
                        }

                        Console.Write(parameter.ToString());

                        first = false;
                    }

                    Console.Write(")\n");
                }
                else if (hierarchyObjectToViewDetailsOf.ImaginaryObjectBase is ImaginaryEditableObject
                         imaginaryEditableObject)
                {
                    Output.Log("This HierarchyObject uses an Editor to be created and modified.");

                    Output.Log($"EditorData count: {imaginaryEditableObject.EditorData.Count}");

                    Console.Write("EditorData: (");
                    var first = true;
                    foreach (KeyValuePair <string, string> data in imaginaryEditableObject.EditorData)
                    {
                        // Put commas after every parameter if unless it's the first parameter.
                        if (!first)
                        {
                            Console.Write(", ");
                        }

                        Console.Write($"{data.Key}: {data.Value}");

                        first = false;
                    }

                    Console.Write(")\n");
                }
                else
                {
                    Output.ErrorLog(
                        $"Cannot detail the construction of a HierarchyObject of type {hierarchyObjectToViewDetailsOf.ImaginaryObjectBase?.GetType()}.");
                    // TODO: use reflection to try and detail it anyways?
                }

                // TODO: add LocalHierarchy and AttachedScripts information here.
            }
Example #11
0
        // TODO: should make main method that wraps loop around this.
        public static void ParseCommand(string input, ref ImaginaryHierarchyObject rootHierarchyObject, ref ImaginaryHierarchyObject currentSelectedHierarchyObject, Assembly userGeneratedCode)
        {
            string[] commandSections = input.Split(' ');

            // Command recognition.
#if RELEASE
            try
            {
#endif
            switch (commandSections[0])
            {
            case "asset":
                switch (commandSections[1])
                {
                case "new":
                    switch (commandSections[2])
                    {
                    case "script":
                        AssetManagement.CreateNewScript(commandSections[3]);
                        break;

                    case "hierarchy":
                        AssetManagement.CreateNewHiearchy(commandSections[3]);
                        break;
                    }
                    break;

                case "delete":
                    AssetManagement.DeleteAsset(commandSections[3]);
                    break;

                case "list":
                    break;
                }
                break;

            case "compile":
                userGeneratedCode = Compile();
                Analyze(userGeneratedCode);
                break;

            case "new":
                AddHierarchyObject(currentSelectedHierarchyObject, CreateImaginaryHierarchyObject(ref currentSelectedHierarchyObject, SelectItem(HierarchyObjectTypes)), commandSections[1]);
                break;

            case "del":
                DeleteHierarchyObject(currentSelectedHierarchyObject, commandSections.Length < 1
                                                        ? GetName(currentSelectedHierarchyObject)
                                                        : commandSections[1]);
                break;

            case "rename":
                Rename(currentSelectedHierarchyObject, commandSections[1]);
                break;

            case "modify":
                Modify(ref currentSelectedHierarchyObject, commandSections.Length >= 2 ? commandSections[1] : null);
                break;

            case "details":
                Details(ref currentSelectedHierarchyObject, commandSections.Length >= 2 ? commandSections[1] : null);
                break;

            case "add":
                AddScript(currentSelectedHierarchyObject, CreateImaginaryScript(SelectItem(ScriptTypes)), commandSections[1]);
                break;

            case "rem":
                RemoveScript(currentSelectedHierarchyObject, commandSections[1]);
                break;

            case "save":
                Save(commandSections[1], rootHierarchyObject);
                break;

            case "load":
                rootHierarchyObject            = Load(commandSections[1]);
                currentSelectedHierarchyObject = rootHierarchyObject;
                break;

            case "pack":
                Pack(commandSections[1], rootHierarchyObject);
                break;

            case "unpack":
                rootHierarchyObject            = Unpack(commandSections[1]);
                currentSelectedHierarchyObject = rootHierarchyObject;
                break;

            case "list":
                List(ref currentSelectedHierarchyObject, commandSections.Length >= 2 ? commandSections[1] : null);
                break;

            case "select":
                Select(ref currentSelectedHierarchyObject, commandSections.Length >= 2 ? commandSections[1] : null);
                break;

            case "export":
                switch (commandSections[1])
                {
                case "prefab":
                    ExportPrefab(currentSelectedHierarchyObject, commandSections[2], commandSections[3]);
                    break;

                case "copy":
                    ExportHierarchy(currentSelectedHierarchyObject, commandSections[2], commandSections[3]);
                    break;

                default:
                    Output.ErrorLog("command error: unknown subcommand");
                    break;
                }

                break;

            case "import":
                switch (commandSections[1])
                {
                case "prefab":
                    var prefab = ImportPrefab(commandSections[2]);
                    AddHierarchyObject(currentSelectedHierarchyObject, (ImaginaryHierarchyObject)prefab.CreateInstance(), prefab.Name);
                    break;

                case "copy":
                    var hierarchy = ImportHierarchy(commandSections[2]);
                    AddHierarchyObject(currentSelectedHierarchyObject, (ImaginaryHierarchyObject)hierarchy.CreateInstance(), hierarchy.Name);
                    break;

                default:
                    Output.ErrorLog("command error: unknown subcommand");
                    break;
                }

                break;

            case "project":
                return;

            case "script":
                break;

            case "build":
                Builder.Build(commandSections[1], commandSections[2]);
                break;

            case "run":
                Run(rootHierarchyObject, userGeneratedCode);
                break;

            case "exit":
                Environment.Exit(0);
                break;

            default:
                Output.ErrorLog("command error: unknown command");
                break;
            }
#if RELEASE
        }

#pragma warning disable CA1031 // Do not catch general exception types
        catch (Exception)      // Implement the same error handling as before.
        {
            throw;
        }
Example #12
0
 private static void UI(ref ImaginaryHierarchyObject rootHierarchyObject, ref ImaginaryHierarchyObject currentSelectedHierarchyObject, Assembly userGeneratedCode)
 {
     MenuBar();
     DrawWindows();
 }