Esempio n. 1
0
        /// <summary>
        /// Load module collection info from path
        /// </summary>
        /// <param name="url">The URL of the collection file to load</param>
        /// <returns></returns>
        public static bool TryLoadFromURL(string url, out ModuleCollection collection)
        {
            collection         = new ModuleCollection();
            collection.fileUrl = Path.GetFullPath(url);


            // Check file exists
            if (!File.Exists(collection.fileUrl))
            {
                Logger.LogError("Unable to load ModuleCollection from path (Unable to locate): '" + collection.fileUrl + "'");
                return(false);
            }

            // Load file info from aetherproj njox
            NjoxNode rootNode;

            if (!NjoxStatics.TryParseObject(File.ReadAllText(collection.fileUrl), out rootNode))
            {
                Logger.LogError("Unable to load ModuleCollection from path (Unable to parse njox): '" + collection.fileUrl + "'");
                return(false);
            }
            string mainDirectory = new FileInfo(collection.fileUrl).Directory.FullName;


            if (rootNode.Properties.Length != 2)
            {
                Logger.LogError("Unable to load ModuleCollection from path (Expected root node Project <version_number>): '" + collection.fileUrl + "'");
                return(false);
            }
            if (rootNode.PrimaryProperty.Key != "Project")
            {
                Logger.LogError("Unable to load ModuleCollection from path (Expected root node Project <version_number>): '" + collection.fileUrl + "'");
                return(false);
            }

            float version;

            if (!rootNode.Properties[1].GetKeyAs <float>(out version) || version != 1.0f)
            {
                Logger.LogError("Unable to load ModuleCollection from path (Expected Project version to be 1.0): '" + collection.fileUrl + "'");
                return(false);
            }


            // Read collection data
            if (!rootNode.HasChild("name"))
            {
                Logger.LogError("Unable to load ModuleCollection from path (Expected name node): '" + collection.fileUrl + "'");
                return(false);
            }

            // Read internal and display name
            {
                var nameNode = rootNode.GetFirstChild("name");
                if (!nameNode.HasProperty("internal"))
                {
                    Logger.LogError("ModuleCollection node 'name' is missing required property 'internal': '" + collection.fileUrl + "'");
                    return(false);
                }

                collection.internalName = nameNode.GetFirstProperty("internal").Value;
                collection.displayName  = collection.internalName;

                if (nameNode.HasProperty("display"))
                {
                    collection.displayName = nameNode.GetFirstProperty("display").Value;
                }
            }


            // Load build directories
            {
                NjoxNode modulesDir;
                if (!rootNode.TryGetFirstChild("modules_directory", out modulesDir))
                {
                    Logger.LogError("ModuleCollection missing required node 'modules_directory': '" + collection.fileUrl + "'");
                    return(false);
                }
                collection.modulesDirectory = Path.GetFullPath(Path.Combine(mainDirectory, modulesDir.PrimaryProperty.Value));

                NjoxNode metaDir;
                if (!rootNode.TryGetFirstChild("meta_directory", out metaDir))
                {
                    Logger.LogError("ModuleCollection missing required node 'meta_directory': '" + collection.fileUrl + "'");
                    return(false);
                }
                collection.metaDirectory = Path.GetFullPath(Path.Combine(mainDirectory, metaDir.PrimaryProperty.Value));

                NjoxNode binDir;
                if (!rootNode.TryGetFirstChild("output_directory", out binDir))
                {
                    Logger.LogError("ModuleCollection missing required node 'output_directory': '" + collection.fileUrl + "'");
                    return(false);
                }
                collection.outputDirectory = Path.GetFullPath(Path.Combine(mainDirectory, binDir.PrimaryProperty.Value));
            }


            if (!collection.LoadAllModules())
            {
                Logger.LogError("ModuleCollection failed to load all modules '" + collection.fileUrl + "'");
                return(false);
            }

            Logger.Log("ModuleCollection successfully loaded '" + collection.fileUrl + "' with " + collection.modules.Count + " modules", 4);
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Attempt to load
        /// </summary>
        /// <param name="url">The url to the module.aethermod file</param>
        /// <param name="container">The container which will be created</param>
        /// <returns>True if the container is successfully loaded</returns>
        public static bool TryLoadFromFile(string url, out ModuleContainer container)
        {
            container         = new ModuleContainer();
            container.fileUrl = Path.GetFullPath(url);


            // Check file exists
            if (!File.Exists(container.fileUrl))
            {
                Logger.LogError("Unable to load ModuleContainer from path (Unable to locate): '" + container.fileUrl + "'");
                return(false);
            }

            // Load file info from aetherproj njox
            NjoxNode rootNode;

            if (!NjoxStatics.TryParseObject(File.ReadAllText(container.fileUrl), out rootNode))
            {
                Logger.LogError("Unable to load ModuleContainer from path (Unable to parse njox): '" + container.fileUrl + "'");
                return(false);
            }
            string mainDirectory = new FileInfo(container.fileUrl).Directory.FullName;


            if (rootNode.Properties.Length != 2)
            {
                Logger.LogError("Unable to load ModuleContainer from path (Expected root node Module <version_number>): '" + container.fileUrl + "'");
                return(false);
            }
            if (rootNode.PrimaryProperty.Key != "Module")
            {
                Logger.LogError("Unable to load ModuleContainer from path (Expected root node Module <version_number>): '" + container.fileUrl + "'");
                return(false);
            }

            float version;

            if (!rootNode.Properties[1].GetKeyAs <float>(out version) || version != 1.0f)
            {
                Logger.LogError("Unable to load ModuleContainer from path (Expected Module version to be 1.0): '" + container.fileUrl + "'");
                return(false);
            }


            // Read collection data
            if (!rootNode.HasChild("name"))
            {
                Logger.LogError("Unable to load ModuleContainer from path (Expected name node): '" + container.fileUrl + "'");
                return(false);
            }

            // Read internal and display name
            {
                var nameNode = rootNode.GetFirstChild("name");
                if (!nameNode.HasProperty("internal"))
                {
                    Logger.LogError("ModuleContainer node 'name' is missing required property 'internal': '" + container.fileUrl + "'");
                    return(false);
                }

                container.internalName = nameNode.GetFirstProperty("internal").Value;
                container.displayName  = container.internalName;

                if (nameNode.HasProperty("display"))
                {
                    container.displayName = nameNode.GetFirstProperty("display").Value;
                }
            }


            // Load recipe assembly for this module
            {
                // Load recipe assembly
                Assembly recipeAssembly;
                if (!ModuleRecipe.TryLoadAssembly(Path.Combine(mainDirectory, container.internalName + "ModuleRecipe.cs"), out recipeAssembly))
                {
                    Logger.LogError("Failed to compile ModuleRecipe for '" + container.fileUrl + "'");
                    return(false);
                }

                // Check required class exists
                container.recipeType = recipeAssembly.GetType(container.internalName + "ModuleRecipe");
                if (container.recipeType == null)
                {
                    Logger.LogError("ModuleRecipe is missing " + container.internalName + "ModuleRecipe class for '" + container.fileUrl + "'");
                    return(false);
                }
                if (!container.recipeType.IsSubclassOf(typeof(ModuleRecipe)))
                {
                    Logger.LogError("ModuleRecipe " + container.internalName + "ModuleRecipe class needs to extend from ModuleRecipe for '" + container.fileUrl + "'");
                    return(false);
                }
            }


            Logger.Log("ModuleContainer successfully loaded '" + container.fileUrl, 5);
            return(true);
        }