| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.imgfmt.app.mdr; |
| 14 |
|
| 15 |
import java.text.Collator; |
| 16 |
import java.util.ArrayList; |
| 17 |
import java.util.Collections; |
| 18 |
import java.util.HashMap; |
| 19 |
import java.util.List; |
| 20 |
import java.util.Map; |
| 21 |
|
| 22 |
import uk.me.parabola.imgfmt.Utils; |
| 23 |
import uk.me.parabola.imgfmt.app.ImgFileWriter; |
| 24 |
import uk.me.parabola.imgfmt.app.srt.MultiSortKey; |
| 25 |
import uk.me.parabola.imgfmt.app.srt.Sort; |
| 26 |
import uk.me.parabola.imgfmt.app.srt.SortKey; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class Mdr5 extends MdrMapSection { |
| 37 |
private ArrayList<Mdr5Record> allCities = new ArrayList<>(); |
| 38 |
private ArrayList<Mdr5Record> cities; |
| 39 |
private int maxCityIndex; |
| 40 |
private int localCitySize; |
| 41 |
private int mdr20PointerSize = 0; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private int[] mdr20s; |
| 46 |
|
| 47 |
public Mdr5(MdrConfig config) { |
| 48 |
setConfig(config); |
| 49 |
} |
| 50 |
|
| 51 |
public void addCity(Mdr5Record record) { |
| 52 |
assert record.getMapIndex() != 0; |
| 53 |
allCities.add(record); |
| 54 |
if (record.getCityIndex() > maxCityIndex) |
| 55 |
maxCityIndex = record.getCityIndex(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@Override |
| 62 |
public void preWriteImpl() { |
| 63 |
allCities.trimToSize(); |
| 64 |
localCitySize = Utils.numberToPointerSize(maxCityIndex + 1); |
| 65 |
Sort sort = getConfig().getSort(); |
| 66 |
genCitiesAndMdr20s(sort); |
| 67 |
|
| 68 |
calcMdr20SortPos(sort); |
| 69 |
calcMdr21SortPos(sort); |
| 70 |
calcMdr22SortPos(sort); |
| 71 |
} |
| 72 |
|
| 73 |
private void genCitiesAndMdr20s(Sort sort) { |
| 74 |
List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size()); |
| 75 |
Map<String, byte[]> regionCache = new HashMap<>(); |
| 76 |
Map<String, byte[]> countryCache = new HashMap<>(); |
| 77 |
for (Mdr5Record m : allCities) { |
| 78 |
if (m.getName() == null) |
| 79 |
continue; |
| 80 |
|
| 81 |
|
| 82 |
SortKey<Mdr5Record> sortKey = sort.createSortKey(m, m.getName()); |
| 83 |
SortKey<Mdr5Record> regionKey = sort.createSortKey(null, m.getRegionName(), 0, regionCache); |
| 84 |
SortKey<Mdr5Record> countryKey = sort.createSortKey(null, m.getCountryName(), m.getMapIndex(), countryCache); |
| 85 |
sortKey = new MultiSortKey<>(sortKey, regionKey, countryKey); |
| 86 |
sortKeys.add(sortKey); |
| 87 |
} |
| 88 |
regionCache = null; |
| 89 |
countryCache = null; |
| 90 |
sortKeys.sort(null); |
| 91 |
|
| 92 |
cities = new ArrayList<>(sortKeys.size()); |
| 93 |
Collator collator = sort.getCollator(); |
| 94 |
|
| 95 |
int count = 0; |
| 96 |
Mdr5Record lastCity = null; |
| 97 |
|
| 98 |
mdr20s = new int[sortKeys.size()+1]; |
| 99 |
int mdr20count = 0; |
| 100 |
|
| 101 |
for (SortKey<Mdr5Record> key : sortKeys) { |
| 102 |
Mdr5Record c = key.getObject(); |
| 103 |
c.setMdr20set(mdr20s); |
| 104 |
|
| 105 |
if (!c.isSameByName(collator, lastCity)) |
| 106 |
mdr20count++; |
| 107 |
c.setMdr20Index(mdr20count); |
| 108 |
|
| 109 |
if (c.isSameByMapAndName(collator, lastCity)) { |
| 110 |
c.setGlobalCityIndex(count); |
| 111 |
} else { |
| 112 |
count++; |
| 113 |
c.setGlobalCityIndex(count); |
| 114 |
cities.add(c); |
| 115 |
|
| 116 |
lastCity = c; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private void calcMdr20SortPos(Sort sort) { |
| 126 |
List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size()); |
| 127 |
Map<String, byte[]> regionCache = new HashMap<>(); |
| 128 |
Map<String, byte[]> countryCache = new HashMap<>(); |
| 129 |
for (Mdr5Record m : allCities) { |
| 130 |
if (m.getName() == null) |
| 131 |
continue; |
| 132 |
|
| 133 |
|
| 134 |
SortKey<Mdr5Record> sortKey = sort.createSortKey(m, m.getName()); |
| 135 |
SortKey<Mdr5Record> regionKey = sort.createSortKey(null, m.getRegionName(), 0, regionCache); |
| 136 |
SortKey<Mdr5Record> countryKey = sort.createSortKey(null, m.getCountryName(), 0, countryCache); |
| 137 |
sortKey = new MultiSortKey<>(sortKey, regionKey, countryKey); |
| 138 |
sortKeys.add(sortKey); |
| 139 |
} |
| 140 |
regionCache = null; |
| 141 |
countryCache = null; |
| 142 |
sortKeys.sort(null); |
| 143 |
|
| 144 |
SortKey<Mdr5Record> lastKey = null; |
| 145 |
int pos = 0; |
| 146 |
for (SortKey<Mdr5Record> key : sortKeys) { |
| 147 |
Mdr5Record c = key.getObject(); |
| 148 |
if (lastKey == null || key.compareTo(lastKey) != 0) |
| 149 |
pos++; |
| 150 |
c.setMdr20SortPos(pos); |
| 151 |
lastKey = key; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
private void calcMdr21SortPos(Sort sort) { |
| 159 |
List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size()); |
| 160 |
for (Mdr5Record m : allCities) { |
| 161 |
if (m.getRegionName() == null) |
| 162 |
continue; |
| 163 |
|
| 164 |
|
| 165 |
sortKeys.add(sort.createSortKey(m, m.getRegionName())); |
| 166 |
} |
| 167 |
sortKeys.sort(null); |
| 168 |
|
| 169 |
SortKey<Mdr5Record> lastKey = null; |
| 170 |
int pos = 0; |
| 171 |
for (SortKey<Mdr5Record> key : sortKeys) { |
| 172 |
Mdr5Record c = key.getObject(); |
| 173 |
if (lastKey == null || key.compareTo(lastKey) != 0) |
| 174 |
pos++; |
| 175 |
c.setMdr21SortPos(pos); |
| 176 |
lastKey = key; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
private void calcMdr22SortPos(Sort sort) { |
| 185 |
List<SortKey<Mdr5Record>> sortKeys = new ArrayList<>(allCities.size()); |
| 186 |
for (Mdr5Record m : allCities) { |
| 187 |
if (m.getCountryName() == null) |
| 188 |
continue; |
| 189 |
|
| 190 |
|
| 191 |
SortKey<Mdr5Record> countryKey = sort.createSortKey(m, m.getCountryName()); |
| 192 |
sortKeys.add(countryKey); |
| 193 |
} |
| 194 |
sortKeys.sort(null); |
| 195 |
|
| 196 |
SortKey<Mdr5Record> lastKey = null; |
| 197 |
int pos = 0; |
| 198 |
for (SortKey<Mdr5Record> key : sortKeys) { |
| 199 |
Mdr5Record c = key.getObject(); |
| 200 |
if (lastKey == null || key.compareTo(lastKey) != 0) |
| 201 |
pos++; |
| 202 |
c.setMdr22SortPos(pos); |
| 203 |
lastKey = key; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
public void writeSectData(ImgFileWriter writer) { |
| 208 |
Mdr5Record lastCity = null; |
| 209 |
boolean hasString = hasFlag(0x8); |
| 210 |
boolean hasRegion = hasFlag(0x4); |
| 211 |
Collator collator = getConfig().getSort().getCollator(); |
| 212 |
for (Mdr5Record city : cities) { |
| 213 |
int gci = city.getGlobalCityIndex(); |
| 214 |
addIndexPointer(city.getMapIndex(), gci); |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
int flag = 0; |
| 219 |
int mapIndex = city.getMapIndex(); |
| 220 |
int region = city.getRegionIndex(); |
| 221 |
|
| 222 |
|
| 223 |
if (!city.isSameByName(collator, lastCity)) { |
| 224 |
flag = 0x800000; |
| 225 |
lastCity = city; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
putMapIndex(writer, mapIndex); |
| 230 |
putLocalCityIndex(writer, city.getCityIndex()); |
| 231 |
writer.put3u(flag | city.getLblOffset()); |
| 232 |
if (hasRegion) |
| 233 |
writer.put2u(region); |
| 234 |
if (hasString) |
| 235 |
putStringOffset(writer, city.getStringOffset()); |
| 236 |
if (mdr20PointerSize > 0) |
| 237 |
writer.putNu(mdr20PointerSize, city.getMdr20()); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private void putLocalCityIndex(ImgFileWriter writer, int cityIndex) { |
| 246 |
writer.putNu(localCitySize, cityIndex); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public int getItemSize() { |
| 255 |
PointerSizes sizes = getSizes(); |
| 256 |
int size = sizes.getMapSize() |
| 257 |
+ localCitySize |
| 258 |
+ 3 |
| 259 |
+ mdr20PointerSize; |
| 260 |
if (hasFlag(0x4)) |
| 261 |
size += 2; |
| 262 |
if (hasFlag(0x8)) |
| 263 |
size += sizes.getStrOffSize(); |
| 264 |
return size; |
| 265 |
} |
| 266 |
|
| 267 |
protected int numberOfItems() { |
| 268 |
return cities.size(); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public int getExtraValue() { |
| 284 |
int val = (localCitySize - 1); |
| 285 |
|
| 286 |
if (isForDevice() ) { |
| 287 |
if (!getConfig().getSort().isMulti()) |
| 288 |
val |= 0x40; |
| 289 |
} else { |
| 290 |
val |= 0x04; |
| 291 |
val |= 0x08; |
| 292 |
} |
| 293 |
val |= 0x10; |
| 294 |
if (getSizes().getNumberOfItems(20) > 0) { |
| 295 |
mdr20PointerSize = getSizes().getMdr20Size(); |
| 296 |
val |= 0x100; |
| 297 |
} |
| 298 |
return val; |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
protected void releaseMemory() { |
| 303 |
allCities = null; |
| 304 |
cities = null; |
| 305 |
mdr20s = null; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public List<Mdr5Record> getCities() { |
| 313 |
return Collections.unmodifiableList(allCities); |
| 314 |
} |
| 315 |
|
| 316 |
public List<Mdr5Record> getSortedCities() { |
| 317 |
return Collections.unmodifiableList(cities); |
| 318 |
} |
| 319 |
} |