• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 227

29 Nov 2023 11:46AM UTC coverage: 62.48% (-0.09%) from 62.574%
227

Pull #1083

circleci

Frotty
remove confusing mpq error, make some mpq loads readonly
Pull Request #1083: Show dialog for choosing game path

17295 of 27681 relevant lines covered (62.48%)

0.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.05
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/intermediateLang/interpreter/CompiletimeNatives.java
1
package de.peeeq.wurstio.intermediateLang.interpreter;
2

3

4
import config.WurstProjectConfigData;
5
import de.peeeq.wurstio.jassinterpreter.InterpreterException;
6
import de.peeeq.wurstio.jassinterpreter.ReflectionBasedNativeProvider;
7
import de.peeeq.wurstio.objectreader.ObjectHelper;
8
import de.peeeq.wurstscript.intermediatelang.*;
9
import de.peeeq.wurstscript.intermediatelang.interpreter.NativesProvider;
10
import de.peeeq.wurstscript.intermediatelang.interpreter.ProgramState;
11
import net.moonlightflower.wc3libs.bin.ObjMod;
12
import net.moonlightflower.wc3libs.bin.app.objMod.W3U;
13
import net.moonlightflower.wc3libs.dataTypes.DataType;
14
import net.moonlightflower.wc3libs.dataTypes.app.War3Int;
15
import net.moonlightflower.wc3libs.dataTypes.app.War3Real;
16
import net.moonlightflower.wc3libs.dataTypes.app.War3String;
17
import net.moonlightflower.wc3libs.misc.MetaFieldId;
18
import net.moonlightflower.wc3libs.misc.ObjId;
19

20
import java.time.LocalDateTime;
21
import java.time.temporal.ChronoUnit;
22

23
@SuppressWarnings("ucd") // ignore unused code detector warnings, because this class uses reflection
24
public class CompiletimeNatives extends ReflectionBasedNativeProvider implements NativesProvider {
25
    private final boolean isProd;
26
    private final ProgramStateIO globalState;
27
    private final WurstProjectConfigData projectConfigData;
28

29
    public CompiletimeNatives(ProgramStateIO globalState, WurstProjectConfigData projectConfigData, boolean isProd) {
1✔
30
        this.globalState = globalState;
1✔
31
        this.projectConfigData = projectConfigData;
1✔
32
        this.isProd = isProd;
1✔
33
    }
1✔
34

35

36
    private ILconstTuple makeKey(String key) {
37
        return new ILconstTuple(new ILconstString(key));
1✔
38
    }
39

40
    public ILconstTuple createObjectDefinition(ILconstString fileType, ILconstInt newUnitId, ILconstInt deriveFrom) {
41
        ObjMod<? extends ObjMod.Obj> dataStore = globalState.getDataStore(fileType.getVal());
1✔
42
        String objIdString = ObjectHelper.objectIdIntToString(newUnitId.getVal());
1✔
43

44
        if (dataStore.getObjs().containsKey(ObjId.valueOf(objIdString))) {
1✔
45
            globalState.compilationError("Object definition with id " + objIdString + " already exists.");
×
46
        }
47
        ObjMod.Obj objDef = newDefFromFiletype(dataStore, deriveFrom.getVal(), newUnitId.getVal());
1✔
48
        // mark object with special field
49
        ObjMod.Obj.Mod mod = new ObjMod.Obj.Mod(MetaFieldId.valueOf("wurs"), ObjMod.ValType.INT, War3Int.valueOf(ProgramState.GENERATED_BY_WURST));
1✔
50
        objDef.addMod(mod);
1✔
51
        String key = globalState.addObjectDefinition(objDef);
1✔
52

53
        return makeKey(key);
1✔
54
    }
55

56
    private W3U.Obj newDefFromFiletype(ObjMod<? extends ObjMod.Obj> dataStore, int base, int newId) {
57
        ObjId baseIdS = ObjId.valueOf(ObjectHelper.objectIdIntToString(base));
1✔
58
        ObjId newIdS = ObjId.valueOf(ObjectHelper.objectIdIntToString(newId));
1✔
59
        return dataStore.addObj(newIdS, baseIdS);
1✔
60
    }
61

62

63
    public void ObjectDefinition_setInt(ILconstTuple unitType, ILconstString modification, ILconstInt value) {
64
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
65
        modifyObject(od, modification, ObjMod.ValType.INT, War3Int.valueOf(value.getVal()));
1✔
66
    }
1✔
67

68
    public void ObjectDefinition_setString(ILconstTuple unitType, ILconstString modification, ILconstString value) {
69
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
70
        modifyObject(od, modification, ObjMod.ValType.STRING, War3String.valueOf(value.getVal()));
1✔
71
    }
1✔
72

73
    public void ObjectDefinition_setReal(ILconstTuple unitType, ILconstString modification, ILconstReal value) {
74
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
75
        modifyObject(od, modification, ObjMod.ValType.REAL, War3Real.valueOf(value.getVal()));
1✔
76
    }
1✔
77

78
    public void ObjectDefinition_setUnreal(ILconstTuple unitType, ILconstString modification, ILconstReal value) {
79
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
80
        modifyObject(od, modification, ObjMod.ValType.UNREAL, War3Real.valueOf(value.getVal()));
1✔
81
    }
1✔
82

83

84
    public void ObjectDefinition_setLvlInt(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt value) {
85
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
×
86
        modifyObject(od, modification, ObjMod.ValType.INT, level.getVal(), War3Int.valueOf(value.getVal()));
×
87
    }
×
88

89
    public void ObjectDefinition_setLvlString(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstString value) {
90
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
×
91
        modifyObject(od, modification, ObjMod.ValType.STRING, level.getVal(), War3String.valueOf(value.getVal()));
×
92
    }
×
93

94
    public void ObjectDefinition_setLvlReal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstReal value) {
95
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
×
96
        modifyObject(od, modification, ObjMod.ValType.REAL, level.getVal(), War3Real.valueOf(value.getVal()));
×
97
    }
×
98

99
    public void ObjectDefinition_setLvlUnreal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstReal value) {
100
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
×
101
        modifyObject(od, modification, ObjMod.ValType.UNREAL, level.getVal(), War3Real.valueOf(value.getVal()));
×
102
    }
×
103

104

105
    public void ObjectDefinition_setLvlDataInt(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstInt value) {
106
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
107
        modifyObject(od, modification, ObjMod.ValType.INT, level.getVal(), dataPointer.getVal(), War3Int.valueOf(value.getVal()));
1✔
108
    }
1✔
109

110
    public void ObjectDefinition_setLvlDataString(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstString value) {
111
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
112
        modifyObject(od, modification, ObjMod.ValType.STRING, level.getVal(), dataPointer.getVal(), War3String.valueOf(value.getVal()));
1✔
113
    }
1✔
114

115
    public void ObjectDefinition_setLvlDataReal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstReal value) {
116
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
×
117
        modifyObject(od, modification, ObjMod.ValType.REAL, level.getVal(), dataPointer.getVal(), War3Real.valueOf(value.getVal()));
×
118
    }
×
119

120
    public void ObjectDefinition_setLvlDataUnreal(ILconstTuple unitType, ILconstString modification, ILconstInt level, ILconstInt dataPointer, ILconstReal value) {
121
        ObjMod.Obj od = globalState.getObjectDefinition(getKey(unitType));
1✔
122
        modifyObject(od, modification, ObjMod.ValType.UNREAL, level.getVal(), dataPointer.getVal(), War3Real.valueOf(value.getVal()));
1✔
123
    }
1✔
124

125

126
    private <T> void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, DataType value) {
127
        modifyObject(od, modification, variableType, 1, 0, value);
1✔
128
    }
1✔
129

130
    private <T> void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, int level, DataType value) {
131
        modifyObject(od, modification, variableType, level, 0, value);
×
132
    }
×
133

134
    private void modifyObject(ObjMod.Obj od, ILconstString modification, ObjMod.ValType variableType, int level, int datapointer, DataType value) {
135
        String modificationId = modification.getVal();
1✔
136
        ObjMod.Obj.Mod foundMod = null;
1✔
137
        for (ObjMod.Obj.Mod m : od.getMods()) {
1✔
138
            if (m instanceof ObjMod.Obj.ExtendedMod) {
1✔
139
                ObjMod.Obj.ExtendedMod extMod = (ObjMod.Obj.ExtendedMod) m;
1✔
140
                if (extMod.getId().getVal().equals(modificationId) && extMod.getLevel() == level) {
1✔
141
                    // How to set data???
142
                    foundMod = extMod;
1✔
143
                    break;
1✔
144
                }
145
            }
146

147
        }
1✔
148

149
        // create new modification:
150
        if (foundMod != null) {
1✔
151
            od.remove(foundMod);
1✔
152
        }
153
        od.addMod(new ObjMod.Obj.ExtendedMod(MetaFieldId.valueOf(modificationId), variableType, value, level, datapointer));
1✔
154

155
    }
1✔
156

157
    private String getKey(ILconstTuple unitType) {
158
        return ((ILconstString) unitType.getValue(0)).getVal();
1✔
159
    }
160

161
    public void compileError(ILconstString msg) {
162
        throw new InterpreterException(msg.getVal());
×
163
    }
164

165
    public ILconstString getMapName() {
166
        return new ILconstString(projectConfigData.getBuildMapData().getName());
×
167
    }
168

169
    public ILconstString getBuildDate() {
170
        return new ILconstString(LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES).toString());
×
171
    }
172

173
    public ILconstBool isProductionBuild() {
174
        return isProd ? ILconstBool.TRUE : ILconstBool.FALSE;
×
175
    }
176
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc