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
31 public BadModuleImporter Clone(bool onlyTransient = true)
32 {
34 if (onlyTransient)
35 {
36 importer.m_Handlers.AddRange(m_Handlers.Where(x => x.IsTransient()));
37 }
38 else
39 {
40 importer.m_Handlers.AddRange(m_Handlers);
41 }
42
43 return importer;
44 }
45
52 {
53 m_Handlers.Add(handler);
54
55 return this;
56 }
57
63 public IEnumerable<BadObject> Get(string path)
64 {
65 for (int i = m_Handlers.Count - 1; i >= 0; i--)
66 {
67 BadImportHandler handler = m_Handlers[i];
68 string hash = handler.GetHash(path);
69 if (BadModuleSettings.Instance.UseModuleCaching && m_Store.IsCached(hash))
70 {
71 yield return m_Store.Get(hash);
72
73 yield break;
74 }
75
76 if (handler.Has(path))
77 {
78 IEnumerable<BadObject> result = handler.Get(path);
80 foreach (BadObject o in result)
81 {
82 r = o;
83
84 yield return r;
85 }
86
87 r = r.Dereference();
88
89 if (!BadModuleSettings.Instance.AllowImportHandlerNullReturn && r == BadObject.Null)
90 {
91 continue;
92 }
93
94 if (BadModuleSettings.Instance.UseModuleCaching && r != BadObject.Null)
95 {
96 m_Store.Cache(hash, r);
97 }
98
99 yield return r;
100
101 yield break;
102 }
103 }
104
105 if (BadModuleSettings.Instance.ThrowOnModuleUnresolved)
106 {
107 throw new BadRuntimeException("Module " + path + " could not be resolved.");
108 }
109
110 yield return BadObject.Null;
111 }
112}
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.