-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathCompiletimeNatives.java
More file actions
176 lines (138 loc) · 8.69 KB
/
CompiletimeNatives.java
File metadata and controls
176 lines (138 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package de.peeeq.wurstio.intermediateLang.interpreter;
import config.WurstProjectConfigData;
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
import de.peeeq.wurstio.jassinterpreter.ReflectionBasedNativeProvider;
import de.peeeq.wurstio.objectreader.ObjectHelper;
import de.peeeq.wurstscript.intermediatelang.*;
import de.peeeq.wurstscript.intermediatelang.interpreter.NativesProvider;
import de.peeeq.wurstscript.intermediatelang.interpreter.ProgramState;
import net.moonlightflower.wc3libs.bin.ObjMod;
import net.moonlightflower.wc3libs.bin.app.objMod.W3U;
import net.moonlightflower.wc3libs.dataTypes.DataType;
import net.moonlightflower.wc3libs.dataTypes.app.War3Int;
import net.moonlightflower.wc3libs.dataTypes.app.War3Real;
import net.moonlightflower.wc3libs.dataTypes.app.War3String;
import net.moonlightflower.wc3libs.misc.MetaFieldId;
import net.moonlightflower.wc3libs.misc.ObjId;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
@SuppressWarnings("ucd") // ignore unused code detector warnings, because this class uses reflection
public class CompiletimeNatives extends ReflectionBasedNativeProvider implements NativesProvider {
private final boolean isProd;
private final ProgramStateIO globalState;
private final WurstProjectConfigData projectConfigData;
public CompiletimeNatives(ProgramStateIO globalState, WurstProjectConfigData projectConfigData, boolean isProd) {
this.globalState = globalState;
this.projectConfigData = projectConfigData;
this.isProd = isProd;
}
private ILconstTuple makeKey(String key) {
return new ILconstTuple(new ILconstString(key));
}
public ILconstTuple createObjectDefinition(ILconstString fileType, ILconstInt newUnitId, ILconstInt deriveFrom) {
ObjMod<? extends ObjMod.Obj> dataStore = globalState.getDataStore(fileType.getVal());
String objIdString = ObjectHelper.objectIdIntToString(newUnitId.getVal());
if (dataStore.getObjs().containsKey(ObjId.valueOf(objIdString))) {
globalState.compilationError("Object definition with id " + objIdString + " already exists.");
}
ObjMod.Obj objDef = newDefFromFiletype(dataStore, deriveFrom.getVal(), newUnitId.getVal());
// mark object with special field
ObjMod.Obj.Mod mod = new ObjMod.Obj.Mod(MetaFieldId.valueOf("wurs"), ObjMod.ValType.INT, War3Int.valueOf(ProgramState.GENERATED_BY_WURST));
objDef.addMod(mod);
String key = globalState.addObjectDefinition(objDef);
return makeKey(key);
}
private W3U.Obj newDefFromFiletype(ObjMod<? extends ObjMod.Obj> dataStore, int base, int newId) {
ObjId baseIdS = ObjId.valueOf(ObjectHelper.objectIdIntToString(base));
ObjId newIdS = ObjId.valueOf(ObjectHelper.objectIdIntToString(newId));
return dataStore.addObj(newIdS, baseIdS);
}
public void ObjectDefinition_setInt(ILconstTuple unitType, ILconstString modification, ILconstInt value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.INT, War3Int.valueOf(value.getVal()));
}
public void ObjectDefinition_setString(ILconstTuple unitType, ILconstString modification, ILconstString value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.STRING, War3String.valueOf(value.getVal()));
}
public void ObjectDefinition_setReal(ILconstTuple unitType, ILconstString modification, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.REAL, War3Real.valueOf(value.getVal()));
}
public void ObjectDefinition_setUnreal(ILconstTuple unitType, ILconstString modification, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.UNREAL, War3Real.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlInt(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.INT, level.getVal(), War3Int.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlString(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstString value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.STRING, level.getVal(), War3String.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlReal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.REAL, level.getVal(), War3Real.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlUnreal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.UNREAL, level.getVal(), War3Real.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlDataInt(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstInt value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.INT, level.getVal(), dataPointer.getVal(), War3Int.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlDataString(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstString value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.STRING, level.getVal(), dataPointer.getVal(), War3String.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlDataReal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.REAL, level.getVal(), dataPointer.getVal(), War3Real.valueOf(value.getVal()));
}
public void ObjectDefinition_setLvlDataUnreal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstReal value) {
ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
modifyObject(od, modification, ObjMod.ValType.UNREAL, level.getVal(), dataPointer.getVal(), War3Real.valueOf(value.getVal()));
}
private <T> void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, DataType value) {
modifyObject(od, modification, variableType, 1, 0, value);
}
private <T> void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, int level, DataType value) {
modifyObject(od, modification, variableType, level, 0, value);
}
private void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, int level, int datapointer, DataType value) {
String modificationId = modification.getVal();
ObjMod.Obj.Mod foundMod = null;
for (ObjMod.Obj.Mod m : od.getMods()) {
if (m instanceof ObjMod.Obj.ExtendedMod) {
ObjMod.Obj.ExtendedMod extMod = (ObjMod.Obj.ExtendedMod) m;
if (extMod.getId().getVal().equals(modificationId) && extMod.getLevel() == level && extMod.getDataPt() == datapointer) {
// How to set data???
foundMod = extMod;
break;
}
}
}
// create new modification:
if (foundMod != null) {
od.remove(foundMod);
}
od.addMod(new ObjMod.Obj.ExtendedMod(MetaFieldId.valueOf(modificationId), variableType, value, level, datapointer));
}
private String getKey(ILconstTuple unitType) {
return ((ILconstString) unitType.getValue(0)).getVal();
}
public void compileError(ILconstString msg) {
throw new InterpreterException(msg.getVal());
}
public ILconstString getMapName() {
return new ILconstString(projectConfigData.getBuildMapData().getName());
}
public ILconstString getBuildDate() {
return new ILconstString(LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toString());
}
public ILconstBool isProductionBuild() {
return isProd ? ILconstBool.TRUE : ILconstBool.FALSE;
}
}