// dnlib: See LICENSE.txt for more info
using System;
using System.Collections.Generic;
using dnlib.Protection;
namespace dnlib.DotNet.Writer {
///
/// #GUID heap
///
public sealed class GuidHeap : HeapBase, IOffsetHeap {
readonly Dictionary guids = new Dictionary();
Dictionary userRawData;
///
public override string Name => "#GUID";
///
/// Adds a guid to the #GUID heap
///
/// The guid
/// The index of the guid in the #GUID heap
public uint Add(Guid? guid) {
if (isReadOnly)
throw new ModuleWriterException("Trying to modify #GUID when it's read-only");
if (guid is null)
return 0;
if (guids.TryGetValue(guid.Value, out uint index))
return index;
index = (uint)guids.Count + 1;
guids.Add(guid.Value, index);
return index;
}
///
public override uint GetRawLength() => (uint)guids.Count * 16;
protected override EncryptionMethod GetEncryptionMethod(IEncryption e) => null;
///
protected override void WriteToImpl(DataWriter writer) {
uint offset = 0;
foreach (var kv in guids) {
if (userRawData is null || !userRawData.TryGetValue(offset, out var rawData))
rawData = kv.Key.ToByteArray();
writer.WriteBytes(rawData);
offset += 16;
}
}
///
public int GetRawDataSize(Guid data) => 16;
///
public void SetRawData(uint offset, byte[] rawData) {
if (rawData is null || rawData.Length != 16)
throw new ArgumentException("Invalid size of GUID raw data");
if (userRawData is null)
userRawData = new Dictionary();
userRawData[offset] = rawData;
}
///
public IEnumerable> GetAllRawData() {
uint offset = 0;
foreach (var kv in guids) {
yield return new KeyValuePair(offset, kv.Key.ToByteArray());
offset += 16;
}
}
}
}