ScriptStack 1.0.4
Loading...
Searching...
No Matches
Manager.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq.Expressions;
5using System.Reflection;
6using System.Text;
7
11
12namespace ScriptStack
13{
14
19 public class Manager
20 {
21
22 #region Private Variables
23
24 private string name;
25 private Scanner scanner;
26 private Memory sharedMemory;
27 private Dictionary<string, Routine> routines;
28 private Dictionary<object, Interpreter> locks;
29 private bool debug;
30 private bool optimize;
31
32 #endregion
33
34 #region Internal Properties
35
36 internal Dictionary<object, Interpreter> Locks
37 {
38 get { return locks; }
39 }
40
41 #endregion
42
43 #region Public Methods
44
48 public Manager()
49 {
50
51 scanner = new ScannerPrototype();
52
53 sharedMemory = Memory.AllocateSharedMemory();
54
55 routines = new Dictionary<string, Routine>();
56
57 locks = new Dictionary<object, Interpreter>();
58
59 debug = false;
60
61 optimize = true;
62
63 }
64
65 public void LoadComponents(string relativeDirectoryPath)
66 {
67
68 string path = relativeDirectoryPath; // System.AppDomain.CurrentDomain.BaseDirectory;
69
70 foreach (string dll in System.IO.Directory.GetFiles(path, "*.dll"))
71 {
72
73 if (dll == path + "ScriptStack.dll")
74 continue;
75
76 Assembly assembly = null;
77
78 try
79 {
80 assembly = Assembly.LoadFile(dll);
81 }
82 catch (Exception e) { }
83
84 Type[] arrayTypes = assembly.GetExportedTypes();
85
86 foreach (Type type in arrayTypes)
87 {
88
89 if (!typeof(Model).IsAssignableFrom(type))
90 continue;
91
92 ConstructorInfo constructorInfo = null;
93 try
94 {
95 constructorInfo = type.GetConstructor(new Type[0]);
96 }
97 catch (Exception e) { continue; }
98
99 object objectHostModule = constructorInfo.Invoke(new object[0]);
100 Model hostModule = (Model)objectHostModule;
101
102 /*
103 Console.WriteLine("[INFO] Lade Modul '" + hostModule.ToString() + "'");
104 foreach(Routine r in hostModule.Routines)
105 Console.WriteLine("[INFO] Lade Routine '" + r.Name.ToString() + "'");
106 */
107
108 Register(hostModule);
109
110 }
111
112 }
113
114 }
115
116 public void LoadComponent(string relativeDirectoryPath)
117 {
118
119 Assembly assembly = null;
120 try
121 {
122 assembly = Assembly.LoadFile(relativeDirectoryPath);
123 }
124 catch (Exception e) { }
125
126 Type[] arrayTypes = assembly.GetExportedTypes();
127
128 foreach (Type type in arrayTypes)
129 {
130
131 if (!typeof(Model).IsAssignableFrom(type))
132 continue;
133
134 ConstructorInfo constructorInfo = null;
135 try
136 {
137 constructorInfo = type.GetConstructor(new Type[0]);
138 }
139 catch (Exception e) { continue; }
140
141 object objectHostModule = constructorInfo.Invoke(new object[0]);
142 Model hostModule = (Model)objectHostModule;
143
144 /*
145 Console.WriteLine("[INFO] Lade Modul '" + hostModule.ToString() + "'");
146 foreach(Routine r in hostModule.Routines)
147 Console.WriteLine("[INFO] Lade Routine '" + r.Name.ToString() + "'");
148 */
149
150 Register(hostModule);
151
152 }
153
154 }
155
157 {
158 get { return name; }
159 set {
160 name = value;
161 }
162 }
163
164 public bool IsRegistered(string routine)
165 {
166 return routines.ContainsKey(routine);
167 }
168
169 public void Register(Model model)
170 {
171
172 foreach (Routine routine in model.Routines)
173 Register(routine, model);
174
175 }
176
177 public void UnRegister(Model model)
178 {
179
180 foreach (Routine routine in model.Routines)
181 UnRegister(routine.Name);
182
183 }
184
185 public void Register(Routine routine, Host host)
186 {
187
188 string name = routine.Name;
189
190 if (routines.ContainsKey(name))
191 throw new ScriptStackException("Die Routine '" + name + "' ist bereits registriert.");
192
193 routine.Handler = host;
194
195 routines[name] = routine;
196
197 }
198
199 public void UnRegister(string routine)
200 {
201
202 if (!routines.ContainsKey(routine))
203 throw new ScriptStackException("Die Routine '" + routine + "' wurde nicht gefunden.");
204
205 routines.Remove(routine);
206
207 }
208
213 public void Register(Routine routine)
214 {
215 Register(routine, null);
216 }
217
218 public void ClearActiveLocks()
219 {
220 locks.Clear();
221 }
222
223 #endregion
224
225 #region Public Properties
226
231 {
232 get { return scanner; }
233 set { scanner = value; }
234 }
235
237 {
238 get { return sharedMemory; }
239 }
240
241 public ReadOnlyDictionary<String, Routine> Routines {
242 get
243 {
244 return new ReadOnlyDictionary<string, Routine>(routines);
245 }
246 }
247
248 public bool Debug
249 {
250 get { return debug; }
251 set { debug = value; }
252 }
253
254 public bool Optimize
255 {
256 get { return optimize; }
257 set { optimize = value; }
258 }
259
260 public ReadOnlyDictionary<object, Interpreter> ActiveLocks
261 {
262 get { return new ReadOnlyDictionary<object,Interpreter>(locks); }
263 }
264
265 #endregion
266
267 }
268
269}
void LoadComponent(string relativeDirectoryPath)
Definition Manager.cs:116
void ClearActiveLocks()
Definition Manager.cs:218
Manager()
A Manager object is responsible for memory management, type evaluation and loading of plugins.
Definition Manager.cs:48
void Register(Model model)
Definition Manager.cs:169
ReadOnlyDictionary< object, Interpreter > ActiveLocks
Definition Manager.cs:261
bool IsRegistered(string routine)
Definition Manager.cs:164
Scanner Scanner
A Scanner reference.
Definition Manager.cs:231
void UnRegister(string routine)
Definition Manager.cs:199
void LoadComponents(string relativeDirectoryPath)
Definition Manager.cs:65
void Register(Routine routine)
Register a new Routine.
Definition Manager.cs:213
void UnRegister(Model model)
Definition Manager.cs:177
ReadOnlyDictionary< String, Routine > Routines
Definition Manager.cs:241
void Register(Routine routine, Host host)
Definition Manager.cs:185
static Memory AllocateSharedMemory()
Definition Memory.cs:55
A Routine is an abstract representation of a method.
Definition Routine.cs:70
The main interface to create a Host. A Host can implement Routine's to extend its functionality.
Definition Host.cs:80