private void Install()
        {
            string sevenZipExecutable = Path.Combine(InstallerSettings.DownloadDirectory, "7za.exe");

            if (!File.Exists(sevenZipExecutable))
            {
                // extract 7za from executable
                File.WriteAllBytes(sevenZipExecutable, Properties.Resources._7za);
            }

            // store the patches for later
            // we'll apply them when everything is installed
            Dictionary <string, KeyValuePair <string, string> > patchList
                = new Dictionary <string, KeyValuePair <string, string> >();

            foreach (InstallerComponent component in InstallerComponents.Components)
            {
                // if it's disabled,
                // skip it
                if (!component.Enabled)
                {
                    continue;
                }

                Log($"Installing {component.Name}...");

                if (component.Patches != null)
                {
                    // store patches in a dictionary,
                    // we're gonna execute them when everything is installed
                    foreach (KeyValuePair <string, KeyValuePair <string, string> > patch in component.Patches)
                    {
                        patchList.Add(patch.Key, patch.Value);
                    }
                }

                // if it's not an archive,
                // just copy the files
                if (!component.Archive)
                {
                    foreach (KeyValuePair <string, string> cFiles in component.Files)
                    {
                        string sourceFile = Path.Combine(InstallerSettings.DownloadDirectory,
                                                         cFiles.Key);
                        string targetFile = Path.Combine(InstallerSettings.InstallDirectory,
                                                         cFiles.Value);

                        try
                        {
                            File.Copy(sourceFile, targetFile, true);
                        }
                        catch (Exception e)
                        {
                            Log($"Installing {component.Name} Failed");
                            Log(e.Message);
                            Log(e.StackTrace);
                            return;
                        }
                    }

                    continue;
                }

                // if it's an archive, extract each file
                // in the right location

                string archiveFile = Path.Combine(InstallerSettings.DownloadDirectory,
                                                  component.Urls.First().FileName);

                using (ArchiveFile archive = new ArchiveFile(archiveFile, sevenZipExecutable))
                {
                    if (component.ExtractAll)
                    {
                        bool extractSuccess = archive.ExtractAll(InstallerSettings.InstallDirectory);

                        if (!extractSuccess)
                        {
                            Log($"Installing {component.Name} Failed");

                            if (archive.Exception != null)
                            {
                                Log(archive.Exception.Message);
                                Log(archive.Exception.StackTrace);
                            }

                            return;
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair <string, string> extractFileInfo in component.Files)
                        {
                            archive.OnProgressChange += (object source, int value) =>
                            {
                                ChangeProgressBarValue(value);
                            };

                            string targetFile = Path.Combine(InstallerSettings.InstallDirectory,
                                                             extractFileInfo.Value);

                            bool extractSuccess = archive.ExtractFile(extractFileInfo.Key, targetFile);

                            if (extractSuccess)
                            {
                                continue;
                            }

                            Log($"Installing {component.Name} Failed");

                            if (archive.Exception != null)
                            {
                                Log(archive.Exception.Message);
                                Log(archive.Exception.StackTrace);
                            }

                            return;
                        }
                    }
                }
            }

            // execute the 'patches'
            // these 'patches' only allow us to replace text
            // so just read the file, replace the text and write the file
            try
            {
                foreach (KeyValuePair <string, KeyValuePair <string, string> > patch in patchList)
                {
                    string file = Path.Combine(InstallerSettings.InstallDirectory, patch.Key);

                    File.WriteAllText(file, File.ReadAllText(file)
                                      .Replace(patch.Value.Key, patch.Value.Value));
                }
            }
            catch (Exception e)
            {
                Log("Patching Failed");
                Log(e.Message);
                Log(e.StackTrace);
                return;
            }

            Log("Completed");

            // launch post-install screen
            LaunchCompleted();
        }
Exemplo n.º 2
0
        private void Install()
        {
            string sevenZipExecutable = Path.Combine(InstallerSettings.DownloadDirectory, "7za.exe");

            if (!File.Exists(sevenZipExecutable))
            {
                // extract 7za from executable
                File.WriteAllBytes(sevenZipExecutable, Properties.Resources._7za);
            }

            // store the additions for later
            // we'll apply them when everything is installed
            var additionsList = new Dictionary <string, string[]>();

            foreach (InstallerComponent component in InstallerSettings.InstallerComponents.Components)
            {
                // skip when component is disabled
                if (!component.Enabled)
                {
                    continue;
                }

                ChangeProgressBarValue(0);
                Log($"Installing {component.Name}...");

                // store additions for later
                if (component.Additions != null)
                {
                    foreach (var addition in component.Additions)
                    {
                        if (additionsList.ContainsKey(addition.Key))
                        { // append to values
                            var values = additionsList[addition.Key].ToList();
                            values.AddRange(addition.Value);
                            additionsList[addition.Key] = values.ToArray();
                        }
                        else
                        { // add to dictionary
                            additionsList.Add(addition.Key, addition.Value);
                        }
                    }
                }

                // if it's not an archive,
                // just copy the files
                if (!component.Archive)
                {
                    foreach (KeyValuePair <string, string> cFiles in component.Files)
                    {
                        string sourceFile = Path.Combine(InstallerSettings.DownloadDirectory,
                                                         cFiles.Key);
                        string targetFile = Path.Combine(InstallerSettings.InstallDirectory,
                                                         cFiles.Value);

                        try
                        {
                            File.Copy(sourceFile, targetFile, true);
                        }
                        catch (Exception e)
                        {
                            Log($"Installing {component.Name} Failed");
                            Log(e.Message);
                            Log(e.StackTrace);
                            ComponentHelper.CleanupInstallationFiles();
                            return;
                        }
                    }

                    continue;
                }

                // if it's an archive, extract each file
                // in the right location

                string filename = component.Urls.First().FileName == null?
                                  component.FallbackUrls.First().FileName:
                                  component.Urls.First().FileName;

                string archiveFile = Path.Combine(InstallerSettings.DownloadDirectory, filename);

                using (ArchiveFile archive = new ArchiveFile(archiveFile, sevenZipExecutable))
                {
                    if (component.ExtractAll)
                    {
                        bool extractSuccess = archive.ExtractAll(InstallerSettings.InstallDirectory);

                        if (!extractSuccess)
                        {
                            Log($"Installing {component.Name} Failed");

                            if (archive.Exception != null)
                            {
                                Log(archive.Exception.Message);
                                Log(archive.Exception.StackTrace);
                            }

                            ComponentHelper.CleanupInstallationFiles();
                            return;
                        }
                    }
                    else
                    {
                        foreach (KeyValuePair <string, string> extractFileInfo in component.Files)
                        {
                            archive.OnProgressChange += (object source, int value) =>
                            {
                                ChangeProgressBarValue(value);
                            };

                            string targetFile = Path.Combine(InstallerSettings.InstallDirectory,
                                                             extractFileInfo.Value);

                            bool extractSuccess = archive.ExtractFile(extractFileInfo.Key, targetFile);

                            if (!extractSuccess)
                            {
                                Log($"Installing {component.Name} Failed");

                                if (archive.Exception != null)
                                {
                                    Log(archive.Exception.Message);
                                    Log(archive.Exception.StackTrace);
                                }

                                ComponentHelper.CleanupInstallationFiles();
                                return;
                            }
                        }
                    }
                }
            }

            // apply additions
            try
            {
                foreach (var addition in additionsList)
                {
                    string file = Path.Combine(InstallerSettings.InstallDirectory, addition.Key);

                    File.AppendAllLines(file, addition.Value);
                }
            }
            catch (Exception e)
            {
                Log("Applying Additions Failed");
                Log(e.Message);
                Log(e.StackTrace);
                ComponentHelper.CleanupInstallationFiles();
                return;
            }

            Log("Completed");

            // launch post-install screen
            LaunchCompleted();
        }