diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/ActionConverter.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/ActionConverter.cs index c91e7fd..9195cc4 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/ActionConverter.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/ActionConverter.cs @@ -11,31 +11,20 @@ namespace Syroot.Worms.Armageddon.ProjectX public object Read(Stream stream, object instance, BinaryMemberAttribute memberAttribute, ByteConverter byteConverter) { - ExplosionAction explosionAction; - switch (instance) + ExplosionAction explosionAction = instance switch { - case LauncherStyle launcherStyle: - explosionAction = launcherStyle.ExplosionAction; - break; - case ClusterTarget clusterTarget: - explosionAction = clusterTarget.ExplosionAction; - break; - default: - throw new NotImplementedException(); - } - - switch (explosionAction) + LauncherStyle launcherStyle => launcherStyle.ExplosionAction, + ClusterTarget clusterTarget => clusterTarget.ExplosionAction, + _ => throw new NotImplementedException(), + }; + return explosionAction switch { - case ExplosionAction.Bounce: - return stream.ReadObject(); - case ExplosionAction.Dig: - return stream.ReadObject(); - case ExplosionAction.Home: - return stream.ReadObject(); - case ExplosionAction.Roam: - return stream.ReadObject(); - } - return null; + ExplosionAction.Bounce => stream.ReadObject(), + ExplosionAction.Dig => stream.ReadObject(), + ExplosionAction.Home => stream.ReadObject(), + ExplosionAction.Roam => stream.ReadObject(), + _ => null, + }; } public void Write(Stream stream, object instance, BinaryMemberAttribute memberAttribute, object value, diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/IAction.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/IAction.cs index 20f22a8..a4e4501 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/IAction.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Actions/IAction.cs @@ -1,6 +1,4 @@ namespace Syroot.Worms.Armageddon.ProjectX { - public interface IAction - { - } + public interface IAction { } } diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Library.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Library.cs index 759aca3..1d57f5e 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Library.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Library.cs @@ -25,28 +25,20 @@ namespace Syroot.Worms.Armageddon.ProjectX /// /// Initializes a new instance of the class. /// - public Library() - { - } + public Library() { } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public Library(Stream stream) - { - Load(stream); - } + public Library(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public Library(string fileName) - { - Load(fileName); - } + public Library(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -59,13 +51,7 @@ namespace Syroot.Worms.Armageddon.ProjectX /// /// The key of the entries to match. /// All matching entries. - public IEnumerable this[string key] - { - get - { - return this.Where(x => x.Key == key); - } - } + public IEnumerable this[string key] => this.Where(x => x.Key == key); // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- @@ -75,34 +61,31 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadInt32() != _signature) - { - throw new InvalidDataException("Invalid PXL file signature."); - } - Version = reader.Read1Byte(); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the items. - Clear(); - int itemCount = reader.ReadInt32(); - for (int i = 0; i < itemCount; i++) + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid PXL file signature."); + Version = reader.Read1Byte(); + + // Read the items. + Clear(); + int itemCount = reader.ReadInt32(); + for (int i = 0; i < itemCount; i++) + { + LibraryItemType type = reader.ReadEnum(true); + string name = reader.ReadString(StringCoding.Int32CharCount); + switch (type) { - LibraryItemType type = reader.ReadEnum(true); - string name = reader.ReadString(StringCoding.Int32CharCount); - switch (type) - { - case LibraryItemType.File: - Add(new LibraryItem(name, reader.ReadBytes(reader.ReadInt32()))); - break; - case LibraryItemType.Script: - Add(new LibraryItem(name, reader.ReadString(StringCoding.Int32CharCount))); - break; - case LibraryItemType.Weapon: - Add(new LibraryItem(name, reader.Load())); - break; - } + case LibraryItemType.File: + Add(new LibraryItem(name, reader.ReadBytes(reader.ReadInt32()))); + break; + case LibraryItemType.Script: + Add(new LibraryItem(name, reader.ReadString(StringCoding.Int32CharCount))); + break; + case LibraryItemType.Weapon: + Add(new LibraryItem(name, reader.Load())); + break; } } } @@ -113,10 +96,8 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -125,32 +106,31 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature); - writer.Write(Version); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the items. - writer.Write(Count); - foreach (LibraryItem item in this) + // Write the header. + writer.Write(_signature); + writer.Write(Version); + + // Write the items. + writer.Write(Count); + foreach (LibraryItem item in this) + { + writer.WriteEnum(item.Type, true); + writer.Write(item.Key, StringCoding.Int32CharCount); + switch (item.Type) { - writer.WriteEnum(item.Type, true); - writer.Write(item.Key, StringCoding.Int32CharCount); - switch (item.Type) - { - case LibraryItemType.File: - byte[] value = (byte[])item.Value; - writer.Write(value.Length); - writer.WriteStructs(value); - break; - case LibraryItemType.Script: - writer.Write((string)item.Value, StringCoding.Int32CharCount); - break; - case LibraryItemType.Weapon: - writer.Save((Weapon)item.Value); - break; - } + case LibraryItemType.File: + byte[] value = (byte[])item.Value; + writer.Write(value.Length); + writer.WriteStructs(value); + break; + case LibraryItemType.Script: + writer.Write((string)item.Value, StringCoding.Int32CharCount); + break; + case LibraryItemType.Weapon: + writer.Save((Weapon)item.Value); + break; } } } @@ -161,10 +141,8 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } /// @@ -172,27 +150,21 @@ namespace Syroot.Worms.Armageddon.ProjectX /// /// The enumeration of attached files. public IEnumerable GetFiles() - { - return this.Where(x => x.Type == LibraryItemType.File).Select(x => (byte[])x.Value); - } + => this.Where(x => x.Type == LibraryItemType.File).Select(x => (byte[])x.Value); /// /// Gets all attached scripts. /// /// The enumeration of attached scripts. public IEnumerable GetScripts() - { - return this.Where(x => x.Type == LibraryItemType.Script).Select(x => (string)x.Value); - } + => this.Where(x => x.Type == LibraryItemType.Script).Select(x => (string)x.Value); /// /// Gets all attached weapons. /// /// The enumeration of attached weapons. public IEnumerable GetWeapons() - { - return this.Where(x => x.Type == LibraryItemType.Weapon).Select(x => (Weapon)x.Value); - } + => this.Where(x => x.Type == LibraryItemType.Weapon).Select(x => (Weapon)x.Value); } /// @@ -236,29 +208,18 @@ namespace Syroot.Worms.Armageddon.ProjectX /// public object Value { - get - { - return _value; - } + get => _value; set { // Validate the type. if (value.GetType() == typeof(byte[])) - { Type = LibraryItemType.File; - } else if (value.GetType() == typeof(string)) - { Type = LibraryItemType.Script; - } else if (value.GetType() == typeof(Weapon)) - { Type = LibraryItemType.Weapon; - } else - { throw new ArgumentException("Invalid LibraryItemType.", nameof(value)); - } _value = value; } } diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Scheme.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Scheme.cs index 8eea9d8..f951bc6 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Scheme.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Scheme.cs @@ -23,28 +23,20 @@ namespace Syroot.Worms.Armageddon.ProjectX /// /// Initializes a new instance of the class. /// - public Scheme() - { - } + public Scheme() { } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public Scheme(Stream stream) - { - Load(stream); - } + public Scheme(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public Scheme(string fileName) - { - Load(fileName); - } + public Scheme(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -72,59 +64,52 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + // Read the header. + if (reader.ReadString(_signature.Length) != _signature) + throw new InvalidDataException("Invalid PXS file signature."); + Version = reader.ReadInt32(); + + // Read the scheme flags. + Flags = reader.ReadStruct(); + + // Read the weapon tables. + int weaponTableCount = reader.ReadInt32(); + WeaponTables = new List(weaponTableCount); + for (int i = 0; i < weaponTableCount; i++) + WeaponTables.Add(reader.Load(_weaponsPerTable)); + + // Read a placeholder array. + reader.Seek(sizeof(int)); + + // Read attached files. + int filesCount = reader.ReadInt32(); + Files = new Dictionary(filesCount); + for (int i = 0; i < filesCount; i++) { - // Read the header. - if (reader.ReadString(_signature.Length) != _signature) - { - throw new InvalidDataException("Invalid PXS file signature."); - } - Version = reader.ReadInt32(); + string name = reader.ReadString(StringCoding.Int32CharCount); + int length = reader.ReadInt32(); + Files.Add(name, reader.ReadBytes(length)); + } - // Read the scheme flags. - Flags = reader.ReadStruct(); + // Read attached scripts. + int scriptsCount = reader.ReadInt32(); + Scripts = new Dictionary(scriptsCount); + for (int i = 0; i < scriptsCount; i++) + Scripts.Add(reader.ReadString(StringCoding.Int32CharCount), + reader.ReadString(StringCoding.Int32CharCount)); - // Read the weapon tables. - int weaponTableCount = reader.ReadInt32(); - WeaponTables = new List(weaponTableCount); - for (int i = 0; i < weaponTableCount; i++) - { - WeaponTables.Add(reader.Load(_weaponsPerTable)); - } + // Read required libraries. + int librariesCount = reader.ReadInt32(); + Libraries = new List(reader.ReadStrings(librariesCount, StringCoding.Int32CharCount)); - // Read a placeholder array. - reader.Seek(sizeof(int)); - - // Read attached files. - int filesCount = reader.ReadInt32(); - Files = new Dictionary(filesCount); - for (int i = 0; i < filesCount; i++) - { - string name = reader.ReadString(StringCoding.Int32CharCount); - int length = reader.ReadInt32(); - Files.Add(name, reader.ReadBytes(length)); - } - - // Read attached scripts. - int scriptsCount = reader.ReadInt32(); - Scripts = new Dictionary(scriptsCount); - for (int i = 0; i < scriptsCount; i++) - { - Scripts.Add(reader.ReadString(StringCoding.Int32CharCount), - reader.ReadString(StringCoding.Int32CharCount)); - } - - // Read required libraries. - int librariesCount = reader.ReadInt32(); - Libraries = new List(reader.ReadStrings(librariesCount, StringCoding.Int32CharCount)); - - // Read a possibly attached scheme file. - if (reader.ReadBoolean()) - { - reader.Seek(sizeof(int)); // Scheme length not required due to intelligent loading. - GameScheme = reader.Load(); - GameSchemeName = reader.ReadString(StringCoding.Int32CharCount); - } + // Read a possibly attached scheme file. + if (reader.ReadBoolean()) + { + reader.Seek(sizeof(int)); // Scheme length not required due to intelligent loading. + GameScheme = reader.Load(); + GameSchemeName = reader.ReadString(StringCoding.Int32CharCount); } } @@ -134,10 +119,8 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -146,58 +129,55 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + + // Write the header. + writer.Write(_signature, StringCoding.Raw); + writer.Write(Version); + + // Write the scheme flags. + writer.WriteStruct(Flags); + + // Write the weapon tables. + writer.Write(WeaponTables.Count); + foreach (Weapon[] weaponTable in WeaponTables) + writer.Save(weaponTable); + + // Write a placeholder array. + writer.Write(0); + + // Write attached files. + writer.Write(Files.Count); + foreach (KeyValuePair file in Files) { - // Write the header. - writer.Write(_signature, StringCoding.Raw); - writer.Write(Version); + writer.Write(file.Key, StringCoding.Int32CharCount); + writer.Write(file.Value.Length); + writer.WriteStructs(file.Value); + } - // Write the scheme flags. - writer.WriteStruct(Flags); + // Write attached scripts. + writer.Write(Scripts.Count); + foreach (KeyValuePair script in Scripts) + { + writer.Write(script.Key, StringCoding.Int32CharCount); + writer.Write(script.Value, StringCoding.Int32CharCount); + } - // Write the weapon tables. - writer.Write(WeaponTables.Count); - foreach (Weapon[] weaponTable in WeaponTables) + // Write required libraries. + writer.Write(Libraries.Count); + writer.Write(Libraries); + + // Write a possibly attached scheme file. + if (GameScheme != null) + { + byte[] schemeBytes; + using (MemoryStream schemeStream = new MemoryStream()) { - writer.Save(weaponTable); - } - - // Write a placeholder array. - writer.Write(0); - - // Write attached files. - writer.Write(Files.Count); - foreach (KeyValuePair file in Files) - { - writer.Write(file.Key, StringCoding.Int32CharCount); - writer.Write(file.Value.Length); - writer.WriteStructs(file.Value); - } - - // Write attached scripts. - writer.Write(Scripts.Count); - foreach (KeyValuePair script in Scripts) - { - writer.Write(script.Key, StringCoding.Int32CharCount); - writer.Write(script.Value, StringCoding.Int32CharCount); - } - - // Write required libraries. - writer.Write(Libraries.Count); - writer.Write(Libraries); - - // Write a possibly attached scheme file. - if (GameScheme != null) - { - byte[] schemeBytes; - using (MemoryStream schemeStream = new MemoryStream()) - { - GameScheme.Save(schemeStream); - schemeBytes = schemeStream.ToArray(); - } - writer.Write(schemeBytes.Length); - writer.Write(GameSchemeName, StringCoding.Int32CharCount); + GameScheme.Save(schemeStream); + schemeBytes = schemeStream.ToArray(); } + writer.Write(schemeBytes.Length); + writer.Write(GameSchemeName, StringCoding.Int32CharCount); } } @@ -207,10 +187,8 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/AirstrikeSubstyleConverter.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/AirstrikeSubstyleConverter.cs index 442f618..04f800f 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/AirstrikeSubstyleConverter.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/AirstrikeSubstyleConverter.cs @@ -7,28 +7,21 @@ namespace Syroot.Worms.Armageddon.ProjectX public class AirstrikeSubstyleConverter : IBinaryConverter { // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- - + public object Read(Stream stream, object instance, BinaryMemberAttribute memberAttribute, ByteConverter byteConverter) { - WeaponAirstrikeSubstyle airstrikeSubstyle; - switch (instance) + WeaponAirstrikeSubstyle airstrikeSubstyle = instance switch { - case AirstrikeStyle airstrikeStyle: - airstrikeSubstyle = airstrikeStyle.AirstrikeSubstyle; - break; - default: - throw new NotImplementedException(); - } - - switch (airstrikeSubstyle) + AirstrikeStyle airstrikeStyle => airstrikeStyle.AirstrikeSubstyle, + _ => throw new NotImplementedException(), + }; + return airstrikeSubstyle switch { - case WeaponAirstrikeSubstyle.Launcher: - return stream.ReadObject(); - case WeaponAirstrikeSubstyle.Mines: - return stream.ReadObject(); - } - return null; + WeaponAirstrikeSubstyle.Launcher => stream.ReadObject(), + WeaponAirstrikeSubstyle.Mines => stream.ReadObject(), + _ => null, + }; } public void Write(Stream stream, object instance, BinaryMemberAttribute memberAttribute, object value, diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/IStyle.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/IStyle.cs index 266fa44..abf59b5 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/IStyle.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Styles/IStyle.cs @@ -1,6 +1,4 @@ namespace Syroot.Worms.Armageddon.ProjectX { - public interface IStyle - { - } + public interface IStyle { } } diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Syroot.Worms.Armageddon.ProjectX.csproj b/src/library/Syroot.Worms.Armageddon.ProjectX/Syroot.Worms.Armageddon.ProjectX.csproj index 391dbc7..f0ceaff 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Syroot.Worms.Armageddon.ProjectX.csproj +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Syroot.Worms.Armageddon.ProjectX.csproj @@ -1,24 +1,24 @@  - - Syroot.Worms.Armageddon-ProjectX - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Worms Armageddon ProjectX. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms.Armageddon.ProjectX - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - - + + Syroot.Worms.Armageddon-ProjectX + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Worms Armageddon ProjectX. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms.Armageddon.ProjectX + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0-alpha1 + + + + + \ No newline at end of file diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/ITarget.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/ITarget.cs index 4beda55..0d9df29 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/ITarget.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/ITarget.cs @@ -1,6 +1,4 @@ namespace Syroot.Worms.Armageddon.ProjectX { - public interface ITarget - { - } + public interface ITarget { } } diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/TargetConverter.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/TargetConverter.cs index 986f83c..c17471f 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/TargetConverter.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Targets/TargetConverter.cs @@ -11,24 +11,17 @@ namespace Syroot.Worms.Armageddon.ProjectX public object Read(Stream stream, object instance, BinaryMemberAttribute memberAttribute, ByteConverter byteConverter) { - ExplosionTarget explosionTarget; - switch (instance) + ExplosionTarget explosionTarget = instance switch { - case LauncherStyle launcherStyle: - explosionTarget = launcherStyle.ExplosionTarget; - break; - default: - throw new NotImplementedException(); - } - - switch (explosionTarget) + LauncherStyle launcherStyle => launcherStyle.ExplosionTarget, + _ => throw new NotImplementedException(), + }; + return explosionTarget switch { - case ExplosionTarget.Clusters: - return stream.ReadObject(); - case ExplosionTarget.Fire: - return stream.ReadObject(); - } - return null; + ExplosionTarget.Clusters => stream.ReadObject(), + ExplosionTarget.Fire => stream.ReadObject(), + _ => null, + }; } public void Write(Stream stream, object instance, BinaryMemberAttribute memberAttribute, object value, diff --git a/src/library/Syroot.Worms.Armageddon.ProjectX/Weapon.cs b/src/library/Syroot.Worms.Armageddon.ProjectX/Weapon.cs index c8c3ea6..05af71c 100644 --- a/src/library/Syroot.Worms.Armageddon.ProjectX/Weapon.cs +++ b/src/library/Syroot.Worms.Armageddon.ProjectX/Weapon.cs @@ -2,7 +2,6 @@ using System.Diagnostics; using System.IO; using System.Text; using Syroot.BinaryData; -using Syroot.Worms.Core; using Syroot.Worms.Core.IO; namespace Syroot.Worms.Armageddon.ProjectX @@ -114,168 +113,165 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + // Read the header. + long offset = reader.Position; + Version = reader.ReadEnum(true); + Checksum = reader.ReadInt64(); + + // Read general settings. + TableRow = reader.ReadInt32(); + Remembered = reader.ReadBoolean(BooleanCoding.Dword); + UsableInCavern = reader.ReadBoolean(BooleanCoding.Dword); + Shots = reader.ReadInt32(); + ShotEndsTurn = reader.ReadBoolean(BooleanCoding.Dword); + RetreatTime = reader.ReadInt32(); + Unknown1 = reader.ReadInt32(); + CrateChance = reader.ReadInt32(); + CrateCount = reader.ReadInt32(); + Unknown2 = reader.ReadInt32(); + + // Read the activation and the corresponding weapon settings. + Activation = reader.ReadEnum(false); + switch (Activation) { - // Read the header. - long offset = reader.Position; - Version = reader.ReadEnum(true); - Checksum = reader.ReadInt64(); - - // Read general settings. - TableRow = reader.ReadInt32(); - Remembered = reader.ReadBoolean(BooleanCoding.Dword); - UsableInCavern = reader.ReadBoolean(BooleanCoding.Dword); - Shots = reader.ReadInt32(); - ShotEndsTurn = reader.ReadBoolean(BooleanCoding.Dword); - RetreatTime = reader.ReadInt32(); - Unknown1 = reader.ReadInt32(); - CrateChance = reader.ReadInt32(); - CrateCount = reader.ReadInt32(); - Unknown2 = reader.ReadInt32(); - - // Read the activation and the corresponding weapon settings. - Activation = reader.ReadEnum(false); - switch (Activation) - { - case WeaponActivation.Airstrike: - AirstrikeSubtype = reader.ReadInt32(); - Style = reader.ReadObject(); - break; - case WeaponActivation.Crosshair: - NotUsed = reader.ReadInt32(); - CrosshairAction = reader.ReadEnum(false); - switch (CrosshairAction) - { - case WeaponCrosshairAction.Bow: - Style = reader.ReadObject(); - break; - case WeaponCrosshairAction.Flamethrower: - Style = reader.ReadObject(); - break; - case WeaponCrosshairAction.Gun: - Style = reader.ReadObject(); - break; - case WeaponCrosshairAction.Launcher: - Style = reader.ReadObject(); - break; - } - break; - case WeaponActivation.Spacebar: - SpacebarAction = reader.ReadEnum(false); - switch (SpacebarAction) - { - case WeaponSpacebarAction.Armageddon: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.BaseballBat: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.BattleAxe: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Blowtorch: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Dragonball: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Firepunch: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Jetpack: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Kamikaze: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.NinjaRope: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.NuclearTest: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Parachute: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.PneumaticDrill: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Prod: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.SuicideBomber: - Style = reader.ReadObject(); - break; - case WeaponSpacebarAction.Bungee: - case WeaponSpacebarAction.Earthquake: - case WeaponSpacebarAction.Freeze: - case WeaponSpacebarAction.Girder: - case WeaponSpacebarAction.ScalesOfJustice: - case WeaponSpacebarAction.SelectWorm: - case WeaponSpacebarAction.SkipGo: - case WeaponSpacebarAction.Surrender: - case WeaponSpacebarAction.Teleport: - case WeaponSpacebarAction.Utility: - Style = null; - break; - } - break; - case WeaponActivation.Throw: - ThrowHerdCount = reader.ReadInt32(); - ThrowAction = reader.ReadEnum(false); - switch (ThrowAction) - { - case WeaponThrowAction.Canister: - Style = reader.ReadObject(); - break; - case WeaponThrowAction.Launcher: - Style = reader.ReadObject(); - break; - case WeaponThrowAction.Mine: - Style = reader.ReadObject(); - break; - } - break; - } - - // Read additional settings. - reader.Position = offset + 468; - AmmunitionOverride = reader.ReadBoolean(BooleanCoding.Dword); - Ammunition = reader.ReadInt32(); - Unknown3 = reader.ReadInt32(); - WeaponSprite = reader.ReadInt32(); - NameLong = reader.ReadString(StringCoding.Int32CharCount); - Name = reader.ReadString(StringCoding.Int32CharCount); - GridImageFile = reader.ReadString(StringCoding.Int32CharCount); - GfxDirectoryFile = reader.ReadString(StringCoding.Int32CharCount); - SpriteNames = reader.ReadStrings(5, StringCoding.Int32CharCount); - DelayOverride = reader.ReadBoolean(BooleanCoding.Dword); - Delay = reader.ReadInt32(); - - UseLibrary = reader.ReadBoolean(); - if (UseLibrary) - { - LibraryName = reader.ReadString(StringCoding.Int32CharCount); - LibraryWeaponName = reader.ReadString(StringCoding.Int32CharCount); - } - - AimSpriteEven = reader.ReadString(StringCoding.Int32CharCount); - AimSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); - AimSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); - PickSpriteEven = reader.ReadString(StringCoding.Int32CharCount); - PickSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); - PickSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); - FireSpriteEven = reader.ReadString(StringCoding.Int32CharCount); - FireSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); - FireSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); - AimSpriteOverride = reader.ReadBoolean(); - PickSpriteOverride = reader.ReadBoolean(); - FireSpriteOverride = reader.ReadBoolean(); - if (Version == WeaponVersion.Version_0_8_0) - { - Utility = reader.ReadBoolean(); - } + case WeaponActivation.Airstrike: + AirstrikeSubtype = reader.ReadInt32(); + Style = reader.ReadObject(); + break; + case WeaponActivation.Crosshair: + NotUsed = reader.ReadInt32(); + CrosshairAction = reader.ReadEnum(false); + switch (CrosshairAction) + { + case WeaponCrosshairAction.Bow: + Style = reader.ReadObject(); + break; + case WeaponCrosshairAction.Flamethrower: + Style = reader.ReadObject(); + break; + case WeaponCrosshairAction.Gun: + Style = reader.ReadObject(); + break; + case WeaponCrosshairAction.Launcher: + Style = reader.ReadObject(); + break; + } + break; + case WeaponActivation.Spacebar: + SpacebarAction = reader.ReadEnum(false); + switch (SpacebarAction) + { + case WeaponSpacebarAction.Armageddon: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.BaseballBat: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.BattleAxe: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Blowtorch: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Dragonball: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Firepunch: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Jetpack: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Kamikaze: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.NinjaRope: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.NuclearTest: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Parachute: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.PneumaticDrill: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Prod: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.SuicideBomber: + Style = reader.ReadObject(); + break; + case WeaponSpacebarAction.Bungee: + case WeaponSpacebarAction.Earthquake: + case WeaponSpacebarAction.Freeze: + case WeaponSpacebarAction.Girder: + case WeaponSpacebarAction.ScalesOfJustice: + case WeaponSpacebarAction.SelectWorm: + case WeaponSpacebarAction.SkipGo: + case WeaponSpacebarAction.Surrender: + case WeaponSpacebarAction.Teleport: + case WeaponSpacebarAction.Utility: + Style = null; + break; + } + break; + case WeaponActivation.Throw: + ThrowHerdCount = reader.ReadInt32(); + ThrowAction = reader.ReadEnum(false); + switch (ThrowAction) + { + case WeaponThrowAction.Canister: + Style = reader.ReadObject(); + break; + case WeaponThrowAction.Launcher: + Style = reader.ReadObject(); + break; + case WeaponThrowAction.Mine: + Style = reader.ReadObject(); + break; + } + break; } + + // Read additional settings. + reader.Position = offset + 468; + AmmunitionOverride = reader.ReadBoolean(BooleanCoding.Dword); + Ammunition = reader.ReadInt32(); + Unknown3 = reader.ReadInt32(); + WeaponSprite = reader.ReadInt32(); + NameLong = reader.ReadString(StringCoding.Int32CharCount); + Name = reader.ReadString(StringCoding.Int32CharCount); + GridImageFile = reader.ReadString(StringCoding.Int32CharCount); + GfxDirectoryFile = reader.ReadString(StringCoding.Int32CharCount); + SpriteNames = reader.ReadStrings(5, StringCoding.Int32CharCount); + DelayOverride = reader.ReadBoolean(BooleanCoding.Dword); + Delay = reader.ReadInt32(); + + UseLibrary = reader.ReadBoolean(); + if (UseLibrary) + { + LibraryName = reader.ReadString(StringCoding.Int32CharCount); + LibraryWeaponName = reader.ReadString(StringCoding.Int32CharCount); + } + + AimSpriteEven = reader.ReadString(StringCoding.Int32CharCount); + AimSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); + AimSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); + PickSpriteEven = reader.ReadString(StringCoding.Int32CharCount); + PickSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); + PickSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); + FireSpriteEven = reader.ReadString(StringCoding.Int32CharCount); + FireSpriteUphill = reader.ReadString(StringCoding.Int32CharCount); + FireSpriteDownhill = reader.ReadString(StringCoding.Int32CharCount); + AimSpriteOverride = reader.ReadBoolean(); + PickSpriteOverride = reader.ReadBoolean(); + FireSpriteOverride = reader.ReadBoolean(); + if (Version == WeaponVersion.Version_0_8_0) + Utility = reader.ReadBoolean(); } /// @@ -284,111 +280,104 @@ namespace Syroot.Worms.Armageddon.ProjectX /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + // Write the header. + long offset = writer.Position; + writer.WriteEnum(Version, true); + writer.Write(Checksum); + + // Write the general settings. + writer.Write(TableRow); + writer.Write(Remembered, BooleanCoding.Dword); + writer.Write(UsableInCavern, BooleanCoding.Dword); + writer.Write(Shots); + writer.Write(ShotEndsTurn, BooleanCoding.Dword); + writer.Write(RetreatTime); + writer.Write(Unknown1); + writer.Write(CrateChance); + writer.Write(CrateCount); + writer.Write(Unknown2); + + // Write the activation and the corresponding weapon settings. + writer.WriteEnum(Activation, true); + switch (Activation) { - // Write the header. - long offset = writer.Position; - writer.WriteEnum(Version, true); - writer.Write(Checksum); - - // Write the general settings. - writer.Write(TableRow); - writer.Write(Remembered, BooleanCoding.Dword); - writer.Write(UsableInCavern, BooleanCoding.Dword); - writer.Write(Shots); - writer.Write(ShotEndsTurn, BooleanCoding.Dword); - writer.Write(RetreatTime); - writer.Write(Unknown1); - writer.Write(CrateChance); - writer.Write(CrateCount); - writer.Write(Unknown2); - - // Write the activation and the corresponding weapon settings. - writer.WriteEnum(Activation, true); - switch (Activation) - { - case WeaponActivation.Airstrike: - writer.Write(AirstrikeSubtype); + case WeaponActivation.Airstrike: + writer.Write(AirstrikeSubtype); + writer.WriteObject(Style); + break; + case WeaponActivation.Crosshair: + writer.Write(NotUsed); + writer.WriteEnum(CrosshairAction, true); + if (CrosshairAction != WeaponCrosshairAction.None) writer.WriteObject(Style); - break; - case WeaponActivation.Crosshair: - writer.Write(NotUsed); - writer.WriteEnum(CrosshairAction, true); - if (CrosshairAction != WeaponCrosshairAction.None) - { + break; + case WeaponActivation.Spacebar: + writer.WriteEnum(SpacebarAction, true); + switch (SpacebarAction) + { + case WeaponSpacebarAction.Armageddon: + case WeaponSpacebarAction.BaseballBat: + case WeaponSpacebarAction.BattleAxe: + case WeaponSpacebarAction.Blowtorch: + case WeaponSpacebarAction.Dragonball: + case WeaponSpacebarAction.Firepunch: + case WeaponSpacebarAction.Jetpack: + case WeaponSpacebarAction.Kamikaze: + case WeaponSpacebarAction.NinjaRope: + case WeaponSpacebarAction.NuclearTest: + case WeaponSpacebarAction.Parachute: + case WeaponSpacebarAction.PneumaticDrill: + case WeaponSpacebarAction.Prod: + case WeaponSpacebarAction.SuicideBomber: writer.WriteObject(Style); - } - break; - case WeaponActivation.Spacebar: - writer.WriteEnum(SpacebarAction, true); - switch (SpacebarAction) - { - case WeaponSpacebarAction.Armageddon: - case WeaponSpacebarAction.BaseballBat: - case WeaponSpacebarAction.BattleAxe: - case WeaponSpacebarAction.Blowtorch: - case WeaponSpacebarAction.Dragonball: - case WeaponSpacebarAction.Firepunch: - case WeaponSpacebarAction.Jetpack: - case WeaponSpacebarAction.Kamikaze: - case WeaponSpacebarAction.NinjaRope: - case WeaponSpacebarAction.NuclearTest: - case WeaponSpacebarAction.Parachute: - case WeaponSpacebarAction.PneumaticDrill: - case WeaponSpacebarAction.Prod: - case WeaponSpacebarAction.SuicideBomber: - writer.WriteObject(Style); - break; - } - break; - case WeaponActivation.Throw: - writer.Write(ThrowHerdCount); - writer.WriteEnum(ThrowAction, true); - if (ThrowAction != WeaponThrowAction.None) - { - writer.WriteObject(Style); - } - break; - } - - // Write additional settings. - writer.Position = offset + 468; - writer.Write(AmmunitionOverride, BooleanCoding.Dword); - writer.Write(Ammunition); - writer.Write(Unknown3); - writer.Write(WeaponSprite); - writer.Write(NameLong, StringCoding.Int32CharCount); - writer.Write(Name, StringCoding.Int32CharCount); - writer.Write(GridImageFile, StringCoding.Int32CharCount); - writer.Write(GfxDirectoryFile, StringCoding.Int32CharCount); - writer.Write(SpriteNames, StringCoding.Int32CharCount); - writer.Write(DelayOverride, BooleanCoding.Dword); - writer.Write(Delay); - - writer.Write(UseLibrary); - if (UseLibrary) - { - writer.Write(LibraryName, StringCoding.Int32CharCount); - writer.Write(LibraryWeaponName, StringCoding.Int32CharCount); - } - - writer.Write(AimSpriteEven, StringCoding.Int32CharCount); - writer.Write(AimSpriteUphill, StringCoding.Int32CharCount); - writer.Write(AimSpriteDownhill, StringCoding.Int32CharCount); - writer.Write(PickSpriteEven, StringCoding.Int32CharCount); - writer.Write(PickSpriteUphill, StringCoding.Int32CharCount); - writer.Write(PickSpriteDownhill, StringCoding.Int32CharCount); - writer.Write(FireSpriteEven, StringCoding.Int32CharCount); - writer.Write(FireSpriteUphill, StringCoding.Int32CharCount); - writer.Write(FireSpriteDownhill, StringCoding.Int32CharCount); - writer.Write(AimSpriteOverride); - writer.Write(PickSpriteOverride); - writer.Write(FireSpriteOverride); - if (Version == WeaponVersion.Version_0_8_0) - { - writer.Write(Utility); - } + break; + } + break; + case WeaponActivation.Throw: + writer.Write(ThrowHerdCount); + writer.WriteEnum(ThrowAction, true); + if (ThrowAction != WeaponThrowAction.None) + writer.WriteObject(Style); + break; } + + // Write additional settings. + writer.Position = offset + 468; + writer.Write(AmmunitionOverride, BooleanCoding.Dword); + writer.Write(Ammunition); + writer.Write(Unknown3); + writer.Write(WeaponSprite); + writer.Write(NameLong, StringCoding.Int32CharCount); + writer.Write(Name, StringCoding.Int32CharCount); + writer.Write(GridImageFile, StringCoding.Int32CharCount); + writer.Write(GfxDirectoryFile, StringCoding.Int32CharCount); + writer.Write(SpriteNames, StringCoding.Int32CharCount); + writer.Write(DelayOverride, BooleanCoding.Dword); + writer.Write(Delay); + + writer.Write(UseLibrary); + if (UseLibrary) + { + writer.Write(LibraryName, StringCoding.Int32CharCount); + writer.Write(LibraryWeaponName, StringCoding.Int32CharCount); + } + + writer.Write(AimSpriteEven, StringCoding.Int32CharCount); + writer.Write(AimSpriteUphill, StringCoding.Int32CharCount); + writer.Write(AimSpriteDownhill, StringCoding.Int32CharCount); + writer.Write(PickSpriteEven, StringCoding.Int32CharCount); + writer.Write(PickSpriteUphill, StringCoding.Int32CharCount); + writer.Write(PickSpriteDownhill, StringCoding.Int32CharCount); + writer.Write(FireSpriteEven, StringCoding.Int32CharCount); + writer.Write(FireSpriteUphill, StringCoding.Int32CharCount); + writer.Write(FireSpriteDownhill, StringCoding.Int32CharCount); + writer.Write(AimSpriteOverride); + writer.Write(PickSpriteOverride); + writer.Write(FireSpriteOverride); + if (Version == WeaponVersion.Version_0_8_0) + writer.Write(Utility); } } diff --git a/src/library/Syroot.Worms.Armageddon/GeneratedMap.cs b/src/library/Syroot.Worms.Armageddon/GeneratedMap.cs index ebd0003..8b4d546 100644 --- a/src/library/Syroot.Worms.Armageddon/GeneratedMap.cs +++ b/src/library/Syroot.Worms.Armageddon/GeneratedMap.cs @@ -16,28 +16,20 @@ namespace Syroot.Worms.Armageddon /// /// Initializes a new instance of the class. /// - public GeneratedMap() - { - } + public GeneratedMap() { } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public GeneratedMap(Stream stream) - { - Load(stream); - } + public GeneratedMap(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public GeneratedMap(string fileName) - { - Load(fileName); - } + public GeneratedMap(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -54,10 +46,8 @@ namespace Syroot.Worms.Armageddon /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - Settings = reader.ReadStruct(); - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + Settings = reader.ReadStruct(); } /// @@ -66,10 +56,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -78,10 +66,8 @@ namespace Syroot.Worms.Armageddon /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - writer.WriteStruct(Settings); - } + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + writer.WriteStruct(Settings); } /// @@ -90,10 +76,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Armageddon/LandData.cs b/src/library/Syroot.Worms.Armageddon/LandData.cs index 9b2e84e..42efa01 100644 --- a/src/library/Syroot.Worms.Armageddon/LandData.cs +++ b/src/library/Syroot.Worms.Armageddon/LandData.cs @@ -28,19 +28,13 @@ namespace Syroot.Worms.Armageddon /// . /// /// The to load the data from. - public LandData(Stream stream) - { - Load(stream); - } + public LandData(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public LandData(string fileName) - { - Load(fileName); - } + public LandData(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -102,31 +96,30 @@ namespace Syroot.Worms.Armageddon /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadInt32() != _signature) - throw new InvalidDataException("Invalid LND file signature."); - int fileSize = reader.ReadInt32(); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the data. - Size = reader.ReadStruct(); - TopBorder = reader.ReadBoolean(BooleanCoding.Dword); - WaterHeight = reader.ReadInt32(); - Unknown = reader.ReadInt32(); + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid LND file signature."); + int fileSize = reader.ReadInt32(); - // Read the possible object coordinate array. - ObjectLocations = reader.ReadStructs(reader.ReadInt32()); + // Read the data. + Size = reader.ReadStruct(); + TopBorder = reader.ReadBoolean(BooleanCoding.Dword); + WaterHeight = reader.ReadInt32(); + Unknown = reader.ReadInt32(); - // Read the image data. - Foreground = reader.Load(); - CollisionMask = reader.Load(); - Background = reader.Load(); + // Read the possible object coordinate array. + ObjectLocations = reader.ReadStructs(reader.ReadInt32()); - // Read the file paths. - LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); - WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); - } + // Read the image data. + Foreground = reader.Load(); + CollisionMask = reader.Load(); + Background = reader.Load(); + + // Read the file paths. + LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); + WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); } /// @@ -135,8 +128,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -145,33 +138,32 @@ namespace Syroot.Worms.Armageddon /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature); - uint fileSizeOffset = writer.ReserveOffset(); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the data. - writer.WriteStruct(Size); - writer.Write(TopBorder, BooleanCoding.Dword); - writer.Write(WaterHeight); - writer.Write(Unknown); + // Write the header. + writer.Write(_signature); + uint fileSizeOffset = writer.ReserveOffset(); - // Write the possible object coordinate array. - writer.Write(ObjectLocations.Length); - writer.WriteStructs(ObjectLocations); + // Write the data. + writer.WriteStruct(Size); + writer.Write(TopBorder, BooleanCoding.Dword); + writer.Write(WaterHeight); + writer.Write(Unknown); - // Write the image data. - Foreground.Save(writer.BaseStream); - CollisionMask.Save(writer.BaseStream); - Background.Save(writer.BaseStream); + // Write the possible object coordinate array. + writer.Write(ObjectLocations.Length); + writer.WriteStructs(ObjectLocations); - // Write the file paths. - writer.Write(LandTexturePath, StringCoding.ByteCharCount); - writer.Write(WaterDirPath, StringCoding.ByteCharCount); + // Write the image data. + Foreground.Save(writer.BaseStream); + CollisionMask.Save(writer.BaseStream); + Background.Save(writer.BaseStream); - writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); - } + // Write the file paths. + writer.Write(LandTexturePath, StringCoding.ByteCharCount); + writer.Write(WaterDirPath, StringCoding.ByteCharCount); + + writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); } /// @@ -180,8 +172,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - Save(stream); + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Armageddon/Scheme.cs b/src/library/Syroot.Worms.Armageddon/Scheme.cs index f7f69fa..40a7396 100644 --- a/src/library/Syroot.Worms.Armageddon/Scheme.cs +++ b/src/library/Syroot.Worms.Armageddon/Scheme.cs @@ -99,19 +99,13 @@ namespace Syroot.Worms.Armageddon /// . /// /// The to load the data from. - public Scheme(Stream stream) - { - Load(stream); - } + public Scheme(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public Scheme(string fileName) - { - Load(fileName); - } + public Scheme(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -229,16 +223,11 @@ namespace Syroot.Worms.Armageddon /// public byte MineDelay { - get - { - return _mineDelay; - } + get => _mineDelay; set { if (value == 4 || value > 0x7F) - { throw new ArgumentException("Mine delay must be between 0-127 and not be 4.", nameof(value)); - } _mineDelay = value; } } @@ -270,16 +259,11 @@ namespace Syroot.Worms.Armageddon /// public byte TurnTime { - get - { - return _turnTime; - } + get => _turnTime; set { if (value > 0x7F) - { throw new ArgumentException("Turn time must be between 0-127.", nameof(value)); - } _turnTime = value; } } @@ -295,16 +279,11 @@ namespace Syroot.Worms.Armageddon /// public byte RoundTimeMinutes { - get - { - return _roundTimeMinutes; - } + get => _roundTimeMinutes; set { if (value > 0x7F) - { throw new ArgumentException("Round time must be between 0-127 minutes.", nameof(value)); - } _roundTimeMinutes = value; } } @@ -315,16 +294,11 @@ namespace Syroot.Worms.Armageddon /// public byte RoundTimeSeconds { - get - { - return _roundTimeSeconds; - } + get => _roundTimeSeconds; set { if (value > 0x80) - { throw new ArgumentException("Round time must be between 0-128 seconds.", nameof(value)); - } _roundTimeSeconds = value; } } @@ -334,16 +308,11 @@ namespace Syroot.Worms.Armageddon /// public byte NumberOfWins { - get - { - return _numberOfWins; - } + get => _numberOfWins; set { if (value == 0) - { throw new ArgumentException("Number of wins must not be 0.", nameof(value)); - } _numberOfWins = value; } } @@ -486,10 +455,7 @@ namespace Syroot.Worms.Armageddon /// public sbyte RwGravity { - get - { - return _rwGravity; - } + get => _rwGravity; set { if (value != 0) @@ -507,10 +473,7 @@ namespace Syroot.Worms.Armageddon /// public sbyte RwGravityConstBlackHole { - get - { - return _rwGravityConstBlackHole; - } + get => _rwGravityConstBlackHole; set { if (value != 0) @@ -529,10 +492,7 @@ namespace Syroot.Worms.Armageddon /// public sbyte RwGravityPropBlackHole { - get - { - return _rwGravityPropBlackHole; - } + get => _rwGravityPropBlackHole; set { if (value != 0) @@ -550,16 +510,11 @@ namespace Syroot.Worms.Armageddon /// public byte RwKaosMod { - get - { - return _rwKaosMod; - } + get => _rwKaosMod; set { if (value > 0xF) - { throw new ArgumentException("Kaos mod must not be greater than 15."); - } _rwKaosMod = value; } } @@ -650,66 +605,61 @@ namespace Syroot.Worms.Armageddon /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadString(_signature.Length) != _signature) - { - throw new InvalidDataException("Invalid WSC file signature."); - } - Version = reader.ReadEnum(true); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the options. - HotSeatDelay = reader.Read1Byte(); - RetreatTime = reader.Read1Byte(); - RetreatTimeRope = reader.Read1Byte(); - ShowRoundTime = reader.ReadBoolean(); - AutomaticReplays = reader.ReadBoolean(); - FallDamage = (SchemeFallDamage)(reader.ReadByte() * 50 % 0x100 * 2); - ArtilleryMode = reader.ReadBoolean(); - SchemeEditor = reader.ReadEnum(false); - StockpilingMode = reader.ReadEnum(true); - WormSelectMode = reader.ReadEnum(true); - SuddenDeathEvent = reader.ReadEnum(true); - WaterRiseRate = (SchemeWaterRise)(Math.Pow(reader.ReadByte(), 2) * 5 % 0x100); - WeaponCrateProbability = reader.ReadSByte(); - DonorCards = reader.ReadBoolean(); - HealthCrateProbability = reader.ReadSByte(); - HealthCrateEnergy = reader.Read1Byte(); - UtilityCrateProbability = reader.ReadSByte(); - LoadObjectTypesAndCount(reader); - LoadMineDelayConfig(reader); - DudMines = reader.ReadBoolean(); - ManualWormPlacement = reader.ReadBoolean(); - WormEnergy = reader.Read1Byte(); - LoadTurnTimeConfig(reader); - LoadRoundTimeConfig(reader); - NumberOfWins = (byte)Math.Max(1, reader.ReadByte()); - Blood = reader.ReadBoolean(); - AquaSheep = reader.ReadBoolean(); - SheepHeaven = reader.ReadBoolean(); - GodWorms = reader.ReadBoolean(); - IndestructibleLand = reader.ReadBoolean(); - UpgradedGrenade = reader.ReadBoolean(); - UpgradedShotgun = reader.ReadBoolean(); - UpgradedCluster = reader.ReadBoolean(); - UpgradedLongbow = reader.ReadBoolean(); - EnableTeamWeapons = reader.ReadBoolean(); - EnableSuperWeapons = reader.ReadBoolean(); + // Read the header. + if (reader.ReadString(_signature.Length) != _signature) + throw new InvalidDataException("Invalid WSC file signature."); + Version = reader.ReadEnum(true); - // Read the weapon settings. Old versions do not store super weapon settings. - Weapons = new SchemeWeaponSetting[64]; - int weaponCount = GetWeaponCount(); - for (int i = 0; i < weaponCount; i++) - { - Weapons[i] = reader.ReadStruct(); - } + // Read the options. + HotSeatDelay = reader.Read1Byte(); + RetreatTime = reader.Read1Byte(); + RetreatTimeRope = reader.Read1Byte(); + ShowRoundTime = reader.ReadBoolean(); + AutomaticReplays = reader.ReadBoolean(); + FallDamage = (SchemeFallDamage)(reader.ReadByte() * 50 % 0x100 * 2); + ArtilleryMode = reader.ReadBoolean(); + SchemeEditor = reader.ReadEnum(false); + StockpilingMode = reader.ReadEnum(true); + WormSelectMode = reader.ReadEnum(true); + SuddenDeathEvent = reader.ReadEnum(true); + WaterRiseRate = (SchemeWaterRise)(Math.Pow(reader.ReadByte(), 2) * 5 % 0x100); + WeaponCrateProbability = reader.ReadSByte(); + DonorCards = reader.ReadBoolean(); + HealthCrateProbability = reader.ReadSByte(); + HealthCrateEnergy = reader.Read1Byte(); + UtilityCrateProbability = reader.ReadSByte(); + LoadObjectTypesAndCount(reader); + LoadMineDelayConfig(reader); + DudMines = reader.ReadBoolean(); + ManualWormPlacement = reader.ReadBoolean(); + WormEnergy = reader.Read1Byte(); + LoadTurnTimeConfig(reader); + LoadRoundTimeConfig(reader); + NumberOfWins = (byte)Math.Max(1, reader.ReadByte()); + Blood = reader.ReadBoolean(); + AquaSheep = reader.ReadBoolean(); + SheepHeaven = reader.ReadBoolean(); + GodWorms = reader.ReadBoolean(); + IndestructibleLand = reader.ReadBoolean(); + UpgradedGrenade = reader.ReadBoolean(); + UpgradedShotgun = reader.ReadBoolean(); + UpgradedCluster = reader.ReadBoolean(); + UpgradedLongbow = reader.ReadBoolean(); + EnableTeamWeapons = reader.ReadBoolean(); + EnableSuperWeapons = reader.ReadBoolean(); - // Ignore possible unknown WWP trash at the end of the file. + // Read the weapon settings. Old versions do not store super weapon settings. + Weapons = new SchemeWeaponSetting[64]; + int weaponCount = GetWeaponCount(); + for (int i = 0; i < weaponCount; i++) + Weapons[i] = reader.ReadStruct(); - // Parse the RubberWorm settings. - LoadRubberWormSettings(); - } + // Ignore possible unknown WWP trash at the end of the file. + + // Parse the RubberWorm settings. + LoadRubberWormSettings(); } /// @@ -718,20 +668,15 @@ namespace Syroot.Worms.Armageddon /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// /// Saves the data into the given . /// /// The to save the data to. - public void Save(Stream stream) - { - Save(stream, SchemeSaveFormat.ExtendedWithObjectCount); - } + public void Save(Stream stream) => Save(stream, SchemeSaveFormat.ExtendedWithObjectCount); /// /// Saves the data into the given with the specified . @@ -740,72 +685,64 @@ namespace Syroot.Worms.Armageddon /// The to respect when storing the settings. public void Save(Stream stream, SchemeSaveFormat format) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature, StringCoding.Raw); - writer.Write((byte)Version); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the options. - writer.Write(HotSeatDelay); - writer.Write(RetreatTime); - writer.Write(RetreatTimeRope); - writer.Write(ShowRoundTime); - writer.Write(AutomaticReplays); - writer.Write((byte)((int)FallDamage / 4 * 41 % 0x80)); - writer.Write(ArtilleryMode); - writer.WriteEnum(SchemeEditor, false); - writer.WriteEnum(StockpilingMode, true); - writer.WriteEnum(WormSelectMode, true); - writer.WriteEnum(SuddenDeathEvent, true); - writer.Write(_mapWaterRiseToRaw[(byte)WaterRiseRate]); - writer.Write(WeaponCrateProbability); - writer.Write(DonorCards); - writer.Write(HealthCrateProbability); - writer.Write(HealthCrateEnergy); - writer.Write(UtilityCrateProbability); - SaveObjectTypesAndCount(writer, format); - SaveMineDelayConfig(writer); - writer.Write(DudMines); - writer.Write(ManualWormPlacement); - writer.Write(WormEnergy); - SaveTurnTimeConfig(writer); - SaveRoundTimeConfig(writer); - writer.Write(NumberOfWins); - writer.Write(Blood); - writer.Write(AquaSheep); - writer.Write(SheepHeaven); - writer.Write(GodWorms); - writer.Write(IndestructibleLand); - writer.Write(UpgradedGrenade); - writer.Write(UpgradedShotgun); - writer.Write(UpgradedCluster); - writer.Write(UpgradedLongbow); - writer.Write(EnableTeamWeapons); - writer.Write(EnableSuperWeapons); + // Write the header. + writer.Write(_signature, StringCoding.Raw); + writer.Write((byte)Version); - // Transfer the RubberWorm settings to unused weapon configuration. - SaveRubberWormSettings(); + // Write the options. + writer.Write(HotSeatDelay); + writer.Write(RetreatTime); + writer.Write(RetreatTimeRope); + writer.Write(ShowRoundTime); + writer.Write(AutomaticReplays); + writer.Write((byte)((int)FallDamage / 4 * 41 % 0x80)); + writer.Write(ArtilleryMode); + writer.WriteEnum(SchemeEditor, false); + writer.WriteEnum(StockpilingMode, true); + writer.WriteEnum(WormSelectMode, true); + writer.WriteEnum(SuddenDeathEvent, true); + writer.Write(_mapWaterRiseToRaw[(byte)WaterRiseRate]); + writer.Write(WeaponCrateProbability); + writer.Write(DonorCards); + writer.Write(HealthCrateProbability); + writer.Write(HealthCrateEnergy); + writer.Write(UtilityCrateProbability); + SaveObjectTypesAndCount(writer, format); + SaveMineDelayConfig(writer); + writer.Write(DudMines); + writer.Write(ManualWormPlacement); + writer.Write(WormEnergy); + SaveTurnTimeConfig(writer); + SaveRoundTimeConfig(writer); + writer.Write(NumberOfWins); + writer.Write(Blood); + writer.Write(AquaSheep); + writer.Write(SheepHeaven); + writer.Write(GodWorms); + writer.Write(IndestructibleLand); + writer.Write(UpgradedGrenade); + writer.Write(UpgradedShotgun); + writer.Write(UpgradedCluster); + writer.Write(UpgradedLongbow); + writer.Write(EnableTeamWeapons); + writer.Write(EnableSuperWeapons); - // Write the weapon settings. Old versions do not store super weapon settings. - int weaponCount = GetWeaponCount(); - foreach (SchemeWeaponSetting weapon in Weapons) - { - writer.WriteStruct(weapon); - } + // Transfer the RubberWorm settings to unused weapon configuration. + SaveRubberWormSettings(); - // Ignore possible unknown WWP trash at the end of the file. - } + // Write the weapon settings. Old versions do not store super weapon settings. + int weaponCount = GetWeaponCount(); + foreach (SchemeWeaponSetting weapon in Weapons) + writer.WriteStruct(weapon); } /// /// Saves the data in the given file. /// /// The name of the file to save the data in. - public void Save(string fileName) - { - Save(fileName, SchemeSaveFormat.ExtendedWithObjectCount); - } + public void Save(string fileName) => Save(fileName, SchemeSaveFormat.ExtendedWithObjectCount); /// /// Saves the data in the given file with the specified . @@ -814,10 +751,8 @@ namespace Syroot.Worms.Armageddon /// The to respect when storing the settings. public void Save(string fileName, SchemeSaveFormat format) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream, format); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream, format); } // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- @@ -942,13 +877,9 @@ namespace Syroot.Worms.Armageddon if (mailStrikeProb.GetBit(7)) { if (mailStrikeProb.GetBit(6)) - { RwGravityPropBlackHole = mailStrikeProb.DecodeSByte(6); - } else - { RwGravityConstBlackHole = mailStrikeProb.DecodeSByte(6); - } } else { @@ -1019,37 +950,25 @@ namespace Syroot.Worms.Armageddon private void SaveMineDelayConfig(BinaryStream writer) { if (MineDelayRandom) - { writer.Write((byte)4); - } else - { writer.Write(MineDelay); - } } private void SaveTurnTimeConfig(BinaryStream writer) { if (TurnTimeInfinite) - { writer.Write((byte)0xFF); - } else - { writer.Write(TurnTime); - } } private void SaveRoundTimeConfig(BinaryStream writer) { if (RoundTimeSeconds > 0) - { writer.Write((byte)(0xFF - (RoundTimeSeconds - 1))); - } else - { writer.Write(RoundTimeMinutes); - } } private void SaveRubberWormSettings() diff --git a/src/library/Syroot.Worms.Armageddon/Syroot.Worms.Armageddon.csproj b/src/library/Syroot.Worms.Armageddon/Syroot.Worms.Armageddon.csproj index 7df7c6c..93b7b8d 100644 --- a/src/library/Syroot.Worms.Armageddon/Syroot.Worms.Armageddon.csproj +++ b/src/library/Syroot.Worms.Armageddon/Syroot.Worms.Armageddon.csproj @@ -1,23 +1,23 @@  - - Syroot.Worms.Armageddon - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Team17's Worms Armageddon. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms.Armageddon - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - + + Syroot.Worms.Armageddon + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Team17's Worms Armageddon. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms.Armageddon + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0-alpha1 + + + + \ No newline at end of file diff --git a/src/library/Syroot.Worms.Armageddon/Team.cs b/src/library/Syroot.Worms.Armageddon/Team.cs index 6df6595..50241d4 100644 --- a/src/library/Syroot.Worms.Armageddon/Team.cs +++ b/src/library/Syroot.Worms.Armageddon/Team.cs @@ -196,59 +196,58 @@ namespace Syroot.Worms.Armageddon /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + Name = reader.ReadString(17); + WormNames = reader.ReadStrings(8, 17); + CpuLevel = reader.Read1Byte(); + SoundBankName = reader.ReadString(0x20); + SoundBankLocation = reader.Read1Byte(); + FanfareName = reader.ReadString(0x20); + UseCustomFanfare = reader.Read1Byte(); + + GraveSprite = reader.ReadSByte(); + if (GraveSprite < 0) { - Name = reader.ReadString(17); - WormNames = reader.ReadStrings(8, 17); - CpuLevel = reader.Read1Byte(); - SoundBankName = reader.ReadString(0x20); - SoundBankLocation = reader.Read1Byte(); - FanfareName = reader.ReadString(0x20); - UseCustomFanfare = reader.Read1Byte(); - - GraveSprite = reader.ReadSByte(); - if (GraveSprite < 0) - { - GraveFileName = reader.ReadString(0x20); - Grave = new RawBitmap() - { - BitsPerPixel = 8, - Size = new Size(24, 32), - Palette = reader.ReadStructs(256), - Data = reader.ReadBytes(24 * 32) - }; - } - - TeamWeapon = reader.ReadEnum(true); - GamesLost = reader.ReadInt32(); - DeathmatchesLost = reader.ReadInt32(); - GamesWon = reader.ReadInt32(); - DeathmatchesWon = reader.ReadInt32(); - GamesDrawn = reader.ReadInt32(); - DeathmatchesDrawn = reader.ReadInt32(); - Kills = reader.ReadInt32(); - DeathmatchKills = reader.ReadInt32(); - Deaths = reader.ReadInt32(); - DeathmatchDeaths = reader.ReadInt32(); - MissionStatuses = reader.ReadStructs(_missionCount); - - FlagFileName = reader.ReadString(0x20); - Flag = new RawBitmap() + GraveFileName = reader.ReadString(0x20); + Grave = new RawBitmap() { BitsPerPixel = 8, - Size = new Size(20, 17), + Size = new Size(24, 32), Palette = reader.ReadStructs(256), - Data = reader.ReadBytes(20 * 17) + Data = reader.ReadBytes(24 * 32) }; - - DeathmatchRank = reader.Read1Byte(); - TrainingMissionTimes = reader.ReadInt32s(_trainingMissionCount); - Unknown1 = reader.ReadInt32s(10); - TrainingMissionMedals = reader.ReadBytes(_trainingMissionCount); - Unknown2 = reader.ReadBytes(10); - Unknown3 = reader.ReadInt32s(7); - Unknown4 = reader.Read1Byte(); } + + TeamWeapon = reader.ReadEnum(true); + GamesLost = reader.ReadInt32(); + DeathmatchesLost = reader.ReadInt32(); + GamesWon = reader.ReadInt32(); + DeathmatchesWon = reader.ReadInt32(); + GamesDrawn = reader.ReadInt32(); + DeathmatchesDrawn = reader.ReadInt32(); + Kills = reader.ReadInt32(); + DeathmatchKills = reader.ReadInt32(); + Deaths = reader.ReadInt32(); + DeathmatchDeaths = reader.ReadInt32(); + MissionStatuses = reader.ReadStructs(_missionCount); + + FlagFileName = reader.ReadString(0x20); + Flag = new RawBitmap() + { + BitsPerPixel = 8, + Size = new Size(20, 17), + Palette = reader.ReadStructs(256), + Data = reader.ReadBytes(20 * 17) + }; + + DeathmatchRank = reader.Read1Byte(); + TrainingMissionTimes = reader.ReadInt32s(_trainingMissionCount); + Unknown1 = reader.ReadInt32s(10); + TrainingMissionMedals = reader.ReadBytes(_trainingMissionCount); + Unknown2 = reader.ReadBytes(10); + Unknown3 = reader.ReadInt32s(7); + Unknown4 = reader.Read1Byte(); } /// @@ -257,49 +256,48 @@ namespace Syroot.Worms.Armageddon /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + writer.WriteString(Name, 17); + writer.WriteStrings(WormNames, 17); + writer.Write(CpuLevel); + writer.WriteString(SoundBankName, 0x20); + writer.Write(SoundBankLocation); + writer.WriteString(FanfareName, 0x20); + writer.Write(UseCustomFanfare); + + writer.Write(GraveSprite); + if (GraveSprite < 0) { - writer.WriteString(Name, 17); - writer.WriteStrings(WormNames, 17); - writer.Write(CpuLevel); - writer.WriteString(SoundBankName, 0x20); - writer.Write(SoundBankLocation); - writer.WriteString(FanfareName, 0x20); - writer.Write(UseCustomFanfare); - - writer.Write(GraveSprite); - if (GraveSprite < 0) - { - writer.WriteString(GraveFileName, 0x20); - writer.WriteStructs(Grave.Palette); - writer.Write(Grave.Data); - } - - writer.WriteEnum(TeamWeapon, true); - writer.Write(GamesLost); - writer.Write(DeathmatchesLost); - writer.Write(GamesWon); - writer.Write(DeathmatchesWon); - writer.Write(GamesDrawn); - writer.Write(DeathmatchesDrawn); - writer.Write(Kills); - writer.Write(DeathmatchKills); - writer.Write(Deaths); - writer.Write(DeathmatchDeaths); - writer.WriteStructs(MissionStatuses); - - writer.WriteString(FlagFileName, 0x20); - writer.WriteStructs(Flag.Palette); - writer.Write(Flag.Data); - - writer.Write(DeathmatchRank); - writer.Write(TrainingMissionTimes); - writer.Write(Unknown1); - writer.Write(TrainingMissionMedals); - writer.Write(Unknown2); - writer.Write(Unknown3); - writer.Write(Unknown4); + writer.WriteString(GraveFileName, 0x20); + writer.WriteStructs(Grave.Palette); + writer.Write(Grave.Data); } + + writer.WriteEnum(TeamWeapon, true); + writer.Write(GamesLost); + writer.Write(DeathmatchesLost); + writer.Write(GamesWon); + writer.Write(DeathmatchesWon); + writer.Write(GamesDrawn); + writer.Write(DeathmatchesDrawn); + writer.Write(Kills); + writer.Write(DeathmatchKills); + writer.Write(Deaths); + writer.Write(DeathmatchDeaths); + writer.WriteStructs(MissionStatuses); + + writer.WriteString(FlagFileName, 0x20); + writer.WriteStructs(Flag.Palette); + writer.Write(Flag.Data); + + writer.Write(DeathmatchRank); + writer.Write(TrainingMissionTimes); + writer.Write(Unknown1); + writer.Write(TrainingMissionMedals); + writer.Write(Unknown2); + writer.Write(Unknown3); + writer.Write(Unknown4); } } diff --git a/src/library/Syroot.Worms.Armageddon/TeamContainer.cs b/src/library/Syroot.Worms.Armageddon/TeamContainer.cs index fdb31e2..53b5572 100644 --- a/src/library/Syroot.Worms.Armageddon/TeamContainer.cs +++ b/src/library/Syroot.Worms.Armageddon/TeamContainer.cs @@ -22,29 +22,20 @@ namespace Syroot.Worms.Armageddon /// /// Initializes a new instance of the class. /// - public TeamContainer() - { - Teams = new List(); - } + public TeamContainer() => Teams = new List(); /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public TeamContainer(Stream stream) - { - Load(stream); - } + public TeamContainer(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public TeamContainer(string fileName) - { - Load(fileName); - } + public TeamContainer(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -76,23 +67,20 @@ namespace Syroot.Worms.Armageddon /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) - { - throw new InvalidDataException("Invalid WGT file signature."); - } - Version = reader.Read1Byte(); // Really version? + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read global settings. - byte teamCount = reader.Read1Byte(); - UnlockedFeatures = reader.ReadEnum(false); - Unknown = reader.Read1Byte(); + // Read the header. + if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) + throw new InvalidDataException("Invalid WGT file signature."); + Version = reader.Read1Byte(); // Really version? - // Read the teams. - Teams = new List(reader.Load(teamCount)); - } + // Read global settings. + byte teamCount = reader.Read1Byte(); + UnlockedFeatures = reader.ReadEnum(false); + Unknown = reader.Read1Byte(); + + // Read the teams. + Teams = new List(reader.Load(teamCount)); } /// @@ -101,10 +89,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -113,23 +99,20 @@ namespace Syroot.Worms.Armageddon /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature, StringCoding.ZeroTerminated); - writer.Write(Version); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write global settings. - writer.Write((byte)Teams.Count); - writer.WriteEnum(UnlockedFeatures, false); - writer.Write(Unknown); + // Write the header. + writer.Write(_signature, StringCoding.ZeroTerminated); + writer.Write(Version); - // Write the teams. - foreach (Team team in Teams) - { - team.Save(writer.BaseStream); - } - } + // Write global settings. + writer.Write((byte)Teams.Count); + writer.WriteEnum(UnlockedFeatures, false); + writer.Write(Unknown); + + // Write the teams. + foreach (Team team in Teams) + team.Save(writer.BaseStream); } /// @@ -138,10 +121,8 @@ namespace Syroot.Worms.Armageddon /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } diff --git a/src/library/Syroot.Worms.Mgame/Igd.cs b/src/library/Syroot.Worms.Mgame/Igd.cs index 656ec5a..85a473c 100644 --- a/src/library/Syroot.Worms.Mgame/Igd.cs +++ b/src/library/Syroot.Worms.Mgame/Igd.cs @@ -3,13 +3,14 @@ using System.Drawing; using System.IO; using Syroot.BinaryData; using Syroot.Worms.Core; +using Syroot.Worms.Core.IO; namespace Syroot.Worms.Mgame { /// /// Represents an IGD image container. /// - public class Igd + public class Igd : ILoadableFile { // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ @@ -18,20 +19,14 @@ namespace Syroot.Worms.Mgame /// . /// /// The name of the file to load the data from. - public Igd(string fileName) - { - Load(fileName); - } + public Igd(string fileName) => Load(fileName); /// /// Initializes a new instance of the class, loading data from the given /// . /// /// The to load the data from. - public Igd(Stream stream) - { - Load(stream); - } + public Igd(Stream stream) => Load(stream); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -50,8 +45,8 @@ namespace Syroot.Worms.Mgame /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// diff --git a/src/library/Syroot.Worms.Mgame/Ksf.cs b/src/library/Syroot.Worms.Mgame/Ksf.cs index 98b2636..0328444 100644 --- a/src/library/Syroot.Worms.Mgame/Ksf.cs +++ b/src/library/Syroot.Worms.Mgame/Ksf.cs @@ -23,10 +23,7 @@ namespace Syroot.Worms.Mgame /// /// The name of the file to load the data from. /// The color palette which is indexed by the image data. - public Ksf(string fileName, Palette palette) - { - Load(fileName, palette); - } + public Ksf(string fileName, Palette palette) => Load(fileName, palette); /// /// Initializes a new instance of the class, loading data from the given @@ -34,10 +31,7 @@ namespace Syroot.Worms.Mgame /// /// The to load the data from. /// The color palette which is indexed by the image data. - public Ksf(Stream stream, Palette palette) - { - Load(stream, palette); - } + public Ksf(Stream stream, Palette palette) => Load(stream, palette); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -56,8 +50,8 @@ namespace Syroot.Worms.Mgame /// The color palette which is indexed by the image data. public void Load(string fileName, Palette palette) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream, palette); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream, palette); } /// @@ -69,7 +63,7 @@ namespace Syroot.Worms.Mgame public void Load(Stream stream, Palette palette) { int imageCount = stream.ReadInt32(); // Includes terminator. - int dataSize = stream.ReadInt32(); + _ = stream.ReadInt32(); // data size // Read image headers. Terminating image is of 0 size and data offset at end of data block. KsfImage[] images = new KsfImage[imageCount]; diff --git a/src/library/Syroot.Worms.Mgame/Lpd.cs b/src/library/Syroot.Worms.Mgame/Lpd.cs index 6639160..95e4e22 100644 --- a/src/library/Syroot.Worms.Mgame/Lpd.cs +++ b/src/library/Syroot.Worms.Mgame/Lpd.cs @@ -2,13 +2,14 @@ using System.Drawing; using System.IO; using Syroot.BinaryData; +using Syroot.Worms.Core.IO; namespace Syroot.Worms.Mgame { /// /// Represents an LPD layout description file used in Worms World Party Aqua. /// - public class Lpd + public class Lpd : ILoadableFile { // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ @@ -17,20 +18,14 @@ namespace Syroot.Worms.Mgame /// . /// /// The name of the file to load the data from. - public Lpd(string fileName) - { - Load(fileName); - } + public Lpd(string fileName) => Load(fileName); /// /// Initializes a new instance of the class, loading data from the given /// . /// /// The to load the data from. - public Lpd(Stream stream) - { - Load(stream); - } + public Lpd(Stream stream) => Load(stream); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -47,8 +42,8 @@ namespace Syroot.Worms.Mgame /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// diff --git a/src/library/Syroot.Worms.Mgame/PacketCompression.cs b/src/library/Syroot.Worms.Mgame/PacketCompression.cs index 045c610..1469f3a 100644 --- a/src/library/Syroot.Worms.Mgame/PacketCompression.cs +++ b/src/library/Syroot.Worms.Mgame/PacketCompression.cs @@ -19,17 +19,12 @@ namespace Syroot.Worms.Mgame { // ---- FIELDS ------------------------------------------------------------------------------------------------- - private static int _numBytesTransferred = 0; - - private static int _field_4 = 4; private static int _field_8 = 0; - - private static int _field_C = 0; private static int _field_10 = 0; private static readonly int[] _bufferDwords = new int[512]; - private static byte[] _buffer = new byte[256]; + private static readonly byte[] _buffer = new byte[256]; private static int _bufferCursor = 0; // ---- METHODS (INTERNAL) ------------------------------------------------------------------------------------- @@ -102,7 +97,6 @@ namespace Syroot.Worms.Mgame compressor.Shift(shiftValue1, 8); compressor.Shift(idxDword - 18, 8); field_10 = _field_10; - ++_field_C; _field_10 = idxDword - 3 + field_10; } else @@ -110,7 +104,6 @@ namespace Syroot.Worms.Mgame compressor.Shift(idxDword - 2, 4); compressor.Shift(shiftValue1 - 1, 8); field_8 = _field_8; - ++_field_4; _field_8 = idxDword - 2 + field_8; } idx += idxDword; @@ -209,7 +202,6 @@ namespace Syroot.Worms.Mgame bufferCursor = _bufferCursor; if (bufferCursor != 0) { - _numBytesTransferred += bufferCursor; compressor.Compress(false); compressor.Shift(_bufferCursor - 1, 8); for (i = 0; i < _bufferCursor; ++i) diff --git a/src/library/Syroot.Worms.Mgame/Palette.cs b/src/library/Syroot.Worms.Mgame/Palette.cs index d53c91a..76063cb 100644 --- a/src/library/Syroot.Worms.Mgame/Palette.cs +++ b/src/library/Syroot.Worms.Mgame/Palette.cs @@ -54,8 +54,8 @@ namespace Syroot.Worms.Mgame /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// diff --git a/src/library/Syroot.Worms.Mgame/PasswordCrypto.cs b/src/library/Syroot.Worms.Mgame/PasswordCrypto.cs index 154d0c5..4e07a9d 100644 --- a/src/library/Syroot.Worms.Mgame/PasswordCrypto.cs +++ b/src/library/Syroot.Worms.Mgame/PasswordCrypto.cs @@ -37,29 +37,27 @@ namespace Syroot.Worms.Mgame /// The encrypted text. public static string Encrypt(string data, uint key) { - using (MemoryStream inStream = new MemoryStream(new byte[_bufferSize])) + using MemoryStream inStream = new MemoryStream(new byte[_bufferSize]); + + // Write input into a buffer. Required to loop over the input password end. + inStream.WriteString(data, StringCoding.ZeroTerminated, Encodings.Korean); + inStream.Position = 0; + using MemoryStream outStream = new MemoryStream(new byte[_bufferSize]); + + // Encrypt the contents character by character. + while (inStream.Position < data.Length) { - // Write input into a buffer. Required to loop over the input password end. - inStream.WriteString(data, StringCoding.ZeroTerminated, Encodings.Korean); - inStream.Position = 0; - using (MemoryStream outStream = new MemoryStream(new byte[_bufferSize])) + // Begin a new dword value at every 7th index. + uint dword = TransformDword(inStream.ReadUInt32() + key, true); + for (int j = 0; j < 7; j++) { - // Encrypt the contents character by character. - while (inStream.Position < data.Length) - { - // Begin a new dword value at every 7th index. - uint dword = TransformDword(inStream.ReadUInt32() + key, true); - for (int j = 0; j < 7; j++) - { - outStream.WriteByte((byte)_encryptCharacters[(int)(dword % _encryptCharacters.Length)]); - dword /= (uint)_encryptCharacters.Length; - } - } - // Return the encrypted password as a zero-terminated ASCII string. - outStream.Position = 0; - return outStream.ReadString(StringCoding.ZeroTerminated, Encodings.Korean); + outStream.WriteByte((byte)_encryptCharacters[(int)(dword % _encryptCharacters.Length)]); + dword /= (uint)_encryptCharacters.Length; } } + // Return the encrypted password as a zero-terminated ASCII string. + outStream.Position = 0; + return outStream.ReadString(StringCoding.ZeroTerminated, Encodings.Korean); } /// @@ -70,32 +68,30 @@ namespace Syroot.Worms.Mgame /// The decrypted text. public static string Decrypt(string data, uint key) { - using (MemoryStream inStream = new MemoryStream(new byte[_bufferSize])) + using MemoryStream inStream = new MemoryStream(new byte[_bufferSize]); + + // Write input into a buffer. Required to loop over the input password end. + inStream.WriteString(data, StringCoding.Raw, Encodings.Korean); + inStream.Position = 0; + using MemoryStream outStream = new MemoryStream(new byte[_bufferSize]); + + // Decrypt the contents character by character. + for (int i = 0; i < data.Length; i += 7) { - // Write input into a buffer. Required to loop over the input password end. - inStream.WriteString(data, StringCoding.Raw, Encodings.Korean); - inStream.Position = 0; - using (MemoryStream outStream = new MemoryStream(new byte[_bufferSize])) + uint dword = 0; + for (int j = 0; j < 7; j++) { - // Decrypt the contents character by character. - for (int i = 0; i < data.Length; i += 7) - { - uint dword = 0; - for (int j = 0; j < 7; j++) - { - byte b = inStream.Read1Byte(); - uint op1 = (uint)Math.Pow(_encryptCharacters.Length, j); - uint op2 = (uint)(b < '0' || b > '9' ? b - '7' : b - '0'); - dword += op1 * op2; - } - // Finalize a new dword value at every 7th index. - outStream.WriteUInt32(TransformDword(dword, false) - key); - } - // Return the decrypted password as a zero-terminated ASCII string. - outStream.Position = 0; - return outStream.ReadString(StringCoding.ZeroTerminated, Encodings.Korean); + byte b = inStream.Read1Byte(); + uint op1 = (uint)Math.Pow(_encryptCharacters.Length, j); + uint op2 = (uint)(b < '0' || b > '9' ? b - '7' : b - '0'); + dword += op1 * op2; } + // Finalize a new dword value at every 7th index. + outStream.WriteUInt32(TransformDword(dword, false) - key); } + // Return the decrypted password as a zero-terminated ASCII string. + outStream.Position = 0; + return outStream.ReadString(StringCoding.ZeroTerminated, Encodings.Korean); } // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms.Mgame/Syroot.Worms.Mgame.csproj b/src/library/Syroot.Worms.Mgame/Syroot.Worms.Mgame.csproj index 9d16b6a..9705f87 100644 --- a/src/library/Syroot.Worms.Mgame/Syroot.Worms.Mgame.csproj +++ b/src/library/Syroot.Worms.Mgame/Syroot.Worms.Mgame.csproj @@ -1,23 +1,23 @@  - - Syroot.Worms.Mgame - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Mgame Worms clients. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms.Mgame - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - + + Syroot.Worms.Mgame + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Mgame Worms clients. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms.Mgame + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0-alpha1 + + + + \ No newline at end of file diff --git a/src/library/Syroot.Worms.WorldParty/LandData.cs b/src/library/Syroot.Worms.WorldParty/LandData.cs index c20b8c9..e0bfd41 100644 --- a/src/library/Syroot.Worms.WorldParty/LandData.cs +++ b/src/library/Syroot.Worms.WorldParty/LandData.cs @@ -91,30 +91,29 @@ namespace Syroot.Worms.WorldParty /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadInt32() != _signature) - throw new InvalidDataException("Invalid LND file signature."); - int fileSize = reader.ReadInt32(); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the data. - Size = reader.ReadStruct(); - TopBorder = reader.ReadBoolean(BooleanCoding.Dword); - WaterHeight = reader.ReadInt32(); + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid LND file signature."); + int fileSize = reader.ReadInt32(); - // Read the possible object coordinate array. - ObjectLocations = reader.ReadStructs(reader.ReadInt32()); + // Read the data. + Size = reader.ReadStruct(); + TopBorder = reader.ReadBoolean(BooleanCoding.Dword); + WaterHeight = reader.ReadInt32(); - // Read the image data. - Foreground = new Img(stream, true); - CollisionMask = new Img(stream, true); - Background = new Img(stream, true); + // Read the possible object coordinate array. + ObjectLocations = reader.ReadStructs(reader.ReadInt32()); - // Read the file paths. - LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); - WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); - } + // Read the image data. + Foreground = new Img(stream, true); + CollisionMask = new Img(stream, true); + Background = new Img(stream, true); + + // Read the file paths. + LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); + WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); } /// @@ -123,8 +122,8 @@ namespace Syroot.Worms.WorldParty /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -133,32 +132,31 @@ namespace Syroot.Worms.WorldParty /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature); - uint fileSizeOffset = writer.ReserveOffset(); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the data. - writer.WriteStruct(Size); - writer.Write(TopBorder, BooleanCoding.Dword); - writer.Write(WaterHeight); + // Write the header. + writer.Write(_signature); + uint fileSizeOffset = writer.ReserveOffset(); - // Write the possible object coordinate array. - writer.Write(ObjectLocations.Length); - writer.WriteStructs(ObjectLocations); + // Write the data. + writer.WriteStruct(Size); + writer.Write(TopBorder, BooleanCoding.Dword); + writer.Write(WaterHeight); - // Write the image data. - Foreground.Save(writer.BaseStream, false, true); - CollisionMask.Save(writer.BaseStream, false, true); - Background.Save(writer.BaseStream, false, true); + // Write the possible object coordinate array. + writer.Write(ObjectLocations.Length); + writer.WriteStructs(ObjectLocations); - // Write the file paths. - writer.Write(LandTexturePath, StringCoding.ByteCharCount); - writer.Write(WaterDirPath, StringCoding.ByteCharCount); + // Write the image data. + Foreground.Save(writer.BaseStream, false, true); + CollisionMask.Save(writer.BaseStream, false, true); + Background.Save(writer.BaseStream, false, true); - writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); - } + // Write the file paths. + writer.Write(LandTexturePath, StringCoding.ByteCharCount); + writer.Write(WaterDirPath, StringCoding.ByteCharCount); + + writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); } /// @@ -167,8 +165,8 @@ namespace Syroot.Worms.WorldParty /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - Save(stream); + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.WorldParty/Syroot.Worms.WorldParty.csproj b/src/library/Syroot.Worms.WorldParty/Syroot.Worms.WorldParty.csproj index cddd487..f6136ec 100644 --- a/src/library/Syroot.Worms.WorldParty/Syroot.Worms.WorldParty.csproj +++ b/src/library/Syroot.Worms.WorldParty/Syroot.Worms.WorldParty.csproj @@ -1,23 +1,23 @@  - - Syroot.Worms.WorldParty - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Team17's Worms World Party. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms.WorldParty - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - + + Syroot.Worms.WorldParty + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Team17's Worms World Party. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms.WorldParty + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0-alpha1 + + + + \ No newline at end of file diff --git a/src/library/Syroot.Worms.WorldParty/Team.cs b/src/library/Syroot.Worms.WorldParty/Team.cs index ffad627..495e6cf 100644 --- a/src/library/Syroot.Worms.WorldParty/Team.cs +++ b/src/library/Syroot.Worms.WorldParty/Team.cs @@ -195,58 +195,57 @@ namespace Syroot.Worms.WorldParty /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + Name = reader.ReadString(17); + WormNames = reader.ReadStrings(8, 17); + CpuLevel = reader.Read1Byte(); + SoundBankName = reader.ReadString(0x20); + SoundBankLocation = reader.Read1Byte(); + FanfareName = reader.ReadString(0x20); + UseCustomFanfare = reader.Read1Byte(); + + GraveSprite = reader.ReadSByte(); + if (GraveSprite < 0) { - Name = reader.ReadString(17); - WormNames = reader.ReadStrings(8, 17); - CpuLevel = reader.Read1Byte(); - SoundBankName = reader.ReadString(0x20); - SoundBankLocation = reader.Read1Byte(); - FanfareName = reader.ReadString(0x20); - UseCustomFanfare = reader.Read1Byte(); - - GraveSprite = reader.ReadSByte(); - if (GraveSprite < 0) - { - GraveFileName = reader.ReadString(0x20); - Grave = new RawBitmap() - { - BitsPerPixel = 8, - Size = new Size(24, 32), - Palette = reader.ReadStructs(256), - Data = reader.ReadBytes(24 * 32) - }; - } - - TeamWeapon = reader.ReadEnum(true); - GamesLost = reader.ReadInt32(); - DeathmatchesLost = reader.ReadInt32(); - GamesWon = reader.ReadInt32(); - DeathmatchesWon = reader.ReadInt32(); - GamesDrawn = reader.ReadInt32(); - DeathmatchesDrawn = reader.ReadInt32(); - Kills = reader.ReadInt32(); - DeathmatchKills = reader.ReadInt32(); - Deaths = reader.ReadInt32(); - DeathmatchDeaths = reader.ReadInt32(); - MissionStatuses = reader.ReadStructs(_missionCount); - - FlagFileName = reader.ReadString(0x20); - Flag = new RawBitmap() + GraveFileName = reader.ReadString(0x20); + Grave = new RawBitmap() { BitsPerPixel = 8, - Size = new Size(20, 17), + Size = new Size(24, 32), Palette = reader.ReadStructs(256), - Data = reader.ReadBytes(20 * 17) + Data = reader.ReadBytes(24 * 32) }; - - Unknown1 = reader.Read1Byte(); - DeathmatchRank = reader.Read1Byte(); - TrainingMissionTimes = reader.ReadInt32s(_trainingMissionCount); - WeaponPoints = reader.ReadBytes(46); - Fort = reader.Read1Byte(); - Unknown2 = reader.ReadInt32s(7); } + + TeamWeapon = reader.ReadEnum(true); + GamesLost = reader.ReadInt32(); + DeathmatchesLost = reader.ReadInt32(); + GamesWon = reader.ReadInt32(); + DeathmatchesWon = reader.ReadInt32(); + GamesDrawn = reader.ReadInt32(); + DeathmatchesDrawn = reader.ReadInt32(); + Kills = reader.ReadInt32(); + DeathmatchKills = reader.ReadInt32(); + Deaths = reader.ReadInt32(); + DeathmatchDeaths = reader.ReadInt32(); + MissionStatuses = reader.ReadStructs(_missionCount); + + FlagFileName = reader.ReadString(0x20); + Flag = new RawBitmap() + { + BitsPerPixel = 8, + Size = new Size(20, 17), + Palette = reader.ReadStructs(256), + Data = reader.ReadBytes(20 * 17) + }; + + Unknown1 = reader.Read1Byte(); + DeathmatchRank = reader.Read1Byte(); + TrainingMissionTimes = reader.ReadInt32s(_trainingMissionCount); + WeaponPoints = reader.ReadBytes(46); + Fort = reader.Read1Byte(); + Unknown2 = reader.ReadInt32s(7); } /// @@ -255,49 +254,48 @@ namespace Syroot.Worms.WorldParty /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + writer.WriteString(Name, 17); + writer.WriteStrings(WormNames, 17); + writer.Write(CpuLevel); + writer.WriteString(SoundBankName, 0x20); + writer.Write(SoundBankLocation); + writer.WriteString(FanfareName, 0x20); + writer.Write(UseCustomFanfare); + + writer.Write(GraveSprite); + if (GraveSprite < 0) { - writer.WriteString(Name, 17); - writer.WriteStrings(WormNames, 17); - writer.Write(CpuLevel); - writer.WriteString(SoundBankName, 0x20); - writer.Write(SoundBankLocation); - writer.WriteString(FanfareName, 0x20); - writer.Write(UseCustomFanfare); - - writer.Write(GraveSprite); - if (GraveSprite < 0) - { - writer.WriteString(GraveFileName, 0x20); - writer.WriteStructs(Grave.Palette); - writer.WriteStructs(Grave.Data); - } - - writer.WriteEnum(TeamWeapon, true); - writer.Write(GamesLost); - writer.Write(DeathmatchesLost); - writer.Write(GamesWon); - writer.Write(DeathmatchesWon); - writer.Write(GamesDrawn); - writer.Write(DeathmatchesDrawn); - writer.Write(Kills); - writer.Write(DeathmatchKills); - writer.Write(Deaths); - writer.Write(DeathmatchDeaths); - writer.WriteStructs(MissionStatuses); - - writer.WriteString(FlagFileName, 0x20); - writer.WriteStructs(Flag.Palette); - writer.WriteStructs(Flag.Data); - - writer.Write(Unknown1); - writer.Write(DeathmatchRank); - writer.Write(TrainingMissionTimes); - writer.Write(UnknownTrainingMissionTime); - writer.WriteStructs(WeaponPoints); - writer.Write(Fort); - writer.Write(Unknown2); + writer.WriteString(GraveFileName, 0x20); + writer.WriteStructs(Grave.Palette); + writer.WriteStructs(Grave.Data); } + + writer.WriteEnum(TeamWeapon, true); + writer.Write(GamesLost); + writer.Write(DeathmatchesLost); + writer.Write(GamesWon); + writer.Write(DeathmatchesWon); + writer.Write(GamesDrawn); + writer.Write(DeathmatchesDrawn); + writer.Write(Kills); + writer.Write(DeathmatchKills); + writer.Write(Deaths); + writer.Write(DeathmatchDeaths); + writer.WriteStructs(MissionStatuses); + + writer.WriteString(FlagFileName, 0x20); + writer.WriteStructs(Flag.Palette); + writer.WriteStructs(Flag.Data); + + writer.Write(Unknown1); + writer.Write(DeathmatchRank); + writer.Write(TrainingMissionTimes); + writer.Write(UnknownTrainingMissionTime); + writer.WriteStructs(WeaponPoints); + writer.Write(Fort); + writer.Write(Unknown2); } } diff --git a/src/library/Syroot.Worms.WorldParty/TeamContainer.cs b/src/library/Syroot.Worms.WorldParty/TeamContainer.cs index c4bb4f9..ca74b0b 100644 --- a/src/library/Syroot.Worms.WorldParty/TeamContainer.cs +++ b/src/library/Syroot.Worms.WorldParty/TeamContainer.cs @@ -21,29 +21,20 @@ namespace Syroot.Worms.WorldParty /// /// Initializes a new instance of the class. /// - public TeamContainer() - { - Teams = new List(); - } + public TeamContainer() => Teams = new List(); /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public TeamContainer(Stream stream) - { - Load(stream); - } + public TeamContainer(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public TeamContainer(string fileName) - { - Load(fileName); - } + public TeamContainer(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -80,24 +71,21 @@ namespace Syroot.Worms.WorldParty /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) - { - throw new InvalidDataException("Invalid WWP file signature."); - } - Version = reader.Read1Byte(); // Really version? + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read global settings. - byte teamCount = reader.Read1Byte(); - Unknown1 = reader.Read1Byte(); - Unknown2 = reader.Read1Byte(); - Unknown3 = reader.ReadBytes(840); + // Read the header. + if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) + throw new InvalidDataException("Invalid WWP file signature."); + Version = reader.Read1Byte(); // Really version? - // Read the teams. - Teams = new List(reader.Load(teamCount)); - } + // Read global settings. + byte teamCount = reader.Read1Byte(); + Unknown1 = reader.Read1Byte(); + Unknown2 = reader.Read1Byte(); + Unknown3 = reader.ReadBytes(840); + + // Read the teams. + Teams = new List(reader.Load(teamCount)); } /// @@ -106,10 +94,8 @@ namespace Syroot.Worms.WorldParty /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -118,24 +104,21 @@ namespace Syroot.Worms.WorldParty /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature, StringCoding.ZeroTerminated); - writer.Write(Version); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write global settings. - writer.Write((byte)Teams.Count); - writer.Write(Unknown1); - writer.Write(Unknown2); - writer.WriteStructs(Unknown3); + // Write the header. + writer.Write(_signature, StringCoding.ZeroTerminated); + writer.Write(Version); - // Write the teams. - foreach (Team team in Teams) - { - team.Save(writer.BaseStream); - } - } + // Write global settings. + writer.Write((byte)Teams.Count); + writer.Write(Unknown1); + writer.Write(Unknown2); + writer.WriteStructs(Unknown3); + + // Write the teams. + foreach (Team team in Teams) + team.Save(writer.BaseStream); } /// @@ -144,10 +127,8 @@ namespace Syroot.Worms.WorldParty /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Worms2/LandData.cs b/src/library/Syroot.Worms.Worms2/LandData.cs index 88f8f66..9b693c7 100644 --- a/src/library/Syroot.Worms.Worms2/LandData.cs +++ b/src/library/Syroot.Worms.Worms2/LandData.cs @@ -96,31 +96,30 @@ namespace Syroot.Worms.Worms2 /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadInt32() != _signature) - throw new InvalidDataException("Invalid LND file signature."); - int fileSize = reader.ReadInt32(); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the data. - Size = reader.ReadStruct(); - TopBorder = reader.ReadBoolean(BooleanCoding.Dword); + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid LND file signature."); + int fileSize = reader.ReadInt32(); - // Read the possible object coordinate array. - ObjectLocations = reader.ReadStructs(reader.ReadInt32()); - Unknown = reader.ReadInt32(); + // Read the data. + Size = reader.ReadStruct(); + TopBorder = reader.ReadBoolean(BooleanCoding.Dword); - // Read the image data. - Foreground = reader.Load(); - CollisionMask = reader.Load(); - Background = reader.Load(); - UnknownImage = reader.Load(); + // Read the possible object coordinate array. + ObjectLocations = reader.ReadStructs(reader.ReadInt32()); + Unknown = reader.ReadInt32(); - // Read the file paths. - LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); - WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); - } + // Read the image data. + Foreground = reader.Load(); + CollisionMask = reader.Load(); + Background = reader.Load(); + UnknownImage = reader.Load(); + + // Read the file paths. + LandTexturePath = reader.ReadString(StringCoding.ByteCharCount); + WaterDirPath = reader.ReadString(StringCoding.ByteCharCount); } /// @@ -129,8 +128,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -139,33 +138,32 @@ namespace Syroot.Worms.Worms2 /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature); - uint fileSizeOffset = writer.ReserveOffset(); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the data. - writer.WriteStruct(Size); - writer.Write(TopBorder, BooleanCoding.Dword); + // Write the header. + writer.Write(_signature); + uint fileSizeOffset = writer.ReserveOffset(); - // Write the possible object coordinate array. - writer.Write(ObjectLocations.Length); - writer.WriteStructs(ObjectLocations); - writer.Write(Unknown); + // Write the data. + writer.WriteStruct(Size); + writer.Write(TopBorder, BooleanCoding.Dword); - // Write the image data. - Foreground.Save(writer.BaseStream); - CollisionMask.Save(writer.BaseStream); - Background.Save(writer.BaseStream); - UnknownImage.Save(writer.BaseStream); + // Write the possible object coordinate array. + writer.Write(ObjectLocations.Length); + writer.WriteStructs(ObjectLocations); + writer.Write(Unknown); - // Write the file paths. - writer.Write(LandTexturePath, StringCoding.ByteCharCount); - writer.Write(WaterDirPath, StringCoding.ByteCharCount); + // Write the image data. + Foreground.Save(writer.BaseStream); + CollisionMask.Save(writer.BaseStream); + Background.Save(writer.BaseStream); + UnknownImage.Save(writer.BaseStream); - writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); - } + // Write the file paths. + writer.Write(LandTexturePath, StringCoding.ByteCharCount); + writer.Write(WaterDirPath, StringCoding.ByteCharCount); + + writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); } /// @@ -174,8 +172,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - Save(stream); + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Worms2/SchemeEnums.cs b/src/library/Syroot.Worms.Worms2/SchemeEnums.cs index 16b26ad..ff99b83 100644 --- a/src/library/Syroot.Worms.Worms2/SchemeEnums.cs +++ b/src/library/Syroot.Worms.Worms2/SchemeEnums.cs @@ -26,7 +26,6 @@ namespace Syroot.Worms.Worms2 Manual = 3 } - /// /// Represents the weapons in the game. /// @@ -136,7 +135,7 @@ namespace Syroot.Worms.Worms2 /// The Napalm Strike weapon. /// NapalmStrike, - + /// /// The Mail Strike weapon. /// diff --git a/src/library/Syroot.Worms.Worms2/SchemeOptions.cs b/src/library/Syroot.Worms.Worms2/SchemeOptions.cs index 9fd80f2..f9f6c79 100644 --- a/src/library/Syroot.Worms.Worms2/SchemeOptions.cs +++ b/src/library/Syroot.Worms.Worms2/SchemeOptions.cs @@ -20,28 +20,20 @@ namespace Syroot.Worms.Worms2 /// /// Initializs a new instance of the class. /// - public SchemeOptions() - { - } + public SchemeOptions() { } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public SchemeOptions(Stream stream) - { - Load(stream); - } + public SchemeOptions(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public SchemeOptions(string fileName) - { - Load(fileName); - } + public SchemeOptions(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -221,52 +213,47 @@ namespace Syroot.Worms.Worms2 /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadString(_signature.Length) != _signature) - { - throw new InvalidDataException("Invalid OPT file signature."); - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the options. - RoundTime = reader.ReadInt32(); - TurnTime = reader.ReadInt32(); - RetreatTime = reader.ReadInt32(); - RetreatTimeRope = reader.ReadInt32(); - ObjectCount = reader.ReadInt32(); - MineDelay = reader.ReadInt32(); - DudMines = reader.ReadBoolean(BooleanCoding.Dword); - WindPower = reader.ReadInt32(); - Friction = reader.ReadInt32(); - ReplayRequiredKills = reader.ReadInt32(); - ReplayRequiredDamage = reader.ReadInt32(); - AutomaticReplays = reader.ReadBoolean(BooleanCoding.Dword); - FallDamage = reader.ReadInt32(); - RopeSwings = reader.ReadInt32(); - ShowRoundTime = reader.ReadBoolean(BooleanCoding.Dword); - WaterRiseRate = reader.ReadInt32(); - SuddenDeathHealthDrop = reader.ReadBoolean(BooleanCoding.Dword); - IndestructibleBorder = reader.ReadBoolean(BooleanCoding.Dword); - RestrictGirders = reader.ReadBoolean(BooleanCoding.Dword); - WormSelectMode = reader.ReadEnum(true); - ExtendedChatControls = reader.ReadBoolean(BooleanCoding.Dword); - HotSeatDelay = reader.ReadInt32(); - EnableStockpiling = reader.ReadBoolean(BooleanCoding.Dword); - CrateProbability = reader.ReadInt32(); - CrateIntelligence = reader.ReadInt32(); - HealthCrateEnergy = reader.ReadInt32(); - BoobyTraps = reader.ReadBoolean(BooleanCoding.Dword); - EnableSuperWeapons = reader.ReadBoolean(BooleanCoding.Dword); - WormEnergy = reader.ReadInt32(); - ArtilleryMode = reader.ReadBoolean(BooleanCoding.Dword); - SuddenDeathDisableWormSelect = reader.ReadBoolean(BooleanCoding.Dword); - // The following option does not exist in all schemes. - if (!reader.EndOfStream) - { - UseOilDrums = reader.ReadBoolean(BooleanCoding.Dword); - } - } + // Read the header. + if (reader.ReadString(_signature.Length) != _signature) + throw new InvalidDataException("Invalid OPT file signature."); + + // Read the options. + RoundTime = reader.ReadInt32(); + TurnTime = reader.ReadInt32(); + RetreatTime = reader.ReadInt32(); + RetreatTimeRope = reader.ReadInt32(); + ObjectCount = reader.ReadInt32(); + MineDelay = reader.ReadInt32(); + DudMines = reader.ReadBoolean(BooleanCoding.Dword); + WindPower = reader.ReadInt32(); + Friction = reader.ReadInt32(); + ReplayRequiredKills = reader.ReadInt32(); + ReplayRequiredDamage = reader.ReadInt32(); + AutomaticReplays = reader.ReadBoolean(BooleanCoding.Dword); + FallDamage = reader.ReadInt32(); + RopeSwings = reader.ReadInt32(); + ShowRoundTime = reader.ReadBoolean(BooleanCoding.Dword); + WaterRiseRate = reader.ReadInt32(); + SuddenDeathHealthDrop = reader.ReadBoolean(BooleanCoding.Dword); + IndestructibleBorder = reader.ReadBoolean(BooleanCoding.Dword); + RestrictGirders = reader.ReadBoolean(BooleanCoding.Dword); + WormSelectMode = reader.ReadEnum(true); + ExtendedChatControls = reader.ReadBoolean(BooleanCoding.Dword); + HotSeatDelay = reader.ReadInt32(); + EnableStockpiling = reader.ReadBoolean(BooleanCoding.Dword); + CrateProbability = reader.ReadInt32(); + CrateIntelligence = reader.ReadInt32(); + HealthCrateEnergy = reader.ReadInt32(); + BoobyTraps = reader.ReadBoolean(BooleanCoding.Dword); + EnableSuperWeapons = reader.ReadBoolean(BooleanCoding.Dword); + WormEnergy = reader.ReadInt32(); + ArtilleryMode = reader.ReadBoolean(BooleanCoding.Dword); + SuddenDeathDisableWormSelect = reader.ReadBoolean(BooleanCoding.Dword); + // The following option does not exist in all schemes. + if (!reader.EndOfStream) + UseOilDrums = reader.ReadBoolean(BooleanCoding.Dword); } /// @@ -275,10 +262,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -287,45 +272,44 @@ namespace Syroot.Worms.Worms2 /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.Write(_signature, StringCoding.Raw); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the options. - writer.Write(RoundTime); - writer.Write(TurnTime); - writer.Write(RetreatTime); - writer.Write(RetreatTimeRope); - writer.Write(ObjectCount); - writer.Write(MineDelay); - writer.Write(DudMines, BooleanCoding.Dword); - writer.Write(WindPower); - writer.Write(Friction); - writer.Write(ReplayRequiredKills); - writer.Write(ReplayRequiredDamage); - writer.Write(AutomaticReplays, BooleanCoding.Dword); - writer.Write(FallDamage); - writer.Write(RopeSwings); - writer.Write(ShowRoundTime, BooleanCoding.Dword); - writer.Write(WaterRiseRate); - writer.Write(SuddenDeathHealthDrop, BooleanCoding.Dword); - writer.Write(IndestructibleBorder, BooleanCoding.Dword); - writer.Write(RestrictGirders, BooleanCoding.Dword); - writer.WriteEnum(WormSelectMode, true); - writer.Write(ExtendedChatControls, BooleanCoding.Dword); - writer.Write(HotSeatDelay); - writer.Write(EnableStockpiling, BooleanCoding.Dword); - writer.Write(CrateProbability); - writer.Write(CrateIntelligence); - writer.Write(HealthCrateEnergy); - writer.Write(BoobyTraps, BooleanCoding.Dword); - writer.Write(EnableSuperWeapons, BooleanCoding.Dword); - writer.Write(WormEnergy); - writer.Write(ArtilleryMode, BooleanCoding.Dword); - writer.Write(SuddenDeathDisableWormSelect, BooleanCoding.Dword); - writer.Write(UseOilDrums, BooleanCoding.Dword); - } + // Write the header. + writer.Write(_signature, StringCoding.Raw); + + // Write the options. + writer.Write(RoundTime); + writer.Write(TurnTime); + writer.Write(RetreatTime); + writer.Write(RetreatTimeRope); + writer.Write(ObjectCount); + writer.Write(MineDelay); + writer.Write(DudMines, BooleanCoding.Dword); + writer.Write(WindPower); + writer.Write(Friction); + writer.Write(ReplayRequiredKills); + writer.Write(ReplayRequiredDamage); + writer.Write(AutomaticReplays, BooleanCoding.Dword); + writer.Write(FallDamage); + writer.Write(RopeSwings); + writer.Write(ShowRoundTime, BooleanCoding.Dword); + writer.Write(WaterRiseRate); + writer.Write(SuddenDeathHealthDrop, BooleanCoding.Dword); + writer.Write(IndestructibleBorder, BooleanCoding.Dword); + writer.Write(RestrictGirders, BooleanCoding.Dword); + writer.WriteEnum(WormSelectMode, true); + writer.Write(ExtendedChatControls, BooleanCoding.Dword); + writer.Write(HotSeatDelay); + writer.Write(EnableStockpiling, BooleanCoding.Dword); + writer.Write(CrateProbability); + writer.Write(CrateIntelligence); + writer.Write(HealthCrateEnergy); + writer.Write(BoobyTraps, BooleanCoding.Dword); + writer.Write(EnableSuperWeapons, BooleanCoding.Dword); + writer.Write(WormEnergy); + writer.Write(ArtilleryMode, BooleanCoding.Dword); + writer.Write(SuddenDeathDisableWormSelect, BooleanCoding.Dword); + writer.Write(UseOilDrums, BooleanCoding.Dword); } /// @@ -334,10 +318,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Worms2/SchemeWeapons.cs b/src/library/Syroot.Worms.Worms2/SchemeWeapons.cs index fd0d017..5c1d312 100644 --- a/src/library/Syroot.Worms.Worms2/SchemeWeapons.cs +++ b/src/library/Syroot.Worms.Worms2/SchemeWeapons.cs @@ -22,29 +22,20 @@ namespace Syroot.Worms.Worms2 /// /// Initializes a new instance of the class. /// - public SchemeWeapons() - { - Weapons = new SchemeWeaponSetting[_weaponCount]; - } + public SchemeWeapons() => Weapons = new SchemeWeaponSetting[_weaponCount]; /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public SchemeWeapons(Stream stream) - { - Load(stream); - } + public SchemeWeapons(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public SchemeWeapons(string fileName) - { - Load(fileName); - } + public SchemeWeapons(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -62,22 +53,17 @@ namespace Syroot.Worms.Worms2 /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - reader.Seek(_trashLength); - if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) - { - throw new InvalidDataException("Invalid WEP file signature."); - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the weapon settings. - Weapons = new SchemeWeaponSetting[_weaponCount]; - for (int i = 0; i < _weaponCount; i++) - { - Weapons[i] = reader.ReadStruct(); - } - } + // Read the header. + reader.Seek(_trashLength); + if (reader.ReadString(StringCoding.ZeroTerminated) != _signature) + throw new InvalidDataException("Invalid WEP file signature."); + + // Read the weapon settings. + Weapons = new SchemeWeaponSetting[_weaponCount]; + for (int i = 0; i < _weaponCount; i++) + Weapons[i] = reader.ReadStruct(); } /// @@ -86,10 +72,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -98,18 +82,15 @@ namespace Syroot.Worms.Worms2 /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - // Write the header. - writer.WriteStructs(new byte[_trashLength]); - writer.Write(_signature, StringCoding.ZeroTerminated); + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); - // Write the weapon settings. - foreach (SchemeWeaponSetting weapon in Weapons) - { - writer.WriteStruct(weapon); - } - } + // Write the header. + writer.WriteStructs(new byte[_trashLength]); + writer.Write(_signature, StringCoding.ZeroTerminated); + + // Write the weapon settings. + foreach (SchemeWeaponSetting weapon in Weapons) + writer.WriteStruct(weapon); } /// @@ -118,10 +99,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms.Worms2/Syroot.Worms.Worms2.csproj b/src/library/Syroot.Worms.Worms2/Syroot.Worms.Worms2.csproj index 3ccd725..7b5c55b 100644 --- a/src/library/Syroot.Worms.Worms2/Syroot.Worms.Worms2.csproj +++ b/src/library/Syroot.Worms.Worms2/Syroot.Worms.Worms2.csproj @@ -1,23 +1,23 @@  - - Syroot.Worms.Worms2 - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Team17's Worms 2. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms.Worms2 - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - + + Syroot.Worms.Worms2 + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Team17's Worms 2. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms.Worms2 + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0-alpha1 + + + + \ No newline at end of file diff --git a/src/library/Syroot.Worms.Worms2/Team.cs b/src/library/Syroot.Worms.Worms2/Team.cs index 83b0ff5..a13600f 100644 --- a/src/library/Syroot.Worms.Worms2/Team.cs +++ b/src/library/Syroot.Worms.Worms2/Team.cs @@ -109,50 +109,48 @@ namespace Syroot.Worms.Worms2 /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - Unknown1 = reader.ReadInt16(); - Name = reader.ReadString(66); - SoundBankName = reader.ReadString(36); - WormNames = reader.ReadStrings(8, 20); - Unknown2 = reader.ReadInt32(); - Unknown3 = reader.ReadInt32(); - Unknown4 = reader.ReadInt32(); - Unknown5 = reader.ReadInt32(); - Unknown6 = reader.ReadInt32(); - Unknown7 = reader.ReadInt32(); - Unknown8 = reader.ReadInt32(); - Unknown9 = reader.ReadInt32(); - Unknown10 = reader.ReadInt32(); - Unknown11 = reader.ReadInt32(); - Unknown12 = reader.ReadInt32(); - Unknown13 = reader.ReadInt32(); - Unknown14 = reader.ReadInt32(); - Unknown15 = reader.ReadInt32(); - Unknown16 = reader.ReadInt32(); - Unknown17 = reader.ReadInt32(); - Unknown18 = reader.ReadInt32(); - Unknown19 = reader.ReadInt32(); - Unknown20 = reader.ReadInt32(); - Unknown21 = reader.ReadInt32(); - Unknown22 = reader.ReadInt32(); - Unknown23 = reader.ReadInt32(); - Unknown24 = reader.ReadInt32(); - Unknown25 = reader.ReadInt32(); - GamesLost = reader.ReadInt32(); - GamesWon = reader.ReadInt32(); - Unknown26 = reader.ReadInt32(); - Unknown27 = reader.ReadInt32(); - Kills = reader.ReadInt32(); - Deaths = reader.ReadInt32(); - CpuLevel = reader.ReadInt32(); - Unknown28 = reader.ReadInt32(); - Unknown29 = reader.ReadInt32(); - Unknown30 = reader.ReadInt32(); - Difference = reader.ReadInt32(); - GamesPlayed = reader.ReadInt32(); - Points = reader.ReadInt32(); - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + Unknown1 = reader.ReadInt16(); + Name = reader.ReadString(66); + SoundBankName = reader.ReadString(36); + WormNames = reader.ReadStrings(8, 20); + Unknown2 = reader.ReadInt32(); + Unknown3 = reader.ReadInt32(); + Unknown4 = reader.ReadInt32(); + Unknown5 = reader.ReadInt32(); + Unknown6 = reader.ReadInt32(); + Unknown7 = reader.ReadInt32(); + Unknown8 = reader.ReadInt32(); + Unknown9 = reader.ReadInt32(); + Unknown10 = reader.ReadInt32(); + Unknown11 = reader.ReadInt32(); + Unknown12 = reader.ReadInt32(); + Unknown13 = reader.ReadInt32(); + Unknown14 = reader.ReadInt32(); + Unknown15 = reader.ReadInt32(); + Unknown16 = reader.ReadInt32(); + Unknown17 = reader.ReadInt32(); + Unknown18 = reader.ReadInt32(); + Unknown19 = reader.ReadInt32(); + Unknown20 = reader.ReadInt32(); + Unknown21 = reader.ReadInt32(); + Unknown22 = reader.ReadInt32(); + Unknown23 = reader.ReadInt32(); + Unknown24 = reader.ReadInt32(); + Unknown25 = reader.ReadInt32(); + GamesLost = reader.ReadInt32(); + GamesWon = reader.ReadInt32(); + Unknown26 = reader.ReadInt32(); + Unknown27 = reader.ReadInt32(); + Kills = reader.ReadInt32(); + Deaths = reader.ReadInt32(); + CpuLevel = reader.ReadInt32(); + Unknown28 = reader.ReadInt32(); + Unknown29 = reader.ReadInt32(); + Unknown30 = reader.ReadInt32(); + Difference = reader.ReadInt32(); + GamesPlayed = reader.ReadInt32(); + Points = reader.ReadInt32(); } /// @@ -161,52 +159,50 @@ namespace Syroot.Worms.Worms2 /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - writer.Write(Unknown1); - writer.WriteString(Name, 66); - writer.WriteString(SoundBankName, 36); - writer.WriteStrings(WormNames, 20); - writer.Write(Unknown2); - writer.Write(Unknown3); - writer.Write(Unknown4); - writer.Write(Unknown5); - writer.Write(Unknown6); - writer.Write(Unknown7); - writer.Write(Unknown8); - writer.Write(Unknown9); - writer.Write(Unknown10); - writer.Write(Unknown11); - writer.Write(Unknown12); - writer.Write(Unknown13); - writer.Write(Unknown14); - writer.Write(Unknown15); - writer.Write(Unknown16); - writer.Write(Unknown17); - writer.Write(Unknown18); - writer.Write(Unknown19); - writer.Write(Unknown20); - writer.Write(Unknown21); - writer.Write(Unknown22); - writer.Write(Unknown23); - writer.Write(Unknown24); - writer.Write(Unknown25); - writer.Write(GamesLost); - writer.Write(GamesWon); - writer.Write(Unknown26); - writer.Write(Unknown27); - writer.Write(Kills); - writer.Write(Deaths); - writer.Write(CpuLevel); - writer.Write(Unknown28); - writer.Write(Unknown29); - writer.Write(Unknown30); - writer.Write(Kills); - writer.Write(Deaths); - writer.Write(Difference); - writer.Write(GamesPlayed); - writer.Write(Points); - } + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + writer.Write(Unknown1); + writer.WriteString(Name, 66); + writer.WriteString(SoundBankName, 36); + writer.WriteStrings(WormNames, 20); + writer.Write(Unknown2); + writer.Write(Unknown3); + writer.Write(Unknown4); + writer.Write(Unknown5); + writer.Write(Unknown6); + writer.Write(Unknown7); + writer.Write(Unknown8); + writer.Write(Unknown9); + writer.Write(Unknown10); + writer.Write(Unknown11); + writer.Write(Unknown12); + writer.Write(Unknown13); + writer.Write(Unknown14); + writer.Write(Unknown15); + writer.Write(Unknown16); + writer.Write(Unknown17); + writer.Write(Unknown18); + writer.Write(Unknown19); + writer.Write(Unknown20); + writer.Write(Unknown21); + writer.Write(Unknown22); + writer.Write(Unknown23); + writer.Write(Unknown24); + writer.Write(Unknown25); + writer.Write(GamesLost); + writer.Write(GamesWon); + writer.Write(Unknown26); + writer.Write(Unknown27); + writer.Write(Kills); + writer.Write(Deaths); + writer.Write(CpuLevel); + writer.Write(Unknown28); + writer.Write(Unknown29); + writer.Write(Unknown30); + writer.Write(Kills); + writer.Write(Deaths); + writer.Write(Difference); + writer.Write(GamesPlayed); + writer.Write(Points); } } } diff --git a/src/library/Syroot.Worms.Worms2/TeamContainer.cs b/src/library/Syroot.Worms.Worms2/TeamContainer.cs index cff4e14..5f1a12d 100644 --- a/src/library/Syroot.Worms.Worms2/TeamContainer.cs +++ b/src/library/Syroot.Worms.Worms2/TeamContainer.cs @@ -17,29 +17,20 @@ namespace Syroot.Worms.Worms2 /// /// Initializes a new instance of the class. /// - public TeamContainer() - { - Teams = new List(); - } + public TeamContainer() => Teams = new List(); /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public TeamContainer(Stream stream) - { - Load(stream); - } + public TeamContainer(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public TeamContainer(string fileName) - { - Load(fileName); - } + public TeamContainer(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -56,14 +47,10 @@ namespace Syroot.Worms.Worms2 /// The to load the data from. public void Load(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - Teams = new List(); - while (!reader.EndOfStream) - { - Teams.Add(reader.Load()); - } - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + Teams = new List(); + while (!reader.EndOfStream) + Teams.Add(reader.Load()); } /// @@ -72,10 +59,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -84,13 +69,9 @@ namespace Syroot.Worms.Worms2 /// The to save the data to. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) - { - foreach (Team team in Teams) - { - team.Save(writer.BaseStream); - } - } + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + foreach (Team team in Teams) + team.Save(writer.BaseStream); } /// @@ -99,10 +80,8 @@ namespace Syroot.Worms.Worms2 /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } } } diff --git a/src/library/Syroot.Worms/Archive.cs b/src/library/Syroot.Worms/Archive.cs index 55fa268..6ce3d65 100644 --- a/src/library/Syroot.Worms/Archive.cs +++ b/src/library/Syroot.Worms/Archive.cs @@ -28,28 +28,20 @@ namespace Syroot.Worms /// /// Initializes a new instance of the class. /// - public Archive() - { - } + public Archive() { } /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public Archive(Stream stream) - { - Load(stream); - } + public Archive(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public Archive(string fileName) - { - Load(fileName); - } + public Archive(string fileName) => Load(fileName); // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- @@ -60,49 +52,40 @@ namespace Syroot.Worms public void Load(Stream stream) { if (!stream.CanSeek) - { throw new ArgumentException("Stream requires to be seekable.", nameof(stream)); - } Clear(); - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the header. - if (reader.ReadInt32() != _signature) - { - throw new InvalidDataException("Invalid DIR file signature."); - } - int fileSize = reader.ReadInt32(); - int tocOffset = reader.ReadInt32(); + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the table of contents. - reader.Position = tocOffset; - int tocSignature = reader.ReadInt32(); - if (tocSignature != _tocSignature) + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid DIR file signature."); + int fileSize = reader.ReadInt32(); + int tocOffset = reader.ReadInt32(); + + // Read the table of contents. + reader.Position = tocOffset; + int tocSignature = reader.ReadInt32(); + if (tocSignature != _tocSignature) + throw new InvalidDataException("Invalid DIR table of contents signature."); + // Generate a data dictionary out of the hash table and file entries. + int[] hashTable = reader.ReadInt32s(_hashSize); + foreach (int entryOffset in hashTable) + { + // If the hash is not 0, it points to a list of files which have a hash being the hash table index. + if (entryOffset > 0) { - throw new InvalidDataException("Invalid DIR table of contents signature."); - } - // Generate a data dictionary out of the hash table and file entries. - int[] hashTable = reader.ReadInt32s(_hashSize); - foreach (int entryOffset in hashTable) - { - // If the hash is not 0, it points to a list of files which have a hash being the hash table index. - if (entryOffset > 0) + int nextEntryOffset = entryOffset; + do { - int nextEntryOffset = entryOffset; - do - { - reader.Position = tocOffset + nextEntryOffset; - nextEntryOffset = reader.ReadInt32(); - int offset = reader.ReadInt32(); - int length = reader.ReadInt32(); - string name = reader.ReadString(StringCoding.ZeroTerminated); - using (reader.TemporarySeek(offset, SeekOrigin.Begin)) - { - Add(name, reader.ReadBytes(length)); - } - } while (nextEntryOffset != 0); - } + reader.Position = tocOffset + nextEntryOffset; + nextEntryOffset = reader.ReadInt32(); + int offset = reader.ReadInt32(); + int length = reader.ReadInt32(); + string name = reader.ReadString(StringCoding.ZeroTerminated); + using (reader.TemporarySeek(offset, SeekOrigin.Begin)) + Add(name, reader.ReadBytes(length)); + } while (nextEntryOffset != 0); } } } @@ -113,10 +96,8 @@ namespace Syroot.Worms /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -125,71 +106,66 @@ namespace Syroot.Worms /// The to save the data in. public void Save(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream)) + using BinaryStream writer = new BinaryStream(stream); + + // Write the header. + writer.Write(_signature); + uint fileSizeOffset = writer.ReserveOffset(); + uint tocOffset = writer.ReserveOffset(); + + // Write the data and build the hash table and file entries. + List[] hashTable = new List[_hashSize]; + foreach (KeyValuePair item in this) { - // Write the header. - writer.Write(_signature); - uint fileSizeOffset = writer.ReserveOffset(); - uint tocOffset = writer.ReserveOffset(); - - // Write the data and build the hash table and file entries. - List[] hashTable = new List[_hashSize]; - foreach (KeyValuePair item in this) + HashTableEntry entry = new HashTableEntry() { - HashTableEntry entry = new HashTableEntry() - { - Name = item.Key, - Offset = (int)writer.Position, - Length = item.Value.Length - }; - writer.WriteStructs(item.Value); + Name = item.Key, + Offset = (int)writer.Position, + Length = item.Value.Length + }; + writer.WriteStructs(item.Value); - int hash = CalculateHash(item.Key); - if (hashTable[hash] == null) - { - hashTable[hash] = new List(); - } - hashTable[hash].Add(entry); - } - - // Write the hash table and file entries. - int tocStart = (int)writer.Position; - int fileEntryOffset = sizeof(int) + _hashSize * sizeof(int); - writer.SatisfyOffset(tocOffset, tocStart); - writer.Write(_tocSignature); - for (int i = 0; i < _hashSize; i++) - { - List entries = hashTable[i]; - if (entries == null) - { - writer.Write(0); - } - else - { - // Write the entries resolving to the current hash. - writer.Write(fileEntryOffset); - using (writer.TemporarySeek(tocStart + fileEntryOffset, SeekOrigin.Begin)) - { - for (int j = 0; j < entries.Count; j++) - { - HashTableEntry entry = entries[j]; - uint nextEntryOffset = writer.ReserveOffset(); - writer.Write(entry.Offset); - writer.Write(entry.Length); - writer.Write(entry.Name, StringCoding.ZeroTerminated); - writer.Align(4); - if (j < entries.Count - 1) - { - writer.SatisfyOffset(nextEntryOffset, (int)writer.Position - tocStart); - } - } - fileEntryOffset = (int)writer.Position - tocStart; - } - } - } - - writer.SatisfyOffset(fileSizeOffset, tocStart + fileEntryOffset - 1); + int hash = CalculateHash(item.Key); + if (hashTable[hash] == null) + hashTable[hash] = new List(); + hashTable[hash].Add(entry); } + + // Write the hash table and file entries. + int tocStart = (int)writer.Position; + int fileEntryOffset = sizeof(int) + _hashSize * sizeof(int); + writer.SatisfyOffset(tocOffset, tocStart); + writer.Write(_tocSignature); + for (int i = 0; i < _hashSize; i++) + { + List entries = hashTable[i]; + if (entries == null) + { + writer.Write(0); + } + else + { + // Write the entries resolving to the current hash. + writer.Write(fileEntryOffset); + using (writer.TemporarySeek(tocStart + fileEntryOffset, SeekOrigin.Begin)) + { + for (int j = 0; j < entries.Count; j++) + { + HashTableEntry entry = entries[j]; + uint nextEntryOffset = writer.ReserveOffset(); + writer.Write(entry.Offset); + writer.Write(entry.Length); + writer.Write(entry.Name, StringCoding.ZeroTerminated); + writer.Align(4); + if (j < entries.Count - 1) + writer.SatisfyOffset(nextEntryOffset, (int)writer.Position - tocStart); + } + fileEntryOffset = (int)writer.Position - tocStart; + } + } + } + + writer.SatisfyOffset(fileSizeOffset, tocStart + fileEntryOffset - 1); } /// @@ -198,10 +174,8 @@ namespace Syroot.Worms /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms/Core/Algebra.cs b/src/library/Syroot.Worms/Core/Algebra.cs index 38e779d..722d821 100644 --- a/src/library/Syroot.Worms/Core/Algebra.cs +++ b/src/library/Syroot.Worms/Core/Algebra.cs @@ -5,6 +5,8 @@ /// public static class Algebra { + // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- + /// /// Gets the nearest, bigger of the given . /// diff --git a/src/library/Syroot.Worms/Core/ByteExtensions.cs b/src/library/Syroot.Worms/Core/ByteExtensions.cs index 1ef38c9..eb6de9c 100644 --- a/src/library/Syroot.Worms/Core/ByteExtensions.cs +++ b/src/library/Syroot.Worms/Core/ByteExtensions.cs @@ -65,9 +65,7 @@ namespace Syroot.Worms.Core /// true to enable the bit; otherwise false. /// The current byte with the bit enabled or disabled. public static byte SetBit(this byte self, int index, bool enable) - { - return enable ? EnableBit(self, index) : DisableBit(self, index); - } + => enable ? EnableBit(self, index) : DisableBit(self, index); /// /// Returns the current byte with the bit at the enabled when it is disabled or @@ -77,9 +75,7 @@ namespace Syroot.Worms.Core /// The 0-based index of the bit to toggle. /// The current byte with the bit toggled. public static byte ToggleBit(this byte self, int index) - { - return GetBit(self, index) ? DisableBit(self, index) : EnableBit(self, index); - } + => GetBit(self, index) ? DisableBit(self, index) : EnableBit(self, index); /// /// Returns an instance represented by the given number of . @@ -100,10 +96,7 @@ namespace Syroot.Worms.Core /// The first bit of the encoded value. /// The decoded . public static byte DecodeByte(this byte self, int bits, int firstBit) - { - // Shift to the first bit and keep only the required bits. - return (byte)((self >> firstBit) & ((1 << bits) - 1)); - } + => (byte)((self >> firstBit) & ((1 << bits) - 1)); // shift to the first bit and keep only required bits /// /// Returns an instance represented by the given number of . diff --git a/src/library/Syroot.Worms/Core/DisposableGCHandle.cs b/src/library/Syroot.Worms/Core/DisposableGCHandle.cs index 928217d..922e65f 100644 --- a/src/library/Syroot.Worms/Core/DisposableGCHandle.cs +++ b/src/library/Syroot.Worms/Core/DisposableGCHandle.cs @@ -15,10 +15,7 @@ namespace Syroot.Worms.Core // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ - public DisposableGCHandle(object value, GCHandleType type) - { - _handle = GCHandle.Alloc(value, GCHandleType.Pinned); - } + public DisposableGCHandle(object value, GCHandleType type) => _handle = GCHandle.Alloc(value, type); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -26,26 +23,22 @@ namespace Syroot.Worms.Core // ---- METHODS (PUBLIC) --------------------------------------------------------------------------------------- - public void Dispose() - { - // Do not change this code. Put cleanup code in Dispose(bool disposing). - Dispose(true); - } + public void Dispose() => Dispose(true); // ---- METHODS (PROTECTED) ------------------------------------------------------------------------------------ protected virtual void Dispose(bool disposing) { - if (!_disposed) - { - if (disposing) - { - if (_handle.IsAllocated) - _handle.Free(); - } + if (_disposed) + return; - _disposed = true; + if (disposing) + { + if (_handle.IsAllocated) + _handle.Free(); } + + _disposed = true; } } } diff --git a/src/library/Syroot.Worms/Core/Graphics/BitmapTools.cs b/src/library/Syroot.Worms/Core/Graphics/BitmapTools.cs index 041425e..9b0ca94 100644 --- a/src/library/Syroot.Worms/Core/Graphics/BitmapTools.cs +++ b/src/library/Syroot.Worms/Core/Graphics/BitmapTools.cs @@ -22,24 +22,23 @@ namespace Syroot.Worms.Core.Graphics /// The instance. public static Bitmap CreateIndexed(Size size, IList palette, byte[] data) { - using (DisposableGCHandle dataPin = new DisposableGCHandle(data, GCHandleType.Pinned)) - { - // Transfer the pixel data, respecting power-of-2 strides. - Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format8bppIndexed); - BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), - ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); - for (int y = 0; y < size.Height; y++) - Marshal.Copy(data, y * size.Width, bitmapData.Scan0 + y * bitmapData.Stride, size.Width); - bitmap.UnlockBits(bitmapData); + using DisposableGCHandle dataPin = new DisposableGCHandle(data, GCHandleType.Pinned); - // Transfer the palette. - ColorPalette bitmapPalette = bitmap.Palette; - for (int i = 0; i < palette.Count; i++) - bitmapPalette.Entries[i] = palette[i]; - bitmap.Palette = bitmapPalette; + // Transfer the pixel data, respecting power-of-2 strides. + Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format8bppIndexed); + BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, size.Width, size.Height), + ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); + for (int y = 0; y < size.Height; y++) + Marshal.Copy(data, y * size.Width, bitmapData.Scan0 + y * bitmapData.Stride, size.Width); + bitmap.UnlockBits(bitmapData); - return bitmap; - } + // Transfer the palette. + ColorPalette bitmapPalette = bitmap.Palette; + for (int i = 0; i < palette.Count; i++) + bitmapPalette.Entries[i] = palette[i]; + bitmap.Palette = bitmapPalette; + + return bitmap; } } } diff --git a/src/library/Syroot.Worms/Core/Graphics/RawBitmap.cs b/src/library/Syroot.Worms/Core/Graphics/RawBitmap.cs index 0747852..d33c483 100644 --- a/src/library/Syroot.Worms/Core/Graphics/RawBitmap.cs +++ b/src/library/Syroot.Worms/Core/Graphics/RawBitmap.cs @@ -41,24 +41,23 @@ namespace Syroot.Worms /// The created from the raw data. public Bitmap ToBitmap() { - using (DisposableGCHandle dataPin = new DisposableGCHandle(Data, GCHandleType.Pinned)) - { - // Transfer the pixel data, respecting power-of-2 strides. - Bitmap bitmap = new Bitmap(Size.Width, Size.Height, PixelFormat.Format8bppIndexed); - BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, Size.Width, Size.Height), - ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); - for (int y = 0; y < Size.Height; y++) - Marshal.Copy(Data, y * Size.Width, bitmapData.Scan0 + y * bitmapData.Stride, Size.Width); - bitmap.UnlockBits(bitmapData); + using DisposableGCHandle dataPin = new DisposableGCHandle(Data, GCHandleType.Pinned); - // Transfer the palette. - ColorPalette bitmapPalette = bitmap.Palette; - for (int i = 0; i < Palette.Count; i++) - bitmapPalette.Entries[i] = Palette[i]; - bitmap.Palette = bitmapPalette; + // Transfer the pixel data, respecting power-of-2 strides. + Bitmap bitmap = new Bitmap(Size.Width, Size.Height, PixelFormat.Format8bppIndexed); + BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, Size.Width, Size.Height), + ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); + for (int y = 0; y < Size.Height; y++) + Marshal.Copy(Data, y * Size.Width, bitmapData.Scan0 + y * bitmapData.Stride, Size.Width); + bitmap.UnlockBits(bitmapData); - return bitmap; - } + // Transfer the palette. + ColorPalette bitmapPalette = bitmap.Palette; + for (int i = 0; i < Palette.Count; i++) + bitmapPalette.Entries[i] = Palette[i]; + bitmap.Palette = bitmapPalette; + + return bitmap; } } } diff --git a/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs b/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs index 88a7f16..d66abb7 100644 --- a/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs +++ b/src/library/Syroot.Worms/Core/IO/StreamExtensions.cs @@ -87,11 +87,9 @@ namespace Syroot.Worms.Core.IO byte[] bytes = self.ReadBytes(Marshal.SizeOf()); // Convert them to a structure instance and return it. - using (DisposableGCHandle handle = new DisposableGCHandle(bytes, GCHandleType.Pinned)) - { - T instance = Marshal.PtrToStructure(handle.AddrOfPinnedObject); - return instance; - } + using DisposableGCHandle handle = new DisposableGCHandle(bytes, GCHandleType.Pinned); + T instance = Marshal.PtrToStructure(handle.AddrOfPinnedObject); + return instance; } /// diff --git a/src/library/Syroot.Worms/Core/Riff/RiffChunkLoadAttribute.cs b/src/library/Syroot.Worms/Core/Riff/RiffChunkLoadAttribute.cs index 9521c30..55764e2 100644 --- a/src/library/Syroot.Worms/Core/Riff/RiffChunkLoadAttribute.cs +++ b/src/library/Syroot.Worms/Core/Riff/RiffChunkLoadAttribute.cs @@ -16,10 +16,7 @@ namespace Syroot.Worms.Core.Riff /// . /// /// The chunk identifier required to invoke this method for loading it. - internal RiffChunkLoadAttribute(string identifier) - { - Identifier = identifier; - } + internal RiffChunkLoadAttribute(string identifier) => Identifier = identifier; // ---- PROPERTIES --------------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms/Core/Riff/RiffChunkSaveAttribute.cs b/src/library/Syroot.Worms/Core/Riff/RiffChunkSaveAttribute.cs index cc7f528..80ccb9b 100644 --- a/src/library/Syroot.Worms/Core/Riff/RiffChunkSaveAttribute.cs +++ b/src/library/Syroot.Worms/Core/Riff/RiffChunkSaveAttribute.cs @@ -16,10 +16,7 @@ namespace Syroot.Worms.Core.Riff /// . /// /// The chunk identifier saved in the file. - internal RiffChunkSaveAttribute(string identifier) - { - Identifier = identifier; - } + internal RiffChunkSaveAttribute(string identifier) => Identifier = identifier; // ---- PROPERTIES --------------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms/Core/Riff/RiffFile.cs b/src/library/Syroot.Worms/Core/Riff/RiffFile.cs index 92c45eb..cfd7a8a 100644 --- a/src/library/Syroot.Worms/Core/Riff/RiffFile.cs +++ b/src/library/Syroot.Worms/Core/Riff/RiffFile.cs @@ -21,17 +21,14 @@ namespace Syroot.Worms.Core.Riff private static readonly Dictionary _typeDataCache = new Dictionary(); - private TypeData _typeData; + private readonly TypeData _typeData; // ---- CONSTRUCTORS & DESTRUCTOR ------------------------------------------------------------------------------ /// /// Initializes a new instance of the class. /// - protected RiffFile() - { - _typeData = GetTypeData(); - } + protected RiffFile() => _typeData = GetTypeData(); // ---- METHODS (PROTECTED) ------------------------------------------------------------------------------------ @@ -41,35 +38,26 @@ namespace Syroot.Worms.Core.Riff /// The to load the RIFF data from. protected void LoadRiff(Stream stream) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) - { - // Read the file header. - if (reader.ReadString(_signature.Length) != _signature) - { - throw new InvalidDataException("Invalid RIFF file signature."); - } - int fileSize = reader.ReadInt32(); - string fileIdentifier = reader.ReadString(4); - if (fileIdentifier != _typeData.FileIdentifier) - { - throw new InvalidDataException("Invalid RIFF file identifier."); - } + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); - // Read the chunks. - while (!reader.EndOfStream) - { - string chunkIdentifier = reader.ReadString(4); - int chunkLength = reader.ReadInt32(); - // Invoke a loader method if matching the identifier or skip the chunk. - if (_typeData.ChunkLoaders.TryGetValue(chunkIdentifier, out MethodInfo loader)) - { - loader.Invoke(this, new object[] { reader, chunkLength }); - } - else - { - reader.Seek(chunkLength); - } - } + // Read the file header. + if (reader.ReadString(_signature.Length) != _signature) + throw new InvalidDataException("Invalid RIFF file signature."); + int fileSize = reader.ReadInt32(); + string fileIdentifier = reader.ReadString(4); + if (fileIdentifier != _typeData.FileIdentifier) + throw new InvalidDataException("Invalid RIFF file identifier."); + + // Read the chunks. + while (!reader.EndOfStream) + { + string chunkIdentifier = reader.ReadString(4); + int chunkLength = reader.ReadInt32(); + // Invoke a loader method if matching the identifier or skip the chunk. + if (_typeData.ChunkLoaders.TryGetValue(chunkIdentifier, out MethodInfo loader)) + loader.Invoke(this, new object[] { reader, chunkLength }); + else + reader.Seek(chunkLength); } } @@ -79,26 +67,25 @@ namespace Syroot.Worms.Core.Riff /// The to save the RIFF data in. protected void SaveRiff(Stream stream) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + + // Write the header. + writer.Write(_signature, StringCoding.Raw); + uint fileSizeOffset = writer.ReserveOffset(); + writer.Write(_typeData.FileIdentifier, StringCoding.Raw); + + // Write the chunks. + foreach (KeyValuePair chunkSaver in _typeData.ChunkSavers) { - // Write the header. - writer.Write(_signature, StringCoding.Raw); - uint fileSizeOffset = writer.ReserveOffset(); - writer.Write(_typeData.FileIdentifier, StringCoding.Raw); + writer.Write(chunkSaver.Key, StringCoding.Raw); + uint chunkSizeOffset = writer.ReserveOffset(); - // Write the chunks. - foreach (KeyValuePair chunkSaver in _typeData.ChunkSavers) - { - writer.Write(chunkSaver.Key, StringCoding.Raw); - uint chunkSizeOffset = writer.ReserveOffset(); + chunkSaver.Value.Invoke(this, new object[] { writer }); - chunkSaver.Value.Invoke(this, new object[] { writer }); - - writer.SatisfyOffset(chunkSizeOffset, (int)(writer.Position - chunkSizeOffset - 4)); - } - - writer.SatisfyOffset(fileSizeOffset, (int)(writer.Position - 8)); + writer.SatisfyOffset(chunkSizeOffset, (int)(writer.Position - chunkSizeOffset - 4)); } + + writer.SatisfyOffset(fileSizeOffset, (int)(writer.Position - 8)); } // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- @@ -135,11 +122,8 @@ namespace Syroot.Worms.Core.Riff if (saveAttribute != null) { ParameterInfo[] parameters = method.GetParameters(); - if (parameters.Length == 1 - && parameters[0].ParameterType == typeof(BinaryStream)) - { + if (parameters.Length == 1 && parameters[0].ParameterType == typeof(BinaryStream)) typeData.ChunkSavers.Add(saveAttribute.Identifier, method); - } continue; } } diff --git a/src/library/Syroot.Worms/Core/Riff/RiffFileAttribute.cs b/src/library/Syroot.Worms/Core/Riff/RiffFileAttribute.cs index 33897f9..e96aaf7 100644 --- a/src/library/Syroot.Worms/Core/Riff/RiffFileAttribute.cs +++ b/src/library/Syroot.Worms/Core/Riff/RiffFileAttribute.cs @@ -15,10 +15,7 @@ namespace Syroot.Worms.Core.Riff /// . /// /// The file identifier in the RIFF file header which will be validated. - internal RiffFileAttribute(string identifier) - { - Identifier = identifier; - } + internal RiffFileAttribute(string identifier) => Identifier = identifier; // ---- PROPERTIES --------------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms/Core/Team17Compression.cs b/src/library/Syroot.Worms/Core/Team17Compression.cs index ec17a9c..63d7a19 100644 --- a/src/library/Syroot.Worms/Core/Team17Compression.cs +++ b/src/library/Syroot.Worms/Core/Team17Compression.cs @@ -18,9 +18,7 @@ namespace Syroot.Worms.Core /// The data to compress. /// The compressed data. internal static byte[] Compress(byte[] bytes) - { - throw new NotImplementedException("Compressing data has not been implemented yet."); - } + => throw new NotImplementedException("Compressing data has not been implemented yet."); /// /// Decompresses the data available in the given into the provided diff --git a/src/library/Syroot.Worms/Img.cs b/src/library/Syroot.Worms/Img.cs index ea2645d..d6b9b7d 100644 --- a/src/library/Syroot.Worms/Img.cs +++ b/src/library/Syroot.Worms/Img.cs @@ -67,8 +67,8 @@ namespace Syroot.Worms /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// @@ -78,61 +78,60 @@ namespace Syroot.Worms /// true to align the data array by 4 bytes. public void Load(Stream stream, bool alignData) { - using (BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true)) + using BinaryStream reader = new BinaryStream(stream, encoding: Encoding.ASCII, leaveOpen: true); + + // Read the header. + if (reader.ReadInt32() != _signature) + throw new InvalidDataException("Invalid IMG file signature."); + int fileSize = reader.ReadInt32(); + + // Read an optional string describing the image contents and the bits per pixel. + BitsPerPixel = reader.Read1Byte(); + if (BitsPerPixel == 0) { - // Read the header. - if (reader.ReadInt32() != _signature) - throw new InvalidDataException("Invalid IMG file signature."); - int fileSize = reader.ReadInt32(); - - // Read an optional string describing the image contents and the bits per pixel. + Description = String.Empty; BitsPerPixel = reader.Read1Byte(); - if (BitsPerPixel == 0) - { - Description = String.Empty; - BitsPerPixel = reader.Read1Byte(); - } - else if (BitsPerPixel > 32) - { - Description = (char)BitsPerPixel + reader.ReadString(StringCoding.ZeroTerminated); - BitsPerPixel = reader.Read1Byte(); - } - else - { - Description = null; - } - - // Read image flags describing the format and availability of the following contents. - Flags flags = (Flags)reader.ReadByte(); - - // Read the image palette if available. The first color of the palette is implicitly black. - if (flags.HasFlag(Flags.Palettized)) - { - int colorCount = reader.ReadInt16(); - Palette = new Color[colorCount + 1]; - Palette[0] = Color.Black; - for (int i = 1; i <= colorCount; i++) - Palette[i] = Color.FromArgb(reader.Read1Byte(), reader.Read1Byte(), reader.Read1Byte()); - } - else - { - Palette = null; - } - - // Read the image size. - Size = new Size(reader.ReadInt16(), reader.ReadInt16()); - - // Read the data byte array, which might be compressed or aligned. - if (alignData) - reader.Align(4); - byte[] data = new byte[Size.Width * Size.Height * BitsPerPixel / 8]; - if (flags.HasFlag(Flags.Compressed)) - Team17Compression.Decompress(reader.BaseStream, data); - else - data = reader.ReadBytes(data.Length); - - Data = data; } + else if (BitsPerPixel > 32) + { + Description = (char)BitsPerPixel + reader.ReadString(StringCoding.ZeroTerminated); + BitsPerPixel = reader.Read1Byte(); + } + else + { + Description = null; + } + + // Read image flags describing the format and availability of the following contents. + Flags flags = (Flags)reader.ReadByte(); + + // Read the image palette if available. The first color of the palette is implicitly black. + if (flags.HasFlag(Flags.Palettized)) + { + int colorCount = reader.ReadInt16(); + Palette = new Color[colorCount + 1]; + Palette[0] = Color.Black; + for (int i = 1; i <= colorCount; i++) + Palette[i] = Color.FromArgb(reader.Read1Byte(), reader.Read1Byte(), reader.Read1Byte()); + } + else + { + Palette = null; + } + + // Read the image size. + Size = new Size(reader.ReadInt16(), reader.ReadInt16()); + + // Read the data byte array, which might be compressed or aligned. + if (alignData) + reader.Align(4); + byte[] data = new byte[Size.Width * Size.Height * BitsPerPixel / 8]; + if (flags.HasFlag(Flags.Compressed)) + Team17Compression.Decompress(reader.BaseStream, data); + else + data = reader.ReadBytes(data.Length); + + Data = data; } /// @@ -142,8 +141,8 @@ namespace Syroot.Worms /// true to align the data array by 4 bytes. public void Load(string fileName, bool alignData) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - Load(stream); + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream, alignData); } /// @@ -181,52 +180,51 @@ namespace Syroot.Worms /// true to align the data array by 4 bytes. public void Save(Stream stream, bool compress, bool alignData) { - using (BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII)) + using BinaryStream writer = new BinaryStream(stream, encoding: Encoding.ASCII); + + // Write the header. + writer.Write(_signature); + uint fileSizeOffset = writer.ReserveOffset(); + + // Write an optional string describing the image contents and the bits per pixel. + if (Description != null) + writer.Write(Description, StringCoding.ZeroTerminated); + writer.Write(BitsPerPixel); + + // Write image flags describing the format and availability of the following contents. + Flags flags = Flags.None; + if (Palette != null) + flags |= Flags.Palettized; + if (compress) + flags |= Flags.Compressed; + writer.WriteEnum(flags, true); + + // Write the image palette if available. The first color of the palette is implicitly black. + if (Palette != null) { - // Write the header. - writer.Write(_signature); - uint fileSizeOffset = writer.ReserveOffset(); - - // Write an optional string describing the image contents and the bits per pixel. - if (Description != null) - writer.Write(Description, StringCoding.ZeroTerminated); - writer.Write(BitsPerPixel); - - // Write image flags describing the format and availability of the following contents. - Flags flags = Flags.None; - if (Palette != null) - flags |= Flags.Palettized; - if (compress) - flags |= Flags.Compressed; - writer.WriteEnum(flags, true); - - // Write the image palette if available. The first color of the palette is implicitly black. - if (Palette != null) + writer.Write((short)(Palette.Count - 1)); + for (int i = 1; i < Palette.Count; i++) { - writer.Write((short)(Palette.Count - 1)); - for (int i = 1; i < Palette.Count; i++) - { - Color color = Palette[i]; - writer.Write(color.R); - writer.Write(color.G); - writer.Write(color.B); - } + Color color = Palette[i]; + writer.Write(color.R); + writer.Write(color.G); + writer.Write(color.B); } - - // Write the image size. - writer.Write((short)Size.Width); - writer.Write((short)Size.Height); - - // Write the data byte array, which might be compressed or aligned. - if (alignData) - writer.Align(4); - byte[] data = Data; - if (compress) - data = Team17Compression.Compress(data); - writer.WriteStructs(data); - - writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); } + + // Write the image size. + writer.Write((short)Size.Width); + writer.Write((short)Size.Height); + + // Write the data byte array, which might be compressed or aligned. + if (alignData) + writer.Align(4); + byte[] data = Data; + if (compress) + data = Team17Compression.Compress(data); + writer.WriteStructs(data); + + writer.SatisfyOffset(fileSizeOffset, (int)writer.Position); } /// @@ -237,8 +235,8 @@ namespace Syroot.Worms /// true to align the data array by 4 bytes. public void Save(string fileName, bool compress, bool alignData) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - Save(stream, compress, alignData); + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream, compress, alignData); } // ---- ENUMERATIONS ------------------------------------------------------------------------------------------- diff --git a/src/library/Syroot.Worms/RiffPalette.cs b/src/library/Syroot.Worms/RiffPalette.cs index bf0eff8..a384df7 100644 --- a/src/library/Syroot.Worms/RiffPalette.cs +++ b/src/library/Syroot.Worms/RiffPalette.cs @@ -23,29 +23,20 @@ namespace Syroot.Worms /// /// Initializes a new instance of the class. /// - public RiffPalette() - { - Version = _version; - } + public RiffPalette() => Version = _version; /// /// Initializes a new instance of the class, loading the data from the given /// . /// /// The to load the data from. - public RiffPalette(Stream stream) - { - Load(stream); - } + public RiffPalette(Stream stream) => Load(stream); /// /// Initializes a new instance of the class, loading the data from the given file. /// /// The name of the file to load the data from. - public RiffPalette(string fileName) - { - Load(fileName); - } + public RiffPalette(string fileName) => Load(fileName); // ---- PROPERTIES --------------------------------------------------------------------------------------------- @@ -80,10 +71,7 @@ namespace Syroot.Worms /// Loads the data from the given . /// /// The to load the data from. - public void Load(Stream stream) - { - LoadRiff(stream); - } + public void Load(Stream stream) => LoadRiff(stream); /// /// Loads the data from the given file. @@ -91,20 +79,15 @@ namespace Syroot.Worms /// The name of the file to load the data from. public void Load(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) - { - Load(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); + Load(stream); } /// /// Saves the data into the given . /// /// The to save the data to. - public void Save(Stream stream) - { - SaveRiff(stream); - } + public void Save(Stream stream) => SaveRiff(stream); /// /// Saves the data in the given file. @@ -112,10 +95,8 @@ namespace Syroot.Worms /// The name of the file to save the data in. public void Save(string fileName) { - using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)) - { - Save(stream); - } + using FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); + Save(stream); } // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- @@ -126,36 +107,25 @@ namespace Syroot.Worms // Read the PAL version. Version = reader.ReadInt16(); if (Version != _version) - { throw new InvalidDataException("Unknown PAL version."); - } // Read the colors. Colors = new Color[reader.ReadInt16()]; for (int i = 0; i < Colors.Length; i++) { Colors[i] = Color.FromArgb(reader.Read1Byte(), reader.Read1Byte(), reader.Read1Byte()); - int alpha = reader.ReadByte(); // Dismiss alpha, as it is not used in WA. + _ = reader.ReadByte(); // Dismiss alpha, as it is not used in WA. } } [RiffChunkLoad("offl")] - private void LoadOfflChunk(BinaryStream reader, int length) - { - OfflData = reader.ReadBytes(length); - } + private void LoadOfflChunk(BinaryStream reader, int length) => OfflData = reader.ReadBytes(length); [RiffChunkLoad("tran")] - private void LoadTranChunk(BinaryStream reader, int length) - { - TranData = reader.ReadBytes(length); - } + private void LoadTranChunk(BinaryStream reader, int length) => TranData = reader.ReadBytes(length); [RiffChunkLoad("unde")] - private void LoadUndeChunk(BinaryStream reader, int length) - { - UndeData = reader.ReadBytes(length); - } + private void LoadUndeChunk(BinaryStream reader, int length) => UndeData = reader.ReadBytes(length); [RiffChunkSave("data")] private void SaveDataChunk(BinaryStream writer) @@ -176,21 +146,12 @@ namespace Syroot.Worms } [RiffChunkSave("offl")] - private void SaveOfflChunk(BinaryStream writer) - { - writer.WriteStructs(OfflData); - } + private void SaveOfflChunk(BinaryStream writer) => writer.WriteStructs(OfflData); [RiffChunkSave("tran")] - private void SaveTranChunk(BinaryStream writer) - { - writer.WriteStructs(TranData); - } + private void SaveTranChunk(BinaryStream writer) => writer.WriteStructs(TranData); [RiffChunkSave("unde")] - private void SaveUndeChunk(BinaryStream writer) - { - writer.WriteStructs(UndeData); - } + private void SaveUndeChunk(BinaryStream writer) => writer.WriteStructs(UndeData); } } diff --git a/src/library/Syroot.Worms/Syroot.Worms.csproj b/src/library/Syroot.Worms/Syroot.Worms.csproj index 38804b7..4b0adee 100644 --- a/src/library/Syroot.Worms/Syroot.Worms.csproj +++ b/src/library/Syroot.Worms/Syroot.Worms.csproj @@ -1,26 +1,26 @@  - - Syroot.Worms - Syroot - (c) Syroot, licensed under MIT - .NET library for loading and modifying files of Team17 Worms games. - true - latest - https://gitlab.com/Syroot/Worms/raw/master/res/icon.png - Syroot.Worms - https://gitlab.com/Syroot/Worms/raw/master/LICENSE - https://gitlab.com/Syroot/Worms - Initial release. - worms;team17 - git - https://gitlab.com/Syroot/Worms - net461;netstandard2.0 - 2.0.0-alpha1 - - - - - - - + + Syroot.Worms + Syroot + (c) Syroot, licensed under MIT + .NET library for loading and modifying files of Team17 Worms games. + true + latest + https://gitlab.com/Syroot/Worms/raw/master/res/icon.png + Syroot.Worms + https://gitlab.com/Syroot/Worms/raw/master/LICENSE + https://gitlab.com/Syroot/Worms + Initial release. + worms;team17 + git + https://gitlab.com/Syroot/Worms + netstandard2.0 + 2.0.0 + + + + + + + \ No newline at end of file diff --git a/src/test/Syroot.Worms.Armageddon.ProjectX.Test/Syroot.Worms.Armageddon.ProjectX.Test.csproj b/src/test/Syroot.Worms.Armageddon.ProjectX.Test/Syroot.Worms.Armageddon.ProjectX.Test.csproj index 2970c51..6200144 100644 --- a/src/test/Syroot.Worms.Armageddon.ProjectX.Test/Syroot.Worms.Armageddon.ProjectX.Test.csproj +++ b/src/test/Syroot.Worms.Armageddon.ProjectX.Test/Syroot.Worms.Armageddon.ProjectX.Test.csproj @@ -1,13 +1,13 @@  - - netcoreapp2.1 - false - - - - - - - - + + netcoreapp2.1 + false + + + + + + + + diff --git a/src/test/Syroot.Worms.Armageddon.Test/Syroot.Worms.Armageddon.Test.csproj b/src/test/Syroot.Worms.Armageddon.Test/Syroot.Worms.Armageddon.Test.csproj index 42bdc21..220b712 100644 --- a/src/test/Syroot.Worms.Armageddon.Test/Syroot.Worms.Armageddon.Test.csproj +++ b/src/test/Syroot.Worms.Armageddon.Test/Syroot.Worms.Armageddon.Test.csproj @@ -1,13 +1,13 @@  - - netcoreapp2.1 - false - - - - - - - - + + netcoreapp2.1 + false + + + + + + + + diff --git a/src/test/Syroot.Worms.Mgame.Test/Syroot.Worms.Mgame.Test.csproj b/src/test/Syroot.Worms.Mgame.Test/Syroot.Worms.Mgame.Test.csproj index d77f426..f09377c 100644 --- a/src/test/Syroot.Worms.Mgame.Test/Syroot.Worms.Mgame.Test.csproj +++ b/src/test/Syroot.Worms.Mgame.Test/Syroot.Worms.Mgame.Test.csproj @@ -1,12 +1,12 @@  - - netcoreapp2.1 - false - - - - - - - + + netcoreapp2.1 + false + + + + + + + diff --git a/src/test/Syroot.Worms.Test/Syroot.Worms.Test.csproj b/src/test/Syroot.Worms.Test/Syroot.Worms.Test.csproj index e3e1ce8..946da76 100644 --- a/src/test/Syroot.Worms.Test/Syroot.Worms.Test.csproj +++ b/src/test/Syroot.Worms.Test/Syroot.Worms.Test.csproj @@ -1,12 +1,12 @@  - - netcoreapp2.1 - false - - - - - - - + + netcoreapp2.1 + false + + + + + + + diff --git a/src/test/Syroot.Worms.WorldParty.Test/Syroot.Worms.WorldParty.Test.csproj b/src/test/Syroot.Worms.WorldParty.Test/Syroot.Worms.WorldParty.Test.csproj index c5dfbe4..5f13b38 100644 --- a/src/test/Syroot.Worms.WorldParty.Test/Syroot.Worms.WorldParty.Test.csproj +++ b/src/test/Syroot.Worms.WorldParty.Test/Syroot.Worms.WorldParty.Test.csproj @@ -1,13 +1,13 @@  - - netcoreapp2.1 - false - - - - - - - - + + netcoreapp2.1 + false + + + + + + + + diff --git a/src/test/Syroot.Worms.Worms2.Test/Syroot.Worms.Worms2.Test.csproj b/src/test/Syroot.Worms.Worms2.Test/Syroot.Worms.Worms2.Test.csproj index dc2f3d8..a0a4079 100644 --- a/src/test/Syroot.Worms.Worms2.Test/Syroot.Worms.Worms2.Test.csproj +++ b/src/test/Syroot.Worms.Worms2.Test/Syroot.Worms.Worms2.Test.csproj @@ -1,13 +1,13 @@ - - netcoreapp2.1 - false - - - - - - - - + + netcoreapp2.1 + false + + + + + + + + diff --git a/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs b/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs index cf74dd0..b9f392e 100644 --- a/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs +++ b/src/tool/Syroot.Worms.Mgame.GameServer/Config.cs @@ -33,8 +33,8 @@ namespace Syroot.Worms.Mgame.GameServer // Retrieve external IP if not yet done and given IP is invalid. if (_ipAddress == null && (IP == null || !IPAddress.TryParse(IP, out _ipAddress))) { - using (WebClient webClient = new WebClient()) - _ipAddress = IPAddress.Parse(webClient.DownloadString("https://ip.syroot.com")); + using WebClient webClient = new WebClient(); + _ipAddress = IPAddress.Parse(webClient.DownloadString("https://ip.syroot.com")); } return _ipAddress; } diff --git a/src/tool/Syroot.Worms.Mgame.GameServer/Packets/PacketConnection.cs b/src/tool/Syroot.Worms.Mgame.GameServer/Packets/PacketConnection.cs index d1377bb..7ca7bdc 100644 --- a/src/tool/Syroot.Worms.Mgame.GameServer/Packets/PacketConnection.cs +++ b/src/tool/Syroot.Worms.Mgame.GameServer/Packets/PacketConnection.cs @@ -91,6 +91,7 @@ namespace Syroot.Worms.Mgame.GameServer.Packets { if (disposing) TcpClient.Dispose(); + _tcpStream.Dispose(); _disposed = true; } } @@ -102,10 +103,8 @@ namespace Syroot.Worms.Mgame.GameServer.Packets { // Let each packet format try to serialize the data. foreach (IPacketFormat format in _packetFormatPipe) - { if (format.TrySave(_tcpStream, packet)) return; - } throw new NotImplementedException("Cannot send unhandled packet format."); } catch (IOException) { } // A network error appeared, and communication should end. diff --git a/src/tool/Syroot.Worms.Mgame.GameServer/Program.cs b/src/tool/Syroot.Worms.Mgame.GameServer/Program.cs index 5bb64d8..6a53466 100644 --- a/src/tool/Syroot.Worms.Mgame.GameServer/Program.cs +++ b/src/tool/Syroot.Worms.Mgame.GameServer/Program.cs @@ -9,7 +9,7 @@ namespace Syroot.Worms.Mgame.GameServer { // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- - private static void Main(string[] args) + private static void Main() { try { diff --git a/src/tool/Syroot.Worms.Mgame.GameServer/Syroot.Worms.Mgame.GameServer.csproj b/src/tool/Syroot.Worms.Mgame.GameServer/Syroot.Worms.Mgame.GameServer.csproj index d37bdfe..419f1a6 100644 --- a/src/tool/Syroot.Worms.Mgame.GameServer/Syroot.Worms.Mgame.GameServer.csproj +++ b/src/tool/Syroot.Worms.Mgame.GameServer/Syroot.Worms.Mgame.GameServer.csproj @@ -1,21 +1,21 @@  - - false - Server - latest - Exe - netcoreapp2.1 - - - - - - - - - - - PreserveNewest - - + + false + Server + latest + Exe + netcoreapp2.1 + + + + + + + + + + + PreserveNewest + + diff --git a/src/tool/Syroot.Worms.Mgame.Launcher/Program.cs b/src/tool/Syroot.Worms.Mgame.Launcher/Program.cs index 1da7599..50b8c91 100644 --- a/src/tool/Syroot.Worms.Mgame.Launcher/Program.cs +++ b/src/tool/Syroot.Worms.Mgame.Launcher/Program.cs @@ -14,7 +14,7 @@ namespace Syroot.Worms.Mgame.Launcher { // ---- METHODS (PRIVATE) -------------------------------------------------------------------------------------- - private static void Main(string[] args) + private static void Main() { try { @@ -36,23 +36,25 @@ namespace Syroot.Worms.Mgame.Launcher using (launchConfig.CreateMappedFile(config.MappingName)) { // Create and run the process. - NativeProcess process = CreateProcess(executablePath, + using (NativeProcess process = CreateProcess(executablePath, String.Join(" ", config.MappingName, config.ExecutableArgs).TrimEnd(), - config.StartSuspended); - if (config.StartSuspended) + config.StartSuspended)) { - if (ShowMessage(MessageBoxIcon.Information, - "Game process has been created and is paused. Click OK to resume or Cancel to kill it.", - MessageBoxButtons.OKCancel) == DialogResult.OK) + if (config.StartSuspended) { - process.Resume(); - } - else - { - process.Terminate(); + if (ShowMessage(MessageBoxIcon.Information, + "Game process has been created and is paused. Click OK to resume or Cancel to kill it.", + MessageBoxButtons.OKCancel) == DialogResult.OK) + { + process.Resume(); + } + else + { + process.Terminate(); + } } + process.WaitForExit(); } - process.WaitForExit(); } } catch (Exception ex) diff --git a/src/tool/Syroot.Worms.Mgame.Launcher/Syroot.Worms.Mgame.Launcher.csproj b/src/tool/Syroot.Worms.Mgame.Launcher/Syroot.Worms.Mgame.Launcher.csproj index d8689c2..cac6a7b 100644 --- a/src/tool/Syroot.Worms.Mgame.Launcher/Syroot.Worms.Mgame.Launcher.csproj +++ b/src/tool/Syroot.Worms.Mgame.Launcher/Syroot.Worms.Mgame.Launcher.csproj @@ -1,29 +1,29 @@  - - false - Resources\Icon.ico - Resources\app.manifest - Launcher - Mgame Worms Launcher - Syroot - (c) Syroot, licensed under MIT - None - Game launcher for Mgame Worms games - WinExe - Online Worms Launcher - net461 - 1.0.0 - - - - - - - - - - - PreserveNewest - - + + false + Resources\Icon.ico + Resources\app.manifest + Launcher + Mgame Worms Launcher + Syroot + (c) Syroot, licensed under MIT + None + Game launcher for Mgame Worms games + WinExe + Online Worms Launcher + net461 + 1.0.0 + + + + + + + + + + + PreserveNewest + + diff --git a/src/tool/Syroot.Worms.Scratchpad/Syroot.Worms.Scratchpad.csproj b/src/tool/Syroot.Worms.Scratchpad/Syroot.Worms.Scratchpad.csproj index 48d49e5..5f313f7 100644 --- a/src/tool/Syroot.Worms.Scratchpad/Syroot.Worms.Scratchpad.csproj +++ b/src/tool/Syroot.Worms.Scratchpad/Syroot.Worms.Scratchpad.csproj @@ -1,15 +1,15 @@  - - Exe - netcoreapp2.1 - - - - - - - - - - + + Exe + netcoreapp2.1 + + + + + + + + + + \ No newline at end of file