-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add random presets completion and validation
- Add completion for cfgspawnabletypes.xml based on cfgrandompresets.xml. - Add validation for cfgspawnabletypes.xml based on cfgrandompresets.xml.
- Loading branch information
Showing
5 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
lemminx-dayz-ce/src/main/java/io/github/rvost/lemminx/dayz/model/RandomPresetsModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package io.github.rvost.lemminx.dayz.model; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static java.util.Map.entry; | ||
|
||
public class RandomPresetsModel { | ||
public static final String CFGRANDOMPRESETS_FILE = "cfgrandompresets.xml"; | ||
public static final String CARGO_TAG = "cargo"; | ||
public static final String ATTACHMENTS_TAG = "attachments"; | ||
public static final String NAME_ATTRIBUTE = "name"; | ||
|
||
public static boolean isRandomPresets(DOMDocument document) { | ||
if (document == null) { | ||
return false; | ||
} | ||
var uri = document.getDocumentURI(); | ||
return uri != null && uri.toLowerCase().endsWith(CFGRANDOMPRESETS_FILE); | ||
} | ||
|
||
public static Map<String, Set<String>> getRandomPresets(Path missionPath) { | ||
var path = missionPath.resolve(CFGRANDOMPRESETS_FILE); | ||
|
||
try (var input = Files.newInputStream(path)) { | ||
return getRandomPresets(input); | ||
} catch (IOException e) { | ||
return Map.of(); | ||
} | ||
} | ||
|
||
public static Map<String, Set<String>> getRandomPresets(InputStream input) throws IOException { | ||
try { | ||
var db = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder(); | ||
var doc = db.parse(input); | ||
if (doc.getDocumentElement() != null) { | ||
doc.getDocumentElement().normalize(); | ||
var cargo = getValues(doc.getElementsByTagName(CARGO_TAG)); | ||
var attachments = getValues(doc.getElementsByTagName(ATTACHMENTS_TAG)); | ||
return new HashMap<>(Map.ofEntries( | ||
entry(CARGO_TAG, cargo), | ||
entry(ATTACHMENTS_TAG, attachments) | ||
)); | ||
} else { | ||
return Map.of(); | ||
} | ||
} catch (ParserConfigurationException | SAXException e) { | ||
return Map.of(); | ||
} | ||
} | ||
|
||
private static Set<String> getValues(NodeList nodes) { | ||
var result = new HashSet<String>(); | ||
for (int i = 0; i < nodes.getLength(); i++) { | ||
var node = nodes.item(i); | ||
var attributes = node.getAttributes(); | ||
if (attributes != null) { | ||
var nameAttr = attributes.getNamedItem(NAME_ATTRIBUTE); | ||
if (nameAttr != null) { | ||
result.add(nameAttr.getNodeValue()); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
lemminx-dayz-ce/src/main/java/io/github/rvost/lemminx/dayz/model/SpawnableTypesModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.github.rvost.lemminx.dayz.model; | ||
|
||
import org.eclipse.lemminx.dom.DOMDocument; | ||
|
||
public class SpawnableTypesModel { | ||
public static final String SPAWNABLETYPES_TAG = "spawnabletypes"; | ||
public static final String PRESET_ATTRIBUTE = "preset"; | ||
|
||
public static boolean isSpawnableTypes(DOMDocument document) { | ||
var docElement = document.getDocumentElement(); | ||
return docElement != null && SPAWNABLETYPES_TAG.equals(docElement.getNodeName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters