├── .gitignore ├── LICENCE.txt ├── OpenTibia ├── OpenTibia.Animation │ ├── Animator.cs │ ├── FrameDuration.cs │ └── FrameGroup.cs ├── OpenTibia.Client │ ├── ClientImpl.cs │ ├── IClient.cs │ ├── Sprites │ │ ├── Sprite.cs │ │ ├── SpriteListChangedArgs.cs │ │ ├── SpriteSheet.cs │ │ └── SpriteStorage.cs │ └── Things │ │ ├── ClothSlot.cs │ │ ├── DatFlags.cs │ │ ├── DatFormat.cs │ │ ├── MarketCategory.cs │ │ ├── ThingListChangedArgs.cs │ │ ├── ThingType.cs │ │ ├── ThingTypeSerializer.cs │ │ └── ThingTypeStorage.cs ├── OpenTibia.Collections │ └── SpriteGroup.cs ├── OpenTibia.Controls │ ├── ColorChangedArgs.cs │ ├── EightBitColorGrid.cs │ ├── HsiColorGrid.cs │ ├── SpriteListBox.cs │ └── ThingTypeListBox.cs ├── OpenTibia.Core │ ├── IStorage.cs │ ├── Version.cs │ ├── VersionListChangedArgs.cs │ └── VersionStorage.cs ├── OpenTibia.Geom │ ├── Direction.cs │ ├── Position.cs │ └── Rect.cs ├── OpenTibia.IO │ ├── BinaryTreeReader.cs │ ├── BinaryTreeWriter.cs │ ├── FlagsWriter.cs │ └── SpecialChar.cs ├── OpenTibia.Obd │ ├── ObdDecoder.cs │ ├── ObdEncoder.cs │ ├── ObdFlags.cs │ ├── ObdVersion.cs │ └── ObjectData.cs ├── OpenTibia.Utils │ ├── BitmapLocker.cs │ ├── Clock.cs │ ├── ColorUtils.cs │ ├── LZMACoder.cs │ ├── OutfitData.cs │ ├── PathUtils.cs │ ├── PropertySorter.cs │ └── SpriteCache.cs ├── OpenTibia.csproj ├── OpenTibia.sln ├── Properties │ └── AssemblyInfo.cs └── Settings.StyleCop ├── README.md └── ThirdParty └── 7zip ├── 7zip.csproj ├── Common ├── CRC.cs ├── CommandLineParser.cs ├── InBuffer.cs └── OutBuffer.cs ├── Compress ├── LZ │ ├── IMatchFinder.cs │ ├── LzBinTree.cs │ ├── LzInWindow.cs │ └── LzOutWindow.cs ├── LZMA │ ├── LzmaBase.cs │ ├── LzmaDecoder.cs │ └── LzmaEncoder.cs └── RangeCoder │ ├── RangeCoder.cs │ ├── RangeCoderBit.cs │ └── RangeCoderBitTree.cs └── ICoder.cs /.gitignore: -------------------------------------------------------------------------------- 1 | # Scripts 2 | release/ 3 | debug/ 4 | packages/ 5 | 6 | # Visual studio intermediate directories 7 | bin/ 8 | obj/ 9 | *.suo 10 | *.csproj.user 11 | 12 | # StyleCop 13 | stylecop.* 14 | *cache 15 | *.lock 16 | *.ide 17 | *.ide-shm 18 | *.ide-wal 19 | -------------------------------------------------------------------------------- /LICENCE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2015 Open Tibia Tools 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Animation/Animator.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Utils; 27 | using System; 28 | #endregion 29 | 30 | namespace OpenTibia.Animation 31 | { 32 | public enum AnimationMode : byte 33 | { 34 | Asynchronous = 0, 35 | Synchronous = 1 36 | } 37 | 38 | public enum FrameMode : short 39 | { 40 | Automatic = -1, 41 | Random = 0xFE, 42 | Asynchronous = 0xFF 43 | } 44 | 45 | public enum AnimationDirection : byte 46 | { 47 | Forward = 0, 48 | Backward = 1 49 | } 50 | 51 | public class Animator 52 | { 53 | #region Private Properties 54 | 55 | private static readonly Random Random = new Random(); 56 | 57 | private int frames; 58 | private int startFrame; 59 | private int loopCount; 60 | private AnimationMode mode; 61 | private FrameDuration[] durations; 62 | private long lastTime; 63 | private int currentFrameDuration; 64 | private int currentFrame; 65 | private int currentLoop; 66 | private AnimationDirection currentDirection; 67 | 68 | #endregion 69 | 70 | #region Constructor 71 | 72 | public Animator(int frames, int startFrame, int loopCount, AnimationMode mode, FrameDuration[] durations) 73 | { 74 | this.frames = frames; 75 | this.startFrame = startFrame; 76 | this.loopCount = loopCount; 77 | this.mode = mode; 78 | this.durations = durations; 79 | this.Frame = (int)FrameMode.Automatic; 80 | } 81 | 82 | public Animator(FrameGroup frameGroup) 83 | { 84 | this.frames = frameGroup.Frames; 85 | this.startFrame = frameGroup.StartFrame; 86 | this.loopCount = frameGroup.LoopCount; 87 | this.mode = frameGroup.AnimationMode; 88 | this.durations = frameGroup.FrameDurations; 89 | this.Frame = (int)FrameMode.Automatic; 90 | } 91 | 92 | #endregion 93 | 94 | #region Public Properties 95 | 96 | public int Frame 97 | { 98 | get 99 | { 100 | return this.currentFrame; 101 | } 102 | 103 | set 104 | { 105 | if (this.currentFrame != value) 106 | { 107 | if (this.mode == AnimationMode.Asynchronous) 108 | { 109 | if (value == (ushort)FrameMode.Asynchronous) 110 | { 111 | this.currentFrame = 0; 112 | } 113 | else if (value == (ushort)FrameMode.Random) 114 | { 115 | this.currentFrame = Random.Next(0, this.frames); 116 | } 117 | else if (value >= 0 && value < this.frames) 118 | { 119 | this.currentFrame = value; 120 | } 121 | else 122 | { 123 | this.currentFrame = this.GetStartFrame(); 124 | } 125 | 126 | this.IsComplete = false; 127 | this.lastTime = Clock.ElapsedMilliseconds; 128 | this.currentFrameDuration = this.durations[this.currentFrame].Duration; 129 | } 130 | else 131 | { 132 | this.CalculateSynchronous(); 133 | } 134 | } 135 | } 136 | } 137 | 138 | public bool IsComplete { get; private set; } 139 | 140 | #endregion 141 | 142 | #region Public Methods 143 | 144 | public void Update(long timestamp) 145 | { 146 | if (timestamp != this.lastTime && !this.IsComplete) 147 | { 148 | int elapsed = (int)(timestamp - this.lastTime); 149 | if (elapsed >= this.currentFrameDuration) 150 | { 151 | int frame = loopCount < 0 ? this.GetPingPongFrame() : this.GetLoopFrame(); 152 | if (this.currentFrame != frame) 153 | { 154 | int duration = this.durations[frame].Duration - (elapsed - this.currentFrameDuration); 155 | if (duration < 0 && this.mode == AnimationMode.Synchronous) 156 | { 157 | this.CalculateSynchronous(); 158 | } 159 | else 160 | { 161 | this.currentFrame = frame; 162 | this.currentFrameDuration = duration < 0 ? 0 : duration; 163 | } 164 | } 165 | else 166 | { 167 | this.IsComplete = true; 168 | } 169 | } 170 | else 171 | { 172 | this.currentFrameDuration = this.currentFrameDuration - elapsed; 173 | } 174 | 175 | this.lastTime = timestamp; 176 | } 177 | } 178 | 179 | public int GetStartFrame() 180 | { 181 | if (this.startFrame > -1) 182 | { 183 | return this.startFrame; 184 | } 185 | 186 | return Random.Next(0, this.frames); 187 | } 188 | 189 | #endregion 190 | 191 | #region Private Methods 192 | 193 | private int GetLoopFrame() 194 | { 195 | int nextFrame = (this.currentFrame + 1); 196 | if (nextFrame < this.frames) 197 | { 198 | return nextFrame; 199 | } 200 | 201 | if (this.loopCount == 0) 202 | { 203 | return 0; 204 | } 205 | 206 | if (this.currentLoop < (loopCount - 1)) 207 | { 208 | this.currentLoop++; 209 | return 0; 210 | } 211 | 212 | return this.currentFrame; 213 | } 214 | 215 | private int GetPingPongFrame() 216 | { 217 | int count = this.currentDirection == AnimationDirection.Forward ? 1 : -1; 218 | int nextFrame = this.currentFrame + count; 219 | if (this.currentFrame + count < 0 || nextFrame >= frames) 220 | { 221 | this.currentDirection = this.currentDirection == AnimationDirection.Forward ? AnimationDirection.Backward : AnimationDirection.Forward; 222 | count *= -1; 223 | } 224 | 225 | return this.currentFrame + count; 226 | } 227 | 228 | private void CalculateSynchronous() 229 | { 230 | int totalDuration = 0; 231 | for (int i = 0; i < this.frames; i++) 232 | { 233 | totalDuration += durations[i].Duration; 234 | } 235 | 236 | long time = Clock.ElapsedMilliseconds; 237 | long elapsed = time % totalDuration; 238 | long totalTime = 0; 239 | 240 | for (int i = 0; i < this.frames; i++) 241 | { 242 | long duration = this.durations[i].Duration; 243 | if (elapsed >= totalTime && elapsed < totalTime + duration) 244 | { 245 | this.currentFrame = i; 246 | long timeDiff = elapsed - totalTime; 247 | this.currentFrameDuration = (int)(duration - timeDiff); 248 | break; 249 | } 250 | 251 | totalTime += duration; 252 | } 253 | 254 | this.lastTime = time; 255 | } 256 | 257 | #endregion 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Animation/FrameDuration.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client.Things; 27 | using System; 28 | #endregion 29 | 30 | namespace OpenTibia.Animation 31 | { 32 | public class FrameDuration 33 | { 34 | #region | Private Properties | 35 | 36 | private static readonly Random Random = new Random(); 37 | 38 | #endregion 39 | 40 | #region | Constructor | 41 | 42 | public FrameDuration(int minimum, int maximum) 43 | { 44 | this.SetTo(minimum, maximum); 45 | } 46 | 47 | public FrameDuration(uint minimum, uint maximum) 48 | { 49 | this.SetTo((int)minimum, (int)maximum); 50 | } 51 | 52 | public FrameDuration(ThingCategory category) 53 | { 54 | switch (category) 55 | { 56 | case ThingCategory.Item: 57 | this.SetTo(500, 500); 58 | break; 59 | case ThingCategory.Outfit: 60 | this.SetTo(300, 300); 61 | break; 62 | 63 | case ThingCategory.Effect: 64 | this.SetTo(100, 100); 65 | break; 66 | default: 67 | this.SetTo(0, 0); 68 | break; 69 | } 70 | } 71 | 72 | #endregion 73 | 74 | #region | Public Properties | 75 | 76 | public int Minimum { get; private set; } 77 | 78 | public int Maximum { get; private set; } 79 | 80 | public int Duration 81 | { 82 | get 83 | { 84 | if (this.Minimum == this.Maximum) 85 | { 86 | return this.Minimum; 87 | } 88 | 89 | return (this.Minimum + Random.Next(0, this.Maximum - this.Minimum)); 90 | } 91 | } 92 | 93 | #endregion 94 | 95 | #region | Public Methods | 96 | 97 | public FrameDuration SetTo(int minimum, int maximum) 98 | { 99 | if (minimum > maximum) 100 | { 101 | throw new ArgumentException("The minimum value may not be greater than the maximum value."); 102 | } 103 | 104 | this.Minimum = minimum; 105 | this.Maximum = maximum; 106 | return this; 107 | } 108 | 109 | public FrameDuration CopyFrom(FrameDuration fd) 110 | { 111 | return this.SetTo(fd.Minimum, fd.Maximum); 112 | } 113 | 114 | public FrameDuration CopyTo(FrameDuration fd) 115 | { 116 | return fd.SetTo(this.Minimum, this.Maximum); 117 | } 118 | 119 | public FrameDuration Clone() 120 | { 121 | return new FrameDuration(this.Minimum, this.Maximum); 122 | } 123 | 124 | #endregion 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Animation/FrameGroup.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Animation 26 | { 27 | public enum FrameGroupType : byte 28 | { 29 | Default = 0, 30 | Walking = 1 31 | } 32 | 33 | public class FrameGroup 34 | { 35 | #region Public Properties 36 | 37 | public byte Width { get; set; } 38 | 39 | public byte Height { get; set; } 40 | 41 | public byte ExactSize { get; set; } 42 | 43 | public byte Layers { get; set; } 44 | 45 | public byte PatternX { get; set; } 46 | 47 | public byte PatternY { get; set; } 48 | 49 | public byte PatternZ { get; set; } 50 | 51 | public byte Frames { get; set; } 52 | 53 | public uint[] SpriteIDs { get; set; } 54 | 55 | public bool IsAnimation { get; set; } 56 | 57 | public AnimationMode AnimationMode { get; set; } 58 | 59 | public int LoopCount { get; set; } 60 | 61 | public sbyte StartFrame { get; set; } 62 | 63 | public FrameDuration[] FrameDurations { get; set; } 64 | 65 | #endregion 66 | 67 | #region Public Methods 68 | 69 | public int GetTotalSprites() 70 | { 71 | return this.Width * this.Height * this.PatternX * this.PatternY * this.PatternZ * this.Frames * this.Layers; 72 | } 73 | 74 | public int GetSpriteIndex(int width, int height, int layers, int patternX, int patternY, int patternZ, int frames) 75 | { 76 | return ((((((frames % this.Frames) * this.PatternZ + patternZ) * this.PatternY + patternY) * this.PatternX + patternX) * this.Layers + layers) * this.Height + height) * this.Width + width; 77 | } 78 | 79 | public int GetTextureIndex(int layer, int patternX, int patternY, int patternZ, int frame) 80 | { 81 | return (((frame % this.Frames * this.PatternZ + patternZ) * this.PatternY + patternY) * this.PatternX + patternX) * this.Layers + layer; 82 | } 83 | 84 | public FrameGroup Clone() 85 | { 86 | FrameGroup group = new FrameGroup(); 87 | group.Width = this.Width; 88 | group.Height = this.Height; 89 | group.Layers = this.Layers; 90 | group.Frames = this.Frames; 91 | group.PatternX = this.PatternX; 92 | group.PatternY = this.PatternY; 93 | group.PatternZ = this.PatternZ; 94 | group.ExactSize = this.ExactSize; 95 | group.SpriteIDs = (uint[])this.SpriteIDs.Clone(); 96 | group.AnimationMode = this.AnimationMode; 97 | group.LoopCount = this.LoopCount; 98 | group.StartFrame = this.StartFrame; 99 | 100 | if (this.Frames > 1) 101 | { 102 | group.IsAnimation = true; 103 | group.FrameDurations = new FrameDuration[this.Frames]; 104 | 105 | for (int i = 0; i < this.Frames; i++) 106 | { 107 | group.FrameDurations[i] = this.FrameDurations[i].Clone(); 108 | } 109 | } 110 | 111 | return group; 112 | } 113 | 114 | #endregion 115 | 116 | #region Public Static Methods 117 | 118 | public static FrameGroup Create() 119 | { 120 | FrameGroup group = new FrameGroup(); 121 | group.Width = 1; 122 | group.Height = 1; 123 | group.Layers = 1; 124 | group.Frames = 1; 125 | group.PatternX = 1; 126 | group.PatternY = 1; 127 | group.PatternZ = 1; 128 | group.ExactSize = 32; 129 | group.SpriteIDs = new uint[1]; 130 | group.IsAnimation = false; 131 | group.AnimationMode = AnimationMode.Asynchronous; 132 | group.LoopCount = 0; 133 | group.StartFrame = 0; 134 | group.FrameDurations = null; 135 | return group; 136 | } 137 | 138 | #endregion 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/IClient.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Animation; 27 | using OpenTibia.Client.Sprites; 28 | using OpenTibia.Client.Things; 29 | using OpenTibia.Core; 30 | using OpenTibia.Geom; 31 | using OpenTibia.Obd; 32 | using OpenTibia.Utils; 33 | using System; 34 | using System.Drawing; 35 | #endregion 36 | 37 | namespace OpenTibia.Client 38 | { 39 | [Flags] 40 | public enum ClientFeatures 41 | { 42 | None = 0, 43 | PatternZ = 1 << 0, 44 | Extended = 1 << 1, 45 | FrameDurations = 1 << 2, 46 | FrameGroups = 1 << 3, 47 | Transparency = 1 << 4 48 | } 49 | 50 | public interface IClient : IDisposable 51 | { 52 | #region | Events | 53 | 54 | event EventHandler ClientLoaded; 55 | 56 | event EventHandler ClientChanged; 57 | 58 | event EventHandler ClientCompiled; 59 | 60 | event EventHandler ClientUnloaded; 61 | 62 | event ProgressHandler ProgressChanged; 63 | 64 | #endregion 65 | 66 | #region | Properties | 67 | 68 | ThingTypeStorage Things { get; } 69 | 70 | SpriteStorage Sprites { get; } 71 | 72 | bool Changed { get; } 73 | 74 | bool Loaded { get; } 75 | 76 | #endregion 77 | 78 | #region | Methods | 79 | 80 | bool CreateEmpty(Core.Version version, ClientFeatures features); 81 | 82 | bool CreateEmpty(Core.Version version); 83 | 84 | bool Load(string datPath, string sprPath, Core.Version version, ClientFeatures features); 85 | 86 | bool Load(string datPath, string sprPath, Core.Version version); 87 | 88 | FrameGroup GetFrameGroup(ushort id, ThingCategory category, FrameGroupType groupType); 89 | 90 | ObjectData GetThingData(ushort id, ThingCategory category, bool singleFrameGroup); 91 | 92 | ObjectData GetThingData(ushort id, ThingCategory category); 93 | 94 | Bitmap GetObjectImage(ushort id, ThingCategory category, FrameGroupType groupType); 95 | 96 | Bitmap GetObjectImage(ushort id, ThingCategory category); 97 | 98 | Bitmap GetObjectImage(ushort id, Direction direction, OutfitData data, bool mount); 99 | 100 | Bitmap GetObjectImage(ushort id, Direction direction, OutfitData data); 101 | 102 | Bitmap GetObjectImage(ThingType thing, FrameGroupType groupType); 103 | 104 | Bitmap GetObjectImage(ThingType thing); 105 | 106 | SpriteSheet GetSpriteSheet(ushort id, ThingCategory category, FrameGroupType groupType); 107 | 108 | SpriteSheet GetSpriteSheet(ushort id, ThingCategory category, FrameGroupType groupType, OutfitData outfitData); 109 | 110 | ThingType[] GetAllItems(); 111 | 112 | ThingType[] GetAllOutfits(); 113 | 114 | ThingType[] GetAllEffects(); 115 | 116 | ThingType[] GetAllMissiles(); 117 | 118 | bool Save(string datPath, string sprPath, Core.Version version, ClientFeatures features); 119 | 120 | bool Save(string datPath, string sprPath, Core.Version version); 121 | 122 | bool Save(); 123 | 124 | #endregion 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Sprites/SpriteListChangedArgs.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Core; 27 | #endregion 28 | 29 | namespace OpenTibia.Client.Sprites 30 | { 31 | public delegate void SpriteListChangedHandler(object sender, SpriteListChangedArgs e); 32 | 33 | public class SpriteListChangedArgs 34 | { 35 | #region Constructor 36 | 37 | public SpriteListChangedArgs(Sprite[] changedSprites, StorageChangeType changeType) 38 | { 39 | this.ChangedSprites = changedSprites; 40 | this.ChangeType = changeType; 41 | } 42 | 43 | #endregion 44 | 45 | #region Public Properties 46 | 47 | public Sprite[] ChangedSprites { get; private set; } 48 | 49 | public StorageChangeType ChangeType { get; private set; } 50 | 51 | #endregion 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Sprites/SpriteSheet.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Geom; 27 | using System; 28 | using System.Collections.Generic; 29 | using System.Drawing; 30 | using System.Drawing.Imaging; 31 | #endregion 32 | 33 | namespace OpenTibia.Client.Sprites 34 | { 35 | public class SpriteSheet 36 | { 37 | #region Constructor 38 | 39 | public SpriteSheet(Bitmap bitmap, Dictionary rectList) 40 | { 41 | if (bitmap == null) 42 | { 43 | throw new ArgumentNullException(nameof(bitmap)); 44 | } 45 | 46 | if (rectList == null || rectList.Count == 0) 47 | { 48 | throw new ArgumentNullException(nameof(rectList)); 49 | } 50 | 51 | this.Bitmap = bitmap; 52 | this.RectList = rectList; 53 | } 54 | 55 | #endregion 56 | 57 | #region Public Properties 58 | 59 | public Bitmap Bitmap { get; private set; } 60 | 61 | public Dictionary RectList { get; private set; } 62 | 63 | #endregion 64 | 65 | #region Public Methods 66 | 67 | public bool Save(string path, ImageFormat format) 68 | { 69 | this.Bitmap.Save(path, format); 70 | return true; 71 | } 72 | 73 | public bool Save(string path) 74 | { 75 | this.Bitmap.Save(path, ImageFormat.Png); 76 | return true; 77 | } 78 | 79 | #endregion 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Things/ClothSlot.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Client.Things 26 | { 27 | public enum ClothSlot : ushort 28 | { 29 | None = 0, 30 | TwoHandWeapon = 1, 31 | Helmet = 2, 32 | Amulet = 3, 33 | Backpack = 4, 34 | Armor = 5, 35 | Shield = 6, 36 | OneHandWeapon = 7, 37 | Legs = 8, 38 | Boots = 9, 39 | Ring = 10, 40 | Arrow = 11 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Things/DatFlags.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Client.Things 26 | { 27 | public enum DatFlags1010 : byte 28 | { 29 | Ground = 0x00, 30 | GroundBorder = 0x01, 31 | OnBottom = 0x02, 32 | OnTop = 0x03, 33 | Container = 0x04, 34 | Stackable = 0x05, 35 | ForceUse = 0x06, 36 | MultiUse = 0x07, 37 | Writable = 0x08, 38 | WritableOnce = 0x09, 39 | FluidContainer = 0x0A, 40 | Fluid = 0x0B, 41 | IsUnpassable = 0x0C, 42 | IsUnmovable = 0x0D, 43 | BlockMissiles = 0x0E, 44 | BlockPathfinder = 0x0F, 45 | NoMoveAnimation = 0x10, 46 | Pickupable = 0x11, 47 | Hangable = 0x12, 48 | HookSouth = 0x13, 49 | HookEast = 0x14, 50 | Rotatable = 0x15, 51 | HasLight = 0x16, 52 | DontHide = 0x17, 53 | Translucent = 0x18, 54 | HasOffset = 0x19, 55 | HasElevation = 0x1A, 56 | LyingObject = 0x1B, 57 | AnimateAlways = 0x1C, 58 | Minimap = 0x1D, 59 | LensHelp = 0x1E, 60 | FullGround = 0x1F, 61 | IgnoreLook = 0x20, 62 | Cloth = 0x21, 63 | Market = 0x22, 64 | DefaultAction = 0x23, 65 | Wrappable = 0x24, 66 | Unwrappable = 0x25, 67 | TopEffect = 0x26, 68 | Usable = 0xFE, 69 | 70 | LastFlag = 0xFF 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Things/DatFormat.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | #endregion 28 | 29 | namespace OpenTibia.Client.Things 30 | { 31 | public enum DatFormat : ushort 32 | { 33 | InvalidFormat = 0, 34 | Format_710 = 710, 35 | Format_740 = 740, 36 | Format_755 = 755, 37 | Format_780 = 780, 38 | Format_860 = 860, 39 | Format_960 = 960, 40 | Format_1010 = 1010, 41 | Format_1050 = 1050, 42 | Format_1057 = 1057, 43 | Format_1092 = 1092, 44 | Format_1093 = 1093, 45 | Format_Last = Format_1093 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Things/MarketCategory.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Client.Things 26 | { 27 | public enum MarketCategory : ushort 28 | { 29 | None = 0, 30 | Armors = 1, 31 | Amulets = 2, 32 | Boots = 3, 33 | Containers = 4, 34 | Decoration = 5, 35 | Food = 6, 36 | HelmetsAndHats = 7, 37 | Legs = 8, 38 | Others = 9, 39 | Potions = 10, 40 | Rings = 11, 41 | Runes = 12, 42 | Shields = 13, 43 | Tools = 14, 44 | Valuables = 15, 45 | Ammunition = 16, 46 | Axes = 17, 47 | Clubs = 18, 48 | DistanceWeapons = 19, 49 | Swords = 20, 50 | WandsAndRods = 21, 51 | PremiumScrolls = 22, 52 | MetaWeapons = 255 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Client/Things/ThingListChangedArgs.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Core; 27 | #endregion 28 | 29 | namespace OpenTibia.Client.Things 30 | { 31 | public class ThingListChangedArgs 32 | { 33 | #region | Constructor | 34 | 35 | public ThingListChangedArgs(ThingType[] changedThings, StorageChangeType changeType) 36 | { 37 | this.ChangedThings = changedThings; 38 | this.ChangeType = changeType; 39 | } 40 | 41 | #endregion 42 | 43 | #region | Public Properties | 44 | 45 | public ThingType[] ChangedThings { get; private set; } 46 | 47 | public StorageChangeType ChangeType { get; private set; } 48 | 49 | #endregion 50 | } 51 | 52 | public delegate void ThingListChangedHandler(object sender, ThingListChangedArgs e); 53 | } 54 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Collections/SpriteGroup.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using OpenTibia.Animation; 28 | using OpenTibia.Client.Sprites; 29 | using System.Collections.Generic; 30 | using System.Drawing; 31 | #endregion 32 | 33 | namespace OpenTibia.Collections 34 | { 35 | public class SpriteGroup : Dictionary 36 | { 37 | #region Public Methods 38 | 39 | public Sprite GetSprite(int index, FrameGroupType groupType) 40 | { 41 | if (this.ContainsKey(groupType)) 42 | { 43 | Sprite[] group = this[groupType]; 44 | 45 | if (index >= 0 && index < group.Length) 46 | { 47 | return group[index]; 48 | } 49 | } 50 | 51 | return null; 52 | } 53 | 54 | public Sprite GetSprite(int index) 55 | { 56 | Sprite[] group = this[FrameGroupType.Default]; 57 | 58 | if (index >= 0 && index < group.Length) 59 | { 60 | return group[index]; 61 | } 62 | 63 | return null; 64 | } 65 | 66 | public Bitmap GetSpriteBitmap(int index, FrameGroupType groupType) 67 | { 68 | if (this.ContainsKey(groupType)) 69 | { 70 | Sprite[] group = this[groupType]; 71 | 72 | if (index >= 0 && index < group.Length) 73 | { 74 | return group[index].GetBitmap(); 75 | } 76 | } 77 | 78 | return null; 79 | } 80 | 81 | public Bitmap GetSpriteBitmap(int index) 82 | { 83 | Sprite[] group = this[FrameGroupType.Default]; 84 | 85 | if (index >= 0 && index < group.Length) 86 | { 87 | return group[index].GetBitmap(); 88 | } 89 | 90 | return null; 91 | } 92 | 93 | public void SetSprites(FrameGroupType groupType, Sprite[] sprites) 94 | { 95 | if (sprites == null) 96 | { 97 | throw new ArgumentNullException(nameof(sprites)); 98 | } 99 | 100 | if (this.ContainsKey(groupType)) 101 | { 102 | this[groupType] = sprites; 103 | } 104 | else 105 | { 106 | this.Add(groupType, sprites); 107 | } 108 | } 109 | 110 | public SpriteGroup Clone() 111 | { 112 | SpriteGroup clone = new SpriteGroup(); 113 | 114 | foreach (KeyValuePair item in this) 115 | { 116 | Sprite[] sprites = item.Value; 117 | int length = sprites.Length; 118 | Sprite[] cloneSprites = new Sprite[length]; 119 | 120 | for (int i = 0; i < length; i++) 121 | { 122 | cloneSprites[i] = sprites[i].Clone(); 123 | } 124 | 125 | clone.Add(item.Key, cloneSprites); 126 | } 127 | 128 | return clone; 129 | } 130 | 131 | #endregion 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Controls/ColorChangedArgs.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.Drawing; 28 | #endregion 29 | 30 | namespace OpenTibia.Controls 31 | { 32 | public class ColorChangedArgs 33 | { 34 | #region Constructor 35 | 36 | public ColorChangedArgs(Color oldRgbColor, Color newRgbColor, int oldColor, int newColor) 37 | { 38 | this.OldRgbColor = oldRgbColor; 39 | this.NewRgbColor = newRgbColor; 40 | this.OldColor = oldColor; 41 | this.NewColor = newColor; 42 | } 43 | 44 | #endregion 45 | 46 | #region Public Properties 47 | 48 | public Color OldRgbColor { get; private set; } 49 | 50 | public Color NewRgbColor { get; private set; } 51 | 52 | public int OldColor { get; private set; } 53 | 54 | public int NewColor { get; private set; } 55 | 56 | #endregion 57 | } 58 | 59 | public delegate void ColorChangedHandler(object sender, ColorChangedArgs e); 60 | } 61 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Controls/EightBitColorGrid.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | #endregion 20 | 21 | #region Using Statements 22 | using OpenTibia.Utils; 23 | using System; 24 | using System.Drawing; 25 | using System.Windows.Forms; 26 | #endregion 27 | 28 | namespace OpenTibia.Controls 29 | { 30 | public class EightBitColorGrid : HsiColorGrid 31 | { 32 | #region Constructor 33 | 34 | public EightBitColorGrid() : base() 35 | { 36 | this.columns = 16; 37 | this.rows = 14; 38 | this.length = 215; 39 | } 40 | 41 | #endregion 42 | 43 | #region Protected Methods 44 | 45 | protected override Color GetRgbColor(int color) 46 | { 47 | return ColorUtils.EightBitToRgb(color); 48 | } 49 | 50 | #endregion 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Controls/HsiColorGrid.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | #endregion 20 | 21 | #region Using Statements 22 | using OpenTibia.Utils; 23 | using System; 24 | using System.ComponentModel; 25 | using System.Drawing; 26 | using System.Windows.Forms; 27 | #endregion 28 | 29 | namespace OpenTibia.Controls 30 | { 31 | [DefaultProperty("Color")] 32 | [DefaultEvent("ColorChanged")] 33 | public class HsiColorGrid : Control 34 | { 35 | #region Protected Properties 36 | 37 | protected int columns; 38 | protected int rows; 39 | protected int length; 40 | 41 | private Color lastRgbColor; 42 | private Color rgbColor; 43 | private int overColor; 44 | private int lastColor; 45 | private int color; 46 | private int cellSize; 47 | private int gap; 48 | private Pen pen; 49 | private SolidBrush brush; 50 | private Rectangle bounds; 51 | private ToolTip toolTip; 52 | 53 | #endregion 54 | 55 | #region Constructor 56 | 57 | public HsiColorGrid() 58 | { 59 | this.columns = 19; 60 | this.rows = 7; 61 | this.length = this.columns * this.rows; 62 | this.overColor = 0; 63 | this.lastColor = 0; 64 | this.color = 0; 65 | this.lastRgbColor = System.Drawing.Color.Black; 66 | this.rgbColor = System.Drawing.Color.Black; 67 | this.cellSize = 14; 68 | this.gap = 2; 69 | this.pen = new Pen(System.Drawing.Color.Transparent); 70 | this.brush = new SolidBrush(System.Drawing.Color.Transparent); 71 | this.bounds = new Rectangle(); 72 | this.toolTip = new ToolTip(); 73 | } 74 | 75 | #endregion 76 | 77 | #region Events 78 | 79 | public event ColorChangedHandler ColorChanged; 80 | 81 | #endregion 82 | 83 | #region Public Properties 84 | 85 | public Color RgbColor 86 | { 87 | get 88 | { 89 | return this.rgbColor; 90 | } 91 | } 92 | 93 | public int Color 94 | { 95 | get 96 | { 97 | return this.color; 98 | } 99 | 100 | set 101 | { 102 | if (value < 0 || value > this.length) 103 | { 104 | value = 0; 105 | } 106 | 107 | if (this.color != value) 108 | { 109 | this.lastColor = this.color; 110 | this.lastRgbColor = this.rgbColor; 111 | this.InvalidateColor(this.lastColor); 112 | 113 | this.color = value; 114 | this.rgbColor = this.GetRgbColor(this.color); 115 | this.InvalidateColor(this.color); 116 | this.UpdateToolTip(); 117 | 118 | if (this.ColorChanged != null) 119 | { 120 | this.ColorChanged(this, new ColorChangedArgs(this.lastRgbColor, this.rgbColor, this.lastColor, this.color)); 121 | } 122 | } 123 | } 124 | } 125 | 126 | #endregion 127 | 128 | #region Protected Methods 129 | 130 | protected int HitTest(Point point) 131 | { 132 | if (point.X > this.bounds.Left && point.X < this.bounds.Right && point.Y > this.bounds.Top && point.Y < this.bounds.Bottom) 133 | { 134 | int w = (this.cellSize + this.gap); 135 | int h = (this.cellSize + this.gap); 136 | int column = (int)Math.Floor((double)(point.X / w)); 137 | int row = (int)Math.Floor((double)(point.Y / h)); 138 | return row * this.columns + column; 139 | } 140 | 141 | return -1; 142 | } 143 | 144 | protected virtual void UpdateToolTip() 145 | { 146 | this.toolTip.SetToolTip(this, this.overColor != -1 ? this.overColor.ToString() : null); 147 | } 148 | 149 | protected virtual Color GetRgbColor(int color) 150 | { 151 | return ColorUtils.HsiToRgb(color); 152 | } 153 | 154 | protected void InvalidateOverColor(int color) 155 | { 156 | if (color != this.overColor) 157 | { 158 | this.InvalidateColor(this.overColor); 159 | this.overColor = color; 160 | this.UpdateToolTip(); 161 | this.InvalidateColor(color); 162 | } 163 | } 164 | 165 | protected void InvalidateColor(int Color) 166 | { 167 | int row = (int)(Color / this.columns); 168 | int column = Color - (row * this.columns); 169 | int px = column * (this.cellSize + this.gap); 170 | int py = row * (this.cellSize + this.gap); 171 | Rectangle rect = new Rectangle(px, py, this.cellSize, this.cellSize); 172 | this.Invalidate(rect); 173 | } 174 | 175 | #endregion 176 | 177 | #region Event Handlers 178 | 179 | protected override void OnMouseDown(MouseEventArgs e) 180 | { 181 | base.OnMouseDown(e); 182 | 183 | if (!this.Focused && this.TabStop) 184 | { 185 | this.Focus(); 186 | } 187 | 188 | this.Color = this.HitTest(e.Location); 189 | } 190 | 191 | protected override void OnMouseMove(MouseEventArgs e) 192 | { 193 | base.OnMouseMove(e); 194 | this.InvalidateOverColor(this.HitTest(e.Location)); 195 | } 196 | 197 | protected override void OnMouseLeave(EventArgs e) 198 | { 199 | base.OnMouseLeave(e); 200 | this.InvalidateOverColor(-1); 201 | } 202 | 203 | protected override void OnPaint(PaintEventArgs e) 204 | { 205 | base.OnPaint(e); 206 | 207 | bounds.Width = this.columns * (this.cellSize + this.gap); 208 | bounds.Height = this.rows * (this.cellSize + this.gap); 209 | 210 | int width = this.cellSize + this.gap; 211 | int height = this.cellSize + this.gap; 212 | int color = 0; 213 | 214 | for (int r = 0; r < this.rows; r++) 215 | { 216 | for (int c = 0; c < this.columns; c++) 217 | { 218 | int x = c * width; 219 | int y = r * height; 220 | 221 | this.brush.Color = this.GetRgbColor(color); 222 | e.Graphics.FillRectangle(this.brush, x, y, this.cellSize, this.cellSize); 223 | e.Graphics.Save(); 224 | 225 | if (this.color == color) 226 | { 227 | this.pen.Color = System.Drawing.Color.Black; 228 | e.Graphics.DrawRectangle(this.pen, x, y, this.cellSize - 1, this.cellSize - 1); 229 | e.Graphics.DrawRectangle(this.pen, x + 1, y + 1, this.cellSize - 3, this.cellSize - 3); 230 | 231 | this.pen.Color = System.Drawing.Color.White; 232 | e.Graphics.DrawRectangle(this.pen, x + 2, y + 2, this.cellSize - 5, this.cellSize - 5); 233 | e.Graphics.Save(); 234 | } 235 | else if (this.overColor == color) 236 | { 237 | this.pen.Color = System.Drawing.Color.White; 238 | e.Graphics.DrawRectangle(this.pen, x, y, this.cellSize - 1, this.cellSize - 1); 239 | e.Graphics.Save(); 240 | 241 | this.pen.Color = System.Drawing.Color.Black; 242 | e.Graphics.DrawRectangle(this.pen, x + 1, y + 1, this.cellSize - 3, this.cellSize - 3); 243 | e.Graphics.Save(); 244 | } 245 | else 246 | { 247 | this.pen.Color = System.Drawing.Color.Gray; 248 | e.Graphics.DrawRectangle(this.pen, x, y, this.cellSize - 1, this.cellSize - 1); 249 | e.Graphics.Save(); 250 | } 251 | 252 | color++; 253 | } 254 | } 255 | } 256 | 257 | #endregion 258 | } 259 | } 260 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Controls/SpriteListBox.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client.Sprites; 27 | using System.Drawing; 28 | using System.Windows.Forms; 29 | #endregion 30 | 31 | namespace OpenTibia.Controls 32 | { 33 | public class SpriteListBox : ListBox 34 | { 35 | #region Private Properties 36 | 37 | private const int ItemMargin = 5; 38 | 39 | private Rectangle layoutRect; 40 | private Rectangle destRect; 41 | private Rectangle sourceRect; 42 | private Pen pen; 43 | 44 | #endregion 45 | 46 | #region Constructor 47 | 48 | public SpriteListBox() 49 | { 50 | this.layoutRect = new Rectangle(); 51 | this.destRect = new Rectangle(ItemMargin, 0, Sprite.DefaultSize, Sprite.DefaultSize); 52 | this.sourceRect = new Rectangle(); 53 | this.pen = new Pen(Color.DimGray); 54 | this.MeasureItem += new MeasureItemEventHandler(this.MeasureItemHandler); 55 | this.DrawItem += new DrawItemEventHandler(this.DrawItemHandler); 56 | this.DrawMode = DrawMode.OwnerDrawVariable; 57 | } 58 | 59 | #endregion 60 | 61 | #region Public Methods 62 | 63 | public void Add(Sprite sprite) 64 | { 65 | this.Items.Add(sprite); 66 | } 67 | 68 | public void AddRange(Sprite[] sprites) 69 | { 70 | this.Items.AddRange(sprites); 71 | } 72 | 73 | public void RemoveSelectedSprites() 74 | { 75 | if (this.SelectedIndex != -1) 76 | { 77 | SelectedObjectCollection selectedItems = this.SelectedItems; 78 | 79 | for (int i = selectedItems.Count - 1; i >= 0; i--) 80 | { 81 | this.Items.Remove(selectedItems[i]); 82 | } 83 | } 84 | } 85 | 86 | public void Clear() 87 | { 88 | this.Items.Clear(); 89 | } 90 | 91 | #endregion 92 | 93 | #region Event Handlers 94 | 95 | private void MeasureItemHandler(object sender, MeasureItemEventArgs e) 96 | { 97 | e.ItemHeight = (int)(Sprite.DefaultSize + (2 * ItemMargin)); 98 | } 99 | 100 | private void DrawItemHandler(object sender, DrawItemEventArgs ev) 101 | { 102 | if (this.Items.Count == 0 || ev.Index == -1) 103 | { 104 | return; 105 | } 106 | 107 | Rectangle bounds = ev.Bounds; 108 | 109 | // draw background 110 | ev.DrawBackground(); 111 | 112 | // draw border 113 | ev.Graphics.DrawRectangle(Pens.Gray, bounds); 114 | 115 | Sprite sprite = (Sprite)this.Items[ev.Index]; 116 | 117 | // find the area in which to put the text and draw. 118 | this.layoutRect.X = bounds.Left + Sprite.DefaultSize + (3 * ItemMargin); 119 | this.layoutRect.Y = bounds.Top + (ItemMargin * 2); 120 | this.layoutRect.Width = bounds.Right - ItemMargin - this.layoutRect.X; 121 | this.layoutRect.Height = bounds.Bottom - ItemMargin - this.layoutRect.Y; 122 | 123 | // draw sprite id 124 | if ((ev.State & DrawItemState.Selected) == DrawItemState.Selected) 125 | { 126 | this.pen.Brush = WhiteBrush; 127 | ev.Graphics.DrawString(sprite.ToString(), this.Font, WhiteBrush, this.layoutRect); 128 | } 129 | else 130 | { 131 | this.pen.Brush = BlackBrush; 132 | ev.Graphics.DrawString(sprite.ToString(), this.Font, BlackBrush, this.layoutRect); 133 | } 134 | 135 | this.destRect.Y = bounds.Top + ItemMargin; 136 | 137 | Bitmap bitmap = sprite.GetBitmap(); 138 | if (bitmap != null) 139 | { 140 | this.sourceRect.Width = bitmap.Width; 141 | this.sourceRect.Height = bitmap.Height; 142 | ev.Graphics.DrawImage(bitmap, this.destRect, this.sourceRect, GraphicsUnit.Pixel); 143 | } 144 | 145 | // draw sprite border 146 | ev.Graphics.DrawRectangle(this.pen, this.destRect); 147 | 148 | // draw focus rectangle 149 | ev.DrawFocusRectangle(); 150 | } 151 | 152 | #endregion 153 | 154 | #region Class Properties 155 | 156 | private static readonly Brush WhiteBrush = new SolidBrush(Color.White); 157 | private static readonly Brush BlackBrush = new SolidBrush(Color.Black); 158 | 159 | #endregion 160 | } 161 | } 162 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Controls/ThingTypeListBox.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client; 27 | using OpenTibia.Client.Things; 28 | using System.Drawing; 29 | using System.Windows.Forms; 30 | #endregion 31 | 32 | namespace OpenTibia.Controls 33 | { 34 | public class ThingTypeListBox : ListBox 35 | { 36 | #region Private Properties 37 | 38 | private const int ItemMargin = 5; 39 | 40 | private Rectangle layoutRect; 41 | private Rectangle destRect; 42 | private Rectangle sourceRect; 43 | private Pen pen; 44 | 45 | #endregion 46 | 47 | #region Constructor 48 | 49 | public ThingTypeListBox() 50 | { 51 | this.layoutRect = new Rectangle(); 52 | this.destRect = new Rectangle(ItemMargin, 0, 32, 32); 53 | this.sourceRect = new Rectangle(); 54 | this.pen = new Pen(Color.Transparent); 55 | this.MeasureItem += new MeasureItemEventHandler(this.MeasureItemHandler); 56 | this.DrawItem += new DrawItemEventHandler(this.DrawItemHandler); 57 | this.DrawMode = DrawMode.OwnerDrawVariable; 58 | } 59 | 60 | #endregion 61 | 62 | #region Public Properties 63 | 64 | public IClient Client { get; set; } 65 | 66 | #endregion 67 | 68 | #region Public Methods 69 | 70 | public void Add(ThingType thing) 71 | { 72 | Items.Add(thing); 73 | } 74 | 75 | public void AddRange(ThingType[] things) 76 | { 77 | Items.AddRange(things); 78 | } 79 | 80 | public void RemoveSelectedThings() 81 | { 82 | if (this.SelectedIndex != -1) 83 | { 84 | SelectedObjectCollection selectedItems = this.SelectedItems; 85 | 86 | for (int i = selectedItems.Count - 1; i >= 0; i--) 87 | { 88 | this.Items.Remove(selectedItems[i]); 89 | } 90 | } 91 | } 92 | 93 | public void Clear() 94 | { 95 | Items.Clear(); 96 | } 97 | 98 | #endregion 99 | 100 | #region Event Handlers 101 | 102 | private void MeasureItemHandler(object sender, MeasureItemEventArgs e) 103 | { 104 | e.ItemHeight = (int)(32 + (2 * ItemMargin)); 105 | } 106 | 107 | private void DrawItemHandler(object sender, DrawItemEventArgs ev) 108 | { 109 | Rectangle bounds = ev.Bounds; 110 | bounds.Width--; 111 | 112 | // draw background 113 | ev.DrawBackground(); 114 | 115 | // draw border 116 | ev.Graphics.DrawRectangle(Pens.Gray, bounds); 117 | 118 | if (this.Client != null && ev.Index != -1) 119 | { 120 | ThingType thing = (ThingType)this.Items[ev.Index]; 121 | 122 | // find the area in which to put the text and draw. 123 | this.layoutRect.X = bounds.Left + 32 + (3 * ItemMargin); 124 | this.layoutRect.Y = bounds.Top + (ItemMargin * 2); 125 | this.layoutRect.Width = bounds.Right - ItemMargin - this.layoutRect.X; 126 | this.layoutRect.Height = bounds.Bottom - ItemMargin - this.layoutRect.Y; 127 | 128 | // draw thing id end name 129 | if ((ev.State & DrawItemState.Selected) == DrawItemState.Selected) 130 | { 131 | this.pen.Brush = WhiteBrush; 132 | ev.Graphics.DrawString(thing.ToString(), this.Font, WhiteBrush, this.layoutRect); 133 | } 134 | else 135 | { 136 | this.pen.Brush = BlackBrush; 137 | ev.Graphics.DrawString(thing.ToString(), this.Font, BlackBrush, this.layoutRect); 138 | } 139 | 140 | this.destRect.Y = bounds.Top + ItemMargin; 141 | 142 | Bitmap bitmap = this.Client.GetObjectImage(thing); 143 | if (bitmap != null) 144 | { 145 | this.sourceRect.Width = bitmap.Width; 146 | this.sourceRect.Height = bitmap.Height; 147 | ev.Graphics.DrawImage(bitmap, this.destRect, this.sourceRect, GraphicsUnit.Pixel); 148 | } 149 | } 150 | 151 | // draw view border 152 | ev.Graphics.DrawRectangle(this.pen, this.destRect); 153 | 154 | // draw focus rect 155 | ev.DrawFocusRectangle(); 156 | } 157 | 158 | #endregion 159 | 160 | #region Class Properties 161 | 162 | private static readonly Brush WhiteBrush = new SolidBrush(Color.White); 163 | private static readonly Brush BlackBrush = new SolidBrush(Color.Black); 164 | 165 | #endregion 166 | } 167 | } 168 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Core/IStorage.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | #endregion 28 | 29 | namespace OpenTibia.Core 30 | { 31 | public enum StorageChangeType 32 | { 33 | Add, 34 | Replace, 35 | Remove 36 | } 37 | 38 | public interface IStorage 39 | { 40 | bool Changed { get; } 41 | bool Loaded { get; } 42 | } 43 | 44 | public delegate void StorageHandler(IStorage sender); 45 | public delegate void ProgressHandler(object sender, int percentage); 46 | } 47 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Core/Version.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client.Things; 27 | using System; 28 | #endregion 29 | 30 | namespace OpenTibia.Core 31 | { 32 | public class Version 33 | { 34 | #region Constructor 35 | 36 | public Version(ushort value, string description, uint datSignature, uint sprSignature, uint otbValue) 37 | { 38 | this.Value = value; 39 | this.Description = string.IsNullOrEmpty(description) ? $"Client {value / 100}.{value % 100}" : description; 40 | this.DatSignature = datSignature; 41 | this.SprSignature = sprSignature; 42 | this.OtbValue = otbValue; 43 | this.Format = VesionValueToDatFormat(value); 44 | } 45 | 46 | public Version(ushort value, uint datSignature, uint sprSignature, uint otbValue) : this(value, null, datSignature, sprSignature, otbValue) 47 | { 48 | //// 49 | } 50 | 51 | #endregion 52 | 53 | #region Public Properties 54 | 55 | public ushort Value { get; private set; } 56 | 57 | public string Description { get; private set; } 58 | 59 | public uint DatSignature { get; private set; } 60 | 61 | public uint SprSignature { get; private set; } 62 | 63 | public uint OtbValue { get; private set; } 64 | 65 | public DatFormat Format { get; private set; } 66 | 67 | public bool IsValid 68 | { 69 | get 70 | { 71 | return this.Value != 0 && !string.IsNullOrEmpty(this.Description) && this.DatSignature != 0 && this.SprSignature != 0 && this.OtbValue != 0; 72 | } 73 | } 74 | 75 | #endregion 76 | 77 | #region Public Methods 78 | 79 | public override string ToString() 80 | { 81 | return this.Description; 82 | } 83 | 84 | #endregion 85 | 86 | #region Public Static Methods 87 | 88 | public static DatFormat VesionValueToDatFormat(ushort value) 89 | { 90 | if (value == 0) 91 | { 92 | return DatFormat.InvalidFormat; 93 | } 94 | 95 | if (value < 740) 96 | { 97 | return DatFormat.Format_710; 98 | } 99 | else if (value < 755) 100 | { 101 | return DatFormat.Format_740; 102 | } 103 | else if (value < 780) 104 | { 105 | return DatFormat.Format_755; 106 | } 107 | else if (value < 860) 108 | { 109 | return DatFormat.Format_780; 110 | } 111 | else if (value < 960) 112 | { 113 | return DatFormat.Format_860; 114 | } 115 | else if (value < 1010) 116 | { 117 | return DatFormat.Format_960; 118 | } 119 | else if (value < 1050) 120 | { 121 | return DatFormat.Format_1010; 122 | } 123 | else if (value < 1057) 124 | { 125 | return DatFormat.Format_1050; 126 | } 127 | else if (value < 1092) 128 | { 129 | return DatFormat.Format_1057; 130 | } 131 | else if (value < 1093) 132 | { 133 | return DatFormat.Format_1092; 134 | } 135 | else if (value >= 1093) 136 | { 137 | return DatFormat.Format_1093; 138 | } 139 | 140 | return DatFormat.InvalidFormat; 141 | } 142 | 143 | #endregion 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Core/VersionListChangedArgs.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | #endregion 28 | 29 | namespace OpenTibia.Core 30 | { 31 | public class VersionListChangedArgs 32 | { 33 | #region Constructor 34 | 35 | public VersionListChangedArgs(OpenTibia.Core.Version changedVersion, StorageChangeType changeType) 36 | { 37 | this.ChangedVersion = changedVersion; 38 | this.ChangeType = changeType; 39 | } 40 | 41 | #endregion 42 | 43 | #region Public Properties 44 | 45 | public OpenTibia.Core.Version ChangedVersion { get; private set; } 46 | 47 | public StorageChangeType ChangeType { get; private set; } 48 | 49 | #endregion 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Geom/Direction.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | #endregion 28 | 29 | namespace OpenTibia.Geom 30 | { 31 | public enum Direction : byte 32 | { 33 | North = 0, 34 | East = 1, 35 | South = 2, 36 | West = 3, 37 | Southwest = 4, 38 | Southeast = 5, 39 | Northwest = 6, 40 | Northeast = 7 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Geom/Position.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | #endregion 28 | 29 | namespace OpenTibia.Geom 30 | { 31 | public class Position 32 | { 33 | #region Contructors 34 | 35 | public Position(int x, int y, int z) 36 | { 37 | this.X = x; 38 | this.Y = y; 39 | this.Z = z; 40 | } 41 | 42 | public Position() : this(-1, -1, -1) 43 | { 44 | //// 45 | } 46 | 47 | public Position(Position position) : this(position.X, position.Y, position.Z) 48 | { 49 | //// 50 | } 51 | 52 | #endregion 53 | 54 | #region Public Properties 55 | 56 | public int X { get; set; } 57 | public int Y { get; set; } 58 | public int Z { get; set; } 59 | 60 | #endregion 61 | 62 | #region Public Methods 63 | 64 | public override bool Equals(object position) 65 | { 66 | if (position == null) 67 | { 68 | return false; 69 | } 70 | 71 | Position pos = position as Position; 72 | 73 | if ((object)pos == null) 74 | { 75 | return false; 76 | } 77 | 78 | return (this.X == pos.X && this.Y == pos.Y && this.Z == pos.Z); 79 | } 80 | 81 | public bool Equals(Position position) 82 | { 83 | if ((object)position == null) 84 | { 85 | return false; 86 | } 87 | 88 | return (this.X == position.X && this.Y == position.Y && this.Z == position.Z); 89 | } 90 | 91 | public override int GetHashCode() 92 | { 93 | return Tuple.Create(this.X, this.Y, this.Z).GetHashCode(); 94 | } 95 | 96 | public Position SetTo(int x, int y, int z) 97 | { 98 | this.X = x; 99 | this.Y = y; 100 | this.Z = z; 101 | return this; 102 | } 103 | 104 | public Position CopyFrom(Position position) 105 | { 106 | this.X = position.X; 107 | this.Y = position.Y; 108 | this.Z = position.Z; 109 | return this; 110 | } 111 | 112 | public Position CopyTo(Position position) 113 | { 114 | position.X = this.X; 115 | position.Y = this.Y; 116 | position.Z = this.Z; 117 | return position; 118 | } 119 | 120 | public bool IsValid() 121 | { 122 | return (this.X >= 0 && this.X <= 0xFFFF && this.Y >= 0 && this.Y <= 0xFFFF && this.Z >= 0 && this.Z <= 15); 123 | } 124 | 125 | #endregion 126 | 127 | #region Operators 128 | 129 | public static Position operator +(Position p1, Position p2) 130 | { 131 | if ((object)p1 == null || (object)p2 == null) 132 | { 133 | return null; 134 | } 135 | 136 | return new Position(p1.X + p2.X, p1.Y + p2.Y, p1.Z + p1.Z); 137 | } 138 | 139 | public static Position operator -(Position p1, Position p2) 140 | { 141 | if ((object)p1 == null || (object)p2 == null) 142 | { 143 | return null; 144 | } 145 | 146 | return new Position(p1.X - p2.X, p1.Y - p2.Y, p1.Z - p1.Z); 147 | } 148 | 149 | public static bool operator ==(Position p1, Position p2) 150 | { 151 | if (object.ReferenceEquals(p1, p2)) 152 | { 153 | return true; 154 | } 155 | 156 | if ((object)p1 == null || (object)p2 == null) 157 | { 158 | return false; 159 | } 160 | 161 | return p1.X == p2.X && p1.Y == p2.Y && p1.Z == p2.Z; 162 | } 163 | 164 | public static bool operator !=(Position p1, Position p2) 165 | { 166 | if (!object.ReferenceEquals(p1, p2)) 167 | { 168 | return true; 169 | } 170 | 171 | if ((object)p1 == null || (object)p2 == null) 172 | { 173 | return true; 174 | } 175 | 176 | return p1.X != p2.X || p1.Y != p2.Y || p1.Z != p2.Z; 177 | } 178 | 179 | #endregion 180 | } 181 | } 182 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Geom/Rect.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.Drawing; 28 | #endregion 29 | 30 | namespace OpenTibia.Geom 31 | { 32 | public class Rect 33 | { 34 | #region Constructor 35 | 36 | public Rect(int x, int y, int width, int height) 37 | { 38 | X = x; 39 | Y = y; 40 | Width = width; 41 | Height = height; 42 | } 43 | 44 | public Rect() : this(0, 0, 0, 0) 45 | { 46 | //// 47 | } 48 | 49 | public Rect(Rect rect) : this(rect.X, rect.Y, rect.Width, rect.Height) 50 | { 51 | //// 52 | } 53 | 54 | #endregion 55 | 56 | #region Public Properties 57 | 58 | public int X { get; set; } 59 | 60 | public int Y { get; set; } 61 | 62 | public int Width { get; set; } 63 | 64 | public int Height { get; set; } 65 | 66 | public int Left 67 | { 68 | get 69 | { 70 | return this.X; 71 | } 72 | } 73 | 74 | public int Top 75 | { 76 | get 77 | { 78 | return this.Y; 79 | } 80 | } 81 | 82 | public int Right 83 | { 84 | get 85 | { 86 | return this.Width; 87 | } 88 | } 89 | 90 | public int Bottom 91 | { 92 | get 93 | { 94 | return this.Height; 95 | } 96 | } 97 | 98 | public bool IsValid 99 | { 100 | get 101 | { 102 | return this.X <= this.Width && this.Y <= this.Height; 103 | } 104 | } 105 | 106 | #endregion 107 | 108 | #region Public Methods 109 | 110 | public Rect SetTo(int x, int y, int width, int height) 111 | { 112 | this.X = x; 113 | this.Y = y; 114 | this.Width = width; 115 | this.Height = height; 116 | return this; 117 | } 118 | 119 | public Rect CopyFrom(Rect rect) 120 | { 121 | this.X = rect.X; 122 | this.Y = rect.Y; 123 | this.Width = rect.Width; 124 | this.Height = rect.Height; 125 | return this; 126 | } 127 | 128 | public Rect CopyFrom(Rectangle rectangle) 129 | { 130 | this.X = rectangle.X; 131 | this.Y = rectangle.Y; 132 | this.Width = rectangle.Width; 133 | this.Height = rectangle.Height; 134 | return this; 135 | } 136 | 137 | public Rect CopyTo(Rect rect) 138 | { 139 | rect.X = this.X; 140 | rect.Y = this.Y; 141 | rect.Width = this.Width; 142 | rect.Height = this.Height; 143 | return rect; 144 | } 145 | 146 | public Rectangle CopyTo(Rectangle rectangle) 147 | { 148 | rectangle.X = this.X; 149 | rectangle.Y = this.Y; 150 | rectangle.Width = this.Width; 151 | rectangle.Height = this.Height; 152 | return rectangle; 153 | } 154 | 155 | #endregion 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.IO/BinaryTreeReader.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | #endregion 20 | 21 | #region Using Statements 22 | using System; 23 | using System.IO; 24 | #endregion 25 | 26 | namespace OpenTibia.IO 27 | { 28 | public class BinaryTreeReader : IDisposable 29 | { 30 | #region Private Properties 31 | 32 | private BinaryReader reader; 33 | private long currentNodePosition; 34 | private uint currentNodeSize; 35 | 36 | #endregion 37 | 38 | #region Constructor 39 | 40 | public BinaryTreeReader(string path) 41 | { 42 | if (string.IsNullOrEmpty(path)) 43 | { 44 | throw new ArgumentNullException(nameof(path)); 45 | } 46 | 47 | this.reader = new BinaryReader(new FileStream(path, FileMode.Open)); 48 | this.Disposed = false; 49 | } 50 | 51 | #endregion 52 | 53 | #region Public Properties 54 | 55 | public bool Disposed { get; private set; } 56 | 57 | #endregion 58 | 59 | #region Public Properties 60 | 61 | public BinaryReader GetRootNode() 62 | { 63 | return this.GetChildNode(); 64 | } 65 | 66 | public BinaryReader GetChildNode() 67 | { 68 | this.Advance(); 69 | return this.GetNodeData(); 70 | } 71 | 72 | public BinaryReader GetNextNode() 73 | { 74 | this.reader.BaseStream.Seek(this.currentNodePosition, SeekOrigin.Begin); 75 | 76 | SpecialChar value = (SpecialChar)this.reader.ReadByte(); 77 | if (value != SpecialChar.NodeStart) 78 | { 79 | return null; 80 | } 81 | 82 | value = (SpecialChar)this.reader.ReadByte(); 83 | 84 | int level = 1; 85 | while (true) 86 | { 87 | value = (SpecialChar)this.reader.ReadByte(); 88 | if (value == SpecialChar.NodeEnd) 89 | { 90 | --level; 91 | if (level == 0) 92 | { 93 | value = (SpecialChar)this.reader.ReadByte(); 94 | if (value == SpecialChar.NodeEnd) 95 | { 96 | return null; 97 | } 98 | else if (value != SpecialChar.NodeStart) 99 | { 100 | return null; 101 | } 102 | else 103 | { 104 | this.currentNodePosition = this.reader.BaseStream.Position - 1; 105 | return this.GetNodeData(); 106 | } 107 | } 108 | } 109 | else if (value == SpecialChar.NodeStart) 110 | { 111 | ++level; 112 | } 113 | else if (value == SpecialChar.EscapeChar) 114 | { 115 | this.reader.ReadByte(); 116 | } 117 | } 118 | } 119 | 120 | public void Dispose() 121 | { 122 | if (this.reader != null) 123 | { 124 | this.reader.Dispose(); 125 | this.reader = null; 126 | this.Disposed = true; 127 | } 128 | } 129 | 130 | #endregion 131 | 132 | #region Private Methods 133 | 134 | private BinaryReader GetNodeData() 135 | { 136 | this.reader.BaseStream.Seek(this.currentNodePosition, SeekOrigin.Begin); 137 | 138 | // read node type 139 | byte value = this.reader.ReadByte(); 140 | 141 | if ((SpecialChar)value != SpecialChar.NodeStart) 142 | { 143 | return null; 144 | } 145 | 146 | MemoryStream ms = new MemoryStream(200); 147 | 148 | this.currentNodeSize = 0; 149 | while (true) 150 | { 151 | value = this.reader.ReadByte(); 152 | if ((SpecialChar)value == SpecialChar.NodeEnd || (SpecialChar)value == SpecialChar.NodeStart) 153 | { 154 | break; 155 | } 156 | else if ((SpecialChar)value == SpecialChar.EscapeChar) 157 | { 158 | value = this.reader.ReadByte(); 159 | } 160 | 161 | this.currentNodeSize++; 162 | ms.WriteByte(value); 163 | } 164 | 165 | this.reader.BaseStream.Seek(this.currentNodePosition, SeekOrigin.Begin); 166 | ms.Position = 0; 167 | return new BinaryReader(ms); 168 | } 169 | 170 | private bool Advance() 171 | { 172 | try 173 | { 174 | long seekPos = 0; 175 | if (this.currentNodePosition == 0) 176 | { 177 | seekPos = 4; 178 | } 179 | else 180 | { 181 | seekPos = this.currentNodePosition; 182 | } 183 | 184 | this.reader.BaseStream.Seek(seekPos, SeekOrigin.Begin); 185 | 186 | SpecialChar value = (SpecialChar)this.reader.ReadByte(); 187 | if (value != SpecialChar.NodeStart) 188 | { 189 | return false; 190 | } 191 | 192 | if (this.currentNodePosition == 0) 193 | { 194 | this.currentNodePosition = this.reader.BaseStream.Position - 1; 195 | return true; 196 | } 197 | else 198 | { 199 | value = (SpecialChar)this.reader.ReadByte(); 200 | 201 | while (true) 202 | { 203 | value = (SpecialChar)this.reader.ReadByte(); 204 | if (value == SpecialChar.NodeEnd) 205 | { 206 | return false; 207 | } 208 | else if (value == SpecialChar.NodeStart) 209 | { 210 | this.currentNodePosition = this.reader.BaseStream.Position - 1; 211 | return true; 212 | } 213 | else if (value == SpecialChar.EscapeChar) 214 | { 215 | this.reader.ReadByte(); 216 | } 217 | } 218 | } 219 | } 220 | catch (Exception) 221 | { 222 | return false; 223 | } 224 | } 225 | 226 | #endregion 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.IO/BinaryTreeWriter.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | #endregion 20 | 21 | #region Using Statements 22 | using System; 23 | using System.IO; 24 | #endregion 25 | 26 | namespace OpenTibia.IO 27 | { 28 | public class BinaryTreeWriter : IDisposable 29 | { 30 | #region Private Properties 31 | 32 | private BinaryReader writer; 33 | 34 | #endregion 35 | 36 | #region Constructor 37 | 38 | public BinaryTreeWriter(string path) 39 | { 40 | if (string.IsNullOrEmpty(path)) 41 | { 42 | throw new ArgumentNullException(nameof(path)); 43 | } 44 | 45 | this.writer = new BinaryReader(new FileStream(path, FileMode.Create)); 46 | this.Disposed = false; 47 | } 48 | 49 | #endregion 50 | 51 | #region Public Properties 52 | 53 | public bool Disposed { get; private set; } 54 | 55 | #endregion 56 | 57 | #region Public Properties 58 | 59 | public void CreateNode(byte type) 60 | { 61 | this.WriteByte((byte)SpecialChar.NodeStart, false); 62 | this.WriteByte(type); 63 | } 64 | 65 | public void WriteByte(byte value) 66 | { 67 | this.WriteBytes(new byte[1] { value }, true); 68 | } 69 | 70 | public void WriteByte(byte value, bool unescape) 71 | { 72 | this.WriteBytes(new byte[1] { value }, unescape); 73 | } 74 | 75 | public void WriteUInt16(ushort value) 76 | { 77 | this.WriteBytes(BitConverter.GetBytes(value), true); 78 | } 79 | 80 | public void WriteUInt16(ushort value, bool unescape) 81 | { 82 | this.WriteBytes(BitConverter.GetBytes(value), unescape); 83 | } 84 | 85 | public void WriteUInt32(uint value) 86 | { 87 | this.WriteBytes(BitConverter.GetBytes(value), true); 88 | } 89 | 90 | public void WriteUInt32(uint value, bool unescape) 91 | { 92 | this.WriteBytes(BitConverter.GetBytes(value), unescape); 93 | } 94 | 95 | public void WriteProp(RootAttribute attribute, BinaryWriter writer) 96 | { 97 | writer.BaseStream.Position = 0; 98 | byte[] bytes = new byte[writer.BaseStream.Length]; 99 | writer.BaseStream.Read(bytes, 0, (int)writer.BaseStream.Length); 100 | writer.BaseStream.Position = 0; 101 | writer.BaseStream.SetLength(0); 102 | 103 | this.WriteProp((byte)attribute, bytes); 104 | } 105 | 106 | public void WriteBytes(byte[] bytes, bool unescape) 107 | { 108 | foreach (byte b in bytes) 109 | { 110 | if (unescape && (b == (byte)SpecialChar.NodeStart || b == (byte)SpecialChar.NodeEnd || b == (byte)SpecialChar.EscapeChar)) 111 | { 112 | this.writer.BaseStream.WriteByte((byte)SpecialChar.EscapeChar); 113 | } 114 | 115 | this.writer.BaseStream.WriteByte(b); 116 | } 117 | } 118 | 119 | public void CloseNode() 120 | { 121 | this.WriteByte((byte)SpecialChar.NodeEnd, false); 122 | } 123 | 124 | public void Dispose() 125 | { 126 | if (this.writer != null) 127 | { 128 | this.writer.Dispose(); 129 | this.writer = null; 130 | this.Disposed = true; 131 | } 132 | } 133 | 134 | #endregion 135 | 136 | #region Private Methods 137 | 138 | private void WriteProp(byte attr, byte[] bytes) 139 | { 140 | this.WriteByte((byte)attr); 141 | this.WriteUInt16((ushort)bytes.Length); 142 | this.WriteBytes(bytes, true); 143 | } 144 | 145 | #endregion 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.IO/FlagsWriter.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client.Things; 27 | using OpenTibia.Obd; 28 | using System.IO; 29 | #endregion 30 | 31 | namespace OpenTibia.IO 32 | { 33 | public class FlagsBinaryWriter : BinaryWriter 34 | { 35 | #region | Constructor | 36 | 37 | public FlagsBinaryWriter(Stream output) : base(output) 38 | { 39 | //// 40 | } 41 | 42 | #endregion 43 | 44 | #region | Public Methods | 45 | 46 | public void Write(DatFlags1010 value) 47 | { 48 | base.Write((byte)value); 49 | } 50 | 51 | public void Write(ObdFlags value) 52 | { 53 | base.Write((byte)value); 54 | } 55 | 56 | #endregion 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.IO/SpecialChar.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * This program is free software; you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation; either version 2 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along 16 | * with this program; if not, write to the Free Software Foundation, Inc., 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 | */ 19 | #endregion 20 | 21 | #region Using Statements 22 | using System; 23 | #endregion 24 | 25 | namespace OpenTibia.IO 26 | { 27 | public enum SpecialChar : byte 28 | { 29 | NodeStart = 0xFE, 30 | NodeEnd = 0xFF, 31 | EscapeChar = 0xFD, 32 | } 33 | 34 | public enum RootAttribute 35 | { 36 | Version = 0x01 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Obd/ObdDecoder.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Animation; 27 | using OpenTibia.Client.Sprites; 28 | using OpenTibia.Client.Things; 29 | using OpenTibia.Collections; 30 | using OpenTibia.Utils; 31 | using System; 32 | using System.IO; 33 | using System.Text; 34 | #endregion 35 | 36 | namespace OpenTibia.Obd 37 | { 38 | public class ObdDecoder 39 | { 40 | #region | Public Static Methods | 41 | 42 | public static ObjectData Decode(byte[] bytes) 43 | { 44 | if (bytes == null) 45 | { 46 | return null; 47 | } 48 | 49 | bytes = LZMACoder.Uncompress(bytes); 50 | 51 | using (BinaryReader reader = new BinaryReader(new MemoryStream(bytes))) 52 | { 53 | ushort version = reader.ReadUInt16(); 54 | 55 | if (version == (ushort)ObdVersion.Version3) 56 | { 57 | return DecodeV3(reader); 58 | } 59 | else if (version == (ushort)ObdVersion.Version2) 60 | { 61 | return DecodeV2(reader); 62 | } 63 | else if (version >= (ushort)DatFormat.Format_710) 64 | { 65 | return DecodeV1(reader); 66 | } 67 | else 68 | { 69 | //// 70 | } 71 | } 72 | 73 | return null; 74 | } 75 | 76 | #endregion 77 | 78 | #region | Private Static Methods | 79 | 80 | private static ObjectData DecodeV1(BinaryReader reader) 81 | { 82 | reader.BaseStream.Position = 0; 83 | 84 | Console.WriteLine(reader.ReadUInt16()); 85 | 86 | ushort nameLength = reader.ReadUInt16(); 87 | byte[] buffer = reader.ReadBytes(nameLength); 88 | string categoryStr = Encoding.UTF8.GetString(buffer, 0, buffer.Length); 89 | ThingCategory category = ThingCategory.Invalid; 90 | 91 | switch (categoryStr) 92 | { 93 | case "item": 94 | category = ThingCategory.Item; 95 | break; 96 | 97 | case "outfit": 98 | category = ThingCategory.Outfit; 99 | break; 100 | 101 | case "effect": 102 | category = ThingCategory.Effect; 103 | break; 104 | 105 | case "missile": 106 | category = ThingCategory.Missile; 107 | break; 108 | } 109 | 110 | ThingType thing = new ThingType(category); 111 | 112 | if (!ThingTypeSerializer.ReadProperties(thing, DatFormat.Format_1010, reader)) 113 | { 114 | return null; 115 | } 116 | 117 | FrameGroup group = new FrameGroup(); 118 | 119 | group.Width = reader.ReadByte(); 120 | group.Height = reader.ReadByte(); 121 | 122 | if (group.Width > 1 || group.Height > 1) 123 | { 124 | group.ExactSize = reader.ReadByte(); 125 | } 126 | else 127 | { 128 | group.ExactSize = Sprite.DefaultSize; 129 | } 130 | 131 | group.Layers = reader.ReadByte(); 132 | group.PatternX = reader.ReadByte(); 133 | group.PatternY = reader.ReadByte(); 134 | group.PatternZ = reader.ReadByte(); 135 | group.Frames = reader.ReadByte(); 136 | 137 | if (group.Frames > 1) 138 | { 139 | group.IsAnimation = true; 140 | group.AnimationMode = AnimationMode.Asynchronous; 141 | group.LoopCount = 0; 142 | group.StartFrame = 0; 143 | group.FrameDurations = new FrameDuration[group.Frames]; 144 | 145 | for (byte i = 0; i < group.Frames; i++) 146 | { 147 | group.FrameDurations[i] = new FrameDuration(category); 148 | } 149 | } 150 | 151 | int totalSprites = group.GetTotalSprites(); 152 | if (totalSprites > 4096) 153 | { 154 | throw new Exception("The ThingData has more than 4096 sprites."); 155 | } 156 | 157 | group.SpriteIDs = new uint[totalSprites]; 158 | SpriteGroup spriteGroup = new SpriteGroup(); 159 | Sprite[] sprites = new Sprite[totalSprites]; 160 | 161 | for (int i = 0; i < totalSprites; i++) 162 | { 163 | uint spriteID = reader.ReadUInt32(); 164 | group.SpriteIDs[i] = spriteID; 165 | 166 | uint dataSize = reader.ReadUInt32(); 167 | if (dataSize > Sprite.PixelsDataSize) 168 | { 169 | throw new Exception("Invalid sprite data size."); 170 | } 171 | 172 | byte[] pixels = reader.ReadBytes((int)dataSize); 173 | 174 | Sprite sprite = new Sprite(spriteID, true); 175 | sprite.SetPixelsARGB(pixels); 176 | sprites[i] = sprite; 177 | } 178 | 179 | thing.SetFrameGroup(FrameGroupType.Default, group); 180 | spriteGroup.Add(FrameGroupType.Default, sprites); 181 | return new ObjectData(thing, spriteGroup); 182 | } 183 | 184 | private static ObjectData DecodeV2(BinaryReader reader) 185 | { 186 | throw new NotImplementedException(); 187 | } 188 | 189 | private static ObjectData DecodeV3(BinaryReader reader) 190 | { 191 | throw new NotImplementedException(); 192 | } 193 | 194 | #endregion 195 | } 196 | } 197 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Obd/ObdFlags.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Obd 26 | { 27 | public enum ObdFlags : byte 28 | { 29 | Ground = 0x00, 30 | GroundBorder = 0x01, 31 | OnBottom = 0x02, 32 | OnTop = 0x03, 33 | Container = 0x04, 34 | Stackable = 0x05, 35 | ForceUse = 0x06, 36 | MultiUse = 0x07, 37 | Writable = 0x08, 38 | WritableOnce = 0x09, 39 | FluidContainer = 0x0A, 40 | Fluid = 0x0B, 41 | IsUnpassable = 0x0C, 42 | IsUnmovable = 0x0D, 43 | BlockMissiles = 0x0E, 44 | BlockPathfinder = 0x0F, 45 | NoMoveAnimation = 0x10, 46 | Pickupable = 0x11, 47 | Hangable = 0x12, 48 | HookSouth = 0x13, 49 | HookEast = 0x14, 50 | Rotatable = 0x15, 51 | HasLight = 0x16, 52 | DontHide = 0x17, 53 | Translucent = 0x18, 54 | HasOffset = 0x19, 55 | HasElevation = 0x1A, 56 | LyingObject = 0x1B, 57 | Minimap = 0x1D, 58 | AnimateAlways = 0x1C, 59 | LensHelp = 0x1E, 60 | FullGround = 0x1F, 61 | IgnoreLook = 0x20, 62 | Cloth = 0x21, 63 | Market = 0x22, 64 | DefaultAction = 0x23, 65 | Wrappable = 0x24, 66 | Unwrappable = 0x25, 67 | TopEffect = 0x26, 68 | 69 | HasChanges = 0xFC, 70 | FloorChange = 0xFD, 71 | Usable = 0xFE, 72 | 73 | LastFlag = 0xFF 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Obd/ObdVersion.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Obd 26 | { 27 | public enum ObdVersion : ushort 28 | { 29 | InvalidVersion = 0, 30 | Version1 = 100, 31 | Version2 = 200, 32 | Version3 = 300 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/BitmapLocker.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Utils; 27 | using System; 28 | using System.Drawing; 29 | using System.Drawing.Imaging; 30 | using System.Runtime.InteropServices; 31 | #endregion 32 | 33 | namespace OpenTibia.Utilities 34 | { 35 | public class BitmapLocker : IDisposable 36 | { 37 | #region Private Properties 38 | 39 | private Bitmap bitmap; 40 | private BitmapData bitmapData; 41 | private IntPtr address = IntPtr.Zero; 42 | 43 | #endregion 44 | 45 | #region Constructor 46 | 47 | public BitmapLocker(Bitmap bitmap) 48 | { 49 | this.bitmap = bitmap; 50 | this.Width = bitmap.Width; 51 | this.Height = bitmap.Height; 52 | this.Length = bitmap.Width * bitmap.Height * 4; 53 | this.Pixels = new byte[this.Length]; 54 | } 55 | 56 | #endregion 57 | 58 | #region Public Properties 59 | 60 | public byte[] Pixels { get; private set; } 61 | 62 | public int Width { get; private set; } 63 | 64 | public int Height { get; private set; } 65 | 66 | public int Length { get; private set; } 67 | 68 | #endregion 69 | 70 | #region Public Methods 71 | 72 | public void LockBits() 73 | { 74 | // create rectangle to lock 75 | Rectangle rect = new Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height); 76 | 77 | // lock bitmap and return bitmap data 78 | this.bitmapData = this.bitmap.LockBits(rect, ImageLockMode.ReadWrite, this.bitmap.PixelFormat); 79 | 80 | // gets the address of the first pixel data in the bitmap 81 | this.address = this.bitmapData.Scan0; 82 | 83 | // copy data from pointer to array 84 | Marshal.Copy(this.address, this.Pixels, 0, this.Length); 85 | } 86 | 87 | public void CopyPixels(Bitmap source, int px, int py) 88 | { 89 | int width = source.Width; 90 | int height = source.Height; 91 | int maxIndex = this.Length - 4; 92 | 93 | for (int y = 0; y < height; y++) 94 | { 95 | int row = ((py + y) * this.Width); 96 | 97 | for (int x = 0; x < width; x++) 98 | { 99 | Color color = source.GetPixel(x, y); 100 | if (color.A != 0) 101 | { 102 | int i = (row + (px + x)) * 4; 103 | if (i > maxIndex) 104 | { 105 | continue; 106 | } 107 | 108 | this.Pixels[i] = color.B; 109 | this.Pixels[i + 1] = color.G; 110 | this.Pixels[i + 2] = color.R; 111 | this.Pixels[i + 3] = color.A; 112 | } 113 | } 114 | } 115 | } 116 | 117 | public void CopyPixels(Bitmap source, int rx, int ry, int rw, int rh, int px, int py) 118 | { 119 | int maxIndex = this.Length - 4; 120 | 121 | for (int y = 0; y < rh; y++) 122 | { 123 | int row = ((py + y) * this.Width); 124 | 125 | for (int x = 0; x < rw; x++) 126 | { 127 | Color color = source.GetPixel(rx + x, ry + y); 128 | if (color.A != 0) 129 | { 130 | int i = (row + (px + x)) * 4; 131 | if (i > maxIndex) 132 | { 133 | continue; 134 | } 135 | 136 | this.Pixels[i] = color.B; 137 | this.Pixels[i + 1] = color.G; 138 | this.Pixels[i + 2] = color.R; 139 | this.Pixels[i + 3] = color.A; 140 | } 141 | } 142 | } 143 | } 144 | 145 | public void ColorizePixels(byte[] grayPixels, byte[] blendPixels, byte yellowChannel, byte redChannel, byte greenChannel, byte blueChannel) 146 | { 147 | Color rgb = Color.Transparent; 148 | 149 | for (int y = 0; y < this.Height; y++) 150 | { 151 | int row = (y * this.Width); 152 | 153 | for (int x = 0; x < this.Width; x++) 154 | { 155 | int bi = (row + x) * 4; // blue index 156 | int gi = bi + 1; // green index 157 | int ri = bi + 2; // red index 158 | int ai = bi + 3; // alpha index 159 | 160 | if (grayPixels[ai] == 0 || blendPixels[ai] == 0) 161 | { 162 | this.Pixels[bi] = grayPixels[bi]; // blue 163 | this.Pixels[gi] = grayPixels[gi]; // green 164 | this.Pixels[ri] = grayPixels[ri]; // red 165 | this.Pixels[ai] = grayPixels[ai]; // alpha 166 | continue; 167 | } 168 | 169 | byte blendBlue = blendPixels[bi]; 170 | byte blendGreen = blendPixels[gi]; 171 | byte blendRed = blendPixels[ri]; 172 | 173 | if (blendRed != 0 && blendGreen != 0 && blendBlue == 0) 174 | { 175 | rgb = ColorUtils.HsiToRgb(yellowChannel); 176 | } 177 | else if (blendRed != 0 && blendGreen == 0 && blendBlue == 0) 178 | { 179 | rgb = ColorUtils.HsiToRgb(redChannel); 180 | } 181 | else if (blendRed == 0 && blendGreen != 0 && blendBlue == 0) 182 | { 183 | rgb = ColorUtils.HsiToRgb(greenChannel); 184 | } 185 | else if (blendRed == 0 && blendGreen == 0 && blendBlue != 0) 186 | { 187 | rgb = ColorUtils.HsiToRgb(blueChannel); 188 | } 189 | 190 | this.Pixels[bi] = (byte)(grayPixels[bi] * (rgb.B / 255f)); 191 | this.Pixels[gi] = (byte)(grayPixels[gi] * (rgb.G / 255f)); 192 | this.Pixels[ri] = (byte)(grayPixels[ri] * (rgb.R / 255f)); 193 | this.Pixels[ai] = grayPixels[ai]; 194 | } 195 | } 196 | } 197 | 198 | public void UnlockBits() 199 | { 200 | // copy data from byte array to pointer 201 | Marshal.Copy(this.Pixels, 0, this.address, this.Length); 202 | 203 | // unlock bitmap data 204 | this.bitmap.UnlockBits(this.bitmapData); 205 | } 206 | 207 | public void Dispose() 208 | { 209 | this.bitmap = null; 210 | this.bitmapData = null; 211 | this.Pixels = null; 212 | this.Width = 0; 213 | this.Height = 0; 214 | this.Length = 0; 215 | } 216 | 217 | #endregion 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/Clock.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.Diagnostics; 28 | #endregion 29 | 30 | namespace OpenTibia.Utils 31 | { 32 | public static class Clock 33 | { 34 | #region Private Static Properties 35 | 36 | private static Stopwatch stopwatch; 37 | 38 | #endregion 39 | 40 | #region Static Constructor 41 | 42 | static Clock() 43 | { 44 | stopwatch = new Stopwatch(); 45 | } 46 | 47 | #endregion 48 | 49 | #region Public Static Properties 50 | 51 | public static long ElapsedMilliseconds 52 | { 53 | get 54 | { 55 | return stopwatch.ElapsedMilliseconds; 56 | } 57 | } 58 | 59 | #endregion 60 | 61 | #region Public Static Methods 62 | 63 | public static void Start() 64 | { 65 | stopwatch.Start(); 66 | } 67 | 68 | #endregion 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/ColorUtils.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.Drawing; 28 | #endregion 29 | 30 | namespace OpenTibia.Utils 31 | { 32 | public static class ColorUtils 33 | { 34 | public static int RgbToEightBit(Color color) 35 | { 36 | int c = 0; 37 | c += (color.R / 51) * 36; 38 | c += (color.G / 51) * 6; 39 | c += (color.B / 51); 40 | return c; 41 | } 42 | 43 | public static Color EightBitToRgb(int color) 44 | { 45 | if (color <= 0 || color >= 216) 46 | { 47 | return Color.FromArgb(0, 0, 0); 48 | } 49 | 50 | int r = (int)(color / 36) % 6 * 51; 51 | int g = (int)(color / 6) % 6 * 51; 52 | int b = color % 6 * 51; 53 | return Color.FromArgb(r, g, b); 54 | } 55 | 56 | public static Color HsiToRgb(int hsi) 57 | { 58 | const int values = 7; 59 | const int steps = 19; 60 | double hue = 0; 61 | double saturation = 0; 62 | double intensity = 0; 63 | double red = 0; 64 | double green = 0; 65 | double blue = 0; 66 | 67 | if (hsi >= steps * values) 68 | { 69 | hsi = 0; 70 | } 71 | 72 | if (hsi % steps == 0) 73 | { 74 | hue = 0; 75 | saturation = 0; 76 | intensity = 1 - (double)hsi / steps / values; 77 | } 78 | else 79 | { 80 | hue = hsi % steps * (1.0d / 18.0d); 81 | saturation = 1.0d; 82 | intensity = 1.0d; 83 | 84 | switch ((int)(hsi / steps)) 85 | { 86 | case 0: 87 | saturation = 0.25d; 88 | intensity = 1.0d; 89 | break; 90 | 91 | case 1: 92 | saturation = 0.25d; 93 | intensity = 0.75d; 94 | break; 95 | 96 | case 2: 97 | saturation = 0.5d; 98 | intensity = 0.75d; 99 | break; 100 | 101 | case 3: 102 | saturation = 0.667d; 103 | intensity = 0.75d; 104 | break; 105 | 106 | case 4: 107 | saturation = 1.0d; 108 | intensity = 1.0d; 109 | break; 110 | 111 | case 5: 112 | saturation = 1.0d; 113 | intensity = 0.75d; 114 | break; 115 | 116 | case 6: 117 | saturation = 1.0d; 118 | intensity = 0.5d; 119 | break; 120 | } 121 | } 122 | 123 | if (intensity == 0) 124 | { 125 | return Color.Black; 126 | } 127 | 128 | if (saturation == 0) 129 | { 130 | byte value = (byte)(intensity * 255); 131 | return Color.FromArgb(value, value, value); 132 | } 133 | 134 | if (hue < (1.0d / 6.0d)) 135 | { 136 | red = intensity; 137 | blue = intensity * (1 - saturation); 138 | green = blue + (intensity - blue) * 6 * hue; 139 | } 140 | else if (hue < (2.0d / 6.0d)) 141 | { 142 | green = intensity; 143 | blue = intensity * (1 - saturation); 144 | red = green - (intensity - blue) * (6 * hue - 1); 145 | } 146 | else if (hue < (3.0d / 6.0d)) 147 | { 148 | green = intensity; 149 | red = intensity * (1 - saturation); 150 | blue = red + (intensity - red) * (6 * hue - 2); 151 | } 152 | else if (hue < (4.0d / 6.0d)) 153 | { 154 | blue = intensity; 155 | red = intensity * (1 - saturation); 156 | green = blue - (intensity - red) * (6 * hue - 3); 157 | } 158 | else if (hue < (5.0d / 6.0d)) 159 | { 160 | blue = intensity; 161 | green = intensity * (1 - saturation); 162 | red = green + (intensity - green) * (6 * hue - 4); 163 | } 164 | else 165 | { 166 | red = intensity; 167 | green = intensity * (1 - saturation); 168 | blue = red - (intensity - green) * (6 * hue - 5); 169 | } 170 | 171 | return Color.FromArgb((byte)(red * 0xFF), (byte)(green * 0xFF), (byte)(blue * 0xFF)); 172 | } 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/LZMACoder.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | using System; 24 | #endregion 25 | 26 | #region Using Statements 27 | using SevenZip; 28 | using System.IO; 29 | using SevenZip.Compression.LZMA; 30 | #endregion 31 | 32 | namespace OpenTibia.Utils 33 | { 34 | public static class LZMACoder 35 | { 36 | #region Private Static Properties 37 | 38 | private static CoderPropID[] propIDs = 39 | { 40 | CoderPropID.DictionarySize, 41 | CoderPropID.PosStateBits, 42 | CoderPropID.LitContextBits, 43 | CoderPropID.LitPosBits, 44 | CoderPropID.Algorithm, 45 | CoderPropID.NumFastBytes, 46 | CoderPropID.MatchFinder, 47 | CoderPropID.EndMarker 48 | }; 49 | 50 | private static object[] properties = 51 | { 52 | (int)(1 << 21), // DictionarySize 53 | (int)(2), // PosStateBits 54 | (int)(3), // LitContextBits 55 | (int)(0), // LitPosBits 56 | (int)(2), // Algorithm 57 | (int)(128), // NumFastBytes 58 | "bt4", // MatchFinder 59 | false // EndMarker 60 | }; 61 | 62 | #endregion 63 | 64 | #region Public Static Methods 65 | 66 | public static byte[] Uncompress(byte[] bytes) 67 | { 68 | if (bytes.Length < 5) 69 | { 70 | throw new Exception("LZMA data is too short."); 71 | } 72 | 73 | using (MemoryStream input = new MemoryStream(bytes)) 74 | { 75 | // read the decoder properties 76 | byte[] properties = new byte[5]; 77 | if (input.Read(properties, 0, 5) != 5) 78 | { 79 | throw (new Exception("LZMA data is too short.")); 80 | } 81 | 82 | long outSize = 0; 83 | 84 | if (BitConverter.IsLittleEndian) 85 | { 86 | for (int i = 0; i < 8; i++) 87 | { 88 | int v = input.ReadByte(); 89 | if (v < 0) 90 | { 91 | throw (new Exception("Can't Read 1.")); 92 | } 93 | 94 | outSize |= ((long)v) << (8 * i); 95 | } 96 | } 97 | 98 | MemoryStream output = new MemoryStream(); 99 | long compressedSize = input.Length - input.Position; 100 | Decoder decoder = new Decoder(); 101 | decoder.SetDecoderProperties(properties); 102 | decoder.Code(input, output, compressedSize, outSize, null); 103 | return output.ToArray(); 104 | } 105 | } 106 | 107 | public static byte[] Compress(byte[] bytes) 108 | { 109 | using (MemoryStream input = new MemoryStream(bytes)) 110 | { 111 | MemoryStream output = new MemoryStream(); 112 | Encoder encoder = new Encoder(); 113 | encoder.SetCoderProperties(propIDs, properties); 114 | encoder.WriteCoderProperties(output); 115 | 116 | if (BitConverter.IsLittleEndian) 117 | { 118 | byte[] LengthHeader = BitConverter.GetBytes(input.Length); 119 | output.Write(LengthHeader, 0, LengthHeader.Length); 120 | } 121 | 122 | encoder.Code(input, output, input.Length, -1, null); 123 | return output.ToArray(); 124 | } 125 | } 126 | 127 | #endregion 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/OutfitData.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | namespace OpenTibia.Utils 26 | { 27 | public class OutfitData 28 | { 29 | #region | Constructor | 30 | 31 | public OutfitData(ushort type, byte head, byte body, byte legs, byte feet, byte addons) 32 | { 33 | this.Type = type; 34 | this.Head = head; 35 | this.Body = body; 36 | this.Legs = legs; 37 | this.Feet = feet; 38 | this.Addons = addons; 39 | } 40 | 41 | public OutfitData() : this(0, 0, 0, 0, 0, 0) 42 | { 43 | //// 44 | } 45 | 46 | #endregion 47 | 48 | #region | Public Properties | 49 | 50 | public ushort Type { get; set; } 51 | 52 | public byte Head { get; set; } 53 | 54 | public byte Body { get; set; } 55 | 56 | public byte Legs { get; set; } 57 | 58 | public byte Feet { get; set; } 59 | 60 | public byte Addons { get; set; } 61 | 62 | #endregion 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/PathUtils.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.IO; 28 | using System.Windows.Forms; 29 | #endregion 30 | 31 | namespace OpenTibia.Utils 32 | { 33 | public abstract class PathUtils 34 | { 35 | /// 36 | /// The folder containing the application's installed files. 37 | /// 38 | public static string ApplicationDirectory 39 | { 40 | get 41 | { 42 | return Path.GetDirectoryName(Application.ExecutablePath); 43 | } 44 | } 45 | 46 | /// 47 | /// Path to the user's application directory 48 | /// 49 | public static string UserDirectory 50 | { 51 | get 52 | { 53 | return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 54 | } 55 | } 56 | 57 | /// 58 | /// Path to the plugin directory 59 | /// 60 | public static string PluginDirectory 61 | { 62 | get 63 | { 64 | return Path.Combine(ApplicationDirectory, "Plugins"); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/PropertySorter.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using System; 27 | using System.Collections; 28 | using System.ComponentModel; 29 | #endregion 30 | 31 | namespace OpenTibia.Utils 32 | { 33 | public class PropertySorter : ExpandableObjectConverter 34 | { 35 | #region | Public Methods | 36 | 37 | public override bool GetPropertiesSupported(ITypeDescriptorContext context) 38 | { 39 | return true; 40 | } 41 | 42 | public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) 43 | { 44 | PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attributes); 45 | ArrayList orderedProperties = new ArrayList(); 46 | 47 | foreach (PropertyDescriptor pd in pdc) 48 | { 49 | Attribute attribute = pd.Attributes[typeof(PropertyOrderAttribute)]; 50 | if (attribute != null) 51 | { 52 | PropertyOrderAttribute poa = (PropertyOrderAttribute)attribute; 53 | orderedProperties.Add(new PropertyOrderPair(pd.Name, poa.Order)); 54 | } 55 | else 56 | { 57 | orderedProperties.Add(new PropertyOrderPair(pd.Name, 0)); 58 | } 59 | } 60 | 61 | orderedProperties.Sort(); 62 | 63 | ArrayList propertyNames = new ArrayList(); 64 | foreach (PropertyOrderPair pop in orderedProperties) 65 | { 66 | propertyNames.Add(pop.Name); 67 | } 68 | 69 | return pdc.Sort((string[])propertyNames.ToArray(typeof(string))); 70 | } 71 | 72 | #endregion 73 | } 74 | 75 | [AttributeUsage(AttributeTargets.Property)] 76 | public class PropertyOrderAttribute : Attribute 77 | { 78 | #region | Constructor | 79 | 80 | public PropertyOrderAttribute(int order) 81 | { 82 | this.Order = order; 83 | } 84 | 85 | #endregion 86 | 87 | #region | Public Properties | 88 | 89 | public int Order { get; private set; } 90 | 91 | #endregion 92 | } 93 | 94 | public class PropertyOrderPair : IComparable 95 | { 96 | #region Private Properties 97 | 98 | private int order; 99 | 100 | #endregion 101 | 102 | #region | Contructor | 103 | 104 | public PropertyOrderPair(string name, int order) 105 | { 106 | this.order = order; 107 | this.Name = name; 108 | } 109 | 110 | #endregion 111 | 112 | #region | Public Properties | 113 | 114 | public string Name { get; private set; } 115 | 116 | #endregion 117 | 118 | #region | Public Methods | 119 | 120 | public int CompareTo(object obj) 121 | { 122 | int otherOrder = ((PropertyOrderPair)obj).order; 123 | 124 | if (otherOrder == this.order) 125 | { 126 | return string.Compare(this.Name, ((PropertyOrderPair)obj).Name); 127 | } 128 | else if (otherOrder > this.order) 129 | { 130 | return -1; 131 | } 132 | 133 | return 1; 134 | } 135 | 136 | #endregion 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.Utils/SpriteCache.cs: -------------------------------------------------------------------------------- 1 | #region Licence 2 | /** 3 | * Copyright © 2015-2018 OTTools 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy 6 | * of this software and associated documentation files (the "Software"), to deal 7 | * in the Software without restriction, including without limitation the rights 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | * copies of the Software, and to permit persons to whom the Software is 10 | * furnished to do so, subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | * SOFTWARE. 22 | */ 23 | #endregion 24 | 25 | #region Using Statements 26 | using OpenTibia.Client.Things; 27 | using System; 28 | using System.Collections.Generic; 29 | using System.Drawing; 30 | #endregion 31 | 32 | namespace OpenTibia.Utils 33 | { 34 | public class SpriteCache 35 | { 36 | #region Private Properties 37 | 38 | private Dictionary items; 39 | private Dictionary outfits; 40 | private Dictionary effects; 41 | private Dictionary missiles; 42 | 43 | #endregion 44 | 45 | #region Constructor 46 | 47 | public SpriteCache() 48 | { 49 | this.items = new Dictionary(); 50 | this.outfits = new Dictionary(); 51 | this.effects = new Dictionary(); 52 | this.missiles = new Dictionary(); 53 | } 54 | 55 | #endregion 56 | 57 | #region Public Methods 58 | 59 | public void SetPicture(ushort id, ThingCategory category, Bitmap bitmap) 60 | { 61 | switch (category) 62 | { 63 | case ThingCategory.Item: 64 | this.items[id] = bitmap; 65 | break; 66 | 67 | case ThingCategory.Outfit: 68 | this.outfits[id] = bitmap; 69 | break; 70 | 71 | case ThingCategory.Effect: 72 | this.effects[id] = bitmap; 73 | break; 74 | 75 | case ThingCategory.Missile: 76 | this.missiles[id] = bitmap; 77 | break; 78 | } 79 | } 80 | 81 | public Bitmap GetPicture(ushort id, ThingCategory category) 82 | { 83 | if (category == ThingCategory.Item && this.items.ContainsKey(id)) 84 | { 85 | return this.items[id]; 86 | } 87 | else if (category == ThingCategory.Outfit && this.outfits.ContainsKey(id)) 88 | { 89 | return this.outfits[id]; 90 | } 91 | else if (category == ThingCategory.Effect && this.effects.ContainsKey(id)) 92 | { 93 | return this.effects[id]; 94 | } 95 | else if (category == ThingCategory.Missile && this.missiles.ContainsKey(id)) 96 | { 97 | return this.missiles[id]; 98 | } 99 | 100 | return null; 101 | } 102 | 103 | public void Clear() 104 | { 105 | this.items.Clear(); 106 | this.outfits.Clear(); 107 | this.effects.Clear(); 108 | this.missiles.Clear(); 109 | } 110 | 111 | #endregion 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {AED9EA24-52F5-46A4-9F62-2462EA077123} 8 | Library 9 | Properties 10 | OpenTibia 11 | OpenTibia 12 | v4.6.1 13 | 512 14 | 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | Component 71 | 72 | 73 | Component 74 | 75 | 76 | Component 77 | 78 | 79 | Component 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | {cd9d40f7-4d62-4ca3-bac1-237e69e2f3a7} 105 | 7zip 106 | 107 | 108 | 109 | 116 | -------------------------------------------------------------------------------- /OpenTibia/OpenTibia.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTibia", "OpenTibia.csproj", "{AED9EA24-52F5-46A4-9F62-2462EA077123}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "7zip", "..\ThirdParty\7zip\7zip.csproj", "{CD9D40F7-4D62-4CA3-BAC1-237E69E2F3A7}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {AED9EA24-52F5-46A4-9F62-2462EA077123}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {AED9EA24-52F5-46A4-9F62-2462EA077123}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {AED9EA24-52F5-46A4-9F62-2462EA077123}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {AED9EA24-52F5-46A4-9F62-2462EA077123}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {CD9D40F7-4D62-4CA3-BAC1-237E69E2F3A7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {CD9D40F7-4D62-4CA3-BAC1-237E69E2F3A7}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {CD9D40F7-4D62-4CA3-BAC1-237E69E2F3A7}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {CD9D40F7-4D62-4CA3-BAC1-237E69E2F3A7}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {9BF8B21A-6221-42BE-8A6E-433708912783} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /OpenTibia/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("OpenTibia")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("OpenTibia")] 13 | [assembly: AssemblyCopyright("Copyright © OTTools 2016-2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3717ae2a-1698-4da0-b87d-0af15f6fd928")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("0.6.3.0")] 36 | [assembly: AssemblyFileVersion("0.6.3.0")] 37 | -------------------------------------------------------------------------------- /OpenTibia/Settings.StyleCop: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | False 8 | 9 | 10 | 11 | 12 | False 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | False 23 | 24 | 25 | 26 | 27 | False 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### Open Tibia Framework 2 | 3 | --- 4 | 5 | #### Loading versions from a xml file 6 | 7 | XML format sample 8 | ``` XML 9 | 10 | 11 | 12 | ``` 13 | Code 14 | ``` C# 15 | // path to the versions xml file. 16 | string path = @"versions.xml"; 17 | 18 | // creates a VersionStorage instance. 19 | OpenTibia.Core.VersionStorage versions = new OpenTibia.Core.VersionStorage(); 20 | 21 | // loads the xml 22 | versions.Load(path); 23 | 24 | // gets a version from the storage by the signatures. 25 | OpenTibia.Core.Version version = versions.GetBySignatures(0x3A71, 0x557A5E34); 26 | 27 | // gets all versions 10.79 28 | System.Collections.Generic.List result = versions.GetByVersionValue(1079); 29 | 30 | // adds a new version. 31 | versions.AddVersion(new OpenTibia.Core.Version(1078, "Client 10.78", 0x39CC, 0x554C7373, 56)); 32 | 33 | // replaces a version by the signatures. 34 | versions.ReplaceVersion(new OpenTibia.Core.Version(1078, "My description 10.78", 0x39CC, 0x554C7373, 56), 0x39CC, 0x554C7373); 35 | 36 | // removes a version by the signatures. 37 | versions.RemoveVersion(0x39CC, 0x554C7373); 38 | 39 | // saves the xml. 40 | versions.Save(); 41 | ``` 42 | 43 | --- 44 | 45 | #### Loading and compiling a SPR file 46 | 47 | ``` C# 48 | // creates a Version 10.79. 49 | OpenTibia.Core.Version version = new OpenTibia.Core.Version(1079, "Client 10.79", 0x3A71, 0x557A5E34, 0); 50 | 51 | // the path to the spr file. 52 | string path = @"C:\Clients\10.79\Tibia.spr"; 53 | 54 | // loads the spr file. 55 | OpenTibia.Client.Sprites.SpriteStorage sprites = OpenTibia.Client.Sprites.SpriteStorage.Load(path, version); 56 | 57 | // gets a sprite from the storage 58 | OpenTibia.Client.Sprites.Sprite sprite = sprites.GetSprite(100); 59 | 60 | // adding a sprite. 61 | sprites.AddSprite(new OpenTibia.Client.Sprites.Sprite()); 62 | 63 | // replacing a sprite. 64 | sprites.ReplaceSprite(new OpenTibia.Client.Sprites.Sprite(), 12); 65 | 66 | // removing a sprite. 67 | sprites.RemoveSprite(10); 68 | 69 | // compiles the spr file. 70 | sprites.Save(); 71 | ``` 72 | 73 | --- 74 | 75 | #### Loading and displaying sprites 76 | 77 | ``` C# 78 | // Assuming that you have a SpriteListBox named 'spriteListBox' in the form. 79 | 80 | // creates a Version 10.79. 81 | OpenTibia.Core.Version version = new OpenTibia.Core.Version(1079, "Client 10.79", 0x3A71, 0x557A5E34, 0); 82 | 83 | // the path to the spr file. 84 | string path = @"C:\Clients\10.79\Tibia.spr"; 85 | 86 | // loads the spr file. 87 | OpenTibia.Client.Sprites.SpriteStorage sprites = OpenTibia.Client.Sprites.SpriteStorage.Load(path, version); 88 | 89 | // gets 100 sprites from the storage and displays in the SpriteListBox 90 | OpenTibia.Client.Sprites.Sprite[] list = new OpenTibia.Client.Sprites.Sprite[100]; 91 | 92 | for (uint i = 0; i < list.Length; i++) 93 | { 94 | list[i] = sprites.GetSprite(i); 95 | } 96 | 97 | this.spriteListBox.AddRange(list); 98 | ``` 99 | 100 | --- 101 | 102 | #### SpriteListBox control 103 | 104 | A control to display a list of sprites. 105 | 106 | ![SpriteListBox](http://s22.postimg.org/48ttg8xpd/Sprite_List_Box.png) 107 | 108 | #### EightBitColorGrid control 109 | 110 | A control that displays minimap and light colors. 111 | 112 | ![EightBitColorGrid](http://s15.postimg.org/ah0eeme8b/Hsi_Color_Grid.png) 113 | 114 | #### HsiColorGrid control 115 | 116 | A control that displays outfit colors. 117 | 118 | ![HsiColorGrid](http://s13.postimg.org/526xlym2f/Eight_Bit_Color_Grid.png) 119 | -------------------------------------------------------------------------------- /ThirdParty/7zip/7zip.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | 6 | 7 | 8 | 7.2 9 | 10 | 11 | 12 | 7.2 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Common/CRC.cs: -------------------------------------------------------------------------------- 1 | // Common/CRC.cs 2 | 3 | namespace SevenZip 4 | { 5 | class CRC 6 | { 7 | public static readonly uint[] Table; 8 | 9 | static CRC() 10 | { 11 | Table = new uint[256]; 12 | const uint kPoly = 0xEDB88320; 13 | for (uint i = 0; i < 256; i++) 14 | { 15 | uint r = i; 16 | for (int j = 0; j < 8; j++) 17 | if ((r & 1) != 0) 18 | r = (r >> 1) ^ kPoly; 19 | else 20 | r >>= 1; 21 | Table[i] = r; 22 | } 23 | } 24 | 25 | uint _value = 0xFFFFFFFF; 26 | 27 | public void Init() { _value = 0xFFFFFFFF; } 28 | 29 | public void UpdateByte(byte b) 30 | { 31 | _value = Table[(((byte)(_value)) ^ b)] ^ (_value >> 8); 32 | } 33 | 34 | public void Update(byte[] data, uint offset, uint size) 35 | { 36 | for (uint i = 0; i < size; i++) 37 | _value = Table[(((byte)(_value)) ^ data[offset + i])] ^ (_value >> 8); 38 | } 39 | 40 | public uint GetDigest() { return _value ^ 0xFFFFFFFF; } 41 | 42 | static uint CalculateDigest(byte[] data, uint offset, uint size) 43 | { 44 | CRC crc = new CRC(); 45 | // crc.Init(); 46 | crc.Update(data, offset, size); 47 | return crc.GetDigest(); 48 | } 49 | 50 | static bool VerifyDigest(uint digest, byte[] data, uint offset, uint size) 51 | { 52 | return (CalculateDigest(data, offset, size) == digest); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Common/CommandLineParser.cs: -------------------------------------------------------------------------------- 1 | // CommandLineParser.cs 2 | 3 | using System; 4 | using System.Collections; 5 | 6 | namespace SevenZip.CommandLineParser 7 | { 8 | public enum SwitchType 9 | { 10 | Simple, 11 | PostMinus, 12 | LimitedPostString, 13 | UnLimitedPostString, 14 | PostChar 15 | } 16 | 17 | public class SwitchForm 18 | { 19 | public string IDString; 20 | public SwitchType Type; 21 | public bool Multi; 22 | public int MinLen; 23 | public int MaxLen; 24 | public string PostCharSet; 25 | 26 | public SwitchForm(string idString, SwitchType type, bool multi, 27 | int minLen, int maxLen, string postCharSet) 28 | { 29 | IDString = idString; 30 | Type = type; 31 | Multi = multi; 32 | MinLen = minLen; 33 | MaxLen = maxLen; 34 | PostCharSet = postCharSet; 35 | } 36 | public SwitchForm(string idString, SwitchType type, bool multi, int minLen): 37 | this(idString, type, multi, minLen, 0, "") 38 | { 39 | } 40 | public SwitchForm(string idString, SwitchType type, bool multi): 41 | this(idString, type, multi, 0) 42 | { 43 | } 44 | } 45 | 46 | public class SwitchResult 47 | { 48 | public bool ThereIs; 49 | public bool WithMinus; 50 | public ArrayList PostStrings = new ArrayList(); 51 | public int PostCharIndex; 52 | public SwitchResult() 53 | { 54 | ThereIs = false; 55 | } 56 | } 57 | 58 | public class Parser 59 | { 60 | public ArrayList NonSwitchStrings = new ArrayList(); 61 | SwitchResult[] _switches; 62 | 63 | public Parser(int numSwitches) 64 | { 65 | _switches = new SwitchResult[numSwitches]; 66 | for (int i = 0; i < numSwitches; i++) 67 | _switches[i] = new SwitchResult(); 68 | } 69 | 70 | bool ParseString(string srcString, SwitchForm[] switchForms) 71 | { 72 | int len = srcString.Length; 73 | if (len == 0) 74 | return false; 75 | int pos = 0; 76 | if (!IsItSwitchChar(srcString[pos])) 77 | return false; 78 | while (pos < len) 79 | { 80 | if (IsItSwitchChar(srcString[pos])) 81 | pos++; 82 | const int kNoLen = -1; 83 | int matchedSwitchIndex = 0; 84 | int maxLen = kNoLen; 85 | for (int switchIndex = 0; switchIndex < _switches.Length; switchIndex++) 86 | { 87 | int switchLen = switchForms[switchIndex].IDString.Length; 88 | if (switchLen <= maxLen || pos + switchLen > len) 89 | continue; 90 | if (String.Compare(switchForms[switchIndex].IDString, 0, 91 | srcString, pos, switchLen, true) == 0) 92 | { 93 | matchedSwitchIndex = switchIndex; 94 | maxLen = switchLen; 95 | } 96 | } 97 | if (maxLen == kNoLen) 98 | throw new Exception("maxLen == kNoLen"); 99 | SwitchResult matchedSwitch = _switches[matchedSwitchIndex]; 100 | SwitchForm switchForm = switchForms[matchedSwitchIndex]; 101 | if ((!switchForm.Multi) && matchedSwitch.ThereIs) 102 | throw new Exception("switch must be single"); 103 | matchedSwitch.ThereIs = true; 104 | pos += maxLen; 105 | int tailSize = len - pos; 106 | SwitchType type = switchForm.Type; 107 | switch (type) 108 | { 109 | case SwitchType.PostMinus: 110 | { 111 | if (tailSize == 0) 112 | matchedSwitch.WithMinus = false; 113 | else 114 | { 115 | matchedSwitch.WithMinus = (srcString[pos] == kSwitchMinus); 116 | if (matchedSwitch.WithMinus) 117 | pos++; 118 | } 119 | break; 120 | } 121 | case SwitchType.PostChar: 122 | { 123 | if (tailSize < switchForm.MinLen) 124 | throw new Exception("switch is not full"); 125 | string charSet = switchForm.PostCharSet; 126 | const int kEmptyCharValue = -1; 127 | if (tailSize == 0) 128 | matchedSwitch.PostCharIndex = kEmptyCharValue; 129 | else 130 | { 131 | int index = charSet.IndexOf(srcString[pos]); 132 | if (index < 0) 133 | matchedSwitch.PostCharIndex = kEmptyCharValue; 134 | else 135 | { 136 | matchedSwitch.PostCharIndex = index; 137 | pos++; 138 | } 139 | } 140 | break; 141 | } 142 | case SwitchType.LimitedPostString: 143 | case SwitchType.UnLimitedPostString: 144 | { 145 | int minLen = switchForm.MinLen; 146 | if (tailSize < minLen) 147 | throw new Exception("switch is not full"); 148 | if (type == SwitchType.UnLimitedPostString) 149 | { 150 | matchedSwitch.PostStrings.Add(srcString.Substring(pos)); 151 | return true; 152 | } 153 | String stringSwitch = srcString.Substring(pos, minLen); 154 | pos += minLen; 155 | for (int i = minLen; i < switchForm.MaxLen && pos < len; i++, pos++) 156 | { 157 | char c = srcString[pos]; 158 | if (IsItSwitchChar(c)) 159 | break; 160 | stringSwitch += c; 161 | } 162 | matchedSwitch.PostStrings.Add(stringSwitch); 163 | break; 164 | } 165 | } 166 | } 167 | return true; 168 | 169 | } 170 | 171 | public void ParseStrings(SwitchForm[] switchForms, string[] commandStrings) 172 | { 173 | int numCommandStrings = commandStrings.Length; 174 | bool stopSwitch = false; 175 | for (int i = 0; i < numCommandStrings; i++) 176 | { 177 | string s = commandStrings[i]; 178 | if (stopSwitch) 179 | NonSwitchStrings.Add(s); 180 | else 181 | if (s == kStopSwitchParsing) 182 | stopSwitch = true; 183 | else 184 | if (!ParseString(s, switchForms)) 185 | NonSwitchStrings.Add(s); 186 | } 187 | } 188 | 189 | public SwitchResult this[int index] { get { return _switches[index]; } } 190 | 191 | public static int ParseCommand(CommandForm[] commandForms, string commandString, 192 | out string postString) 193 | { 194 | for (int i = 0; i < commandForms.Length; i++) 195 | { 196 | string id = commandForms[i].IDString; 197 | if (commandForms[i].PostStringMode) 198 | { 199 | if (commandString.IndexOf(id) == 0) 200 | { 201 | postString = commandString.Substring(id.Length); 202 | return i; 203 | } 204 | } 205 | else 206 | if (commandString == id) 207 | { 208 | postString = ""; 209 | return i; 210 | } 211 | } 212 | postString = ""; 213 | return -1; 214 | } 215 | 216 | static bool ParseSubCharsCommand(int numForms, CommandSubCharsSet[] forms, 217 | string commandString, ArrayList indices) 218 | { 219 | indices.Clear(); 220 | int numUsedChars = 0; 221 | for (int i = 0; i < numForms; i++) 222 | { 223 | CommandSubCharsSet charsSet = forms[i]; 224 | int currentIndex = -1; 225 | int len = charsSet.Chars.Length; 226 | for (int j = 0; j < len; j++) 227 | { 228 | char c = charsSet.Chars[j]; 229 | int newIndex = commandString.IndexOf(c); 230 | if (newIndex >= 0) 231 | { 232 | if (currentIndex >= 0) 233 | return false; 234 | if (commandString.IndexOf(c, newIndex + 1) >= 0) 235 | return false; 236 | currentIndex = j; 237 | numUsedChars++; 238 | } 239 | } 240 | if (currentIndex == -1 && !charsSet.EmptyAllowed) 241 | return false; 242 | indices.Add(currentIndex); 243 | } 244 | return (numUsedChars == commandString.Length); 245 | } 246 | const char kSwitchID1 = '-'; 247 | const char kSwitchID2 = '/'; 248 | 249 | const char kSwitchMinus = '-'; 250 | const string kStopSwitchParsing = "--"; 251 | 252 | static bool IsItSwitchChar(char c) 253 | { 254 | return (c == kSwitchID1 || c == kSwitchID2); 255 | } 256 | } 257 | 258 | public class CommandForm 259 | { 260 | public string IDString = ""; 261 | public bool PostStringMode = false; 262 | public CommandForm(string idString, bool postStringMode) 263 | { 264 | IDString = idString; 265 | PostStringMode = postStringMode; 266 | } 267 | } 268 | 269 | class CommandSubCharsSet 270 | { 271 | public string Chars = ""; 272 | public bool EmptyAllowed = false; 273 | } 274 | } 275 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Common/InBuffer.cs: -------------------------------------------------------------------------------- 1 | // InBuffer.cs 2 | 3 | namespace SevenZip.Buffer 4 | { 5 | public class InBuffer 6 | { 7 | byte[] m_Buffer; 8 | uint m_Pos; 9 | uint m_Limit; 10 | uint m_BufferSize; 11 | System.IO.Stream m_Stream; 12 | bool m_StreamWasExhausted; 13 | ulong m_ProcessedSize; 14 | 15 | public InBuffer(uint bufferSize) 16 | { 17 | m_Buffer = new byte[bufferSize]; 18 | m_BufferSize = bufferSize; 19 | } 20 | 21 | public void Init(System.IO.Stream stream) 22 | { 23 | m_Stream = stream; 24 | m_ProcessedSize = 0; 25 | m_Limit = 0; 26 | m_Pos = 0; 27 | m_StreamWasExhausted = false; 28 | } 29 | 30 | public bool ReadBlock() 31 | { 32 | if (m_StreamWasExhausted) 33 | return false; 34 | m_ProcessedSize += m_Pos; 35 | int aNumProcessedBytes = m_Stream.Read(m_Buffer, 0, (int)m_BufferSize); 36 | m_Pos = 0; 37 | m_Limit = (uint)aNumProcessedBytes; 38 | m_StreamWasExhausted = (aNumProcessedBytes == 0); 39 | return (!m_StreamWasExhausted); 40 | } 41 | 42 | 43 | public void ReleaseStream() 44 | { 45 | // m_Stream.Close(); 46 | m_Stream = null; 47 | } 48 | 49 | public bool ReadByte(byte b) // check it 50 | { 51 | if (m_Pos >= m_Limit) 52 | if (!ReadBlock()) 53 | return false; 54 | b = m_Buffer[m_Pos++]; 55 | return true; 56 | } 57 | 58 | public byte ReadByte() 59 | { 60 | // return (byte)m_Stream.ReadByte(); 61 | if (m_Pos >= m_Limit) 62 | if (!ReadBlock()) 63 | return 0xFF; 64 | return m_Buffer[m_Pos++]; 65 | } 66 | 67 | public ulong GetProcessedSize() 68 | { 69 | return m_ProcessedSize + m_Pos; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Common/OutBuffer.cs: -------------------------------------------------------------------------------- 1 | // OutBuffer.cs 2 | 3 | namespace SevenZip.Buffer 4 | { 5 | public class OutBuffer 6 | { 7 | byte[] m_Buffer; 8 | uint m_Pos; 9 | uint m_BufferSize; 10 | System.IO.Stream m_Stream; 11 | ulong m_ProcessedSize; 12 | 13 | public OutBuffer(uint bufferSize) 14 | { 15 | m_Buffer = new byte[bufferSize]; 16 | m_BufferSize = bufferSize; 17 | } 18 | 19 | public void SetStream(System.IO.Stream stream) { m_Stream = stream; } 20 | public void FlushStream() { m_Stream.Flush(); } 21 | public void CloseStream() { m_Stream.Close(); } 22 | public void ReleaseStream() { m_Stream = null; } 23 | 24 | public void Init() 25 | { 26 | m_ProcessedSize = 0; 27 | m_Pos = 0; 28 | } 29 | 30 | public void WriteByte(byte b) 31 | { 32 | m_Buffer[m_Pos++] = b; 33 | if (m_Pos >= m_BufferSize) 34 | FlushData(); 35 | } 36 | 37 | public void FlushData() 38 | { 39 | if (m_Pos == 0) 40 | return; 41 | m_Stream.Write(m_Buffer, 0, (int)m_Pos); 42 | m_Pos = 0; 43 | } 44 | 45 | public ulong GetProcessedSize() { return m_ProcessedSize + m_Pos; } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/LZ/IMatchFinder.cs: -------------------------------------------------------------------------------- 1 | // IMatchFinder.cs 2 | 3 | using System; 4 | 5 | namespace SevenZip.Compression.LZ 6 | { 7 | interface IInWindowStream 8 | { 9 | void SetStream(System.IO.Stream inStream); 10 | void Init(); 11 | void ReleaseStream(); 12 | Byte GetIndexByte(Int32 index); 13 | UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit); 14 | UInt32 GetNumAvailableBytes(); 15 | } 16 | 17 | interface IMatchFinder : IInWindowStream 18 | { 19 | void Create(UInt32 historySize, UInt32 keepAddBufferBefore, 20 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter); 21 | UInt32 GetMatches(UInt32[] distances); 22 | void Skip(UInt32 num); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/LZ/LzBinTree.cs: -------------------------------------------------------------------------------- 1 | // LzBinTree.cs 2 | 3 | using System; 4 | 5 | namespace SevenZip.Compression.LZ 6 | { 7 | public class BinTree : InWindow, IMatchFinder 8 | { 9 | UInt32 _cyclicBufferPos; 10 | UInt32 _cyclicBufferSize = 0; 11 | UInt32 _matchMaxLen; 12 | 13 | UInt32[] _son; 14 | UInt32[] _hash; 15 | 16 | UInt32 _cutValue = 0xFF; 17 | UInt32 _hashMask; 18 | UInt32 _hashSizeSum = 0; 19 | 20 | bool HASH_ARRAY = true; 21 | 22 | const UInt32 kHash2Size = 1 << 10; 23 | const UInt32 kHash3Size = 1 << 16; 24 | const UInt32 kBT2HashSize = 1 << 16; 25 | const UInt32 kStartMaxLen = 1; 26 | const UInt32 kHash3Offset = kHash2Size; 27 | const UInt32 kEmptyHashValue = 0; 28 | const UInt32 kMaxValForNormalize = ((UInt32)1 << 31) - 1; 29 | 30 | UInt32 kNumHashDirectBytes = 0; 31 | UInt32 kMinMatchCheck = 4; 32 | UInt32 kFixHashSize = kHash2Size + kHash3Size; 33 | 34 | public void SetType(int numHashBytes) 35 | { 36 | HASH_ARRAY = (numHashBytes > 2); 37 | if (HASH_ARRAY) 38 | { 39 | kNumHashDirectBytes = 0; 40 | kMinMatchCheck = 4; 41 | kFixHashSize = kHash2Size + kHash3Size; 42 | } 43 | else 44 | { 45 | kNumHashDirectBytes = 2; 46 | kMinMatchCheck = 2 + 1; 47 | kFixHashSize = 0; 48 | } 49 | } 50 | 51 | public new void SetStream(System.IO.Stream stream) { base.SetStream(stream); } 52 | public new void ReleaseStream() { base.ReleaseStream(); } 53 | 54 | public new void Init() 55 | { 56 | base.Init(); 57 | for (UInt32 i = 0; i < _hashSizeSum; i++) 58 | _hash[i] = kEmptyHashValue; 59 | _cyclicBufferPos = 0; 60 | ReduceOffsets(-1); 61 | } 62 | 63 | public new void MovePos() 64 | { 65 | if (++_cyclicBufferPos >= _cyclicBufferSize) 66 | _cyclicBufferPos = 0; 67 | base.MovePos(); 68 | if (_pos == kMaxValForNormalize) 69 | Normalize(); 70 | } 71 | 72 | public new Byte GetIndexByte(Int32 index) { return base.GetIndexByte(index); } 73 | 74 | public new UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) 75 | { return base.GetMatchLen(index, distance, limit); } 76 | 77 | public new UInt32 GetNumAvailableBytes() { return base.GetNumAvailableBytes(); } 78 | 79 | public void Create(UInt32 historySize, UInt32 keepAddBufferBefore, 80 | UInt32 matchMaxLen, UInt32 keepAddBufferAfter) 81 | { 82 | if (historySize > kMaxValForNormalize - 256) 83 | throw new Exception(); 84 | _cutValue = 16 + (matchMaxLen >> 1); 85 | 86 | UInt32 windowReservSize = (historySize + keepAddBufferBefore + 87 | matchMaxLen + keepAddBufferAfter) / 2 + 256; 88 | 89 | base.Create(historySize + keepAddBufferBefore, matchMaxLen + keepAddBufferAfter, windowReservSize); 90 | 91 | _matchMaxLen = matchMaxLen; 92 | 93 | UInt32 cyclicBufferSize = historySize + 1; 94 | if (_cyclicBufferSize != cyclicBufferSize) 95 | _son = new UInt32[(_cyclicBufferSize = cyclicBufferSize) * 2]; 96 | 97 | UInt32 hs = kBT2HashSize; 98 | 99 | if (HASH_ARRAY) 100 | { 101 | hs = historySize - 1; 102 | hs |= (hs >> 1); 103 | hs |= (hs >> 2); 104 | hs |= (hs >> 4); 105 | hs |= (hs >> 8); 106 | hs >>= 1; 107 | hs |= 0xFFFF; 108 | if (hs > (1 << 24)) 109 | hs >>= 1; 110 | _hashMask = hs; 111 | hs++; 112 | hs += kFixHashSize; 113 | } 114 | if (hs != _hashSizeSum) 115 | _hash = new UInt32[_hashSizeSum = hs]; 116 | } 117 | 118 | public UInt32 GetMatches(UInt32[] distances) 119 | { 120 | UInt32 lenLimit; 121 | if (_pos + _matchMaxLen <= _streamPos) 122 | lenLimit = _matchMaxLen; 123 | else 124 | { 125 | lenLimit = _streamPos - _pos; 126 | if (lenLimit < kMinMatchCheck) 127 | { 128 | MovePos(); 129 | return 0; 130 | } 131 | } 132 | 133 | UInt32 offset = 0; 134 | UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0; 135 | UInt32 cur = _bufferOffset + _pos; 136 | UInt32 maxLen = kStartMaxLen; // to avoid items for len < hashSize; 137 | UInt32 hashValue, hash2Value = 0, hash3Value = 0; 138 | 139 | if (HASH_ARRAY) 140 | { 141 | UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1]; 142 | hash2Value = temp & (kHash2Size - 1); 143 | temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8); 144 | hash3Value = temp & (kHash3Size - 1); 145 | hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask; 146 | } 147 | else 148 | hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8); 149 | 150 | UInt32 curMatch = _hash[kFixHashSize + hashValue]; 151 | if (HASH_ARRAY) 152 | { 153 | UInt32 curMatch2 = _hash[hash2Value]; 154 | UInt32 curMatch3 = _hash[kHash3Offset + hash3Value]; 155 | _hash[hash2Value] = _pos; 156 | _hash[kHash3Offset + hash3Value] = _pos; 157 | if (curMatch2 > matchMinPos) 158 | if (_bufferBase[_bufferOffset + curMatch2] == _bufferBase[cur]) 159 | { 160 | distances[offset++] = maxLen = 2; 161 | distances[offset++] = _pos - curMatch2 - 1; 162 | } 163 | if (curMatch3 > matchMinPos) 164 | if (_bufferBase[_bufferOffset + curMatch3] == _bufferBase[cur]) 165 | { 166 | if (curMatch3 == curMatch2) 167 | offset -= 2; 168 | distances[offset++] = maxLen = 3; 169 | distances[offset++] = _pos - curMatch3 - 1; 170 | curMatch2 = curMatch3; 171 | } 172 | if (offset != 0 && curMatch2 == curMatch) 173 | { 174 | offset -= 2; 175 | maxLen = kStartMaxLen; 176 | } 177 | } 178 | 179 | _hash[kFixHashSize + hashValue] = _pos; 180 | 181 | UInt32 ptr0 = (_cyclicBufferPos << 1) + 1; 182 | UInt32 ptr1 = (_cyclicBufferPos << 1); 183 | 184 | UInt32 len0, len1; 185 | len0 = len1 = kNumHashDirectBytes; 186 | 187 | if (kNumHashDirectBytes != 0) 188 | { 189 | if (curMatch > matchMinPos) 190 | { 191 | if (_bufferBase[_bufferOffset + curMatch + kNumHashDirectBytes] != 192 | _bufferBase[cur + kNumHashDirectBytes]) 193 | { 194 | distances[offset++] = maxLen = kNumHashDirectBytes; 195 | distances[offset++] = _pos - curMatch - 1; 196 | } 197 | } 198 | } 199 | 200 | UInt32 count = _cutValue; 201 | 202 | while(true) 203 | { 204 | if(curMatch <= matchMinPos || count-- == 0) 205 | { 206 | _son[ptr0] = _son[ptr1] = kEmptyHashValue; 207 | break; 208 | } 209 | UInt32 delta = _pos - curMatch; 210 | UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ? 211 | (_cyclicBufferPos - delta) : 212 | (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1; 213 | 214 | UInt32 pby1 = _bufferOffset + curMatch; 215 | UInt32 len = Math.Min(len0, len1); 216 | if (_bufferBase[pby1 + len] == _bufferBase[cur + len]) 217 | { 218 | while(++len != lenLimit) 219 | if (_bufferBase[pby1 + len] != _bufferBase[cur + len]) 220 | break; 221 | if (maxLen < len) 222 | { 223 | distances[offset++] = maxLen = len; 224 | distances[offset++] = delta - 1; 225 | if (len == lenLimit) 226 | { 227 | _son[ptr1] = _son[cyclicPos]; 228 | _son[ptr0] = _son[cyclicPos + 1]; 229 | break; 230 | } 231 | } 232 | } 233 | if (_bufferBase[pby1 + len] < _bufferBase[cur + len]) 234 | { 235 | _son[ptr1] = curMatch; 236 | ptr1 = cyclicPos + 1; 237 | curMatch = _son[ptr1]; 238 | len1 = len; 239 | } 240 | else 241 | { 242 | _son[ptr0] = curMatch; 243 | ptr0 = cyclicPos; 244 | curMatch = _son[ptr0]; 245 | len0 = len; 246 | } 247 | } 248 | MovePos(); 249 | return offset; 250 | } 251 | 252 | public void Skip(UInt32 num) 253 | { 254 | do 255 | { 256 | UInt32 lenLimit; 257 | if (_pos + _matchMaxLen <= _streamPos) 258 | lenLimit = _matchMaxLen; 259 | else 260 | { 261 | lenLimit = _streamPos - _pos; 262 | if (lenLimit < kMinMatchCheck) 263 | { 264 | MovePos(); 265 | continue; 266 | } 267 | } 268 | 269 | UInt32 matchMinPos = (_pos > _cyclicBufferSize) ? (_pos - _cyclicBufferSize) : 0; 270 | UInt32 cur = _bufferOffset + _pos; 271 | 272 | UInt32 hashValue; 273 | 274 | if (HASH_ARRAY) 275 | { 276 | UInt32 temp = CRC.Table[_bufferBase[cur]] ^ _bufferBase[cur + 1]; 277 | UInt32 hash2Value = temp & (kHash2Size - 1); 278 | _hash[hash2Value] = _pos; 279 | temp ^= ((UInt32)(_bufferBase[cur + 2]) << 8); 280 | UInt32 hash3Value = temp & (kHash3Size - 1); 281 | _hash[kHash3Offset + hash3Value] = _pos; 282 | hashValue = (temp ^ (CRC.Table[_bufferBase[cur + 3]] << 5)) & _hashMask; 283 | } 284 | else 285 | hashValue = _bufferBase[cur] ^ ((UInt32)(_bufferBase[cur + 1]) << 8); 286 | 287 | UInt32 curMatch = _hash[kFixHashSize + hashValue]; 288 | _hash[kFixHashSize + hashValue] = _pos; 289 | 290 | UInt32 ptr0 = (_cyclicBufferPos << 1) + 1; 291 | UInt32 ptr1 = (_cyclicBufferPos << 1); 292 | 293 | UInt32 len0, len1; 294 | len0 = len1 = kNumHashDirectBytes; 295 | 296 | UInt32 count = _cutValue; 297 | while (true) 298 | { 299 | if (curMatch <= matchMinPos || count-- == 0) 300 | { 301 | _son[ptr0] = _son[ptr1] = kEmptyHashValue; 302 | break; 303 | } 304 | 305 | UInt32 delta = _pos - curMatch; 306 | UInt32 cyclicPos = ((delta <= _cyclicBufferPos) ? 307 | (_cyclicBufferPos - delta) : 308 | (_cyclicBufferPos - delta + _cyclicBufferSize)) << 1; 309 | 310 | UInt32 pby1 = _bufferOffset + curMatch; 311 | UInt32 len = Math.Min(len0, len1); 312 | if (_bufferBase[pby1 + len] == _bufferBase[cur + len]) 313 | { 314 | while (++len != lenLimit) 315 | if (_bufferBase[pby1 + len] != _bufferBase[cur + len]) 316 | break; 317 | if (len == lenLimit) 318 | { 319 | _son[ptr1] = _son[cyclicPos]; 320 | _son[ptr0] = _son[cyclicPos + 1]; 321 | break; 322 | } 323 | } 324 | if (_bufferBase[pby1 + len] < _bufferBase[cur + len]) 325 | { 326 | _son[ptr1] = curMatch; 327 | ptr1 = cyclicPos + 1; 328 | curMatch = _son[ptr1]; 329 | len1 = len; 330 | } 331 | else 332 | { 333 | _son[ptr0] = curMatch; 334 | ptr0 = cyclicPos; 335 | curMatch = _son[ptr0]; 336 | len0 = len; 337 | } 338 | } 339 | MovePos(); 340 | } 341 | while (--num != 0); 342 | } 343 | 344 | void NormalizeLinks(UInt32[] items, UInt32 numItems, UInt32 subValue) 345 | { 346 | for (UInt32 i = 0; i < numItems; i++) 347 | { 348 | UInt32 value = items[i]; 349 | if (value <= subValue) 350 | value = kEmptyHashValue; 351 | else 352 | value -= subValue; 353 | items[i] = value; 354 | } 355 | } 356 | 357 | void Normalize() 358 | { 359 | UInt32 subValue = _pos - _cyclicBufferSize; 360 | NormalizeLinks(_son, _cyclicBufferSize * 2, subValue); 361 | NormalizeLinks(_hash, _hashSizeSum, subValue); 362 | ReduceOffsets((Int32)subValue); 363 | } 364 | 365 | public void SetCutValue(UInt32 cutValue) { _cutValue = cutValue; } 366 | } 367 | } 368 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/LZ/LzInWindow.cs: -------------------------------------------------------------------------------- 1 | // LzInWindow.cs 2 | 3 | using System; 4 | 5 | namespace SevenZip.Compression.LZ 6 | { 7 | public class InWindow 8 | { 9 | public Byte[] _bufferBase = null; // pointer to buffer with data 10 | System.IO.Stream _stream; 11 | UInt32 _posLimit; // offset (from _buffer) of first byte when new block reading must be done 12 | bool _streamEndWasReached; // if (true) then _streamPos shows real end of stream 13 | 14 | UInt32 _pointerToLastSafePosition; 15 | 16 | public UInt32 _bufferOffset; 17 | 18 | public UInt32 _blockSize; // Size of Allocated memory block 19 | public UInt32 _pos; // offset (from _buffer) of curent byte 20 | UInt32 _keepSizeBefore; // how many BYTEs must be kept in buffer before _pos 21 | UInt32 _keepSizeAfter; // how many BYTEs must be kept buffer after _pos 22 | public UInt32 _streamPos; // offset (from _buffer) of first not read byte from Stream 23 | 24 | public void MoveBlock() 25 | { 26 | UInt32 offset = (UInt32)(_bufferOffset) + _pos - _keepSizeBefore; 27 | // we need one additional byte, since MovePos moves on 1 byte. 28 | if (offset > 0) 29 | offset--; 30 | 31 | UInt32 numBytes = (UInt32)(_bufferOffset) + _streamPos - offset; 32 | 33 | // check negative offset ???? 34 | for (UInt32 i = 0; i < numBytes; i++) 35 | _bufferBase[i] = _bufferBase[offset + i]; 36 | _bufferOffset -= offset; 37 | } 38 | 39 | public virtual void ReadBlock() 40 | { 41 | if (_streamEndWasReached) 42 | return; 43 | while (true) 44 | { 45 | int size = (int)((0 - _bufferOffset) + _blockSize - _streamPos); 46 | if (size == 0) 47 | return; 48 | int numReadBytes = _stream.Read(_bufferBase, (int)(_bufferOffset + _streamPos), size); 49 | if (numReadBytes == 0) 50 | { 51 | _posLimit = _streamPos; 52 | UInt32 pointerToPostion = _bufferOffset + _posLimit; 53 | if (pointerToPostion > _pointerToLastSafePosition) 54 | _posLimit = (UInt32)(_pointerToLastSafePosition - _bufferOffset); 55 | 56 | _streamEndWasReached = true; 57 | return; 58 | } 59 | _streamPos += (UInt32)numReadBytes; 60 | if (_streamPos >= _pos + _keepSizeAfter) 61 | _posLimit = _streamPos - _keepSizeAfter; 62 | } 63 | } 64 | 65 | void Free() { _bufferBase = null; } 66 | 67 | public void Create(UInt32 keepSizeBefore, UInt32 keepSizeAfter, UInt32 keepSizeReserv) 68 | { 69 | _keepSizeBefore = keepSizeBefore; 70 | _keepSizeAfter = keepSizeAfter; 71 | UInt32 blockSize = keepSizeBefore + keepSizeAfter + keepSizeReserv; 72 | if (_bufferBase == null || _blockSize != blockSize) 73 | { 74 | Free(); 75 | _blockSize = blockSize; 76 | _bufferBase = new Byte[_blockSize]; 77 | } 78 | _pointerToLastSafePosition = _blockSize - keepSizeAfter; 79 | } 80 | 81 | public void SetStream(System.IO.Stream stream) { _stream = stream; } 82 | public void ReleaseStream() { _stream = null; } 83 | 84 | public void Init() 85 | { 86 | _bufferOffset = 0; 87 | _pos = 0; 88 | _streamPos = 0; 89 | _streamEndWasReached = false; 90 | ReadBlock(); 91 | } 92 | 93 | public void MovePos() 94 | { 95 | _pos++; 96 | if (_pos > _posLimit) 97 | { 98 | UInt32 pointerToPostion = _bufferOffset + _pos; 99 | if (pointerToPostion > _pointerToLastSafePosition) 100 | MoveBlock(); 101 | ReadBlock(); 102 | } 103 | } 104 | 105 | public Byte GetIndexByte(Int32 index) { return _bufferBase[_bufferOffset + _pos + index]; } 106 | 107 | // index + limit have not to exceed _keepSizeAfter; 108 | public UInt32 GetMatchLen(Int32 index, UInt32 distance, UInt32 limit) 109 | { 110 | if (_streamEndWasReached) 111 | if ((_pos + index) + limit > _streamPos) 112 | limit = _streamPos - (UInt32)(_pos + index); 113 | distance++; 114 | // Byte *pby = _buffer + (size_t)_pos + index; 115 | UInt32 pby = _bufferOffset + _pos + (UInt32)index; 116 | 117 | UInt32 i; 118 | for (i = 0; i < limit && _bufferBase[pby + i] == _bufferBase[pby + i - distance]; i++); 119 | return i; 120 | } 121 | 122 | public UInt32 GetNumAvailableBytes() { return _streamPos - _pos; } 123 | 124 | public void ReduceOffsets(Int32 subValue) 125 | { 126 | _bufferOffset += (UInt32)subValue; 127 | _posLimit -= (UInt32)subValue; 128 | _pos -= (UInt32)subValue; 129 | _streamPos -= (UInt32)subValue; 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/LZ/LzOutWindow.cs: -------------------------------------------------------------------------------- 1 | // LzOutWindow.cs 2 | 3 | namespace SevenZip.Compression.LZ 4 | { 5 | public class OutWindow 6 | { 7 | byte[] _buffer = null; 8 | uint _pos; 9 | uint _windowSize = 0; 10 | uint _streamPos; 11 | System.IO.Stream _stream; 12 | 13 | public uint TrainSize = 0; 14 | 15 | public void Create(uint windowSize) 16 | { 17 | if (_windowSize != windowSize) 18 | { 19 | // System.GC.Collect(); 20 | _buffer = new byte[windowSize]; 21 | } 22 | _windowSize = windowSize; 23 | _pos = 0; 24 | _streamPos = 0; 25 | } 26 | 27 | public void Init(System.IO.Stream stream, bool solid) 28 | { 29 | ReleaseStream(); 30 | _stream = stream; 31 | if (!solid) 32 | { 33 | _streamPos = 0; 34 | _pos = 0; 35 | TrainSize = 0; 36 | } 37 | } 38 | 39 | public bool Train(System.IO.Stream stream) 40 | { 41 | long len = stream.Length; 42 | uint size = (len < _windowSize) ? (uint)len : _windowSize; 43 | TrainSize = size; 44 | stream.Position = len - size; 45 | _streamPos = _pos = 0; 46 | while (size > 0) 47 | { 48 | uint curSize = _windowSize - _pos; 49 | if (size < curSize) 50 | curSize = size; 51 | int numReadBytes = stream.Read(_buffer, (int)_pos, (int)curSize); 52 | if (numReadBytes == 0) 53 | return false; 54 | size -= (uint)numReadBytes; 55 | _pos += (uint)numReadBytes; 56 | _streamPos += (uint)numReadBytes; 57 | if (_pos == _windowSize) 58 | _streamPos = _pos = 0; 59 | } 60 | return true; 61 | } 62 | 63 | public void ReleaseStream() 64 | { 65 | Flush(); 66 | _stream = null; 67 | } 68 | 69 | public void Flush() 70 | { 71 | uint size = _pos - _streamPos; 72 | if (size == 0) 73 | return; 74 | _stream.Write(_buffer, (int)_streamPos, (int)size); 75 | if (_pos >= _windowSize) 76 | _pos = 0; 77 | _streamPos = _pos; 78 | } 79 | 80 | public void CopyBlock(uint distance, uint len) 81 | { 82 | uint pos = _pos - distance - 1; 83 | if (pos >= _windowSize) 84 | pos += _windowSize; 85 | for (; len > 0; len--) 86 | { 87 | if (pos >= _windowSize) 88 | pos = 0; 89 | _buffer[_pos++] = _buffer[pos++]; 90 | if (_pos >= _windowSize) 91 | Flush(); 92 | } 93 | } 94 | 95 | public void PutByte(byte b) 96 | { 97 | _buffer[_pos++] = b; 98 | if (_pos >= _windowSize) 99 | Flush(); 100 | } 101 | 102 | public byte GetByte(uint distance) 103 | { 104 | uint pos = _pos - distance - 1; 105 | if (pos >= _windowSize) 106 | pos += _windowSize; 107 | return _buffer[pos]; 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/LZMA/LzmaBase.cs: -------------------------------------------------------------------------------- 1 | // LzmaBase.cs 2 | 3 | namespace SevenZip.Compression.LZMA 4 | { 5 | internal abstract class Base 6 | { 7 | public const uint kNumRepDistances = 4; 8 | public const uint kNumStates = 12; 9 | 10 | // static byte []kLiteralNextStates = {0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 4, 5}; 11 | // static byte []kMatchNextStates = {7, 7, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10}; 12 | // static byte []kRepNextStates = {8, 8, 8, 8, 8, 8, 8, 11, 11, 11, 11, 11}; 13 | // static byte []kShortRepNextStates = {9, 9, 9, 9, 9, 9, 9, 11, 11, 11, 11, 11}; 14 | 15 | public struct State 16 | { 17 | public uint Index; 18 | public void Init() { Index = 0; } 19 | public void UpdateChar() 20 | { 21 | if (Index < 4) Index = 0; 22 | else if (Index < 10) Index -= 3; 23 | else Index -= 6; 24 | } 25 | public void UpdateMatch() { Index = (uint)(Index < 7 ? 7 : 10); } 26 | public void UpdateRep() { Index = (uint)(Index < 7 ? 8 : 11); } 27 | public void UpdateShortRep() { Index = (uint)(Index < 7 ? 9 : 11); } 28 | public bool IsCharState() { return Index < 7; } 29 | } 30 | 31 | public const int kNumPosSlotBits = 6; 32 | public const int kDicLogSizeMin = 0; 33 | // public const int kDicLogSizeMax = 30; 34 | // public const uint kDistTableSizeMax = kDicLogSizeMax * 2; 35 | 36 | public const int kNumLenToPosStatesBits = 2; // it's for speed optimization 37 | public const uint kNumLenToPosStates = 1 << kNumLenToPosStatesBits; 38 | 39 | public const uint kMatchMinLen = 2; 40 | 41 | public static uint GetLenToPosState(uint len) 42 | { 43 | len -= kMatchMinLen; 44 | if (len < kNumLenToPosStates) 45 | return len; 46 | return (uint)(kNumLenToPosStates - 1); 47 | } 48 | 49 | public const int kNumAlignBits = 4; 50 | public const uint kAlignTableSize = 1 << kNumAlignBits; 51 | public const uint kAlignMask = (kAlignTableSize - 1); 52 | 53 | public const uint kStartPosModelIndex = 4; 54 | public const uint kEndPosModelIndex = 14; 55 | public const uint kNumPosModels = kEndPosModelIndex - kStartPosModelIndex; 56 | 57 | public const uint kNumFullDistances = 1 << ((int)kEndPosModelIndex / 2); 58 | 59 | public const uint kNumLitPosStatesBitsEncodingMax = 4; 60 | public const uint kNumLitContextBitsMax = 8; 61 | 62 | public const int kNumPosStatesBitsMax = 4; 63 | public const uint kNumPosStatesMax = (1 << kNumPosStatesBitsMax); 64 | public const int kNumPosStatesBitsEncodingMax = 4; 65 | public const uint kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax); 66 | 67 | public const int kNumLowLenBits = 3; 68 | public const int kNumMidLenBits = 3; 69 | public const int kNumHighLenBits = 8; 70 | public const uint kNumLowLenSymbols = 1 << kNumLowLenBits; 71 | public const uint kNumMidLenSymbols = 1 << kNumMidLenBits; 72 | public const uint kNumLenSymbols = kNumLowLenSymbols + kNumMidLenSymbols + 73 | (1 << kNumHighLenBits); 74 | public const uint kMatchMaxLen = kMatchMinLen + kNumLenSymbols - 1; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/RangeCoder/RangeCoder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SevenZip.Compression.RangeCoder 4 | { 5 | class Encoder 6 | { 7 | public const uint kTopValue = (1 << 24); 8 | 9 | System.IO.Stream Stream; 10 | 11 | public UInt64 Low; 12 | public uint Range; 13 | uint _cacheSize; 14 | byte _cache; 15 | 16 | long StartPosition; 17 | 18 | public void SetStream(System.IO.Stream stream) 19 | { 20 | Stream = stream; 21 | } 22 | 23 | public void ReleaseStream() 24 | { 25 | Stream = null; 26 | } 27 | 28 | public void Init() 29 | { 30 | StartPosition = Stream.Position; 31 | 32 | Low = 0; 33 | Range = 0xFFFFFFFF; 34 | _cacheSize = 1; 35 | _cache = 0; 36 | } 37 | 38 | public void FlushData() 39 | { 40 | for (int i = 0; i < 5; i++) 41 | ShiftLow(); 42 | } 43 | 44 | public void FlushStream() 45 | { 46 | Stream.Flush(); 47 | } 48 | 49 | public void CloseStream() 50 | { 51 | Stream.Close(); 52 | } 53 | 54 | public void Encode(uint start, uint size, uint total) 55 | { 56 | Low += start * (Range /= total); 57 | Range *= size; 58 | while (Range < kTopValue) 59 | { 60 | Range <<= 8; 61 | ShiftLow(); 62 | } 63 | } 64 | 65 | public void ShiftLow() 66 | { 67 | if ((uint)Low < (uint)0xFF000000 || (uint)(Low >> 32) == 1) 68 | { 69 | byte temp = _cache; 70 | do 71 | { 72 | Stream.WriteByte((byte)(temp + (Low >> 32))); 73 | temp = 0xFF; 74 | } 75 | while (--_cacheSize != 0); 76 | _cache = (byte)(((uint)Low) >> 24); 77 | } 78 | _cacheSize++; 79 | Low = ((uint)Low) << 8; 80 | } 81 | 82 | public void EncodeDirectBits(uint v, int numTotalBits) 83 | { 84 | for (int i = numTotalBits - 1; i >= 0; i--) 85 | { 86 | Range >>= 1; 87 | if (((v >> i) & 1) == 1) 88 | Low += Range; 89 | if (Range < kTopValue) 90 | { 91 | Range <<= 8; 92 | ShiftLow(); 93 | } 94 | } 95 | } 96 | 97 | public void EncodeBit(uint size0, int numTotalBits, uint symbol) 98 | { 99 | uint newBound = (Range >> numTotalBits) * size0; 100 | if (symbol == 0) 101 | Range = newBound; 102 | else 103 | { 104 | Low += newBound; 105 | Range -= newBound; 106 | } 107 | while (Range < kTopValue) 108 | { 109 | Range <<= 8; 110 | ShiftLow(); 111 | } 112 | } 113 | 114 | public long GetProcessedSizeAdd() 115 | { 116 | return _cacheSize + 117 | Stream.Position - StartPosition + 4; 118 | // (long)Stream.GetProcessedSize(); 119 | } 120 | } 121 | 122 | class Decoder 123 | { 124 | public const uint kTopValue = (1 << 24); 125 | public uint Range; 126 | public uint Code; 127 | // public Buffer.InBuffer Stream = new Buffer.InBuffer(1 << 16); 128 | public System.IO.Stream Stream; 129 | 130 | public void Init(System.IO.Stream stream) 131 | { 132 | // Stream.Init(stream); 133 | Stream = stream; 134 | 135 | Code = 0; 136 | Range = 0xFFFFFFFF; 137 | for (int i = 0; i < 5; i++) 138 | Code = (Code << 8) | (byte)Stream.ReadByte(); 139 | } 140 | 141 | public void ReleaseStream() 142 | { 143 | // Stream.ReleaseStream(); 144 | Stream = null; 145 | } 146 | 147 | public void CloseStream() 148 | { 149 | Stream.Close(); 150 | } 151 | 152 | public void Normalize() 153 | { 154 | while (Range < kTopValue) 155 | { 156 | Code = (Code << 8) | (byte)Stream.ReadByte(); 157 | Range <<= 8; 158 | } 159 | } 160 | 161 | public void Normalize2() 162 | { 163 | if (Range < kTopValue) 164 | { 165 | Code = (Code << 8) | (byte)Stream.ReadByte(); 166 | Range <<= 8; 167 | } 168 | } 169 | 170 | public uint GetThreshold(uint total) 171 | { 172 | return Code / (Range /= total); 173 | } 174 | 175 | public void Decode(uint start, uint size, uint total) 176 | { 177 | Code -= start * Range; 178 | Range *= size; 179 | Normalize(); 180 | } 181 | 182 | public uint DecodeDirectBits(int numTotalBits) 183 | { 184 | uint range = Range; 185 | uint code = Code; 186 | uint result = 0; 187 | for (int i = numTotalBits; i > 0; i--) 188 | { 189 | range >>= 1; 190 | /* 191 | result <<= 1; 192 | if (code >= range) 193 | { 194 | code -= range; 195 | result |= 1; 196 | } 197 | */ 198 | uint t = (code - range) >> 31; 199 | code -= range & (t - 1); 200 | result = (result << 1) | (1 - t); 201 | 202 | if (range < kTopValue) 203 | { 204 | code = (code << 8) | (byte)Stream.ReadByte(); 205 | range <<= 8; 206 | } 207 | } 208 | Range = range; 209 | Code = code; 210 | return result; 211 | } 212 | 213 | public uint DecodeBit(uint size0, int numTotalBits) 214 | { 215 | uint newBound = (Range >> numTotalBits) * size0; 216 | uint symbol; 217 | if (Code < newBound) 218 | { 219 | symbol = 0; 220 | Range = newBound; 221 | } 222 | else 223 | { 224 | symbol = 1; 225 | Code -= newBound; 226 | Range -= newBound; 227 | } 228 | Normalize(); 229 | return symbol; 230 | } 231 | 232 | // ulong GetProcessedSize() {return Stream.GetProcessedSize(); } 233 | } 234 | } 235 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/RangeCoder/RangeCoderBit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SevenZip.Compression.RangeCoder 4 | { 5 | struct BitEncoder 6 | { 7 | public const int kNumBitModelTotalBits = 11; 8 | public const uint kBitModelTotal = (1 << kNumBitModelTotalBits); 9 | const int kNumMoveBits = 5; 10 | const int kNumMoveReducingBits = 2; 11 | public const int kNumBitPriceShiftBits = 6; 12 | 13 | uint Prob; 14 | 15 | public void Init() { Prob = kBitModelTotal >> 1; } 16 | 17 | public void UpdateModel(uint symbol) 18 | { 19 | if (symbol == 0) 20 | Prob += (kBitModelTotal - Prob) >> kNumMoveBits; 21 | else 22 | Prob -= (Prob) >> kNumMoveBits; 23 | } 24 | 25 | public void Encode(Encoder encoder, uint symbol) 26 | { 27 | // encoder.EncodeBit(Prob, kNumBitModelTotalBits, symbol); 28 | // UpdateModel(symbol); 29 | uint newBound = (encoder.Range >> kNumBitModelTotalBits) * Prob; 30 | if (symbol == 0) 31 | { 32 | encoder.Range = newBound; 33 | Prob += (kBitModelTotal - Prob) >> kNumMoveBits; 34 | } 35 | else 36 | { 37 | encoder.Low += newBound; 38 | encoder.Range -= newBound; 39 | Prob -= (Prob) >> kNumMoveBits; 40 | } 41 | if (encoder.Range < Encoder.kTopValue) 42 | { 43 | encoder.Range <<= 8; 44 | encoder.ShiftLow(); 45 | } 46 | } 47 | 48 | private static UInt32[] ProbPrices = new UInt32[kBitModelTotal >> kNumMoveReducingBits]; 49 | 50 | static BitEncoder() 51 | { 52 | const int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits); 53 | for (int i = kNumBits - 1; i >= 0; i--) 54 | { 55 | UInt32 start = (UInt32)1 << (kNumBits - i - 1); 56 | UInt32 end = (UInt32)1 << (kNumBits - i); 57 | for (UInt32 j = start; j < end; j++) 58 | ProbPrices[j] = ((UInt32)i << kNumBitPriceShiftBits) + 59 | (((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1)); 60 | } 61 | } 62 | 63 | public uint GetPrice(uint symbol) 64 | { 65 | return ProbPrices[(((Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits]; 66 | } 67 | public uint GetPrice0() { return ProbPrices[Prob >> kNumMoveReducingBits]; } 68 | public uint GetPrice1() { return ProbPrices[(kBitModelTotal - Prob) >> kNumMoveReducingBits]; } 69 | } 70 | 71 | struct BitDecoder 72 | { 73 | public const int kNumBitModelTotalBits = 11; 74 | public const uint kBitModelTotal = (1 << kNumBitModelTotalBits); 75 | const int kNumMoveBits = 5; 76 | 77 | uint Prob; 78 | 79 | public void UpdateModel(int numMoveBits, uint symbol) 80 | { 81 | if (symbol == 0) 82 | Prob += (kBitModelTotal - Prob) >> numMoveBits; 83 | else 84 | Prob -= (Prob) >> numMoveBits; 85 | } 86 | 87 | public void Init() { Prob = kBitModelTotal >> 1; } 88 | 89 | public uint Decode(RangeCoder.Decoder rangeDecoder) 90 | { 91 | uint newBound = (uint)(rangeDecoder.Range >> kNumBitModelTotalBits) * (uint)Prob; 92 | if (rangeDecoder.Code < newBound) 93 | { 94 | rangeDecoder.Range = newBound; 95 | Prob += (kBitModelTotal - Prob) >> kNumMoveBits; 96 | if (rangeDecoder.Range < Decoder.kTopValue) 97 | { 98 | rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte(); 99 | rangeDecoder.Range <<= 8; 100 | } 101 | return 0; 102 | } 103 | else 104 | { 105 | rangeDecoder.Range -= newBound; 106 | rangeDecoder.Code -= newBound; 107 | Prob -= (Prob) >> kNumMoveBits; 108 | if (rangeDecoder.Range < Decoder.kTopValue) 109 | { 110 | rangeDecoder.Code = (rangeDecoder.Code << 8) | (byte)rangeDecoder.Stream.ReadByte(); 111 | rangeDecoder.Range <<= 8; 112 | } 113 | return 1; 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /ThirdParty/7zip/Compress/RangeCoder/RangeCoderBitTree.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SevenZip.Compression.RangeCoder 4 | { 5 | struct BitTreeEncoder 6 | { 7 | BitEncoder[] Models; 8 | int NumBitLevels; 9 | 10 | public BitTreeEncoder(int numBitLevels) 11 | { 12 | NumBitLevels = numBitLevels; 13 | Models = new BitEncoder[1 << numBitLevels]; 14 | } 15 | 16 | public void Init() 17 | { 18 | for (uint i = 1; i < (1 << NumBitLevels); i++) 19 | Models[i].Init(); 20 | } 21 | 22 | public void Encode(Encoder rangeEncoder, UInt32 symbol) 23 | { 24 | UInt32 m = 1; 25 | for (int bitIndex = NumBitLevels; bitIndex > 0; ) 26 | { 27 | bitIndex--; 28 | UInt32 bit = (symbol >> bitIndex) & 1; 29 | Models[m].Encode(rangeEncoder, bit); 30 | m = (m << 1) | bit; 31 | } 32 | } 33 | 34 | public void ReverseEncode(Encoder rangeEncoder, UInt32 symbol) 35 | { 36 | UInt32 m = 1; 37 | for (UInt32 i = 0; i < NumBitLevels; i++) 38 | { 39 | UInt32 bit = symbol & 1; 40 | Models[m].Encode(rangeEncoder, bit); 41 | m = (m << 1) | bit; 42 | symbol >>= 1; 43 | } 44 | } 45 | 46 | public UInt32 GetPrice(UInt32 symbol) 47 | { 48 | UInt32 price = 0; 49 | UInt32 m = 1; 50 | for (int bitIndex = NumBitLevels; bitIndex > 0; ) 51 | { 52 | bitIndex--; 53 | UInt32 bit = (symbol >> bitIndex) & 1; 54 | price += Models[m].GetPrice(bit); 55 | m = (m << 1) + bit; 56 | } 57 | return price; 58 | } 59 | 60 | public UInt32 ReverseGetPrice(UInt32 symbol) 61 | { 62 | UInt32 price = 0; 63 | UInt32 m = 1; 64 | for (int i = NumBitLevels; i > 0; i--) 65 | { 66 | UInt32 bit = symbol & 1; 67 | symbol >>= 1; 68 | price += Models[m].GetPrice(bit); 69 | m = (m << 1) | bit; 70 | } 71 | return price; 72 | } 73 | 74 | public static UInt32 ReverseGetPrice(BitEncoder[] Models, UInt32 startIndex, 75 | int NumBitLevels, UInt32 symbol) 76 | { 77 | UInt32 price = 0; 78 | UInt32 m = 1; 79 | for (int i = NumBitLevels; i > 0; i--) 80 | { 81 | UInt32 bit = symbol & 1; 82 | symbol >>= 1; 83 | price += Models[startIndex + m].GetPrice(bit); 84 | m = (m << 1) | bit; 85 | } 86 | return price; 87 | } 88 | 89 | public static void ReverseEncode(BitEncoder[] Models, UInt32 startIndex, 90 | Encoder rangeEncoder, int NumBitLevels, UInt32 symbol) 91 | { 92 | UInt32 m = 1; 93 | for (int i = 0; i < NumBitLevels; i++) 94 | { 95 | UInt32 bit = symbol & 1; 96 | Models[startIndex + m].Encode(rangeEncoder, bit); 97 | m = (m << 1) | bit; 98 | symbol >>= 1; 99 | } 100 | } 101 | } 102 | 103 | struct BitTreeDecoder 104 | { 105 | BitDecoder[] Models; 106 | int NumBitLevels; 107 | 108 | public BitTreeDecoder(int numBitLevels) 109 | { 110 | NumBitLevels = numBitLevels; 111 | Models = new BitDecoder[1 << numBitLevels]; 112 | } 113 | 114 | public void Init() 115 | { 116 | for (uint i = 1; i < (1 << NumBitLevels); i++) 117 | Models[i].Init(); 118 | } 119 | 120 | public uint Decode(RangeCoder.Decoder rangeDecoder) 121 | { 122 | uint m = 1; 123 | for (int bitIndex = NumBitLevels; bitIndex > 0; bitIndex--) 124 | m = (m << 1) + Models[m].Decode(rangeDecoder); 125 | return m - ((uint)1 << NumBitLevels); 126 | } 127 | 128 | public uint ReverseDecode(RangeCoder.Decoder rangeDecoder) 129 | { 130 | uint m = 1; 131 | uint symbol = 0; 132 | for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 133 | { 134 | uint bit = Models[m].Decode(rangeDecoder); 135 | m <<= 1; 136 | m += bit; 137 | symbol |= (bit << bitIndex); 138 | } 139 | return symbol; 140 | } 141 | 142 | public static uint ReverseDecode(BitDecoder[] Models, UInt32 startIndex, 143 | RangeCoder.Decoder rangeDecoder, int NumBitLevels) 144 | { 145 | uint m = 1; 146 | uint symbol = 0; 147 | for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++) 148 | { 149 | uint bit = Models[startIndex + m].Decode(rangeDecoder); 150 | m <<= 1; 151 | m += bit; 152 | symbol |= (bit << bitIndex); 153 | } 154 | return symbol; 155 | } 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /ThirdParty/7zip/ICoder.cs: -------------------------------------------------------------------------------- 1 | // ICoder.h 2 | 3 | using System; 4 | 5 | namespace SevenZip 6 | { 7 | /// 8 | /// The exception that is thrown when an error in input stream occurs during decoding. 9 | /// 10 | class DataErrorException : ApplicationException 11 | { 12 | public DataErrorException(): base("Data Error") { } 13 | } 14 | 15 | /// 16 | /// The exception that is thrown when the value of an argument is outside the allowable range. 17 | /// 18 | class InvalidParamException : ApplicationException 19 | { 20 | public InvalidParamException(): base("Invalid Parameter") { } 21 | } 22 | 23 | public interface ICodeProgress 24 | { 25 | /// 26 | /// Callback progress. 27 | /// 28 | /// 29 | /// input size. -1 if unknown. 30 | /// 31 | /// 32 | /// output size. -1 if unknown. 33 | /// 34 | void SetProgress(Int64 inSize, Int64 outSize); 35 | }; 36 | 37 | public interface ICoder 38 | { 39 | /// 40 | /// Codes streams. 41 | /// 42 | /// 43 | /// input Stream. 44 | /// 45 | /// 46 | /// output Stream. 47 | /// 48 | /// 49 | /// input Size. -1 if unknown. 50 | /// 51 | /// 52 | /// output Size. -1 if unknown. 53 | /// 54 | /// 55 | /// callback progress reference. 56 | /// 57 | /// 58 | /// if input stream is not valid 59 | /// 60 | void Code(System.IO.Stream inStream, System.IO.Stream outStream, 61 | Int64 inSize, Int64 outSize, ICodeProgress progress); 62 | }; 63 | 64 | /* 65 | public interface ICoder2 66 | { 67 | void Code(ISequentialInStream []inStreams, 68 | const UInt64 []inSizes, 69 | ISequentialOutStream []outStreams, 70 | UInt64 []outSizes, 71 | ICodeProgress progress); 72 | }; 73 | */ 74 | 75 | /// 76 | /// Provides the fields that represent properties idenitifiers for compressing. 77 | /// 78 | public enum CoderPropID 79 | { 80 | /// 81 | /// Specifies default property. 82 | /// 83 | DefaultProp = 0, 84 | /// 85 | /// Specifies size of dictionary. 86 | /// 87 | DictionarySize, 88 | /// 89 | /// Specifies size of memory for PPM*. 90 | /// 91 | UsedMemorySize, 92 | /// 93 | /// Specifies order for PPM methods. 94 | /// 95 | Order, 96 | /// 97 | /// Specifies Block Size. 98 | /// 99 | BlockSize, 100 | /// 101 | /// Specifies number of postion state bits for LZMA (0 <= x <= 4). 102 | /// 103 | PosStateBits, 104 | /// 105 | /// Specifies number of literal context bits for LZMA (0 <= x <= 8). 106 | /// 107 | LitContextBits, 108 | /// 109 | /// Specifies number of literal position bits for LZMA (0 <= x <= 4). 110 | /// 111 | LitPosBits, 112 | /// 113 | /// Specifies number of fast bytes for LZ*. 114 | /// 115 | NumFastBytes, 116 | /// 117 | /// Specifies match finder. LZMA: "BT2", "BT4" or "BT4B". 118 | /// 119 | MatchFinder, 120 | /// 121 | /// Specifies the number of match finder cyckes. 122 | /// 123 | MatchFinderCycles, 124 | /// 125 | /// Specifies number of passes. 126 | /// 127 | NumPasses, 128 | /// 129 | /// Specifies number of algorithm. 130 | /// 131 | Algorithm, 132 | /// 133 | /// Specifies the number of threads. 134 | /// 135 | NumThreads, 136 | /// 137 | /// Specifies mode with end marker. 138 | /// 139 | EndMarker 140 | }; 141 | 142 | 143 | public interface ISetCoderProperties 144 | { 145 | void SetCoderProperties(CoderPropID[] propIDs, object[] properties); 146 | }; 147 | 148 | public interface IWriteCoderProperties 149 | { 150 | void WriteCoderProperties(System.IO.Stream outStream); 151 | } 152 | 153 | public interface ISetDecoderProperties 154 | { 155 | void SetDecoderProperties(byte[] properties); 156 | } 157 | } 158 | --------------------------------------------------------------------------------