|
| 1 | +/******************************************************************************* |
| 2 | + * Part of NGECore2 |
| 3 | + * Copyright (c) 2013 <Project SWG> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Lesser General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + ******************************************************************************/ |
| 19 | +package com.projectswg.tools.stf; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.ByteOrder; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.apache.mina.core.buffer.IoBuffer; |
| 27 | + |
| 28 | +/* |
| 29 | + * Stf files don't appear to use the IFF format. |
| 30 | + * |
| 31 | + * ID 0 will probably always be null because they start at 1. |
| 32 | + */ |
| 33 | +public class StfTable { |
| 34 | + |
| 35 | + private String[][] orderedTable; |
| 36 | + private List<Pair<String, String>> disorderedTable; |
| 37 | + |
| 38 | + public class Pair<K, V> { |
| 39 | + |
| 40 | + private K key; |
| 41 | + private V value; |
| 42 | + |
| 43 | + public Pair(K key, V value) { |
| 44 | + this.key = key; |
| 45 | + this.value = value; |
| 46 | + } |
| 47 | + |
| 48 | + public K getKey() { |
| 49 | + return key; |
| 50 | + } |
| 51 | + |
| 52 | + public V getValue() { |
| 53 | + return value; |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + public StfTable() { |
| 59 | + |
| 60 | + } |
| 61 | + |
| 62 | + public StfTable(String filePath) throws IOException { |
| 63 | + readFile(filePath); |
| 64 | + } |
| 65 | + |
| 66 | + public void readFile(String filePath) throws IOException { |
| 67 | + java.io.FileInputStream stf = new java.io.FileInputStream(filePath); |
| 68 | + |
| 69 | + IoBuffer buffer = IoBuffer.allocate(stf.available(), false); |
| 70 | + |
| 71 | + buffer.setAutoExpand(true); |
| 72 | + buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 73 | + |
| 74 | + byte[] buf = new byte[1024]; |
| 75 | + |
| 76 | + for (int i = stf.read(buf); i != -1; i = stf.read(buf)) { |
| 77 | + buffer.put(buf, 0, i); |
| 78 | + } |
| 79 | + |
| 80 | + buffer.flip(); |
| 81 | + |
| 82 | + buffer.getInt(); // Size? |
| 83 | + |
| 84 | + buffer.get(); // isMore? |
| 85 | + |
| 86 | + int arrayCount = buffer.getInt(); |
| 87 | + |
| 88 | + int rowCount = buffer.getInt(); |
| 89 | + |
| 90 | + orderedTable = new String[arrayCount][2]; |
| 91 | + disorderedTable = new ArrayList<Pair<String, String>>(); |
| 92 | + |
| 93 | + for (int i = 0; i < rowCount; i++) { |
| 94 | + int id = buffer.getInt(); |
| 95 | + buffer.getInt(); |
| 96 | + String value = ""; |
| 97 | + value = StringUtilities.getUnicodeString(buffer, true); |
| 98 | + orderedTable[id][0] = null; |
| 99 | + orderedTable[id][1] = value; |
| 100 | + } |
| 101 | + |
| 102 | + for (int i = 0; i < rowCount; i++) { |
| 103 | + int id = buffer.getInt(); |
| 104 | + String name = StringUtilities.getAsciiString(buffer, true); |
| 105 | + orderedTable[id][0] = name; |
| 106 | + disorderedTable.add(new Pair<String, String>(name, orderedTable[id][1])); |
| 107 | + } |
| 108 | + |
| 109 | + stf.close(); |
| 110 | + } |
| 111 | + |
| 112 | + public int getRowCount() { |
| 113 | + return ((orderedTable == null) ? 0 : orderedTable.length); |
| 114 | + } |
| 115 | + |
| 116 | + public int getColumnCount() { |
| 117 | + return ((orderedTable == null) ? 0 : 3); |
| 118 | + } |
| 119 | + |
| 120 | + /* |
| 121 | + * @param id Iteration number |
| 122 | + * |
| 123 | + * @returns String's key-value pair from an alphanumeric list |
| 124 | + */ |
| 125 | + public Pair<String, String> getString(int id) { |
| 126 | + return disorderedTable.get(id); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + * @param id Identifying number of the string from the .stf file, unlike above |
| 131 | + * |
| 132 | + * @returns String's key-value pair from an unordered list |
| 133 | + */ |
| 134 | + public Pair<String, String> getStringById(int id) { |
| 135 | + return new Pair<String, String>(orderedTable[id][0], orderedTable[id][1]); |
| 136 | + } |
| 137 | + |
| 138 | + /* |
| 139 | + * @param name Name of the string to return |
| 140 | + * |
| 141 | + * @returns The value for this key, or null if the key is not found |
| 142 | + */ |
| 143 | + public String getString(String name) { |
| 144 | + for (String[] columns : orderedTable) { |
| 145 | + if (columns[0].equals(name)) { |
| 146 | + return columns[1]; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return null; |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments