Exemplo n.º 1
0
        /// <summary>
        /// Attempt to update BepInEx to the latest version.
        /// </summary>
        public static void UpdateBepInEx()
        {
            if (!Folders.IsCurrentOutwardPathValid())
            {
                Console.WriteLine("Current Outward folder path not set or invalid! Cannot update BepInEx.");
                return;
            }

            // If an update check hasn't been done this launch, do one now.
            if (IsBepInExUpdated())
            {
                return;
            }

            // If the check we just did failed (no query result), we need to abort.
            if (string.IsNullOrEmpty(s_latestBepInExVersion))
            {
                return;
            }

            try
            {
                string releaseURL = $@"https://github.com/BepInEx/BepInEx/releases/latest/download/BepInEx_x64_{s_latestBepInExVersion}.zip";

                var tempFile = TemporaryFile.CreateFile();

                MefinoGUI.SetProgressMessage($"Downloading BepInEx {s_latestBepInExVersion}");

                WebClientManager.DownloadFileAsync(releaseURL, tempFile);

                while (WebClientManager.IsBusy)
                {
                    MefinoApp.SendAsyncProgress(WebClientManager.LastDownloadProgress);
                }

                MefinoGUI.SetProgressMessage($"Extracting BepInEx {s_latestBepInExVersion}");

                ZipHelper.ExtractZip(tempFile, Folders.OUTWARD_FOLDER);

                Console.WriteLine("Updated BepInEx to version '" + s_latestBepInExVersion + "'");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception downloading and installing BepInEx!");
                Console.WriteLine(ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Actually download and install the provided PackageManifest instance, which would presumably be a web manifest.
        /// </summary>
        /// <param name="manifest">The PackageManifest to install.</param>
        /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns>
        internal static bool DownloadAndInstallPackage(PackageManifest manifest)
        {
            try
            {
                MefinoGUI.SetProgressBarActive(true);

                MefinoGUI.DisableSensitiveControls();

                var dirPath = $@"{Folders.MEFINO_DISABLED_FOLDER}\{manifest.GUID}";

                if (Directory.Exists(dirPath))
                {
                    Directory.Delete(dirPath, true);
                }

                var tempFile = TemporaryFile.CreateFile();

                var zipUrl = $"{manifest.GithubURL}/releases/latest/download/{manifest.DownloadFileName}";

                Web.WebClientManager.DownloadFileAsync(zipUrl, tempFile);

                while (Web.WebClientManager.IsBusy)
                {
                    Thread.Sleep(20);
                    MefinoApp.SendAsyncProgress(Web.WebClientManager.LastDownloadProgress);
                }

                MefinoGUI.SetProgressMessage($"Installing '{manifest.GUID}' (version {manifest.version})");

                if (ZipHelper.ExtractZip(tempFile, dirPath))
                {
                    var manifestPath = $@"{dirPath}\mefino-manifest.json";

                    if (File.Exists(manifestPath))
                    {
                        File.Delete(manifestPath);
                    }

                    File.WriteAllText(manifestPath, manifest.ToJsonObject().ToString(true));

                    //Console.WriteLine($"Installed package: {manifest.GUID} {manifest.version}");

                    MefinoGUI.SetProgressBarActive(false);

                    MefinoGUI.ReEnableSensitiveControls();

                    return(true);
                }
                else
                {
                    throw new Exception("Zip extraction failed!");
                }
            }
            catch (Exception ex)
            {
                MefinoGUI.SetProgressBarActive(false);
                MefinoGUI.ReEnableSensitiveControls();

                Console.WriteLine("Exception isntalling package '" + manifest.GUID + "'");
                Console.WriteLine($"{ex.GetType()}: {ex.Message}");
                //Mefino.SendAsyncCompletion(false);
                return(false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempt to install a GUID from a GitHub web package, if such a package can be found.
        /// </summary>
        /// <param name="guid">The guid to try to install.</param>
        /// <param name="andEnable">Should the package be enabled after installing? Otherwise it will be disabled.</param>
        /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns>
        public static bool TryInstallWebPackage(string guid, bool andEnable = true)
        {
            if (OutwardHelper.IsOutwardRunning())
            {
                MessageBox.Show("You need to close Outward to do that.");
                return(false);
            }

            if (!Folders.IsCurrentOutwardPathValid())
            {
                Console.WriteLine("Outward folder is not set! Cannot install package.");
                return(false);
            }

            WebManifestManager.s_webManifests.TryGetValue(guid, out PackageManifest webManifest);

            if (webManifest == null)
            {
                if (!string.IsNullOrEmpty(guid) && TryGetInstalledPackage(guid) != null)
                {
                    Console.WriteLine("Could not find online package by GUID '" + guid + ", but an installed package exists with that GUID, enabling.");
                    TryEnablePackage(guid);
                    return(true);
                }

                Console.WriteLine("Could not find web package by GUID '" + guid + "'");
                return(false);
            }

            var existing = TryGetInstalledPackage(guid);

            if (existing != null && existing.CompareVersionAgainst(webManifest) != InstallState.Outdated)
            {
                //Console.WriteLine("Installed package is already greater or equal version, skipping install.");

                if (existing.IsDisabled)
                {
                    return(TryEnablePackage(guid));
                }

                return(true);
            }

            if (webManifest.dependencies != null && webManifest.dependencies.Length > 0)
            {
                int i     = 1;
                int count = webManifest.dependencies.Length;
                foreach (var dependency in webManifest.dependencies)
                {
                    MefinoGUI.SetProgressMessage($"Downloading dependency {i} of {count}: {dependency}");

                    if (!TryInstallWebPackage(dependency))
                    {
                        var msgResult = MessageBox.Show("Installing dependency '" + dependency + "' failed!\n\nContinue anyway?", "Dependency failed!", MessageBoxButtons.OKCancel);
                        if (msgResult == DialogResult.OK)
                        {
                            continue;
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    i++;
                }
            }

            // check that the package itself wasn't one of the dependencies.
            existing = TryGetInstalledPackage(guid);
            if (existing != null && existing.CompareVersionAgainst(webManifest) != InstallState.Outdated)
            {
                return(true);
            }

            MefinoGUI.SetProgressMessage($"Downloading package '{webManifest.GUID}'");
            MefinoApp.SendAsyncProgress(0);

            if (!DownloadAndInstallPackage(webManifest))
            {
                MessageBox.Show("Failed to download and install " + guid + "!");
                RefreshInstalledPackages();
                return(false);
            }

            RefreshInstalledPackages();

            OnPackageInstalled?.Invoke(webManifest);

            if (andEnable)
            {
                return(TryEnablePackage(guid));
            }
            else
            {
                return(true);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Attempt to unzip a .zip file from the <paramref name="zipFilePath"/> into the <paramref name="dirpath"/>.
        /// </summary>
        /// <returns><see langword="true"/> if successful, otherwise <see langword="false"/></returns>
        public static bool ExtractZip(string zipFilePath, string dirpath)
        {
            try
            {
                using (var stream = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read))
                {
                    if (stream == null || stream.Length == 0)
                    {
                        throw new IOException("The requested zip file was not found or was invalid!");
                    }

                    using (var zip = new ZipArchive(stream))
                    {
                        int    total_entry_count = zip.Entries.Count;
                        string fullName          = IOHelper.CreateDirectory(dirpath).FullName;

                        for (int i = 0; i < total_entry_count; i++)
                        {
                            ZipArchiveEntry entry    = zip.Entries[i];
                            string          fullPath = Path.GetFullPath(Path.Combine(fullName, entry.FullName));

                            if (!fullPath.StartsWith(fullName))
                            {
                                throw new IOException("Extracting Zip entry would have resulted in a file outside the specified destination directory.");
                            }

                            if (Path.GetFileName(fullPath).Length != 0)
                            {
                                IOHelper.CreateDirectory(Path.GetDirectoryName(fullPath));
                                entry.ExtractToFile(fullPath, true);
                            }
                            else
                            {
                                if (entry.Length != 0)
                                {
                                    throw new IOException("Zip entry name ends in directory separator character but contains data.");
                                }

                                IOHelper.CreateDirectory(fullPath);
                            }

                            int prog = (int)((decimal)i / total_entry_count);
                            MefinoApp.SendAsyncProgress(prog);
                        }
                    }
                }

                TemporaryFile.CleanupFile(zipFilePath);

                //Mefino.SendAsyncCompletion(true);

                return(true);
            }
            catch (Exception ex)
            {
                TemporaryFile.CleanupFile(zipFilePath);
                Console.WriteLine("Exception getting zip at '" + zipFilePath + "'");
                Console.WriteLine($"{ex.GetType()}: {ex.Message}");

                //Mefino.SendAsyncCompletion(false);
                return(false);
            }
        }