Skip to content

Commit

Permalink
Optimize Wad File usage in HXDD Builder
Browse files Browse the repository at this point in the history
Wasn't being properly used and was eating up memory, causing slowdown and excess gc.
  • Loading branch information
Lemon-King committed Jun 24, 2024
1 parent 63ada9b commit d025a15
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 55 deletions.
19 changes: 5 additions & 14 deletions src/main/java/lemon/hxdd/builder/AssetExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ public void ExtractFromWad() {
ExportData(data, pal);
}

public void ExtractFromPK3() {
// Extract from ZIP
//ZipAssets pk3 = new ZipAssets(this.mf.sourcePK3 + ".pk3");
this.za.SetFile(new File(this.mf.sourcePK3));
byte[] data = this.za.ExtractFileAsData(this.mf.inputName);
Palette pal = GetPlaypal();
ExportData(data, pal);
}

private void ExportData(byte[] data, Palette pal) {
if (mf.decodeType == "lumps") {
LumpExport(data);
Expand Down Expand Up @@ -132,7 +123,7 @@ private void TextLumpExport(byte[] data) {

//System.out.println("Exported " + path);
} catch (IOException e) {
System.out.println("Failed to export lump " + path + " from " + mf.source);
System.out.println("Failed to export lump " + path);
}
}

Expand Down Expand Up @@ -160,7 +151,7 @@ private void GraphicsExport(byte[] data, Palette pal) {

//System.out.println("Exported " + imagePath);
} catch (IOException e) {
System.out.println("Failed to export graphics " + imagePath + " from " + mf.source);
System.out.println("Failed to export graphics " + imagePath);
//e.printStackTrace();
}
}
Expand All @@ -185,7 +176,7 @@ private void FlatExport(byte[] data, Palette pal) {
File newFile = new File(imagePath);
ImageIO.write(image, "PNG", newFile);
} catch (IOException e) {
System.out.println("Failed to export flat " + imagePath + " from " + mf.source);
System.out.println("Failed to export flat " + imagePath);
}
}

Expand Down Expand Up @@ -228,7 +219,7 @@ private void SoundExport() {
//System.out.println("Exported " + filePath + ".lmp");
}
} catch (IOException | UnsupportedAudioFileException e) {
System.out.println("Failed to export flat " + filePath + " from " + mf.source);
System.out.println("Failed to export flat " + filePath);
e.printStackTrace();
}
}
Expand Down Expand Up @@ -259,7 +250,7 @@ private void MusicExport(boolean lowerVolume) {
music.writeBytes(new FileOutputStream(target, false));
//System.out.println("Exported " + filePath + ".mus");
} catch (IOException e) {
System.out.println("Failed to export " + filePath + ".mus" + " from " + mf.source);
System.out.println("Failed to export " + filePath + ".mus");
//e.printStackTrace();
}
}
Expand Down
40 changes: 14 additions & 26 deletions src/main/java/lemon/hxdd/builder/MetaFile.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lemon.hxdd.builder;

import net.mtrop.doom.Wad;
import net.mtrop.doom.WadFile;
import net.mtrop.doom.graphics.Flat;
import net.mtrop.doom.graphics.PNGPicture;
import net.mtrop.doom.graphics.Palette;
Expand All @@ -23,8 +22,7 @@
import java.util.Objects;

public class MetaFile {
String source;
String sourcePK3;
//String source;
String inputName;
String outputName;
String type;
Expand All @@ -43,9 +41,8 @@ public class MetaFile {
MetaFile() {
}

public void Define(String name, String decodeType, String folder, String sourceName) {
this.source = sourceName; // check if pk3 from filename
this.sourcePK3 = null; // PK3 file, overrides wad extract
public void Define(String name, String decodeType, String folder, Wad sourceWad) {
this.wad = sourceWad;
this.inputName = name; // input filename
this.outputName = name; // output filename
this.folder = folder; // export folder
Expand All @@ -61,35 +58,26 @@ public void ExtractFile(String path) {
this.pathTemp = path;
// Get data from WAD or PK3 ZIP.
try {
if (this.sourcePK3 != null) {
this.ExtractFromPK3();
} else {
//if (this.sourcePK3 != null) {
// this.ExtractFromPK3();
//} else {
this.ExtractFromWad();
}
//}
} catch (IOException e) {
// log error
}
}

public void ExtractFromWad() throws IOException {
this.wad = new WadFile(this.source);
//this.wad = new WadFile(this.source);
byte[] data = this.wad.getData(this.inputName);

if (this.pal == null) {
this.pal = GetPlaypal();
}
ExportData(data);

this.wad.close();
}

public void ExtractFromPK3() {
// Extract from ZIP
//ZipAssets pk3 = new ZipAssets(this.mf.sourcePK3 + ".pk3");
this.za.SetFile(new File(this.sourcePK3));
byte[] data = this.za.ExtractFileAsData(this.inputName);
Palette pal = GetPlaypal();
ExportData(data, pal);
//this.wad.close();
}

public void SetWad(Wad wf) {
Expand Down Expand Up @@ -167,7 +155,7 @@ private void TextLumpExport(byte[] data) {

//System.out.println("Exported " + path);
} catch (IOException e) {
System.out.println("Failed to export lump " + path + " from " + this.source);
System.out.println("Failed to export lump " + path);
}
}

Expand Down Expand Up @@ -195,7 +183,7 @@ private void GraphicsExport(byte[] data, Palette pal) {

//System.out.println("Exported " + imagePath);
} catch (IOException e) {
System.out.println("Failed to export graphics " + imagePath + " from " + this.source);
System.out.println("Failed to export graphics " + imagePath);
//e.printStackTrace();
}
}
Expand All @@ -220,7 +208,7 @@ private void FlatExport(byte[] data, Palette pal) {
File newFile = new File(imagePath);
ImageIO.write(image, "PNG", newFile);
} catch (IOException e) {
System.out.println("Failed to export flat " + imagePath + " from " + this.source);
System.out.println("Failed to export flat " + imagePath);
}
}

Expand Down Expand Up @@ -263,7 +251,7 @@ private void SoundExport() {
//System.out.println("Exported " + filePath + ".lmp");
}
} catch (IOException | UnsupportedAudioFileException e) {
System.out.println("Failed to export flat " + filePath + " from " + this.source);
System.out.println("Failed to export flat " + filePath);
e.printStackTrace();
}
}
Expand Down Expand Up @@ -294,7 +282,7 @@ private void MusicExport(boolean lowerVolume) {
music.writeBytes(new FileOutputStream(target, false));
//System.out.println("Exported " + filePath + ".mus");
} catch (IOException e) {
System.out.println("Failed to export " + filePath + ".mus" + " from " + this.source);
System.out.println("Failed to export " + filePath + ".mus");
//e.printStackTrace();
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/lemon/hxdd/builder/PackageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void Start() {
try {
p.getValue().close();
} catch (IOException e) {
//
e.printStackTrace();
}
});

Expand Down Expand Up @@ -746,10 +746,12 @@ private void ExportRealm667() {

String pathMarineStuff = path + "MarineStuff.wad";
try {
Wad wadMarineStuff = new WadFile(pathMarineStuff);

MetaFile f = new MetaFile();
f.SetPalette(GraphicUtils.DOOM);
f.SetWad(new WadFile(pathMarineStuff));
f.Define("AHLMA0", "sprite", "sprites", pathMarineStuff);
f.Define("AHLMA0", "sprite", "sprites", wadMarineStuff);
f.folder = "sprites/realm667";
f.ExtractFile(pathTemp);
f.inputName = "BOOTA0";
Expand All @@ -759,12 +761,14 @@ private void ExportRealm667() {
f.outputName = "UNIFA0";
f.ExtractFile(pathTemp);

f.Define("CREDITS", "textlump","", pathMarineStuff);
f.Define("CREDITS", "textlump","", wadMarineStuff);
f.outputName = "realm667/marinestuff/CREDITS.txt";
f.ExtractFile(pathTemp);
f.Define("INFO", "textlump","", pathMarineStuff);
f.Define("INFO", "textlump","", wadMarineStuff);
f.outputName = "realm667/marinestuff/INFO.txt";
f.ExtractFile(pathTemp);

wadMarineStuff.close();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/lemon/hxdd/builder/WadFileOrganizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ public void Parse(String path) throws IOException {
MetaFile mf = new MetaFile();
mf.SetWad(this.wad);
if (Arrays.asList(EngineLumps).contains(entryName)) {
mf.Define(entryName, "lump","lumps", source);
mf.Define(entryName, "lump","lumps", this.wad);
mf.SetWad(this.wad);
this.entryMaps.get("lumps").put(entryName, mf);
} else if (Arrays.asList(GameLumps).contains(entryName.toLowerCase())) {
// Will let files be unique and not fight over a single entry.
mf.Define(entryName, "lump","lumps", source);
mf.Define(entryName, "lump","lumps", this.wad);
this.entryMaps.get("lumps").put(entryName, mf);
} else if (Arrays.asList(TextLumps).contains(entryName.toLowerCase())) {
// TextLumps should be renamed per game as to prevent conflicts
mf.Define(entryName, "textlump","lumps", source);
mf.Define(entryName, "textlump","lumps", this.wad);
mf.outputName = entryName;
this.entryMaps.get("lumps").put(entryName, mf);
} else if (!Arrays.asList(EntryIgnoreList).contains(entryName)) {
Expand All @@ -102,30 +102,30 @@ public void Parse(String path) throws IOException {
folder = "";
}
} else if (entryName.startsWith("FONT")) {
mf.Define(entryName, "graphic","graphics", source);
mf.Define(entryName, "graphic","graphics", this.wad);
this.entryMaps.get("graphics").put(entryName, mf);
} else if (entryName.equals("ADVISOR")) {
// Heretic / Hexen only advisory
mf.Define(entryName, "sprite", "graphics", source);
mf.Define(entryName, "sprite", "graphics", this.wad);
this.entryMaps.get("graphics").put(entryName, mf);
} else if (Arrays.asList(GraphicLumps).contains(entryName) || folder.equals("graphics")) {
decodeType = "graphic";
folder = "graphics";
mf.Define(entryName, decodeType, folder, source);
mf.Define(entryName, decodeType, folder, this.wad);
this.entryMaps.get(folder).put(entryName, mf);
} else if (folder.equals("sprites") || folder.equals("patches") || folder.equals("flats")) {
mf.Define(entryName, decodeType, folder, source);
mf.Define(entryName, decodeType, folder, this.wad);
this.entryMaps.get(folder).put(entryName, mf);
} else {
try {
byte[] data = this.wad.getData(entry);
if (data.length > 4) {
// startswith is hacky, but it works
if ((data[0] + "" + data[1] + "" + data[2] + "" + data[3]).startsWith("778583")) {
mf.Define(entryName, "music","music", source);
mf.Define(entryName, "music","music", this.wad);
this.entryMaps.get("music").put(entryName, mf);
} else if ((data[0] + "" + data[1] + "" + data[2] + "" + data[3]).startsWith("301743")) {
mf.Define(entryName, "sound","sounds", source);
mf.Define(entryName, "sound","sounds", this.wad);
this.entryMaps.get("sounds").put(entryName, mf);
}
}
Expand Down Expand Up @@ -167,9 +167,9 @@ public MetaFile CopyFile(String folder, String source, String target) {
MetaFile mfFrom = this.entryMaps.get(folder).get(source);
if (mfFrom != null) {
MetaFile mfCopy = new MetaFile();
mfCopy.Define(target, mfFrom.decodeType, folder, mfFrom.source);
mfCopy.Define(target, mfFrom.decodeType, folder, mfFrom.wad);
mfCopy.SetWad(mfFrom.wad);
mfCopy.sourcePK3 = mfFrom.sourcePK3;
//mfCopy.sourcePK3 = mfFrom.sourcePK3;
mfCopy.inputName = mfFrom.inputName;
mfCopy.folder = mfFrom.folder;
mfCopy.decodeType = mfFrom.decodeType;
Expand Down

0 comments on commit d025a15

Please sign in to comment.