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
**1.1.2**
* Cache Reader Objects
* Improve Logging
**1.1.1**
* Use CurseForge For Fabric API

View File

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

View File

@ -6,7 +6,7 @@ import net.fabricmc.api.ModInitializer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Objects;
import javax.annotation.Nullable;
public class ModUpdater implements ModInitializer {
public static final String NAMESPACE = "modupdater";
@ -17,34 +17,28 @@ public class ModUpdater implements ModInitializer {
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));
}
public static void logInfo(String info) {
getLogger().info(info);
}
private static volatile ModUpdate[] updates;
private static Thread updateThread;
public static void findUpdates() {
updates = UpdateStrategyRunner.checkAllModsForUpdates();
}
@Nullable
public static ModUpdate[] getUpdates() {
if (updates == null) {
if (Thread.currentThread() == updateThread) {
updates = UpdateStrategyRunner.checkAllModsForUpdates();
} else {
return null;
}
}
return updates;
}
@Override
public void onInitialize() {
updateThread = new Thread(() -> {
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");
});
Thread updateThread = new Thread(ModUpdater::findUpdates);
updateThread.start();
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -25,7 +25,7 @@ public class UpdateStrategyRunner {
try {
obj = new ConfigObjectCustom(metadata.getCustomValue(ModUpdater.NAMESPACE).getAsObject());
} 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;
}
} else {
@ -41,13 +41,13 @@ public class UpdateStrategyRunner {
try {
strategy = obj.getString("strategy");
} catch (ConfigObject.MissingValueException e) {
ModUpdater.log(name, e.getMessage());
ModUpdater.logWarn(name, e.getMessage());
return null;
}
UpdateStrategy strategyObj = UpdateStrategyRegistry.get(strategy);
if (strategyObj == null) {
ModUpdater.log(name, "Invalid Strategy: " + name);
ModUpdater.logWarn(name, "Invalid Strategy: " + name);
return null;
}
@ -55,6 +55,8 @@ public class UpdateStrategyRunner {
}
public static ModUpdate[] checkAllModsForUpdates() {
ModUpdater.logInfo("Checking For Mod Updates...");
List<ModUpdate> updates = new ArrayList<>();
AtomicInteger remaining = new AtomicInteger(0);
@ -66,6 +68,7 @@ public class UpdateStrategyRunner {
ModUpdate update = checkModForUpdate(metadata);
if (update != null) {
ModUpdater.logInfo(update.text + " (" + update.downloadURL + ')');
synchronized (updates) {
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]);
}
}