A registry of all supported mod formats.
상속: IModFormatRegistry
예제 #1
0
        /// <summary>
        /// Searches for mod format assemblies in the specified path, and loads
        /// any mod formats that are found into a registry.
        /// </summary>
        /// <remarks>
        /// A mod format is loaded if the class implements <see cref="IModFormat"/> and is
        /// not abstract. Once loaded, the format is added to the registry.
        /// </remarks>
        /// <param name="p_mcmModCacheManager">The manager being used to manage mod caches.</param>
        /// <param name="p_stgScriptTypeRegistry">The registry listing the supported script types.</param>
        /// <param name="p_strSearchPath">The path in which to search for mod format assemblies.</param>
        /// <returns>A registry containing all of the discovered mod formats.</returns>
        public static IModFormatRegistry DiscoverFormats(IModCacheManager p_mcmModCacheManager, IScriptTypeRegistry p_stgScriptTypeRegistry, string p_strSearchPath)
        {
            Trace.TraceInformation("Discovering Mod Formats...");
            Trace.Indent();

            Trace.TraceInformation("Looking in: {0}", p_strSearchPath);

            IModFormatRegistry mfrRegistry = new ModFormatRegistry();

            if (!Directory.Exists(p_strSearchPath))
            {
                Trace.TraceError("Format search path does not exist.");
                Trace.Unindent();
                return(mfrRegistry);
            }

            string[] strAssemblies = Directory.GetFiles(p_strSearchPath, "*.dll");
            foreach (string strAssembly in strAssemblies)
            {
                Trace.TraceInformation("Checking: {0}", Path.GetFileName(strAssembly));
                Trace.Indent();

                Assembly asmGameMode = Assembly.LoadFile(strAssembly);
                Type[]   tpeTypes    = asmGameMode.GetExportedTypes();
                foreach (Type tpeType in tpeTypes)
                {
                    if (typeof(IModFormat).IsAssignableFrom(tpeType) && !tpeType.IsAbstract)
                    {
                        Trace.TraceInformation("Initializing: {0}", tpeType.FullName);
                        Trace.Indent();

                        ConstructorInfo cifConstructor = tpeType.GetConstructor(new Type[] { typeof(IModCacheManager), typeof(IScriptTypeRegistry) });
                        IModFormat      mftModFormat   = null;
                        if (cifConstructor != null)
                        {
                            mftModFormat = (IModFormat)cifConstructor.Invoke(new object[] { p_mcmModCacheManager, p_stgScriptTypeRegistry });
                        }
                        else
                        {
                            cifConstructor = tpeType.GetConstructor(new Type[] { });
                            if (cifConstructor != null)
                            {
                                mftModFormat = (IModFormat)cifConstructor.Invoke(null);
                            }
                        }
                        if (mftModFormat != null)
                        {
                            mfrRegistry.RegisterFormat(mftModFormat);
                        }

                        Trace.Unindent();
                    }
                }
                Trace.Unindent();
            }
            Trace.Unindent();
            return(mfrRegistry);
        }
예제 #2
0
		/// <summary>
		/// Searches for mod format assemblies in the specified path, and loads
		/// any mod formats that are found into a registry.
		/// </summary>
		/// <remarks>
		/// A mod format is loaded if the class implements <see cref="IModFormat"/> and is
		/// not abstract. Once loaded, the format is added to the registry.
		/// </remarks>
		/// <param name="p_mcmModCacheManager">The manager being used to manage mod caches.</param>
		/// <param name="p_stgScriptTypeRegistry">The registry listing the supported script types.</param>
		/// <param name="p_strSearchPath">The path in which to search for mod format assemblies.</param>
		/// <returns>A registry containing all of the discovered mod formats.</returns>
		public static IModFormatRegistry DiscoverFormats(IModCacheManager p_mcmModCacheManager, IScriptTypeRegistry p_stgScriptTypeRegistry, string p_strSearchPath)
		{
			Trace.TraceInformation("Discovering Mod Formats...");
			Trace.Indent();

			Trace.TraceInformation("Looking in: {0}", p_strSearchPath);

			IModFormatRegistry mfrRegistry = new ModFormatRegistry();
			if (!Directory.Exists(p_strSearchPath))
			{
				Trace.TraceError("Format search path does not exist.");
				Trace.Unindent();
				return mfrRegistry;
			}

			string[] strAssemblies = Directory.GetFiles(p_strSearchPath, "*.dll");
			foreach (string strAssembly in strAssemblies)
			{
				Trace.TraceInformation("Checking: {0}", Path.GetFileName(strAssembly));
				Trace.Indent();

				Assembly asmGameMode = Assembly.LoadFile(strAssembly);
				Type[] tpeTypes = asmGameMode.GetExportedTypes();
				foreach (Type tpeType in tpeTypes)
				{
					if (typeof(IModFormat).IsAssignableFrom(tpeType) && !tpeType.IsAbstract)
					{
						Trace.TraceInformation("Initializing: {0}", tpeType.FullName);
						Trace.Indent();

						ConstructorInfo cifConstructor = tpeType.GetConstructor(new Type[] { typeof(IModCacheManager), typeof(IScriptTypeRegistry) });
						IModFormat mftModFormat = null;
						if (cifConstructor != null)
							mftModFormat = (IModFormat)cifConstructor.Invoke(new object[] { p_mcmModCacheManager, p_stgScriptTypeRegistry });
						else
						{
							cifConstructor = tpeType.GetConstructor(new Type[] { });
							if (cifConstructor != null)
								mftModFormat = (IModFormat)cifConstructor.Invoke(null);
						}
						if (mftModFormat != null)
							mfrRegistry.RegisterFormat(mftModFormat);

						Trace.Unindent();
					}
				}
				Trace.Unindent();
			}
			Trace.Unindent();
			return mfrRegistry;
		}