exportSprite是整个程序的入口
exportSprite.java
文件如下:
<wbr style="line-height:25px"><wbr style="line-height:25px"></wbr></wbr>
import java.io.*;
enum ACTION
{
kNone,
kLoad,
kExportBSprite,
kCreatePackage,
kCreateInterface,
kCreatStruct,
kJavaExportFramesID,
kExportFramesID,
kExportAnimsID,
kJavaExportAnimsID,
kCloseStruct,
kCloseInterface,
kJavaCloseInterface,
kSetToolsPath,
kSetRGBImagePath,}
public classexportSprite
{
/**
* @param args the command line arguments
*/
public static voidmain(String[] args)
{
// TODO code application logic here
if (args.length != 1)
{
System.out.println("miss the config file for files to export!");
return;
}
boolean result = getExportFilesConfig(args[0]);
if (result)
{
doExport();
}
}
final static String strCmdLoad = "Load";
final static int CMD_BUFFER_MIN_SIZE = 1000;
static String strCmdArray[] = new String[CMD_BUFFER_MIN_SIZE];
static int cmdLineLen = 0;
static ASprite sprite = new ASprite(); ;
static ACTION kActions[] =
{
ACTION.kLoad,
ACTION.kExportBSprite,
ACTION.kCreatePackage,
ACTION.kCreateInterface,
ACTION.kCreatStruct,
ACTION.kExportFramesID,
ACTION.kJavaExportFramesID,
ACTION.kExportAnimsID,
ACTION.kJavaExportAnimsID,
ACTION.kCloseStruct,
ACTION.kCloseInterface,
ACTION.kCloseInterface,
ACTION.kSetToolsPath,
ACTION.kSetRGBImagePath,
};
static String kActionNames[] =
{"Load",
"ExportBSprite",
"CreatePackage",
"CreateInterface",
"CreateStruct",
"ExportFramesID",
"JavaExportFramesID",
"ExportAnimsID",
"JavaExportAnimsID",
"ColseStruct",
"CloseInterface",
"JavaCloseInterface",
"SetToolsPath",
"SetRGBImagePath",
};
public static booleangetExportFilesConfig(String strConfigFile)
{
DataInputStream in;
int i = 0;
try
{
in = new DataInputStream(new FileInputStream(strConfigFile));
String strTemp = in.readLine();
while (strTemp != null)
{
strTemp = strTemp.trim();
if (strTemp == null || strTemp.length() == 0 || strTemp.startsWith("\\\\"))
{
} else
{
if (i > strCmdArray.length - 1)
{
String strArrayTemp[] = new String[strCmdArray.length + CMD_BUFFER_MIN_SIZE];
for (int j = 0; j < i; j++)
{
strArrayTemp[j] = strCmdArray[i];
}
strCmdArray = strArrayTemp;
}
strCmdArray[i++] = strTemp;
}
strTemp = in.readLine();
}
} catch (Exception e)
{
e.printStackTrace();
return false;
}
cmdLineLen = i;
return true;
}
static voiddoExport()
{
ASprite sprite = new ASprite();
String strCmd;
String strParameter[];
ACTION action;
boolean result = false;
ACTION lastOkKeyAction = ACTION.kNone;
for (int i = 0; i < cmdLineLen; i++)
{
strCmd = strCmdArray[i];
strParameter = getCmdParameter(strCmd);
action = getCmd(strCmd);
if (action == ACTION.kLoad)
{
result = doLoad(strParameter);
if (result)
{
lastOkKeyAction = action;
}
} else if (action == ACTION.kExportBSprite)
{
if (lastOkKeyAction == ACTION.kLoad)
{
result = doExportBSprite(strParameter);
if (result)
{
lastOkKeyAction = action;
}
}
}
else if (action==ACTION.kCreatePackage)
{
doCreatePackage(strParameter);
}
else if (action == ACTION.kCreateInterface)
{
result = doCreateInterface(strParameter);
}
else if (action == ACTION.kCreatStruct)
{
doCreateStruct(strParameter);
} else if (action == ACTION.kExportFramesID)
{
doExportFramesID(strParameter);
} else if (action == ACTION.kExportAnimsID)
{
doExportAnims(strParameter);
} else if (action == ACTION.kCloseStruct)
{
doCloseStruct();
} else if (action == ACTION.kCloseInterface)
{
doColseInterface();
} else if (action == ACTION.kSetToolsPath)
{
doSetToolsPath(strParameter);
} else if (action == ACTION.kSetRGBImagePath)
{
doSetRGBImagePath(strParameter);
}
}
}
static String[]getCmdParameter(String str)
{
int start = str.indexOf("(");
int end = str.indexOf(")");
if (start + 1 == end)
{
return null;
}
str = str.substring(start + 1, end);
String strParam[] = str.split(",");
if (strParam != null)
{
for (int i = 0; i < strParam.length; i++)
{
strParam[i] = strParam[i].replaceAll("\"", "");
strParam[i] = strParam[i].replaceAll("\"", "");
}
;
}
return strParam;
}
static ACTION getCmd(String str)
{
int end = str.indexOf("(");
if (end == -1 || end == 0)
{
return ACTION.kNone;
}
str = str.substring(0, end);
for (int i = 0; i < kActionNames.length; i++)
{
if (str.equalsIgnoreCase(kActionNames[i]))
{
return kActions[i];
}
}
return ACTION.kNone;
}
static boolean doLoad(String parm[])
{
sprite.reset();
return sprite.Load(Util.trimSemicolon(parm[0]));
}
static String kExportFlagsName[] = {"BS_MODULE_IMAGES"};
static int kExportFlags[] = {ASprite.BS_MODULE_IMAGES};
static boolean doExportBSprite(String parm[])
{
int flag = 0;
String strFlag[] = parm[1].trim().split("\\|");
for (int i = 0; i < strFlag.length; i++)
{
for (int j = 0; j < kExportFlagsName.length; j++)
{
if (strFlag[i].equalsIgnoreCase(kExportFlagsName[j]))
{
flag = flag | kExportFlags[j];
}
}
}
return sprite.ExportBSprite(parm[0], flag);
}
static boolean doCreatePackage(String parm[])
{
boolean val=false;
try
{
String packPath =null;
packPath=parm[0];
while(packPath.contains("."))
packPath=packPath.replace(".","\\");
File file=new File(packPath);
if(!file.exists())
{
val= file.mkdirs();
}
ASprite.setJavaPackageName(parm[0]);
ASprite.setJavaPackagePath(packPath);
Util.setDataWriteFormat(Util.kJavaFormatWrite);
} catch (Exception e)
{
e.printStackTrace();
return false;
}
return val;
}
static boolean doCreateInterface(String parm[])
{
return sprite.CreateInterface(Util.trimSemicolon(parm[0]));
}
static boolean doCreateStruct(String parm[])
{
return sprite.CreateStruct(Util.trimSemicolon(parm[0]));
}
static boolean doExportFramesID(String parm[])
{
return sprite.ExportFramesID(Util.trimSemicolon(parm[0]));
}
static boolean doExportAnims(String parm[])
{
return sprite.ExportAnimsID(Util.trimSemicolon(parm[0]));
}
static boolean doCloseStruct()
{
return sprite.CloseStruct();
}
static boolean doColseInterface()
{
return sprite.CloseInterface();
}
static String toolsPath = "";
static boolean doSetToolsPath(String parm[])
{
String dirName = Util.trimSemicolon(parm[0]);
try
{
File file = new File(dirName);
if (!file.exists())
{
return false;
}
if (!file.isDirectory())
{
return false;
}
StringBuffer strBuffer = new StringBuffer();
char c = 0;
for (int i = 0; i < dirName.length(); i++)
{
c = dirName.charAt(i);
if (c == '/')
{
strBuffer.append("\\");
} else
{
strBuffer.append(c);
}
}
toolsPath = strBuffer.toString();
} catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
static String rgbImagePath = "";
static boolean doSetRGBImagePath(String parm[])
{
String dirName = Util.trimSemicolon(parm[0]);
try
{
File file = new File(dirName);
if (!file.exists())
{
return false;
}
if (!file.isDirectory())
{
return false;
}
StringBuffer strBuffer = new StringBuffer();
char c = 0;
for (int i = 0; i < dirName.length(); i++)
{
c = dirName.charAt(i);
if (c == '/')
{
strBuffer.append("\\");
} else
{
strBuffer.append(c);
}
}
rgbImagePath = strBuffer.toString();
} catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
final static String kBbmSourceFileName = "temp.png";
static byte[] getRGBFromBBM(String imgFileName)
{
byte data[] = null;
DataInputStream is = null;
ByteArrayOutputStream byteos = null;
DataOutputStream outPutFileStream = null;
DataOutputStream os = null;
String cmdarray[] = new String[4];
String rgbFileName = null;
File imgFile = new File(imgFileName);
if (!imgFile.exists())
{
return null;
}
rgbFileName = imgFile.getName();
cmdarray[0] = "copy " + rgbImagePath + "\\" + rgbFileName + " " + kBbmSourceFileName + " /y";
cmdarray[1] = toolsPath + "\\ConvPng2.exe -bbm " + kBbmSourceFileName;
cmdarray[2] = toolsPath + "\\CovertBbmColors.exe";
cmdarray[3] = "del " + kBbmSourceFileName;
try
{
String execFileName = "exec.bat";
File execFile = new File(execFileName);
if (!execFile.exists())
{
execFile.createNewFile();
}
execFile.deleteOnExit();
byteos = new ByteArrayOutputStream();
os = new DataOutputStream(byteos);
for (int i = 0; i < cmdarray.length; i++)
{
os.writeBytes(cmdarray[i]);
os.writeBytes("\n");
}
outPutFileStream = new DataOutputStream(new FileOutputStream(execFileName));
outPutFileStream.write(byteos.toByteArray());
outPutFileStream.close();
os.close();
Process process = Runtime.getRuntime().exec(execFile.getAbsolutePath());
InputStream in = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = br.readLine();
while (line != null)
{
System.out.println(line);
line = br.readLine();
}
String bbmFileName = kBbmSourceFileName.substring(0, kBbmSourceFileName.indexOf(".")) + ".bbm";
File file = new File(bbmFileName);
if (!file.exists())
{
return null;
}
file.deleteOnExit();
is = new DataInputStream(new FileInputStream(bbmFileName));
byte c1 = is.readByte();
byte c2 = is.readByte();
byte c3 = is.readByte();
if ((c1 != 'B') || (c2 != 'B') || (c3 != 'M'))
{
return null;
}
byte flag = is.readByte();
is.readByte();
int w = Util.readShort2(is);
int h = Util.readShort2(is);
int size = w * h;
if (flag == 24)
{
size = size * 3;
} else if (flag == 32)
{
size = size * 4;
} else
{
size = size * 4;
}
data = new byte[size + 1];
data[0] = flag;
is.read(data, 1, size);
is.close();
} catch (Exception e)
{
Util.closeInputStream(is);
e.printStackTrace();
}
return data;
}
}