Files
lakebane/src/engine/ConfigParsing/EffectsParser.java
T

88 lines
3.4 KiB
Java
Raw Normal View History

2024-07-23 20:47:39 -05:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2024
// www.magicbane.com
package engine.ConfigParsing;
2024-07-24 12:56:24 -04:00
2024-08-06 16:56:41 -04:00
import engine.ConfigParsing.EffectEntry.EffectEntry;
2024-07-23 20:47:39 -05:00
import engine.gameManager.ConfigManager;
2024-07-23 21:15:37 -05:00
2024-07-23 20:47:39 -05:00
import java.io.IOException;
2024-07-24 12:56:24 -04:00
import java.nio.file.Files;
import java.nio.file.Paths;
2024-08-08 12:19:53 -04:00
import java.util.ArrayList;
2024-07-23 21:07:29 -05:00
import java.util.HashMap;
2024-08-06 16:52:06 -04:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2024-07-23 20:47:39 -05:00
public class EffectsParser {
2024-07-24 12:56:24 -04:00
2024-07-23 21:02:41 -05:00
public static String EffectsPath = ConfigManager.DEFAULT_DATA_DIR + "wpak/Effects.cfg";
2024-08-06 16:56:41 -04:00
public static HashMap<String, EffectEntry> effect_data = new HashMap<>();
2024-08-08 11:56:31 -04:00
private static final Pattern EFFECT_REGEX = Pattern.compile("(?<=EFFECTBEGIN)(.+?)(?=EFFECTEND)", Pattern.DOTALL);
private static final Pattern SOURCE_REGEX = Pattern.compile("(?<=SOURCEBEGIN)(.+?)(?=SOURCEEND)", Pattern.DOTALL);
private static final Pattern MODS_REGEX = Pattern.compile("(?<=MODSBEGIN)(.+?)(?=MODSEEND)", Pattern.DOTALL);
2024-08-08 12:19:53 -04:00
private static final Pattern STRSPLIT_REGEX = Pattern.compile("([^\"]\\S*|\".+?\")\\s*");
2024-07-23 20:47:39 -05:00
public static void init() throws IOException {
2024-07-24 12:56:24 -04:00
2024-08-06 16:52:06 -04:00
byte[] fileData = Files.readAllBytes(Paths.get(EffectsPath));
String fileContents = new String(fileData);
2024-08-06 17:07:07 -04:00
Matcher matcher = EFFECT_REGEX.matcher(fileContents);
2024-07-24 12:56:24 -04:00
2024-08-06 17:07:07 -04:00
// Iterate effect entries from .wpak config data
2024-07-24 12:56:24 -04:00
2024-08-06 16:52:06 -04:00
while (matcher.find()) {
2024-08-08 11:47:45 -04:00
EffectEntry effectEntry = parseEffectEntry(matcher.group());
2024-07-24 12:56:24 -04:00
2024-08-06 17:07:07 -04:00
if (effectEntry != null)
effect_data.put(effectEntry.id, effectEntry);
}
2024-07-23 20:47:39 -05:00
}
2024-08-06 17:07:07 -04:00
private static EffectEntry parseEffectEntry(String effectData) {
EffectEntry effectEntry = new EffectEntry();
2024-08-08 11:38:09 -04:00
// Remove all lines that contain a # and leading/trailing blanks
2024-08-06 17:07:07 -04:00
2024-08-08 11:36:00 -04:00
effectData = effectData.replaceAll("(?m)^.*#.*\r?\n?", "");
2024-08-08 11:38:09 -04:00
effectData = effectData.trim();
2024-08-06 16:56:41 -04:00
2024-08-06 17:17:57 -04:00
// Parse effect entry description
2024-08-08 12:54:58 -04:00
String firstLine;
ArrayList<String> effectDescription = new ArrayList<>();
2024-08-08 12:28:35 -04:00
2024-08-08 12:54:58 -04:00
if (effectData.indexOf('\n') > 0)
2024-08-08 12:28:35 -04:00
firstLine = effectData.substring(0, effectData.indexOf('\n'));
2024-08-08 12:54:58 -04:00
else
firstLine = effectData;
2024-08-08 12:28:35 -04:00
2024-08-08 12:54:58 -04:00
// Regex ignores spaces within quotes
2024-08-08 12:19:53 -04:00
Matcher matcher = STRSPLIT_REGEX.matcher(firstLine);
while (matcher.find())
effectDescription.add(matcher.group(1));
2024-08-06 17:17:57 -04:00
2024-08-08 12:19:53 -04:00
effectEntry.id = effectDescription.get(0);
effectEntry.name = effectDescription.get(1);
effectEntry.icon = Integer.parseInt(effectDescription.get(2));
2024-08-06 17:17:57 -04:00
// Parse source entries
2024-08-08 12:19:53 -04:00
matcher = SOURCE_REGEX.matcher(effectData);
2024-08-06 17:17:57 -04:00
while (matcher.find())
2024-08-08 11:47:45 -04:00
effectEntry.sources.add(matcher.group().trim());
2024-08-06 17:17:57 -04:00
2024-08-06 17:18:46 -04:00
// Parse modifier entries
2024-08-06 17:17:57 -04:00
2024-08-06 17:07:07 -04:00
return effectEntry;
2024-08-06 16:56:41 -04:00
}
2024-08-06 16:52:06 -04:00
}