BadScript 2
Loading...
Searching...
No Matches
BadModuleImporter.cs
Go to the documentation of this file.
4
6
11{
15 private readonly List<BadImportHandler> m_Handlers = new List<BadImportHandler>();
16
20 private readonly BadModuleStore m_Store;
21
27 {
28 m_Store = store;
29 }
30
32 {
34 return this;
35 }
36
37 public BadModuleImporter Clone(bool onlyTransient = true)
38 {
40
41 if (onlyTransient)
42 {
43 importer.m_Handlers.AddRange(m_Handlers.Where(x => x.IsTransient()));
44 }
45 else
46 {
47 importer.m_Handlers.AddRange(m_Handlers);
48 }
49
50 return importer;
51 }
52
59 {
60 m_Handlers.Add(handler);
61
62 return this;
63 }
64
70 public IEnumerable<BadObject> Get(string path)
71 {
72 for (int i = m_Handlers.Count - 1; i >= 0; i--)
73 {
74 BadImportHandler handler = m_Handlers[i];
75 string hash = handler.GetHash(path);
76
77 if (BadModuleSettings.Instance.UseModuleCaching && m_Store.IsCached(hash))
78 {
79 yield return m_Store.Get(hash);
80
81 yield break;
82 }
83
84 if (handler.Has(path))
85 {
86 IEnumerable<BadObject> result = handler.Get(path);
88
89 foreach (BadObject o in result)
90 {
91 r = o;
92
93 yield return r;
94 }
95
96 r = r.Dereference(null);
97
98 if (!BadModuleSettings.Instance.AllowImportHandlerNullReturn && r == BadObject.Null)
99 {
100 continue;
101 }
102
103 if (BadModuleSettings.Instance.UseModuleCaching && r != BadObject.Null)
104 {
105 m_Store.Cache(hash, r);
106 }
107
108 yield return r;
109
110 yield break;
111 }
112 }
113
114 if (BadModuleSettings.Instance.ThrowOnModuleUnresolved)
115 {
116 throw new BadRuntimeException("Module " + path + " could not be resolved.");
117 }
118
119 yield return BadObject.Null;
120 }
121}
Defines the shape of the import handler for the module importer.
IEnumerable< BadObject > Get(string path)
Imports a module from the specified path.
bool Has(string path)
Returns true if the specified path is available.
string GetHash(string path)
Returns a unique hash for the specified path.
The Class that manages the importing of modules.
BadModuleImporter Clone(bool onlyTransient=true)
BadModuleImporter AddHandler(BadImportHandler handler)
Adds a new Import Handler to the Importer.
readonly BadModuleStore m_Store
The Module Store instance.
readonly List< BadImportHandler > m_Handlers
The List of Import Handlers.
IEnumerable< BadObject > Get(string path)
Imports a module from the specified path.
BadModuleImporter(BadModuleStore store)
Creates a new BadModuleImporter.
Is the store for all loaded modules.
void Cache(string hash, BadObject module)
Cache the specified module.
bool IsCached(string hash)
Returns true if the module is cached.
BadObject Get(string hash)
Returns the module with the specified hash.
The Base Class for all BadScript Objects.
Definition BadObject.cs:14
static readonly BadObject Null
The Null Value for the BadScript Language.
Definition BadObject.cs:28
Provides settings for the module system.
static T Instance
Returns the Instance of the Settings Provider.
Contains the Error Objects for the BadScript2 Language.
Contains the Runtime Objects.
Definition BadArray.cs:10
Contains Runtime Settings Objects.