|
| 1 | +# ForgeInstaller |
| 2 | + |
| 3 | +ForgeInstaller provides functionality to download and install Forge versions for Minecraft. |
| 4 | + |
| 5 | +## Initializing the Installer |
| 6 | + |
| 7 | +```csharp |
| 8 | +var path = new MinecraftPath(); |
| 9 | +var launcher = new MinecraftLauncher(path); |
| 10 | +var forgeInstaller = new ForgeInstaller(launcher); |
| 11 | +``` |
| 12 | + |
| 13 | +Optionally, you can configure the HttpClient used to access the Forge website by using `new ForgeInstaller(launcher, new HttpClient())`. |
| 14 | + |
| 15 | +## Listing Available Versions |
| 16 | + |
| 17 | +```csharp |
| 18 | +var versions = await forgeInstaller.GetForgeVersions("1.20.1"); |
| 19 | +foreach (var version in versions) |
| 20 | +{ |
| 21 | + Console.WriteLine(); |
| 22 | + Console.WriteLine("MinecraftVersion: " + version.MinecraftVersionName); |
| 23 | + Console.WriteLine("ForgeVersion: " + version.ForgeVersionName); |
| 24 | + Console.WriteLine("Time: " + version.Time); |
| 25 | + Console.WriteLine("IsLatestVersion: " + version.IsLatestVersion); |
| 26 | + Console.WriteLine("IsRecommendedVersion: " + version.IsRecommendedVersion); |
| 27 | + |
| 28 | + var installerFile = version.GetInstallerFile(); |
| 29 | + if (installerFile != null) |
| 30 | + { |
| 31 | + Console.WriteLine("Type: " + installerFile.Type); |
| 32 | + Console.WriteLine("DirectUrl: " + installerFile.DirectUrl); |
| 33 | + Console.WriteLine("AdUrl: " + installerFile.AdUrl); |
| 34 | + Console.WriteLine("MD5: " + installerFile.MD5); |
| 35 | + Console.WriteLine("SHA1: " + installerFile.SHA1); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This retrieves all available Forge versions that can be installed for the specified Minecraft version. |
| 41 | + |
| 42 | +## Installation |
| 43 | + |
| 44 | +### Installing the Best Version |
| 45 | + |
| 46 | +```csharp |
| 47 | +var installedVersionName = await forgeInstaller.Install("1.20.1", new ForgeInstallOptions()); |
| 48 | +``` |
| 49 | + |
| 50 | +This finds and installs the most appropriate Forge version available for Minecraft 1.20.1. |
| 51 | + |
| 52 | +The appropriate version is selected using the following priority rules: |
| 53 | + |
| 54 | +1. First version with the 'Recommended Version' flag |
| 55 | +2. First version with the 'Latest Version' flag |
| 56 | +3. The topmost version in the version list |
| 57 | + |
| 58 | +### Installing a Specific Version |
| 59 | + |
| 60 | +```csharp |
| 61 | +var installedVersionName = await forgeInstaller.Install("1.20.1", "47.1.0", new ForgeInstallOptions()); |
| 62 | +``` |
| 63 | + |
| 64 | +This installs Forge version 47.1.0 for Minecraft 1.20.1. |
| 65 | + |
| 66 | +### Installing the Latest Version |
| 67 | + |
| 68 | +```csharp |
| 69 | +var versions = await forgeInstaller.GetForgeVersions("1.20.1"); |
| 70 | +var latestVersion = versions.First(v => v.IsLatestVersion); |
| 71 | +var installedVersionName = await forgeInstaller.Install(latestVersion, new ForgeInstallOptions()); |
| 72 | +``` |
| 73 | + |
| 74 | +This finds and installs the latest Forge version available for Minecraft 1.20.1. |
| 75 | + |
| 76 | +## Installation Options |
| 77 | + |
| 78 | +To use features like installation progress display and cancellation, configure the appropriate values in `ForgeInstallOptions`. |
| 79 | + |
| 80 | +```csharp |
| 81 | +var installOptions = new ForgeInstallOptions |
| 82 | +{ |
| 83 | + FileProgress = new Progress<InstallerProgressChangedEventArgs>(e => |
| 84 | + Console.WriteLine($"[{e.EventType}][{e.ProgressedTasks}/{e.TotalTasks}] {e.Name}")), |
| 85 | + ByteProgress = new Progress<ByteProgress>(e => |
| 86 | + Console.WriteLine(e.ToRatio() * 100 + "%")), |
| 87 | + InstallerOutput = new Progress<string>(e => |
| 88 | + Console.WriteLine(e)), |
| 89 | + CancellationToken = CancellationToken.None, |
| 90 | + |
| 91 | + JavaPath = "java.exe", |
| 92 | + SkipIfAlreadyInstalled = true, |
| 93 | +}; |
| 94 | +var installedVersionName = await forgeInstaller.Install("1.20.1", installOptions); |
| 95 | +``` |
| 96 | + |
| 97 | +- **FileProgress** and **ByteProgress**: Report file download progress. See [Event Handling](../cmllib.core/getting-started/Handling-Events.md) for more details. |
| 98 | +- **InstallerOutput**: Reports logs output from the installer's console. |
| 99 | +- **CancellationToken**: Allows you to cancel the installation process. |
| 100 | +- **JavaPath**: Sets the path to the Java runtime used to execute the installer. The default value is `null`, which automatically determines the Java runtime path. |
| 101 | +- **SkipIfAlreadyInstalled**: When set to `true`, skips installation if the target version is already installed. The default value is `true`. |
| 102 | + |
| 103 | +## Important Note About Complete Installation |
| 104 | + |
| 105 | +`forgeInstaller.Install` does not fully install the Forge version. The version still needs additional files such as sound assets, Java runtime, and vanilla version files. Therefore, you should always call `launcher.InstallAsync` before launching the game. |
| 106 | + |
| 107 | +```csharp |
| 108 | +// Install Forge |
| 109 | +var versionName = await forgeInstaller.Install("1.20.1", new ForgeInstallOptions()); |
| 110 | + |
| 111 | +// Install remaining dependencies (sound assets, Java runtime, vanilla version) |
| 112 | +await launcher.InstallAsync(versionName); |
| 113 | + |
| 114 | +// Launch the game |
| 115 | +var process = await launcher.BuildProcessAsync(versionName, new MLaunchOption |
| 116 | +{ |
| 117 | + MaximumRamMb = 1024, |
| 118 | + Session = MSession.CreateOfflineSession("Gamer123"), |
| 119 | +}); |
| 120 | +process.Start(); |
| 121 | +``` |
| 122 | + |
| 123 | +## About Ads |
| 124 | + |
| 125 | +`ForgeInstaller` will display an ad page after a successful installation. The official Forge installer shows the following message: |
| 126 | + |
| 127 | +``` |
| 128 | +Please do not automate the download and installation of Forge. |
| 129 | +Our efforts are supported by ads from the download page. |
| 130 | +If you MUST automate this, please consider supporting the project through https://www.patreon.com/LexManos |
| 131 | +``` |
| 132 | + |
| 133 | +If you want to disable this behavior, you can modify the [ForgeInstaller source code](https://github.com/CmlLib/CmlLib.Core.Installer.Forge/blob/main/CmlLib.Core.Installer.Forge/ForgeInstaller.cs) yourself. |
0 commit comments