我们做移植的,尤其是做渠道移植的,经常会遇到一个游戏要改上几十遍,适配到各个渠道里去。
通常这类需求只需要改个LOGO换个ABOUT,还有就是改个封面,甚至改个游戏跳转流程。
这里先讲讲如何替换JAR包里的图片。
1,如果图片直接存在于jar包里,解压缩后就能预览图片,这样的修改是非常容易的,替换压缩包里的图片就行了。
2,如果图片是包含于JAR里的某些资源文件中的,这样的话我们就得花点功夫来搜索并替换的。
我自己写了个算法,用来搜索,替换jar包里无法直接替换的图片。
原理:搜索PNG头和PNG尾,把中间部分给摘出来,独立成文件就行了。如果要替换文件,一般情况下是构造一个新的PNG,大小和原始文件一模一样,这样就万无一失了。
代码奉上。
1,定义PNG标记头和尾,便于搜索
public static final byte[] PNG_START_TAG = {
(byte) 0x89, (byte) 0x50, (byte) 0x4E, (byte) 0x47, (byte) 0x0D,
(byte) 0x0A, (byte) 0x1A, (byte) 0x0A};
public static final byte[] PNG_END_TAG = {
(byte) 0x49, (byte) 0x45, (byte) 0x4E, (byte) 0x44, (byte) 0xAE,
(byte) 0x42, (byte) 0x60, (byte) 0x82};
2,读入资源文件到byte数组缓存。
RandomAccessFile raf = new RandomAccessFile(filepath + filename, "rw");
int length = (int) raf.length();
byte[] filebuf = new byte[length];
raf.read(filebuf);
raf.close();
3,用新图片构造一个大小和资源包里要被替换掉的文件一样的数组BUFFER.
public static byte[] PNG_SELF_CHUNK_START = {0x77,0x4C,0x45,0x4E}; //wLEN 自定义的PNG标记,实际无用,只是为了占用空间,匹配大小而已。
public static byte[] rebuildImgByLength(String filepath, int newLength) {
try {
Vector s_pngVector = new Vector();
Vector s_chunkLength = new Vector();
RandomAccessFile raf = new RandomAccessFile(filepath, "rw");
int filelength = (int)raf.length();
if(newLength - filelength < 12)
{
System.out.println("File length must bigger that 8");
return null;
}
byte[] png_label = new byte[8];
raf.read(png_label);
//s_pngVector.add(png_label);
int totallength = 8;
int chunklength = 0;
while(totallength < filelength)
{
chunklength = raf.readInt();
totallength += 4;
byte[] temp = new byte[chunklength + 4 + 4];
totallength +=chunklength + 4 + 4;
raf.read(temp);
s_pngVector.add(temp);
s_chunkLength.add(""+chunklength);
}
int chunklength_add = newLength - filelength - 12;
byte[] newChunk = new byte[newLength - filelength - 4];
for(int i=0;i< newChunk.length;i++)
{
if(i < 4)
{
newChunk[i] = PNG_SELF_CHUNK_START[i];
}
else
{
newChunk[i] = 0x0;
}
}
s_chunkLength.insertElementAt(""+chunklength_add, s_chunkLength.size() - 1);
s_pngVector.insertElementAt(newChunk , s_pngVector.size() - 1);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(png_label);
for(int j=0;j<s_chunkLength.size();j++)
{
String chunklengthtemp = (String)s_chunkLength.elementAt(j);
byte[] tempchunk = (byte[])s_pngVector.elementAt(j);
byte[] lengthbyte = int2bytes(Integer.parseInt(chunklengthtemp));
bos.write(lengthbyte);
bos.write(tempchunk);
}
RandomAccessFile rafoutput = new RandomAccessFile(StringUtil.getFileNameWithoutExt(filepath)+ "_outputaaaa.bin", "rw");
rafoutput.write(bos.toByteArray());
rafoutput.close();
return bos.toByteArray();
}
catch (Exception e) {
e.printStackTrace();
}
4,搜索文件,找并保存为独立的PNG文件,同时替换掉要替换的文件,保存为原来的BIN,这里旧的PNG已经被第3步构造出来的BYTE缓存替换掉了。
int filecount = 0;
int searchStart = Replace.searchByteArray(filebuf, PNG_START_TAG, 0);
int searchEnd = Replace.searchByteArray(filebuf, PNG_END_TAG, 0);
while (searchStart >= 0 && searchEnd >= 0) {
byte[] png_temp = new byte[searchEnd + PNG_END_TAG.length - searchStart];
RandomAccessFile rafpng = new RandomAccessFile(outputDir + "/" +
filename + "_" + filecount +
".png", "rw");
if(filecount == fileCount)
{
byte[] replaceBuf = rebuildImgByLength(replaceFile, png_temp.length);
System.arraycopy(replaceBuf, 0, filebuf, searchStart, png_temp.length);
}
System.arraycopy(filebuf, searchStart, png_temp, 0, png_temp.length);
rafpng.write(png_temp);
rafpng.close();
filecount++;
System.out.println("searchStart " + searchStart + " searchEnd " +
searchEnd);
searchStart = Replace.searchByteArray(filebuf, PNG_START_TAG,
searchEnd + 1);
searchEnd = Replace.searchByteArray(filebuf, PNG_END_TAG, searchEnd + 1);
}
RandomAccessFile rafoutput = new RandomAccessFile(filepath + filename+"_output", "rw");
rafoutput.write(filebuf);
rafoutput.close();