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

wurstscript / WurstScript / 194

pending completion
194

push

circleci

Eliminate stacktrace when compiling on linux (#1076)

* Eliminate stacktrace when compiling on linux

* catch unsupported platform at exe path

17138 of 27014 relevant lines covered (63.44%)

0.63 hits per line

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

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/languageserver/ProjectConfigBuilder.java
1
package de.peeeq.wurstio.languageserver;
2

3
import com.google.common.io.Files;
4
import config.*;
5
import de.peeeq.wurstio.Pjass;
6
import de.peeeq.wurstio.languageserver.requests.RequestFailedException;
7
import de.peeeq.wurstio.mpq.MpqEditor;
8
import de.peeeq.wurstio.mpq.MpqEditorFactory;
9
import de.peeeq.wurstio.utils.W3InstallationData;
10
import de.peeeq.wurstscript.RunArgs;
11
import net.moonlightflower.wc3libs.bin.app.MapFlag;
12
import net.moonlightflower.wc3libs.bin.app.MapHeader;
13
import net.moonlightflower.wc3libs.bin.app.W3I;
14
import net.moonlightflower.wc3libs.bin.app.W3I.Force;
15
import net.moonlightflower.wc3libs.bin.app.W3I.Player;
16
import net.moonlightflower.wc3libs.dataTypes.app.Controller;
17
import net.moonlightflower.wc3libs.dataTypes.app.LoadingScreenBackground;
18
import net.moonlightflower.wc3libs.port.GameVersion;
19
import org.apache.commons.lang.StringUtils;
20
import org.eclipse.lsp4j.MessageType;
21

22
import java.io.File;
23
import java.io.FileInputStream;
24
import java.io.IOException;
25
import java.io.StringWriter;
26
import java.nio.charset.StandardCharsets;
27
import java.util.ArrayList;
28
import java.util.List;
29
import java.util.Optional;
30

31
public class ProjectConfigBuilder {
×
32
    public static final String FILE_NAME = "wurst.build";
33

34
    public static void apply(WurstProjectConfigData projectConfig, File targetMap, File compiledScript, File buildDir,
35
            RunArgs runArgs, W3InstallationData w3data) throws IOException {
36
        if (projectConfig.getProjectName().isEmpty()) {
×
37
            throw new RequestFailedException(MessageType.Error, "wurst.build is missing projectName.");
×
38
        }
39

40

41
        try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(targetMap))) {
×
42
            File file = new File(buildDir, "wc3libs_injected.j");
×
43
            byte[] scriptBytes;
44
            if (!projectConfig.getBuildMapData().getName().isEmpty()) {
×
45
                // Apply w3i config values
46
                W3I w3I = prepareW3I(projectConfig, mpq);
×
47
                FileInputStream inputStream = new FileInputStream(compiledScript);
×
48
                StringWriter sw = new StringWriter();
×
49

50
                // TODO apply config for hot start before JHCR transformation
51
                if (runArgs.isLua() || runArgs.isHotStartmap()) {
×
52
                    w3I.injectConfigsInLuaScript(inputStream, sw);
×
53
                } else {
54
                    if (w3data.getWc3PatchVersion().isPresent()) {
×
55
                        w3I.injectConfigsInJassScript(inputStream, sw, w3data.getWc3PatchVersion().get());
×
56
                    } else {
57
                        GameVersion version = GameVersion.VERSION_1_32;
×
58
                        System.out.println(
×
59
                            "Failed to determine installed game version. Falling back to " + version.toString()
×
60
                        );
61
                        w3I.injectConfigsInJassScript(inputStream, sw, version);
×
62
                    }
63
                }
64
                scriptBytes = sw.toString().getBytes(StandardCharsets.UTF_8);
×
65

66
                File w3iFile = new File("w3iFile");
×
67
                if (runArgs.isLua()) {
×
68
                    w3I.setScriptLang(W3I.ScriptLang.LUA);
×
69
                    w3I.setFileVersion(W3I.EncodingFormat.W3I_0x1C.getVersion());
×
70
                }
71
                w3I.write(w3iFile);
×
72

73
                mpq.deleteFile("war3map.w3i");
×
74
                mpq.insertFile("war3map.w3i", java.nio.file.Files.readAllBytes(w3iFile.toPath()));
×
75

76
                w3iFile.delete();
×
77
            } else {
×
78
                scriptBytes = java.nio.file.Files.readAllBytes(compiledScript.toPath());
×
79
            }
80

81
            Files.write(scriptBytes, file);
×
82
            if (!runArgs.isDisablePjass()) {
×
83
                Pjass.runPjass(file, new File(buildDir, "common.j").getAbsolutePath(),
×
84
                    new File(buildDir, "blizzard.j").getAbsolutePath());
×
85
            }
86
            String mapScriptName;
87
            if (runArgs.isLua()) {
×
88
                mapScriptName = "war3map.lua";
×
89
            } else {
90
                mapScriptName = "war3map.j";
×
91
            }
92
            mpq.deleteFile("war3map.lua");
×
93
            mpq.deleteFile("war3map.j");
×
94
            mpq.insertFile(mapScriptName, scriptBytes);
×
95

96
            file.delete();
×
97
        } catch (Exception e) {
×
98
            throw new RuntimeException(e);
×
99
        }
×
100

101
        applyMapHeader(projectConfig, targetMap);
×
102
    }
×
103

104
    private static W3I prepareW3I(WurstProjectConfigData projectConfig, MpqEditor mpq) throws Exception {
105
        W3I w3I = new W3I(mpq.extractFile("war3map.w3i"));
×
106
        WurstProjectBuildMapData buildMapData = projectConfig.getBuildMapData();
×
107
        if (StringUtils.isNotBlank(buildMapData.getName())) {
×
108
            w3I.setMapName(buildMapData.getName());
×
109
        }
110
        if (StringUtils.isNotBlank(buildMapData.getAuthor())) {
×
111
            w3I.setMapAuthor(buildMapData.getAuthor());
×
112
        }
113
        applyScenarioData(w3I, buildMapData);
×
114

115
        if (buildMapData.getPlayers().size() > 0) {
×
116
            applyPlayers(projectConfig, w3I);
×
117
        }
118
        if (buildMapData.getForces().size() > 0) {
×
119
            applyForces(projectConfig, w3I);
×
120
        }
121
        applyOptionFlags(projectConfig, w3I);
×
122

123
        return w3I;
×
124
    }
125

126
    private static void applyOptionFlags(WurstProjectConfigData projectConfig, W3I w3I) {
127
        WurstProjectBuildOptionFlagsData optionsFlags = projectConfig.getBuildMapData().getOptionsFlags();
×
128
        w3I.setFlag(MapFlag.HIDE_MINIMAP, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.HIDE_MINIMAP));
×
129
        w3I.setFlag(MapFlag.FIXED_PLAYER_FORCE_SETTING, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.FIXED_PLAYER_FORCE_SETTING));
×
130
        w3I.setFlag(MapFlag.MASKED_AREAS_PARTIALLY_VISIBLE, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.MASKED_AREAS_PARTIALLY_VISIBLE));
×
131
        w3I.setFlag(MapFlag.SHOW_WATER_WAVES_ON_CLIFF_SHORES, optionsFlags.getShowWavesOnCliffShores() || w3I.getFlag(MapFlag.SHOW_WATER_WAVES_ON_CLIFF_SHORES));
×
132
        w3I.setFlag(MapFlag.SHOW_WATER_WAVES_ON_ROLLING_SHORES, optionsFlags.getShowWavesOnRollingShores() || w3I.getFlag(MapFlag.SHOW_WATER_WAVES_ON_ROLLING_SHORES));
×
133
    }
×
134

135
    private static void applyScenarioData(W3I w3I, WurstProjectBuildMapData buildMapData) {
136
        WurstProjectBuildScenarioData scenarioData = buildMapData.getScenarioData();
×
137
        if (StringUtils.isNotBlank(scenarioData.getSuggestedPlayers())) {
×
138
            w3I.setPlayersRecommendedAmount(scenarioData.getSuggestedPlayers());
×
139
        }
140
        if (StringUtils.isNotBlank(scenarioData.getDescription())) {
×
141
            w3I.setMapDescription(scenarioData.getDescription());
×
142
        }
143
        if (scenarioData.getLoadingScreen() != null) {
×
144
            applyLoadingScreen(w3I, scenarioData.getLoadingScreen());
×
145
        }
146
    }
×
147

148
    private static void applyLoadingScreen(W3I w3I, WurstProjectBuildLoadingScreenData loadingScreen) {
149
        if (StringUtils.isNotBlank(loadingScreen.getModel())) {
×
150
            w3I.getLoadingScreen().setBackground(new LoadingScreenBackground.CustomBackground(new File(loadingScreen.getModel())));
×
151
        } else {
152
            w3I.getLoadingScreen().setBackground(LoadingScreenBackground.PresetBackground.findByName(loadingScreen.getBackground()));
×
153
        }
154

155
        w3I.getLoadingScreen().setTitle(loadingScreen.getTitle());
×
156
        w3I.getLoadingScreen().setSubtitle(loadingScreen.getSubTitle());
×
157
        w3I.getLoadingScreen().setText(loadingScreen.getText());
×
158
    }
×
159

160
    private static void applyForces(WurstProjectConfigData projectConfig, W3I w3I) {
161
        w3I.clearForces();
×
162
        ArrayList<WurstProjectBuildForce> forces = projectConfig.getBuildMapData().getForces();
×
163
        for (WurstProjectBuildForce wforce : forces) {
×
164
            W3I.Force force = new Force();
×
165
            force.setName(wforce.getName());
×
166
            force.setFlag(W3I.Force.Flags.Flag.ALLIED, wforce.getFlags().getAllied());
×
167
            force.setFlag(W3I.Force.Flags.Flag.ALLIED_VICTORY, wforce.getFlags().getAlliedVictory());
×
168
            force.setFlag(W3I.Force.Flags.Flag.SHARED_VISION, wforce.getFlags().getSharedVision());
×
169
            force.setFlag(W3I.Force.Flags.Flag.SHARED_UNIT_CONTROL, wforce.getFlags().getSharedControl());
×
170
            force.setFlag(W3I.Force.Flags.Flag.SHARED_UNIT_CONTROL_ADVANCED, wforce.getFlags().getSharedControlAdvanced());
×
171
            force.addPlayerNums(wforce.getPlayerIds());
×
172
            w3I.addForce(force);
×
173
        }
×
174
        w3I.setFlag(MapFlag.USE_CUSTOM_FORCES, true);
×
175
    }
×
176

177
    private static void applyPlayers(WurstProjectConfigData projectConfig, W3I w3I) {
178
        List<W3I.Player> existing = new ArrayList<>(w3I.getPlayers());
×
179
        w3I.getPlayers().clear();
×
180
        ArrayList<WurstProjectBuildPlayer> players = projectConfig.getBuildMapData().getPlayers();
×
181
        for (WurstProjectBuildPlayer wplayer : players) {
×
182
            Optional<W3I.Player> old = existing.stream().filter(player -> player.getNum() == wplayer.getId()).findFirst();
×
183
            W3I.Player player = new Player();
×
184
            player.setNum(wplayer.getId());
×
185
            w3I.addPlayer(player);
×
186

187
            old.ifPresent(player1 -> applyExistingPlayerConfig(player1, player));
×
188

189
            setVolatilePlayerConfig(wplayer, player);
×
190
        }
×
191
    }
×
192

193
    private static void applyExistingPlayerConfig(W3I.Player oldPlayer, W3I.Player player) {
194
        player.setStartPos(oldPlayer.getStartPos());
×
195
        player.setName(oldPlayer.getName());
×
196
        player.setRace(oldPlayer.getRace());
×
197
        player.setType(oldPlayer.getType());
×
198
        player.setStartPosFixed(oldPlayer.getStartPosFixed());
×
199
        player.setAllyLowPrioFlags(oldPlayer.getAllyLowPrioFlags());
×
200
        player.setAllyHighPrioFlags(oldPlayer.getAllyHighPrioFlags());
×
201
    }
×
202

203
    private static void setVolatilePlayerConfig(WurstProjectBuildPlayer wplayer, W3I.Player player) {
204
        if (wplayer.getName() != null) {
×
205
            player.setName(wplayer.getName());
×
206
        }
207

208
        if (wplayer.getRace() != null) {
×
209
            W3I.Player.UnitRace val = W3I.Player.UnitRace.valueOf(wplayer.getRace().toString());
×
210
            if (val != null) {
×
211
                player.setRace(val);
×
212
            }
213
        }
214
        if (wplayer.getController() != null) {
×
215
            net.moonlightflower.wc3libs.dataTypes.app.Controller val1 = Controller.valueOf(wplayer.getController().toString());
×
216
            if (val1 != null) {
×
217
                player.setType(val1);
×
218
            }
219
        }
220
        if (wplayer.getFixedStartLoc() != null) {
×
221
            player.setStartPosFixed(wplayer.getFixedStartLoc() ? 1 : 0);
×
222
        }
223
    }
×
224

225
    private static void applyMapHeader(WurstProjectConfigData projectConfig, File targetMap) throws IOException {
226
        MapHeader mapHeader = MapHeader.ofFile(targetMap);
×
227
        if (projectConfig.getBuildMapData().getPlayers().size() > 0) {
×
228
            mapHeader.setMaxPlayersCount(projectConfig.getBuildMapData().getPlayers().size());
×
229
        }
230
        if (StringUtils.isNotBlank(projectConfig.getBuildMapData().getName())) {
×
231
            mapHeader.setMapName(projectConfig.getBuildMapData().getName());
×
232
        }
233
        mapHeader.writeToMapFile(targetMap);
×
234
    }
×
235
}
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

© 2025 Coveralls, Inc