From 86547875fddc16257abb7d216ad6a22ae4106bd4 Mon Sep 17 00:00:00 2001 From: Tim Sarbin Date: Sat, 8 Dec 2018 12:08:01 -0500 Subject: [PATCH] Replaced all system exceptions with OpenDiablo2Exception --- OpenDiablo2.Common/Enums/eLevelId.cs | 2 +- .../Exceptions/OpenDiablo2Exception.cs | 32 +++++++++++++++++++ OpenDiablo2.Common/Models/MPQ.cs | 9 +++--- OpenDiablo2.Common/Models/MPQDCC.cs | 19 +++++------ OpenDiablo2.Common/Models/MPQDT1.cs | 3 +- OpenDiablo2.Common/Models/MPQFont.cs | 3 +- OpenDiablo2.Common/Models/MPQHuffman.cs | 6 +++- OpenDiablo2.Common/Models/MPQStream.cs | 19 +++++------ .../Models/Mobs/LevelExperienceConfig.cs | 3 +- OpenDiablo2.Common/OpenDiablo2.Common.csproj | 1 + OpenDiablo2.Common/StringUtils.cs | 2 +- OpenDiablo2.Core/GameState/GameState.cs | 7 ++-- OpenDiablo2.Core/GameState/MobManager.cs | 5 +-- OpenDiablo2.SDL2/SDL2CharacterRenderer.cs | 5 +-- OpenDiablo2.SDL2/SDL2Label.cs | 5 +-- OpenDiablo2.SDL2/SDL2RenderWindow.cs | 11 ++++--- OpenDiablo2.SDL2/SDL2Sprite.cs | 3 +- .../Message Frames/Client/MFJoinGame.cs | 3 +- OpenDiablo2.ServiceBus/SessionManager.cs | 9 +++--- OpenDiablo2.ServiceBus/SessionServer.cs | 3 +- OpenDiablo2.TestConsole/Program.cs | 13 +++----- 21 files changed, 106 insertions(+), 57 deletions(-) create mode 100644 OpenDiablo2.Common/Exceptions/OpenDiablo2Exception.cs diff --git a/OpenDiablo2.Common/Enums/eLevelId.cs b/OpenDiablo2.Common/Enums/eLevelId.cs index c949eaf5..54a2ec10 100644 --- a/OpenDiablo2.Common/Enums/eLevelId.cs +++ b/OpenDiablo2.Common/Enums/eLevelId.cs @@ -46,7 +46,7 @@ namespace OpenDiablo2.Common.Enums Act5_BaalEntrance = 120, } - public class ELevelIdHelper + public static class ELevelIdHelper { public static string GenerateEnum(List levelPresets) { diff --git a/OpenDiablo2.Common/Exceptions/OpenDiablo2Exception.cs b/OpenDiablo2.Common/Exceptions/OpenDiablo2Exception.cs new file mode 100644 index 00000000..225a9e7c --- /dev/null +++ b/OpenDiablo2.Common/Exceptions/OpenDiablo2Exception.cs @@ -0,0 +1,32 @@ +/* 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; + +namespace OpenDiablo2.Common.Exceptions +{ + + [Serializable] + public class OpenDiablo2Exception : Exception + { + public OpenDiablo2Exception() { } + public OpenDiablo2Exception(string message) : base(message) { } + public OpenDiablo2Exception(string message, Exception inner) : base(message, inner) { } + protected OpenDiablo2Exception( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/OpenDiablo2.Common/Models/MPQ.cs b/OpenDiablo2.Common/Models/MPQ.cs index 8b4c98e5..467223be 100644 --- a/OpenDiablo2.Common/Models/MPQ.cs +++ b/OpenDiablo2.Common/Models/MPQ.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; namespace OpenDiablo2.Common.Models { @@ -115,7 +116,7 @@ namespace OpenDiablo2.Common.Models { var header = Encoding.ASCII.GetString(br.ReadBytes(4)); if (header != HEADER_SIGNATURE) - throw new ApplicationException($"Unknown header signature '{header}' detected while processing '{Path}'!"); + throw new OpenDiablo2Exception($"Unknown header signature '{header}' detected while processing '{Path}'!"); ParseMPQHeader(br); } @@ -198,10 +199,10 @@ namespace OpenDiablo2.Common.Models }; if (FormatVersion != eMPQFormatVersion.Format1) - throw new ApplicationException($"Unsupported MPQ format version of {Header.FormatVersion} detected for '{Path}'!"); + throw new OpenDiablo2Exception($"Unsupported MPQ format version of {Header.FormatVersion} detected for '{Path}'!"); if (br.BaseStream.Position != Header.HeaderSize) - throw new ApplicationException($"Invalid header size detected for '{Path}'. Expected to be at offset {Header.HeaderSize} but we are at offset {br.BaseStream.Position} instead!"); + throw new OpenDiablo2Exception($"Invalid header size detected for '{Path}'. Expected to be at offset {Header.HeaderSize} but we are at offset {br.BaseStream.Position} instead!"); br.BaseStream.Seek(Header.BlockTablePos, SeekOrigin.Begin); @@ -254,7 +255,7 @@ namespace OpenDiablo2.Common.Models private static UInt32 HashString(string inputString, UInt32 hashType) { if (hashType > MPQ_HASH_FILE_KEY) - throw new ApplicationException($"Unknown hash type {hashType} for input string {inputString}"); + throw new OpenDiablo2Exception($"Unknown hash type {hashType} for input string {inputString}"); UInt32 seed1 = 0x7FED7FED; UInt32 seed2 = 0xEEEEEEEE; diff --git a/OpenDiablo2.Common/Models/MPQDCC.cs b/OpenDiablo2.Common/Models/MPQDCC.cs index 7790ab69..ae42efc6 100644 --- a/OpenDiablo2.Common/Models/MPQDCC.cs +++ b/OpenDiablo2.Common/Models/MPQDCC.cs @@ -3,6 +3,7 @@ using System.Linq; using System.IO; using System.Collections.Generic; using System.Drawing; +using OpenDiablo2.Common.Exceptions; namespace OpenDiablo2.Common.Models { @@ -198,7 +199,7 @@ namespace OpenDiablo2.Common.Models }; if (OptionalDataBits > 0) - throw new ApplicationException("Optional bits in DCC data is not currently supported."); + throw new OpenDiablo2Exception("Optional bits in DCC data is not currently supported."); if ((CompressionFlags & 0x2) > 0) EqualCellsBitstreamSize = (int)bm.GetBits(20); @@ -259,25 +260,25 @@ namespace OpenDiablo2.Common.Models FillPixelBuffer(pixelCodeandDisplacement, equalCellsBitstream, pixelMaskBitstream, encodingTypeBitsream, rawPixelCodesBitstream); // Generate the actual frame pixel data - GenerateFrames(file, pixelCodeandDisplacement); + GenerateFrames(pixelCodeandDisplacement); // Verify that everything we expected to read was actually read (sanity check)... if (equalCellsBitstream.BitsRead != EqualCellsBitstreamSize) - throw new ApplicationException("Did not read the correct number of bits!"); + throw new OpenDiablo2Exception("Did not read the correct number of bits!"); if (pixelMaskBitstream.BitsRead != PixelMaskBitstreamSize) - throw new ApplicationException("Did not read the correct number of bits!"); + throw new OpenDiablo2Exception("Did not read the correct number of bits!"); if (encodingTypeBitsream.BitsRead != EncodingTypeBitsreamSize) - throw new ApplicationException("Did not read the correct number of bits!"); + throw new OpenDiablo2Exception("Did not read the correct number of bits!"); if (rawPixelCodesBitstream.BitsRead != RawPixelCodesBitstreamSize) - throw new ApplicationException("Did not read the correct number of bits!"); + throw new OpenDiablo2Exception("Did not read the correct number of bits!"); bm.SkipBits(pixelCodeandDisplacement.BitsRead); } - private void GenerateFrames(MPQDCC file, BitMuncher pcd) + private void GenerateFrames(BitMuncher pcd) { var pbIdx = 0; @@ -552,14 +553,14 @@ namespace OpenDiablo2.Common.Models var bm = new BitMuncher(data); Signature = bm.GetByte(); if (Signature != 0x74) - throw new ApplicationException("Signature expected to be 0x74 but it is not."); + throw new OpenDiablo2Exception("Signature expected to be 0x74 but it is not."); Version = bm.GetByte(); NumberOfDirections = bm.GetByte(); FramesPerDirection = bm.GetInt32(); if (bm.GetInt32() != 1) - throw new ApplicationException("This value isn't 1. It has to be 1."); + throw new OpenDiablo2Exception("This value isn't 1. It has to be 1."); var totalSizeCoded = bm.GetInt32(); var directionOffsets = new int[NumberOfDirections]; diff --git a/OpenDiablo2.Common/Models/MPQDT1.cs b/OpenDiablo2.Common/Models/MPQDT1.cs index 8ad463b3..118007a0 100644 --- a/OpenDiablo2.Common/Models/MPQDT1.cs +++ b/OpenDiablo2.Common/Models/MPQDT1.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using OpenDiablo2.Common.Exceptions; namespace OpenDiablo2.Common.Models { @@ -136,7 +137,7 @@ namespace OpenDiablo2.Common.Models { // 3D isometric block if (block.Length != 256) - throw new ApplicationException($"Expected exactly 256 bytes of data, but got {block.Length} instead!"); + throw new OpenDiablo2Exception($"Expected exactly 256 bytes of data, but got {block.Length} instead!"); int x = 0; int y = 0; diff --git a/OpenDiablo2.Common/Models/MPQFont.cs b/OpenDiablo2.Common/Models/MPQFont.cs index 76f0a77b..4f0cbfc3 100644 --- a/OpenDiablo2.Common/Models/MPQFont.cs +++ b/OpenDiablo2.Common/Models/MPQFont.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Text; +using OpenDiablo2.Common.Exceptions; namespace OpenDiablo2.Common.Models { @@ -37,7 +38,7 @@ namespace OpenDiablo2.Common.Models var br = new BinaryReader(tableStream); var wooCheck = Encoding.UTF8.GetString(br.ReadBytes(4)); if (wooCheck != "Woo!") - throw new ApplicationException("Error loading font. Missing the Woo!"); + throw new OpenDiablo2Exception("Error loading font. Missing the Woo!"); br.ReadBytes(8); while (tableStream.Position < tableStream.Length) diff --git a/OpenDiablo2.Common/Models/MPQHuffman.cs b/OpenDiablo2.Common/Models/MPQHuffman.cs index 0670ff96..71a335a5 100644 --- a/OpenDiablo2.Common/Models/MPQHuffman.cs +++ b/OpenDiablo2.Common/Models/MPQHuffman.cs @@ -29,6 +29,7 @@ using System; using System.IO; using System.Collections; +using OpenDiablo2.Common.Exceptions; namespace OpenDiablo2.Common.Models { @@ -265,7 +266,7 @@ namespace OpenDiablo2.Common.Models { int bit = input.ReadBits(1); if (bit == -1) - throw new Exception("Unexpected end of file"); + throw new OpenDiablo2Exception("Unexpected end of file"); node = bit == 0 ? node.Child0 : node.Child1; } @@ -384,6 +385,9 @@ namespace OpenDiablo2.Common.Models current.Next.Prev = current.Prev; // insert current after prev + if (prev == null) + throw new OpenDiablo2Exception("Previous frame not defined!"); + LinkedNode temp = prev.Next; current.Next = temp; current.Prev = prev; diff --git a/OpenDiablo2.Common/Models/MPQStream.cs b/OpenDiablo2.Common/Models/MPQStream.cs index 7bfed2e2..d3ea7553 100644 --- a/OpenDiablo2.Common/Models/MPQStream.cs +++ b/OpenDiablo2.Common/Models/MPQStream.cs @@ -1,4 +1,5 @@ using ICSharpCode.SharpZipLib.Zip.Compression.Streams; +using OpenDiablo2.Common.Exceptions; using System; using System.Collections.Generic; using System.IO; @@ -53,9 +54,9 @@ namespace OpenDiablo2.Common.Models MPQ.DecryptBlock(blockPositions, blockRecord.EncryptionSeed - 1); if (blockPositions[0] != blockpossize) - throw new ApplicationException("Decryption failed"); + throw new OpenDiablo2Exception("Decryption failed"); if (blockPositions[1] > blockSize + blockpossize) - throw new ApplicationException("Decryption failed"); + throw new OpenDiablo2Exception("Decryption failed"); } } @@ -120,7 +121,7 @@ namespace OpenDiablo2.Common.Models mpq.fileStream.Seek(mpq.Header.HeaderSize + blockRecord.BlockOffset, SeekOrigin.Begin); int read = mpq.fileStream.Read(filedata, 0, filedata.Length); if (read != filedata.Length) - throw new ApplicationException("Insufficient data or invalid data length"); + throw new OpenDiablo2Exception("Insufficient data or invalid data length"); } if (blockSize == blockRecord.FileSize) @@ -204,13 +205,13 @@ namespace OpenDiablo2.Common.Models mpq.fileStream.Seek(offset, SeekOrigin.Begin); int read = mpq.fileStream.Read(data, 0, toread); if (read != toread) - throw new ApplicationException("Insufficient data or invalid data length"); + throw new OpenDiablo2Exception("Insufficient data or invalid data length"); } if (blockRecord.IsEncrypted && blockRecord.FileSize > 3) { if (blockRecord.EncryptionSeed == 0) - throw new ApplicationException("Unable to determine encryption key"); + throw new OpenDiablo2Exception("Unable to determine encryption key"); encryptionseed = (uint)(blockIndex + blockRecord.EncryptionSeed); MPQ.DecryptBlock(data, encryptionseed); @@ -250,14 +251,14 @@ namespace OpenDiablo2.Common.Models return MpqWavCompression.Decompress(sinput, 1); case 0x12: - throw new ApplicationException("LZMA compression is not yet supported"); + throw new OpenDiablo2Exception("LZMA compression is not yet supported"); // Combos case 0x22: // TODO: sparse then zlib - throw new ApplicationException("Sparse compression + Deflate compression is not yet supported"); + throw new OpenDiablo2Exception("Sparse compression + Deflate compression is not yet supported"); case 0x30: // TODO: sparse then bzip2 - throw new ApplicationException("Sparse compression + BZip2 compression is not yet supported"); + throw new OpenDiablo2Exception("Sparse compression + BZip2 compression is not yet supported"); case 0x41: sinput = MpqHuffman.Decompress(sinput); return MpqWavCompression.Decompress(sinput, 1); @@ -275,7 +276,7 @@ namespace OpenDiablo2.Common.Models return MpqWavCompression.Decompress(new MemoryStream(result), 2); } default: - throw new ApplicationException("Compression is not yet supported: 0x" + comptype.ToString("X")); + throw new OpenDiablo2Exception("Compression is not yet supported: 0x" + comptype.ToString("X")); } } diff --git a/OpenDiablo2.Common/Models/Mobs/LevelExperienceConfig.cs b/OpenDiablo2.Common/Models/Mobs/LevelExperienceConfig.cs index f547fd74..2ba2ef55 100644 --- a/OpenDiablo2.Common/Models/Mobs/LevelExperienceConfig.cs +++ b/OpenDiablo2.Common/Models/Mobs/LevelExperienceConfig.cs @@ -1,4 +1,5 @@ using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces.Mobs; using System; using System.Collections.Generic; @@ -57,7 +58,7 @@ namespace OpenDiablo2.Common.Models.Mobs { if (!long.TryParse(data[o][i], out long exp)) { - throw new Exception("Could not parse experience number '" + data[o][i] + "'."); + throw new OpenDiablo2Exception("Could not parse experience number '" + data[o][i] + "'."); } expperlevel.Add(exp); } diff --git a/OpenDiablo2.Common/OpenDiablo2.Common.csproj b/OpenDiablo2.Common/OpenDiablo2.Common.csproj index b79410aa..de188ffd 100644 --- a/OpenDiablo2.Common/OpenDiablo2.Common.csproj +++ b/OpenDiablo2.Common/OpenDiablo2.Common.csproj @@ -80,6 +80,7 @@ + diff --git a/OpenDiablo2.Common/StringUtils.cs b/OpenDiablo2.Common/StringUtils.cs index 67956e9d..77e7deeb 100644 --- a/OpenDiablo2.Common/StringUtils.cs +++ b/OpenDiablo2.Common/StringUtils.cs @@ -3,7 +3,7 @@ using System.Text; namespace OpenDiablo2.Common { - public class StringUtils + public static class StringUtils { public static List SplitIntoLinesWithMaxWidth(string fullSentence, int maxChars) { diff --git a/OpenDiablo2.Core/GameState/GameState.cs b/OpenDiablo2.Core/GameState/GameState.cs index faf0faa4..cba1122f 100644 --- a/OpenDiablo2.Core/GameState/GameState.cs +++ b/OpenDiablo2.Core/GameState/GameState.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.Linq; using OpenDiablo2.Common; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.Common.Models; using OpenDiablo2.Core.Map_Engine; @@ -210,7 +211,7 @@ namespace OpenDiablo2.Core.GameState_ .Where(x => x != null); default: - throw new ApplicationException("Unknown render cell type!"); + throw new OpenDiablo2Exception("Unknown render cell type!"); } } @@ -353,7 +354,7 @@ namespace OpenDiablo2.Core.GameState_ { #if DEBUG if (!tiles.All(x => x.Animated)) - throw new ApplicationException("Some tiles are animated and some aren't..."); + throw new OpenDiablo2Exception("Some tiles are animated and some aren't..."); #endif var frameIndex = (int)Math.Floor(tiles.Count() * animationTime); tile = tiles.ElementAt(frameIndex); @@ -377,7 +378,7 @@ namespace OpenDiablo2.Core.GameState_ } if (tile.Animated) - throw new ApplicationException("Why are we randomly finding an animated tile? Something's wrong here."); + throw new OpenDiablo2Exception("Why are we randomly finding an animated tile? Something's wrong here."); } else tile = tiles.First(); } diff --git a/OpenDiablo2.Core/GameState/MobManager.cs b/OpenDiablo2.Core/GameState/MobManager.cs index af3c8858..e236f76e 100644 --- a/OpenDiablo2.Core/GameState/MobManager.cs +++ b/OpenDiablo2.Core/GameState/MobManager.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces.Mobs; using OpenDiablo2.Common.Models.Mobs; @@ -35,7 +36,7 @@ namespace OpenDiablo2.Core.GameState_ { Mobs.Add(mob); if (IdsUsed.Contains(mob.Id)) - throw new ApplicationException("Tried to insert an existing mob id!"); + throw new OpenDiablo2Exception("Tried to insert an existing mob id!"); IdsUsed.Add(mob.Id); } public void RemoveMob(MobState mob) @@ -49,7 +50,7 @@ namespace OpenDiablo2.Core.GameState_ if (!IdsUsed.Contains(i)) return i; - throw new ApplicationException("Ran out of IDs. How did this even happen?!"); + throw new OpenDiablo2Exception("Ran out of IDs. How did this even happen?!"); } #endregion Mob Controls diff --git a/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs b/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs index d00a8a98..1193297d 100644 --- a/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs +++ b/OpenDiablo2.SDL2/SDL2CharacterRenderer.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.Linq; using OpenDiablo2.Common; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.Common.Interfaces.Drawing; using OpenDiablo2.Common.Models; @@ -134,7 +135,7 @@ namespace OpenDiablo2.SDL2_ animationData = resourceManager.GetPlayerAnimation(Hero, WeaponClass, MobMode); if (animationData == null) - throw new ApplicationException("Could not locate animation for the character!"); + throw new OpenDiablo2Exception("Could not locate animation for the character!"); var palette = paletteProvider.PaletteTable["Units"]; CacheFrames(animationData.Layers.Select(layer => resourceManager.GetPlayerDCC(layer, ArmorType, palette))); @@ -221,7 +222,7 @@ namespace OpenDiablo2.SDL2_ var offsetX = x + cell.XOffset + (frame.Box.X - minX); var offsetY = y + cell.YOffset + (frame.Box.Y - minY); if (offsetX < 0 || offsetX > frameW || offsetY < 0 || offsetY > frameH) - throw new ApplicationException("There is nothing we can do now."); + throw new OpenDiablo2Exception("There is nothing we can do now."); data[offsetX + (offsetY * (pitch / 4))] = color; } diff --git a/OpenDiablo2.SDL2/SDL2Label.cs b/OpenDiablo2.SDL2/SDL2Label.cs index ae69b60b..02cfd48f 100644 --- a/OpenDiablo2.SDL2/SDL2Label.cs +++ b/OpenDiablo2.SDL2/SDL2Label.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using System.Drawing; using System.Linq; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using SDL2; @@ -85,7 +86,7 @@ namespace OpenDiablo2.SDL2_ } if (MaxWidth < (font.sprite.FrameSize.Width)) - throw new ApplicationException("Max label width cannot be smaller than a single character."); + throw new OpenDiablo2Exception("Max label width cannot be smaller than a single character."); var lastWordIndex = 0; var width = 0; @@ -130,7 +131,7 @@ namespace OpenDiablo2.SDL2_ texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_TARGET, textureSize.Width, textureSize.Height); if (texture == IntPtr.Zero) - throw new ApplicationException("Unaple to initialize texture."); + throw new OpenDiablo2Exception("Unaple to initialize texture."); SDL.SDL_SetTextureBlendMode(texture, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND); SDL.SDL_SetRenderTarget(renderer, texture); diff --git a/OpenDiablo2.SDL2/SDL2RenderWindow.cs b/OpenDiablo2.SDL2/SDL2RenderWindow.cs index fdac7a3c..785c844d 100644 --- a/OpenDiablo2.SDL2/SDL2RenderWindow.cs +++ b/OpenDiablo2.SDL2/SDL2RenderWindow.cs @@ -18,6 +18,7 @@ using System; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.Common.Interfaces.Drawing; using OpenDiablo2.Common.Models; @@ -82,16 +83,16 @@ namespace OpenDiablo2.SDL2_ SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING); if (SDL.SDL_SetHint(SDL.SDL_HINT_RENDER_SCALE_QUALITY, "0") == SDL.SDL_bool.SDL_FALSE) - throw new ApplicationException($"Unable to Init hinting: {SDL.SDL_GetError()}"); + throw new OpenDiablo2Exception($"Unable to Init hinting: {SDL.SDL_GetError()}"); window = SDL.SDL_CreateWindow("OpenDiablo2", SDL.SDL_WINDOWPOS_UNDEFINED, SDL.SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL.SDL_WindowFlags.SDL_WINDOW_SHOWN | (fullscreen ? SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN : 0)); if (window == IntPtr.Zero) - throw new ApplicationException($"Unable to create SDL Window: {SDL.SDL_GetError()}"); + throw new OpenDiablo2Exception($"Unable to create SDL Window: {SDL.SDL_GetError()}"); renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RendererFlags.SDL_RENDERER_ACCELERATED | SDL.SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); if (renderer == IntPtr.Zero) - throw new ApplicationException($"Unable to create SDL Window: {SDL.SDL_GetError()}"); + throw new OpenDiablo2Exception($"Unable to create SDL Window: {SDL.SDL_GetError()}"); SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND); @@ -366,7 +367,7 @@ namespace OpenDiablo2.SDL2_ SDL.SDL_SetTextureBlendMode(texId, SDL.SDL_BlendMode.SDL_BLENDMODE_BLEND); if (SDL.SDL_LockTexture(texId, IntPtr.Zero, out IntPtr pixels, out int pitch) != 0) - throw new ApplicationException("Could not lock texture for map rendering"); + throw new OpenDiablo2Exception("Could not lock texture for map rendering"); try { @@ -470,7 +471,7 @@ namespace OpenDiablo2.SDL2_ var cursor = SDL.SDL_CreateColorCursor(surface, hotspot.X * multiple, hotspot.Y * multiple); if (cursor == IntPtr.Zero) - throw new ApplicationException($"Unable to set the cursor cursor: {SDL.SDL_GetError()}"); // TODO: Is this supported everywhere? May need to still support software cursors. + throw new OpenDiablo2Exception($"Unable to set the cursor cursor: {SDL.SDL_GetError()}"); // TODO: Is this supported everywhere? May need to still support software cursors. return new SDL2MouseCursor { HWSurface = cursor }; } diff --git a/OpenDiablo2.SDL2/SDL2Sprite.cs b/OpenDiablo2.SDL2/SDL2Sprite.cs index d50f40da..14078568 100644 --- a/OpenDiablo2.SDL2/SDL2Sprite.cs +++ b/OpenDiablo2.SDL2/SDL2Sprite.cs @@ -17,6 +17,7 @@ using System; using System.Drawing; using System.Linq; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.Common.Models; using SDL2; @@ -113,7 +114,7 @@ namespace OpenDiablo2.SDL2_ texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_ARGB8888, (int)SDL.SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, Pow2(FrameSize.Width), Pow2(FrameSize.Height)); if (texture == IntPtr.Zero) - throw new ApplicationException("Unaple to initialize texture."); + throw new OpenDiablo2Exception("Unaple to initialize texture."); Frame = 0; } diff --git a/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs b/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs index 9cb4afbe..553beef3 100644 --- a/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs +++ b/OpenDiablo2.ServiceBus/Message Frames/Client/MFJoinGame.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using OpenDiablo2.Common.Attributes; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; namespace OpenDiablo2.ServiceBus.Message_Frames.Client @@ -30,7 +31,7 @@ namespace OpenDiablo2.ServiceBus.Message_Frames.Client PlayerName = Encoding.UTF8.GetString(value, 3, value.Length - 3); if (PlayerName.Length != playerNameLen) - throw new ApplicationException("Invalid player length!"); + throw new OpenDiablo2Exception("Invalid player length!"); } } diff --git a/OpenDiablo2.ServiceBus/SessionManager.cs b/OpenDiablo2.ServiceBus/SessionManager.cs index 60eaf1b3..a0f8129a 100644 --- a/OpenDiablo2.ServiceBus/SessionManager.cs +++ b/OpenDiablo2.ServiceBus/SessionManager.cs @@ -22,6 +22,7 @@ using NetMQ; using NetMQ.Sockets; using OpenDiablo2.Common.Attributes; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.ServiceBus.Message_Frames.Client; using OpenDiablo2.ServiceBus.Message_Frames.Server; @@ -89,7 +90,7 @@ namespace OpenDiablo2.ServiceBus case eSessionType.Server: case eSessionType.Remote: default: - throw new ApplicationException("This session type is currently unsupported."); + throw new OpenDiablo2Exception("This session type is currently unsupported."); } running = true; @@ -125,7 +126,7 @@ namespace OpenDiablo2.ServiceBus private void ProcessMessageFrame() where T : IMessageFrame, new() { if (!running) - throw new ApplicationException("You have made a terrible mistake. Cannot get a message frame if you are not connected."); + throw new OpenDiablo2Exception("You have made a terrible mistake. Cannot get a message frame if you are not connected."); var bytes = requestSocket.ReceiveFrameBytes(); var frameType = (eMessageFrameType)bytes[0]; @@ -133,7 +134,7 @@ namespace OpenDiablo2.ServiceBus var frameData = bytes.Skip(1).ToArray(); // TODO: Can we maybe use pointers? This seems wasteful var messageFrame = getMessageFrame(frameType); if (messageFrame.GetType() != typeof(T)) - throw new ApplicationException("Recieved unexpected message frame!"); + throw new OpenDiablo2Exception("Recieved unexpected message frame!"); messageFrame.Data = frameData; lock (getGameState().ThreadLocker) { @@ -145,7 +146,7 @@ namespace OpenDiablo2.ServiceBus { var bytes = requestSocket.ReceiveFrameBytes(); if ((eMessageFrameType)bytes[0] != eMessageFrameType.None) - throw new ApplicationException("Excepted a NoOp but got a command instead!"); + throw new OpenDiablo2Exception("Excepted a NoOp but got a command instead!"); } public void JoinGame(string playerName, eHero heroType) diff --git a/OpenDiablo2.ServiceBus/SessionServer.cs b/OpenDiablo2.ServiceBus/SessionServer.cs index c20b4742..8b59b6ef 100644 --- a/OpenDiablo2.ServiceBus/SessionServer.cs +++ b/OpenDiablo2.ServiceBus/SessionServer.cs @@ -22,6 +22,7 @@ using NetMQ; using NetMQ.Sockets; using OpenDiablo2.Common.Attributes; using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Exceptions; using OpenDiablo2.Common.Interfaces; using OpenDiablo2.Common.Models; using OpenDiablo2.ServiceBus.Message_Frames.Server; @@ -84,7 +85,7 @@ namespace OpenDiablo2.ServiceBus case eSessionType.Server: case eSessionType.Remote: default: - throw new ApplicationException("This session type is currently unsupported."); + throw new OpenDiablo2Exception("This session type is currently unsupported."); } OnJoinGame += OnJoinGameHandler; diff --git a/OpenDiablo2.TestConsole/Program.cs b/OpenDiablo2.TestConsole/Program.cs index 64b3732c..bace4f4c 100644 --- a/OpenDiablo2.TestConsole/Program.cs +++ b/OpenDiablo2.TestConsole/Program.cs @@ -1,16 +1,13 @@ -using OpenDiablo2.Common.Enums; -using OpenDiablo2.Common.Models; -using OpenDiablo2.Core; -using System; +using System; using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using OpenDiablo2.Common.Enums; +using OpenDiablo2.Common.Models; +using OpenDiablo2.Core; namespace OpenDiablo2.TestConsole { - class Program + static class Program { private static GlobalConfiguration GlobalConfig = null; private static MPQProvider MPQProv = null;