private void MainWindowLoad(object sender, EventArgs e) { CheckVersion(); // Location can not be changed in constructor, has to be done here Location = Settings.WindowLocation; Size = Settings.WindowSize; CmdTable = CommandTable.Deserialize(Settings.DisableInternalCommands); if (CmdTable == null) { MessageBox.Show(this, Resources.MCEController_commands_read_error, Resources.App_FullName); _notifyIcon.Visible = false; Opacity = 100; } else { AddLogEntry("MCEC: " + CmdTable.NumCommands + " commands available."); Opacity = (double)Settings.Opacity / 100; if (Settings.HideOnStartup) { Opacity = 0; Win32.PostMessage(Handle, (UInt32)WM.SYSCOMMAND, (UInt32)SC.CLOSE, 0); } } if (_cmdWindow == null) { _cmdWindow = new CommandWindow(); } //_cmdWindow.Visible = Settings.ShowCommandWindow; //var t = new System.Timers.Timer() { // AutoReset = false, // Interval = 2000 //}; //t.Elapsed += (sender, args) => Start(); //AddLogEntry("Starting services..."); //t.Start(); Start(); }
public static CommandTable Deserialize(bool DisableInternalCommands) { CommandTable cmds = null; CommandTable userCmds = null; if (!DisableInternalCommands) { // Load the built-in commands from an assembly resource try { var serializer = new XmlSerializer(typeof(CommandTable)); XmlReader reader = new XmlTextReader( Assembly.GetExecutingAssembly() .GetManifestResourceStream("MCEControl.Resources.MCEControl.commands")); cmds = (CommandTable)serializer.Deserialize(reader); foreach (var cmd in cmds.List) { if (cmds._hashTable.ContainsKey(cmd.Key.ToUpper())) { cmds._hashTable.Remove(cmd.Key.ToUpper()); } cmds._hashTable.Add(cmd.Key.ToUpper(), cmd); } } catch (Exception ex) { MessageBox.Show(String.Format("No commands loaded. Error parsing built-in commands. {0}", ex.Message)); MainWindow.AddLogEntry( String.Format("MCEC: No commands loaded. Error parsing built-in commands. {0}", ex.Message)); Util.DumpException(ex); return(null); } // Populate default VK_ codes foreach (VirtualKeyCode vk in Enum.GetValues(typeof(VirtualKeyCode))) { string s; if (vk > VirtualKeyCode.HELP && vk < VirtualKeyCode.LWIN) { s = vk.ToString(); // already have VK_ } else { s = "VK_" + vk.ToString(); } var cmd = new SendInputCommand(s, false, false, false, false); if (!cmds._hashTable.ContainsKey(s)) { cmds._hashTable.Add(s, cmd); } } } else { cmds = new CommandTable(); } // Load any over-rides from a text file FileStream fs = null; try { var serializer = new XmlSerializer(typeof(CommandTable)); // A FileStream is needed to read the XML document. fs = new FileStream("MCEControl.commands", FileMode.Open, FileAccess.Read); XmlReader reader = new XmlTextReader(fs); userCmds = (CommandTable)serializer.Deserialize(reader); foreach (var cmd in userCmds.List) { if (cmds._hashTable.ContainsKey(cmd.Key.ToUpper())) { cmds._hashTable.Remove(cmd.Key.ToUpper()); } cmds._hashTable.Add(cmd.Key.ToUpper(), cmd); } MainWindow.AddLogEntry(String.Format("MCEC: User defined commands loaded.")); } catch (FileNotFoundException ex) { MainWindow.AddLogEntry("MCEC: No user defined commands loaded; MCEControl.commands was not found."); Util.DumpException(ex); } catch (InvalidOperationException ex) { MainWindow.AddLogEntry( String.Format("MCEC: No commands loaded. Error parsing MCEControl.commands file. {0} {1}", ex.Message, ex.InnerException.Message)); Util.DumpException(ex); } catch (Exception ex) { MessageBox.Show(String.Format("No commands loaded. Error parsing MCEControl.commands file. {0}", ex.Message)); MainWindow.AddLogEntry(String.Format("MCEC: No commands loaded. Error parsing MCEControl.commands file. {0}", ex.Message)); Util.DumpException(ex); } finally { if (fs != null) { fs.Close(); } } return(cmds); }