diff --git a/Source/Collections/Parentable.cs b/Source/Collections/Parentable.cs index 73cdb4e..a900f4f 100644 --- a/Source/Collections/Parentable.cs +++ b/Source/Collections/Parentable.cs @@ -5,8 +5,7 @@ namespace Nuclex.Support.Collections { /// Base class for objects that can be parented to an owner /// Type of the parent object - public class Parentable - where ParentType : class { + public class Parentable where ParentType : class { /// Assigns a new parent to this instance internal void SetParent(ParentType parent) { diff --git a/Source/Collections/PriorityQueue.cs b/Source/Collections/PriorityQueue.cs index 42bf14c..b71213f 100644 --- a/Source/Collections/PriorityQueue.cs +++ b/Source/Collections/PriorityQueue.cs @@ -78,21 +78,22 @@ namespace Nuclex.Support.Collections { /// Comparer to use for ordering the items public PriorityQueue(IComparer comparer) { this.comparer = comparer; - capacity = 15; // 15 is equal to 4 complete levels - heap = new ItemType[capacity]; + this.capacity = 15; // 15 is equal to 4 complete levels + this.heap = new ItemType[this.capacity]; } /// Takes the item with the highest priority off from the queue /// The item with the highest priority in the list + /// When the queue is empty public ItemType Dequeue() { if(count == 0) throw new InvalidOperationException("No items available to dequeue"); - ItemType result = heap[0]; - --count; - trickleDown(0, heap[count]); + ItemType result = this.heap[0]; + --this.count; + trickleDown(0, heap[this.count]); - ++version; + ++this.version; return result; } @@ -100,18 +101,18 @@ namespace Nuclex.Support.Collections { /// Puts an item into the priority queue /// Item to be queued public void Enqueue(ItemType item) { - if(count == capacity) + if(this.count == capacity) growHeap(); - ++count; - bubbleUp(count - 1, item); - ++version; + ++this.count; + bubbleUp(this.count - 1, item); + ++this.version; } /// Removes all items from the priority queue public void Clear() { this.count = 0; - ++version; + ++this.version; } @@ -124,7 +125,7 @@ namespace Nuclex.Support.Collections { /// Array to copy the priority queue into /// Starting index for the destination array public void CopyTo(Array array, int index) { - Array.Copy(heap, 0, array, index, count); + Array.Copy(heap, 0, array, index, this.count); } /// @@ -153,27 +154,30 @@ namespace Nuclex.Support.Collections { int parent = getParent(index); // Note: (index > 0) means there is a parent - while((index > 0) && (this.comparer.Compare(heap[parent], item) < 0)) { - heap[index] = heap[parent]; + while((index > 0) && (this.comparer.Compare(this.heap[parent], item) < 0)) { + this.heap[index] = this.heap[parent]; index = parent; parent = getParent(index); } - heap[index] = item; + this.heap[index] = item; } - /// Moved the item downwards in the heap tree + /// Move the item downwards in the heap tree /// Index of the item to be moved /// Item to be moved private void trickleDown(int index, ItemType item) { int child = getLeftChild(index); - while(child < count) { + while(child < this.count) { - if(((child + 1) < count) && (this.comparer.Compare(heap[child], heap[child + 1]) < 0)) + if( + ((child + 1) < this.count) && + (this.comparer.Compare(heap[child], this.heap[child + 1]) < 0) + ) ++child; - heap[index] = heap[child]; + this.heap[index] = this.heap[child]; index = child; child = getLeftChild(index); } @@ -197,11 +201,11 @@ namespace Nuclex.Support.Collections { /// Increases the size of the priority collection's heap private void growHeap() { - capacity = (capacity * 2) + 1; + this.capacity = (capacity * 2) + 1; - ItemType[] newHeap = new ItemType[capacity]; - Array.Copy(heap, 0, newHeap, 0, count); - heap = newHeap; + ItemType[] newHeap = new ItemType[this.capacity]; + Array.Copy(this.heap, 0, newHeap, 0, this.count); + this.heap = newHeap; } /// Returns an enumerator for the priority queue diff --git a/Source/Collections/RingMemoryStream.Test.cs b/Source/Collections/RingMemoryStream.Test.cs index f73b207..9bb2154 100644 --- a/Source/Collections/RingMemoryStream.Test.cs +++ b/Source/Collections/RingMemoryStream.Test.cs @@ -111,7 +111,7 @@ namespace Nuclex.Support.Collections { } /// - /// Tests whether the auto reset feature works (resets the buffer point to the + /// Tests whether the auto reset feature works (resets the buffer pointer to the /// left end of the buffer when it gets empty; mainly a performance feature). /// [Test] diff --git a/Source/Collections/RingMemoryStream.cs b/Source/Collections/RingMemoryStream.cs index a15f244..11e104f 100644 --- a/Source/Collections/RingMemoryStream.cs +++ b/Source/Collections/RingMemoryStream.cs @@ -15,6 +15,9 @@ namespace Nuclex.Support.Collections { } /// Maximum amount of data that will fit into the ring memory stream + /// + /// If the reduced capacity is too small for the ring buffer's current data + /// public long Capacity { get { return this.ringBuffer.Length; } set { @@ -25,8 +28,9 @@ namespace Nuclex.Support.Collections { ); // This could be done in a more efficient manner than just replacing - // the stream, but this operation will probably be called only once - // during the lifetime of the application -- if at all... + // the entire buffer, but since this operation will probably be only called + // once during the lifetime of the application, if at all, I don't see + // the need to optimize it... MemoryStream newBuffer = new MemoryStream((int)value); newBuffer.SetLength(value); @@ -62,6 +66,7 @@ namespace Nuclex.Support.Collections { } /// Current cursor position within the stream + /// Always public override long Position { get { throw new NotSupportedException("The ring buffer does not support seeking"); } set { throw new NotSupportedException("The ring buffer does not support seeking"); } @@ -129,6 +134,7 @@ namespace Nuclex.Support.Collections { /// Buffer containing the data to append /// Starting index of the data in the buffer /// Number of bytes to write to the stream + /// When the ring buffer is full public override void Write(byte[] buffer, int offset, int count) { // The end index lies behind the start index (usual case), so the @@ -179,12 +185,14 @@ namespace Nuclex.Support.Collections { /// Position to jump to /// Origin towards which to interpret the offset /// The new offset within the stream + /// Always public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException("The ring buffer does not support seeking"); } /// Changes the length of the stream /// New length to resize the stream to + /// Always public override void SetLength(long value) { throw new NotSupportedException("This operation is not supported"); } diff --git a/Source/Licensing/LicenseKey.cs b/Source/Licensing/LicenseKey.cs index 408c5a4..bff111e 100644 --- a/Source/Licensing/LicenseKey.cs +++ b/Source/Licensing/LicenseKey.cs @@ -25,11 +25,48 @@ namespace Nuclex.Licensing { /// Parses the license key contained in a string /// String containing a license key that is to be parsed /// The license key parsed from provided string + /// + /// When the provided string is not a license key + /// public static LicenseKey Parse(string key) { - LicenseKey newKey = new LicenseKey(); - newKey.parse(key); + key = key.Replace(" ", string.Empty).Replace("-", string.Empty).ToUpper(); + if(key.Length != 25) + throw new ArgumentException("This is not a license key"); - return newKey; + BitArray bits = new BitArray(128); + uint sequence; + + // Convert the first 4 sequences of 6 chars into 124 bits + for(int j = 0; j < 4; j++) { + + sequence = + (uint)codeTable.IndexOf(key[j * 6 + 5]) * 60466176 + + (uint)codeTable.IndexOf(key[j * 6 + 4]) * 1679616 + + (uint)codeTable.IndexOf(key[j * 6 + 3]) * 46656 + + (uint)codeTable.IndexOf(key[j * 6 + 2]) * 1296 + + (uint)codeTable.IndexOf(key[j * 6 + 1]) * 36 + + (uint)codeTable.IndexOf(key[j * 6 + 0]); + + for(int i = 0; i < 31; i++) + bits[j * 31 + i] = (sequence & powersOfTwo[i, 1]) != 0; + + } + + // Append the remaining character's 4 bits + sequence = (uint)codeTable.IndexOf(key[24]); + bits[124] = (sequence & powersOfTwo[4, 1]) != 0; + bits[125] = (sequence & powersOfTwo[3, 1]) != 0; + bits[126] = (sequence & powersOfTwo[2, 1]) != 0; + bits[127] = (sequence & powersOfTwo[1, 1]) != 0; + + // Revert the mangling that was applied to the key when encoding... + unmangle(bits); + + // ...and we've got our GUID back! + byte[] guidBytes = new byte[16]; + bits.CopyTo(guidBytes, 0); + + return new LicenseKey(new Guid(guidBytes)); } /// Initializes a new, empty license key @@ -38,26 +75,29 @@ namespace Nuclex.Licensing { /// Initializes the license key from a GUID /// GUID that is used to create the license key public LicenseKey(Guid source) { - guid = source; + this.guid = source; } /// Accesses the four integer values within a license key + /// + /// When the index lies outside of the key's fields + /// public int this[int index] { get { if((index < 0) || (index > 3)) throw new IndexOutOfRangeException("Index out of range"); - return BitConverter.ToInt32(guid.ToByteArray(), index * 4); + return BitConverter.ToInt32(this.guid.ToByteArray(), index * 4); } set { if((index < 0) || (index > 3)) throw new IndexOutOfRangeException("Index out of range"); - using(MemoryStream guidBytes = new MemoryStream(guid.ToByteArray())) { + using(MemoryStream guidBytes = new MemoryStream(this.guid.ToByteArray())) { guidBytes.Position = index * 4; new BinaryWriter(guidBytes).Write(value); - guid = new Guid(guidBytes.ToArray()); + this.guid = new Guid(guidBytes.ToArray()); } } } @@ -65,13 +105,13 @@ namespace Nuclex.Licensing { /// Converts the license key into a GUID /// The GUID created from the license key public Guid ToGuid() { - return guid; + return this.guid; } /// Converts the license key into a byte array /// A byte array containing the converted license key public byte[] ToByteArray() { - return guid.ToByteArray(); + return this.guid.ToByteArray(); } /// Converts the license key to a string @@ -80,7 +120,7 @@ namespace Nuclex.Licensing { StringBuilder resultBuilder = new StringBuilder(); // Build a bit array from the input data - BitArray bits = new BitArray(guid.ToByteArray()); + BitArray bits = new BitArray(this.guid.ToByteArray()); mangle(bits); int sequence = 0; @@ -152,49 +192,6 @@ namespace Nuclex.Licensing { } } - /// Parses a license key from a string - /// String the license key is read from - /// The license key parsed from the provided string - private void parse(string key) { - key = key.Replace(" ", string.Empty).Replace("-", string.Empty).ToUpper(); - if(key.Length != 25) - throw new ArgumentException("This is not a license key"); - - BitArray bits = new BitArray(128); - uint sequence; - - // Convert the first 4 sequences of 6 chars into 124 bits - for(int j = 0; j < 4; j++) { - - sequence = - (uint)codeTable.IndexOf(key[j * 6 + 5]) * 60466176 + - (uint)codeTable.IndexOf(key[j * 6 + 4]) * 1679616 + - (uint)codeTable.IndexOf(key[j * 6 + 3]) * 46656 + - (uint)codeTable.IndexOf(key[j * 6 + 2]) * 1296 + - (uint)codeTable.IndexOf(key[j * 6 + 1]) * 36 + - (uint)codeTable.IndexOf(key[j * 6 + 0]); - - for(int i = 0; i < 31; i++) - bits[j * 31 + i] = (sequence & powersOfTwo[i, 1]) != 0; - - } - - // Append the remaining character's 4 bits - sequence = (uint)codeTable.IndexOf(key[24]); - bits[124] = (sequence & powersOfTwo[4, 1]) != 0; - bits[125] = (sequence & powersOfTwo[3, 1]) != 0; - bits[126] = (sequence & powersOfTwo[2, 1]) != 0; - bits[127] = (sequence & powersOfTwo[1, 1]) != 0; - - // Revert the mangling that was applied to the key when encoding... - unmangle(bits); - - // ...and we've got our GUID back! - byte[] guidBytes = new byte[16]; - bits.CopyTo(guidBytes, 0); - guid = new Guid(guidBytes); - } - /// Table with the individual characters in a key private static readonly string codeTable = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; diff --git a/Source/Serialization/IBinarySerializable.cs b/Source/Serialization/IBinarySerializable.cs index c8ff23e..a396a6b 100644 --- a/Source/Serialization/IBinarySerializable.cs +++ b/Source/Serialization/IBinarySerializable.cs @@ -18,7 +18,7 @@ namespace Nuclex.Support.Serialization { /// Reader to use for reading the object's state void Load(BinaryReader reader); - /// Save the object's state into a serialized representation + /// Saves the object's state into a serialized representation /// Writer to use for writing the object's state void Save(BinaryWriter writer);