Added .pak rebuilder plugin. Added error handling to Gim class.

This commit is contained in:
codestation 2011-01-23 02:24:35 +00:00
parent e8eabbca35
commit 79f5f1e8c8
4 changed files with 133 additions and 3 deletions

View file

@ -34,6 +34,7 @@ import enc.RebuildPluginA;
import enc.RebuildPluginB;
import enc.RebuildPluginC;
import enc.RebuildPluginD;
import enc.RebuildPluginE;
public class Mhtrans {
@ -95,6 +96,9 @@ public class Mhtrans {
case 5:
enc = new RebuildPluginD();
break;
case 6:
enc = new RebuildPluginE();
break;
default:
System.err.println("Unknown encoder: " + encoder);
System.exit(1);

View file

@ -60,7 +60,7 @@ public class RebuildPluginD extends EndianFixer implements Encoder {
}
});
if(files.length > 0) {
FileOutputStream out = new FileOutputStream(dir.getName() + ".bin");
FileOutputStream out = new FileOutputStream(dir.getName() + ".tmh");
out.write(id);
writeInt(out, files.length);
writeInt(out, 0);
@ -89,7 +89,8 @@ public class RebuildPluginD extends EndianFixer implements Encoder {
depth = Gim.RGBA5551;
else
depth = 0;
gim.setRGBarray(img.getWidth(), img.getHeight(), rgbArray, type, depth);
if(!gim.setRGBarray(img.getWidth(), img.getHeight(), rgbArray, type, depth))
System.err.println("Create RGB array failed");
}
gim.write(out);
}

114
src/enc/RebuildPluginE.java Normal file
View file

@ -0,0 +1,114 @@
/* MHP2GENC v1.0 - PAK rebuilder
Copyright (C) 2008-2010 codestation
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package enc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Vector;
import base.Encoder;
import base.EndianFixer;
/**
* RebuildPluginE v1.0
*
* @author Codestation
*/
public class RebuildPluginE extends EndianFixer implements Encoder {
@Override
public void compile(String filepath) {
try {
File dir = new File(filepath);
if(!dir.isDirectory()) {
System.err.println("Isn't a directory: " + filepath);
return;
}
File files[] = dir.listFiles();
Arrays.sort(files, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});
Vector<File> clean_files = new Vector<File>();
for(File file : files) {
if(!file.isDirectory())
clean_files.add(file);
}
if(clean_files.size() > 0) {
RandomAccessFile out = new RandomAccessFile(dir.getName() + ".pak","rw");
int table_size = 4 + clean_files.size() * 8;
if(table_size % 16 > 0) {
table_size += 16 - (table_size % 16);
System.out.println("Table size (padded): " + table_size + " bytes");
}
writeInt(out, clean_files.size());
long table_start = out.getFilePointer();
byte pad[] = new byte[table_size - 4];
out.write(pad);
long data_start = out.getFilePointer();
for(File file : clean_files) {
if(file.getName().endsWith(".tmh")) {
File tmhdir = new File(dir + "/" + file.getName().replaceAll(".tmh$", ""));
if(tmhdir.isDirectory()) {
file.delete();
Encoder enc = new RebuildPluginD();
System.out.println("Processing directory " + tmhdir.getName());
enc.compile(dir + "/" + tmhdir.getName());
new File(file.getName()).renameTo(new File(dir + "/" + file.getName()));
}
}
out.seek(table_start);
writeInt(out, (int)data_start);
writeInt(out, (int)file.length());
table_start = out.getFilePointer();
System.out.println("Processing " + file.getName());
FileInputStream in = new FileInputStream(file);
byte buffer[] = new byte[(int) file.length()];
in.read(buffer);
out.seek(data_start);
out.write(buffer);
long curr_pos = out.getFilePointer();
data_start = curr_pos;
if(data_start % 16 > 0)
data_start += 16 - (data_start % 16);
while(curr_pos < data_start) {
out.writeByte(0);
curr_pos++;
}
}
out.setLength(data_start);
out.close();
} else {
System.err.println("Empty directory\n");
}
System.out.println("Finished!");
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View file

@ -78,6 +78,8 @@ public class Gim extends EndianFixer {
data_size += (width / 2) * height;
imagedata = new byte[data_size - HEADER_SIZE];
palette_count = fill_to_palette(rgb);
if(palette_count == -1)
return false;
palette_count = 16;
palette_size += palettesize;
size = data_size + palette_size + HEADER_SIZE;
@ -87,6 +89,8 @@ public class Gim extends EndianFixer {
data_size += width * height;
imagedata = new byte[data_size - HEADER_SIZE];
palette_count = fill_to_palette(rgb);
if(palette_count == -1)
return false;
palette_count = 256;
palette_size += palettesize;
size = data_size + palette_size + HEADER_SIZE;
@ -182,6 +186,8 @@ public class Gim extends EndianFixer {
}
int color = rgb[off];
int index = set_unique_color(color);
if(index == -1)
return -1;
if(data_type == GIM_TYPE_PIXELS) {
if(flip) {
imagedata[counter] |= (byte) (index << 4);
@ -199,7 +205,7 @@ public class Gim extends EndianFixer {
}
}
}
return palette_count;
return palettedata_count;
}
private void set_color(int color, int offset) {
@ -234,6 +240,11 @@ public class Gim extends EndianFixer {
}
i++;
}
if(data_type == GIM_TYPE_PALETTE && i >= 256 ||
data_type == GIM_TYPE_PIXELS && i >= 16) {
System.err.println("Maximum number of color in palette reached (" + i + ")");
return -1;
}
set_color(color, i * (palette_type == RGBA8888 ? 4 : 2));
palettedata_count = i + 1;
return i;