Files
lakebane/src/engine/wpak/PowerActionParser.java
T

92 lines
3.3 KiB
Java
Raw Normal View History

2024-08-19 13:15:33 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2024
// www.magicbane.com
package engine.wpak;
2024-08-19 20:41:30 -04:00
import engine.gameManager.ConfigManager;
2024-08-19 21:11:53 -04:00
import engine.wpak.data.EffectDescription;
2024-08-19 20:49:43 -04:00
import engine.wpak.data.PowerActionEntry;
2024-08-19 20:41:30 -04:00
2024-08-19 20:49:43 -04:00
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
2024-08-19 21:02:13 -04:00
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
2024-08-19 20:49:43 -04:00
import java.util.regex.Matcher;
2024-08-19 20:41:30 -04:00
import java.util.regex.Pattern;
2024-08-19 13:17:44 -04:00
2024-08-19 20:41:30 -04:00
public class PowerActionParser {
private static final Pattern STRSPLIT_REGEX = Pattern.compile("([^\"]\\S*|\"[^\"]*\")\\s*");
private static final Pattern POWER_ACTION_REGEX = Pattern.compile("(?<=POWERACTIONBEGIN)(.+?)(?=POWERACTIONEND)", Pattern.DOTALL);
private static final String powerActionPath = ConfigManager.DEFAULT_DATA_DIR + "wpak/PowerActions.cfg";
2024-08-19 20:49:43 -04:00
public static void parseWpakFile() {
// Read .wpak file from disk
byte[] fileData;
try {
fileData = Files.readAllBytes(Paths.get(powerActionPath));
} catch (
IOException e) {
throw new RuntimeException(e);
}
String fileContents = new String(fileData);
// Iterate over power entries from .wpak data
Matcher matcher = POWER_ACTION_REGEX.matcher(fileContents);
while (matcher.find()) {
2024-08-19 20:50:45 -04:00
PowerActionEntry powerActionEntry = parsePowerActionEntry(matcher.group().trim());
2024-08-19 20:49:43 -04:00
}
}
2024-08-19 20:50:45 -04:00
private static PowerActionEntry parsePowerActionEntry(String powerActionData) {
2024-08-19 20:49:43 -04:00
PowerActionEntry powerActionEntry = new PowerActionEntry();
2024-08-19 21:11:53 -04:00
EffectDescription effectDescription;
2024-08-19 21:02:13 -04:00
// Remove all lines that contain a # and leading/trailing blank lines
powerActionData = powerActionData.replaceAll("(?m)^(\\s*#.*|\\s*)\r?\n?", "").trim();
String[] lineData = powerActionData.trim().split("\n");
// Parse effect entry header
2024-08-19 21:11:53 -04:00
Iterator<String> entryIterator = Arrays.stream(lineData).iterator();
2024-08-19 21:02:13 -04:00
2024-08-19 21:11:53 -04:00
String headerLine = entryIterator.next();
2024-08-19 21:03:34 -04:00
ArrayList<String> headerData = new ArrayList<>();
2024-08-19 21:02:13 -04:00
2024-08-19 21:03:34 -04:00
Matcher matcher = STRSPLIT_REGEX.matcher(headerLine.trim());
while (matcher.find())
headerData.add(matcher.group().trim());
2024-08-19 20:49:43 -04:00
2024-08-19 21:11:53 -04:00
Iterator<String> headerIterator = headerData.iterator();
powerActionEntry.action_id = headerIterator.next();
powerActionEntry.action_type = headerIterator.next();
while (headerIterator.hasNext()) {
effectDescription = new EffectDescription();
effectDescription.effect_id = headerIterator.next();
effectDescription.level = Integer.parseInt(headerIterator.next());
}
2024-08-19 20:49:43 -04:00
return powerActionEntry;
}
2024-08-19 13:15:33 -04:00
}