source: OniSplit/Objects/ObjectLoadContext.cs@ 1194

Last change on this file since 1194 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 1.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4
5namespace 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}
Note: See TracBrowser for help on using the repository browser.