public static void Serialize(this Shim shim, LevelSerializeContext context) { context.WriteAnimationSet(shim.AnimationSet); context.bw.Write(shim.Position); context.bw.Write(shim.FacingLeft); context.bw.Write(shim.parallaxX); context.bw.Write(shim.parallaxY); context.bw.Write(shim.animationNumber); context.bw.WriteNullableString(shim.ambientSoundSource); if (context.Version >= 14) { context.bw.Write(shim.tag); } if (context.Version >= 16) { context.bw.Write(shim.properties.Count); foreach (var kvp in shim.properties) { context.bw.Write(kvp.Key); context.bw.Write(kvp.Value ?? string.Empty); // (null value should probably be blocked by editor, but being safe...) } } }
/// <summary>Check that an AnimationSet round-trips through serialization cleanly</summary> public void RoundTripCheck(GraphicsDevice graphicsDevice, IAssetProvider assetProvider, IAssetPathProvider assetPathProvider, bool useExternalImages) { // Serialize a first time MemoryStream firstMemoryStream = new MemoryStream(); BinaryWriter firstBinaryWriter = new BinaryWriter(firstMemoryStream); ImageWriter firstImageWriter = null; if (useExternalImages) { firstImageWriter = new ImageWriter(); this.RegisterImages(firstImageWriter, assetPathProvider); firstImageWriter.WriteOutAllImages(firstMemoryStream); } LevelSerializeContext firstSerializeContext = new LevelSerializeContext(firstBinaryWriter, firstImageWriter, assetPathProvider); Serialize(firstSerializeContext); byte[] originalData = firstMemoryStream.ToArray(); // Then deserialize that data BinaryReader br = new BinaryReader(new MemoryStream(originalData)); ImageBundle imageBundle = null; if (useExternalImages) { var helper = new SimpleTextureLoadHelper(graphicsDevice); imageBundle = new ImageBundle(); br.BaseStream.Position = imageBundle.ReadAllImages(originalData, (int)br.BaseStream.Position, helper); } LevelDeserializeContext deserializeContext = new LevelDeserializeContext(br, imageBundle, assetProvider, graphicsDevice); Level deserialized = new Level(deserializeContext); // Then serialize that deserialized data and see if it matches MemoryCompareStream secondMemoryStream = new MemoryCompareStream(originalData); BinaryWriter secondBinaryWriter = new BinaryWriter(secondMemoryStream); ImageWriter secondImageWriter = null; if (useExternalImages) { secondImageWriter = new ImageWriter(); deserialized.RegisterImages(secondImageWriter, assetPathProvider); secondImageWriter.WriteOutAllImages(secondMemoryStream); } LevelSerializeContext secondSerializeContext = new LevelSerializeContext(secondBinaryWriter, secondImageWriter, assetPathProvider); deserialized.Serialize(secondSerializeContext); // Clean-up: if (imageBundle != null) { imageBundle.Dispose(); } }
public virtual void Serialize(LevelSerializeContext context) { context.bw.WriteBoolean(looped); context.bw.Write(positions.Count); foreach (var position in positions) { position.Serialize(context); } context.bw.Write(properties.Count); foreach (var kvp in properties) { context.bw.Write(kvp.Key); context.bw.Write(kvp.Value ?? string.Empty); // (null value should probably be blocked by editor, but being safe...) } }
public static void Serialize(this Teleporter teleporter, LevelSerializeContext context) { if (teleporter.TargetLevel != null) { teleporter.TargetLevel = teleporter.TargetLevel.ToLowerInvariant(); } context.bw.WriteNullableString(teleporter.TargetLevel); context.bw.WriteNullableString(teleporter.targetSpawn); if (context.Version >= 18) { context.bw.Write(teleporter.neverSelectAtRandom); } SerializeRegion(context, teleporter); }
public void Serialize(LevelSerializeContext context) { if (context.Version < 19) { throw new Exception("Cannot write backwards-compatible level before version 19"); } else { context.bw.Write((byte)direction); context.bw.Write((byte)type); } context.bw.Write(nextSector); context.bw.Write(region.startX); context.bw.Write(region.startZ); context.bw.Write(region.endX); context.bw.Write(region.endZ); }
/// <summary>IMPORTANT: Must match with calls to RegisterImages</summary> public static void WriteAnimationSet(this LevelSerializeContext context, AnimationSet animationSet) { string name = context.assetPathProvider.GetAssetPath(animationSet); Debug.Assert(name == null || !name.StartsWith("\\")); if (name != null) { // Write a reference context.bw.Write(true); context.bw.Write(name); } else { // Embed the animation context.bw.Write(false); animationSet.Serialize(context.animationSerializeContext); } }
public static void Serialize(this Thing thing, LevelSerializeContext context) { context.WriteAnimationSet(thing.AnimationSet); context.bw.Write(thing.Position); context.bw.Write(thing.FacingLeft); context.bw.WriteNullableString(thing.overrideBehaviour); context.bw.Write(thing.includeInNavigation); // Properties: { context.bw.Write(thing.properties.Count); foreach (var kvp in thing.properties) { context.bw.Write(kvp.Key); context.bw.Write(kvp.Value ?? string.Empty); // (null value should probably be blocked by editor, but being safe...) } } }
private static void SerializeRegion(LevelSerializeContext context, Region region) { Debug.Assert(!Asserts.enabled || region.mask.Valid); region.mask.Serialize(context.bw); if (context.Version >= 15) { context.bw.Write(region.startY); if (context.Version < 17 && region.endY != beforeVersion17WorldPhysicsMaximumHeight) { context.bw.Write(region.endY - 1); // <- Old version had an inclusive upper bound } else { context.bw.Write(region.endY); } } if (!context.monitor) { region.regionIndex = context.nextRegionIndex++; } }
public virtual void Serialize(LevelSerializeContext context) { context.bw.WriteNullableStringNonBlank(friendlyName); context.bw.WriteNullableStringNonBlank(behaviourName); if (context.bw.WriteBoolean(levelAnimation != null)) { context.WriteAnimationSet(levelAnimation); } // Properties { context.bw.Write(properties.Count); foreach (var kvp in properties) { context.bw.Write(kvp.Key); context.bw.Write(kvp.Value ?? string.Empty); // (null value should probably be blocked by editor, but being safe...) } } // Teleporters { context.bw.Write(teleporters.Count); foreach (var teleporter in teleporters) { teleporter.Serialize(context); } } // Player Spawns { context.bw.Write(playerSpawns.Count); foreach (var item in playerSpawns) { context.bw.Write(item.Key); // string context.bw.Write(item.Value.Count); foreach (var position in item.Value) { context.bw.Write(position); } } } // Things { context.bw.Write(things.Count); foreach (var thing in things) { thing.Serialize(context); } } // Geometry { // Regions { context.bw.Write(regions.Count); foreach (var region in regions) { context.bw.Write(region.Key); context.bw.Write(region.Count()); foreach (var area in region) { area.Serialize(context); } } } // Paths { context.bw.Write(paths.Count); foreach (var kvp in paths) { context.bw.Write(kvp.Key); kvp.Value.Serialize(context); } } // Positions { context.bw.Write(positions.Count); foreach (var cluster in positions) { context.bw.Write(cluster.Key); context.bw.Write(cluster.Count()); foreach (LevelPosition levelPosition in cluster) { levelPosition.Serialize(context); } } } } // Shims { context.bw.Write(shims.Count); foreach (var shim in shims) { shim.Serialize(context); } context.bw.Write(backgroundShimsEndIndex); context.bw.Write(foregroundShimsStartIndex); context.bw.Write(animatedShimCount); } // Camera Bounds context.bw.Write(cameraBounds); if (context.Version >= 20) { context.bw.Write(isAPitLevel); } }
public static void Serialize(this Region region, LevelSerializeContext context) { SerializeRegion(context, region); }