11package net .hypercubemc .iris_installer ;
22
3+ import net .fabricmc .installer .client .ProfileInstaller ;
34import net .fabricmc .installer .util .Reference ;
45import net .fabricmc .installer .util .Utils ;
56import org .json .JSONObject ;
67
8+ import javax .swing .*;
79import java .io .IOException ;
810import java .io .InputStream ;
911import java .net .URL ;
1012import java .nio .file .Files ;
1113import java .nio .file .Path ;
1214import java .util .*;
15+ import java .util .List ;
16+ import java .util .stream .Collectors ;
1317
1418public class VanillaLauncherIntegration {
15- public static void installToLauncher (Path vanillaGameDir , Path instanceDir , String profileName , String gameVersion , String loaderName , String loaderVersion , Icon icon ) throws IOException {
19+ public static boolean installToLauncher (Path vanillaGameDir , Path instanceDir , String profileName , String gameVersion , String loaderName , String loaderVersion , Icon icon ) throws IOException {
1620 String versionId = String .format ("%s-%s-%s" , loaderName , loaderVersion , gameVersion );
1721
18- installVersion (vanillaGameDir , gameVersion , loaderName , loaderVersion );
19- installProfile (vanillaGameDir , instanceDir , profileName , versionId , icon );
22+ ProfileInstaller .LauncherType launcherType = System .getProperty ("os.name" ).contains ("Windows" ) ? getLauncherType (vanillaGameDir ) : /* Return standalone if we aren't on Windows.*/ ProfileInstaller .LauncherType .WIN32 ;
23+ if (launcherType == null ) {
24+ // The installation has been canceled via closing the window, most likely.
25+ return false ;
26+ }
27+ installVersion (vanillaGameDir , gameVersion , loaderName , loaderVersion , launcherType );
28+ installProfile (vanillaGameDir , instanceDir , profileName , versionId , icon , launcherType );
29+ return true ;
2030 }
2131
22- public static void installVersion (Path mcDir , String gameVersion , String loaderName , String loaderVersion ) throws IOException {
23- System .out .println ("Installing " + gameVersion + " with fabric " + loaderVersion );
32+ public static void installVersion (Path mcDir , String gameVersion , String loaderName , String loaderVersion , ProfileInstaller . LauncherType launcherType ) throws IOException {
33+ System .out .println ("Installing " + gameVersion + " with fabric " + loaderVersion + " to launcher " + launcherType );
2434 String versionId = String .format ("%s-%s-%s" , loaderName , loaderVersion , gameVersion );
2535 Path versionsDir = mcDir .resolve ("versions" );
2636 Path profileDir = versionsDir .resolve (versionId );
@@ -36,8 +46,8 @@ public static void installVersion(Path mcDir, String gameVersion, String loaderN
3646 Utils .downloadFile (profileUrl , profileJson );
3747 }
3848
39- private static void installProfile (Path mcDir , Path instanceDir , String profileName , String versionId , Icon icon ) throws IOException {
40- Path launcherProfiles = mcDir .resolve ("launcher_profiles.json" );
49+ private static void installProfile (Path mcDir , Path instanceDir , String profileName , String versionId , Icon icon , ProfileInstaller . LauncherType launcherType ) throws IOException {
50+ Path launcherProfiles = mcDir .resolve (launcherType . profileJsonName );
4151 if (!Files .exists (launcherProfiles )) {
4252 System .out .println ("Could not find launcher_profiles" );
4353 return ;
@@ -129,6 +139,43 @@ private static String getProfileIcon(Icon icon) {
129139 }
130140 }
131141
142+
143+ private static ProfileInstaller .LauncherType showLauncherTypeSelection () {
144+ String [] options = new String []{Utils .BUNDLE .getString ("prompt.launcher.type.xbox" ), Utils .BUNDLE .getString ("prompt.launcher.type.win32" )};
145+ int result = JOptionPane .showOptionDialog (null , Utils .BUNDLE .getString ("prompt.launcher.type.body" ), Utils .BUNDLE .getString ("installer.title" ), JOptionPane .YES_NO_CANCEL_OPTION , JOptionPane .QUESTION_MESSAGE , null , options , options [0 ]);
146+ if (result == JOptionPane .CLOSED_OPTION ) {
147+ return null ;
148+ } else {
149+ return result == JOptionPane .YES_OPTION ? ProfileInstaller .LauncherType .MICROSOFT_STORE : ProfileInstaller .LauncherType .WIN32 ;
150+ }
151+ }
152+
153+ public static ProfileInstaller .LauncherType getLauncherType (Path vanillaGameDir ) {
154+ ProfileInstaller .LauncherType launcherType ;
155+ List <ProfileInstaller .LauncherType > types = getInstalledLauncherTypes (vanillaGameDir );
156+ if (types .size () == 0 ) {
157+ // Default to WIN32, since nothing will happen anyway
158+ System .out .println ("No launchers found, profile installation will not take place!" );
159+ launcherType = ProfileInstaller .LauncherType .WIN32 ;
160+ } else if (types .size () == 1 ) {
161+ System .out .println ("Found only one launcher (" + types .get (0 ) + "), will proceed with that!" );
162+ launcherType = types .get (0 );
163+ } else {
164+ System .out .println ("Multiple launchers found, showing selection screen!" );
165+ launcherType = showLauncherTypeSelection ();
166+ if (launcherType == null ) {
167+ System .out .println (Utils .BUNDLE .getString ("prompt.ready.install" ));
168+ launcherType = ProfileInstaller .LauncherType .WIN32 ;
169+ }
170+ }
171+
172+ return launcherType ;
173+ }
174+
175+ public static List <ProfileInstaller .LauncherType > getInstalledLauncherTypes (Path mcDir ) {
176+ return Arrays .stream (ProfileInstaller .LauncherType .values ()).filter ((launcherType ) -> Files .exists (mcDir .resolve (launcherType .profileJsonName ))).collect (Collectors .toList ());
177+ }
178+
132179 public enum Icon {
133180 IRIS ,
134181 FABRIC
0 commit comments