package jsettlers.logic.map;

import jsettlers.logic.map.save.loader.MapLoader;

import java.io.*;
import java.util.Vector;

/**
 * @author codingberlin
 */
public class OriginalMapLoader {
	public MapLoader loadOriginalMap(File originalMapFile) {
		try {
			OriginalMapFileContent mapContent = new OriginalMapFileContent(originalMapFile);

			if (!mapContent.isChecksumValid()) {
				System.out.println("Checksum of original map was not valid!");
				return null;
			}
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public class OriginalMapFileContent {

		byte[] mapContent;

		public OriginalMapFileContent(File originalMapFile) throws IOException {
			DataInputStream dis = new DataInputStream(new FileInputStream(originalMapFile));
			Vector<Byte> mapContent = new Vector<Byte>();

			while (dis.available() > 0) {
				byte currentByte = (byte) dis.readUnsignedByte();
				mapContent.add(currentByte);
			}
			dis.close();

			this.mapContent = new byte[mapContent.size()];
			for (int i = 0; i < mapContent.size(); i++) {
				this.mapContent[i] = mapContent.get(i);
			}
		}

		public int readBytesFrom(int numberOfBytes, int offset) {
			int result = 0;
			for (int i = offset; i < offset+numberOfBytes; i++) {
				result += mapContent[i] << (i - offset) * 8;
			}
			return result;
		}

		boolean isChecksumValid() {
			long fileChecksum = readBytesFrom(4, 0);
			System.out.println(fileChecksum);
			int count = (mapContent.length/4) - 2;

			int currentChecksum = 0;
			for (int i = 8; i < count; i+=4) {
				int currentInt = readBytesFrom(4, i);
				if (currentInt < 0) {
				}
				currentChecksum = ((currentChecksum >> 31) | ((currentChecksum << 1) ^ currentInt));
				if (currentChecksum == fileChecksum) {
					System.out.println("Treffer " + i);
				}
			}

			if (currentChecksum != fileChecksum) {
				return false;
			}

			return true;
		}

	}

}

package jsettlers.logic.map;

import jsettlers.logic.map.save.loader.MapLoader;

import java.io.*;
import java.util.Vector;

/**
 * @author codingberlin
 */
public class OriginalMapLoader {
	public MapLoader loadOriginalMap(File originalMapFile) {
		try {
			OriginalMapFileContent mapContent = new OriginalMapFileContent(originalMapFile);

			if (!mapContent.isChecksumValid()) {
				System.out.println("Checksum of original map was not valid!");
				return null;
			}
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public class OriginalMapFileContent {

		long[] mapContent;

		public OriginalMapFileContent(File originalMapFile) throws IOException {
			DataInputStream dis = new DataInputStream(new FileInputStream(originalMapFile));
			Vector<Long> mapContent = new Vector<Long>();

			while (dis.available() > 0) {
				long currentByte = dis.readUnsignedByte();
				mapContent.add(currentByte);
			}
			dis.close();

			this.mapContent = new long[mapContent.size()];
			for (int i = 0; i < mapContent.size(); i++) {
				this.mapContent[i] = mapContent.get(i);
			}
		}

		public int readBytesFrom(int numberOfBytes, int offset) {
			int result = 0;
			for (int i = offset; i < offset+numberOfBytes; i++) {
				result += mapContent[i] << (i - offset) * 8;
			}
			return result;
		}

		boolean isChecksumValid() {
			long fileChecksum = readBytesFrom(4, 0);
			System.out.println(fileChecksum);
			int count = (mapContent.length/4) - 2;

			int currentChecksum = 0;
			for (int i = 8; i < count; i+=4) {
				int currentInt = readBytesFrom(4, i);
				currentChecksum = ((currentChecksum >> 31) | ((currentChecksum << 1) ^ currentInt));
				if (currentChecksum == fileChecksum) {
					System.out.println("Treffer " + i);
				}
			}

			if (currentChecksum != fileChecksum) {
				return false;
			}

			return true;
		}

	}

	public static void main(String[] args) {
		(new OriginalMapLoader()).loadOriginalMap(new File("/Users/stephanbauer/Documents/siedler3/Map/MULTI/a.map"));
	}

}

change byte into long to prevent interpreting the read bytes wrong due to unsigned/signed bit