public static void Startup(Config config, params string[] pathimports) { sinceStartup.Start(); TimeSpan ts = DateTime.UtcNow - UTCTime; timeStartup = ts.TotalMilliseconds; Thread.CurrentThread.Name = "TinyMUD.Main"; MainLoop = Loop.Current; Dictionary <Assembly, int> assemblies = new Dictionary <Assembly, int>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { assemblies[assembly] = 0; } Dictionary <string, string> imports = new Dictionary <string, string>(); foreach (Config import in config["Import"]) { string name = import["Name"].Value ?? Guid.NewGuid().ToString(); if (imports.ContainsKey(name)) { continue; } string path = import["Path"].Value; if (File.Exists(path)) { try { Assembly assembly = Assembly.LoadFrom(path); imports.Add(name, path); assemblies[assembly] = imports.Count; } catch (FileLoadException) { } catch (BadImageFormatException) { } } } foreach (string path in pathimports) { string name = Guid.NewGuid().ToString(); if (File.Exists(path)) { try { Assembly assembly = Assembly.LoadFrom(path); imports.Add(name, path); assemblies[assembly] = imports.Count; } catch (FileLoadException) { } catch (BadImageFormatException) { } } } Dictionary <Type, InitializeOnLoad> loadtypes = new Dictionary <Type, InitializeOnLoad>(); List <Tuple <Type, Assembly> > loadlist = new List <Tuple <Type, Assembly> >(); List <Tuple <Type, ModuleAttribute, Assembly> > modules = new List <Tuple <Type, ModuleAttribute, Assembly> >(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { object[] attrs = type.GetCustomAttributes(false); for (int i = 0; i < attrs.Length; ++i) { ModuleAttribute attr = attrs[i] as ModuleAttribute; if (attr != null) { modules.Add(Tuple.Create(type, attr, assembly)); break; } } for (int i = 0; i < attrs.Length; ++i) { InitializeOnLoad attr = attrs[i] as InitializeOnLoad; if (attr != null) { if (!loadtypes.ContainsKey(type)) { loadtypes.Add(type, attr); loadlist.Add(Tuple.Create(type, assembly)); } break; } } } } loadlist.Sort((x, y) => { int prix; if (!assemblies.TryGetValue(x.Item2, out prix)) { prix = int.MaxValue; } int priy; if (!assemblies.TryGetValue(y.Item2, out priy)) { priy = int.MaxValue; } if (prix == priy) { return(string.CompareOrdinal(x.Item1.FullName, y.Item1.FullName)); } return(prix - priy); }); modules.Sort((x, y) => { int prix; if (!assemblies.TryGetValue(x.Item3, out prix)) { prix = int.MaxValue; } int priy; if (!assemblies.TryGetValue(y.Item3, out priy)) { priy = int.MaxValue; } if (prix == priy) { return(string.CompareOrdinal(x.Item1.FullName, y.Item1.FullName)); } return(prix - priy); }); Type[] loadorders = new Type[loadlist.Count]; int index = 0; Dictionary <Type, bool> pendings = new Dictionary <Type, bool>(); for (int i = 0; i < loadlist.Count; ++i) { SortLoad(loadtypes, pendings, loadorders, loadlist[i].Item1, ref index); } for (int i = 0; i < loadorders.Length; ++i) { RuntimeHelpers.RunClassConstructor(loadorders[i].TypeHandle); } for (int i = 0; i < modules.Count; ++i) { Type type = modules[i].Item1; ModuleAttribute attr = modules[i].Item2; Action load = FindMethod(type, attr.Load, "Load"); if (load != null) { load(); } Action unload = FindMethod(type, attr.Unload, "Unload"); if (unload != null) { unloads.Add(unload); } } if (Load != null) { Load(config); } }
public static void Startup(string[] args) { sinceStartup.Start(); TimeSpan ts = DateTime.UtcNow - UTCTime; timeStartup = ts.TotalMilliseconds; Thread.CurrentThread.Name = "TinyMUD.Main"; MainLoop = Loop.Current; Dictionary <Type, InitializeOnLoad> loadtypes = new Dictionary <Type, InitializeOnLoad>(); List <Type> loadlist = new List <Type>(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in assembly.GetTypes()) { object[] attrs = type.GetCustomAttributes(false); for (int i = 0; i < attrs.Length; ++i) { ModuleAttribute attr = attrs[i] as ModuleAttribute; if (attr != null) { modules.Add(new KeyValuePair <Type, ModuleAttribute>(type, attr)); break; } } for (int i = 0; i < attrs.Length; ++i) { InitializeOnLoad attr = attrs[i] as InitializeOnLoad; if (attr != null) { if (!loadtypes.ContainsKey(type)) { loadtypes.Add(type, attr); loadlist.Add(type); } break; } } } } loadlist.Sort(); Type[] loadorders = new Type[loadlist.Count]; int index = 0; Dictionary <Type, bool> pendings = new Dictionary <Type, bool>(); for (int i = 0; i < loadlist.Count; ++i) { SortLoad(loadtypes, pendings, loadorders, loadlist[i], ref index); } for (int i = 0; i < loadorders.Length; ++i) { RuntimeHelpers.RunClassConstructor(loadorders[i].TypeHandle); } for (int i = 0; i < modules.Count; ++i) { Type type = modules[i].Key; ModuleAttribute module = modules[i].Value; Action load = Delegate.CreateDelegate(typeof(Action), type, module.load) as Action; if (load != null) { load(); } } }