private void loadMapContent(EMapStartResources startResources, PlayerSetting[] playerSettings) throws MapLoadException { try { // - the map buffer of the class may is closed and need to reopen! mapContent.reOpen(this.listedMap.getInputStream()); } catch (Exception e) { throw new MapLoadException(e); } // - load all common map information mapContent.loadMapResources(); mapContent.readBasicMapInformation(); // - read the landscape mapContent.readMapData(); // - read Stacks mapContent.readStacks(); // - read Settlers mapContent.readSettlers(); // - read the buildings mapContent.readBuildings(); // - add player resources mapContent.addStartTowerMaterialsAndSettlers(startResources, playerSettings); } }
private void readPlayerInfo() throws MapLoadException { MapResourceInfo filePart = findAndDecryptFilePartSafe(EOriginalMapFilePartType.PLAYER_INFO); // - file position int pos = filePart.offset; for (int i = 0; i < mapData.getPlayerCount(); i++) { int nation = readBEIntFrom(pos); pos += 4; int startX = readBEIntFrom(pos); pos += 4; int startY = readBEIntFrom(pos); pos += 4; String playerName = readCStrFrom(pos, 33); pos += 33; mapData.setPlayer(i, startX, startY, nation, playerName); } }
fileChecksum = readBEIntFrom(0); readMapInfo(); readPlayerInfo(); readMapQuestText(); readMapQuestTip(); this.previewImage = getPreviewImage(previewWidth, previewHeight); this.previewWidth = (short) previewWidth; this.previewHeight = (short) previewHeight; MapResourceInfo filePart = findResource(EOriginalMapFilePartType.AREA); widthHeight = readBEIntFrom(pos);
Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.BUILDINGS); int buildingsCount = readBEIntFrom(pos); pos += 4; int party = readByteFrom(pos++); // - Party starts with 0 int buildingType = readByteFrom(pos++); int posX = readBEWordFrom(pos); pos += 2; int posY = readBEWordFrom(pos); pos += 2; int countSword1 = readHighNibbleFrom(pos); int countSword2 = readLowNibbleFrom(pos); pos++; int countArcher2 = readHighNibbleFrom(pos); int countArcher3 = readLowNibbleFrom(pos); pos++; int countSword3 = readHighNibbleFrom(pos); int countArcher1 = readLowNibbleFrom(pos); pos++; int countSpear3 = readHighNibbleFrom(pos); int countSpear1 = readHighNibbleFrom(pos); int countSpear2 = readLowNibbleFrom(pos); pos++;
/** * Reads in the Map Data / Landscape and MapObjects like trees */ void readMapData() throws MapLoadException { MapResourceInfo filePart = findAndDecryptFilePartSafe(EOriginalMapFilePartType.AREA); // - file position int pos = filePart.offset; // - height and width are the same int widthHeight = readBEIntFrom(pos); pos += 4; // - init size of MapData mapData.setWidthHeight(widthHeight); // - points to read int dataCount = widthHeight * widthHeight; for (int i = 0; i < dataCount; i++) { mapData.setLandscapeHeight(i, readByteFrom(pos++)); mapData.setLandscape(i, readByteFrom(pos++)); mapData.setMapObject(i, readByteFrom(pos++)); readByteFrom(pos++); // - which Player is the owner of this position mapData.setAccessible(i, mapContent[pos++]); mapData.setResources(i, readHighNibbleFrom(pos), readLowNibbleFrom(pos)); pos++; } }
public OriginalMapLoader(IListedMap listedMap) throws MapLoadException { this.listedMap = listedMap; fileName = listedMap.getFileName(); creationDate = getCreationDateFrom(listedMap); try { mapContent = new OriginalMapFileContentReader(listedMap.getInputStream()); } catch (IOException e) { throw new MapLoadException(e); } if (!CommonConstants.DISABLE_ORIGINAL_MAPS_CHECKSUM && !mapContent.isChecksumValid()) { throw new MapLoadException("Checksum of original map (" + fileName + ") is not valid!"); } // - read all important information from file mapContent.loadMapResources(); mapContent.readBasicMapInformation(MapFileHeader.PREVIEW_IMAGE_SIZE, MapFileHeader.PREVIEW_IMAGE_SIZE); // - free the DataBuffer mapContent.freeBuffer(); isMapOK = true; }
void readSettlers() throws MapLoadException { Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.SETTLERS); if (filePartOptional.isPresent()) { MapResourceInfo filePart = filePartOptional.get(); // - file position int pos = filePart.offset; // - Number of buildings int settlerCount = readBEIntFrom(pos); pos += 4; // - safety check if ((settlerCount * 6 > filePart.size) || (settlerCount < 0)) { throw new MapLoadException("wrong number of settlers in map File: " + settlerCount); } // - read all Stacks for (int i = 0; i < settlerCount; i++) { int party = readByteFrom(pos++); int settlerType = readByteFrom(pos++); int posX = readBEWordFrom(pos); pos += 2; int posY = readBEWordFrom(pos); pos += 2; // ------------- // - update data mapData.setSettler(posX, posY, settlerType, party); } } }
private String readMapQuestTip() throws MapLoadException { if (mapQuestTip != null) { return mapQuestTip; } return findAndDecryptFilePart(EOriginalMapFilePartType.QUEST_TIP).map(filePart -> { mapQuestTip = readCStrFrom(filePart.offset, filePart.size); return mapQuestTip; }).orElse(""); }
MapResourceInfo filePart = findResource(EOriginalMapFilePartType.PREVIEW); int widthAndHeight = readBEWordFrom(pos); pos += 2; @SuppressWarnings("unused") int unknown = readBEWordFrom(pos); pos += 2;
private void readMapInfo() throws MapLoadException { MapResourceInfo filePartOptional = findAndDecryptFilePartSafe(EOriginalMapFilePartType.MAP_INFO); // - file position int pos = filePartOptional.offset; // ---------------------------------- // - read mapType (single / multiplayer map?) int mapType = readBEIntFrom(pos); pos += 4; if (mapType == 1) { isSinglePlayerMap = true; } else if (mapType == 0) { isSinglePlayerMap = false; } else { throw new MapLoadException("wrong value for 'isSinglePlayerMap' " + Integer.toString(mapType) + " in mapfile!"); } // ---------------------------------- // - read Player count int playerCount = readBEIntFrom(pos); pos += 4; mapData.setPlayerCount(playerCount); // ---------------------------------- // - read start resources int startResourcesValue = readBEIntFrom(pos); this.startResources = EMapStartResources.fromMapValue(startResourcesValue); }
OriginalMapFileContentReader(InputStream originalMapFile) throws IOException { // - init players mapData.setPlayerCount(1); // - read File into buffer mapContent = getBytesFromInputStream(originalMapFile); }
@Override public short[] getImage() { return mapContent.getPreviewImage(); }
public void addStartTowerMaterialsAndSettlers(EMapStartResources startResources) { addStartTowerMaterialsAndSettlers(startResources, null); }
@Override public String getMapId() { return mapContent.getChecksum() + getMapName(); }
private Optional<MapResourceInfo> findAndDecryptFilePart(EOriginalMapFilePartType partType) throws MapLoadException { MapResourceInfo filePart = findResource(partType); if ((filePart == null) || (filePart.size == 0)) { return Optional.empty(); } // Decrypt this resource if necessary filePart.doDecrypt(); // Call consumer return Optional.of(filePart); }
private MapResourceInfo findAndDecryptFilePartSafe(EOriginalMapFilePartType partType) throws MapLoadException { Optional<MapResourceInfo> filePart = findAndDecryptFilePart(partType); if (filePart.isPresent()) { return filePart.get(); } else { throw new MapLoadException("No " + partType + " information available in mapfile!"); } }
void readStacks() throws MapLoadException { Optional<MapResourceInfo> filePartOptional = findAndDecryptFilePart(EOriginalMapFilePartType.STACKS); if (filePartOptional.isPresent()) { MapResourceInfo filePart = filePartOptional.get(); // - file position int pos = filePart.offset; // - Number of buildings int stackCount = readBEIntFrom(pos); pos += 4; // - safety check if ((stackCount * 8 > filePart.size) || (stackCount < 0)) { throw new MapLoadException("wrong number of stacks in map File: " + stackCount); } // - read all Stacks for (int i = 0; i < stackCount; i++) { int posX = readBEWordFrom(pos); pos += 2; int posY = readBEWordFrom(pos); pos += 2; int stackType = readByteFrom(pos++); int count = readByteFrom(pos++); pos += 2; // not used - maybe: padding to size of 8 (2 INTs) // ------------- // - update data mapData.setStack(posX, posY, stackType, count); } } }
String readMapQuestText() throws MapLoadException { if (mapQuestText != null) { return mapQuestText; } return findAndDecryptFilePart(EOriginalMapFilePartType.QUEST_TEXT) .map(filePart -> { mapQuestText = readCStrFrom(filePart.offset, filePart.size); return mapQuestText; }) .orElse(""); }
void reOpen(InputStream originalMapFile) throws IOException { // - read File into buffer mapContent = getBytesFromInputStream(originalMapFile); // - reset Crypt Info for (MapResourceInfo element : resources.values()) { element.resetDecryptedFlag(); } }