|
1 | 1 | package org.mockengine;
|
2 | 2 |
|
| 3 | +import java.util.HashMap; |
3 | 4 | import java.util.Map;
|
4 | 5 |
|
5 | 6 | import javassist.CannotCompileException;
|
|
19 | 20 | import org.mozilla.javascript.Context;
|
20 | 21 | import org.mozilla.javascript.Scriptable;
|
21 | 22 |
|
22 |
| -public class DynamicComponentFactory { |
23 |
| - private static final String COMPONENT_NAME_TEMPLATE = "%sComponent"; |
| 23 | +public class DynamicEntitySystemFactory { |
| 24 | + private static final String COMPONENT_NAME_TEMPLATE = "org.terasology.gen.%sComponent"; |
24 | 25 | private static final String SYSTEM_NAME_TEMPLATE = "%sComponentSystem";
|
25 | 26 | private final ClassPool cp;
|
26 | 27 | private final JsModManager manager;
|
27 | 28 | private final Context cx;
|
28 | 29 | private final Scriptable scope;
|
29 | 30 |
|
30 |
| - public DynamicComponentFactory(JsModManager mm) { |
| 31 | + private Map<String, Class<? extends DynamicComponent>> compCache; |
| 32 | + |
| 33 | + public DynamicEntitySystemFactory(JsModManager mm) { |
31 | 34 | cp = new ClassPool();
|
32 | 35 | cp.appendSystemPath();
|
33 | 36 |
|
34 | 37 | this.manager = mm;
|
35 | 38 | this.cx = this.manager.getContext();
|
36 | 39 | this.scope = this.manager.getScope();
|
| 40 | + |
| 41 | + this.compCache = new HashMap<String, Class<? extends DynamicComponent>>(); |
37 | 42 | }
|
38 | 43 |
|
39 |
| - public Class<? extends DynamicComponent> makeComponent(String name) throws Exception { |
40 |
| - return makeComponent(manager.getPrototype(name)); |
| 44 | + public Class<? extends DynamicComponent> getComponent(String name) throws Exception { |
| 45 | + Class<? extends DynamicComponent> result = compCache.get(name); |
| 46 | + if (result == null) { |
| 47 | + result = makeComponent(manager.getPrototype(name)); |
| 48 | + compCache.put(name, result); |
| 49 | + } |
| 50 | + return result; |
41 | 51 | }
|
42 | 52 |
|
43 |
| - public Class<? extends DynamicSystem> makeSystem(String name) throws Exception { |
44 |
| - return makeSystem(manager.getPrototype(name)); |
| 53 | + public DynamicSystem getSystem(String name) throws Exception { |
| 54 | + return makeSystem(manager.getPrototype(name), getComponent(name)); |
45 | 55 | }
|
46 | 56 |
|
47 | 57 | public Class<? extends DynamicComponent> makeComponent(Prototype p) throws Exception {
|
@@ -95,75 +105,9 @@ public Class<? extends DynamicComponent> makeComponent(Prototype p) throws Excep
|
95 | 105 |
|
96 | 106 | }
|
97 | 107 |
|
98 |
| - public Class<? extends DynamicSystem> makeSystem(Prototype p) throws Exception { |
99 |
| - String className = makeSystemClassName(p.getId()); |
100 |
| - CtClass comp = cp.makeClass(className, cp.get(DynamicSystem.class.getName())); |
101 |
| - comp.addConstructor(CtNewConstructor.defaultConstructor(comp)); |
102 |
| - |
103 |
| - CtField scopeField = new CtField(cp.get(Scriptable.class.getName()), "__SCOPE__", comp); |
104 |
| - scopeField.setModifiers(Modifier.STATIC + Modifier.PUBLIC); |
105 |
| - comp.addField(scopeField); |
106 |
| - |
107 |
| - CtField cxField = new CtField(cp.get(Context.class.getName()), "__CX__", comp); |
108 |
| - cxField.setModifiers(Modifier.STATIC + Modifier.PUBLIC); |
109 |
| - comp.addField(cxField); |
110 |
| - |
111 |
| - CtField updateCallable = new CtField(cp.get(Callable.class.getName()), "__UPDATER__", comp); |
112 |
| - updateCallable.setModifiers(Modifier.STATIC + Modifier.PUBLIC); |
113 |
| - comp.addField(updateCallable); |
114 |
| - |
115 |
| - CtMethod updater = CtNewMethod.make( |
116 |
| - Modifier.PROTECTED, |
117 |
| - CtClass.voidType, |
118 |
| - "_update", |
119 |
| - new CtClass[] { cp.get(Float.class.getName()), |
120 |
| - cp.get(DynamicComponent.class.getName()) }, |
121 |
| - new CtClass[0], |
122 |
| - "{__UPDATER__.call(__CX__, __SCOPE__, (" |
123 |
| - + Scriptable.class.getName() + ") " |
124 |
| - + Context.class.getName() |
125 |
| - + ".javaToJS($2, __SCOPE__), new Object[] {$1} );}", |
126 |
| - comp); |
127 |
| - comp.addMethod(updater); |
128 |
| - |
129 |
| - |
130 |
| -// for (Map.Entry<String, DynamicProperty> e : p.getPropertyMap().entrySet()) { |
131 |
| -// DynamicProperty prop = e.getValue(); |
132 |
| -// addToClass(e.getKey(), comp, prop.getFieldType(), prop.isStatic()); |
133 |
| -// } |
134 |
| -// |
135 |
| - |
136 |
| -// |
137 |
| -// |
138 |
| -// CtClass[] types = getParamClasses(p.getConstructor().getProvided(), p.getPropertyMap()); |
139 |
| -// String body = "{"; |
140 |
| -// String[] provided = p.getConstructor().getProvided(); |
141 |
| -// for (int i = 0; i < provided.length; i++) { |
142 |
| -// body += "this." + provided[i] + "=$" + (i+1) + ";"; |
143 |
| -// } |
144 |
| -// body += "}"; |
145 |
| -// comp.addConstructor(CtNewConstructor.make(types, new CtClass[0], body, comp)); |
146 |
| -// |
147 |
| -// CtField initField = new CtField(ClassPool.getDefault().get(Callable.class.getName()), "__INIT__", comp); |
148 |
| -// initField.setModifiers(Modifier.STATIC + Modifier.PUBLIC); |
149 |
| -// comp.addField(initField); |
150 |
| -// |
151 |
| -// @SuppressWarnings("unchecked") |
152 |
| - Class<? extends DynamicSystem> result = comp.toClass(); |
153 |
| -// |
154 |
| -// for (Map.Entry<String, DynamicProperty> e : p.getPropertyMap().entrySet()) { |
155 |
| -// DynamicProperty prop = e.getValue(); |
156 |
| -// if (prop.isStatic()) { |
157 |
| -// result.getField(e.getKey()).set(null, prop.getValue()); |
158 |
| -// } |
159 |
| -// } |
160 |
| -// |
161 |
| - result.getField("__SCOPE__").set(null, scope); |
162 |
| - result.getField("__CX__").set(null, cx); |
163 |
| - result.getField("__UPDATER__").set(null, p.getUpdater()); |
164 |
| -// |
165 |
| - return result; |
166 |
| - |
| 108 | + public DynamicSystem makeSystem(Prototype p, Class<? extends DynamicComponent> compClass) throws Exception { |
| 109 | + String name = makeSystemClassName(p.getId()); |
| 110 | + return new DynamicSystem(name, scope, cx, p.getUpdater(), compClass, p.getHandlers()); |
167 | 111 | }
|
168 | 112 |
|
169 | 113 |
|
|
0 commit comments