Updated BinGenerationTool with support for validating the serialized bins against their source excels

This commit is contained in:
Collin Smith
2019-09-28 03:50:41 -07:00
parent 0416c93abf
commit 28c6bd95dc

View File

@ -7,22 +7,20 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.headless.HeadlessApplication; import com.badlogic.gdx.backends.headless.HeadlessApplication;
import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.GdxRuntimeException; import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.StreamUtils;
import com.riiablo.mpq.MPQFileHandleResolver; import com.riiablo.mpq.MPQFileHandleResolver;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ClassPathUtils; import org.apache.commons.lang3.ClassPathUtils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class BinGenerationTool extends ApplicationAdapter { public class BinGenerationTool extends ApplicationAdapter {
private static final String TAG = "BinGenerationTool"; private static final String TAG = "BinGenerationTool";
private static final boolean VALIDATE_BIN = true;
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
new HeadlessApplication(new BinGenerationTool(args)); new HeadlessApplication(new BinGenerationTool(args));
} }
@ -80,9 +78,10 @@ public class BinGenerationTool extends ApplicationAdapter {
Gdx.app.exit(); Gdx.app.exit();
} }
@SuppressWarnings("unchecked")
public void generate(FileHandle txt, FileHandle bin, Class<Excel> excelClass) throws Exception { public void generate(FileHandle txt, FileHandle bin, Class<Excel> excelClass) throws Exception {
Gdx.app.log(TAG, bin.toString()); Gdx.app.log(TAG, bin.toString());
Excel excel = Excel.load(excelClass, txt, Excel.EXPANSION); Excel<Excel.Entry> excel = Excel.load(excelClass, txt, Excel.EXPANSION);
OutputStream out = null; OutputStream out = null;
try { try {
out = bin.write(false, 8192); out = bin.write(false, 8192);
@ -93,63 +92,24 @@ public class BinGenerationTool extends ApplicationAdapter {
} finally { } finally {
IOUtils.closeQuietly(out); IOUtils.closeQuietly(out);
} }
}
public void generate2(String fileName, String className) throws Exception { if (VALIDATE_BIN) {
Class<Excel> excelClass = (Class<Excel>) Class.forName(className); Class<Excel.Entry> entryClass = getEntryClass(excelClass);
if (!Excel.isBinned(excelClass)) { String binClassName = excelClass.getName() + "Bin";
Gdx.app.log(TAG, excelClass + " is not annotated with " + Excel.Binned.class); Class binClass = Class.forName(binClassName);
return; Method equals = binClass.getMethod("equals", entryClass, entryClass);
} Excel binExcel = Excel.load(excelClass, txt, bin, null);
if (binExcel.size() != excel.size()) Gdx.app.error(TAG, "excel sizes do not match!");
File file = new File(fileName); for (Excel.Entry e1 : excel) {
//InputStream in = FileUtils.openInputStream(file); Excel.Entry eq = getEqual(equals, e1, binExcel);
//TXT txt = TXT.loadFromStream(in); if (eq == null) {
FileHandle txt = new FileHandle(file); Gdx.app.log(TAG, "ERROR at index " + e1);
//break;
Excel<Excel.Entry> excel = Excel.load(excelClass, txt, null, Excel.EXPANSION); } else {
//Gdx.app.log(TAG, e1 + "=" + eq);
String binFileName = generateBinName(fileName); }
File binFile = new File(binFileName);
Gdx.app.log(TAG, txt + "->" + binFileName);
OutputStream out = FileUtils.openOutputStream(binFile);
BufferedOutputStream buffer = new BufferedOutputStream(out);
LittleEndianDataOutputStream dos = null;
try {
dos = new LittleEndianDataOutputStream(buffer);
excel.writeBin(dos);
} finally {
StreamUtils.closeQuietly(dos);
}
Class<Excel.Entry> entryClass = getEntryClass(excelClass);
String binClassName = excelClass.getName() + "Bin";
Class binClass = Class.forName(binClassName);
Method equals = binClass.getMethod("equals", entryClass, entryClass);
Excel binExcel = Excel.load(excelClass, txt, new FileHandle(binFile), null);
if (binExcel.size() != excel.size()) System.out.println("excel sizes do not match!");
for (Excel.Entry e1 : excel) {
Excel.Entry eq = getEqual(equals, e1, binExcel);
if (eq == null) {
Gdx.app.log(TAG, "ERROR at index " + e1);
break;
} else {
Gdx.app.log(TAG, e1 + "=" + eq);
} }
} }
/*
InputStream in = FileUtils.openInputStream(binFile);
BufferedInputStream bufferIn = new BufferedInputStream(in);
LittleEndianDataInputStream dis = null;
try {
dis = new LittleEndianDataInputStream(bufferIn);
Excel copy = excelClass.newInstance();
copy.readBin(dis);
} finally {
StreamUtils.closeQuietly(dis);
}
*/
} }
private static Excel.Entry getEqual(Method equals, Excel.Entry e1, Excel<Excel.Entry> binExcel) throws Exception { private static Excel.Entry getEqual(Method equals, Excel.Entry e1, Excel<Excel.Entry> binExcel) throws Exception {