Commit 5bade2f045be113cde09b520b742f3d84c669b1c

Authored by Mumfrey
1 parent f6e56e35

update debug Start class to parse and handle duplicate arguments so we can work with GradleStart

debug/com/mumfrey/liteloader/debug/Start.java
1 package com.mumfrey.liteloader.debug; 1 package com.mumfrey.liteloader.debug;
  2 +
2 import java.io.File; 3 import java.io.File;
3 import java.util.ArrayList; 4 import java.util.ArrayList;
4 -import java.util.Arrays; 5 +import java.util.HashMap;
  6 +import java.util.HashSet;
5 import java.util.List; 7 import java.util.List;
  8 +import java.util.Map;
  9 +import java.util.Map.Entry;
  10 +import java.util.Set;
6 11
7 import net.minecraft.launchwrapper.Launch; 12 import net.minecraft.launchwrapper.Launch;
8 13
  14 +import com.google.common.base.Strings;
  15 +import com.google.common.collect.ImmutableSet;
9 import com.mumfrey.liteloader.launch.LiteLoaderTweaker; 16 import com.mumfrey.liteloader.launch.LiteLoaderTweaker;
10 import com.mumfrey.liteloader.util.log.LiteLoaderLogger; 17 import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
11 18
12 /** 19 /**
13 - * Wrapper class for LaunchWrapper Main class, which logs into minecraft.net first so that online shizzle can be tested 20 + * Wrapper class for LaunchWrapper Main class, which logs in using Yggdrasil first so that online shizzle can be tested
14 * 21 *
15 * @author Adam Mummery-Smith 22 * @author Adam Mummery-Smith
16 */ 23 */
17 public abstract class Start 24 public abstract class Start
18 { 25 {
19 - private static final String FML_TWEAKER_NAME = "cpw.mods.fml.common.launcher.FMLTweaker"; 26 + /**
  27 + * Number of times to retry Yggdrasil login
  28 + */
  29 + private static final int LOGIN_RETRIES = 5;
  30 +
  31 + /**
  32 + * Arguments which are allowed to have multiple occurrences
  33 + */
  34 + private static final Set<String> MULTI_VALUE_ARGS = ImmutableSet.<String>of(
  35 + "tweakClass"
  36 + );
20 37
21 /** 38 /**
22 * Entry point. 39 * Entry point.
@@ -26,51 +43,125 @@ public abstract class Start @@ -26,51 +43,125 @@ public abstract class Start
26 public static void main(String[] args) 43 public static void main(String[] args)
27 { 44 {
28 System.setProperty("mcpenv", "true"); 45 System.setProperty("mcpenv", "true");
  46 + Launch.main(Start.processArgs(args));
  47 + }
  48 +
  49 + /**
  50 + * Process the launch-time args, since we may be being launched by GradleStart we need to parse
  51 + * out any values passed in and ensure we replace them with our own
  52 + */
  53 + private static String[] processArgs(String[] args)
  54 + {
  55 + List<String> unqualifiedArgs = new ArrayList<String>();
  56 + Map<String, Set<String>> qualifiedArgs = new HashMap<String, Set<String>>();
29 57
30 - boolean fmlDetected = false;  
31 - List<String> argsList = new ArrayList<String>(Arrays.asList(args));  
32 -  
33 - // Detect the FML tweaker specified on the command line, this likely means someone has pulled us  
34 - // into a Forge MCP workspace  
35 - for (String arg : argsList) fmlDetected |= FML_TWEAKER_NAME.equals(arg); 58 + Start.parseArgs(args, unqualifiedArgs, qualifiedArgs);
  59 + Start.addRequiredArgs(args, unqualifiedArgs, qualifiedArgs);
  60 + args = Start.combineArgs(args, unqualifiedArgs, qualifiedArgs);
36 61
37 - if (fmlDetected) 62 + return args;
  63 + }
  64 +
  65 + /**
  66 + * Read the args from the command line into the qualified and unqualified collections
  67 + */
  68 + private static void parseArgs(String[] args, List<String> unqualifiedArgs, Map<String, Set<String>> qualifiedArgs)
  69 + {
  70 + String qualifier = null;
  71 + for (String arg : args)
38 { 72 {
39 - argsList.clear();  
40 - argsList.add("--tweakClass");argsList.add(FML_TWEAKER_NAME); 73 + boolean isQualifier = arg.startsWith("-");
  74 +
  75 + if (isQualifier)
  76 + {
  77 + if (qualifier != null) unqualifiedArgs.add(qualifier);
  78 + qualifier = arg;
  79 + }
  80 + else if (qualifier != null)
  81 + {
  82 + Start.addArg(qualifiedArgs, qualifier, arg);
  83 + qualifier = null;
  84 + }
  85 + else
  86 + {
  87 + unqualifiedArgs.add(arg);
  88 + }
41 } 89 }
42 90
43 - String usernameFromCmdLine = null;  
44 - String passwordFromCmdLine = null;  
45 -  
46 - if (argsList.size() > 0 && !argsList.get(0).startsWith("-"))  
47 - {  
48 - usernameFromCmdLine = argsList.remove(0); 91 + if (qualifier != null) unqualifiedArgs.add(qualifier);
  92 + }
49 93
50 - if (argsList.size() > 0 && !argsList.get(0).startsWith("-"))  
51 - passwordFromCmdLine = argsList.remove(0);  
52 - } 94 + private static void addRequiredArgs(String[] args, List<String> unqualifiedArgs, Map<String, Set<String>> qualifiedArgs)
  95 + {
  96 + LoginManager loginManager = Start.doLogin(qualifiedArgs);
53 97
  98 + File gameDir = new File(System.getProperty("user.dir"));
  99 + File assetsDir = new File(gameDir, "assets");
  100 +
  101 + Start.addArg(qualifiedArgs, "--tweakClass", LiteLoaderTweaker.class.getName());
  102 + Start.addArg(qualifiedArgs, "--username", loginManager.getProfileName());
  103 + Start.addArg(qualifiedArgs, "--uuid", loginManager.getUUID());
  104 + Start.addArg(qualifiedArgs, "--accessToken", loginManager.getAuthenticatedToken());
  105 + Start.addArg(qualifiedArgs, "--userType", loginManager.getUserType());
  106 + Start.addArg(qualifiedArgs, "--userProperties", loginManager.getUserProperties());
  107 + Start.addArg(qualifiedArgs, "--version", "mcp");
  108 + Start.addArg(qualifiedArgs, "--gameDir", gameDir.getAbsolutePath());
  109 + Start.addArg(qualifiedArgs, "--assetIndex", LiteLoaderTweaker.VERSION);
  110 + Start.addArg(qualifiedArgs, "--assetsDir", assetsDir.getAbsolutePath());
  111 + }
  112 +
  113 + private static LoginManager doLogin(Map<String, Set<String>> qualifiedArgs)
  114 + {
54 File loginJson = new File(new File(System.getProperty("user.dir")), ".auth.json"); 115 File loginJson = new File(new File(System.getProperty("user.dir")), ".auth.json");
55 LoginManager loginManager = new LoginManager(loginJson); 116 LoginManager loginManager = new LoginManager(loginJson);
56 - loginManager.login(usernameFromCmdLine, passwordFromCmdLine, 5);  
57 - 117 +
  118 + String usernameFromCmdLine = Start.getArg(qualifiedArgs, "--username");
  119 + String passwordFromCmdLine = Start.getArg(qualifiedArgs, "--password");
  120 +
  121 + loginManager.login(usernameFromCmdLine, passwordFromCmdLine, Start.LOGIN_RETRIES);
  122 +
58 LiteLoaderLogger.info("Launching game as %s", loginManager.getProfileName()); 123 LiteLoaderLogger.info("Launching game as %s", loginManager.getProfileName());
59 124
60 - File gameDir = new File(System.getProperty("user.dir"));  
61 - File assetsDir = new File(gameDir, "assets"); 125 + return loginManager;
  126 + }
  127 +
  128 + private static void addArg(Map<String, Set<String>> qualifiedArgs, String qualifier, String arg)
  129 + {
  130 + Set<String> args = qualifiedArgs.get(qualifier);
  131 +
  132 + if (args == null)
  133 + {
  134 + args = new HashSet<String>();
  135 + qualifiedArgs.put(qualifier, args);
  136 + }
  137 +
  138 + if (!Start.MULTI_VALUE_ARGS.contains(qualifier))
  139 + args.clear();
  140 +
  141 + args.add(arg);
  142 + }
62 143
63 - argsList.add("--tweakClass"); argsList.add(LiteLoaderTweaker.class.getName());  
64 - argsList.add("--username"); argsList.add(loginManager.getProfileName());  
65 - argsList.add("--uuid"); argsList.add(loginManager.getUUID());  
66 - argsList.add("--accessToken"); argsList.add(loginManager.getAuthenticatedToken());  
67 - argsList.add("--userType"); argsList.add(loginManager.getUserType());  
68 - argsList.add("--userProperties"); argsList.add(loginManager.getUserProperties());  
69 - argsList.add("--version"); argsList.add("mcp");  
70 - argsList.add("--gameDir"); argsList.add(gameDir.getAbsolutePath());  
71 - argsList.add("--assetIndex"); argsList.add(LiteLoaderTweaker.VERSION);  
72 - argsList.add("--assetsDir"); argsList.add(assetsDir.getAbsolutePath()); 144 + private static String getArg(Map<String, Set<String>> qualifiedArgs, String arg)
  145 + {
  146 + if (qualifiedArgs.containsKey(arg))
  147 + {
  148 + return qualifiedArgs.get(arg).iterator().next();
  149 + }
  150 +
  151 + return null;
  152 + }
  153 +
  154 + private static String[] combineArgs(String[] args, List<String> unqualifiedArgs, Map<String, Set<String>> qualifiedArgs)
  155 + {
  156 + for (Entry<String, Set<String>> qualifiedArg : qualifiedArgs.entrySet())
  157 + {
  158 + for (String argValue : qualifiedArg.getValue())
  159 + {
  160 + unqualifiedArgs.add(qualifiedArg.getKey());
  161 + if (!Strings.isNullOrEmpty(argValue)) unqualifiedArgs.add(argValue);
  162 + }
  163 + }
73 164
74 - Launch.main(argsList.toArray(args)); 165 + return unqualifiedArgs.toArray(args);
75 } 166 }
76 } 167 }