Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.7 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2024
// www.magicbane.com
package engine.ConfigParsing;
import engine.ConfigParsing.EffectEntry.EffectEntry;
import engine.ConfigParsing.EffectEntry.EffectModifier;
import engine.gameManager.ConfigManager;
import engine.mbEnums;
import org.pmw.tinylog.Logger;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EffectsParser {
public static String EffectsPath = ConfigManager.DEFAULT_DATA_DIR + "wpak/Effects.cfg";
public static HashMap<String, EffectEntry> effect_data = new HashMap<>();
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)(.+?)(?=MODSEND)", Pattern.DOTALL);
private static final Pattern STRSPLIT_REGEX = Pattern.compile("([^\"]\\S*|\"[^\"]*\")\\s*");
public static void init() throws IOException {
byte[] fileData = Files.readAllBytes(Paths.get(EffectsPath));
String fileContents = new String(fileData);
Matcher matcher = EFFECT_REGEX.matcher(fileContents);
// Iterate effect entries from .wpak config data
while (matcher.find()) {
EffectEntry effectEntry = parseEffectEntry(matcher.group());
if (effectEntry != null)
effect_data.put(effectEntry.id, effectEntry);
}
}
private static EffectEntry parseEffectEntry(String effectData) {
EffectEntry effectEntry = new EffectEntry();
// Remove all lines that contain a # and leading/trailing blanks
effectData = effectData.replaceAll("(?m)^.*#.*\r?\n?", "");
effectData = effectData.trim();
// Parse effect entry description
String firstLine;
ArrayList<String> effectDescription = new ArrayList<>();
// Some effects exist without sources/mods or conditions
// (ACID "MOB" 0)
if (effectData.indexOf('\n') > 0)
firstLine = effectData.substring(0, effectData.indexOf('\n'));
else
firstLine = effectData;
// Regex ignores spaces within quotes
Matcher matcher = STRSPLIT_REGEX.matcher(firstLine);
while (matcher.find())
effectDescription.add(matcher.group(1).trim());
effectEntry.id = effectDescription.get(0);
effectEntry.name = effectDescription.get(1);
effectEntry.name = effectEntry.name.replaceAll("\"", "");
// Some effect mods have no icon
// (SEEINVIS-SHADE "See Invis")
if (effectDescription.size() == 3)
effectEntry.icon = Integer.parseInt(effectDescription.get(2));
else
effectEntry.icon = 0;
// Parse source entries
matcher = SOURCE_REGEX.matcher(effectData);
while (matcher.find())
effectEntry.sources.add(matcher.group().trim());
// Parse modifier entries
matcher = MODS_REGEX.matcher(effectData);
// Iterate effect entries from .wpak config data
while (matcher.find()) {
EffectModifier effectModifier = parseModEntry(matcher.group());
if (effectModifier != null)
effectEntry.mods.add(effectModifier);
}
return effectEntry;
}
private static EffectModifier parseModEntry(String modData) {
EffectModifier effectModifier = new EffectModifier();
String[] modEntries = modData.trim().split("\n");
for (String modEntry : modEntries) {
ArrayList<String> modValues = new ArrayList<>();
Matcher matcher = STRSPLIT_REGEX.matcher(modEntry.trim());
while (matcher.find())
modValues.add(matcher.group(1).trim());
effectModifier.type = mbEnums.ModType.valueOf(modValues.get(0).trim());
switch (effectModifier.type) {
case AnimOverride:
effectModifier.min = Integer.parseInt(modValues.get(1).trim());
effectModifier.max = Integer.parseInt(modValues.get(2).trim());
break;
case BladeTrails:
break;
case MeleeDamageModifier:
case OCV:
case DCV:
case AttackDelay:
case AdjustAboveDmgCap:
case DamageCap:
case ArmorPiercing:
effectModifier.min = Integer.parseInt(modValues.get(1).trim());
effectModifier.scale = Float.parseFloat(modValues.get(2).trim());
effectModifier.slopeType = mbEnums.CompoundCurveType.valueOf(modValues.get(3).trim());
break;
default:
Logger.error("Unhandled type: " + effectModifier.type);
break;
}
}
return effectModifier;
}
}