Exemplo n.º 1
0
        public void DownloadModule(Module module)
        {
            try
            {
                string userModDir      = Path.Combine(Program.BaseDirectoryExternal, "user_mods");
                var    installedModule = GetInstalledModule(module);
                //If module is installed remove it
                if (installedModule != null)
                {
                    foreach (var file in installedModule.InstalledFiles)
                    {
                        try
                        {
                            if (file.EndsWith("\\"))
                            {
                                Directory.Delete(Path.Combine(userModDir, file), true);
                            }
                            else
                            {
                                File.Delete(Path.Combine(userModDir, file));
                            }
                        }
                        catch { }
                    }

                    InstalledItems.Remove(installedModule);
                    installedModule = null;
                }
                switch (module.ModType)
                {
                case ModuleType.hmod:
                {
                    string fileLocation = Path.Combine(userModDir, module.Path.Substring(module.Path.LastIndexOf('/') + 1));

                    if (!ProgressBarForm.DownloadFile(module.Path, fileLocation))
                    {
                        throw new Exception("Cannot download file: " + module.Path);
                    }
                    installedModule = module.CreateInstalledItem();
                    installedModule.InstalledFiles.Add(module.Path.Substring(module.Path.LastIndexOf('/') + 1));
                    InstalledItems.Add(installedModule);
                }
                break;

                case ModuleType.compressedFile:
                    SevenZipExtractor.SetLibraryPath(Path.Combine(Program.BaseDirectoryInternal, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll"));
                    var tempFileName = Path.GetTempFileName();
                    if (!ProgressBarForm.DownloadFile(module.Path, tempFileName))
                    {
                        throw new Exception("Cannot download file: " + module.Path);
                    }
                    using (var szExtractor = new SevenZipExtractor(tempFileName))
                    {
                        installedModule = module.CreateInstalledItem();
                        var data = szExtractor.ArchiveFileData;
                        foreach (var file in data)
                        {
                            int index = file.FileName.IndexOf('\\');
                            if (index != -1)
                            {
                                var folder = file.FileName.Substring(0, index + 1);
                                if (!installedModule.InstalledFiles.Contains(folder))
                                {
                                    installedModule.InstalledFiles.Add(folder);
                                    var localFolder = Path.Combine(userModDir, folder);
                                    if (Directory.Exists(localFolder))
                                    {
                                        Directory.Delete(localFolder, true);
                                    }
                                }
                            }
                            else if (!file.IsDirectory)
                            {
                                installedModule.InstalledFiles.Add(file.FileName);
                            }
                        }
                        szExtractor.ExtractArchive(userModDir);
                        InstalledItems.Add(installedModule);
                    }
                    File.Delete(tempFileName);
                    break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Critical error: " + ex.Message + ex.StackTrace, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            SaveConfig();
        }