Exemplo n.º 1
0
        static Map LoadMapMetadata([NotNull] Stream mapStream)
        {
            if (mapStream == null)
            {
                throw new ArgumentNullException("mapStream");
            }
            BinaryReader reader = new BinaryReader(mapStream);

            reader.ReadInt16();
            int metaDataSize = reader.ReadInt32();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OpticraftMetaData));

            byte[] rawMetaData = new byte[metaDataSize];
            reader.Read(rawMetaData, 0, metaDataSize);
            MemoryStream memStream = new MemoryStream(rawMetaData);

            OpticraftMetaData metaData = (OpticraftMetaData)serializer.ReadObject(memStream);
            // ReSharper disable UseObjectOrCollectionInitializer
            Map mapFile = new Map(null, metaData.X, metaData.Y, metaData.Z, false);

            // ReSharper restore UseObjectOrCollectionInitializer
            mapFile.Spawn = new Position {
                X = (short)(metaData.SpawnX),
                Y = (short)(metaData.SpawnY),
                Z = (short)(metaData.SpawnZ),
                R = metaData.SpawnOrientation,
                L = metaData.SpawnPitch
            };
            return(mapFile);
        }
Exemplo n.º 2
0
        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);
        }