Skip to content

Commit

Permalink
ZipAssets: Fix how zipped data is accessed to prevent read errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemon-King committed Oct 15, 2023
1 parent e3f2537 commit 6238a68
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/main/java/lemon/hxdd/builder/ZipAssets.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,18 +250,13 @@ void ExtractFilesFromFolderAndConvert(String input, String output, String[] limi
String fileName = entry.getName();
String cleanedName = fileName.startsWith(input) ? fileName.substring(input.length()) : fileName;

if (limitedFiles != null && limitedFiles.length > 0) {
if (!Arrays.asList(limitedFiles).contains(cleanedName)) {
return;
}
}
try {
if (fileName.contains(".lmp")) {
String npath = path + "/" + output + "/" + cleanedName.replace("lmp", "png");
String pathOutput = path + "/" + output + "/" + cleanedName.replace("lmp", "png");
if (dims != null) {
ExportGraphic(npath, this.zis, finalPal, dims);
ExportGraphic(pathOutput, this.zis.readAllBytes(), finalPal, dims);
} else {
Export(npath, this.zis, finalPal, dims);
Export(pathOutput, this.zis.readAllBytes(), finalPal, dims);
}
} else {
try {
Expand All @@ -284,12 +279,12 @@ void ExtractFilesFromFolderAndConvert(String input, String output, String[] limi
this.Close();
}

private void Export(String path, InputStream in, Palette pal, int[] dimensions) {
private void Export(String path, byte [] data, Palette pal, int[] dimensions) {
try {
File fileOutput = new File(path);
Picture p = new Picture();

byte[] bytesGraphic = GetBytesFromInputStream(in);
byte[] bytesGraphic = data;
p.fromBytes(bytesGraphic);
if (dimensions != null) {
p.setDimensions(dimensions[0], dimensions[1]);
Expand All @@ -304,31 +299,22 @@ private void Export(String path, InputStream in, Palette pal, int[] dimensions)
}
pngImg.writeBytes(new FileOutputStream(fileOutput, false));
} catch (IOException e) {
e.printStackTrace();
System.out.println("ZipAssets.Export: Failed to export " + path);
}
}

private void ExportGraphic(String path, InputStream in, Palette pal, int[] dimensions) {
private void ExportGraphic(String path, byte [] data, Palette pal, int[] dimensions) {
try {
File target = new File(path);

BufferedImage image = null;
Picture p = new Picture();
p.setDimensions(dimensions[0], dimensions[1]);
p.fromBytes(data);
BufferedImage image = GraphicUtils.createImage(p, pal);

boolean AsFlat = false;
if (AsFlat) {
// Results in corrupted data: https://github.com/MTrop/DoomStruct/issues/17#issuecomment-1603050005
Flat f = Flat.read(dimensions[0], dimensions[1], in);
image = GraphicUtils.createImage(f, pal);
} else {
Picture p = new Picture();
p.setDimensions(dimensions[0], dimensions[1]);
p.readBytes(in);
image = GraphicUtils.createImage(p, pal);
}
ImageIO.write(image, "PNG", target);
//System.out.println("Wrote " + path);
} catch (IOException e) {
e.printStackTrace();
System.out.println("ZipAssets.ExportGraphic: Failed to export " + path + "\n" + e);
}
}

Expand Down

0 comments on commit 6238a68

Please sign in to comment.