Skip to content

Commit

Permalink
Implement asset adding via game jars
Browse files Browse the repository at this point in the history
  • Loading branch information
glowredman committed Feb 1, 2023
1 parent bd323d6 commit 1a440d0
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 113 deletions.
56 changes: 56 additions & 0 deletions src/main/java/glowredman/txloader/Asset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package glowredman.txloader;

import java.io.File;
import java.util.Arrays;
import java.util.stream.Collectors;

class Asset {

String resourceLocation;
String resourceLocationOverride;
boolean forceLoad;
String version;
Source source;
transient boolean addedByMod; // ignored by GSON

Asset(String resourceLocation, String version, Source source) {
this.resourceLocation = resourceLocation;
this.version = version;
this.source = source;
}

String getResourceLocation() {
return this.resourceLocationOverride == null ? this.resourceLocation : this.resourceLocationOverride;
}

File getFile() {
File path = this.forceLoad ? TXLoaderCore.forceResourcesDir : TXLoaderCore.resourcesDir;
return new File(path, this.getResourceLocation());
}

String getVersion() {
return this.version == null ? RemoteHandler.latestRelease : this.version;
}

Source getSource() {
return this.source == null ? Source.ASSET : this.source;
}

enum Source {

ASSET,
CLIENT,
SERVER;

static final Iterable<String> NAMES = Arrays.stream(values()).map(Source::name).collect(Collectors.toList());

static Source get(String name) {
try {
return valueOf(name);
} catch (Exception e) {
TXLoaderCore.LOGGER.warn("{} is not a valid source identifier!", name);
return ASSET;
}
}
}
}
17 changes: 15 additions & 2 deletions src/main/java/glowredman/txloader/AssetBuilder.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package glowredman.txloader;

import glowredman.txloader.ConfigHandler.Asset;
import glowredman.txloader.Asset.Source;

public class AssetBuilder {

private final Asset asset;

AssetBuilder(String resourceLocation) {
this.asset = new Asset(resourceLocation, RemoteHandler.latestRelease);
this.asset = new Asset(resourceLocation, RemoteHandler.latestRelease, Source.ASSET);
this.asset.addedByMod = true;
}

Expand Down Expand Up @@ -46,6 +46,19 @@ public AssetBuilder setVersion(String version) {
return this;
}

/**
* Define this {@link Asset}'s source. Default is {@link Source#ASSET}. {@link Source#CLIENT} and
* {@link Source#SERVER} will be cached.
*
* @param source
* @return This {@link AssetBuilder} object to allow chaining of method calls
* @author glowredman
*/
public AssetBuilder setSource(Source source) {
this.asset.source = source;
return this;
}

/**
* Adds this {@link Asset} to the list of remote assets to load.
*
Expand Down
68 changes: 35 additions & 33 deletions src/main/java/glowredman/txloader/CommandTX.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import net.minecraft.util.IChatComponent;
import net.minecraftforge.client.ClientCommandHandler;

import glowredman.txloader.ConfigHandler.Asset;
import glowredman.txloader.Asset.Source;

public class CommandTX implements ICommand {
class CommandTX implements ICommand {

@Override
public int compareTo(Object o) {
Expand All @@ -28,7 +28,7 @@ public String getCommandName() {

@Override
public String getCommandUsage(ICommandSender sender) {
return "/tx <save|add <version> <resourceLocation> [resourceLocationOverride] [force]>";
return "/tx <save|add <version> <source> <resourceLocation> [resourceLocationOverride] [force]>";
}

@SuppressWarnings("rawtypes")
Expand All @@ -39,12 +39,13 @@ public List getCommandAliases() {

@Override
public void processCommand(ICommandSender sender, String[] args) {
System.out.println(args.length);
if (args.length == 0) {
int length = args.length;
if (length == 0) {
sender.addChatMessage(getColoredText("Not enough Arguments!", EnumChatFormatting.RED));
return;
}
if (args[0].equals("save")) {
String mode = args[0];
if (mode.equals("save")) {
if (ConfigHandler.save()) {
sender.addChatMessage(
getColoredText(
Expand All @@ -56,44 +57,41 @@ public void processCommand(ICommandSender sender, String[] args) {
getColoredText("Failed saving Config! Look at the Log to find the Cause.", EnumChatFormatting.RED));
return;
}
if (args[0].equals("add")) {
if (args.length < 3) {
if (mode.equals("add")) {
if (length < 4) {
sender.addChatMessage(getColoredText("Not enough Arguments!", EnumChatFormatting.RED));
return;
}
args = fixSpacesForVersion(args);
if (args.length == 0) {
if (length == 0) {
sender.addChatMessage(getColoredText("Missing closing Quotation Mark!", EnumChatFormatting.RED));
return;
}
if (args.length > 5) {
if (length > 6) {
sender.addChatMessage(
getColoredText(
"Too many Arguments! If your Version has Spaces, wrap it in Quotation Marks.",
EnumChatFormatting.RED));
return;
}
if (args.length == 3) {
TXLoaderCore.REMOTE_ASSETS.add(new Asset(args[2], args[1]));
sender.addChatMessage(getColoredText("Done. Don't forget to save!", EnumChatFormatting.GREEN));
return;
}
if (args.length == 4) {
if (args[3].equals("true")) {
TXLoaderCore.REMOTE_ASSETS.add(new Asset(args[2], args[1], true));
sender.addChatMessage(getColoredText("Done. Don't forget to save!", EnumChatFormatting.GREEN));
return;
}
if (args[3].equals("false")) {
TXLoaderCore.REMOTE_ASSETS.add(new Asset(args[2], args[1], false));
sender.addChatMessage(getColoredText("Done. Don't forget to save!", EnumChatFormatting.GREEN));
return;
final Asset asset = new Asset(args[3], args[1], Source.get(args[2]));
if (length != 4) {
String arg4 = args[4];
if (length == 5) {
// argument 4 may either be forceLoad or resourceLocationOverride
if (arg4.equals("true")) {
asset.forceLoad = true;
} else if (arg4.equals("false")) {
asset.forceLoad = false;
} else {
asset.resourceLocationOverride = arg4;
}
} else {
asset.resourceLocationOverride = arg4;
asset.forceLoad = args[5].equals("true");
}
TXLoaderCore.REMOTE_ASSETS.add(new Asset(args[2], args[1], args[3]));
sender.addChatMessage(getColoredText("Done. Don't forget to save!", EnumChatFormatting.GREEN));
return;
}
TXLoaderCore.REMOTE_ASSETS.add(new Asset(args[2], args[1], args[3], args[4].equals("true")));
TXLoaderCore.REMOTE_ASSETS.add(asset);
sender.addChatMessage(getColoredText("Done. Don't forget to save!", EnumChatFormatting.GREEN));
}
}
Expand All @@ -106,16 +104,20 @@ public boolean canCommandSenderUseCommand(ICommandSender sender) {
@SuppressWarnings("rawtypes")
@Override
public List addTabCompletionOptions(ICommandSender sender, String[] args) {
if (args.length == 1) {
int length = args.length;
if (length == 1) {
return CommandBase.getListOfStringsMatchingLastWord(args, "add", "save");
}
if (!args[0].equals("add") || args.length > 5) {
if (!args[0].equals("add") || length > 6) {
return null;
}
if (args.length == 2) {
if (length == 2) {
return CommandBase.getListOfStringsFromIterableMatchingLastWord(args, RemoteHandler.VERSIONS.keySet());
}
if (args.length == 4 || args.length == 5) {
if (length == 3) {
return CommandBase.getListOfStringsFromIterableMatchingLastWord(args, Source.NAMES);
}
if (length == 5 || length == 6) {
return CommandBase.getListOfStringsMatchingLastWord(args, "true", "false");
}
return null;
Expand Down
35 changes: 0 additions & 35 deletions src/main/java/glowredman/txloader/ConfigHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,4 @@ static void moveRLAssets() {
}
}
}

static class Asset {

String resourceLocation;
String resourceLocationOverride;
boolean forceLoad = false;
String version = RemoteHandler.latestRelease;
transient boolean addedByMod = false;

Asset(String resourceLocation, String version) {
this(resourceLocation, version, null, false);
}

Asset(String resourceLocation, String version, String resourceLocationOverride) {
this(resourceLocation, version, resourceLocationOverride, false);
}

Asset(String resourceLocation, String version, boolean force) {
this(resourceLocation, version, null, force);
}

Asset(String resourceLocation, String version, String resourceLocationOverride, boolean force) {
this.resourceLocation = resourceLocation;
this.resourceLocationOverride = resourceLocationOverride;
this.forceLoad = force;
this.version = version;
}

File getFile() {
File path = this.forceLoad ? TXLoaderCore.forceResourcesDir : TXLoaderCore.resourcesDir;
String resourceLocation = this.resourceLocationOverride == null ? this.resourceLocation
: this.resourceLocationOverride;
return new File(path, resourceLocation);
}
}
}
71 changes: 71 additions & 0 deletions src/main/java/glowredman/txloader/JarHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package glowredman.txloader;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.tuple.Pair;

class JarHandler {

static final Map<String, Path> CACHED_CLIENT_JARS = new HashMap<>();
static final Map<String, Path> CACHED_SERVER_JARS = new HashMap<>();

static Path txloaderCache;

static void indexJars() {
String userHome = System.getProperty("user.home");
txloaderCache = Paths.get(userHome, "txloader");

List<Pair<Path, String>> clientLocations = new ArrayList<>();
clientLocations.add(Pair.of(txloaderCache, "client.jar"));
clientLocations.add(Pair.of(Paths.get(userHome, "AppData", "Roaming", ".minecraft", "versions"), "%s.jar"));
clientLocations.add(
Pair.of(
Paths.get(userHome, ".gradle", "caches", "forge_gradle", "minecraft_repo", "versions"),
"client.jar"));
clientLocations.add(
Pair.of(
Paths.get(userHome, ".gradle", "caches", "minecraft", "net", "minecraft", "minecraft"),
"minecraft-%s.jar"));
clientLocations.add(
Pair.of(Paths.get(userHome, ".gradle", "caches", "retro_futura_gradle", "mc-vanilla"), "client.jar"));

List<Pair<Path, String>> serverLocations = new ArrayList<>();
clientLocations.add(Pair.of(txloaderCache, "server.jar"));
serverLocations.add(
Pair.of(
Paths.get(userHome, ".gradle", "caches", "forge_gradle", "minecraft_repo", "versions"),
"server.jar"));
serverLocations.add(
Pair.of(
Paths.get(userHome, ".gradle", "caches", "minecraft", "net", "minecraft", "minecraft_server"),
"minecraft_server-%s.jar"));
serverLocations.add(
Pair.of(Paths.get(userHome, ".gradle", "caches", "retro_futura_gradle", "mc-vanilla"), "server.jar"));

for (String version : RemoteHandler.VERSIONS.keySet()) {
for (Pair<Path, String> location : clientLocations) {
Path jarPath = location.getLeft().resolve(version).resolve(String.format(location.getRight(), version));
if (Files.exists(jarPath)) {
CACHED_CLIENT_JARS.put(version, jarPath);
TXLoaderCore.LOGGER.debug("Found CLIENT jar for version {} at {}", version, jarPath);
break;
}
}
for (Pair<Path, String> location : serverLocations) {
Path jarPath = location.getLeft().resolve(version).resolve(String.format(location.getRight(), version));
if (Files.exists(jarPath)) {
CACHED_SERVER_JARS.put(version, jarPath);
TXLoaderCore.LOGGER.debug("Found SERVER jar for version {} at {}", version, jarPath);
break;
}
}
}
}

}
Loading

0 comments on commit 1a440d0

Please sign in to comment.