Example #1
0
        private void serviceInstaller_AfterInstall(object sender, InstallEventArgs e)
        {
            //MessageBox.Show("Called AfterInstall");

            // Restore the history file from the %HOMEPATH% if it exists
            string installPath = Context.Parameters["TARGETDIR"].Replace(@"\\", @"\").Trim();
            try
            {
                // Copy the backup files back to the MCEBuddy installation directory with .old extensions (except history)
                // Convert History file to unicode while copying it back
                if (File.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "history")))
                {
                    string sourceHistory = Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "history");
                    string destinationHistory = Path.Combine(installPath, @"config\history");
                    
                    // First check if the file is a UTF16LE file (required for INI files)
                    if (Encodings.GetFileEncoding(sourceHistory) != Encoding.Unicode)
                    {
                        using (StreamReader sourceRead = new StreamReader(sourceHistory)) // Open the source file for reading
                        {
                            FileIO.TryFileDelete(destinationHistory); // Try to delete the destination file first
                            using (FileStream destinationFile = File.Create(destinationHistory)) // Create the destination file
                            {
                                using (StreamWriter destinationWrite = new StreamWriter(destinationFile, new UnicodeEncoding(false, true))) // UTF16, LE with BOM for INI files
                                {
                                    // Read everything from the file
                                    string line;
                                    while ((line = sourceRead.ReadLine()) != null)
                                    {
                                        destinationWrite.WriteLine(line); // Write to the new file
                                    }
                                    destinationWrite.Close();
                                }
                                destinationFile.Close();
                            }
                            sourceRead.Close();
                        }
                    }
                    else // We are good, just copy the file
                        File.Copy(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "history"), Path.Combine(installPath, @"config\history"), true);
                }
                if (File.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "mcebuddy.conf")))
                    File.Copy(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "mcebuddy.conf"), Path.Combine(installPath, @"config\mcebuddy.conf.old"), true);
                if (File.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "profiles.conf")))
                    File.Copy(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "profiles.conf"), Path.Combine(installPath, @"config\profiles.conf.old"), true);
                if (File.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "comskip.ini")))
                    File.Copy(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "comskip.ini"), Path.Combine(installPath, @"comskip\comskip.ini.old"), true);

                // Copy the settings from the last config file (retain user settings) (this will also take care of UTF16LE conversion for ini)
                if (File.Exists(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "mcebuddy.conf")))
                {
                    MCEBuddyConf mceConf = new MCEBuddyConf(Path.Combine(Environment.GetEnvironmentVariable("HOMEPATH"), "mcebuddy.conf")); // Read the old config file
                    mceConf.WriteSettings(Path.Combine(installPath, @"config\mcebuddy.conf")); // Write to the new installation file
                }
            }
            catch (Exception exp)
            {
                Log.WriteSystemEventLog("Failed to restore config files. Error:" + exp.ToString(), EventLogEntryType.Error);
            }

            try
            {
                // Install extras (such as filters etc)
                // Check if Haali Media Splitter is installed, else install it
                string haaliKey = "";

                if (Environment.Is64BitOperatingSystem)
                    haaliKey = @"Software\Wow6432Node\HaaliMkx";
                else
                    haaliKey = @"Software\HaaliMkx";    

                if (Registry.LocalMachine.OpenSubKey(haaliKey) == null)
                {
                    if (MessageBox.Show("MCEBuddy suggests installing Haali Media Splitter for MKV playback support.\nPress OK to Install.", "MKV Playback Support Not Found", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        RunCustomApp(Path.Combine(installPath, "extras", "MatroskaSplitter"), "/S", true); // Install Haali Media Splitter (required for MKV playback)
                }

                // WTV Playback: Uninstall Windows Update KB2670838 if the resulting file does not play in MCE/WMP - nothing to do with ffdshow
                // Check if FFDSHOW is installed, else install FFDSHOW for MPEG2/AVI support else NoRecode profiles does not work
                bool modifyReg = true;
                string ffdshowKey = "";

                if (Environment.Is64BitOperatingSystem)
                    ffdshowKey = @"Software\GNU\ffdshow64";
                else
                    ffdshowKey = @"Software\GNU\ffdshow";

                if (Registry.LocalMachine.OpenSubKey(ffdshowKey) == null) // if the key doesn't exist i.e. not installed, it returns null
                {
                    if (MessageBox.Show("MCEBuddy suggests installing FFDShow for MPEG2/AVI playback support.\nPress OK to install.", "FFDShow Not Found", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        if (RunCustomAppWithMediumPrivilege(Path.Combine(installPath, "extras", "ffdshow.exe"), " /silent", true) == true) // FFDShow needs to be installed with Medium Privileges else the installation does not work
                            Registry.LocalMachine.DeleteSubKeyTree(@"Software\GNU"); // Delete all the keys under GNU (we will only create those required later) - TODO: Can we find a clean way to do a custom install for FFDSHOw, default Silent install enables many filters not required
                        else
                        {
                            MessageBox.Show("FFDShow installation error, please install it manually from the Extra's folder in the MCEBuddy Installation directory.\nEnsure you select MPEG2 during installation", "Error installing FFDSHOW");
                            modifyReg = false;
                        }
                    }
                    else
                        modifyReg = false;
                }

                // FFDSHOW, enable the MPEG-2 splitter in the registry (whenever it's installed)
                if (modifyReg)
                {
                    Registry.LocalMachine.CreateSubKey(ffdshowKey); // Create the key if required (since the silent installation does not create one)
                    Registry.LocalMachine.OpenSubKey(ffdshowKey, true).SetValue("mpegAVI", 1, RegistryValueKind.DWord);
                    Registry.LocalMachine.OpenSubKey(ffdshowKey, true).SetValue("mpg2", 1, RegistryValueKind.DWord);
                    Registry.LocalMachine.OpenSubKey(ffdshowKey, true).SetValue("mpg1", 1, RegistryValueKind.DWord);
                }

                // Install ShowAnalyzer - last one, delayed start to avoid MSI install conflict (only 1 at a time)
                if (!Scanner.ShowAnalyzerInstalled()) // only if not installed
                    if (MessageBox.Show("MCEBuddy comes with Comskip commercial detection software built in. However some countries have better commercial detection with ShowAnalyzer.\nPress OK if you would like to install ShowAnalyzer.", "Commercial detection options", MessageBoxButtons.OKCancel) == DialogResult.OK)
                        RunCustomApp(Path.Combine(installPath, "extras", "InstallShowAnalyzerDelayed.bat"), "", false);
            }
            catch (Exception exp)
            {
                Log.WriteSystemEventLog("Failed to install custom apps. Error:" + exp.ToString(), EventLogEntryType.Error);
            }

            try
            {
                //Set the restart properties for the service
                ServiceControlManager scm = new ServiceControlManager();
                scm.SetRestartOnFailure(engineName);
            }
            catch (Exception exp)
            {
                Log.WriteSystemEventLog("Failed to set MCEBuddy service restart parameters. Error:" + exp.ToString(), EventLogEntryType.Error);
            }

            /*string installPath = Context.Parameters["TARGETDIR"].Replace(@"\\", @"\").Trim();
            CreateExposedDirectory(Path.Combine(installPath, "config"));
            CreateExposedDirectory(Path.Combine(installPath, "log"));
            CreateExposedDirectory(Path.Combine(installPath, "working"));*/ //Dont' create directories here since it create Canonical issues, create within application at runtime

            WindowsService.StartService(engineName);
        }