public Map Load([NotNull] string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } using (FileStream mapStream = File.OpenRead(fileName)) { BinaryReader reader = new BinaryReader(mapStream); // Load MetaData Map mapFile = LoadMapMetadata(mapStream); // Load the data store int dataBlockSize = reader.ReadInt32(); byte[] jsonDataBlock = new byte[dataBlockSize]; reader.Read(jsonDataBlock, 0, dataBlockSize); MemoryStream memStream = new MemoryStream(jsonDataBlock); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OpticraftDataStore)); OpticraftDataStore dataStore = (OpticraftDataStore)serializer.ReadObject(memStream); reader.ReadInt32(); // Load Zones LoadZones(mapFile, dataStore); // Load the block store mapFile.Blocks = new Byte[mapFile.Volume]; using (GZipStream decompressor = new GZipStream(mapStream, CompressionMode.Decompress)) { decompressor.Read(mapFile.Blocks, 0, mapFile.Blocks.Length); } return(mapFile); } }
public bool Save([NotNull] Map mapToSave, [NotNull] string fileName) { if (mapToSave == null) { throw new ArgumentNullException("mapToSave"); } if (fileName == null) { throw new ArgumentNullException("fileName"); } using (FileStream mapStream = File.OpenWrite(fileName)) { BinaryWriter writer = new BinaryWriter(mapStream); // Version writer.Write(MapVersion); MemoryStream serializationStream = new MemoryStream(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OpticraftMetaData)); // Create and serialize core meta data OpticraftMetaData oMetadate = new OpticraftMetaData { X = mapToSave.Width, Y = mapToSave.Length, Z = mapToSave.Height, // Spawn SpawnX = mapToSave.Spawn.X, SpawnY = mapToSave.Spawn.Y, SpawnZ = mapToSave.Spawn.Z, SpawnOrientation = mapToSave.Spawn.R, SpawnPitch = mapToSave.Spawn.L }; // World related values. if (mapToSave.World != null) { oMetadate.Hidden = mapToSave.World.IsHidden; oMetadate.MinimumJoinRank = mapToSave.World.AccessSecurity.MinRank.Name; oMetadate.MinimumBuildRank = mapToSave.World.BuildSecurity.MinRank.Name; } else { oMetadate.Hidden = false; oMetadate.MinimumJoinRank = oMetadate.MinimumBuildRank = "guest"; } oMetadate.CreationDate = 0; // This is ctime for when the world was created. Unsure on how to extract it. Opticraft makes no use of it as of yet serializer.WriteObject(serializationStream, oMetadate); byte[] jsonMetaData = serializationStream.ToArray(); writer.Write(jsonMetaData.Length); writer.Write(jsonMetaData); // Now create and serialize core data store (zones) Zone[] zoneCache = mapToSave.Zones.Cache; OpticraftDataStore oDataStore = new OpticraftDataStore { Zones = new OpticraftZone[zoneCache.Length] }; int i = 0; foreach (Zone zone in zoneCache) { OpticraftZone oZone = new OpticraftZone { Name = zone.Name, MinimumRank = zone.Controller.MinRank.Name, Owner = "", X1 = zone.Bounds.XMin, X2 = zone.Bounds.XMax, Y1 = zone.Bounds.YMin, Y2 = zone.Bounds.YMax, Z1 = zone.Bounds.ZMin, Z2 = zone.Bounds.ZMax, Builders = new string[zone.Controller.ExceptionList.Included.Length] }; // Bounds // Builders int j = 0; foreach (PlayerInfo pInfo in zone.Controller.ExceptionList.Included) { oZone.Builders[j++] = pInfo.Name; } // Excluded players oZone.Excluded = new string[zone.Controller.ExceptionList.Excluded.Length]; j = 0; foreach (PlayerInfo pInfo in zone.Controller.ExceptionList.Excluded) { oZone.Builders[j++] = pInfo.Name; } oDataStore.Zones[i++] = oZone; } // Serialize it serializationStream = new MemoryStream(); serializer = new DataContractJsonSerializer(typeof(OpticraftDataStore)); serializer.WriteObject(serializationStream, oDataStore); byte[] jsonDataStore = serializationStream.ToArray(); writer.Write(jsonDataStore.Length); writer.Write(jsonDataStore); // Blocks MemoryStream blockStream = new MemoryStream(); using (GZipStream zipper = new GZipStream(blockStream, CompressionMode.Compress, true)) { zipper.Write(mapToSave.Blocks, 0, mapToSave.Blocks.Length); } byte[] compressedBlocks = blockStream.ToArray(); writer.Write(compressedBlocks.Length); writer.Write(compressedBlocks); } return(true); }
static void LoadZones([NotNull] Map mapFile, [NotNull] OpticraftDataStore dataStore) { if (mapFile == null) { throw new ArgumentNullException("mapFile"); } if (dataStore == null) { throw new ArgumentNullException("dataStore"); } if (dataStore.Zones.Length == 0) { return; } // TODO: investigate side effects PlayerInfo conversionPlayer = new PlayerInfo("OpticraftConversion", RankManager.HighestRank, true, RankChangeType.AutoPromoted); foreach (OpticraftZone optiZone in dataStore.Zones) { // Make zone Zone fZone = new Zone { Name = optiZone.Name, }; BoundingBox bBox = new BoundingBox(optiZone.X1, optiZone.Y1, optiZone.Z1, optiZone.X2, optiZone.X2, optiZone.Z2); fZone.Create(bBox, conversionPlayer); // Min rank Rank minRank = Rank.Parse(optiZone.MinimumRank); if (minRank != null) { fZone.Controller.MinRank = minRank; } foreach (string playerName in optiZone.Builders) { // These are all lower case names if (!Player.IsValidName(playerName)) { continue; } PlayerInfo pInfo = PlayerDB.FindPlayerInfoExact(playerName); if (pInfo != null) { fZone.Controller.Include(pInfo); } } // Excluded names are not as of yet implemented in opticraft, but will be soon // So add compatibility for them when they arrive. if (optiZone.Excluded != null) { foreach (string playerName in optiZone.Excluded) { // These are all lower case names if (!Player.IsValidName(playerName)) { continue; } PlayerInfo pInfo = PlayerDB.FindPlayerInfoExact(playerName); if (pInfo != null) { fZone.Controller.Exclude(pInfo); } } } mapFile.Zones.Add(fZone); } }