| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.IO; |
| 4 |
|
| 5 |
namespace Oni.Objects |
| 6 |
{ |
| 7 |
internal class ObjectLoadContext |
| 8 |
{ |
| 9 |
private readonly Func<TemplateTag, string, ObjectLoadContext, InstanceDescriptor> getDescriptor; |
| 10 |
private readonly TextWriter info; |
| 11 |
private readonly Dictionary<string, ObjectClass> classCache; |
| 12 |
private string basePath; |
| 13 |
private string filePath; |
| 14 |
|
| 15 |
public ObjectLoadContext(Func<TemplateTag, string, ObjectLoadContext, InstanceDescriptor> getDescriptor, TextWriter info) |
| 16 |
{ |
| 17 |
this.getDescriptor = getDescriptor; |
| 18 |
this.info = info; |
| 19 |
this.classCache = new Dictionary<string, ObjectClass>(StringComparer.Ordinal); |
| 20 |
} |
| 21 |
|
| 22 |
public T GetClass<T>(TemplateTag tag, string name, Func<InstanceDescriptor, T> reader) |
| 23 |
where T : ObjectClass, new() |
| 24 |
{ |
| 25 |
ObjectClass klass; |
| 26 |
|
| 27 |
string fullName = tag.ToString() + name; |
| 28 |
|
| 29 |
if (!classCache.TryGetValue(fullName, out klass)) |
| 30 |
{ |
| 31 |
var descriptor = getDescriptor(tag, name, this); |
| 32 |
|
| 33 |
if (descriptor != null) |
| 34 |
{ |
| 35 |
info.WriteLine("Using {0} object class '{1}' from '{2}'", tag, descriptor.Name, descriptor.FilePath); |
| 36 |
|
| 37 |
klass = reader(descriptor); |
| 38 |
klass.Name = descriptor.Name; |
| 39 |
} |
| 40 |
|
| 41 |
classCache.Add(fullName, klass); |
| 42 |
} |
| 43 |
|
| 44 |
return (T)klass; |
| 45 |
} |
| 46 |
|
| 47 |
public string BasePath |
| 48 |
{ |
| 49 |
get { return basePath; } |
| 50 |
set { basePath = value; } |
| 51 |
} |
| 52 |
|
| 53 |
public string FilePath |
| 54 |
{ |
| 55 |
get { return filePath; } |
| 56 |
set { filePath = value; } |
| 57 |
} |
| 58 |
} |
| 59 |
} |