diff --git a/.classpath b/.classpath index 2b09a45..49e600f 100644 --- a/.classpath +++ b/.classpath @@ -2,7 +2,6 @@ - diff --git a/src/com/projectswg/tools/stf/StfSearcher.java b/src/com/projectswg/tools/stf/StfSearcher.java index 509d58b..aab2f99 100644 --- a/src/com/projectswg/tools/stf/StfSearcher.java +++ b/src/com/projectswg/tools/stf/StfSearcher.java @@ -29,9 +29,6 @@ import javax.swing.UIManager; import javax.swing.JPanel; import javax.swing.JTextPane; -import engine.clientdata.StfTable; -import engine.clientdata.StfTable.Pair; - import java.awt.BorderLayout; import java.io.File; import java.io.IOException; @@ -46,6 +43,8 @@ import java.awt.event.WindowListener; import javax.swing.JScrollPane; import javax.swing.JCheckBox; +import com.projectswg.tools.stf.StfTable.Pair; + public class StfSearcher { private static StfSearcher searcher; diff --git a/src/com/projectswg/tools/stf/StfTable.java b/src/com/projectswg/tools/stf/StfTable.java new file mode 100644 index 0000000..b964e45 --- /dev/null +++ b/src/com/projectswg/tools/stf/StfTable.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Part of NGECore2 + * Copyright (c) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + ******************************************************************************/ +package com.projectswg.tools.stf; + +import java.io.IOException; +import java.nio.ByteOrder; +import java.util.ArrayList; +import java.util.List; + +import org.apache.mina.core.buffer.IoBuffer; + +/* + * Stf files don't appear to use the IFF format. + * + * ID 0 will probably always be null because they start at 1. + */ +public class StfTable { + + private String[][] orderedTable; + private List> disorderedTable; + + public class Pair { + + private K key; + private V value; + + public Pair(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + } + + public StfTable() { + + } + + public StfTable(String filePath) throws IOException { + readFile(filePath); + } + + public void readFile(String filePath) throws IOException { + java.io.FileInputStream stf = new java.io.FileInputStream(filePath); + + IoBuffer buffer = IoBuffer.allocate(stf.available(), false); + + buffer.setAutoExpand(true); + buffer.order(ByteOrder.LITTLE_ENDIAN); + + byte[] buf = new byte[1024]; + + for (int i = stf.read(buf); i != -1; i = stf.read(buf)) { + buffer.put(buf, 0, i); + } + + buffer.flip(); + + buffer.getInt(); // Size? + + buffer.get(); // isMore? + + int arrayCount = buffer.getInt(); + + int rowCount = buffer.getInt(); + + orderedTable = new String[arrayCount][2]; + disorderedTable = new ArrayList>(); + + for (int i = 0; i < rowCount; i++) { + int id = buffer.getInt(); + buffer.getInt(); + String value = ""; + value = StringUtilities.getUnicodeString(buffer, true); + orderedTable[id][0] = null; + orderedTable[id][1] = value; + } + + for (int i = 0; i < rowCount; i++) { + int id = buffer.getInt(); + String name = StringUtilities.getAsciiString(buffer, true); + orderedTable[id][0] = name; + disorderedTable.add(new Pair(name, orderedTable[id][1])); + } + + stf.close(); + } + + public int getRowCount() { + return ((orderedTable == null) ? 0 : orderedTable.length); + } + + public int getColumnCount() { + return ((orderedTable == null) ? 0 : 3); + } + + /* + * @param id Iteration number + * + * @returns String's key-value pair from an alphanumeric list + */ + public Pair getString(int id) { + return disorderedTable.get(id); + } + + /* + * @param id Identifying number of the string from the .stf file, unlike above + * + * @returns String's key-value pair from an unordered list + */ + public Pair getStringById(int id) { + return new Pair(orderedTable[id][0], orderedTable[id][1]); + } + + /* + * @param name Name of the string to return + * + * @returns The value for this key, or null if the key is not found + */ + public String getString(String name) { + for (String[] columns : orderedTable) { + if (columns[0].equals(name)) { + return columns[1]; + } + } + + return null; + } + +} diff --git a/src/com/projectswg/tools/stf/StringUtilities.java b/src/com/projectswg/tools/stf/StringUtilities.java new file mode 100644 index 0000000..c3b3e2e --- /dev/null +++ b/src/com/projectswg/tools/stf/StringUtilities.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Part of NGECore2 + * Copyright (c) 2013 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + ******************************************************************************/ +package com.projectswg.tools.stf; + +import java.io.UnsupportedEncodingException; +import java.nio.ByteOrder; + +import org.apache.mina.core.buffer.IoBuffer; + +public class StringUtilities { + + public static String getUnicodeString(IoBuffer buffer, boolean integer) { + return getString(buffer, "UTF-16LE", integer); + } + + public static String getAsciiString(IoBuffer buffer, boolean integer) { + return getString(buffer, "US-ASCII", integer); + } + + private static String getString(IoBuffer buffer, String charFormat, boolean integer) { + String result; + int length; + + if (charFormat.equals("UTF-16LE")) { + if (integer) { + length = buffer.order(ByteOrder.LITTLE_ENDIAN).getInt() * 2; + } else { + length = buffer.order(ByteOrder.LITTLE_ENDIAN).getInt(); + } + } else { + if (integer) { + length = buffer.order(ByteOrder.LITTLE_ENDIAN).getInt(); + } else { + length = buffer.order(ByteOrder.LITTLE_ENDIAN).getShort(); + } + } + + int bufferPosition = buffer.position(); + + try { + result = new String(buffer.array(), bufferPosition, length, charFormat); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + return ""; + } + + buffer.position(bufferPosition + length); + + return result; + } +}