FreeTrainの進化系を目指す
MainFrameを置き換えやすくするため、MainクラスをApplicationContextに変更
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | |
| 36 | 36 | private static void run( string[] args ) { |
| 37 | 37 | // start the game |
| 38 | - Application.Run(new MainFrame(args)); | |
| 38 | + Application.Run(new Main(args)); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 |
| @@ -20,101 +20,100 @@ | ||
| 20 | 20 | /// <summary> |
| 21 | 21 | /// Entry point to other static instances in the NeoFreeTrain framework. |
| 22 | 22 | /// </summary> |
| 23 | - public sealed class Main | |
| 24 | - { | |
| 25 | - // no instantiation | |
| 26 | - private Main() {} | |
| 23 | + // [参考] https://msdn.microsoft.com/ja-jp/library/system.windows.forms.applicationcontext(v=vs.110).aspx | |
| 24 | + public sealed class Main : ApplicationContext { | |
| 25 | + | |
| 26 | + public Main(string[] args) { | |
| 27 | + I18n.LoadFile(Directories.AppBaseDir + "nftfw.resource.xml"); | |
| 27 | 28 | |
| 28 | - /// <summary> the main window frame </summary> | |
| 29 | - private static MainFrame theFrame; | |
| 30 | - public static IBarHostFrame mainFrame | |
| 31 | - { get{ return theFrame; } } | |
| 29 | + theFrame = CreateFrame(); | |
| 32 | 30 | |
| 33 | - /// <summary> Main resources. </summary> | |
| 31 | + // Handle the ApplicationExit event to know when the application is exiting. | |
| 32 | + Application.ApplicationExit += new EventHandler(this.OnApplicationExit); | |
| 33 | + | |
| 34 | + InitDirectoriesAndAssemblies(args); | |
| 35 | + | |
| 36 | + using (Splash s = new Splash()) { | |
| 37 | + s.Show(); | |
| 38 | + ProgressMonitor monitor = CreateProgressMonitor(new ProgressHandler(s.updateMessage)); | |
| 39 | + InitPlugins(monitor); | |
| 40 | + } | |
| 41 | + | |
| 42 | + FireInitCompleteEvent(); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /// <summary> the main window frame </summary> | |
| 46 | + private static IBarHostFrame theFrame; | |
| 47 | + public static IBarHostFrame mainFrame { get { return theFrame; } } | |
| 48 | + | |
| 49 | + private EventHandler InitComplete; | |
| 50 | + | |
| 51 | + /// <summary> Main resources. </summary> | |
| 34 | 52 | //public static readonly NftProperties resources = NftProperties.LoadFromFile(Directories.AppBaseDir + "nftfw.resource.xml", false); |
| 35 | 53 | |
| 36 | - /// <summary> Plug-ins. </summary> | |
| 37 | - public static readonly PluginManager pluginManager = new PluginManager(); | |
| 54 | + /// <summary> Plug-ins. </summary> | |
| 55 | + public static readonly PluginManager pluginManager = new PluginManager(); | |
| 38 | 56 | |
| 39 | - #region sound and BGM | |
| 40 | -/* | |
| 41 | - /// <summary> | |
| 42 | - /// Handles BGM playback. | |
| 43 | - /// Should be instanciated by attaching the main window. | |
| 44 | - /// </summary> | |
| 45 | - public static BGMManager bgmManager { get { return _bgmManager; } } | |
| 57 | + // Exit entire application. | |
| 58 | + public void Exit() { | |
| 59 | + ExitThread(); | |
| 60 | + } | |
| 46 | 61 | |
| 47 | - /// <summary> | |
| 48 | - /// Handles SFX. | |
| 49 | - /// Should be instanciated by attaching the main window. | |
| 50 | - /// </summary> | |
| 51 | - public static SoundEffectManager soundEffectManager { get { return _soundEffectManager; } } | |
| 52 | - | |
| 62 | + private IBarHostFrame CreateFrame() { | |
| 63 | + MainFrame frame = new MainFrame(null); | |
| 64 | + frame.FormClosed += delegate(object o, FormClosedEventArgs arg) { | |
| 65 | + Exit(); | |
| 66 | + }; | |
| 67 | + | |
| 68 | + InitComplete += delegate(object o, EventArgs arg){ | |
| 69 | + frame.Show(); | |
| 70 | + }; | |
| 71 | + return frame; | |
| 72 | + } | |
| 53 | 73 | |
| 54 | - private static SoundEffectManager _soundEffectManager; | |
| 55 | - private static BGMManager _bgmManager; | |
| 56 | -*/ | |
| 57 | - #endregion | |
| 58 | - | |
| 59 | - /// <summary> | |
| 60 | - /// Initializes the framework. | |
| 61 | - /// Should be called once and only once. | |
| 62 | - /// </summary> | |
| 63 | - /// <param name="additionalPluginDirs"> | |
| 64 | - /// additional directories from which plug-ins are loaded. | |
| 65 | - /// </param> | |
| 66 | - /// <param name="owner">application's main window.</param> | |
| 67 | - /// <param name="bgmMenuItem">"BGM" sub-menu</param> | |
| 68 | - /// <param name="progressHandler"> | |
| 69 | - /// Receives initializtion progress report. Can be null. | |
| 70 | - /// </param> | |
| 71 | - public static void init( string[] args, MainFrame frame ) | |
| 72 | - { | |
| 73 | - I18n.LoadFile(Directories.AppBaseDir + "nftfw.resource.xml"); | |
| 74 | - Hashtable h_args = paresArgs(args); | |
| 74 | + private void InitDirectoriesAndAssemblies(string[] args) { | |
| 75 | + Hashtable h_args = paresArgs(args); | |
| 75 | 76 | InitializeAssemblies(); |
| 76 | 77 | Directories.Initialize(h_args); |
| 78 | + } | |
| 77 | 79 | |
| 78 | - //InformationService service = new InformationService(); | |
| 79 | - theFrame = frame; | |
| 80 | - // To avoid ReBar trouble, we need to show MainFrame | |
| 81 | - // before Splash window created. | |
| 80 | + private void InitPlugins(ProgressMonitor monitor) { | |
| 81 | + new FiberSample().Test(); | |
| 82 | 82 | |
| 83 | - using(Splash s = new Splash()) | |
| 84 | - { | |
| 85 | - s.Show(); | |
| 86 | - new FiberSample().Test(); | |
| 87 | - ProgressMonitor monitor = new ProgressMonitor(2); | |
| 88 | - monitor.SetMaximum(1,5); | |
| 89 | - monitor.OnProgress += new ProgressHandler(s.updateMessage); | |
| 90 | - Application.DoEvents(); | |
| 91 | - // load global modules; | |
| 92 | - GlobalModules.Initialize(); | |
| 93 | - // load plug-ins | |
| 94 | - Main.pluginManager.init(new string[]{Directories.PluginDir}, monitor); | |
| 95 | - } | |
| 83 | + Application.DoEvents(); | |
| 84 | + // load global modules; | |
| 85 | + GlobalModules.Initialize(); | |
| 86 | + // load plug-ins | |
| 87 | + Main.pluginManager.init(new string[] { Directories.PluginDir }, monitor); | |
| 88 | + } | |
| 96 | 89 | |
| 97 | - //new PluginListDialog().ShowDialog(); | |
| 90 | + private void OnApplicationExit(object sender, EventArgs e) { | |
| 91 | + Debug.WriteLine("## Application Exit ##"); | |
| 92 | + } | |
| 98 | 93 | |
| 99 | - | |
| 100 | -// _soundEffectManager = new SoundEffectManager(owner); | |
| 101 | -// _bgmManager = new BGMManager(bgmMenuItem); | |
| 102 | - } | |
| 94 | + private ProgressMonitor CreateProgressMonitor(ProgressHandler progressHandler) { | |
| 95 | + ProgressMonitor monitor = new ProgressMonitor(2); | |
| 96 | + monitor.SetMaximum(1, 5); | |
| 97 | + if (progressHandler != null) monitor.OnProgress += progressHandler; | |
| 98 | + return monitor; | |
| 99 | + } | |
| 103 | 100 | |
| 104 | - private static Hashtable paresArgs(string[] args) | |
| 105 | - { | |
| 106 | - Hashtable table = new Hashtable(); | |
| 107 | - for(int i=0;i<args.Length;i++) | |
| 108 | - { | |
| 109 | - string[] v = args[i].Split(new char[]{'='},2); | |
| 110 | - if(v.Length==2) | |
| 111 | - table.Add(v[0].ToUpper(),v[1]); | |
| 112 | - else | |
| 113 | - table.Add(v[0].ToUpper(),string.Empty); | |
| 114 | - } | |
| 115 | - return table; | |
| 116 | - } | |
| 101 | + private static Hashtable paresArgs(string[] args) { | |
| 102 | + Hashtable table = new Hashtable(); | |
| 103 | + for (int i = 0; i < args.Length; i++) { | |
| 104 | + string[] v = args[i].Split(new char[] { '=' }, 2); | |
| 105 | + if (v.Length == 2) | |
| 106 | + table.Add(v[0].ToUpper(), v[1]); | |
| 107 | + else | |
| 108 | + table.Add(v[0].ToUpper(), string.Empty); | |
| 109 | + } | |
| 110 | + return table; | |
| 111 | + } | |
| 117 | 112 | |
| 113 | + private void FireInitCompleteEvent() { | |
| 114 | + if (InitComplete != null) InitComplete(this, new EventArgs()); | |
| 115 | + } | |
| 116 | + | |
| 118 | 117 | private static void InitializeAssemblies() { |
| 119 | 118 | AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(AssemblyInitializer); |
| 120 | 119 | Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); |
| @@ -125,14 +124,15 @@ | ||
| 125 | 124 | |
| 126 | 125 | private static void AssemblyInitializer(object sender, AssemblyLoadEventArgs args) { |
| 127 | 126 | Assembly asm = args.LoadedAssembly; |
| 128 | - if (asm.GlobalAssemblyCache) | |
| 127 | + if (asm.GlobalAssemblyCache) | |
| 129 | 128 | return; |
| 130 | 129 | // force static constructors in types specified by InitializeOnLoad |
| 131 | 130 | object[] attributes = asm.GetCustomAttributes(typeof(InitializeOnLoadAttribute), false); |
| 132 | 131 | foreach (InitializeOnLoadAttribute attr in attributes) { |
| 133 | 132 | System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(attr.Type.TypeHandle); |
| 134 | - Debug.WriteLine("Init class on load assembly: class="+attr.Type.FullName+ ", module="+ Path.GetFileName(asm.CodeBase)); | |
| 133 | + Debug.WriteLine("Init class on load assembly: class=" + attr.Type.FullName + ", module=" + Path.GetFileName(asm.CodeBase)); | |
| 135 | 134 | } |
| 136 | 135 | } |
| 136 | + | |
| 137 | 137 | } |
| 138 | 138 | } |
| @@ -57,8 +57,7 @@ | ||
| 57 | 57 | Debug.WriteLine(", b2="+GC.GetGeneration(b2)); |
| 58 | 58 | } |
| 59 | 59 | */ |
| 60 | - Main.init(args, this); | |
| 61 | - Show(); | |
| 60 | + | |
| 62 | 61 | } |
| 63 | 62 | |
| 64 | 63 | void MainFrame_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { |
| @@ -389,7 +388,11 @@ | ||
| 389 | 388 | public void SetStatusText(string txt) { |
| 390 | 389 | messageLabel.Text = txt; |
| 391 | 390 | } |
| 392 | - #endregion | |
| 393 | 391 | |
| 394 | - } | |
| 392 | + public IToolWindowsManager ToolWindows { | |
| 393 | + get { return toolWindowsManager; } | |
| 394 | + } | |
| 395 | + #endregion | |
| 396 | + | |
| 397 | + } | |
| 395 | 398 | } |