1.1.2
All checks were successful
ModUpdater/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-06-27 13:56:22 -04:00
parent 26073170b7
commit c0303bf44f
8 changed files with 57 additions and 46 deletions

View File

@ -1,5 +1,9 @@
# Changelog # Changelog
**1.1.2**
* Cache Reader Objects
* Improve Logging
**1.1.1** **1.1.1**
* Use CurseForge For Fabric API * Use CurseForge For Fabric API

View File

@ -9,7 +9,7 @@ org.gradle.jvmargs = -Xmx1G
fabric_loader_version = 0.8.8+build.202 fabric_loader_version = 0.8.8+build.202
# Mod Properties # Mod Properties
mod_version = 1.1.1 mod_version = 1.1.2
maven_group = com.thebrokenrail maven_group = com.thebrokenrail
# Dependencies # Dependencies

View File

@ -6,7 +6,7 @@ import net.fabricmc.api.ModInitializer;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.util.Objects; import javax.annotation.Nullable;
public class ModUpdater implements ModInitializer { public class ModUpdater implements ModInitializer {
public static final String NAMESPACE = "modupdater"; public static final String NAMESPACE = "modupdater";
@ -17,34 +17,28 @@ public class ModUpdater implements ModInitializer {
return LogManager.getLogger(LOGGER_NAME); return LogManager.getLogger(LOGGER_NAME);
} }
public static void log(String name, String msg) { public static void logWarn(String name, String msg) {
getLogger().warn(String.format("%s: %s", name, msg)); getLogger().warn(String.format("%s: %s", name, msg));
} }
public static void logInfo(String info) {
getLogger().info(info);
}
private static volatile ModUpdate[] updates; private static volatile ModUpdate[] updates;
private static Thread updateThread; public static void findUpdates() {
updates = UpdateStrategyRunner.checkAllModsForUpdates();
}
@Nullable
public static ModUpdate[] getUpdates() { public static ModUpdate[] getUpdates() {
if (updates == null) {
if (Thread.currentThread() == updateThread) {
updates = UpdateStrategyRunner.checkAllModsForUpdates();
} else {
return null;
}
}
return updates; return updates;
} }
@Override @Override
public void onInitialize() { public void onInitialize() {
updateThread = new Thread(() -> { Thread updateThread = new Thread(ModUpdater::findUpdates);
getLogger().info("Checking For Mod Updates...");
for (ModUpdate update : Objects.requireNonNull(getUpdates())) {
getLogger().info(update.text + " (" + update.downloadURL + ')');
}
getLogger().info(updates.length + " Mod Update(s) Found");
});
updateThread.start(); updateThread.start();
} }
} }

View File

@ -33,18 +33,18 @@ public class ModUpdateScreen extends Screen {
children.add(list); children.add(list);
int buttonWidth = 150; int buttonWidth = 150;
int paddingX = 5; int paddingX = 5;
int doneX = width / 2 - buttonWidth - paddingX; int downloadX = width / 2 - buttonWidth - paddingX;
int downloadX = width / 2 + paddingX; int doneX = width / 2 + paddingX;
download = addButton(new ButtonWidget(downloadX, height - 30, buttonWidth, 20, new TranslatableText("gui." + ModUpdater.NAMESPACE + ".download"), buttonWidget -> { addButton(new ButtonWidget(downloadX, height - 30, buttonWidth, 20, ScreenTexts.DONE, buttonWidget -> {
assert client != null;
client.openScreen(parent);
}));
download = addButton(new ButtonWidget(doneX, height - 30, buttonWidth, 20, new TranslatableText("gui." + ModUpdater.NAMESPACE + ".download"), buttonWidget -> {
if (list.getSelected() != null) { if (list.getSelected() != null) {
Util.getOperatingSystem().open(list.getSelected().update.downloadURL); Util.getOperatingSystem().open(list.getSelected().update.downloadURL);
} }
})); }));
download.active = false; download.active = false;
addButton(new ButtonWidget(doneX, height - 30, buttonWidth, 20, ScreenTexts.DONE, buttonWidget -> {
assert client != null;
client.openScreen(parent);
}));
super.init(); super.init();
} }

View File

@ -24,6 +24,13 @@ public class CurseForgeStrategy implements UpdateStrategy {
private String[] gameVersion; private String[] gameVersion;
} }
private final JsonAdapter<CurseForgeFile[]> jsonAdapter;
public CurseForgeStrategy() {
Moshi moshi = new Moshi.Builder().build();
jsonAdapter = moshi.adapter(CurseForgeFile[].class);
}
@Override @Override
@Nullable @Nullable
public ModUpdate run(ConfigObject obj, String oldVersion, String name) { public ModUpdate run(ConfigObject obj, String oldVersion, String name) {
@ -31,7 +38,7 @@ public class CurseForgeStrategy implements UpdateStrategy {
try { try {
projectID = obj.getInt("projectID"); projectID = obj.getInt("projectID");
} catch (ConfigObject.MissingValueException e) { } catch (ConfigObject.MissingValueException e) {
ModUpdater.log(name, e.getMessage()); ModUpdater.logWarn(name, e.getMessage());
return null; return null;
} }
@ -39,18 +46,15 @@ public class CurseForgeStrategy implements UpdateStrategy {
try { try {
data = Util.urlToString("https://addons-ecs.forgesvc.net/api/v2/addon/" + projectID + "/files"); data = Util.urlToString("https://addons-ecs.forgesvc.net/api/v2/addon/" + projectID + "/files");
} catch (IOException e) { } catch (IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }
CurseForgeFile[] files; CurseForgeFile[] files;
try { try {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<CurseForgeFile[]> jsonAdapter = moshi.adapter(CurseForgeFile[].class);
files = jsonAdapter.fromJson(data); files = jsonAdapter.fromJson(data);
} catch (JsonDataException | IOException e) { } catch (JsonDataException | IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }

View File

@ -26,6 +26,13 @@ public class GitHubReleasesStrategy implements UpdateStrategy {
private String browser_download_url; private String browser_download_url;
} }
private final JsonAdapter<GitHubRelease[]> jsonAdapter;
public GitHubReleasesStrategy() {
Moshi moshi = new Moshi.Builder().build();
jsonAdapter = moshi.adapter(GitHubRelease[].class).nonNull();
}
@Override @Override
@Nullable @Nullable
public ModUpdate run(ConfigObject obj, String oldVersion, String name) { public ModUpdate run(ConfigObject obj, String oldVersion, String name) {
@ -35,7 +42,7 @@ public class GitHubReleasesStrategy implements UpdateStrategy {
owner = obj.getString("owner"); owner = obj.getString("owner");
repo = obj.getString("repository"); repo = obj.getString("repository");
} catch (ConfigObject.MissingValueException e) { } catch (ConfigObject.MissingValueException e) {
ModUpdater.log(name, e.getMessage()); ModUpdater.logWarn(name, e.getMessage());
return null; return null;
} }
@ -43,19 +50,15 @@ public class GitHubReleasesStrategy implements UpdateStrategy {
try { try {
data = Util.urlToString(String.format("https://api.github.com/repos/%s/%s/releases", owner, repo)); data = Util.urlToString(String.format("https://api.github.com/repos/%s/%s/releases", owner, repo));
} catch (IOException e) { } catch (IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }
GitHubRelease[] releases; GitHubRelease[] releases;
try { try {
Moshi moshi = new Moshi.Builder().build(); releases = jsonAdapter.fromJson(data);
JsonAdapter<GitHubRelease[]> jsonAdapter = moshi.adapter(GitHubRelease[].class);
// GitHub's API never omits values, they're always null
releases = jsonAdapter.nonNull().fromJson(data);
} catch (JsonDataException | IOException e) { } catch (JsonDataException | IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }

View File

@ -19,6 +19,8 @@ import java.io.InputStream;
import java.util.List; import java.util.List;
public class MavenStrategy implements UpdateStrategy { public class MavenStrategy implements UpdateStrategy {
private final SAXReader reader = new SAXReader();
@Override @Override
@Nullable @Nullable
public ModUpdate run(ConfigObject obj, String oldVersion, String name) { public ModUpdate run(ConfigObject obj, String oldVersion, String name) {
@ -30,7 +32,7 @@ public class MavenStrategy implements UpdateStrategy {
group = obj.getString("group"); group = obj.getString("group");
artifact = obj.getString("artifact"); artifact = obj.getString("artifact");
} catch (ConfigObject.MissingValueException e) { } catch (ConfigObject.MissingValueException e) {
ModUpdater.log(name, e.getMessage()); ModUpdater.logWarn(name, e.getMessage());
return null; return null;
} }
@ -40,16 +42,15 @@ public class MavenStrategy implements UpdateStrategy {
try { try {
data = Util.urlToString(mavenRoot + "/maven-metadata.xml"); data = Util.urlToString(mavenRoot + "/maven-metadata.xml");
} catch (IOException e) { } catch (IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }
Document doc; Document doc;
try (InputStream source = new ByteArrayInputStream(data.getBytes())) { try (InputStream source = new ByteArrayInputStream(data.getBytes())) {
SAXReader reader = new SAXReader();
doc = reader.read(source); doc = reader.read(source);
} catch (DocumentException | IOException e) { } catch (DocumentException | IOException e) {
ModUpdater.log(name, e.toString()); ModUpdater.logWarn(name, e.toString());
return null; return null;
} }

View File

@ -25,7 +25,7 @@ public class UpdateStrategyRunner {
try { try {
obj = new ConfigObjectCustom(metadata.getCustomValue(ModUpdater.NAMESPACE).getAsObject()); obj = new ConfigObjectCustom(metadata.getCustomValue(ModUpdater.NAMESPACE).getAsObject());
} catch (ClassCastException e) { } catch (ClassCastException e) {
ModUpdater.log(name, String.format("\"%s\" Is Not An Object", ModUpdater.NAMESPACE)); ModUpdater.logWarn(name, String.format("\"%s\" Is Not An Object", ModUpdater.NAMESPACE));
return null; return null;
} }
} else { } else {
@ -41,13 +41,13 @@ public class UpdateStrategyRunner {
try { try {
strategy = obj.getString("strategy"); strategy = obj.getString("strategy");
} catch (ConfigObject.MissingValueException e) { } catch (ConfigObject.MissingValueException e) {
ModUpdater.log(name, e.getMessage()); ModUpdater.logWarn(name, e.getMessage());
return null; return null;
} }
UpdateStrategy strategyObj = UpdateStrategyRegistry.get(strategy); UpdateStrategy strategyObj = UpdateStrategyRegistry.get(strategy);
if (strategyObj == null) { if (strategyObj == null) {
ModUpdater.log(name, "Invalid Strategy: " + name); ModUpdater.logWarn(name, "Invalid Strategy: " + name);
return null; return null;
} }
@ -55,6 +55,8 @@ public class UpdateStrategyRunner {
} }
public static ModUpdate[] checkAllModsForUpdates() { public static ModUpdate[] checkAllModsForUpdates() {
ModUpdater.logInfo("Checking For Mod Updates...");
List<ModUpdate> updates = new ArrayList<>(); List<ModUpdate> updates = new ArrayList<>();
AtomicInteger remaining = new AtomicInteger(0); AtomicInteger remaining = new AtomicInteger(0);
@ -66,6 +68,7 @@ public class UpdateStrategyRunner {
ModUpdate update = checkModForUpdate(metadata); ModUpdate update = checkModForUpdate(metadata);
if (update != null) { if (update != null) {
ModUpdater.logInfo(update.text + " (" + update.downloadURL + ')');
synchronized (updates) { synchronized (updates) {
updates.add(update); updates.add(update);
} }
@ -91,6 +94,8 @@ public class UpdateStrategyRunner {
} }
} }
ModUpdater.logInfo(updates.size() + String.format(" Mod Update%s Found", updates.size() == 1 ? "" : "s"));
return updates.toArray(new ModUpdate[0]); return updates.toArray(new ModUpdate[0]);
} }
} }