Made outline of region file
-Improved loading screen loading(so it doesnt crash)
-Implemented region file related things, but it doesnt quite work
    -Uses file header to try to look up location of data
    -Writes the data at the end of the file(I need info to how much it writes)
			
			
This commit is contained in:
		| @@ -64,11 +64,13 @@ public class ClientState { | |||||||
|  |  | ||||||
| 		setInstance(client); | 		setInstance(client); | ||||||
|  |  | ||||||
|  | 		MutableString t = new MutableStringLocalized("LayerText.Load"); | ||||||
|  | 		layer = new LayerTestText("Text",() -> {t.update(); return t.get();}); | ||||||
|  | 		 | ||||||
| 		ServerState.getInstance().getChunkManager().register((ChunksLoadStartListener)() -> { | 		ServerState.getInstance().getChunkManager().register((ChunksLoadStartListener)() -> { | ||||||
| 			if (firstLoad) | 			if (firstLoad) | ||||||
| 			{ | 			{ | ||||||
| 				MutableString t = new MutableStringLocalized("LayerText.Load"); | 				 | ||||||
| 				layer = new LayerTestText("Text",() -> {t.update(); return t.get();}); |  | ||||||
| 				GUI.addTopLayer(layer); | 				GUI.addTopLayer(layer); | ||||||
| 			}}); | 			}}); | ||||||
| 		 | 		 | ||||||
|   | |||||||
| @@ -27,6 +27,8 @@ import java.io.File; | |||||||
| import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||||
| import java.io.FileWriter; | import java.io.FileWriter; | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
|  | import java.io.RandomAccessFile; | ||||||
|  | import java.nio.channels.Channels; | ||||||
| import java.nio.file.Files; | import java.nio.file.Files; | ||||||
| import java.nio.file.Path; | import java.nio.file.Path; | ||||||
| import java.nio.file.Paths; | import java.nio.file.Paths; | ||||||
| @@ -53,14 +55,15 @@ public class TestWorldDiskIO { | |||||||
|  |  | ||||||
| 	private static final boolean ENABLE = true; | 	private static final boolean ENABLE = true; | ||||||
|  |  | ||||||
| 	private static final int maxSize = 1048576; | 	private static int maxSize = 1048576; | ||||||
| 	private static final int sectorSize = maxSize / 256; | 	private static int sectorSize = maxSize / 256; | ||||||
|  |  | ||||||
| 	private static final int bestFormat = 2; | 	private static final int bestFormat = 65536; | ||||||
|  |  | ||||||
| 	// private Map<Vec3i,Vec3i> regions = new HashMap<Vec3i,Vec3i>(); | 	// private Map<Vec3i,Vec3i> regions = new HashMap<Vec3i,Vec3i>(); | ||||||
| 	private static Vec3i regionSize; | 	private static Vec3i regionSize; | ||||||
| 	private static int chunksPerRegion; | 	private static int chunksPerRegion; | ||||||
|  | 	private static int offsetBytes; | ||||||
|  |  | ||||||
| 	private static int currentFormat = -1; | 	private static int currentFormat = -1; | ||||||
| 	private static String extension = ".null"; | 	private static String extension = ".null"; | ||||||
| @@ -71,15 +74,25 @@ public class TestWorldDiskIO { | |||||||
| 		return loc << 1; | 		return loc << 1; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	private static int intFromNat(int loc) // Possibly unused | 	/*private static int intFromNat(int loc) // Possibly unused | ||||||
| 	{ | 	{ | ||||||
| 		if ((loc & 1) == 1) | 		if ((loc & 1) == 1) | ||||||
| 			return -loc >> 1; | 			return -loc >> 1; | ||||||
| 		return loc >> 1; | 		return loc >> 1; | ||||||
|  | 	}*/ | ||||||
|  |  | ||||||
|  | 	private static Vec3i getRegion(Vec3i chunkLoc) { | ||||||
|  | 		return new Vec3i(natFromInt(chunkLoc.x/regionSize.x), natFromInt(chunkLoc.y/regionSize.y), natFromInt(chunkLoc.z/regionSize.z)); | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	private static Vec3i getRegionLoc(Vec3i chunkLoc) { | 	private static int mod(int a, int m) | ||||||
| 		return new Vec3i(natFromInt(chunkLoc.x), natFromInt(chunkLoc.y), natFromInt(chunkLoc.z)); | 	{ | ||||||
|  | 		return ((a%m)+m)%m; | ||||||
|  | 	} | ||||||
|  | 	 | ||||||
|  | 	private static Vec3i getRegionLoc(Vec3i chunkLoc) | ||||||
|  | 	{ | ||||||
|  | 		return new Vec3i(mod(chunkLoc.x,regionSize.x),mod(chunkLoc.y,regionSize.y),mod(chunkLoc.z,regionSize.z)); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public static void initRegions() { | 	public static void initRegions() { | ||||||
| @@ -103,10 +116,11 @@ public class TestWorldDiskIO { | |||||||
| 			currentFormat = format; | 			currentFormat = format; | ||||||
| 			extension = ".progressia_chunk"; | 			extension = ".progressia_chunk"; | ||||||
| 			break; | 			break; | ||||||
| 		case 2: | 		case 65536: | ||||||
| 			regionSize = new Vec3i(16); | 			regionSize = new Vec3i(16); | ||||||
| 			chunksPerRegion = 16 * 16 * 16; | 			chunksPerRegion = 16 * 16 * 16; | ||||||
| 			currentFormat = 2; | 			currentFormat = 65536; | ||||||
|  | 			offsetBytes = 4; | ||||||
| 			extension = ".progressia_region"; | 			extension = ".progressia_region"; | ||||||
| 			break; | 			break; | ||||||
| 		} | 		} | ||||||
| @@ -155,11 +169,11 @@ public class TestWorldDiskIO { | |||||||
|  |  | ||||||
| 				Files.createDirectories(SAVE_DIR); | 				Files.createDirectories(SAVE_DIR); | ||||||
|  |  | ||||||
| 				Vec3i saveCoords = getRegionLoc(chunk.getPosition()); | 				Vec3i saveCoords = getRegion(chunk.getPosition()); | ||||||
|  |  | ||||||
| 				Path path = SAVE_DIR.resolve( | 				Path path = SAVE_DIR.resolve( | ||||||
| 					String.format( | 					String.format( | ||||||
| 						"chunk_%+d_%+d_%+d" + extension, | 						"chunk_%d_%d_%d" + extension, | ||||||
| 						saveCoords.x, | 						saveCoords.x, | ||||||
| 						saveCoords.y, | 						saveCoords.y, | ||||||
| 						saveCoords.z | 						saveCoords.z | ||||||
| @@ -175,6 +189,59 @@ public class TestWorldDiskIO { | |||||||
| 					writeGenerationHint(chunk, output, server); | 					writeGenerationHint(chunk, output, server); | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
|  | 			else if (currentFormat == 65536) | ||||||
|  | 			{ | ||||||
|  | 				LOG.debug( | ||||||
|  | 					"Saving {} {} {}", | ||||||
|  | 					chunk.getPosition().x, | ||||||
|  | 					chunk.getPosition().y, | ||||||
|  | 					chunk.getPosition().z | ||||||
|  | 				); | ||||||
|  | 				 | ||||||
|  | 				Files.createDirectories(SAVE_DIR); | ||||||
|  | 				 | ||||||
|  | 				Vec3i saveCoords = getRegion(chunk.getPosition()); | ||||||
|  | 				 | ||||||
|  | 				Path path = SAVE_DIR.resolve( | ||||||
|  | 					String.format( | ||||||
|  | 						"%d_%d_%d" + extension, | ||||||
|  | 						saveCoords.x, | ||||||
|  | 						saveCoords.y, | ||||||
|  | 						saveCoords.z | ||||||
|  | 					) | ||||||
|  | 				); | ||||||
|  | 				 | ||||||
|  | 				try ( | ||||||
|  | 					RandomAccessFile output = new RandomAccessFile(path.toFile(), "rw") | ||||||
|  | 				) { | ||||||
|  | 					//LOG.debug(output.read()); | ||||||
|  | 					if (output.read()<0) | ||||||
|  | 					{ | ||||||
|  | 						output.writeChars("\0".repeat((offsetBytes+1)*chunksPerRegion)); | ||||||
|  | 					} | ||||||
|  | 					 | ||||||
|  | 					Vec3i pos = getRegionLoc(chunk.getPosition()); | ||||||
|  | 					int shortOffset = (offsetBytes+1)*(pos.z+regionSize.z*(pos.y + regionSize.y*pos.x)); | ||||||
|  | 					int fullOffset = (offsetBytes+1)*(chunksPerRegion); | ||||||
|  | 					output.seek(shortOffset); | ||||||
|  | 					int offset = output.readInt(); | ||||||
|  | 					int sectorLength = output.read(); | ||||||
|  | 					if (sectorLength == 0) | ||||||
|  | 					{ | ||||||
|  | 						offset = (int) (output.length()-fullOffset)/sectorSize; | ||||||
|  | 						output.seek(shortOffset); | ||||||
|  | 						output.writeInt(offset); | ||||||
|  | 						output.write(200); | ||||||
|  | 					} | ||||||
|  | 					output.seek(fullOffset+sectorSize*offset); | ||||||
|  | 					//output.mark(sectorSize*sectorLength); | ||||||
|  | 					 | ||||||
|  | 					DataOutputStream trueOutput = new DataOutputStream(Channels.newOutputStream(output.getChannel())); | ||||||
|  | 					 | ||||||
|  | 					ChunkIO.save(chunk, trueOutput, IOContext.SAVE); | ||||||
|  | 					writeGenerationHint(chunk, trueOutput, server); | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
| 			// else if (currentFormat) | 			// else if (currentFormat) | ||||||
| 		} catch (IOException e) { | 		} catch (IOException e) { | ||||||
| 			e.printStackTrace(); | 			e.printStackTrace(); | ||||||
| @@ -215,17 +282,28 @@ public class TestWorldDiskIO { | |||||||
| 			} else { | 			} else { | ||||||
| 				setRegionSize(bestFormat); | 				setRegionSize(bestFormat); | ||||||
| 				 | 				 | ||||||
|  | 				LOG.debug("Making new world with format {}", bestFormat); | ||||||
|  |  | ||||||
| 				BufferedWriter bw; | 				BufferedWriter bw; | ||||||
| 				try { | 				try { | ||||||
| 					bw = new BufferedWriter(new FileWriter(format)); | 					bw = new BufferedWriter(new FileWriter(format)); | ||||||
| 					 | 					 | ||||||
| 					bw.write( | 					int bfClone = bestFormat; | ||||||
|  | 					 | ||||||
|  | 					for (int i=0;i<4;i++) | ||||||
|  | 					{ | ||||||
|  | 						bw.write(bfClone>>24); | ||||||
|  | 						LOG.debug(bfClone>>24); | ||||||
|  | 						bfClone = bfClone << 8; | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					/*bw.write( | ||||||
| 						new char[] { | 						new char[] { | ||||||
| 							bestFormat / (256 * 256 * 256), | 							(char) bestFormat / (256 * 256 * 256), | ||||||
| 							(bestFormat % 256) / (256 * 256), | 							(char) (bestFormat % 256) / (256 * 256), | ||||||
| 							(bestFormat % (256 * 256)) / (256), | 							(char) (bestFormat % (256 * 256)) / (256), | ||||||
| 							bestFormat % (256 * 256 * 256) } | 							(char) (bestFormat % (256 * 256 * 256)) } | ||||||
| 					); | 					);*/ | ||||||
|  |  | ||||||
| 					bw.close(); | 					bw.close(); | ||||||
| 				} catch (IOException e) { | 				} catch (IOException e) { | ||||||
| @@ -280,11 +358,11 @@ public class TestWorldDiskIO { | |||||||
| 				return null; | 				return null; | ||||||
| 			} | 			} | ||||||
| 		} else if (currentFormat == 1) { | 		} else if (currentFormat == 1) { | ||||||
| 			Vec3i saveCoords = getRegionLoc(chunkPos); | 			Vec3i saveCoords = getRegion(chunkPos); | ||||||
|  |  | ||||||
| 			Path path = SAVE_DIR.resolve( | 			Path path = SAVE_DIR.resolve( | ||||||
| 				String.format( | 				String.format( | ||||||
| 					"chunk_%+d_%+d_%+d" + extension, | 					"chunk_%d_%d_%d" + extension, | ||||||
| 					saveCoords.x, | 					saveCoords.x, | ||||||
| 					saveCoords.y, | 					saveCoords.y, | ||||||
| 					saveCoords.z | 					saveCoords.z | ||||||
| @@ -312,6 +390,50 @@ public class TestWorldDiskIO { | |||||||
| 					chunkPos.z | 					chunkPos.z | ||||||
| 				); | 				); | ||||||
|  |  | ||||||
|  | 				return result; | ||||||
|  | 			} catch (Exception e) { | ||||||
|  | 				e.printStackTrace(); | ||||||
|  | 				LOG.debug( | ||||||
|  | 					"Could not load {} {} {}", | ||||||
|  | 					chunkPos.x, | ||||||
|  | 					chunkPos.y, | ||||||
|  | 					chunkPos.z | ||||||
|  | 				); | ||||||
|  | 				return null; | ||||||
|  | 			} | ||||||
|  | 		} else if (currentFormat == 65536) { | ||||||
|  | 			Vec3i saveCoords = getRegion(chunkPos); | ||||||
|  |  | ||||||
|  | 			Path path = SAVE_DIR.resolve( | ||||||
|  | 				String.format( | ||||||
|  | 					"%d_%d_%d" + extension, | ||||||
|  | 					saveCoords.x, | ||||||
|  | 					saveCoords.y, | ||||||
|  | 					saveCoords.z | ||||||
|  | 				) | ||||||
|  | 			); | ||||||
|  | 			 | ||||||
|  | 			if (!Files.exists(path)) { | ||||||
|  | 				LOG.debug( | ||||||
|  | 					"Not found {} {} {}", | ||||||
|  | 					chunkPos.x, | ||||||
|  | 					chunkPos.y, | ||||||
|  | 					chunkPos.z | ||||||
|  | 				); | ||||||
|  |  | ||||||
|  | 				return null; | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			try { | ||||||
|  | 				ChunkData result = loadRegion(path, chunkPos, world, server); | ||||||
|  |  | ||||||
|  | 				LOG.debug( | ||||||
|  | 					"Loaded {} {} {}", | ||||||
|  | 					chunkPos.x, | ||||||
|  | 					chunkPos.y, | ||||||
|  | 					chunkPos.z | ||||||
|  | 				); | ||||||
|  |  | ||||||
| 				return result; | 				return result; | ||||||
| 			} catch (Exception e) { | 			} catch (Exception e) { | ||||||
| 				e.printStackTrace(); | 				e.printStackTrace(); | ||||||
| @@ -341,6 +463,33 @@ public class TestWorldDiskIO { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
|  | 	private static ChunkData loadRegion(Path path, Vec3i chunkPos, WorldData world, Server server) throws IOException, DecodingException | ||||||
|  | 	{ | ||||||
|  | 		try ( | ||||||
|  | 			DataInputStream input = new DataInputStream( | ||||||
|  | 				new InflaterInputStream(new BufferedInputStream(Files.newInputStream(path))) | ||||||
|  | 			) | ||||||
|  | 		) { | ||||||
|  | 			Vec3i pos = getRegionLoc(chunkPos); | ||||||
|  | 			int shortOffset = (offsetBytes+1)*(pos.z+regionSize.z*(pos.y + regionSize.y*pos.x)); | ||||||
|  | 			int fullOffset = (offsetBytes+1)*(chunksPerRegion); | ||||||
|  | 			input.skipNBytes(shortOffset); | ||||||
|  | 			int offset = 0; | ||||||
|  | 			for (int i=0;i<offsetBytes;i++) | ||||||
|  | 			{ | ||||||
|  | 				offset*=256; | ||||||
|  | 				offset += input.read(); | ||||||
|  | 			} | ||||||
|  | 			int sectorLength = input.read(); | ||||||
|  | 			input.skipNBytes(fullOffset-shortOffset-offsetBytes-1); | ||||||
|  | 			input.skipNBytes(sectorSize*offset); | ||||||
|  | 			input.mark(sectorSize*sectorLength); | ||||||
|  | 			ChunkData chunk = ChunkIO.load(world, chunkPos, input, IOContext.SAVE); | ||||||
|  | 			readGenerationHint(chunk, input, server); | ||||||
|  | 			return chunk; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	private static void readGenerationHint(ChunkData chunk, DataInputStream input, Server server) | 	private static void readGenerationHint(ChunkData chunk, DataInputStream input, Server server) | ||||||
| 		throws IOException, | 		throws IOException, | ||||||
| 		DecodingException { | 		DecodingException { | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ | |||||||
|     	<Logger name="Ticker Coordinator" level="DEBUG" /> |     	<Logger name="Ticker Coordinator" level="DEBUG" /> | ||||||
|     	<Logger name="Ticker 0" level="DEBUG" /> |     	<Logger name="Ticker 0" level="DEBUG" /> | ||||||
|     	--> |     	--> | ||||||
|  |     	<!-- <Logger name="TestWorldDiskIO" level="DEBUG" /> --> | ||||||
|     	 |     	 | ||||||
|         <Root level="info"> |         <Root level="info"> | ||||||
|             <AppenderRef ref="FileLog"  /> |             <AppenderRef ref="FileLog"  /> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user