Example #1
0
        public async ValueTask <Version> InstallAsync(Forge forge)
        {
            _logService.Info(nameof(ForgeInstallService), $"Installing forge. Version: {forge.ID} Build: {forge.Build}");

            // Just a dummy json...but required by forge installer
            string profilePath = $"{_gamePathService.RootDir}/launcher_profiles.json";

            if (!File.Exists(profilePath))
            {
                File.WriteAllText(profilePath, "{}");
            }

            // Extract forge-install-bootstrapper to disk
            // See https://github.com/bangbang93/forge-install-bootstrapper
            string bootstrapperPath  = $"{_gamePathService.RootDir}/forge-install-bootstrapper.jar";
            var    embeddedStream    = Application.GetResourceStream(new Uri(FORGE_INSTALL_BOOTSTRAPPER)).Stream;
            var    extractFileStream = File.OpenWrite(bootstrapperPath);

            embeddedStream.CopyTo(extractFileStream);
            embeddedStream.Dispose();
            extractFileStream.Dispose();

            _logService.Info(nameof(ForgeInstallService), "Install bootstrapper extracted");

            // Prepare arguments for bootstrapper
            string installerPath = $"{_gamePathService.RootDir}/{forge.FullName}-installer.jar";

            string args = $"-cp \"{bootstrapperPath};{installerPath}\" " +
                          "com.bangbang93.ForgeInstaller .";

            _logService.Debug(nameof(ForgeInstallService), $"Launching install bootstrapper, args:\n{args}");

            var startInfo = new ProcessStartInfo
            {
                FileName         = _gamePathService.JavawPath,
                WorkingDirectory = _gamePathService.RootDir,
                Arguments        = $"-cp \"{bootstrapperPath};{installerPath}\" " +
                                   "com.bangbang93.ForgeInstaller .",
                UseShellExecute        = false,
                RedirectStandardOutput = true,
            };

            try
            {
                bool isSuccessful = false;

                var process = Process.Start(startInfo);
                process.EnableRaisingEvents = true;

                process.OutputDataReceived += (_, e) =>
                {
                    string message = e.Data;

                    if (message == "true")
                    {
                        isSuccessful = true;
                    }

                    InstallProgressChanged?.Invoke(message);
                };

                process.BeginOutputReadLine();
                await process.WaitForExitAsync();

                string jsonPath = $"{_gamePathService.VersionsDir}/{forge.ID}/{forge.ID}.json";

                if (!isSuccessful)
                {
                    // Cleanup remaining json
                    Directory.Delete(jsonPath);

                    _logService.Warn(nameof(ForgeInstallService), "Installation failed");

                    return(null);
                }

                // Now we're ready to load the installed forge version
                return(_versionService.AddNew(jsonPath));
            }
            catch (Exception ex)
            {
                _logService.Warn(nameof(ForgeInstallService), $"Installation failed\n{ex.Message}");

                return(null);
            }
            finally
            {
                // Clean up
                File.Delete(bootstrapperPath);
                File.Delete(installerPath);
            }
        }
Example #2
0
 private void NotifyInstallProgressChanged(string stage, int progress)
 {
     InstallProgressChanged?.Invoke(this, new InstallProgressChangedEventArgs(Name, stage, progress));
 }
Example #3
0
 private void OnAddonInstallProgress(object sender, InstallProgressChangedEventArgs e)
 {
     InstallProgressChanged?.Invoke(this, e);
 }