Add sleep assets to jar and extract when missing

This will solve missing assets on release tasks or when running the jar solo without any other resources in the dir. Ideally Idle jar is self sufficient and can extract any assets it needs on the fly without worrying about missing dependencies if the jar is moved or something is deleted.
This commit is contained in:
Kaila_Btw committed 2024-01-29 22:53:48 -05:00
1 parent ade88df2a3
commit 67783a5bdb
6 files changed
+8325 -19

No files matched your search

+3 -5
View File
@@ -27,8 +27,6 @@ repositories {
dependencies {
// Ensure gradle is aware of the patched client dependency
implementation files(new File(project(':patcher').getBuildDir(), 'libs/patched_client.jar'))
//include resource icons in jar
//implementation files(new File(project(':app').getProjectDir(), '/src/main/resources/res/'))
// Used to find/populate scripts
implementation libs.reflections
// Dependency of reflections spits out an error when it is not included in the jar.
@@ -42,7 +40,7 @@ dependencies {
tasks.named('compileJava') {
// Create resources before compileJava runs
dependsOn ':createClientCache', ':copyMapData', ':buildPatchedClient'
dependsOn ':createClientCache', ':copyMapData', ':copyAssetData', ':buildPatchedClient'
// Run spotlessApply after successful compilation rather than before, so
// that any developer errors can be dealt with prior to formatting
@@ -99,10 +97,10 @@ tasks.named('clean') {
// Create resources before processResources runs
tasks.named('processResources') {
dependsOn ':createClientCache', ':copyMapData', ':buildPatchedClient'
dependsOn ':createClientCache', ':copyMapData', ':copyAssetData', ':buildPatchedClient'
}
// FTBFS: spotlessMisc complains when there is no dependency on copyMapData
tasks.named('spotlessMisc') {
dependsOn ':copyMapData'
dependsOn ':copyMapData', ':copyAssetData'
}
+2 -3
View File
@@ -1054,8 +1054,7 @@ public class Main {
};
if (spriteFilelist != null) {
String spriteName = spriteFilelist[0].getName();
if (!spriteName.equalsIgnoreCase("Menus.osar")) return false;
if (!spriteFilelist[0].getName().equalsIgnoreCase("Menus.osar")) return false;
} else return false;
if (videoFilelist != null) {
if (videoFilelist.length < 6) return false;
@@ -1101,7 +1100,7 @@ public class Main {
dir.mkdirs();
// Copy embedded cache to cache directory
try { // todo access the zip cache stored in the Idlersc.jar file
try {
Extractor.extractZipResource("/cache/ZipCache.zip", dir.toPath());
} catch (IOException e) {
e.printStackTrace();
+63 -11
View File
@@ -4,16 +4,7 @@ import bot.Main;
import bot.ocrlib.*;
import controller.Controller;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
@@ -22,6 +13,7 @@ import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import javax.imageio.ImageIO;
public class SleepCallback {
@@ -139,7 +131,6 @@ public class SleepCallback {
sleepWord = readLine(slword);
onSleepFatigueUpdate(controller.getFatigue());
}
public static void setOCRType(final OCRType type) {
ocrType = type;
@@ -147,6 +138,9 @@ public class SleepCallback {
switch (ocrType) {
case INTERNAL:
if (!checkAssetFiles()) {
createAssetFiles();
}
try (final BufferedReader mr = new BufferedReader(new FileReader(MODEL_TXT));
final BufferedReader dr = new BufferedReader(new FileReader(DICT_TXT))) {
ocr = new OCR(new DictSearch(dr), mr);
@@ -285,4 +279,62 @@ public class SleepCallback {
BufferedImage img = convertImageTo1Bpp(fullColorImg);
ImageIO.write(img, "bmp", out);
}
private static void copyResource(String res, String dest, Class c) throws IOException {
InputStream src = c.getResourceAsStream(res);
Files.copy(src, Paths.get(dest), StandardCopyOption.REPLACE_EXISTING);
}
private static boolean checkAssetFiles() {
File sleepDirectory = new File("assets" + File.separator + "sleep");
File[] sleepFilelist = sleepDirectory.listFiles();
String[] fileNames = {"dictionary.txt", "model.txt"};
if (sleepFilelist != null) {
if (sleepFilelist.length != 2) return false;
String[] sleepNames = new String[sleepFilelist.length];
for (int i = 0; i < sleepFilelist.length; i++) {
sleepNames[i] = sleepFilelist[i].getName();
}
// compare the file names we read to the names we need
for (String sleepName : sleepNames) {
boolean callReturn = true;
for (String fileName : fileNames) {
if (sleepName.equalsIgnoreCase(fileName)) {
callReturn = false;
break;
}
}
// we are missing a file so regen cache
if (callReturn) return false;
}
} else return false;
return true;
}
private static void createAssetFiles() {
// Create Asset directory
File dir = new File("." + File.separator + "assets" + File.separator + "sleep");
dir.mkdirs();
// Copy embedded assets to assets/sleep directory
try {
copyResource(
"/assets/sleep/dictionary.txt",
"."
+ File.separator
+ "assets"
+ File.separator
+ "sleep"
+ File.separator
+ "dictionary.txt",
SleepCallback.class);
copyResource(
"/assets/sleep/model.txt",
"." + File.separator + "assets" + File.separator + "sleep" + File.separator + "model.txt",
SleepCallback.class);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+7
View File
@@ -52,6 +52,13 @@ tasks.register('copyMapData', Copy) {
into project(':app').file('src/main/resources/map')
}
tasks.register('copyAssetData', Copy) {
group 'build'
description 'Copy asset data for inclusion in the jar archive.'
from 'assets/sleep'
into project(':app').file('src/main/resources/assets/sleep')
}
tasks.register('prepareRelease', Copy) {
group 'Distribution'
description 'Prepare files for release zip.'