Example #1
0
        /// <summary>
        /// Generate treescheme json file for a given type.
        /// </summary>
        /// <param name="rootAliasTypeName">Fullname of the type to use as the root of the tree</param>
        /// <param name="fieldSource">Enum to indicator how to find fields on types</param>
        /// <param name="outputPath">Path to save the output file relative to the Assets directory</param>
        /// <param name="typeIgnorePattern">Optional regex pattern to ignore types</param>
        /// <param name="nodeCommentProvider">Optional provider of node-comments</param>
        /// <param name="logger">Optional logger for diagnostic output</param>
        public static void GenerateSchemeToFile(
            string rootAliasTypeName,
            FieldSource fieldSource,
            string outputPath,
            Regex typeIgnorePattern = null,
            INodeCommentProvider nodeCommentProvider = null,
            Core.ILogger logger = null)
        {
            // Generate the json.
            var json = GenerateScheme(
                rootAliasTypeName, fieldSource, typeIgnorePattern, nodeCommentProvider, logger);

            if (json != null)
            {
                // Write the file.
                try
                {
                    var fullPath  = Path.Combine(UnityEngine.Application.dataPath, outputPath);
                    var outputDir = Path.GetDirectoryName(fullPath);
                    if (!Directory.Exists(outputDir))
                    {
                        logger?.LogDebug($"Creating output directory: '{outputDir}'");
                        Directory.CreateDirectory(outputDir);
                    }

                    File.WriteAllText(fullPath, json);
                    logger?.LogInformation($"Saved scheme: '{outputPath}'");
                }
                catch (Exception e)
                {
                    logger?.LogCritical($"Failed to save file: {e.Message.ToDistinctLines()}");
                }
            }
        }
        /// <summary>
        /// Attempt to load a <see cref="ITypeCollection"/> from a Assembly path.
        /// </summary>
        /// <param name="assemblyPath">Path to the assembly file</param>
        /// <param name="dependencyDirectories">List of directories to search for dependant assemblies</param>
        /// <param name="logger">Optional logger to provide diagnostic output</param>
        /// <returns><see cref="ITypeCollection"/> if successfull otherwise null</returns>
        internal static ITypeCollection TryLoad(
            string assemblyPath,
            IEnumerable <string> dependencyDirectories,
            Core.ILogger logger = null)
        {
            if (assemblyPath == null)
            {
                throw new ArgumentNullException(nameof(assemblyPath));
            }
            if (dependencyDirectories == null)
            {
                throw new ArgumentNullException(nameof(dependencyDirectories));
            }

            // Verify that dependency paths exists.
            foreach (var dependencyDirectory in dependencyDirectories)
            {
                if (!Directory.Exists(dependencyDirectory))
                {
                    logger?.LogCritical($"Dependency directory '{dependencyDirectory}' cannot be found");
                    return(null);
                }
            }

            // Create load context.
            var loadContext = CreateLoadContext(assemblyPath, logger);

            if (loadContext == null)
            {
                return(null);
            }

            // Load assemblies.
            var assemblies = LoadAssemblyFromPath(loadContext, assemblyPath, dependencyDirectories, logger);

            logger?.LogDebug($"Loaded {assemblies.Count} assemblies");

            // Load types.
            var typeCollection = TypeCollection.Create(assemblies, logger);

            logger?.LogDebug($"Loaded {typeCollection.TypeCount} types");
            return(typeCollection);
        }