Skip to content

Commit

Permalink
Merge pull request #1 from clickcrystals-development/improper
Browse files Browse the repository at this point in the history
made it ONLY download files when CC is not UP-TO-DATE. added info req…
  • Loading branch information
I-No-oNe authored Aug 17, 2024
2 parents dc8025f + aeae0ec commit 188a90d
Showing 1 changed file with 94 additions and 57 deletions.
151 changes: 94 additions & 57 deletions src/main/java/net/i_no_am/clickcrystals/ModDownloader.java
Original file line number Diff line number Diff line change
@@ -1,85 +1,122 @@
package net.i_no_am.clickcrystals;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.Gson;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Environment(EnvType.CLIENT)
public class ModDownloader implements ClientModInitializer {
private static final Logger LOGGER = Logger.getLogger(ModDownloader.class.getName());

public static final String REPO = "https://github.com/clickcrystals-development/ClickCrystals";
public static final String INFO_LINK = "https://itzispyder.github.io/clickcrystals/info";
public static final Logger LOGGER = LoggerFactory.getLogger("ClickCrystals Downloader");

@Override
public void onInitializeClient() {
String version = "ClickCrystals-" + getGameVersion() + "-" + getLatestClickCrystalsVersion();
File file = Paths.get(FabricLoader.getInstance().getGameDir().toString(), "mods", version + ".jar").toFile();
boolean wasBefore = file.exists();

try (BufferedInputStream bis = new BufferedInputStream(new URL("https://github.com/clickcrystals-development/ClickCrystals/releases/download/v" + getLatestClickCrystalsVersion() + "/" + version + ".jar").openStream());
FileOutputStream fos = new FileOutputStream(file)) {

System.out.println("Downloading: " + version);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bis.read(buffer, 0, 1024)) != -1) {
fos.write(buffer, 0, bytesRead);
}
Info info = requestInfo();
downloadAsset(info);
}

if (!wasBefore) {
System.out.println("ClickCrystals has been installed. Please restart the game.");
System.exit(-1);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error during ClickCrystals installation", e);
public static Info requestInfo() {
try {
URL url = URI.create(INFO_LINK).toURL();
InputStream is = url.openStream();
String json = new String(is.readAllBytes());

is.close();

Gson gson = new Gson();
Info info = gson.fromJson(json, Info.class);
LOGGER.info("Requested info: {}", gson.toJson(info));
return info;
}
catch (Exception ex) {
LOGGER.error("An unexpected error occurred while trying to download mod information from {}: {}", INFO_LINK, ex);
ex.printStackTrace();
return new Info();
}
}

private static String getLatestClickCrystalsVersion() {
String version = "";
public static void downloadAsset(Info info) {
try {
URL url = new URL("https://itzispyder.github.io/clickcrystals/info.html");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder content = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
URL url = URI.create(info.getFile()).toURL();
FabricLoader loader = FabricLoader.getInstance();
File gameDir = loader.getGameDir().toFile();
Optional<ModContainer> clickcrystals = loader.getModContainer("clickcrystals");

LOGGER.info("Checking for environment...");
if (clickcrystals.isPresent()) {
String version = clickcrystals.get().getMetadata().getVersion().getFriendlyString();
if (version.contains(info.latest)) {
LOGGER.info("ClickCrystals is UP-TO-DATE!");
return;
}
}
in.close();

JsonObject jsonObject = JsonParser.parseString(content.toString()).getAsJsonObject();
version = jsonObject.get("latest").getAsString();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error fetching the latest ClickCrystals version", e);
LOGGER.info("Preparing for install...");

File file = new File("%s/mods/%s".formatted(gameDir, info.getAsset()));

if (!file.getParentFile().exists())
file.getParentFile().mkdirs();
if (!file.exists())
file.createNewFile();

InputStream is = url.openStream();
FileOutputStream fos = new FileOutputStream(file);
byte[] data = is.readAllBytes();
double size = Math.floor(data.length / 10000.0) / 100.0;

LOGGER.info("Downloading ClickCrystals {} ({} MB)", info.latest, size);

fos.write(data);
fos.flush();
fos.close();
is.close();

LOGGER.info("ClickCrystals Installation Completed Successfully!");
}
catch (Exception ex) {
LOGGER.error("An unexpected error occurred while trying to install ClickCrystals: {}", ex.getMessage());
ex.printStackTrace();
}
return version;
}

private static String getGameVersion() {
FabricLoader loader = FabricLoader.getInstance();
ModContainer container = loader.getModContainer("minecraft").orElse(null);
String gameVersion = null;
if (container != null) {
gameVersion = container.getMetadata().getVersion().getFriendlyString();
public static class Info {
public String latest = "unknown";
public final Map<String, String> versionMappings = new HashMap<>();

public String getGameVersion() {
FabricLoader loader = FabricLoader.getInstance();
Optional<ModContainer> minecraft = loader.getModContainer("minecraft");

if (minecraft.isEmpty())
throw new IllegalArgumentException("Minecraft is not installed!");

String version = minecraft.get().getMetadata().getVersion().getFriendlyString();
return versionMappings.getOrDefault(version, "null");
}

public String getAsset() {
return "ClickCrystals-%s-%s.jar".formatted(getGameVersion(), latest);
}

public String getFile() {
return "%s/releases/download/v%s/%s".formatted(REPO, latest, getAsset());
}
return switch (Objects.requireNonNull(gameVersion)) {
case "1.20.1" -> "1.20";
case "1.20.3" -> "1.20.4";
case "1.20.5" -> "1.20.6";
default -> gameVersion;
};
}
}

0 comments on commit 188a90d

Please sign in to comment.