public async Task SaveChunk(ChunkReference chunkLocation) { // It doesn't exist, so we can't save it :P if (!loadedChunkspace.ContainsKey(chunkLocation)) { return; } Chunk chunk = loadedChunkspace[chunkLocation]; string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath()); // If it's empty, then there's no point in saving it if (chunk.IsEmpty) { // Delete the existing chunk file, if it if (File.Exists(chunkFilePath)) { File.Delete(chunkFilePath); } return; } using (Stream chunkDestination = File.Open(chunkFilePath, FileMode.OpenOrCreate)) await chunk.SaveTo(chunkDestination); }
public async Task <Chunk> FetchChunk(ChunkReference chunkLocation, bool autoCreate) { // If the chunk is in the loaded chunk-space, then return it immediately if (loadedChunkspace.ContainsKey(chunkLocation)) { return(loadedChunkspace[chunkLocation]); } // Uh-oh! The chunk isn't loaded at moment. Load it quick & then // return it fast. string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath()); Chunk loadedChunk; if (File.Exists(chunkFilePath)) // If the chunk exists on disk, load it { loadedChunk = await Chunk.FromFile(this, chunkFilePath); } else { // Ooooh! It's a _new_, never-before-seen one! Create a brand new chunk :D // ....but only if we've been told it's ok to create new chunks. if (!autoCreate) { return(null); } loadedChunk = new Chunk(this, ChunkSize, chunkLocation); } loadedChunk.OnChunkUpdate += HandleChunkUpdate; loadedChunkspace.Add(chunkLocation, loadedChunk); return(loadedChunk); }
/// <summary> /// Works out whether a chunk currently exists. /// </summary> /// <param name="chunkLocation">The chunk location to check.</param> /// <returns>Whether the chunk at specified location exists or not.</returns> public bool HasChunk(ChunkReference chunkLocation) { if (loadedChunkspace.ContainsKey(chunkLocation)) { return(true); } string chunkFilePath = Path.Combine(StorageDirectory, chunkLocation.AsFilepath()); if (File.Exists(chunkFilePath)) { return(true); } return(false); }