private bool TryLoadCachedGroup(byte[] data, DateTime lastWriteTime)
{
var timer = System.Diagnostics.Stopwatch.StartNew();
var reader = new BinaryReader(new MemoryStream(data), Encoding.UTF8);
DateTime cacheTime = new DateTime(reader.ReadInt64(), DateTimeKind.Utc);
if (cacheTime != lastWriteTime)
return false;
Dictionary<int, object> objects = new Dictionary<int, object>();
objects.Add(0, null);
// first pass constructs objects
long objectTableOffset = reader.BaseStream.Position;
int objectCount = reader.ReadInt32();
for (int i = 0; i < objectCount; i++)
{
int key = reader.ReadInt32();
object obj = CreateGroupObject(reader, key, objects);
objects.Add(key, obj);
}
reader.BaseStream.Seek(objectTableOffset + 4, SeekOrigin.Begin);
for (int i = 0; i < objectCount; i++)
{
int key = reader.ReadInt32();
LoadGroupObject(reader, key, objects);
}
List<TemplateGroup> importsToClearOnUnload = new List<TemplateGroup>();
Dictionary<string, CompiledTemplate> templates = new Dictionary<string, CompiledTemplate>();
Dictionary<string, IDictionary<string, object>> dictionaries = new Dictionary<string, IDictionary<string, object>>();
// imported groups
int importCount = reader.ReadInt32();
for (int i = 0; i < importCount; i++)
importsToClearOnUnload.Add((TemplateGroup)objects[reader.ReadInt32()]);
// delimiters
char delimiterStartChar = reader.ReadChar();
char delimiterStopChar = reader.ReadChar();
// templates & aliases
int templateCount = reader.ReadInt32();
for (int i = 0; i < templateCount; i++)
{
string key = reader.ReadString();
CompiledTemplate value = (CompiledTemplate)objects[reader.ReadInt32()];
templates[key] = value;
}
// dictionaries
int dictionaryCount = reader.ReadInt32();
for (int i = 0; i < dictionaryCount; i++)
{
string name = reader.ReadString();
IDictionary<string, object> dictionary = new Dictionary<string, object>();
dictionaries[name] = dictionary;
int valueCount = reader.ReadInt32();
for (int j = 0; j < valueCount; j++)
{
string key = reader.ReadString();
object value = objects[reader.ReadInt32()];
dictionary[key] = value;
}
}
this._importsToClearOnUnload.AddRange(importsToClearOnUnload);
this.delimiterStartChar = delimiterStartChar;
this.delimiterStopChar = delimiterStopChar;
foreach (var pair in templates)
this.templates[pair.Key] = pair.Value;
foreach (var pair in dictionaries)
this.dictionaries[pair.Key] = pair.Value;
System.Diagnostics.Debug.WriteLine(string.Format("Successfully loaded the cached group {0} in {1}ms.", Name, timer.ElapsedMilliseconds));
return true;
}