public void BlockFile_ReadWrite_Reverse() { BlockFile bf; string path; byte[] buf = new byte[cbBlock]; byte[] test = new byte[cbBlock]; path = Path.GetTempPath() + Helper.NewGuid().ToString() + ".blocks"; bf = new BlockFile(path, cbBlock, 0, FileMode.Create); try { // Write blocks backwards read them back stepping backwards // as well. for (int i = cBlocks - 1; i >= 0; i--) { for (int j = 0; j < cbBlock; j++) { buf[j] = (byte)(j + i + 7); } bf.Write(i, buf, cbBlock); } for (int i = cBlocks - 1; i >= 0; i--) { for (int j = 0; j < cbBlock; j++) { test[j] = (byte)(j + i + 7); } bf.Read(i, buf, cbBlock); CollectionAssert.AreEqual(test, buf); } } finally { if (bf != null) { bf.Close(); } if (File.Exists(path)) { File.Delete(path); } } }
public void BlockFile_Preallocate() { BlockFile bf; string path; byte[] buf = new byte[cbBlock]; byte[] test = new byte[cbBlock]; path = Path.GetTempPath() + Helper.NewGuid().ToString() + ".blocks"; bf = new BlockFile(path, cbBlock, cBlocks, FileMode.Create); try { // Write blocks forward from block 0 on and then // read them back stepping forward as well. for (int i = 0; i < cBlocks; i++) { for (int j = 0; j < cbBlock; j++) { buf[j] = (byte)(j + i); } bf.Write(i, buf, cbBlock); } for (int i = 0; i < cBlocks; i++) { for (int j = 0; j < cbBlock; j++) { test[j] = (byte)(j + i); } bf.Read(i, buf, cbBlock); CollectionAssert.AreEqual(test, buf); } } finally { if (bf != null) { bf.Close(); } if (File.Exists(path)) { File.Delete(path); } } }
public static void CreateMap(int Width, int Height, TileData[] Tiles, Action <double, string> UpdateLoading, ushort[,] DefaultTiles = null) { UpdateLoading(0.1, $"Creating Map Descriptor..."); DispatchQueue.DispatchIO(() => { if (!Directory.Exists(MapTempPath)) { Directory.CreateDirectory(MapTempPath); } MapDescriptorFile MapFile = new MapDescriptorFile() { BlockWidth = (uint)Math.Ceiling((double)Width / MapDescriptorFile.Block_Size), BlockHeight = (uint)Math.Ceiling((double)Height / MapDescriptorFile.Block_Size) }; int BlocksDone = 0; int Blocks = (int)MapFile.BlockHeight * (int)MapFile.BlockWidth; MapFile.Blocks = new string[MapFile.BlockWidth, MapFile.BlockHeight]; for (int y = 0; y < MapFile.BlockHeight; y++) { for (int x = 0; x < MapFile.BlockWidth; x++) { UpdateLoading(0.1 + 0.5 * (((double)BlocksDone + 1) / Blocks), $"Creating Block {BlocksDone + 1} / {Blocks}..."); int StartX = x * MapDescriptorFile.Block_Size; int StartY = y * MapDescriptorFile.Block_Size; BlockFile Block = new BlockFile() { Width = (ushort)((Width - StartX < MapDescriptorFile.Block_Size) ? Width - StartX : MapDescriptorFile.Block_Size), Height = (ushort)((Height - StartY < MapDescriptorFile.Block_Size) ? Height - StartY : MapDescriptorFile.Block_Size) }; Block.Tiles = new ushort[Block.Width, Block.Height]; for (int yy = 0; yy < Block.Height; yy++) { for (int xx = 0; xx < Block.Width; xx++) { if (DefaultTiles == null) { Block.Tiles[xx, yy] = Tiles[0].Type; } else { Block.Tiles[xx, yy] = DefaultTiles[StartX + xx, StartY + yy]; } } } string FileName = MapDataTempName.Replace("%", $"_{y * MapFile.BlockWidth + x}"); MapFile.Blocks[x, y] = FileName; using (MemoryStream Stream = new MemoryStream()) { NetworkWriter w = new NetworkWriter(Stream); Block.Write(w); File.WriteAllBytes(Path.Combine(MapTempPath, FileName), Stream.ToArray()); } BlocksDone++; } } UpdateLoading(0.7, $"Saving Map Descriptor..."); using (MemoryStream Stream = new MemoryStream()) { NetworkWriter w = new NetworkWriter(Stream); MapFile.Write(w); File.WriteAllBytes(Path.Combine(MapTempPath, MapTempName), Stream.ToArray()); } UpdateLoading(0.8, $"Rasterizing First Block..."); DispatchQueue.DispatchMain(() => { JuixelGame.Shared.ChangeScene(new MapScene(new MapDataLoader(Path.Combine(MapTempPath, MapTempName)), Tiles, JuixelGame.Shared.CurrentScene.Size)); }); }); }