Archive eagler-binary-tools (tool, 1.8.8 / 1.5.2, clean)

This commit is contained in:
2026-11-27 22:14:04 -06:00
parent ec5d2c8be6
commit 01b09537a7
249 changed files with 19595 additions and 0 deletions
@@ -0,0 +1,172 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.awt.image.BufferedImage;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import net.lax1dude.eaglercraft.bintools.utils.IOUtils;
import net.lax1dude.eaglercraft.bintools.utils.LabPBR2Eagler;
public class EBPFileDecoder {
private static int getFromBits(int idxx, int bits, byte[] bytes) {
int startByte = idxx >> 3;
int endByte = (idxx + bits - 1) >> 3;
if (startByte == endByte) {
return (((int) bytes[startByte] & 0xff) >> (8 - (idxx & 7) - bits)) & ((1 << bits) - 1);
} else {
return (((((int) bytes[startByte] & 0xff) << 8) | ((int) bytes[endByte] & 0xff)) >> (16 - (idxx & 7)
- bits)) & ((1 << bits) - 1);
}
}
public static void _main(String[] args) throws IOException {
boolean labPBR = false;
if (args.length > 1 && args[0].equalsIgnoreCase("--labPBR")) {
labPBR = true;
String[] e = new String[args.length - 1];
System.arraycopy(args, 1, e, 0, e.length);
args = e;
}
if (args.length > 1 && args.length < 4 && args[0].equalsIgnoreCase("-r")) {
File input = new File(args[1]);
if (!input.isDirectory()) {
System.err.println("Error: Not a directory: " + input.getAbsolutePath());
System.exit(-1);
return;
}
convertDir(input, args.length == 3 ? new File(args[2]) : input, labPBR);
} else if (args.length == 2) {
System.out.println("Reading input file...");
BufferedImage img;
try (InputStream is = new FileInputStream(new File(args[0]))) {
img = readEBP(is);
}
if (labPBR) {
System.out.println("Converting from Eagler to LabPBR...");
LabPBR2Eagler.convertEaglerToLabPBR(img, img);
}
File output = new File(args[1]);
System.out.println("Writing PNG: " + output.getAbsolutePath());
ImageIO.write(img, "PNG", output);
} else {
System.out.println("Usage: ebp-decode [--labPBR] <input file> <output file>");
System.out.println(" ebp-decode [--labPBR] -r <directory> [output directory]");
}
}
public static void convertDir(File inputDir, File outputDir) throws IOException {
convertDir(inputDir, outputDir, false);
}
public static void convertDir(File inputDir, File outputDir, boolean labPBR) throws IOException {
if (!outputDir.isDirectory() && !outputDir.mkdirs()) {
throw new IOException("Could not create directory: " + outputDir.getAbsolutePath());
}
File[] f = inputDir.listFiles();
for (int i = 0; i < f.length; ++i) {
String name = f[i].getName();
if (f[i].isDirectory()) {
convertDir(f[i], new File(outputDir, name), labPBR);
continue;
}
if (!name.toLowerCase().endsWith(".ebp")) {
continue;
}
File ff = new File(outputDir, name.substring(0, name.length() - 3) + "png");
System.out.println(f[i].getAbsolutePath());
BufferedImage img;
try (InputStream is = new FileInputStream(f[i])) {
img = readEBP(is);
}
if (labPBR) {
LabPBR2Eagler.convertEaglerToLabPBR(img, img);
}
ImageIO.write(img, "PNG", ff);
}
}
private static int readByte(InputStream is) throws IOException {
int i = is.read();
if (i < 0) {
throw new EOFException();
}
return i;
}
public static BufferedImage readEBP(InputStream is) throws IOException {
if (readByte(is) != '%' || readByte(is) != 'E' || readByte(is) != 'B' || readByte(is) != 'P') {
throw new IOException("Not an EBP file!");
}
int v = readByte(is);
if (v != 1) {
throw new IOException("Unknown EBP version: " + v);
}
int c = readByte(is);
if (c != 3 && c != 4) {
throw new IOException("Invalid component count: " + c);
}
int w = readByte(is) | (readByte(is) << 8);
int h = readByte(is) | (readByte(is) << 8);
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
v = readByte(is);
if (v == 0) {
if (c == 3) {
for (int i = 0, l = w * h; i < l; ++i) {
img.setRGB(i % w, i / w, readByte(is) | (readByte(is) << 8) | (readByte(is) << 16) | 0xFF000000);
}
} else {
for (int i = 0, l = w * h; i < l; ++i) {
img.setRGB(i % w, i / w,
readByte(is) | (readByte(is) << 8) | (readByte(is) << 16) | (readByte(is) << 24));
}
}
} else if (v == 1) {
int paletteSize = readByte(is) + 1;
int[] palette = new int[paletteSize];
palette[0] = 0xFF000000;
if (c == 3) {
for (int i = 1; i < paletteSize; ++i) {
palette[i] = readByte(is) | (readByte(is) << 8) | (readByte(is) << 16) | 0xFF000000;
}
} else {
for (int i = 1; i < paletteSize; ++i) {
palette[i] = readByte(is) | (readByte(is) << 8) | (readByte(is) << 16) | (readByte(is) << 24);
}
}
int bpp = readByte(is);
byte[] readSet = new byte[readByte(is) | (readByte(is) << 8) | (readByte(is) << 16)];
IOUtils.readFully(is, readSet);
for (int i = 0, l = w * h; i < l; ++i) {
img.setRGB(i % w, i / w, palette[getFromBits(i * bpp, bpp, readSet)]);
}
} else {
throw new IOException("Unknown EBP storage type: " + v);
}
if (readByte(is) != ':' || readByte(is) != '>') {
throw new IOException("Invalid footer! (:>)");
}
return img;
}
}
@@ -0,0 +1,191 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import net.lax1dude.eaglercraft.bintools.utils.LabPBR2Eagler;
public class EBPFileEncoder {
private static void setBit(int idx, boolean v, byte[] bytes) {
int idx2 = idx >> 3;
if (v) {
bytes[idx2] |= (1 << (7 - (idx & 7)));
} else {
bytes[idx2] &= (-1 ^ (1 << (7 - (idx & 7))));
}
}
public static void _main(String[] args) throws IOException {
boolean labPBR = false;
if (args.length > 1 && args[0].equalsIgnoreCase("--labPBR")) {
labPBR = true;
String[] e = new String[args.length - 1];
System.arraycopy(args, 1, e, 0, e.length);
args = e;
}
if (args.length > 1 && args.length < 4 && args[0].equalsIgnoreCase("-r")) {
File input = new File(args[1]);
if (!input.isDirectory()) {
System.err.println("Error: Not a directory: " + input.getAbsolutePath());
System.exit(-1);
return;
}
convertDir(input, args.length == 3 ? new File(args[2]) : input, labPBR);
} else if (args.length == 2) {
System.out.println("Reading input file...");
BufferedImage img = ImageIO.read(new File(args[0]));
File output = new File(args[1]);
if (labPBR) {
System.out.println("Converting from LabPBR to Eagler...");
LabPBR2Eagler.convertLabPBRToEagler(img, img);
}
System.out.println("Encoding EBP: " + output.getAbsolutePath());
try (OutputStream os = new FileOutputStream(output)) {
writeEBP(img, os);
}
} else {
System.out.println("Usage: ebp-encode [--labPBR] <input file> <output file>");
System.out.println(" ebp-encode [--labPBR] -r <directory> [output directory]");
}
}
public static void convertDir(File inputDir, File outputDir) throws IOException {
convertDir(inputDir, outputDir, false);
}
public static void convertDir(File inputDir, File outputDir, boolean labPBR) throws IOException {
if (!outputDir.isDirectory() && !outputDir.mkdirs()) {
throw new IOException("Could not create directory: " + outputDir.getAbsolutePath());
}
File[] f = inputDir.listFiles();
for (int i = 0; i < f.length; ++i) {
String name = f[i].getName();
if (f[i].isDirectory()) {
convertDir(f[i], new File(outputDir, name), labPBR);
continue;
}
if (!name.toLowerCase().endsWith(".png")) {
continue;
}
File ff = new File(outputDir, name.substring(0, name.length() - 3) + "ebp");
System.out.println(f[i].getAbsolutePath());
BufferedImage img = ImageIO.read(f[i]);
if (labPBR) {
LabPBR2Eagler.convertLabPBRToEagler(img, img);
}
try (OutputStream os = new FileOutputStream(ff)) {
writeEBP(img, os);
}
}
}
public static void writeEBP(BufferedImage img, OutputStream fos) throws IOException {
int w = img.getWidth();
int h = img.getHeight();
int[] pixels = img.getRGB(0, 0, w, h, null, 0, w);
boolean alphaCh = false;
Set<Integer> colorPalette = new HashSet();
for (int i = 0; i < pixels.length; ++i) {
int c = pixels[i];
if ((c & 0xFF000000) == 0) {
c = 0;
}
if ((c & 0xFF000000) != 0xFF000000) {
alphaCh = true;
}
if (c == 0xFF000000) {
continue;
}
colorPalette.add(c);
}
fos.write(new byte[] { '%', 'E', 'B', 'P' });
fos.write(1); // v1
fos.write(alphaCh ? 4 : 3); // components
fos.write(w & 0xFF);
fos.write((w >>> 8) & 0xFF);
fos.write(h & 0xFF);
fos.write((h >>> 8) & 0xFF);
if (colorPalette.size() > 255) {
fos.write(0); // type is no palette
for (int i = 0; i < pixels.length; ++i) {
fos.write((pixels[i] >>> 16) & 0xFF);
fos.write((pixels[i] >>> 8) & 0xFF);
fos.write(pixels[i] & 0xFF);
if (alphaCh) {
fos.write(pixels[i] >>> 24);
}
}
} else {
Map<Integer, Integer> paletteHelper = new HashMap();
fos.write(1); // type is palette
fos.write(colorPalette.size()); // write palette size
Iterator<Integer> paletteItr = colorPalette.iterator();
int paletteIdx = 0;
paletteHelper.put(0xFF000000, 0);
while (paletteItr.hasNext()) {
int j = paletteItr.next().intValue();
paletteHelper.put(j, ++paletteIdx);
fos.write((j >>> 16) & 0xFF);
fos.write((j >>> 8) & 0xFF);
fos.write(j & 0xFF);
if (alphaCh) {
fos.write(j >>> 24);
}
}
int bpp = 1;
while ((paletteIdx >> bpp) != 0) {
++bpp;
}
fos.write(bpp); // write bpp
int totalBits = pixels.length * bpp;
byte[] completedBitSet = new byte[(totalBits + 7) >> 3];
int bsi = 0;
for (int i = 0; i < pixels.length; ++i) {
int c = pixels[i];
if ((c & 0xFF000000) == 0) {
c = 0;
}
int wr = paletteHelper.get(c);
for (int j = bpp - 1; j >= 0; --j) {
setBit(bsi++, ((wr >> j) & 1) != 0, completedBitSet);
}
}
fos.write(completedBitSet.length & 0xFF); // write expect length (as 24 bits not 32)
fos.write((completedBitSet.length >>> 8) & 0xFF);
fos.write((completedBitSet.length >>> 16) & 0xFF);
fos.write(completedBitSet); // write the bits
}
fos.write(new byte[] { ':', '>' });
}
}
@@ -0,0 +1,178 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import net.lax1dude.eaglercraft.bintools.utils.LabPBR2Eagler;
public class EBPFileEncoderLegacy {
private static void setBit(int idx, boolean v, byte[] bytes) {
int idx2 = idx >> 3;
if (v) {
bytes[idx2] |= (1 << (7 - (idx & 7)));
} else {
bytes[idx2] &= (-1 ^ (1 << (7 - (idx & 7))));
}
}
public static void _main(String[] args) throws IOException {
boolean labPBR = false;
if (args.length > 1 && args[0].equalsIgnoreCase("--labPBR")) {
labPBR = true;
String[] e = new String[args.length - 1];
System.arraycopy(args, 1, e, 0, e.length);
args = e;
}
if (args.length > 1 && args.length < 4 && args[0].equalsIgnoreCase("-r")) {
File input = new File(args[1]);
if (!input.isDirectory()) {
System.err.println("Error: Not a directory: " + input.getAbsolutePath());
System.exit(-1);
return;
}
convertDir(input, args.length == 3 ? new File(args[2]) : input, labPBR);
} else if (args.length == 2) {
System.out.println("Reading input file...");
BufferedImage img = ImageIO.read(new File(args[0]));
File output = new File(args[1]);
if (labPBR) {
System.out.println("Converting from LabPBR to Eagler...");
LabPBR2Eagler.convertLabPBRToEaglerLegacy(img, img);
}
System.out.println("Encoding EBP: " + output.getAbsolutePath());
try (OutputStream os = new FileOutputStream(output)) {
writeEBP(img, os);
}
} else {
System.out.println("Usage: legacy-ebp-encode [--labPBR] <input file> <output file>");
System.out.println(" legacy-ebp-encode [--labPBR] -r <directory> [output directory]");
}
}
public static void convertDir(File inputDir, File outputDir) throws IOException {
convertDir(inputDir, outputDir, false);
}
public static void convertDir(File inputDir, File outputDir, boolean labPBR) throws IOException {
if (!outputDir.isDirectory() && !outputDir.mkdirs()) {
throw new IOException("Could not create directory: " + outputDir.getAbsolutePath());
}
File[] f = inputDir.listFiles();
for (int i = 0; i < f.length; ++i) {
String name = f[i].getName();
if (f[i].isDirectory()) {
convertDir(f[i], new File(outputDir, name), labPBR);
continue;
}
if (!name.toLowerCase().endsWith(".png")) {
continue;
}
File ff = new File(outputDir, name.substring(0, name.length() - 3) + "ebp");
System.out.println(f[i].getAbsolutePath());
BufferedImage img = ImageIO.read(f[i]);
if (labPBR) {
LabPBR2Eagler.convertLabPBRToEaglerLegacy(img, img);
}
try (OutputStream os = new FileOutputStream(ff)) {
writeEBP(img, os);
}
}
}
public static void writeEBP(BufferedImage img, OutputStream fos) throws IOException {
fos.write(new byte[] { '%', 'E', 'B', 'P' });
fos.write(1); // v1
fos.write(3); // 3 component
int w = img.getWidth();
int h = img.getHeight();
fos.write(w & 0xFF);
fos.write((w >> 8) & 0xFF);
fos.write(h & 0xFF);
fos.write((h >> 8) & 0xFF);
int[] pixels = img.getRGB(0, 0, w, h, null, 0, w);
Set<Integer> colorPalette = new HashSet();
for (int i = 0; i < pixels.length; ++i) {
if ((pixels[i] & 0xFF000000) == 0) {
pixels[i] = 0;
} else {
pixels[i] &= 0xFFFFFF;
}
if (pixels[i] == 0) {
continue;
}
colorPalette.add(pixels[i]);
}
if (colorPalette.size() > 255) {
fos.write(0); // type is no palette
for (int i = 0; i < pixels.length; ++i) {
fos.write((pixels[i] >> 16) & 0xFF);
fos.write((pixels[i] >> 8) & 0xFF);
fos.write(pixels[i] & 0xFF);
}
} else {
Map<Integer, Integer> paletteHelper = new HashMap();
fos.write(1); // type is palette
fos.write(colorPalette.size()); // write palette size
Iterator<Integer> paletteItr = colorPalette.iterator();
int paletteIdx = 0;
paletteHelper.put(0, 0);
while (paletteItr.hasNext()) {
int j = paletteItr.next().intValue();
paletteHelper.put(j, ++paletteIdx);
fos.write((j >> 16) & 0xFF);
fos.write((j >> 8) & 0xFF);
fos.write(j & 0xFF);
}
int bpp = 1;
while ((paletteIdx >> bpp) != 0) {
++bpp;
}
fos.write(bpp); // write bpp
int totalBits = pixels.length * bpp;
byte[] completedBitSet = new byte[(totalBits & 7) == 0 ? (totalBits >> 3) : ((totalBits >> 3) + 1)];
int bsi = 0;
for (int i = 0; i < pixels.length; ++i) {
int wr = paletteHelper.get(pixels[i]);
for (int j = bpp - 1; j >= 0; --j) {
setBit(bsi++, ((wr >> j) & 1) != 0, completedBitSet);
}
}
fos.write(completedBitSet.length & 0xFF); // write expect length (as 24 bits not 32)
fos.write((completedBitSet.length >> 8) & 0xFF);
fos.write((completedBitSet.length >> 16) & 0xFF);
fos.write(completedBitSet); // write the bits
}
fos.write(new byte[] { ':', '>' });
}
}
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2022-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import net.lax1dude.eaglercraft.bintools.utils.GZIPOutputStream2;
public class EPKCompiler {
public static void _main(String[] args) throws IOException {
if (args.length < 2 || args.length > 4) {
System.out.println("Usage: epkcompiler <input directory> <output file> [gzip|zlib|none] [file-type]");
return;
}
File root = new File(args[0]);
File output = new File(args[1]);
char compressionType;
if (args.length > 2) {
if (args[2].equalsIgnoreCase("gzip")) {
compressionType = 'G';
} else if (args[2].equalsIgnoreCase("zlib")) {
compressionType = 'Z';
} else if (args[2].equalsIgnoreCase("none")) {
compressionType = '0';
} else {
throw new IllegalArgumentException("Unknown compression method: " + args[2]);
}
} else {
compressionType = 'G';
}
System.out.println("Scanning input directory...");
ArrayList<File> files = new ArrayList();
listDirectory(root, files);
ByteArrayOutputStream osb = new ByteArrayOutputStream();
String start = root.getAbsolutePath();
System.out.println("Compiling: " + output.getAbsolutePath());
osb.write("EAGPKG$$".getBytes(StandardCharsets.US_ASCII));
String chars = "ver2.0";
osb.write(chars.length());
osb.write(chars.getBytes(StandardCharsets.US_ASCII));
Date d = new Date();
String comment = "\n\n # Eagler EPK v2.0 - Generated by EaglerBinaryTools\n" + " # update: on "
+ (new SimpleDateFormat("MM/dd/yyyy")).format(d) + " at "
+ (new SimpleDateFormat("hh:mm:ss aa")).format(d) + "\n\n";
String nm = output.getName();
osb.write(nm.length());
osb.write(nm.getBytes(StandardCharsets.US_ASCII));
writeShort(comment.length(), osb);
osb.write(comment.getBytes(StandardCharsets.US_ASCII));
writeLong(d.getTime(), osb);
writeInt(files.size() + 1, osb);
osb.write(compressionType);
OutputStream os;
if (compressionType == 'G') {
System.out.println("Using GZIP compression");
os = new GZIPOutputStream2(osb, 9, 16384, true);
} else if (compressionType == 'Z') {
System.out.println("Using ZLIB (DEFLATE) compression");
os = new DeflaterOutputStream(osb, new Deflater(9), 16384, true);
} else {
System.out.println("Using no compression");
os = osb;
}
os.write("HEAD".getBytes(StandardCharsets.US_ASCII));
String key = "file-type";
os.write(key.length());
os.write(key.getBytes(StandardCharsets.US_ASCII));
String value;
if (args.length > 3) {
value = args[3];
} else {
value = "epk/resources";
}
writeInt(value.length(), os);
os.write(value.getBytes(StandardCharsets.US_ASCII));
os.write('>');
CRC32 checkSum = new CRC32();
for (File f : files) {
InputStream stream = new FileInputStream(f);
byte[] targetArray = new byte[(int) f.length()];
stream.read(targetArray);
stream.close();
checkSum.reset();
checkSum.update(targetArray, 0, targetArray.length);
int ch = (int) checkSum.getValue();
os.write("FILE".getBytes(StandardCharsets.US_ASCII));
String p = f.getAbsolutePath().replace(start, "").replace('\\', '/');
if (p.startsWith("/")) {
p = p.substring(1);
}
os.write(p.length());
os.write(p.getBytes(StandardCharsets.US_ASCII));
writeInt(targetArray.length + 5, os);
writeInt(ch, os);
os.write(targetArray);
os.write(':');
os.write('>');
}
os.write("END$".getBytes(StandardCharsets.US_ASCII));
os.close();
osb.write(":::YEE:>".getBytes(StandardCharsets.US_ASCII));
System.out.println("Compiled " + files.size() + " files into the EPK");
System.out.println("Writing to disk...");
FileOutputStream out = new FileOutputStream(output);
out.write(osb.toByteArray());
out.close();
}
public static void writeShort(int i, OutputStream os) throws IOException {
os.write((i >> 8) & 0xFF);
os.write(i & 0xFF);
}
public static void writeInt(int i, OutputStream os) throws IOException {
os.write((i >> 24) & 0xFF);
os.write((i >> 16) & 0xFF);
os.write((i >> 8) & 0xFF);
os.write(i & 0xFF);
}
public static void writeLong(long i, OutputStream os) throws IOException {
os.write((int) ((i >> 56) & 0xFF));
os.write((int) ((i >> 48) & 0xFF));
os.write((int) ((i >> 40) & 0xFF));
os.write((int) ((i >> 32) & 0xFF));
os.write((int) ((i >> 24) & 0xFF));
os.write((int) ((i >> 16) & 0xFF));
os.write((int) ((i >> 8) & 0xFF));
os.write((int) (i & 0xFF));
}
public static void listDirectory(File dir, ArrayList<File> files) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
listDirectory(f, files);
} else {
files.add(f);
}
}
}
}
@@ -0,0 +1,94 @@
/*
* Copyright (c) 2022-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
public class EPKCompilerLegacy {
public static void _main(String[] args) throws IOException, NoSuchAlgorithmException {
if (args.length != 2) {
System.out.print("Usage: legacy-epkcompiler <input directory> <output file>");
return;
}
File root = new File(args[0]);
ArrayList<File> files = new ArrayList();
System.out.println("Scanning input directory...");
listDirectory(root, files);
ByteArrayOutputStream osb = new ByteArrayOutputStream();
DataOutputStream os = new DataOutputStream(osb);
String start = root.getAbsolutePath();
File output = new File(args[1]);
System.out.println("Compiling: " + output.getAbsolutePath());
os.write("EAGPKG!!".getBytes(StandardCharsets.UTF_8));
os.writeUTF(
"\n\n # eaglercraft package file - assets copyright mojang ab\n # eagler eagler eagler eagler eagler eagler eagler\n\n");
Deflater d = new Deflater(9);
os = new DataOutputStream(new DeflaterOutputStream(osb, d));
MessageDigest md = MessageDigest.getInstance("SHA-1");
for (File f : files) {
os.writeUTF("<file>");
String p = f.getAbsolutePath().replace(start, "").replace('\\', '/');
if (p.startsWith("/"))
p = p.substring(1);
os.writeUTF(p);
InputStream stream = new FileInputStream(f);
byte[] targetArray = new byte[stream.available()];
stream.read(targetArray);
stream.close();
os.write(md.digest(targetArray));
os.writeInt(targetArray.length);
os.write(targetArray);
os.writeUTF("</file>");
}
os.writeUTF(" end");
os.flush();
os.close();
System.out.println("Compiled " + files.size() + " files into the EPK");
System.out.println("Writing to disk...");
FileOutputStream out = new FileOutputStream(output);
out.write(osb.toByteArray());
out.close();
}
public static void listDirectory(File dir, ArrayList<File> files) {
for (File f : dir.listFiles()) {
if (f.isDirectory()) {
listDirectory(f, files);
} else {
files.add(f);
}
}
}
}
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2022-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import net.lax1dude.eaglercraft.bintools.utils.EPKDecompilerSP;
public class EPKDecompiler {
public static void _main(String[] args) throws IOException {
if (args.length != 2) {
System.out.print("Usage: epkdecompiler <input epk> <output folder>");
return;
}
File input = new File(args[0]);
if (!input.isFile()) {
System.err.println("Input file does not exist!");
return;
}
System.out.println("Decompiling: " + input.getAbsolutePath());
File output = new File(args[1]);
byte[] inputBytes = new byte[(int) input.length()];
try (FileInputStream fis = new FileInputStream(input)) {
fis.read(inputBytes);
}
EPKDecompilerSP epkDecompiler = new EPKDecompilerSP(inputBytes);
if (epkDecompiler.isOld()) {
System.out.println("Detected legacy EPK format!");
}
int filesWritten = 0;
try {
EPKDecompilerSP.FileEntry f = null;
while ((f = epkDecompiler.readFile()) != null) {
if (f.type.equals("HEAD")) {
System.out.println("Skipping HEAD: \"" + f.name + "\": \""
+ (new String(f.data, StandardCharsets.US_ASCII)) + "\"");
} else if (f.type.equals("FILE")) {
String safeName = f.name.replace('\\', '/');
if (safeName.startsWith("../") || safeName.contains("/../") || safeName.endsWith("/..")
|| safeName.equals("..")) {
System.out.println("Skipping unsafe relative path: \"" + f.name + "\"");
} else {
File destFile = new File(output, safeName);
File parent = destFile.getParentFile();
if (!parent.isDirectory()) {
if (!parent.mkdirs()) {
throw new IOException("Could not create directory: " + parent.getAbsolutePath());
}
}
try (FileOutputStream fos = new FileOutputStream(destFile)) {
fos.write(f.data);
++filesWritten;
}
}
} else {
System.err.println("Skipping unknown entry type \"" + f.type + "\" name \"" + f.name
+ "\", data is " + f.data.length + " bytes");
}
}
} finally {
epkDecompiler.close();
}
System.out.println("Extracted " + filesWritten + " from the EPK");
}
}
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class EaglerBMPGen {
public static void _main(String[] args) throws IOException {
if (args.length != 3 || (!args[0].equalsIgnoreCase("--red") && !args[0].equalsIgnoreCase("--rgba"))) {
System.out.println("Usage: eagler-bmp-gen <--red|--rgba> <input file> <output file>");
System.out.println("The texture's width and height must be a power of 2!");
return;
}
System.out.println("Reading input file...");
BufferedImage img = ImageIO.read(new File(args[1]));
File output = new File(args[2]);
System.out.println("Encoding Eagler BMP: " + output.getAbsolutePath());
int lvls;
try (FileOutputStream fs = new FileOutputStream(output)) {
ByteArrayOutputStream bao = new ByteArrayOutputStream(0x7FFF);
lvls = encode(img, args[0].equalsIgnoreCase("--rgba"), bao);
fs.write(bao.toByteArray());
}
System.out.println("Wrote " + lvls + " mipmap levels");
}
public static int encode(BufferedImage input, boolean rgba, OutputStream os) throws IOException {
boolean invalid = false;
if (!isPowOf2(input.getWidth())) {
System.err.println("The image's width is not a power of 2!");
invalid = true;
}
if (!isPowOf2(input.getHeight())) {
System.err.println("The image's height is not a power of 2!");
invalid = true;
}
if (invalid) {
throw new IOException("The image has invalid dimensions!");
}
DataOutputStream dos = new DataOutputStream(os);
int lvls = 0;
while (true) {
++lvls;
dos.write('E');
dos.writeShort(input.getWidth());
dos.writeShort(input.getHeight());
for (int y = 0; y < input.getHeight(); ++y) {
for (int x = 0; x < input.getWidth(); ++x) {
int c = input.getRGB(x, input.getHeight() - y - 1);
dos.write((c >>> 16) & 0xFF);
if (rgba) {
dos.write((c >>> 8) & 0xFF);
dos.write((c >>> 0) & 0xFF);
dos.write((c >>> 24) & 0xFF);
}
}
}
if (input.getWidth() > 1 && input.getHeight() > 1) {
BufferedImage img2 = new BufferedImage(input.getWidth() >> 1, input.getHeight() >> 1,
BufferedImage.TYPE_INT_ARGB);
Graphics g = img2.getGraphics();
((Graphics2D) g).setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(input, 0, 0, img2.getWidth(), img2.getHeight(), 0, 0, input.getWidth(), input.getHeight(),
null);
g.dispose();
input = img2;
} else {
dos.write(0);
break;
}
}
return lvls;
}
private static boolean isPowOf2(int x) {
return (x & (x - 1)) == 0;
}
}
@@ -0,0 +1,128 @@
/*
* Copyright (c) 2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
public class EaglerBinaryTools {
public static void main(String[] args) throws Throwable {
if (args.length < 1) {
usage();
return;
}
String[] argz = new String[args.length - 1];
System.arraycopy(args, 1, argz, 0, argz.length);
switch (args[0].toLowerCase()) {
case "epkcompiler":
case "epkcompile":
EPKCompiler._main(argz);
return;
case "legacy-epkcompiler":
case "legacyepkcompiler":
case "legacy-epkcompile":
case "legacyepkcompile":
EPKCompilerLegacy._main(argz);
return;
case "epkdecompiler":
EPKDecompiler._main(argz);
return;
case "obj2mdl-1.5-old":
case "obj2mdl-1.5old":
case "obj2mdl1.5-old":
case "obj2mdl1.5old":
OBJConverter._main(argz, false);
return;
case "obj2mdl-1.8-old":
case "obj2mdl-1.8old":
case "obj2mdl1.8-old":
case "obj2mdl-old":
case "obj2mdlold":
OBJConverter._main(argz, true);
return;
case "obj2mdl-1.5-new":
case "obj2mdl-1.5new":
case "obj2mdl1.5-new":
case "obj2mdl1.5new":
case "obj2mdl-1.5":
case "obj2mdl1.5":
OptimizedOBJConverter._main(argz, false);
return;
case "obj2mdl-1.8-new":
case "obj2mdl-1.8new":
case "obj2mdl1.8-new":
case "obj2mdl1.8new":
case "obj2mdl-new":
case "obj2mdlnew":
case "obj2mdl-1.8":
case "obj2mdl1.8":
case "obj2mdl":
OptimizedOBJConverter._main(argz, true);
return;
case "ebp-encode":
case "ebpencode":
case "ebp-encoder":
case "ebpencoder":
EBPFileEncoder._main(argz);
return;
case "legacy-ebp-encode":
case "legacyebpencode":
case "legacy-ebp-encoder":
case "legacyebpencoder":
EBPFileEncoderLegacy._main(argz);
return;
case "ebp-decode":
case "ebpdecode":
case "ebp-decoder":
case "ebpdecoder":
EBPFileDecoder._main(argz);
return;
case "skybox-gen":
case "skyboxgen":
SkyboxGen._main(argz);
return;
case "light-mesh-gen":
case "lightmeshgen":
LightMeshGen._main(argz);
return;
case "eagler-bmp-gen":
case "eaglerbmpgen":
EaglerBMPGen._main(argz);
return;
default:
usage();
return;
}
}
private static void usage() {
System.out.println(
"Usage: java -jar EaglerBinaryTools.jar <epkcompiler|legacy-epkcompiler|epkdecompiler|obj2mdl-1.5|obj2mdl-1.8|ebp-encode|ebp-decode|skybox-gen|light-mesh-gen|eagler-bmp-gen> [args...]");
System.out.println(" - 'epkcompiler': Compile an EPK file from a folder");
System.out.println(" - 'legacy-epkcompiler': Compile an EPK file in legacy format");
System.out.println(" - 'epkdecompiler': Decompile an EPK file into a folder");
System.out.println(" - 'obj2mdl-1.5': Compile FNAW skin MDL file for 1.5");
System.out.println(" - 'obj2mdl-1.8': Compile FNAW skin MDL file for 1.8");
System.out.println(" - 'obj2mdl-1.5-old': Compile FNAW skin MDL file for 1.5 using the old OBJ converter");
System.out.println(" - 'obj2mdl-1.8-old': Compile FNAW skin MDL file for 1.8 using the old OBJ converter");
System.out.println(" - 'ebp-encode': Encode EBP file from PNG");
System.out.println(" - 'legacy-ebp-encode': Encode EBP file from PNG in legacy format");
System.out.println(" - 'ebp-decode': Decode EBP file to PNG");
System.out.println(" - 'skybox-gen': Generate skybox.dat from OBJ for shader packs");
System.out.println(" - 'light-mesh-gen': Generate light_point_mesh.dat from OBJ for shader packs");
System.out.println(" - 'eagler-bmp-gen': Generate moon and lens flare BMP textures from PNG for shader packs");
}
}
@@ -0,0 +1,159 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import net.lax1dude.eaglercraft.bintools.utils.IEEE754;
public class LightMeshGen {
public static void _main(String[] args) throws IOException {
if (args.length != 2 && args.length != 3) {
System.out.println("Usage: light-mesh-gen <obj file> <output file> [mesh name]");
System.out.println("Input file format is Wavefront OBJ file exported from your 3D modeling program");
System.out.println("The only mesh name currently used by eagler is \"light_point_mesh\"");
return;
}
System.out.println("Reading input file...");
List<String> lns = new ArrayList();
try (BufferedReader bu = new BufferedReader(new FileReader(new File(args[0])))) {
String s;
while ((s = bu.readLine()) != null) {
lns.add(s);
}
}
File output = new File(args[1]);
System.out.println("Exporting light mesh: " + output.getAbsolutePath());
try (FileOutputStream fs = new FileOutputStream(output)) {
convertModel(lns, args.length == 3 ? args[2] : "light_point_mesh", fs);
}
System.out.println("Light mesh export complete!");
}
public static void convertModel(Collection<String> lines, String name, OutputStream out) throws IOException {
List<float[]> vertexes = new ArrayList<float[]>();
List<int[][]> faces = new ArrayList<int[][]>();
List<byte[]> vboentries = new ArrayList<byte[]>();
List<float[]> vboentriesF = new ArrayList<float[]>();
List<byte[]> indexablevboentries = new ArrayList<byte[]>();
List<Integer> indexbuffer = new ArrayList<Integer>();
for (String ul : lines) {
String[] l = ul.split(" ");
if (l[0].equals("v")) {
vertexes.add(new float[] { Float.parseFloat(l[1]), Float.parseFloat(l[2]), Float.parseFloat(l[3]) });
}
if (l[0].equals("f")) {
if (l.length != 4) {
OBJConverter.printTriangulationMessage();
throw new IOException("Incompatible model! (This can be fixed)");
}
String[] v1 = l[1].split("/");
String[] v2 = l[2].split("/");
String[] v3 = l[3].split("/");
faces.add(new int[][] { { Integer.parseInt(v1[0]), Integer.parseInt(v1[1]), Integer.parseInt(v1[2]) },
{ Integer.parseInt(v2[0]), Integer.parseInt(v2[1]), Integer.parseInt(v2[2]) },
{ Integer.parseInt(v3[0]), Integer.parseInt(v3[1]), Integer.parseInt(v3[2]) } });
}
}
int w = 32;
int h = 16;
int[] normalsLookupTexture = new int[w * h];
int normalsId = 0;
for (int[][] f : faces) {
for (int i = 0; i < 3; i++) {
byte[] b = new byte[6];
float[] v = vertexes.get(f[i][0] - 1);
int ix = IEEE754.encodeHalfFloat(v[0]);
int iy = IEEE754.encodeHalfFloat(v[1]);
int iz = IEEE754.encodeHalfFloat(v[2]);
int idx = 0;
b[idx++] = (byte) (ix);
b[idx++] = (byte) (ix >> 8);
b[idx++] = (byte) (iy);
b[idx++] = (byte) (iy >> 8);
b[idx++] = (byte) (iz);
b[idx++] = (byte) (iz >> 8);
vboentries.add(b);
vboentriesF.add(v);
}
}
for (int j = 0; j < vboentries.size(); ++j) {
byte v[] = vboentries.get(j);
int l = indexablevboentries.size();
boolean flag = true;
for (int i = 0; i < l; i++) {
if (Arrays.equals(v, indexablevboentries.get(i))) {
indexbuffer.add(i);
flag = false;
break;
}
}
if (flag) {
if (l > 255) {
throw new UnsupportedOperationException("Too many vertices!");
}
indexbuffer.add(l);
indexablevboentries.add(v);
}
}
DataOutputStream o = new DataOutputStream(out);
o.write(0xEE);
o.write(0xAA);
o.write(0x66);
o.write('%');
o.write(name.length());
o.write(name.getBytes(StandardCharsets.US_ASCII));
o.writeInt(indexablevboentries.size());
for (int i = 0; i < indexablevboentries.size(); ++i) {
byte[] b = indexablevboentries.get(i);
o.write(b, 0, b.length);
}
o.writeInt(indexbuffer.size());
o.write(1); // 1 = byte, 2 = short, 4 = int
for (int i : indexbuffer) {
o.write(i & 0xFF);
}
o.close();
}
}
@@ -0,0 +1,215 @@
/*
* Copyright (c) 2022-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
@Deprecated
public class OBJConverter {
public static void _main(String[] args, boolean v1_8) throws IOException {
if (args.length != 3) {
System.out
.println("Usage: obj2mdl-" + (v1_8 ? "1.8" : "1.5") + " <input file> <output file> <texture mode>");
System.out.println("Input file format is Wavefront OBJ file exported from your 3D modeling program");
System.out.println(
"Texture mode can be 'true' or 'false' to enable/disable exporting texture UV coordinates");
return;
}
System.out.println("Reading input file...");
List<String> lns = new ArrayList();
try (BufferedReader bu = new BufferedReader(new FileReader(new File(args[0])))) {
String s;
while ((s = bu.readLine()) != null) {
lns.add(s);
}
}
File output = new File(args[1]);
System.out.println("Exporting " + (v1_8 ? "1.8" : "1.5") + " MDL: " + output.getAbsolutePath());
boolean tex = args[2].equalsIgnoreCase("true") || args[2].equals("1");
if (!output.exists()) {
if (output.getParentFile() != null)
if (!output.getParentFile().exists())
if (!output.getParentFile().mkdirs())
throw new RuntimeException("Failed to create parent dir!");
if (!output.createNewFile())
throw new RuntimeException("Failed to create file!");
}
try (FileOutputStream fs = new FileOutputStream(output, false)) {
convertModel(lns, fs, tex, v1_8);
}
if (tex) {
System.out.println("Export with UVs completed!");
} else {
System.out.println("Export without UVs competed!");
}
}
public static void convertModel(Collection<String> lines, OutputStream out, boolean textureMode, boolean v1_8)
throws IOException {
List<float[]> vertexes = new ArrayList<float[]>();
List<byte[]> normals = new ArrayList<byte[]>();
List<float[]> texcoords = new ArrayList<float[]>();
List<int[][]> faces = new ArrayList<int[][]>();
List<byte[]> vboentries = new ArrayList<byte[]>();
List<byte[]> indexablevboentries = new ArrayList<byte[]>();
List<Integer> indexbuffer = new ArrayList<Integer>();
for (String ul : lines) {
String[] l = ul.split(" ");
if (l[0].equals("v")) {
vertexes.add(new float[] { Float.parseFloat(l[1]), Float.parseFloat(l[2]), Float.parseFloat(l[3]) });
}
if (l[0].equals("vn")) {
int dumb = v1_8 ? 0 : 127;
normals.add(new byte[] { (byte) ((int) (Float.parseFloat(l[1]) * 127.0F) + dumb),
(byte) ((int) (Float.parseFloat(l[2]) * 127.0F) + dumb),
(byte) ((int) (Float.parseFloat(l[3]) * 127.0F) + dumb), (byte) 0 });
}
if (textureMode) {
if (l[0].equals("vt")) {
texcoords.add(new float[] { Float.parseFloat(l[1]), 1.0f - Float.parseFloat(l[2]) });
}
}
if (l[0].equals("f")) {
if (l.length != 4) {
printTriangulationMessage();
throw new IOException("Incompatible model! (This can be fixed)");
}
String[] v1 = l[1].split("/");
String[] v2 = l[2].split("/");
String[] v3 = l[3].split("/");
faces.add(new int[][] { { Integer.parseInt(v1[0]), Integer.parseInt(v1[1]), Integer.parseInt(v1[2]) },
{ Integer.parseInt(v2[0]), Integer.parseInt(v2[1]), Integer.parseInt(v2[2]) },
{ Integer.parseInt(v3[0]), Integer.parseInt(v3[1]), Integer.parseInt(v3[2]) } });
}
}
for (int[][] f : faces) {
for (int i = 0; i < 3; i++) {
byte[] b = new byte[textureMode ? 24 : 16];
float[] v = vertexes.get(f[i][0] - 1);
int ix = Float.floatToRawIntBits(v[0]);
int iy = Float.floatToRawIntBits(v[1]);
int iz = Float.floatToRawIntBits(v[2]);
int idx = 0;
b[idx++] = (byte) (ix);
b[idx++] = (byte) (ix >> 8);
b[idx++] = (byte) (ix >> 16);
b[idx++] = (byte) (ix >> 24);
b[idx++] = (byte) (iy);
b[idx++] = (byte) (iy >> 8);
b[idx++] = (byte) (iy >> 16);
b[idx++] = (byte) (iy >> 24);
b[idx++] = (byte) (iz);
b[idx++] = (byte) (iz >> 8);
b[idx++] = (byte) (iz >> 16);
b[idx++] = (byte) (iz >> 24);
byte[] n = normals.get(f[i][2] - 1);
b[idx++] = n[0];
b[idx++] = n[1];
b[idx++] = n[2];
b[idx++] = n[3];
if (textureMode) {
float[] t = texcoords.get(f[i][1] - 1);
int ix3 = Float.floatToRawIntBits(t[0]);
int iy3 = Float.floatToRawIntBits(t[1]);
b[idx++] = (byte) (ix3);
b[idx++] = (byte) (ix3 >> 8);
b[idx++] = (byte) (ix3 >> 16);
b[idx++] = (byte) (ix3 >> 24);
b[idx++] = (byte) (iy3);
b[idx++] = (byte) (iy3 >> 8);
b[idx++] = (byte) (iy3 >> 16);
b[idx++] = (byte) (iy3 >> 24);
}
vboentries.add(b);
}
}
for (int j = 0; j < vboentries.size(); ++j) {
byte v[] = vboentries.get(j);
int l = indexablevboentries.size();
boolean flag = true;
for (int i = 0; i < l; i++) {
if (Arrays.equals(v, indexablevboentries.get(i))) {
indexbuffer.add(i);
flag = false;
break;
}
}
if (flag) {
if (l > 65535) {
throw new IOException("Too many vertices!");
}
indexbuffer.add(l);
indexablevboentries.add(v);
}
}
DataOutputStream o = new DataOutputStream(out);
o.write((v1_8 ? "!EAG$mdl" : "!EAG%mdl").getBytes(StandardCharsets.US_ASCII));
o.write(textureMode ? (byte) 'T' : (byte) 'C');
o.writeUTF("\n\nthis file was generated with EaglerBinaryTools\n\n");
o.writeInt(indexablevboentries.size());
o.writeInt(indexbuffer.size());
for (int i = 0; i < indexablevboentries.size(); ++i) {
byte[] b = indexablevboentries.get(i);
o.write(b, 0, b.length);
}
for (int i : indexbuffer) {
o.write((byte) i);
o.write((byte) (i >> 8));
}
o.write("_:>+".getBytes(StandardCharsets.US_ASCII));
o.close();
}
static void printTriangulationMessage() {
System.err.println("=====================================");
System.err.println("THIS OBJ FILE IS NOT COMPATIBLE WITH EAGLERCRAFT!");
System.err.println();
System.err.println("Eaglercraft meshes can only have triangles in them.");
System.err.println();
System.err.println("If you're using Blender, add the \"triangulate\"");
System.err.println("modifier to the mesh before exporting to fix");
System.err.println("=====================================");
}
}
@@ -0,0 +1,306 @@
package net.lax1dude.eaglercraft.bintools;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class OptimizedOBJConverter {
public static void _main(String[] args, boolean v1_8) throws IOException {
if (args.length != 3) {
System.out
.println("Usage: obj2mdl-" + (v1_8 ? "1.8" : "1.5") + " <input file> <output file> <texture mode>");
System.out.println("Input file format is Wavefront OBJ file exported from your 3D modeling program");
System.out.println(
"Texture mode can be 'true' or 'false' to enable/disable exporting texture UV coordinates");
return;
}
System.out.println("Reading input file...");
List<String> lns = new ArrayList();
try (BufferedReader bu = new BufferedReader(new FileReader(new File(args[0])))) {
String s;
while ((s = bu.readLine()) != null) {
lns.add(s);
}
}
File output = new File(args[1]);
System.out.println("Exporting " + (v1_8 ? "1.8" : "1.5") + " MDL: " + output.getAbsolutePath());
boolean tex = args[2].equalsIgnoreCase("true") || args[2].equals("1");
if (!output.exists()) {
if (output.getParentFile() != null)
if (!output.getParentFile().exists())
if (!output.getParentFile().mkdirs())
throw new RuntimeException("Failed to create parent dir!");
if (!output.createNewFile())
throw new RuntimeException("Failed to create file!");
}
try (FileOutputStream fs = new FileOutputStream(output)) {
convertModel(lns, fs, tex, v1_8);
}
if (tex) {
System.out.println("Export with UVs completed!");
} else {
System.out.println("Export without UVs competed!");
}
}
public static void convertModel(Collection<String> lines, OutputStream out, boolean textureMode, boolean v1_8)
throws IOException {
List<float[]> vertexes = new ArrayList<float[]>();
List<byte[]> normals = new ArrayList<byte[]>();
List<float[]> texcoords = new ArrayList<float[]>();
List<int[][]> faces = new ArrayList<int[][]>();
List<byte[]> vboentries = new ArrayList<byte[]>();
IndexLinkedHashSet<ByteArray> indexablevboentries = new IndexLinkedHashSet<>();
List<Integer> indexbuffer = new ArrayList<Integer>();
for (String ul : lines) {
String[] l = ul.split(" ");
if (l[0].equals("v")) {
vertexes.add(new float[] { Float.parseFloat(l[1]), Float.parseFloat(l[2]), Float.parseFloat(l[3]) });
}
if (l[0].equals("vn")) {
int dumb = v1_8 ? 0 : 127;
normals.add(new byte[] { (byte) ((int) (Float.parseFloat(l[1]) * 127.0F) + dumb),
(byte) ((int) (Float.parseFloat(l[2]) * 127.0F) + dumb),
(byte) ((int) (Float.parseFloat(l[3]) * 127.0F) + dumb), (byte) 0 });
}
if (textureMode) {
if (l[0].equals("vt")) {
texcoords.add(new float[] { Float.parseFloat(l[1]), 1.0f - Float.parseFloat(l[2]) });
}
}
if (l[0].equals("f")) {
if (l.length != 4) {
printTriangulationMessage();
throw new IOException("Incompatible model! (This can be fixed)");
}
String[] v1 = l[1].split("/");
String[] v2 = l[2].split("/");
String[] v3 = l[3].split("/");
faces.add(new int[][] { { Integer.parseInt(v1[0]), Integer.parseInt(v1[1]), Integer.parseInt(v1[2]) },
{ Integer.parseInt(v2[0]), Integer.parseInt(v2[1]), Integer.parseInt(v2[2]) },
{ Integer.parseInt(v3[0]), Integer.parseInt(v3[1]), Integer.parseInt(v3[2]) } });
}
}
for (int[][] f : faces) {
for (int i = 0; i < 3; i++) {
byte[] b = new byte[textureMode ? 24 : 16];
float[] v = vertexes.get(f[i][0] - 1);
int ix = Float.floatToRawIntBits(v[0]);
int iy = Float.floatToRawIntBits(v[1]);
int iz = Float.floatToRawIntBits(v[2]);
int idx = 0;
b[idx++] = (byte) (ix);
b[idx++] = (byte) (ix >> 8);
b[idx++] = (byte) (ix >> 16);
b[idx++] = (byte) (ix >> 24);
b[idx++] = (byte) (iy);
b[idx++] = (byte) (iy >> 8);
b[idx++] = (byte) (iy >> 16);
b[idx++] = (byte) (iy >> 24);
b[idx++] = (byte) (iz);
b[idx++] = (byte) (iz >> 8);
b[idx++] = (byte) (iz >> 16);
b[idx++] = (byte) (iz >> 24);
byte[] n = normals.get(f[i][2] - 1);
b[idx++] = n[0];
b[idx++] = n[1];
b[idx++] = n[2];
b[idx++] = n[3];
if (textureMode) {
float[] t = texcoords.get(f[i][1] - 1);
int ix3 = Float.floatToRawIntBits(t[0]);
int iy3 = Float.floatToRawIntBits(t[1]);
b[idx++] = (byte) (ix3);
b[idx++] = (byte) (ix3 >> 8);
b[idx++] = (byte) (ix3 >> 16);
b[idx++] = (byte) (ix3 >> 24);
b[idx++] = (byte) (iy3);
b[idx++] = (byte) (iy3 >> 8);
b[idx++] = (byte) (iy3 >> 16);
b[idx++] = (byte) (iy3 >> 24);
}
vboentries.add(b);
}
}
for (byte[] v : vboentries) {
int l = indexablevboentries.size();
// HashSet is amortized constant time for contains
// (IndexLinkedHashSet is backed by an arraylist for indices and a HashSet for
// performance)
if (indexablevboentries.contains(ByteArray.of(v))) {
indexbuffer.add(indexablevboentries.indexOf(ByteArray.of(v)));
} else {
if (l > 65535)
throw new RuntimeException("Too many vertices!");
indexbuffer.add(l);
indexablevboentries.add(ByteArray.of(v));
}
}
DataOutputStream o = new DataOutputStream(out);
o.write((v1_8 ? "!EAG$mdl" : "!EAG%mdl").getBytes(StandardCharsets.US_ASCII));
o.write(textureMode ? (byte) 'T' : (byte) 'C');
o.writeUTF("\n\nthis file was generated with EaglerBinaryTools\n\n");
o.writeInt(indexablevboentries.size());
o.writeInt(indexbuffer.size());
for (int i = 0; i < indexablevboentries.size(); ++i) {
byte[] b = indexablevboentries.get(i).array;
o.write(b, 0, b.length);
}
for (int i : indexbuffer) {
o.write((byte) i);
o.write((byte) (i >> 8));
}
o.write("_:>+".getBytes(StandardCharsets.US_ASCII));
o.close();
}
static void printTriangulationMessage() {
System.err.println("=====================================");
System.err.println("THIS OBJ FILE IS NOT COMPATIBLE WITH EAGLERCRAFT!");
System.err.println();
System.err.println("Eaglercraft meshes can only have triangles in them.");
System.err.println();
System.err.println("If you're using Blender, add the \"triangulate\"");
System.err.println("modifier to the mesh before exporting to fix");
System.err.println("=====================================");
}
/*
* Copied from https://stackoverflow.com/a/77179230
*/
public static final class IndexLinkedHashSet<E> implements Set<E> {
private final ArrayList<E> list = new ArrayList<>();
private final HashSet<E> set = new HashSet<>();
public synchronized boolean add(E e) {
if (set.add(e)) {
return list.add(e);
}
return false;
}
public synchronized boolean remove(Object o) {
if (set.remove(o)) {
return list.remove(o);
}
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return set.containsAll(c);
}
public synchronized void clear() {
set.clear();
list.clear();
}
public synchronized E get(int index) {
return list.get(index);
}
// Added this function
public synchronized int indexOf(E element) {
return list.indexOf(element);
}
//
public synchronized boolean removeAll(Collection<?> c) {
if (set.removeAll(c)) {
return list.removeAll(c);
}
return true;
}
public synchronized boolean retainAll(Collection<?> c) {
if (set.retainAll(c)) {
return list.retainAll(c);
}
return false;
}
public synchronized boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
@Override
public synchronized int size() {
return set.size();
}
@Override
public synchronized boolean isEmpty() {
return set.isEmpty();
}
@Override
public synchronized boolean contains(Object o) {
return set.contains(o);
}
@Override
public synchronized Iterator<E> iterator() {
return list.iterator();
}
@Override
public synchronized Object[] toArray() {
return list.toArray();
}
@Override
public synchronized <T> T[] toArray(T[] a) {
return list.toArray(a);
}
}
public static class ByteArray {
public final byte[] array;
public ByteArray(byte[] array) {
this.array = array;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ByteArray byteArray = (ByteArray) o;
return Arrays.equals(array, byteArray.array);
}
@Override
public int hashCode() {
return Arrays.hashCode(array);
}
public static ByteArray of(byte... bytes) {
return new ByteArray(bytes);
}
}
}
@@ -0,0 +1,297 @@
/*
* Copyright (c) 2023-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import net.lax1dude.eaglercraft.bintools.utils.IEEE754;
public class SkyboxGen {
public static void _main(String[] args) throws IOException {
if (args.length != 3) {
System.out.println("Usage: skybox-gen <top OBJ> <bottom OBJ> <output file>");
System.out.println("Input file format is Wavefront OBJ file exported from your 3D modeling program");
System.out.println("The top and bottom shapes should combine to form one seamless dome/sphere when");
System.out.println("both are rendered in the same position. Make sure to flip the normals!");
return;
}
System.out.println("Reading top half...");
List<String> lnsTop = new ArrayList();
try (BufferedReader bu = new BufferedReader(new FileReader(new File(args[0])))) {
String s;
while ((s = bu.readLine()) != null) {
lnsTop.add(s);
}
}
System.out.println("Reading bottom half...");
List<String> lnsBottom = new ArrayList();
try (BufferedReader bu = new BufferedReader(new FileReader(new File(args[1])))) {
String s;
while ((s = bu.readLine()) != null) {
lnsBottom.add(s);
}
}
File output = new File(args[2]);
System.out.println("Exporting skybox.dat: " + output.getAbsolutePath());
try (FileOutputStream fs = new FileOutputStream(output)) {
convertModel(lnsTop, lnsBottom, fs);
}
System.out.println("Skybox export complete!");
}
public static void convertModel(Collection<String> linesTop, Collection<String> linesBottom, OutputStream out)
throws IOException {
List<float[]> vertexes = new ArrayList<float[]>();
List<int[][]> faces = new ArrayList<int[][]>();
List<byte[]> vboentriesTop = new ArrayList<byte[]>();
List<float[]> vboentriesFTop = new ArrayList<float[]>();
List<byte[]> indexablevboentries = new ArrayList<byte[]>();
List<Integer> indexbuffer = new ArrayList<Integer>();
for (String ul : linesTop) {
String[] l = ul.split(" ");
if (l[0].equals("v")) {
vertexes.add(new float[] { Float.parseFloat(l[1]), Float.parseFloat(l[2]), Float.parseFloat(l[3]) });
}
if (l[0].equals("f")) {
if (l.length != 4) {
OBJConverter.printTriangulationMessage();
throw new IOException("Incompatible top model! (This can be fixed)");
}
String[] v1 = l[1].split("/");
String[] v2 = l[2].split("/");
String[] v3 = l[3].split("/");
faces.add(new int[][] { { Integer.parseInt(v1[0]), Integer.parseInt(v1[1]), Integer.parseInt(v1[2]) },
{ Integer.parseInt(v2[0]), Integer.parseInt(v2[1]), Integer.parseInt(v2[2]) },
{ Integer.parseInt(v3[0]), Integer.parseInt(v3[1]), Integer.parseInt(v3[2]) } });
}
}
for (int[][] f : faces) {
for (int i = 0; i < 3; i++) {
byte[] b = new byte[6];
float[] v = vertexes.get(f[i][0] - 1);
int ix = IEEE754.encodeHalfFloat(v[0]);
int iy = IEEE754.encodeHalfFloat(v[1]);
int iz = IEEE754.encodeHalfFloat(v[2]);
int idx = 0;
b[idx++] = (byte) (ix);
b[idx++] = (byte) (ix >> 8);
b[idx++] = (byte) (iy);
b[idx++] = (byte) (iy >> 8);
b[idx++] = (byte) (iz);
b[idx++] = (byte) (iz >> 8);
vboentriesTop.add(b);
vboentriesFTop.add(v);
}
}
vertexes.clear();
faces.clear();
List<byte[]> vboentriesBottom = new ArrayList<byte[]>();
List<float[]> vboentriesFBottom = new ArrayList<float[]>();
for (String ul : linesBottom) {
String[] l = ul.split(" ");
if (l[0].equals("v")) {
vertexes.add(new float[] { Float.parseFloat(l[1]), Float.parseFloat(l[2]), Float.parseFloat(l[3]) });
}
if (l[0].equals("f")) {
if (l.length != 4) {
OBJConverter.printTriangulationMessage();
throw new IOException("Incompatible bottom model! (This can be fixed)");
}
String[] v1 = l[1].split("/");
String[] v2 = l[2].split("/");
String[] v3 = l[3].split("/");
faces.add(new int[][] { { Integer.parseInt(v1[0]), Integer.parseInt(v1[1]), Integer.parseInt(v1[2]) },
{ Integer.parseInt(v2[0]), Integer.parseInt(v2[1]), Integer.parseInt(v2[2]) },
{ Integer.parseInt(v3[0]), Integer.parseInt(v3[1]), Integer.parseInt(v3[2]) } });
}
}
for (int[][] f : faces) {
for (int i = 0; i < 3; i++) {
byte[] b = new byte[6];
float[] v = vertexes.get(f[i][0] - 1);
int ix = IEEE754.encodeHalfFloat(v[0]);
int iy = IEEE754.encodeHalfFloat(v[1]);
int iz = IEEE754.encodeHalfFloat(v[2]);
int idx = 0;
b[idx++] = (byte) (ix);
b[idx++] = (byte) (ix >> 8);
b[idx++] = (byte) (iy);
b[idx++] = (byte) (iy >> 8);
b[idx++] = (byte) (iz);
b[idx++] = (byte) (iz >> 8);
vboentriesBottom.add(b);
vboentriesFBottom.add(v);
}
}
int w = 32;
int h = 16;
int[] normalsLookupTexture = new int[w * h];
int normalsId = 0;
for (int j = 0; j < vboentriesTop.size(); ++j) {
byte v[] = vboentriesTop.get(j);
int l = indexablevboentries.size();
boolean flag = true;
e: for (int i = 0; i < l; i++) {
byte[] bb = indexablevboentries.get(i);
for (int k = 0; k < v.length; ++k) {
if (bb[k] != v[k]) {
continue e;
}
}
indexbuffer.add(i);
flag = false;
break;
}
if (flag) {
if (l > 65535) {
throw new IOException("Too many vertices!");
}
indexbuffer.add(l);
byte[] vboWithLUT = new byte[v.length + 2];
vboWithLUT[v.length + 0] = (byte) (int) (((normalsId % w) + 0.5f) / (float) w * 255.0f);
vboWithLUT[v.length + 1] = (byte) (int) (((normalsId / w) + 0.5f) / (float) h * 255.0f);
System.arraycopy(v, 0, vboWithLUT, 0, v.length);
float[] vv = vboentriesFTop.get(j);
float xx = vv[0];
float yy = vv[1];
float zz = vv[2];
float len = (float) Math.sqrt(xx * xx + yy * yy + zz * zz);
xx /= len;
yy /= len;
zz /= len;
normalsLookupTexture[normalsId] = 0xFF000000 | ((int) ((xx + 1.0f) * 0.5f * 255.0f) << 16)
| ((int) ((yy + 1.0f) * 0.5f * 255.0f) << 8) | (int) ((zz + 1.0f) * 0.5f * 255.0f);
indexablevboentries.add(vboWithLUT);
++normalsId;
}
}
for (int j = 0; j < vboentriesBottom.size(); ++j) {
byte v[] = vboentriesBottom.get(j);
int l = indexablevboentries.size();
boolean flag = true;
e: for (int i = 0; i < l; i++) {
byte[] bb = indexablevboentries.get(i);
for (int k = 0; k < v.length; ++k) {
if (bb[k] != v[k]) {
continue e;
}
}
indexbuffer.add(i);
flag = false;
break;
}
if (flag) {
if (l > 65535) {
throw new IOException("Too many vertices!");
}
indexbuffer.add(l);
byte[] vboWithLUT = new byte[v.length + 2];
vboWithLUT[v.length + 0] = (byte) (int) (((normalsId % w) + 0.5f) / (float) w * 255.0f);
vboWithLUT[v.length + 1] = (byte) (int) (((normalsId / w) + 0.5f) / (float) h * 255.0f);
System.arraycopy(v, 0, vboWithLUT, 0, v.length);
float[] vv = vboentriesFBottom.get(j);
float xx = vv[0];
float yy = vv[1];
float zz = vv[2];
float len = (float) Math.sqrt(xx * xx + yy * yy + zz * zz);
xx /= len;
yy /= len;
zz /= len;
normalsLookupTexture[normalsId] = 0xFF000000 | ((int) ((xx + 1.0f) * 0.5f * 255.0f) << 16)
| ((int) ((yy + 1.0f) * 0.5f * 255.0f) << 8) | (int) ((zz + 1.0f) * 0.5f * 255.0f);
indexablevboentries.add(vboWithLUT);
++normalsId;
}
}
DataOutputStream o = new DataOutputStream(out);
o.write(0xEE);
o.write(0xAA);
o.write(0x66);
o.write('%');
o.write(6);
o.write("skybox".getBytes(StandardCharsets.US_ASCII));
o.writeShort(w);
o.writeShort(h);
for (int i = 0; i < normalsLookupTexture.length; ++i) {
o.write((normalsLookupTexture[i] >> 16) & 0xFF);
o.write((normalsLookupTexture[i] >> 8) & 0xFF);
o.write(normalsLookupTexture[i] & 0xFF);
o.write((normalsLookupTexture[i] >> 24) & 0xFF);
}
o.writeInt(0);
o.writeInt(vboentriesTop.size());
o.writeInt(vboentriesTop.size());
o.writeInt(vboentriesBottom.size());
o.writeInt(indexablevboentries.size());
for (int i = 0; i < indexablevboentries.size(); ++i) {
byte[] b = indexablevboentries.get(i);
o.write(b, 0, b.length);
}
o.writeInt(indexbuffer.size());
o.write(2); // 1 = byte, 2 = short, 4 = int
for (int i : indexbuffer) {
o.write(i & 0xFF);
o.write((i >> 8) & 0xFF);
}
o.close();
}
}
@@ -0,0 +1,257 @@
/*
* Copyright (c) 2022-2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools.utils;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
public class EPKDecompilerSP {
public static class FileEntry {
public final String type;
public final String name;
public final byte[] data;
protected FileEntry(String type, String name, byte[] data) {
this.type = type;
this.name = name;
this.data = data;
}
}
private ByteArrayInputStream in2;
private DataInputStream in;
private InputStream zis;
private MessageDigest dg;
private CRC32 crc32;
private int numFiles;
private boolean isFinished = false;
private boolean isOldFormat = false;
/**
* This class is from the Eaglercraft 1.5 client, with a few modifications
*/
public EPKDecompilerSP(byte[] data) throws IOException {
in2 = new ByteArrayInputStream(data);
byte[] header = new byte[8];
in2.read(header);
if (Arrays.equals(header, new byte[] { (byte) 69, (byte) 65, (byte) 71, (byte) 80, (byte) 75, (byte) 71,
(byte) 36, (byte) 36 })) {
byte[] endCode = new byte[] { (byte) ':', (byte) ':', (byte) ':', (byte) 'Y', (byte) 'E', (byte) 'E',
(byte) ':', (byte) '>' };
for (int i = 0; i < 8; ++i) {
if (data[data.length - 8 + i] != endCode[i]) {
throw new IOException("EPK file is missing EOF code (:::YEE:>)");
}
}
in2 = new ByteArrayInputStream(data, 8, data.length - 16);
initNew();
} else if (Arrays.equals(header, new byte[] { (byte) 69, (byte) 65, (byte) 71, (byte) 80, (byte) 75, (byte) 71,
(byte) 33, (byte) 33 })) {
initOld();
}
}
public boolean isOld() {
return isOldFormat;
}
public FileEntry readFile() throws IOException {
if (!isOldFormat) {
return readFileNew();
} else {
return readFileOld();
}
}
private void initNew() throws IOException {
InputStream is = in2;
String vers = readASCII(is);
if (!vers.startsWith("ver2.")) {
throw new IOException("Unknown or invalid EPK version: " + vers);
}
is.skip(is.read()); // skip filename
is.skip(loadShort(is)); // skip comment
is.skip(8); // skip millis date
numFiles = loadInt(is);
char compressionType = (char) is.read();
switch (compressionType) {
case 'G':
zis = new GZIPInputStream(is);
break;
case 'Z':
zis = new InflaterInputStream(is);
break;
case '0':
zis = is;
break;
default:
throw new IOException("Invalid or unsupported EPK compression: " + compressionType);
}
crc32 = new CRC32();
}
private FileEntry readFileNew() throws IOException {
if (isFinished) {
return null;
}
byte[] typeBytes = new byte[4];
IOUtils.readFully(zis, typeBytes);
String type = readASCII(typeBytes);
if (numFiles == 0) {
if (!"END$".equals(type)) {
throw new IOException("EPK file is missing END code (END$)");
}
zis.close();
isFinished = true;
return null;
} else {
if ("END$".equals(type)) {
throw new IOException("Unexpected END when there are still " + numFiles + " files remaining");
} else {
String name = readASCII(zis);
int len = loadInt(zis);
byte[] data;
if ("FILE".equals(type)) {
if (len < 5) {
throw new IOException("File '" + name + "' is incomplete (no crc)");
}
int loadedCrc = loadInt(zis);
data = new byte[len - 5];
IOUtils.readFully(zis, data);
crc32.reset();
crc32.update(data, 0, data.length);
if ((int) crc32.getValue() != loadedCrc) {
throw new IOException("File '" + name + "' has an invalid checksum");
}
if (zis.read() != ':') {
throw new IOException("File '" + name + "' is incomplete");
}
} else {
data = new byte[len];
IOUtils.readFully(zis, data);
}
if (zis.read() != '>') {
throw new IOException("Object '" + name + "' is incomplete");
}
--numFiles;
return new FileEntry(type, name, data);
}
}
}
private static final int loadShort(InputStream is) throws IOException {
return (is.read() << 8) | is.read();
}
private static final int loadInt(InputStream is) throws IOException {
return (is.read() << 24) | (is.read() << 16) | (is.read() << 8) | is.read();
}
public static final String readASCII(byte[] bytesIn) throws IOException {
char[] charIn = new char[bytesIn.length];
for (int i = 0; i < bytesIn.length; ++i) {
charIn[i] = (char) ((int) bytesIn[i] & 0xFF);
}
return new String(charIn);
}
private static final String readASCII(InputStream bytesIn) throws IOException {
int len = bytesIn.read();
char[] charIn = new char[len];
for (int i = 0; i < len; ++i) {
charIn[i] = (char) (bytesIn.read() & 0xFF);
}
return new String(charIn);
}
private void initOld() throws IOException {
isOldFormat = true;
try {
dg = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException("SHA-1 is not supported in this JRE!", ex);
}
in = new DataInputStream(in2);
in.readUTF();
in = new DataInputStream(new InflaterInputStream(in2));
}
private FileEntry readFileOld() throws IOException {
if (isFinished) {
return null;
}
String s = in.readUTF();
if (s.equals(" end")) {
isFinished = true;
in.close();
return null;
} else if (!s.equals("<file>")) {
throw new IOException("invalid epk file");
}
String path = in.readUTF();
byte[] digest = new byte[20];
IOUtils.readFully(in, digest);
int len = in.readInt();
byte[] file = new byte[len];
IOUtils.readFully(in, file);
byte[] digest2 = dg.digest(file);
if (!Arrays.equals(digest, digest2))
throw new IOException("invalid file hash for " + path);
if (!"</file>".equals(in.readUTF()))
throw new IOException("invalid epk file");
return new FileEntry("FILE", path, file);
}
// Avoid Inflater memleak
public void close() throws IOException {
if (zis != null) {
zis.close();
}
if (in != null) {
in.close();
}
isFinished = true;
}
}
@@ -0,0 +1,112 @@
/*
* Copyright (c) 1996, 2022, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package net.lax1dude.eaglercraft.bintools.utils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
/**
* GZIP output stream class that allows you to set the compression level
*/
public class GZIPOutputStream2 extends DeflaterOutputStream {
protected CRC32 crc = new CRC32();
private final static int GZIP_MAGIC = 0x8b1f;
private final static int TRAILER_SIZE = 8;
public GZIPOutputStream2(OutputStream out, int compressionLevel, int size, boolean syncFlush) throws IOException {
super(out, new Deflater(compressionLevel, true), size, syncFlush);
writeHeader();
crc.reset();
}
public synchronized void write(byte[] buf, int off, int len) throws IOException {
super.write(buf, off, len);
crc.update(buf, off, len);
}
public void finish() throws IOException {
if (!def.finished()) {
try {
def.finish();
while (!def.finished()) {
int len = def.deflate(buf, 0, buf.length);
if (def.finished() && len <= buf.length - TRAILER_SIZE) {
// last deflater buffer. Fit trailer at the end
writeTrailer(buf, len);
len = len + TRAILER_SIZE;
out.write(buf, 0, len);
return;
}
if (len > 0)
out.write(buf, 0, len);
}
// if we can't fit the trailer at the end of the last
// deflater buffer, we write it separately
byte[] trailer = new byte[TRAILER_SIZE];
writeTrailer(trailer, 0);
out.write(trailer);
} catch (IOException e) {
def.end();
throw e;
}
}
}
private void writeHeader() throws IOException {
out.write(new byte[] { (byte) GZIP_MAGIC, // Magic number (short)
(byte) (GZIP_MAGIC >> 8), // Magic number (short)
Deflater.DEFLATED, // Compression method (CM)
0, // Flags (FLG)
0, // Modification time MTIME (int)
0, // Modification time MTIME (int)
0, // Modification time MTIME (int)
0, // Modification time MTIME (int)
0, // Extra flags (XFLG)
0 // Operating system (OS)
});
}
private void writeTrailer(byte[] buf, int offset) throws IOException {
writeInt((int) crc.getValue(), buf, offset); // CRC-32 of uncompr. data
writeInt(def.getTotalIn(), buf, offset + 4); // Number of uncompr. bytes
}
private void writeInt(int i, byte[] buf, int offset) throws IOException {
writeShort(i & 0xffff, buf, offset);
writeShort((i >> 16) & 0xffff, buf, offset + 2);
}
private void writeShort(int s, byte[] buf, int offset) throws IOException {
buf[offset] = (byte) (s & 0xff);
buf[offset + 1] = (byte) ((s >> 8) & 0xff);
}
}
@@ -0,0 +1,33 @@
package net.lax1dude.eaglercraft.bintools.utils;
public class IEEE754 {
// source:
// https://stackoverflow.com/questions/6162651/half-precision-floating-point-in-java
public static int encodeHalfFloat(float fval) {
int fbits = Float.floatToIntBits(fval);
int sign = fbits >>> 16 & 0x8000; // sign only
int val = (fbits & 0x7fffffff) + 0x1000; // rounded value
if (val >= 0x47800000) // might be or become NaN/Inf
{ // avoid Inf due to rounding
if ((fbits & 0x7fffffff) >= 0x47800000) { // is or must become NaN/Inf
if (val < 0x7f800000) // was value but too large
return sign | 0x7c00; // make it +/-Inf
return sign | 0x7c00 | // remains +/-Inf or NaN
(fbits & 0x007fffff) >>> 13; // keep NaN (and Inf) bits
}
return sign | 0x7bff; // unrounded not quite Inf
}
if (val >= 0x38800000) // remains normalized value
return sign | val - 0x38000000 >>> 13; // exp - 127 + 15
if (val < 0x33000000) // too small for subnormal
return sign; // becomes +/-0
val = (fbits & 0x7fffffff) >>> 23; // tmp exp for subnormal calc
return sign | ((fbits & 0x7fffff | 0x800000) // add subnormal bit
+ (0x800000 >>> val - 102) // round depending on cut off
>>> 126 - val); // div by 2^(1-(exp-127+15)) and >> 13 | exp=0
}
}
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools.utils;
import java.io.IOException;
import java.io.InputStream;
public class IOUtils {
public static int readFully(InputStream is, byte[] out) throws IOException {
int i = 0, j;
while (i < out.length && (j = is.read(out, i, out.length - i)) != -1) {
i += j;
}
return i;
}
public static long skipFully(InputStream is, long skip) throws IOException {
long i = 0, j;
while (i < skip && (j = is.skip(skip - i)) != 0) {
i += j;
}
return i;
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 lax1dude. All Rights Reserved.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.lax1dude.eaglercraft.bintools.utils;
import java.awt.image.BufferedImage;
public class LabPBR2Eagler {
/**
* See
* net.lax1dude.eaglercraft.v1_8.opengl.ext.deferred.texture.PBRTextureMapUtils
*/
public static void convertLabPBRToEaglerLegacy(BufferedImage input, BufferedImage output) {
for (int w = input.getWidth(), x = 0; x < w; ++x) {
for (int h = input.getHeight(), y = 0; y < h; ++y) {
int pixel = input.getRGB(x, y);
int a = (pixel >>> 24) & 0xFF;
if (a == 0xFF)
a = 0;
output.setRGB(x, y, (pixel & 0x00FFFF00) | Math.min(a << 2, 0xFF) | 0xFF000000);
}
}
}
public static void convertLabPBRToEagler(BufferedImage input, BufferedImage output) {
for (int w = input.getWidth(), x = 0; x < w; ++x) {
for (int h = input.getHeight(), y = 0; y < h; ++y) {
int pixel = input.getRGB(x, y);
int a = (pixel >>> 24) & 0xFF;
if (a == 0xFF)
a = 0;
int b = ((pixel & 0xFF) - 65) * 255 / 190;
if (b < 0)
b = 0;
output.setRGB(x, y, (pixel & 0x00FFFF00) | Math.min(a << 2, 0xFF) | ((255 - b) << 24));
}
}
}
public static void convertEaglerToLabPBR(BufferedImage input, BufferedImage output) {
for (int w = input.getWidth(), x = 0; x < w; ++x) {
for (int h = input.getHeight(), y = 0; y < h; ++y) {
int pixel = input.getRGB(x, y);
int a = (pixel >>> 2) & 0x3F;
if (a == 0)
a = 0xFF;
int b = 255 - (pixel >>> 24);
if (b > 0)
b = (b * 190 / 255) + 65;
output.setRGB(x, y, (pixel & 0x00FFFF00) | (a << 24) | b);
}
}
}
}
@@ -0,0 +1,39 @@
package net.lax1dude.eaglercraft.bintools;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.platform.commons.annotation.Testable;
import java.io.FileInputStream;
import java.io.IOException;
@Testable
public class TestOptimizedOBJConverter {
@Test
@SuppressWarnings("ConstantConditions")
public void test() throws IOException {
String[] paramz = {
"samples/obj2mdl-fnaw/laxativedude1.obj", "samples/obj2mdl-fnaw/tests/output/laxativedude1.mdl", "true"
};
OBJConverter._main(paramz, true);
paramz[1] = "samples/obj2mdl-fnaw/tests/output/o_laxativedude1.mdl";
OptimizedOBJConverter._main(paramz, true);
try (FileInputStream optimizedProgramOutput = new FileInputStream(paramz[1]);
FileInputStream originalProgramOutput = new FileInputStream( "samples/obj2mdl-fnaw/tests/output/laxativedude1.mdl")) {
int o;
int r;
while ((o = optimizedProgramOutput.read()) != -2 && (r = originalProgramOutput.read()) != -2) {
if (o == r && r == -1)
break;
if (o != r)
Assertions.fail("Output is not the same!");
}
} catch (Exception e) {
Assertions.fail(e.getMessage());
}
}
}