diff --git a/src/library/Syroot.Worms.Armageddon/Team.cs b/src/library/Syroot.Worms.Armageddon/Team.cs index c7184d5..5160f31 100644 --- a/src/library/Syroot.Worms.Armageddon/Team.cs +++ b/src/library/Syroot.Worms.Armageddon/Team.cs @@ -211,7 +211,7 @@ namespace Syroot.Worms.Armageddon { BitsPerPixel = 8, Size = new Size(24, 32), - Palette = reader.ReadStructs(256), + Palette = reader.ReadColors(256), Data = reader.ReadBytes(24 * 32) }; } @@ -234,7 +234,7 @@ namespace Syroot.Worms.Armageddon { BitsPerPixel = 8, Size = new Size(20, 17), - Palette = reader.ReadStructs(256), + Palette = reader.ReadColors(256), Data = reader.ReadBytes(20 * 17) }; @@ -264,7 +264,7 @@ namespace Syroot.Worms.Armageddon if (GraveSprite < 0) { writer.WriteString(GraveFileName, 0x20); - writer.WriteStructs(Grave.Palette); + writer.WriteColors(Grave.Palette); writer.Write(Grave.Data); } @@ -282,7 +282,7 @@ namespace Syroot.Worms.Armageddon writer.WriteStructs(MissionStatuses); writer.WriteString(FlagFileName, 0x20); - writer.WriteStructs(Flag.Palette); + writer.WriteColors(Flag.Palette); writer.Write(Flag.Data); writer.Write(DeathmatchRank); @@ -301,14 +301,11 @@ namespace Syroot.Worms.Armageddon [DebuggerDisplay("TeamMissionStatus Attemps={Attempts} Medal={Medal}")] public struct TeamMissionStatus { - /// - /// The number of attempts the team required to solve the mission. - /// - public int Attempts; + // ---- FIELDS ------------------------------------------------------------------------------------------------- - /// - /// The medal the team got to solve the mission. - /// + /// The number of attempts the team required to solve the mission. + public int Attempts; + /// The medal the team got to solve the mission. public int Medal; } @@ -317,44 +314,21 @@ namespace Syroot.Worms.Armageddon /// public enum TeamWeapon : byte { - /// - /// The Flame Thrower weapon. - /// + /// The Flame Thrower weapon. Flamethrower, - - /// - /// The Mole Bomb weapon. - /// + /// The Mole Bomb weapon. MoleBomb, - - /// - /// The Old Woman weapon. - /// + /// The Old Woman weapon. OldWoman, - - /// - /// The Homing Pigeon weapon. - /// + /// The Homing Pigeon weapon. HomingPigeon, - - /// - /// The Sheep Launcher weapon. - /// + /// The Sheep Launcher weapon. SheepLauncher, - - /// - /// The Mad Cow weapon. - /// + /// The Mad Cow weapon. MadCow, - - /// - /// The Holy Hand Grenade weapon. - /// + /// The Holy Hand Grenade weapon. HolyHandGrenade, - - /// - /// The Super Sheep or Aqua Sheep weapon. - /// + /// The Super Sheep or Aqua Sheep weapon. SuperSheep } } diff --git a/src/library/Syroot.Worms.Armageddon/TeamContainer.cs b/src/library/Syroot.Worms.Armageddon/TeamContainer.cs index 6b54529..a2e4cd5 100644 --- a/src/library/Syroot.Worms.Armageddon/TeamContainer.cs +++ b/src/library/Syroot.Worms.Armageddon/TeamContainer.cs @@ -57,7 +57,7 @@ namespace Syroot.Worms.Armageddon /// /// Gets or sets the list of instances stored. /// - public List Teams { get; set; } + public IList Teams { get; set; } // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- @@ -90,7 +90,7 @@ namespace Syroot.Worms.Armageddon /// public void Save(Stream stream) { - using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); // Write the header. writer.Write(_signature, StringCoding.ZeroTerminated); @@ -119,75 +119,36 @@ namespace Syroot.Worms.Armageddon /// [Flags] public enum UnlockedFeatures : int - { - /// - /// The utility weapon Laser Sight can be configured. - /// + { + /// No features have been unlocked yet. + None, + /// The utility weapon Laser Sight can be configured. UtilityLaserSight = 1 << 0, - - /// - /// The utility weapon Fast Walk can be configured. - /// + /// The utility weapon Fast Walk can be configured. UtilityFastWalk = 1 << 1, - - /// - /// The utility weapon Invisibility can be configured. - /// + /// The utility weapon Invisibility can be configured. UtilityInvisibility = 1 << 2, - - /// - /// The utility weapon Low Gravity can be configured. - /// + /// The utility weapon Low Gravity can be configured. UtilityLowGravity = 1 << 3, - - /// - /// The utility weapon Jetpack can be configured. - /// + /// The utility weapon Jetpack can be configured. UtilityJetpack = 1 << 4, - - /// - /// The Grenade upgrade can be enabled. - /// + /// The Grenade upgrade can be enabled. UpgradedGrenade = 1 << 8, - - /// - /// The Shotgun upgrade can be enabled. - /// + /// The Shotgun upgrade can be enabled. UpgradedShotgun = 1 << 9, - - /// - /// The cluster upgrade can be enabled. - /// + /// The cluster upgrade can be enabled. UpgradedClusters = 1 << 10, - - /// - /// The Longbow upgrade can be enabled. - /// + /// The Longbow upgrade can be enabled. UpgradedLongbow = 1 << 11, - - /// - /// The upgrade of Super Sheeps to become Aqua Sheeps can be enabled. - /// + /// The upgrade of Super Sheeps to become Aqua Sheeps can be enabled. AquaSheep = 1 << 12, - - /// - /// Worms can have infinite health and are killable only by drowning them. - /// + /// Worms can have infinite health and are killable only by drowning them. GodWorms = 1 << 16, - - /// - /// Blood effects when hitting worms can be enabled. - /// + /// Blood effects when hitting worms can be enabled. BloodFx = 1 << 17, - - /// - /// Every crate explodes with a sheep. - /// + /// Every crate explodes with a sheep. SheepHeaven = 1 << 18, - - /// - /// Map terrain can be indestructible and Full Wormage scheme is accessible. - /// + /// Map terrain can be indestructible and Full Wormage scheme is accessible. IndestructibleAndFullWormage = 1 << 24 } } diff --git a/src/library/Syroot.Worms.WorldParty/Team.cs b/src/library/Syroot.Worms.WorldParty/Team.cs index 716c44f..9c029cb 100644 --- a/src/library/Syroot.Worms.WorldParty/Team.cs +++ b/src/library/Syroot.Worms.WorldParty/Team.cs @@ -210,7 +210,7 @@ namespace Syroot.Worms.WorldParty { BitsPerPixel = 8, Size = new Size(24, 32), - Palette = reader.ReadStructs(256), + Palette = reader.ReadColors(256), Data = reader.ReadBytes(24 * 32) }; } @@ -233,7 +233,7 @@ namespace Syroot.Worms.WorldParty { BitsPerPixel = 8, Size = new Size(20, 17), - Palette = reader.ReadStructs(256), + Palette = reader.ReadColors(256), Data = reader.ReadBytes(20 * 17) }; @@ -262,7 +262,7 @@ namespace Syroot.Worms.WorldParty if (GraveSprite < 0) { writer.WriteString(GraveFileName, 0x20); - writer.WriteStructs(Grave.Palette); + writer.WriteColors(Grave.Palette); writer.Write(Grave.Data); } @@ -280,7 +280,7 @@ namespace Syroot.Worms.WorldParty writer.WriteStructs(MissionStatuses); writer.WriteString(FlagFileName, 0x20); - writer.WriteStructs(Flag.Palette); + writer.WriteColors(Flag.Palette); writer.Write(Flag.Data); writer.Write(Unknown1); diff --git a/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs b/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs index 928de1b..78ad865 100644 --- a/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs +++ b/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Drawing; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -22,7 +23,7 @@ namespace Syroot.Worms.Core.IO /// Reads an instance from the current stream. /// /// The type of the class to instantiate. - /// The extended . + /// The extended instance. /// The instance. public static T Load(this BinaryStream self) where T : ILoadable, new() { @@ -35,7 +36,7 @@ namespace Syroot.Worms.Core.IO /// Reads instances from the current stream. /// /// The type of the class to instantiate. - /// The extended . + /// The extended instance. /// The number of instances to read. /// The instances. public static T[] Load(this BinaryStream self, int count) where T : ILoadable, new() @@ -46,10 +47,31 @@ namespace Syroot.Worms.Core.IO return instances; } + /// + /// Reads an RGBA 32-bit color. + /// + /// The extended instance. + /// The read color. + public static Color ReadColor(this BinaryStream self) => Color.FromArgb(self.ReadInt32()); + + /// + /// Reads RGBA 32-bit colors. + /// + /// The extended instance. + /// The number of values to read. + /// The read colors. + public static Color[] ReadColors(this BinaryStream self, int count) + { + Color[] values = new Color[count]; + for (int i = 0; i < count; i++) + values[i] = Color.FromArgb(self.ReadInt32()); + return values; + } + /// /// Reads a 0-terminated string which is stored in a fixed-size block of bytes. /// - /// The extended . + /// The extended instance. /// The number of bytes the fixed-size blocks takes. /// The read string. public static string ReadString(this BinaryStream self, int length) @@ -65,10 +87,10 @@ namespace Syroot.Worms.Core.IO /// Reads 0-terminated strings which are stored in a fixed-size block of /// bytes. /// - /// The extended . + /// The extended instance. /// The number of values to read. /// The number of bytes the fixed-size blocks takes. - /// The read string. + /// The read strings. public static string[] ReadStrings(this BinaryStream self, int count, int length) { string[] strings = new string[count]; @@ -83,7 +105,7 @@ namespace Syroot.Worms.Core.IO /// The type of the structure to read. /// The extended instance. /// The structure of type . - public static T ReadStruct(this BinaryStream self) where T : struct + public static T ReadStruct(this BinaryStream self) where T : unmanaged { // Read the raw bytes of the structure. #if NETSTANDARD2_0 @@ -106,7 +128,7 @@ namespace Syroot.Worms.Core.IO /// The extended instance. /// The number of values to read. /// The structures of type . - public static T[] ReadStructs(this BinaryStream self, int count) where T : struct + public static T[] ReadStructs(this BinaryStream self, int count) where T : unmanaged { // Read the raw bytes of the structures. #if NETSTANDARD2_0 @@ -141,7 +163,7 @@ namespace Syroot.Worms.Core.IO /// Writes the given instance into the current stream. /// /// The type of the class to write. - /// The extended . + /// The extended instance. /// The instance to write into the current stream. public static void Save(this BinaryStream self, T value) where T : ISaveable { @@ -152,7 +174,7 @@ namespace Syroot.Worms.Core.IO /// Writes the given instances into the current stream. /// /// The type of the class to write. - /// The extended . + /// The extended instance. /// The instances to write into the current stream. public static void Save(this BinaryStream self, IList values) where T : ISaveable { @@ -166,6 +188,25 @@ namespace Syroot.Worms.Core.IO self.WriteInt32(value); } + /// + /// Writes the given color. + /// + /// The extended instance. + /// The color to write. + public static void WriteColor(this BinaryStream self, Color color) + => self.Write(color.R << 24 | color.G << 16 | color.B << 8 | color.A); + + /// + /// Writes the given colors. + /// + /// The extended instance. + /// The colors to write. + public static void WriteColors(this BinaryStream self, IEnumerable colors) + { + foreach (Color color in colors) + WriteColor(self, color); + } + /// /// Writes the given string into a fixed-size block of bytes and 0-terminates it. /// @@ -187,7 +228,7 @@ namespace Syroot.Worms.Core.IO /// The extended instance. /// The strings to write. /// The number of bytes a fixed-size block takes. - public static void WriteStrings(this BinaryStream self, IList values, int length) + public static void WriteStrings(this BinaryStream self, IEnumerable values, int length) { foreach (string value in values) WriteString(self, value, length); @@ -199,7 +240,7 @@ namespace Syroot.Worms.Core.IO /// The type of the structure to read. /// The extended instance. /// The structure to write. - public static void WriteStruct(this BinaryStream self, T value) where T : struct + public static void WriteStruct(this BinaryStream self, T value) where T : unmanaged { ReadOnlySpan bytes; unsafe { bytes = new ReadOnlySpan(Unsafe.AsPointer(ref value), Unsafe.SizeOf()); } @@ -216,10 +257,10 @@ namespace Syroot.Worms.Core.IO /// The type of the structure to read. /// The extended instance. /// The structures to write. - public static void WriteStructs(this BinaryStream self, IEnumerable values) where T : struct + public static void WriteStructs(this BinaryStream self, IEnumerable values) where T : unmanaged { foreach (T value in values) - self.WriteStruct(value); + WriteStruct(self, value); } } } diff --git a/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/custom.wgt b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/custom.wgt new file mode 100644 index 0000000..4d3368f Binary files /dev/null and b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/custom.wgt differ diff --git a/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wgt b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wgt new file mode 100644 index 0000000..58f6873 Binary files /dev/null and b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wgt differ diff --git a/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wwp b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wwp new file mode 100644 index 0000000..e2dc511 Binary files /dev/null and b/src/test/Syroot.Worms.Armageddon.Test/Files/Teams/default.wwp differ diff --git a/src/test/Syroot.Worms.Armageddon.Test/TeamContainerTests.cs b/src/test/Syroot.Worms.Armageddon.Test/TeamContainerTests.cs index c2bb7c8..b5bd9e2 100644 --- a/src/test/Syroot.Worms.Armageddon.Test/TeamContainerTests.cs +++ b/src/test/Syroot.Worms.Armageddon.Test/TeamContainerTests.cs @@ -13,9 +13,9 @@ namespace Syroot.Worms.Test.Armageddon // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- /// - /// Loads all files found in any game directories. + /// Tests all files found in the test directory. /// [TestMethod] - public void LoadTeamContainers() => FileTester.Run(Game.Armageddon, "*.wgt"); + public void LoadTeamContainers() => FileTester.Run(Game.Test, "*.wgt"); } } diff --git a/src/test/Syroot.Worms.Test/Core/FileTester.cs b/src/test/Syroot.Worms.Test/Core/FileTester.cs index 0ea2f08..276e22b 100644 --- a/src/test/Syroot.Worms.Test/Core/FileTester.cs +++ b/src/test/Syroot.Worms.Test/Core/FileTester.cs @@ -62,8 +62,11 @@ namespace Syroot.Worms.Test.Core T newInstance = new T(); newInstance.Load(stream); - Debug.Write($" compare"); - Assert.IsTrue(newInstance.Equals(instance)); + if (instance is IEquatable equatable) + { + Debug.Write($" compare"); + Assert.IsTrue(((IEquatable)newInstance).Equals(equatable)); + } } Debug.WriteLine($" OK"); }