| 1 | package net.minecraft.client.main;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.google.common.base.Ticker;
|
| 5 | import com.mojang.blaze3d.TracyBootstrap;
|
| 6 | import com.mojang.blaze3d.platform.ClientShutdownWatchdog;
|
| 7 | import com.mojang.blaze3d.platform.DisplayData;
|
| 8 | import com.mojang.blaze3d.platform.NativeLibrariesBootstrap;
|
| 9 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 10 | import com.mojang.jtracy.TracyClient;
|
| 11 | import com.mojang.logging.LogUtils;
|
| 12 | import com.mojang.util.UndashedUuid;
|
| 13 | import java.io.File;
|
| 14 | import java.net.Authenticator;
|
| 15 | import java.net.InetSocketAddress;
|
| 16 | import java.net.PasswordAuthentication;
|
| 17 | import java.net.Proxy;
|
| 18 | import java.net.Proxy.Type;
|
| 19 | import java.util.List;
|
| 20 | import java.util.Optional;
|
| 21 | import java.util.OptionalInt;
|
| 22 | import java.util.UUID;
|
| 23 | import java.util.concurrent.CompletableFuture;
|
| 24 | import java.util.stream.Stream;
|
| 25 | import joptsimple.ArgumentAcceptingOptionSpec;
|
| 26 | import joptsimple.OptionParser;
|
| 27 | import joptsimple.OptionSet;
|
| 28 | import joptsimple.OptionSpec;
|
| 29 | import joptsimple.util.EnumConverter;
|
| 30 | import net.fabricmc.api.EnvType;
|
| 31 | import net.fabricmc.api.Environment;
|
| 32 | import net.minecraft.CrashReport;
|
| 33 | import net.minecraft.CrashReportCategory;
|
| 34 | import net.minecraft.DefaultUncaughtExceptionHandler;
|
| 35 | import net.minecraft.Optionull;
|
| 36 | import net.minecraft.SharedConstants;
|
| 37 | import net.minecraft.SuppressForbidden;
|
| 38 | import net.minecraft.client.ClientBootstrap;
|
| 39 | import net.minecraft.client.Minecraft;
|
| 40 | import net.minecraft.client.PreferredGraphicsApi;
|
| 41 | import net.minecraft.client.User;
|
| 42 | import net.minecraft.client.server.IntegratedServer;
|
| 43 | import net.minecraft.client.telemetry.TelemetryProperty;
|
| 44 | import net.minecraft.client.telemetry.events.GameLoadTimesEvent;
|
| 45 | import net.minecraft.core.UUIDUtil;
|
| 46 | import net.minecraft.server.Bootstrap;
|
| 47 | import net.minecraft.util.NativeModuleLister;
|
| 48 | import net.minecraft.util.Util;
|
| 49 | import net.minecraft.util.datafix.DataFixTypes;
|
| 50 | import net.minecraft.util.datafix.DataFixers;
|
| 51 | import net.minecraft.util.profiling.jfr.JvmProfiler;
|
| 52 | import org.apache.commons.lang3.StringEscapeUtils;
|
| 53 | import org.jspecify.annotations.Nullable;
|
| 54 | import org.slf4j.Logger;
|
| 55 |
|
| 56 | @Environment(EnvType.CLIENT)
|
| 57 | public class Main {
|
| 58 | public static void main(final String[] args) {
|
| 59 | try {
|
| 60 | SharedConstants.tryDetectVersion();
|
| 61 | } catch (Throwable t) {
|
| 62 | logEarlyException(t);
|
| 63 | System.exit(-7);
|
| 64 | return;
|
| 65 | }
|
| 66 |
|
| 67 | OptionParser parser;
|
| 68 | try {
|
| 69 | parser = new OptionParser();
|
| 70 | } catch (Throwable t) {
|
| 71 | logEarlyException(t);
|
| 72 | System.exit(-4);
|
| 73 | return;
|
| 74 | }
|
| 75 |
|
| 76 | parser.allowsUnrecognizedOptions();
|
| 77 | parser.accepts("demo");
|
| 78 | parser.accepts("disableMultiplayer");
|
| 79 | parser.accepts("disableChat");
|
| 80 | parser.accepts("fullscreen");
|
| 81 | parser.accepts("checkGlErrors");
|
| 82 | OptionSpec<Void> renderDebugLabelsOption = parser.accepts("renderDebugLabels");
|
| 83 | OptionSpec<Void> vulkanValidationOption = parser.accepts("vulkanValidation");
|
| 84 | OptionSpec<PreferredGraphicsApi> graphicsBackendOption = parser.accepts("graphicsBackend")
|
| 85 | .withRequiredArg()
|
| 86 | .withValuesConvertedBy(new EnumConverter<PreferredGraphicsApi>(PreferredGraphicsApi.class) {});
|
| 87 | OptionSpec<Void> jfrProfilingOption = parser.accepts("jfrProfile");
|
| 88 | OptionSpec<Void> tracyProfilingOption = parser.accepts("tracy");
|
| 89 | OptionSpec<Void> tracyNoImageOption = parser.accepts("tracyNoImages");
|
| 90 | OptionSpec<String> quickPlayPathOption = parser.accepts("quickPlayPath").withRequiredArg();
|
| 91 | OptionSpec<String> quickPlaySingleplayerOption = parser.accepts("quickPlaySingleplayer").withOptionalArg();
|
| 92 | OptionSpec<String> quickPlayMultiplayerOption = parser.accepts("quickPlayMultiplayer").withRequiredArg();
|
| 93 | OptionSpec<String> quickPlayRealmsOption = parser.accepts("quickPlayRealms").withRequiredArg();
|
| 94 | OptionSpec<File> gameDirOption = parser.accepts("gameDir").withRequiredArg().ofType(File.class).defaultsTo(new File("."));
|
| 95 | OptionSpec<File> assetsDirOption = parser.accepts("assetsDir").withRequiredArg().ofType(File.class);
|
| 96 | OptionSpec<File> resourcePackDirOption = parser.accepts("resourcePackDir").withRequiredArg().ofType(File.class);
|
| 97 | OptionSpec<String> proxyHostOption = parser.accepts("proxyHost").withRequiredArg();
|
| 98 | OptionSpec<Integer> proxyPortOption = parser.accepts("proxyPort").withRequiredArg().defaultsTo("8080").ofType(Integer.class);
|
| 99 | OptionSpec<String> proxyUserOption = parser.accepts("proxyUser").withRequiredArg();
|
| 100 | OptionSpec<String> proxyPassOption = parser.accepts("proxyPass").withRequiredArg();
|
| 101 | OptionSpec<String> usernameOption = parser.accepts("username").withRequiredArg().defaultsTo("Player" + System.currentTimeMillis() % 1000L);
|
| 102 | OptionSpec<Void> offlineDeveloperMode = parser.accepts("offlineDeveloperMode");
|
| 103 | OptionSpec<String> uuidOption = parser.accepts("uuid").withRequiredArg();
|
| 104 | OptionSpec<String> xuidOption = parser.accepts("xuid").withOptionalArg().defaultsTo("");
|
| 105 | OptionSpec<String> clientIdOption = parser.accepts("clientId").withOptionalArg().defaultsTo("");
|
| 106 | OptionSpec<String> accessTokenOption = parser.accepts("accessToken").withRequiredArg().required();
|
| 107 | OptionSpec<String> versionOption = parser.accepts("version").withRequiredArg().required();
|
| 108 | OptionSpec<Integer> widthOption = parser.accepts("width").withRequiredArg().ofType(Integer.class).defaultsTo(854);
|
| 109 | OptionSpec<Integer> heightOption = parser.accepts("height").withRequiredArg().ofType(Integer.class).defaultsTo(480);
|
| 110 | OptionSpec<Integer> fullscreenWidthOption = parser.accepts("fullscreenWidth").withRequiredArg().ofType(Integer.class);
|
| 111 | OptionSpec<Integer> fullscreenHeightOption = parser.accepts("fullscreenHeight").withRequiredArg().ofType(Integer.class);
|
| 112 | OptionSpec<String> assetIndexOption = parser.accepts("assetIndex").withRequiredArg();
|
| 113 | OptionSpec<String> versionTypeString = parser.accepts("versionType").withRequiredArg().defaultsTo("release");
|
| 114 | OptionSpec<String> nonOption = parser.nonOptions();
|
| 115 |
|
| 116 | OptionSet optionSet;
|
| 117 | try {
|
| 118 | optionSet = parser.parse(args);
|
| 119 | } catch (Throwable t) {
|
| 120 | logEarlyException(t);
|
| 121 | System.exit(-5);
|
| 122 | return;
|
| 123 | }
|
| 124 |
|
| 125 | File gameDir = parseArgument(optionSet, gameDirOption);
|
| 126 | String launchedVersion = parseArgument(optionSet, versionOption);
|
| 127 |
|
| 128 | try {
|
| 129 | NativeLibrariesBootstrap.loadLibraries();
|
| 130 | } catch (Throwable t) {
|
| 131 | CrashReport report = CrashReport.forThrowable(t, "Loading native libraries");
|
| 132 | CrashReportCategory initialization = report.addCategory("Initialization");
|
| 133 | NativeModuleLister.addCrashSection(initialization);
|
| 134 | Minecraft.fillReport(null, null, launchedVersion, null, report);
|
| 135 | Minecraft.crash(null, gameDir, report, -3);
|
| 136 | return;
|
| 137 | }
|
| 138 |
|
| 139 | String stage = "Pre-bootstrap";
|
| 140 |
|
| 141 | Logger logger;
|
| 142 | GameConfig gameConfig;
|
| 143 | try {
|
| 144 | if (optionSet.has(jfrProfilingOption)) {
|
| 145 | JvmProfiler.INSTANCE.start(net.minecraft.util.profiling.jfr.Environment.CLIENT);
|
| 146 | }
|
| 147 |
|
| 148 | if (optionSet.has(tracyProfilingOption)) {
|
| 149 | TracyBootstrap.setup();
|
| 150 | }
|
| 151 |
|
| 152 | Stopwatch totalTimePreClassLoadTimer = Stopwatch.createStarted(Ticker.systemTicker());
|
| 153 | Stopwatch preWindowPreClassLoadTimer = Stopwatch.createStarted(Ticker.systemTicker());
|
| 154 | GameLoadTimesEvent.INSTANCE.beginStep(TelemetryProperty.LOAD_TIME_TOTAL_TIME_MS, totalTimePreClassLoadTimer);
|
| 155 | GameLoadTimesEvent.INSTANCE.beginStep(TelemetryProperty.LOAD_TIME_PRE_WINDOW_MS, preWindowPreClassLoadTimer);
|
| 156 | TracyClient.reportAppInfo("Minecraft Java Edition " + SharedConstants.getCurrentVersion().name());
|
| 157 | CompletableFuture<?> dataFixerOptimization = DataFixers.optimize(DataFixTypes.TYPES_FOR_LEVEL_LIST);
|
| 158 | CrashReport.preload();
|
| 159 | logger = LogUtils.getLogger();
|
| 160 | stage = "Bootstrap";
|
| 161 | Bootstrap.bootStrap();
|
| 162 | ClientBootstrap.bootstrap();
|
| 163 | GameLoadTimesEvent.INSTANCE.setBootstrapTime(Bootstrap.bootstrapDuration.get());
|
| 164 | Bootstrap.validate();
|
| 165 | stage = "Argument parsing";
|
| 166 | List<String> leftoverArgs = optionSet.valuesOf(nonOption);
|
| 167 | if (!leftoverArgs.isEmpty()) {
|
| 168 | logger.info("Completely ignored arguments: {}", leftoverArgs);
|
| 169 | }
|
| 170 |
|
| 171 | String hostName = parseArgument(optionSet, proxyHostOption);
|
| 172 | Proxy proxy = Proxy.NO_PROXY;
|
| 173 | if (hostName != null) {
|
| 174 | try {
|
| 175 | proxy = new Proxy(Type.SOCKS, new InetSocketAddress(hostName, parseArgument(optionSet, proxyPortOption)));
|
| 176 | } catch (Exception var72) {
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | final String proxyUser = parseArgument(optionSet, proxyUserOption);
|
| 181 | final String proxyPass = parseArgument(optionSet, proxyPassOption);
|
| 182 | if (!proxy.equals(Proxy.NO_PROXY) && stringHasValue(proxyUser) && stringHasValue(proxyPass)) {
|
| 183 | Authenticator.setDefault(new Authenticator() {
|
| 184 | @Override
|
| 185 | protected PasswordAuthentication getPasswordAuthentication() {
|
| 186 | return new PasswordAuthentication(proxyUser, proxyPass.toCharArray());
|
| 187 | }
|
| 188 | });
|
| 189 | }
|
| 190 |
|
| 191 | int width = parseArgument(optionSet, widthOption);
|
| 192 | int height = parseArgument(optionSet, heightOption);
|
| 193 | OptionalInt fullscreenWidth = ofNullable(parseArgument(optionSet, fullscreenWidthOption));
|
| 194 | OptionalInt fullscreenHeight = ofNullable(parseArgument(optionSet, fullscreenHeightOption));
|
| 195 | boolean isFullscreen = optionSet.has("fullscreen");
|
| 196 | boolean isDemo = optionSet.has("demo");
|
| 197 | boolean disableMultiplayer = optionSet.has("disableMultiplayer");
|
| 198 | boolean disableChat = optionSet.has("disableChat");
|
| 199 | boolean captureTracyImages = !optionSet.has(tracyNoImageOption);
|
| 200 | boolean renderDebugLabels = optionSet.has(renderDebugLabelsOption);
|
| 201 | String versionType = parseArgument(optionSet, versionTypeString);
|
| 202 | boolean vulkanValidation = optionSet.has(vulkanValidationOption);
|
| 203 | PreferredGraphicsApi forcedGraphicsApi = parseArgument(optionSet, graphicsBackendOption);
|
| 204 | File assetsDir = optionSet.has(assetsDirOption) ? parseArgument(optionSet, assetsDirOption) : new File(gameDir, "assets/");
|
| 205 | File resourcePackDir = optionSet.has(resourcePackDirOption) ? parseArgument(optionSet, resourcePackDirOption) : new File(gameDir, "resourcepacks/");
|
| 206 | UUID uuid = hasValidUuid(uuidOption, optionSet, logger)
|
| 207 | ? UndashedUuid.fromStringLenient(uuidOption.value(optionSet))
|
| 208 | : UUIDUtil.createOfflinePlayerUUID(usernameOption.value(optionSet));
|
| 209 | String assetIndex = optionSet.has(assetIndexOption) ? assetIndexOption.value(optionSet) : null;
|
| 210 | String xuid = optionSet.valueOf(xuidOption);
|
| 211 | String clientId = optionSet.valueOf(clientIdOption);
|
| 212 | String quickPlayLogPath = parseArgument(optionSet, quickPlayPathOption);
|
| 213 | GameConfig.QuickPlayVariant quickPlayVariant = getQuickPlayVariant(optionSet, quickPlaySingleplayerOption, quickPlayMultiplayerOption, quickPlayRealmsOption);
|
| 214 | User user = new User(
|
| 215 | usernameOption.value(optionSet), uuid, accessTokenOption.value(optionSet), emptyStringToEmptyOptional(xuid), emptyStringToEmptyOptional(clientId)
|
| 216 | );
|
| 217 | gameConfig = new GameConfig(
|
| 218 | new GameConfig.UserData(user, proxy),
|
| 219 | new DisplayData(width, height, fullscreenWidth, fullscreenHeight, isFullscreen),
|
| 220 | new GameConfig.FolderData(gameDir, resourcePackDir, assetsDir, assetIndex),
|
| 221 | new GameConfig.GameData(
|
| 222 | isDemo,
|
| 223 | launchedVersion,
|
| 224 | versionType,
|
| 225 | disableMultiplayer,
|
| 226 | disableChat,
|
| 227 | captureTracyImages,
|
| 228 | vulkanValidation,
|
| 229 | renderDebugLabels,
|
| 230 | forcedGraphicsApi,
|
| 231 | optionSet.has(offlineDeveloperMode)
|
| 232 | ),
|
| 233 | new GameConfig.QuickPlayData(quickPlayLogPath, quickPlayVariant)
|
| 234 | );
|
| 235 | Util.startTimerHackThread();
|
| 236 | dataFixerOptimization.join();
|
| 237 | } catch (Throwable t) {
|
| 238 | CrashReport report = CrashReport.forThrowable(t, stage);
|
| 239 | CrashReportCategory initialization = report.addCategory("Initialization");
|
| 240 | NativeModuleLister.addCrashSection(initialization);
|
| 241 | Minecraft.fillReport(null, null, launchedVersion, null, report);
|
| 242 | Minecraft.crash(null, gameDir, report, -1);
|
| 243 | return;
|
| 244 | }
|
| 245 |
|
| 246 | Thread shutdownThread = new Thread("Client Shutdown Thread") {
|
| 247 | @Override
|
| 248 | public void run() {
|
| 249 | Minecraft instance = Minecraft.getInstance();
|
| 250 | if (instance != null) {
|
| 251 | IntegratedServer server = instance.getSingleplayerServer();
|
| 252 | if (server != null) {
|
| 253 | server.halt(true);
|
| 254 | }
|
| 255 | }
|
| 256 | }
|
| 257 | };
|
| 258 | shutdownThread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(logger));
|
| 259 | Runtime.getRuntime().addShutdownHook(shutdownThread);
|
| 260 | Minecraft minecraft = null;
|
| 261 |
|
| 262 | try {
|
| 263 | Thread.currentThread().setName("Render thread");
|
| 264 | RenderSystem.initRenderThread();
|
| 265 | minecraft = new Minecraft(gameConfig);
|
| 266 | } catch (SilentInitException e) {
|
| 267 | Util.shutdownExecutors();
|
| 268 | logger.warn("Failed to create window: ", e);
|
| 269 | return;
|
| 270 | } catch (Throwable t) {
|
| 271 | CrashReport report = CrashReport.forThrowable(t, "Initializing game");
|
| 272 | CrashReportCategory initialization = report.addCategory("Initialization");
|
| 273 | NativeModuleLister.addCrashSection(initialization);
|
| 274 | Minecraft.fillReport(minecraft, null, gameConfig.game.launchVersion, null, report);
|
| 275 | Minecraft.crash(minecraft, gameConfig.location.gameDirectory, report, -1);
|
| 276 | return;
|
| 277 | }
|
| 278 |
|
| 279 | minecraft.run();
|
| 280 |
|
| 281 | try {
|
| 282 | minecraft.exitWorldAndClose();
|
| 283 | } catch (Throwable t) {
|
| 284 | CrashReport report = CrashReport.forThrowable(t, "Game shutdown");
|
| 285 | Minecraft.fillReport(null, null, launchedVersion, null, report);
|
| 286 | Minecraft.crash(null, gameDir, report, -6);
|
| 287 | return;
|
| 288 | }
|
| 289 |
|
| 290 | ClientShutdownWatchdog.startShutdownWatchdog("post-main", true, null, gameConfig, Thread.currentThread().threadId());
|
| 291 | }
|
| 292 |
|
| 293 | @SuppressForbidden(reason = "Logging not available yet")
|
| 294 | private static void logEarlyException(final Throwable t) {
|
| 295 | t.printStackTrace();
|
| 296 | }
|
| 297 |
|
| 298 | private static GameConfig.QuickPlayVariant getQuickPlayVariant(
|
| 299 | final OptionSet optionSet,
|
| 300 | final OptionSpec<String> quickPlaySingleplayerOption,
|
| 301 | final OptionSpec<String> quickPlayMultiplayerOption,
|
| 302 | final OptionSpec<String> quickPlayRealmsOption
|
| 303 | ) {
|
| 304 | long enabledOptions = Stream.of(quickPlaySingleplayerOption, quickPlayMultiplayerOption, quickPlayRealmsOption).filter(optionSet::has).count();
|
| 305 | if (enabledOptions == 0L) {
|
| 306 | return GameConfig.QuickPlayVariant.DISABLED;
|
| 307 | } else if (enabledOptions > 1L) {
|
| 308 | throw new IllegalArgumentException("Only one quick play option can be specified");
|
| 309 | } else if (optionSet.has(quickPlaySingleplayerOption)) {
|
| 310 | String worldId = unescapeJavaArgument(parseArgument(optionSet, quickPlaySingleplayerOption));
|
| 311 | return new GameConfig.QuickPlaySinglePlayerData(worldId);
|
| 312 | } else if (optionSet.has(quickPlayMultiplayerOption)) {
|
| 313 | String serverAddress = unescapeJavaArgument(parseArgument(optionSet, quickPlayMultiplayerOption));
|
| 314 | return Optionull.mapOrDefault(serverAddress, GameConfig.QuickPlayMultiplayerData::new, GameConfig.QuickPlayVariant.DISABLED);
|
| 315 | } else if (optionSet.has(quickPlayRealmsOption)) {
|
| 316 | String realmId = unescapeJavaArgument(parseArgument(optionSet, quickPlayRealmsOption));
|
| 317 | return Optionull.mapOrDefault(realmId, GameConfig.QuickPlayRealmsData::new, GameConfig.QuickPlayVariant.DISABLED);
|
| 318 | } else {
|
| 319 | return GameConfig.QuickPlayVariant.DISABLED;
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | private static @Nullable String unescapeJavaArgument(final @Nullable String arg) {
|
| 324 | return arg == null ? null : StringEscapeUtils.unescapeJava(arg);
|
| 325 | }
|
| 326 |
|
| 327 | private static Optional<String> emptyStringToEmptyOptional(final String xuid) {
|
| 328 | return xuid.isEmpty() ? Optional.empty() : Optional.of(xuid);
|
| 329 | }
|
| 330 |
|
| 331 | private static OptionalInt ofNullable(final @Nullable Integer value) {
|
| 332 | return value != null ? OptionalInt.of(value) : OptionalInt.empty();
|
| 333 | }
|
| 334 |
|
| 335 | private static <T> @Nullable T parseArgument(final OptionSet optionSet, final OptionSpec<T> optionSpec) {
|
| 336 | try {
|
| 337 | return optionSet.valueOf(optionSpec);
|
| 338 | } catch (Throwable t) {
|
| 339 | if (optionSpec instanceof ArgumentAcceptingOptionSpec<T> options) {
|
| 340 | List<T> defaultValues = options.defaultValues();
|
| 341 | if (!defaultValues.isEmpty()) {
|
| 342 | return defaultValues.get(0);
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | throw t;
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | private static boolean stringHasValue(final @Nullable String string) {
|
| 351 | return string != null && !string.isEmpty();
|
| 352 | }
|
| 353 |
|
| 354 | private static boolean hasValidUuid(final OptionSpec<String> uuidOption, final OptionSet optionSet, final Logger logger) {
|
| 355 | return optionSet.has(uuidOption) && isUuidValid(uuidOption, optionSet, logger);
|
| 356 | }
|
| 357 |
|
| 358 | private static boolean isUuidValid(final OptionSpec<String> uuidOption, final OptionSet optionSet, final Logger logger) {
|
| 359 | try {
|
| 360 | UndashedUuid.fromStringLenient(uuidOption.value(optionSet));
|
| 361 | return true;
|
| 362 | } catch (IllegalArgumentException e) {
|
| 363 | logger.warn("Invalid UUID: '{}", uuidOption.value(optionSet));
|
| 364 | return false;
|
| 365 | }
|
| 366 | }
|
| 367 |
|
| 368 | static {
|
| 369 | System.setProperty("java.awt.headless", "true");
|
| 370 | }
|
| 371 | }
|
| 372 |
|