From 382b38abe08702865237cb78bcaf07d4b7f1c825 Mon Sep 17 00:00:00 2001 From: Tim Sarbin Date: Sat, 8 Dec 2018 12:52:09 -0500 Subject: [PATCH] Fixed more rule validations. Added ImmultableDictionary reference. --- OpenDiablo2.Common/Enums/Mobs/eMobFlags.cs | 20 +++++++++--- OpenDiablo2.Common/Enums/eArmorType.cs | 5 +-- OpenDiablo2.Common/Enums/eHero.cs | 5 +-- OpenDiablo2.Common/Enums/eLevelId.cs | 6 ++-- OpenDiablo2.Common/Models/BitStream.cs | 9 +++--- OpenDiablo2.Common/Models/ButtonLayout.cs | 11 +++---- OpenDiablo2.Common/Models/ImageSet.cs | 32 ++++++++----------- .../Models/ItemContainerLayout.cs | 31 ++++++++++++------ OpenDiablo2.Common/Models/Mobs/MobState.cs | 7 ++++ OpenDiablo2.Common/OpenDiablo2.Common.csproj | 3 ++ OpenDiablo2.Common/packages.config | 1 + OpenDiablo2.SDL2/CS-SDL/SDL2.cs | 23 ++++++++++--- OpenDiablo2.SDL2/CS-SDL/SDL2_image.cs | 5 ++- OpenDiablo2.SDL2/CS-SDL/SDL2_mixer.cs | 5 ++- OpenDiablo2.SDL2/CS-SDL/SDL2_ttf.cs | 6 +++- OpenDiablo2.Scenes/Game.cs | 12 ------- .../Message Frames/Client/MFJoinGame.cs | 5 +-- OpenDiablo2/OpenDiablo2.csproj | 3 ++ OpenDiablo2/packages.config | 1 + 19 files changed, 114 insertions(+), 76 deletions(-) diff --git a/OpenDiablo2.Common/Enums/Mobs/eMobFlags.cs b/OpenDiablo2.Common/Enums/Mobs/eMobFlags.cs index 69af6816..8d58e354 100644 --- a/OpenDiablo2.Common/Enums/Mobs/eMobFlags.cs +++ b/OpenDiablo2.Common/Enums/Mobs/eMobFlags.cs @@ -1,8 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C# + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ namespace OpenDiablo2.Common.Enums.Mobs { diff --git a/OpenDiablo2.Common/Enums/eArmorType.cs b/OpenDiablo2.Common/Enums/eArmorType.cs index b3ef95a7..a8187a0e 100644 --- a/OpenDiablo2.Common/Enums/eArmorType.cs +++ b/OpenDiablo2.Common/Enums/eArmorType.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,12 +16,12 @@ namespace OpenDiablo2.Common.Enums public static class eArmorTypeHelper { - private static readonly Dictionary tokens = new Dictionary() + private static readonly ImmutableDictionary tokens = new Dictionary() { { eArmorType.Lite , "lit" }, { eArmorType.Medium , "med" }, { eArmorType.Heavy , "hvy" } - }; + }.ToImmutableDictionary(); public static string ToToken(this eArmorType armorType) => tokens[armorType]; public static eArmorType ToArmorType(this string source) => tokens.First(x => x.Value.ToUpper() == source.ToUpper()).Key; } diff --git a/OpenDiablo2.Common/Enums/eHero.cs b/OpenDiablo2.Common/Enums/eHero.cs index dfc3d0de..1a05e89b 100644 --- a/OpenDiablo2.Common/Enums/eHero.cs +++ b/OpenDiablo2.Common/Enums/eHero.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -19,7 +20,7 @@ namespace OpenDiablo2.Common.Enums public static class eHeroExtensions { - public readonly static Dictionary tokens = new Dictionary + public readonly static ImmutableDictionary tokens = new Dictionary { { eHero.Amazon , "AM" }, { eHero.Sorceress , "SO" }, @@ -28,7 +29,7 @@ namespace OpenDiablo2.Common.Enums { eHero.Barbarian , "BA" }, { eHero.Druid , "DZ" }, { eHero.Assassin , "AI" } - }; + }.ToImmutableDictionary(); public static string ToToken(this eHero source) => tokens[source]; public static eHero ToHero(this string source) => tokens.First(x => x.Value.ToUpper() == source.ToUpper()).Key; diff --git a/OpenDiablo2.Common/Enums/eLevelId.cs b/OpenDiablo2.Common/Enums/eLevelId.cs index 54a2ec10..ec58ecc4 100644 --- a/OpenDiablo2.Common/Enums/eLevelId.cs +++ b/OpenDiablo2.Common/Enums/eLevelId.cs @@ -50,7 +50,7 @@ namespace OpenDiablo2.Common.Enums { public static string GenerateEnum(List levelPresets) { - string output = string.Empty; + var output = new StringBuilder(); foreach (LevelPreset lp in levelPresets) { // need to convert e.g. 'Act 1 - Town 1' to 'Act1_Town' @@ -59,9 +59,9 @@ namespace OpenDiablo2.Common.Enums continue; } string name = lp.Name.Replace(" - ", "_").Replace(" ", "").Replace("'", ""); - output += name + " = " + lp.LevelId + ",\r\n"; + output.AppendLine($"{name}={lp.LevelId}"); } - return output; + return output.ToString(); } } } diff --git a/OpenDiablo2.Common/Models/BitStream.cs b/OpenDiablo2.Common/Models/BitStream.cs index 13d86fd3..a17217e1 100644 --- a/OpenDiablo2.Common/Models/BitStream.cs +++ b/OpenDiablo2.Common/Models/BitStream.cs @@ -20,8 +20,8 @@ namespace OpenDiablo2.Common.Models public int ReadBits(int bitCount) { if (bitCount > 16) - throw new ArgumentOutOfRangeException("BitCount", "Maximum BitCount is 16"); - if (EnsureBits(bitCount) == false) return -1; + throw new ArgumentOutOfRangeException("bitCount", "Maximum BitCount is 16"); + if (!EnsureBits(bitCount)) return -1; int result = _current & (0xffff >> (16 - bitCount)); WasteBits(bitCount); return result; @@ -29,7 +29,7 @@ namespace OpenDiablo2.Common.Models public int PeekByte() { - if (EnsureBits(8) == false) return -1; + if (!EnsureBits(8)) return -1; return _current & 0xff; } @@ -44,11 +44,10 @@ namespace OpenDiablo2.Common.Models return true; } - private bool WasteBits(int bitCount) + private void WasteBits(int bitCount) { _current >>= bitCount; _bitCount -= bitCount; - return true; } } } diff --git a/OpenDiablo2.Common/Models/ButtonLayout.cs b/OpenDiablo2.Common/Models/ButtonLayout.cs index 9c200391..6497e487 100644 --- a/OpenDiablo2.Common/Models/ButtonLayout.cs +++ b/OpenDiablo2.Common/Models/ButtonLayout.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Collections.Immutable; using OpenDiablo2.Common.Enums; namespace OpenDiablo2.Common.Models @@ -15,7 +12,7 @@ namespace OpenDiablo2.Common.Models public bool Toggleable { get; internal set; } = false; public int BaseFrame { get; internal set; } = 0; - public static Dictionary Values = new Dictionary + public static ImmutableDictionary Values { get; } = new Dictionary { {eButtonType.Wide, new ButtonLayout { XSegments = 2, ResourceName = ResourcePaths.WideButtonBlank, PaletteName = Palettes.Units } }, {eButtonType.Medium, new ButtonLayout{ XSegments = 1, ResourceName=ResourcePaths.MediumButtonBlank, PaletteName = Palettes.Units } }, @@ -33,7 +30,7 @@ namespace OpenDiablo2.Common.Models {eButtonType.Run, new ButtonLayout {XSegments = 1, ResourceName = ResourcePaths.RunButton,PaletteName = Palettes.Units, Toggleable = true } }, {eButtonType.Menu, new ButtonLayout {XSegments = 1, ResourceName = ResourcePaths.MenuButton,PaletteName = Palettes.Units, Toggleable = true } }, - }; + }.ToImmutableDictionary(); } } diff --git a/OpenDiablo2.Common/Models/ImageSet.cs b/OpenDiablo2.Common/Models/ImageSet.cs index ba4e91da..ce269782 100644 --- a/OpenDiablo2.Common/Models/ImageSet.cs +++ b/OpenDiablo2.Common/Models/ImageSet.cs @@ -1,25 +1,21 @@ using System; -using System.Collections.Generic; -using System.Drawing; using System.IO; using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OpenDiablo2.Common.Models { public class ImageFrame : IDisposable { - public UInt32 Flip; - public UInt32 Width; - public UInt32 Height; - public Int32 OffsetX; - public Int32 OffsetY; // from bottom border, not up - public UInt32 Unknown; - public UInt32 NextBlock; - public UInt32 Length; - public Int16[] ImageData; + public UInt32 Flip { get; internal set; } + public UInt32 Width { get; internal set; } + public UInt32 Height { get; internal set; } + public Int32 OffsetX { get; internal set; } + public Int32 OffsetY { get; internal set; } // from bottom border, not up + public UInt32 Unknown { get; internal set; } + public UInt32 NextBlock { get; internal set; } + public UInt32 Length { get; internal set; } + public Int16[] ImageData { get; internal set; } public void Dispose() { @@ -42,8 +38,6 @@ namespace OpenDiablo2.Common.Models public sealed class ImageSet : IDisposable { - static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - private UInt32[] framePointers; public ImageFrame[] Frames { get; private set; } @@ -53,10 +47,10 @@ namespace OpenDiablo2.Common.Models public static ImageSet LoadFromStream(Stream stream) { var br = new BinaryReader(stream); - var version = br.ReadUInt32(); - var unknown1 = br.ReadUInt32(); - var unknown2 = br.ReadUInt32(); - var termination = br.ReadUInt32(); + br.ReadUInt32(); // Version + br.ReadUInt32(); // Unknown 1 + br.ReadUInt32(); // Unknown 2 + br.ReadUInt32(); // Termination var result = new ImageSet { diff --git a/OpenDiablo2.Common/Models/ItemContainerLayout.cs b/OpenDiablo2.Common/Models/ItemContainerLayout.cs index bd0701b8..aac4c122 100644 --- a/OpenDiablo2.Common/Models/ItemContainerLayout.cs +++ b/OpenDiablo2.Common/Models/ItemContainerLayout.cs @@ -1,8 +1,21 @@ -using System; +/* OpenDiablo 2 - An open source re-implementation of Diablo 2 in C# + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Collections.Immutable; using OpenDiablo2.Common.Enums; namespace OpenDiablo2.Common.Models @@ -13,17 +26,17 @@ namespace OpenDiablo2.Common.Models public string PaletteName { get; internal set; } = Palettes.Units; public int BaseFrame { get; internal set; } = 0; - public static Dictionary Values = new Dictionary + public static ImmutableDictionary Values { get; } = new Dictionary { {eItemContainerType.Helm, new ItemContainerLayout { ResourceName = ResourcePaths.HelmGlovePlaceholder, BaseFrame = 1 } }, {eItemContainerType.Amulet, new ItemContainerLayout{ ResourceName = ResourcePaths.RingAmuletPlaceholder } }, {eItemContainerType.Armor, new ItemContainerLayout { ResourceName = ResourcePaths.ArmorPlaceholder } }, {eItemContainerType.Weapon, new ItemContainerLayout { ResourceName = ResourcePaths.WeaponsPlaceholder } }, - {eItemContainerType.Belt, new ItemContainerLayout { ResourceName = ResourcePaths.BeltPlaceholder } }, - {eItemContainerType.Ring, new ItemContainerLayout { ResourceName = ResourcePaths.RingAmuletPlaceholder, BaseFrame = 1 } }, - {eItemContainerType.Glove, new ItemContainerLayout { ResourceName = ResourcePaths.HelmGlovePlaceholder } }, + {eItemContainerType.Belt, new ItemContainerLayout { ResourceName = ResourcePaths.BeltPlaceholder } }, + {eItemContainerType.Ring, new ItemContainerLayout { ResourceName = ResourcePaths.RingAmuletPlaceholder, BaseFrame = 1 } }, + {eItemContainerType.Glove, new ItemContainerLayout { ResourceName = ResourcePaths.HelmGlovePlaceholder } }, {eItemContainerType.Boots, new ItemContainerLayout { ResourceName = ResourcePaths.BootsPlaceholder } }, - }; + }.ToImmutableDictionary(); } } diff --git a/OpenDiablo2.Common/Models/Mobs/MobState.cs b/OpenDiablo2.Common/Models/Mobs/MobState.cs index ba558127..19541819 100644 --- a/OpenDiablo2.Common/Models/Mobs/MobState.cs +++ b/OpenDiablo2.Common/Models/Mobs/MobState.cs @@ -167,5 +167,12 @@ namespace OpenDiablo2.Common.Models.Mobs public int CompareTo(object obj) => Id - ((MobState)obj).Id; + public override bool Equals(object obj) => Id == (obj as MobState)?.Id; + public static bool operator ==(MobState obj1, MobState obj2) => obj1?.Id == obj2?.Id; + public static bool operator !=(MobState obj1, MobState obj2) => obj1?.Id != obj2?.Id; + public static bool operator <(MobState obj1, MobState obj2) => obj1?.Id < obj2?.Id; + public static bool operator >(MobState obj1, MobState obj2) => obj1?.Id > obj2?.Id; + public static bool operator <=(MobState obj1, MobState obj2) => obj1?.Id <= obj2?.Id; + public static bool operator >=(MobState obj1, MobState obj2) => obj1?.Id >= obj2?.Id; } } diff --git a/OpenDiablo2.Common/OpenDiablo2.Common.csproj b/OpenDiablo2.Common/OpenDiablo2.Common.csproj index de188ffd..f773f62f 100644 --- a/OpenDiablo2.Common/OpenDiablo2.Common.csproj +++ b/OpenDiablo2.Common/OpenDiablo2.Common.csproj @@ -43,6 +43,9 @@ ..\packages\log4net.2.0.8\lib\net45-full\log4net.dll + + ..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll + diff --git a/OpenDiablo2.Common/packages.config b/OpenDiablo2.Common/packages.config index 37e4e453..c9ec91ce 100644 --- a/OpenDiablo2.Common/packages.config +++ b/OpenDiablo2.Common/packages.config @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/OpenDiablo2.SDL2/CS-SDL/SDL2.cs b/OpenDiablo2.SDL2/CS-SDL/SDL2.cs index 46bfc80b..e0f28463 100644 --- a/OpenDiablo2.SDL2/CS-SDL/SDL2.cs +++ b/OpenDiablo2.SDL2/CS-SDL/SDL2.cs @@ -25,7 +25,20 @@ * */ #endregion - +#pragma warning disable S4200 +#pragma warning disable S4214 +#pragma warning disable S2346 +#pragma warning disable S3459 +#pragma warning disable S1854 +#pragma warning disable S101 +#pragma warning disable S2344 +#pragma warning disable S1134 +#pragma warning disable S1144 +#pragma warning disable S125 +#pragma warning disable S1135 +#pragma warning disable S1168 +#pragma warning disable S1104 +#pragma warning disable S1168 #region Using Statements using System; using System.Runtime.InteropServices; @@ -158,13 +171,13 @@ namespace SDL2 /* mem refers to a void*, IntPtr to an SDL_RWops* */ [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] - public static extern IntPtr SDL_RWFromMem(IntPtr mem, int size); + public static extern IntPtr SDL_RWFromMem(IntPtr mem, int size); - #endregion + #endregion - #region SDL_main.h + #region SDL_main.h - [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] + [DllImport(nativeLibName, CallingConvention = CallingConvention.Cdecl)] public static extern void SDL_SetMainReady(); /* This is used as a function pointer to a C main() function for SDL_WinRTRunApp() */ diff --git a/OpenDiablo2.SDL2/CS-SDL/SDL2_image.cs b/OpenDiablo2.SDL2/CS-SDL/SDL2_image.cs index c8105c28..8a7b7796 100644 --- a/OpenDiablo2.SDL2/CS-SDL/SDL2_image.cs +++ b/OpenDiablo2.SDL2/CS-SDL/SDL2_image.cs @@ -25,7 +25,10 @@ * */ #endregion - +#pragma warning disable S4200 +#pragma warning disable S4214 +#pragma warning disable S2344 +#pragma warning disable S101 #region Using Statements using System; using System.Runtime.InteropServices; diff --git a/OpenDiablo2.SDL2/CS-SDL/SDL2_mixer.cs b/OpenDiablo2.SDL2/CS-SDL/SDL2_mixer.cs index aa6e682c..7e03d29b 100644 --- a/OpenDiablo2.SDL2/CS-SDL/SDL2_mixer.cs +++ b/OpenDiablo2.SDL2/CS-SDL/SDL2_mixer.cs @@ -25,7 +25,10 @@ * */ #endregion - +#pragma warning disable S4200 +#pragma warning disable S4214 +#pragma warning disable S2344 +#pragma warning disable S101 #region Using Statements using System; using System.Runtime.InteropServices; diff --git a/OpenDiablo2.SDL2/CS-SDL/SDL2_ttf.cs b/OpenDiablo2.SDL2/CS-SDL/SDL2_ttf.cs index 5d0a2829..d1047f56 100644 --- a/OpenDiablo2.SDL2/CS-SDL/SDL2_ttf.cs +++ b/OpenDiablo2.SDL2/CS-SDL/SDL2_ttf.cs @@ -25,7 +25,9 @@ * */ #endregion - +#pragma warning disable S4200 +#pragma warning disable S4214 +#pragma warning disable S101 #region Using Statements using System; using System.Runtime.InteropServices; @@ -486,3 +488,5 @@ namespace SDL2 #endregion } } +#pragma warning restore S4214 +#pragma warning restore S4200 // Native methods should be wrapped \ No newline at end of file diff --git a/OpenDiablo2.Scenes/Game.cs b/OpenDiablo2.Scenes/Game.cs index 2472a59b..876bd2ec 100644 --- a/OpenDiablo2.Scenes/Game.cs +++ b/OpenDiablo2.Scenes/Game.cs @@ -10,23 +10,13 @@ namespace OpenDiablo2.Scenes [Scene(eSceneType.Game)] public sealed class Game : IScene { - private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); - private readonly IRenderWindow renderWindow; private readonly IMapEngine mapEngine; private readonly IMouseInfoProvider mouseInfoProvider; private readonly IGameState gameState; private readonly ISessionManager sessionManager; - private readonly IKeyboardInfoProvider keyboardInfoProvider; private readonly IGameHUD gameHUD; - //private ISprite[] testSprite; - - private readonly ISprite panelSprite, healthManaSprite, gameGlobeOverlapSprite; - - private readonly IMiniPanel minipanel; - - private readonly IButton runButton, menuButton; private eMovementType lastMovementType = eMovementType.Stopped; private byte lastDirection = 255; @@ -37,7 +27,6 @@ namespace OpenDiablo2.Scenes IMapEngine mapEngine, IGameState gameState, IMouseInfoProvider mouseInfoProvider, - IKeyboardInfoProvider keyboardInfoProvider, IItemManager itemManager, ISessionManager sessionManager, IGameHUD gameHUD @@ -47,7 +36,6 @@ namespace OpenDiablo2.Scenes this.mapEngine = mapEngine; this.gameState = gameState; this.mouseInfoProvider = mouseInfoProvider; - this.keyboardInfoProvider = keyboardInfoProvider; this.sessionManager = sessionManager; this.gameHUD = gameHUD; diff --git a/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs b/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs index 553beef3..c8209ce1 100644 --- a/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs +++ b/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs @@ -16,13 +16,10 @@ namespace OpenDiablo2.ServiceBus.Message_Frames.Client public byte[] Data { - get - { - return new byte[] { (byte)HeroType } + get => new byte[] { (byte)HeroType } .Concat(BitConverter.GetBytes((UInt16)PlayerName.Length)) .Concat(Encoding.UTF8.GetBytes(PlayerName)) .ToArray(); - } set { diff --git a/OpenDiablo2/OpenDiablo2.csproj b/OpenDiablo2/OpenDiablo2.csproj index 319b4867..d845e60b 100644 --- a/OpenDiablo2/OpenDiablo2.csproj +++ b/OpenDiablo2/OpenDiablo2.csproj @@ -63,6 +63,9 @@ ..\packages\log4net.2.0.8\lib\net45-full\log4net.dll + + ..\packages\System.Collections.Immutable.1.5.0\lib\netstandard2.0\System.Collections.Immutable.dll + ..\packages\System.Console.4.3.1\lib\net46\System.Console.dll diff --git a/OpenDiablo2/packages.config b/OpenDiablo2/packages.config index 30b8494a..20f95956 100644 --- a/OpenDiablo2/packages.config +++ b/OpenDiablo2/packages.config @@ -4,6 +4,7 @@ +