public bool SaveBoard(string name, string description, string folderPath) { try { //Make filepath string fullPath = folderPath; if (!folderPath.EndsWith("/")) { fullPath += "/"; } fullPath += name + ".mwm"; //Save the actual file Directory.CreateDirectory(folderPath); using (FileStream stream = new FileStream(fullPath, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(stream)) { //4bytes; File Version writer.Write((uint)1); //string; Level Name writer.Write(name); //string; Description writer.Write(description); //byte; cols writer.Write((byte)GetNumberOfColumns()); //byte; rows writer.Write((byte)GetNumberOfRows()); //4 bytes per tile for (int c = 0, cols = GetNumberOfColumns(); c < cols; ++c) { for (int r = 0, rows = GetNumberOfRows(); r < rows; ++r) { BasicTile tile = GetTileOnPos(c, r); ushort type = TileTypes.GetTileInfo(tile.tag).Number; byte col = (byte)tile.Colour; byte other = 100; //4bytes -> 2byte type; 1bytes color; 1byte other writer.Write(type); writer.Write(col); writer.Write(other); } } //Close the write (automaticly also closes the stream) writer.Close(); } } UnityGrowl.Show("Saved !\nYour level has succesfully been saved."); return(true); } catch { UnityGrowl.Show("Save error\nAn error has occured during saving.\nMake sure the map/file is not read-only.", 5); return(false); } }
public bool LoadBoard(string path, out string name, out string description) { try { //Load the file using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { using (BinaryReader reader = new BinaryReader(stream)) { //4bytes; File Version uint version = reader.ReadUInt32(); switch (version) { case 1: LoadVersionOne(reader, out name, out description); break; default: name = "No Map"; description = "The selected file is corrupt"; return(false); } reader.Close(); } } UnityGrowl.Show("Loaded !\nYour level has succesfully been loaded."); return(true); } catch { UnityGrowl.Show("Load error\nAn error has occured during loading.", 4); name = "Error"; description = "An error has occured, please try again."; DestroyCurrentBoard(); return(false); } }