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

wurstscript / WurstScript / 228

29 Nov 2023 05:00PM UTC coverage: 62.48% (-0.09%) from 62.574%
228

push

circleci

web-flow
Show dialog for choosing game path, cleanup (#1083)

* show dialog for choosing game path

* cleanup code

* remove logs and refactor

* remove confusing mpq error, make some mpq loads readonly

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

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.languageserver.requests.MapRequest;
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 MapRequest.CompilationResult 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
        MapRequest.CompilationResult result = new MapRequest.CompilationResult();
×
41
        result.script = compiledScript;
×
42
        try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(targetMap), true)) {
×
43
            byte[] scriptBytes;
44
            if (!projectConfig.getBuildMapData().getName().isEmpty()) {
×
45
                result.script = new File(buildDir, "war3mapj_with_config.j.txt");
×
46
                // Apply w3i config values
47
                W3I w3I = prepareW3I(projectConfig, mpq);
×
48
                FileInputStream inputStream = new FileInputStream(compiledScript);
×
49
                StringWriter sw = new StringWriter();
×
50

51
                if (runArgs.isLua()) {
×
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
60
                        );
61
                        w3I.injectConfigsInJassScript(inputStream, sw, version);
×
62
                    }
63
                }
64
                scriptBytes = sw.toString().getBytes(StandardCharsets.UTF_8);
×
65

66
                result.w3i = new File(buildDir, "war3map.w3i");
×
67
                if (runArgs.isLua()) {
×
68
                    w3I.setScriptLang(W3I.ScriptLang.LUA);
×
69
                    w3I.setFileVersion(W3I.EncodingFormat.W3I_0x1F.getVersion());
×
70
                }
71
                w3I.write(result.w3i);
×
72
                Files.write(scriptBytes, result.script);
×
73
            }
74
        } catch (Exception e) {
×
75
            throw new RuntimeException(e);
×
76
        }
×
77

78
        applyMapHeader(projectConfig, targetMap);
×
79

80
        return result;
×
81
    }
82

83
    private static W3I prepareW3I(WurstProjectConfigData projectConfig, MpqEditor mpq) throws Exception {
84
        W3I w3I = new W3I(mpq.extractFile("war3map.w3i"));
×
85
        WurstProjectBuildMapData buildMapData = projectConfig.getBuildMapData();
×
86
        if (StringUtils.isNotBlank(buildMapData.getName())) {
×
87
            w3I.setMapName(buildMapData.getName());
×
88
        }
89
        if (StringUtils.isNotBlank(buildMapData.getAuthor())) {
×
90
            w3I.setMapAuthor(buildMapData.getAuthor());
×
91
        }
92
        applyScenarioData(w3I, buildMapData);
×
93

94
        if (!buildMapData.getPlayers().isEmpty()) {
×
95
            applyPlayers(projectConfig, w3I);
×
96
        }
97
        if (!buildMapData.getForces().isEmpty()) {
×
98
            applyForces(projectConfig, w3I);
×
99
        }
100
        applyOptionFlags(projectConfig, w3I);
×
101

102
        return w3I;
×
103
    }
104

105
    private static void applyOptionFlags(WurstProjectConfigData projectConfig, W3I w3I) {
106
        WurstProjectBuildOptionFlagsData optionsFlags = projectConfig.getBuildMapData().getOptionsFlags();
×
107
        w3I.setFlag(MapFlag.HIDE_MINIMAP, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.HIDE_MINIMAP));
×
108
        w3I.setFlag(MapFlag.FIXED_PLAYER_FORCE_SETTING, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.FIXED_PLAYER_FORCE_SETTING));
×
109
        w3I.setFlag(MapFlag.MASKED_AREAS_PARTIALLY_VISIBLE, optionsFlags.getForcesFixed() || w3I.getFlag(MapFlag.MASKED_AREAS_PARTIALLY_VISIBLE));
×
110
        w3I.setFlag(MapFlag.SHOW_WATER_WAVES_ON_CLIFF_SHORES, optionsFlags.getShowWavesOnCliffShores() || w3I.getFlag(MapFlag.SHOW_WATER_WAVES_ON_CLIFF_SHORES));
×
111
        w3I.setFlag(MapFlag.SHOW_WATER_WAVES_ON_ROLLING_SHORES, optionsFlags.getShowWavesOnRollingShores() || w3I.getFlag(MapFlag.SHOW_WATER_WAVES_ON_ROLLING_SHORES));
×
112
    }
×
113

114
    private static void applyScenarioData(W3I w3I, WurstProjectBuildMapData buildMapData) {
115
        WurstProjectBuildScenarioData scenarioData = buildMapData.getScenarioData();
×
116
        if (StringUtils.isNotBlank(scenarioData.getSuggestedPlayers())) {
×
117
            w3I.setPlayersRecommendedAmount(scenarioData.getSuggestedPlayers());
×
118
        }
119
        if (StringUtils.isNotBlank(scenarioData.getDescription())) {
×
120
            w3I.setMapDescription(scenarioData.getDescription());
×
121
        }
122
        if (scenarioData.getLoadingScreen() != null) {
×
123
            applyLoadingScreen(w3I, scenarioData.getLoadingScreen());
×
124
        }
125
    }
×
126

127
    private static void applyLoadingScreen(W3I w3I, WurstProjectBuildLoadingScreenData loadingScreenData) {
128
        if (StringUtils.isNotBlank(loadingScreenData.getModel())) {
×
129
            w3I.getLoadingScreen().setBackground(new LoadingScreenBackground.CustomBackground(new File(loadingScreenData.getModel())));
×
130
        } else if (StringUtils.isNotBlank(loadingScreenData.getBackground())) {
×
131
            w3I.getLoadingScreen().setBackground(LoadingScreenBackground.PresetBackground.findByName(loadingScreenData.getBackground()));
×
132
        }
133

134
        w3I.getLoadingScreen().setTitle(loadingScreenData.getTitle());
×
135
        w3I.getLoadingScreen().setSubtitle(loadingScreenData.getSubTitle());
×
136
        w3I.getLoadingScreen().setText(loadingScreenData.getText());
×
137
    }
×
138

139
    private static void applyForces(WurstProjectConfigData projectConfig, W3I w3I) {
140
        w3I.clearForces();
×
141
        ArrayList<WurstProjectBuildForce> forces = projectConfig.getBuildMapData().getForces();
×
142
        for (WurstProjectBuildForce wforce : forces) {
×
143
            W3I.Force force = new Force();
×
144
            force.setName(wforce.getName());
×
145
            force.setFlag(W3I.Force.Flags.Flag.ALLIED, wforce.getFlags().getAllied());
×
146
            force.setFlag(W3I.Force.Flags.Flag.ALLIED_VICTORY, wforce.getFlags().getAlliedVictory());
×
147
            force.setFlag(W3I.Force.Flags.Flag.SHARED_VISION, wforce.getFlags().getSharedVision());
×
148
            force.setFlag(W3I.Force.Flags.Flag.SHARED_UNIT_CONTROL, wforce.getFlags().getSharedControl());
×
149
            force.setFlag(W3I.Force.Flags.Flag.SHARED_UNIT_CONTROL_ADVANCED, wforce.getFlags().getSharedControlAdvanced());
×
150
            force.addPlayerNums(wforce.getPlayerIds());
×
151
            w3I.addForce(force);
×
152
        }
×
153
        w3I.setFlag(MapFlag.USE_CUSTOM_FORCES, true);
×
154
    }
×
155

156
    private static void applyPlayers(WurstProjectConfigData projectConfig, W3I w3I) {
157
        List<W3I.Player> existing = new ArrayList<>(w3I.getPlayers());
×
158
        w3I.getPlayers().clear();
×
159
        ArrayList<WurstProjectBuildPlayer> players = projectConfig.getBuildMapData().getPlayers();
×
160
        for (WurstProjectBuildPlayer wplayer : players) {
×
161
            Optional<W3I.Player> old = existing.stream().filter(player -> player.getNum() == wplayer.getId()).findFirst();
×
162
            W3I.Player player = new Player();
×
163
            player.setNum(wplayer.getId());
×
164
            w3I.addPlayer(player);
×
165

166
            old.ifPresent(player1 -> applyExistingPlayerConfig(player1, player));
×
167

168
            setVolatilePlayerConfig(wplayer, player);
×
169
        }
×
170
    }
×
171

172
    private static void applyExistingPlayerConfig(W3I.Player oldPlayer, W3I.Player player) {
173
        player.setStartPos(oldPlayer.getStartPos());
×
174
        player.setName(oldPlayer.getName());
×
175
        player.setRace(oldPlayer.getRace());
×
176
        player.setType(oldPlayer.getType());
×
177
        player.setStartPosFixed(oldPlayer.getStartPosFixed());
×
178
        player.setAllyLowPrioFlags(oldPlayer.getAllyLowPrioFlags());
×
179
        player.setAllyHighPrioFlags(oldPlayer.getAllyHighPrioFlags());
×
180
    }
×
181

182
    private static void setVolatilePlayerConfig(WurstProjectBuildPlayer wplayer, W3I.Player player) {
183
        if (wplayer.getName() != null) {
×
184
            player.setName(wplayer.getName());
×
185
        }
186

187
        if (wplayer.getRace() != null) {
×
188
            W3I.Player.UnitRace val = W3I.Player.UnitRace.valueOf(wplayer.getRace().toString());
×
189
            if (val != null) {
×
190
                player.setRace(val);
×
191
            }
192
        }
193
        if (wplayer.getController() != null) {
×
194
            net.moonlightflower.wc3libs.dataTypes.app.Controller val1 = Controller.valueOf(wplayer.getController().toString());
×
195
            if (val1 != null) {
×
196
                player.setType(val1);
×
197
            }
198
        }
199
        if (wplayer.getFixedStartLoc() != null) {
×
200
            player.setStartPosFixed(wplayer.getFixedStartLoc() ? 1 : 0);
×
201
        }
202
    }
×
203

204
    private static void applyMapHeader(WurstProjectConfigData projectConfig, File targetMap) throws IOException {
205
        MapHeader mapHeader = MapHeader.ofFile(targetMap);
×
206
        if (projectConfig.getBuildMapData().getPlayers().size() > 0) {
×
207
            mapHeader.setMaxPlayersCount(projectConfig.getBuildMapData().getPlayers().size());
×
208
        }
209
        if (StringUtils.isNotBlank(projectConfig.getBuildMapData().getName())) {
×
210
            mapHeader.setMapName(projectConfig.getBuildMapData().getName());
×
211
        }
212
        mapHeader.writeToMapFile(targetMap);
×
213
    }
×
214
}
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