Skip to content

Commit

Permalink
feat: add client support
Browse files Browse the repository at this point in the history
  • Loading branch information
xIGBClutchIx committed Dec 11, 2020
1 parent e5c296f commit 4bd8d77
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 50 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ dependencies {
implementation "org.apache.logging.log4j:log4j-api:2.11.2"
implementation "org.apache.logging.log4j:log4j-core:2.11.2"
compileOnly "net.md-5:SpecialSource:1.8.6"
compileOnly "cpw.mods:modlauncher:6.1.1"
compileOnly "org.spongepowered:mixin:0.8"
compileOnly "cpw.mods:modlauncher:8.0.6"
compileOnly "org.spongepowered:mixin:0.8.2"
}

group = theGroup
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
theName=Stream
theVersion=1.0.1
theVersion=1.0.2
theGroup=systems.conduit
systemProp.file.encoding=utf-8
27 changes: 17 additions & 10 deletions src/main/java/systems/conduit/stream/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public class Constants {
public static final Path CONDUIT_CACHE = Paths.get("caches", "conduit");
public static Path MINECRAFT_PATH = Paths.get(".minecraft");

public static final Path LIBRARIES_PATH = Paths.get(".libs");
// TODO: Detect side and use .libs for server
public static final Path LIBRARIES_PATH = Paths.get("libraries");

public static Path VERSION_JSON_PATH;
public static Path SERVER_JAR_PATH;
public static Path SERVER_MAPPED_JAR_PATH;
public static Path SERVER_MAPPINGS_PATH;
public static Path SERVER_MAPPINGS_CONVERTED_PATH;
public static Path JAR_PATH;
public static Path MAPPED_JAR_PATH;
public static Path MAPPINGS_PATH;
public static Path MAPPINGS_CONVERTED_PATH;

public static final Path MIXINS_PATH = Paths.get(".mixins");
public static Path CONDUIT_MIXIN_PATH;
Expand All @@ -50,21 +51,27 @@ public class Constants {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11";

public static final String MAIN_SERVER_FILE = "net.minecraft.server.Main";
public static final String MAIN_CLIENT_FILE = "net.minecraft.client.main.Main";
public static final String MAIN_CONDUIT_FILE = "systems.conduit.main.Conduit";

public static void setMinecraftPaths(Path basePath, String minecraftVersion) {
public static void setMinecraftPaths(Side side, Path basePath, String minecraftVersion) {
MINECRAFT_VERSION = minecraftVersion;
if (basePath != null) MINECRAFT_PATH = basePath.resolve(MINECRAFT_PATH);
VERSION_JSON_PATH = MINECRAFT_PATH.resolve(MINECRAFT_VERSION + ".json");
SERVER_JAR_PATH = MINECRAFT_PATH.resolve("server-" + MINECRAFT_VERSION + ".jar");
SERVER_MAPPED_JAR_PATH = MINECRAFT_PATH.resolve("server-" + MINECRAFT_VERSION + "-remapped.jar");
SERVER_MAPPINGS_PATH = MINECRAFT_PATH.resolve("server-" + MINECRAFT_VERSION + "-mappings.txt");
SERVER_MAPPINGS_CONVERTED_PATH = MINECRAFT_PATH.resolve("server-" + MINECRAFT_VERSION + "-mappings-converted.txt");
JAR_PATH = MINECRAFT_PATH.resolve(side.name().toLowerCase() + "-" + MINECRAFT_VERSION + ".jar");
MAPPED_JAR_PATH = MINECRAFT_PATH.resolve(side.name().toLowerCase() + "-" + MINECRAFT_VERSION + "-remapped.jar");
MAPPINGS_PATH = MINECRAFT_PATH.resolve(side.name().toLowerCase() + "-" + MINECRAFT_VERSION + "-mappings.txt");
MAPPINGS_CONVERTED_PATH = MINECRAFT_PATH.resolve(side.name().toLowerCase() + "-" + MINECRAFT_VERSION + "-mappings-converted.txt");
}

public static void setConduitPaths(String conduitVersion) {
CONDUIT_VERSION = conduitVersion;
CONDUIT_MIXIN_PATH = MIXINS_PATH.resolve("Conduit-" + CONDUIT_VERSION + ".jar");
CONDUIT_DOWNLOAD_PATH = CONDUIT_REPO + "systems/conduit/Conduit/" + CONDUIT_VERSION + "/Conduit-" + CONDUIT_VERSION + ".jar";
}

public enum Side {
CLIENT,
SERVER
}
}
53 changes: 28 additions & 25 deletions src/main/java/systems/conduit/stream/SharedLaunch.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public static void downloadDefaultLibraries(Path basePath, Callback<File> callba
LibraryProcessor.downloadLibrary("default libraries", basePath, defaults.getLibs(), callback);
}

public static void setupMinecraft(Path basePath, String version, Callback<File> callback) {
public static void setupMinecraft(Constants.Side side, Path basePath, String version, Callback<File> callback) {
if (version == null) {
Logger.fatal("Can not find minecraft version");
System.exit(0);
}
// Set correct paths
Constants.setMinecraftPaths(basePath, version);
Constants.setMinecraftPaths(side, basePath, version);
// Make sure we have the correct directories
if (!Constants.MINECRAFT_PATH.toFile().exists() && !Constants.MINECRAFT_PATH.toFile().mkdirs()) {
Logger.fatal("Failed to make minecraft directory");
Expand All @@ -71,7 +71,7 @@ public static void setupMinecraft(Path basePath, String version, Callback<File>
try (InputStreamReader manifestReader = new InputStreamReader(new URL(Constants.VERSION_MANIFEST_ENDPOINT).openStream())) {
MinecraftVersionManifest manifest = new Gson().fromJson(manifestReader, MinecraftVersionManifest.class);
Optional<MinecraftVersionManifestType> versionInfo = getVersion(manifest, Constants.MINECRAFT_VERSION);
// Read version json and get server info
// Read version json and get info
if (versionInfo.isPresent() && versionInfo.get().getUrl() != null && !versionInfo.get().getUrl().isEmpty()) {
try {
Logger.info("Downloading version json");
Expand Down Expand Up @@ -105,7 +105,7 @@ public static void setupMinecraft(Path basePath, String version, Callback<File>
// Download libraries
List<JsonLibraryInfo> minecraftLibraries = new ArrayList<>();
for (MinecraftLibrary minecraftLibrary : minecraftVersion.getLibraries()) {
// Should not need mac only for a server. I think?
// Should not need mac only. I think?
if (!minecraftLibrary.isMac()) {
String[] minecraftLib = minecraftLibrary.getName().split(":");
minecraftLibraries.add(new JsonLibraryInfo("maven", minecraftLib[0], minecraftLib[1], minecraftLib[2], Constants.MINECRAFT_REPO));
Expand All @@ -114,47 +114,49 @@ public static void setupMinecraft(Path basePath, String version, Callback<File>
// Download all the Minecraft libraries
LibraryProcessor.downloadLibrary("Minecraft libraries", basePath, minecraftLibraries, callback);
// Download Minecraft and patch if we don't have the file
if (!Constants.SERVER_MAPPED_JAR_PATH.toFile().exists()) {
// Download server
if (!minecraftVersion.getDownloads().getServer().getUrl().isEmpty()) {
if (!Constants.MAPPED_JAR_PATH.toFile().exists()) {
// Download jar
String jarDownload = side == Constants.Side.SERVER ? minecraftVersion.getDownloads().getServer().getUrl() : minecraftVersion.getDownloads().getClient().getUrl();
if (!jarDownload.isEmpty()) {
try {
Logger.info("Downloading Minecraft server (" + Constants.MINECRAFT_VERSION + ")");
downloadFile(new URL(minecraftVersion.getDownloads().getServer().getUrl()), Constants.SERVER_JAR_PATH.toFile());
Logger.info("Downloading Minecraft " + side.name().toLowerCase() + " (" + Constants.MINECRAFT_VERSION + ")");
downloadFile(new URL(jarDownload), Constants.JAR_PATH.toFile());
} catch (IOException e) {
Logger.exception("Error creating server url", e);
Logger.exception("Error creating " + side.name().toLowerCase() + " url", e);
System.exit(0);
}
} else {
Logger.fatal("Error reading Minecraft server url");
Logger.fatal("Error reading Minecraft " + side.name().toLowerCase() + " url");
System.exit(0);
}
// Cleanup Minecraft
Logger.info("Cleaning up Minecraft");
deleteMinecraftTrash(Constants.SERVER_JAR_PATH.toFile());
//deleteMinecraftTrash(Constants.JAR_PATH.toFile());
Logger.info("Cleaned up Minecraft");
// Download server mappings
if (!minecraftVersion.getDownloads().getServerMappings().getUrl().isEmpty()) {
// Download mappings
String mappingsDownload = side == Constants.Side.SERVER ? minecraftVersion.getDownloads().getServerMappings().getUrl() : minecraftVersion.getDownloads().getClientMappings().getUrl();
if (!mappingsDownload.isEmpty()) {
try {
Logger.info("Downloading server mappings");
downloadFile(new URL(minecraftVersion.getDownloads().getServerMappings().getUrl()), Constants.SERVER_MAPPINGS_PATH.toFile());
Logger.info("Downloading " + side.name().toLowerCase() + " mappings");
downloadFile(new URL(mappingsDownload), Constants.MAPPINGS_PATH.toFile());
} catch (IOException e) {
Logger.exception("Error creating server mappings url", e);
Logger.exception("Error creating " + side.name().toLowerCase() + " mappings url", e);
System.exit(0);
}
} else {
Logger.fatal("Error reading Minecraft server mappings url");
Logger.fatal("Error reading Minecraft " + side.name().toLowerCase() + " mappings url");
System.exit(0);
}
// Convert Minecraft mappings
Logger.info("Converting Minecraft mappings");
new Mojang2Tsrg(Constants.SERVER_MAPPINGS_PATH, Constants.SERVER_MAPPINGS_CONVERTED_PATH);
new Mojang2Tsrg(Constants.MAPPINGS_PATH, Constants.MAPPINGS_CONVERTED_PATH);
// Remapping Minecraft
Logger.info("Remapping Minecraft (This might take a bit)");
Logger.info("Remapping Minecraft " + side.name().toLowerCase() + " (This might take a bit)");
ClassLoader classLoader = new URLClassLoader(specialSourcePaths.toArray(new URL[]{}), ClassLoader.getSystemClassLoader());
String[] specialSourceArgs = Stream.of(
"--in-jar", Constants.SERVER_JAR_PATH.toFile().getAbsolutePath(),
"--out-jar", Constants.SERVER_MAPPED_JAR_PATH.toFile().getAbsolutePath(),
"--srg-in", Constants.SERVER_MAPPINGS_CONVERTED_PATH.toFile().getAbsolutePath(),
"--in-jar", Constants.JAR_PATH.toFile().getAbsolutePath(),
"--out-jar", Constants.MAPPED_JAR_PATH.toFile().getAbsolutePath(),
"--srg-in", Constants.MAPPINGS_CONVERTED_PATH.toFile().getAbsolutePath(),
"--quiet"
).toArray(String[]::new);
// Run remapping Minecraft
Expand All @@ -164,10 +166,11 @@ public static void setupMinecraft(Path basePath, String version, Callback<File>
method.invoke(null, (Object) specialSourceArgs);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
Logger.exception("Error remapping Minecraft", e);
e.printStackTrace();
System.exit(0);
}
Constants.SERVER_JAR_PATH.toFile().delete();
Constants.SERVER_MAPPINGS_CONVERTED_PATH.toFile().delete();
Constants.JAR_PATH.toFile().delete();
Constants.MAPPINGS_CONVERTED_PATH.toFile().delete();
Logger.info("Remapped Minecraft");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class StreamGradlePlugin implements Plugin<Project> {

@Override
public void apply(Project project) {
// TODO: ADD CONFIG
Constants.Side side = Constants.Side.SERVER;
// Register Conduit info
ConduitExtension extension = project.getExtensions().create("conduit", ConduitExtension.class, project);
// Project Defaults
Expand Down Expand Up @@ -63,9 +65,9 @@ public void apply(Project project) {
// Make sure minecraft is present. It always should be if done right.
if (extension.minecraft != null) {
// Download/load minecraft libraries and download and remap minecraft if need to
SharedLaunch.setupMinecraft(cacheFolder, extension.minecraft, registerDependency);
SharedLaunch.setupMinecraft(side, cacheFolder, extension.minecraft, registerDependency);
// Load minecraft
registerDependency.callback(Constants.SERVER_MAPPED_JAR_PATH.toFile());
registerDependency.callback(Constants.MAPPED_JAR_PATH.toFile());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@

public class MinecraftDownload {

@Getter private MinecraftVersionInfo client = new MinecraftVersionInfo();
@Getter private MinecraftVersionInfo server = new MinecraftVersionInfo();
private MinecraftVersionInfo client_mappings = new MinecraftVersionInfo();
private MinecraftVersionInfo server_mappings = new MinecraftVersionInfo();

public MinecraftVersionInfo getClientMappings() {
return client_mappings;
}

public MinecraftVersionInfo getServerMappings() {
return server_mappings;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/systems/conduit/stream/launcher/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ protected static void addClassPath(File f) {
}
} catch (Throwable e) {
System.out.println("Add to classpath error!");
System.out.println(e.getMessage());
System.exit(0);
e.printStackTrace();
//System.exit(0);
}
}
}
13 changes: 8 additions & 5 deletions src/main/java/systems/conduit/stream/launcher/LauncherStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.google.gson.GsonBuilder;
import cpw.mods.modlauncher.Launcher;
import systems.conduit.stream.*;
import systems.conduit.stream.json.download.JsonLibraries;
import systems.conduit.stream.json.JsonStream;
import systems.conduit.stream.json.download.JsonLibraries;

import java.io.*;
import java.net.URL;
Expand All @@ -26,6 +26,9 @@ public class LauncherStart {
public static final List<Path> PATHS = new ArrayList<>();

public static void main(String... args) {
// TODO MAKE CONFIG
Constants.Side side = Constants.Side.SERVER;
// Defaults
System.setProperty("http.agent", Constants.USER_AGENT);
Logger.shouldUseLogger = true;
System.out.println("Starting launcher...");
Expand Down Expand Up @@ -59,19 +62,19 @@ public static void main(String... args) {
Logger.info("Setting up Minecraft");
if (!Constants.DEBUG) {
if (stream != null) {
SharedLaunch.setupMinecraft(null, stream.getMinecraft().getVersion(), registerJar);
SharedLaunch.setupMinecraft(side, null, stream.getMinecraft().getVersion(), registerJar);
} else {
Logger.fatal("Error parsing stream json!");
System.exit(0);
}
} else {
SharedLaunch.setupMinecraft(null, Constants.MINECRAFT_VERSION, registerJar);
SharedLaunch.setupMinecraft(side, null, Constants.MINECRAFT_VERSION, registerJar);
}
Logger.info("Set up Minecraft");
// Load minecraft
if (!Constants.DEBUG) {
Logger.info("Loading Minecraft remapped");
LauncherStart.PATHS.add(Constants.SERVER_MAPPED_JAR_PATH);
LauncherStart.PATHS.add(Constants.MAPPED_JAR_PATH);
Logger.info("Loaded Minecraft remapped");
}
// Create the mixins folder
Expand Down Expand Up @@ -135,7 +138,7 @@ public static void main(String... args) {
}
// Start modlauncher
Logger.info("Starting modlauncher...");
Launcher.main(Stream.concat(Stream.of("--launchTarget", "minecraft-server"), Arrays.stream(args)).toArray(String[]::new));
Launcher.main(Stream.concat(Stream.of("--launchTarget", "minecraft-" + side.name().toLowerCase()), Arrays.stream(args)).toArray(String[]::new));
}

private static List<String> findMixinEntry(JarFile file) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package systems.conduit.stream.launcher.services;

import cpw.mods.modlauncher.api.ILaunchHandlerService;
import cpw.mods.modlauncher.api.ITransformingClassLoader;
import cpw.mods.modlauncher.api.ITransformingClassLoaderBuilder;
import org.spongepowered.asm.mixin.Mixins;
import systems.conduit.stream.Constants;
import systems.conduit.stream.Logger;
import systems.conduit.stream.launcher.LauncherStart;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.util.concurrent.Callable;

public class ClientLaunchHandlerService implements ILaunchHandlerService {

@Override
public void configureTransformationClassLoader(final ITransformingClassLoaderBuilder builder) {
// Add transformation paths
LauncherStart.PATHS.forEach(builder::addTransformationPath);
// Load Minecraft and Conduit jars if in debug
if (Constants.DEBUG) {
Path minecraft = getLoadedJar(Constants.MAIN_CLIENT_FILE);
if (minecraft != null) {
Logger.info("Transforming Minecraft remapped");
builder.addTransformationPath(minecraft);
Logger.info("Transformed Minecraft remapped");
}
Path conduit = getLoadedJar(Constants.MAIN_CONDUIT_FILE);
if (conduit != null) {
Logger.info("Transforming Conduit");
builder.addTransformationPath(conduit);
Logger.info("Transformed Conduit");
}
}
}

@Override
public Callable<Void> launchService(String[] args, ITransformingClassLoader launchClassLoader) {
// Add mixins to configure
LauncherStart.MIXINS.forEach(Mixins::addConfiguration);
return () -> {
final Class<?> mcClass = Class.forName(Constants.MAIN_CLIENT_FILE, true, launchClassLoader.getInstance());
final Method mcClassMethod = mcClass.getMethod("main", String[].class);
mcClassMethod.invoke(null, (Object) args);
return null;
};
}

private Path getLoadedJar(String className) {
// Returns the jars from the classpath if loaded by intellij
try {
return new File(Class.forName(className).getProtectionDomain().getCodeSource().getLocation().toURI()).toPath();
} catch (Exception ignored) {
return null;
}
}

@Override
public String name() {
return "minecraft-client";
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
systems.conduit.stream.launcher.services.ClientLaunchHandlerService
systems.conduit.stream.launcher.services.ServerLaunchHandlerService
6 changes: 3 additions & 3 deletions src/main/resources/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@
"type": "maven",
"groupId": "cpw.mods",
"artifactId": "modlauncher",
"version": "6.1.1",
"version": "8.0.6",
"url": "https://files.minecraftforge.net/maven/"
},
{
"type": "maven",
"groupId": "org.spongepowered",
"artifactId": "mixin",
"version": "0.8",
"url": "https://repo.conduit.systems/repository/releases/"
"version": "0.8.2",
"url": "https://repo.spongepowered.org/maven/"
}
]
}

0 comments on commit 4bd8d77

Please sign in to comment.