Dropped beanshell engine, new Faenor event parser.

This commit is contained in:
MobiusDev
2017-07-20 08:34:56 +00:00
parent 8151e1e43c
commit 5a7592db4e
15 changed files with 234 additions and 558 deletions

View File

@ -17,12 +17,14 @@
package com.l2jmobius.gameserver.script.faenor;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.ThreadPoolManager;
import com.l2jmobius.gameserver.script.DateRange;
import com.l2jmobius.gameserver.script.IntList;
@ -36,38 +38,25 @@ import com.l2jmobius.gameserver.script.ScriptEngine;
public class FaenorEventParser extends FaenorParser
{
static Logger _log = Logger.getLogger(FaenorEventParser.class.getName());
private DateRange eventDates = null;
private DateRange _eventDates = null;
@Override
public void parseScript(final Node eventNode, ScriptContext context)
{
final String ID = attribute(eventNode, "ID");
String ID = attribute(eventNode, "ID");
_eventDates = DateRange.parse(attribute(eventNode, "Active"), DATE_FORMAT);
if (DEBUG)
{
_log.fine("Parsing Event \"" + ID + "\"");
}
eventDates = DateRange.parse(attribute(eventNode, "Active"), DATE_FORMAT);
final Date currentDate = new Date();
if (eventDates.getEndDate().before(currentDate))
Date currentDate = new Date();
if (_eventDates.getEndDate().before(currentDate))
{
_log.info("Event ID: (" + ID + ") has passed... Ignored.");
return;
}
if (eventDates.getStartDate().after(currentDate))
if (_eventDates.getStartDate().after(currentDate))
{
_log.info("Event ID: (" + ID + ") is not active yet... Ignored.");
ThreadPoolManager.getInstance().scheduleGeneral(new Runnable()
{
@Override
public void run()
{
parseEventDropAndMessage(eventNode);
}
}, eventDates.getStartDate().getTime() - currentDate.getTime());
ThreadPoolManager.getInstance().scheduleGeneral(() -> parseEventDropAndMessage(eventNode), _eventDates.getStartDate().getTime() - currentDate.getTime());
return;
}
@ -91,35 +80,24 @@ public class FaenorEventParser extends FaenorParser
private void parseEventMessage(Node sysMsg)
{
if (DEBUG)
{
_log.fine("Parsing Event Message.");
}
try
{
final String type = attribute(sysMsg, "Type");
final String[] message = attribute(sysMsg, "Msg").split("\n");
String type = attribute(sysMsg, "Type");
String[] message = attribute(sysMsg, "Msg").split(Config.EOL);
if (type.equalsIgnoreCase("OnJoin"))
{
bridge.onPlayerLogin(message, eventDates);
_bridge.onPlayerLogin(message, _eventDates);
}
}
catch (final Exception e)
catch (Exception e)
{
_log.warning("Error in event parser.");
e.printStackTrace();
_log.log(Level.WARNING, "Error in event parser: " + e.getMessage(), e);
}
}
private void parseEventDropList(Node dropList)
{
if (DEBUG)
{
_log.fine("Parsing Droplist.");
}
for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "AllDrop"))
@ -131,22 +109,17 @@ public class FaenorEventParser extends FaenorParser
private void parseEventDrop(Node drop)
{
if (DEBUG)
{
_log.fine("Parsing Drop.");
}
try
{
final int[] items = IntList.parse(attribute(drop, "Items"));
final int[] count = IntList.parse(attribute(drop, "Count"));
final double chance = getPercent(attribute(drop, "Chance"));
int[] items = IntList.parse(attribute(drop, "Items"));
int[] count = IntList.parse(attribute(drop, "Count"));
double chance = getPercent(attribute(drop, "Chance"));
bridge.addEventDrop(items, count, chance, eventDates);
_bridge.addEventDrop(items, count, chance, _eventDates);
}
catch (final Exception e)
catch (Exception e)
{
_log.warning("ERROR(parseEventDrop):" + e.getMessage());
_log.log(Level.WARNING, "ERROR(parseEventDrop):" + e.getMessage(), e);
}
}
@ -163,4 +136,4 @@ public class FaenorEventParser extends FaenorParser
{
ScriptEngine.parserFactories.put(getParserName("Event"), new FaenorEventParserFactory());
}
}
}

View File

@ -17,50 +17,31 @@
package com.l2jmobius.gameserver.script.faenor;
import java.util.List;
import java.util.Map;
import javax.script.ScriptContext;
import java.util.logging.Logger;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.Announcements;
import com.l2jmobius.gameserver.EventDroplist;
import com.l2jmobius.gameserver.model.L2DropCategory;
import com.l2jmobius.gameserver.model.L2DropData;
import com.l2jmobius.gameserver.model.L2PetData;
import com.l2jmobius.gameserver.script.DateRange;
import com.l2jmobius.gameserver.script.EngineInterface;
import com.l2jmobius.gameserver.script.Expression;
import com.l2jmobius.gameserver.templates.L2NpcTemplate;
import javolution.util.FastList;
/**
* @author Luis Arias TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
* @author Luis Arias
*/
public class FaenorInterface implements EngineInterface
{
private static FaenorInterface _instance;
protected static final Logger _log = Logger.getLogger(FaenorInterface.class.getName());
public static FaenorInterface getInstance()
{
if (_instance == null)
{
_instance = new FaenorInterface();
}
return _instance;
return SingletonHolder._instance;
}
public FaenorInterface()
{
}
/*
* (non-Javadoc)
* @see com.l2jmobius.gameserver.script.EngineInterface#getAllPlayers()
*/
public List<?> getAllPlayers()
{
// TODO Auto-generated method stub
return null;
}
@ -144,7 +125,6 @@ public class FaenorInterface implements EngineInterface
maxCategory++;
npc.addDropData(drop, maxCategory);
}
}
/**
@ -158,39 +138,10 @@ public class FaenorInterface implements EngineInterface
npc.addDropData(drop, category);
}
/**
* @param npcID
* @return Returns the _questDrops.
*/
public List<L2DropData> getQuestDrops(int npcID)
{
final L2NpcTemplate npc = _npcTable.getTemplate(npcID);
if (npc == null)
{
return null;
}
final List<L2DropData> questDrops = new FastList<>();
if (npc.getDropData() != null)
{
for (final L2DropCategory cat : npc.getDropData())
{
for (final L2DropData drop : cat.getAllDrops())
{
if (drop.getQuestID() != null)
{
questDrops.add(drop);
}
}
}
}
return questDrops;
}
@Override
public void addEventDrop(int[] items, int[] count, double chance, DateRange range)
{
EventDroplist.getInstance().addGlobalDrop(items, count, (int) (chance * L2DropData.MAX_CHANCE), range);
EventDroplist.getInstance().addGlobalDrop(items, count, (int) (chance * 1000000), range);
}
@Override
@ -199,23 +150,8 @@ public class FaenorInterface implements EngineInterface
Announcements.getInstance().addEventAnnouncement(validDateRange, message);
}
public void addPetData(ScriptContext context, int petID, int levelStart, int levelEnd, Map<String, String> stats)
private static class SingletonHolder
{
final L2PetData[] petData = new L2PetData[(levelEnd - levelStart) + 1];
int value = 0;
for (int level = levelStart; level <= levelEnd; level++)
{
petData[level - 1] = new L2PetData();
petData[level - 1].setPetID(petID);
petData[level - 1].setPetLevel(level);
context.setAttribute("level", new Double(level), ScriptContext.ENGINE_SCOPE);
for (final String stat : stats.keySet())
{
value = ((Number) Expression.eval(context, "beanshell", stats.get(stat))).intValue();
petData[level - 1].setStat(stat, value);
}
context.removeAttribute("level", ScriptContext.ENGINE_SCOPE);
}
protected static final FaenorInterface _instance = new FaenorInterface();
}
}
}

View File

@ -34,10 +34,8 @@ import com.l2jmobius.gameserver.script.Parser;
*/
public abstract class FaenorParser extends Parser
{
protected static FaenorInterface bridge = FaenorInterface.getInstance();
protected static DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US);
public final static boolean DEBUG = true;
protected static FaenorInterface _bridge = FaenorInterface.getInstance();
protected final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US);
/*
* UTILITY FUNCTIONS
@ -58,7 +56,7 @@ public abstract class FaenorParser extends Parser
{
return node.getAttributes().getNamedItem(attributeName).getNodeValue();
}
catch (final Exception e)
catch (Exception e)
{
if (defaultValue != null)
{
@ -72,17 +70,17 @@ public abstract class FaenorParser extends Parser
{
try
{
final NodeList list = parentNode.getChildNodes();
NodeList list = parentNode.getChildNodes();
for (int i = 0; i < list.getLength(); i++)
{
final Node node = list.item(i);
Node node = list.item(i);
if (node.getNodeName().equalsIgnoreCase(elementName))
{
return node.getTextContent();
}
}
}
catch (final Exception e)
catch (Exception e)
{
}
if (defaultValue != null)
@ -90,7 +88,6 @@ public abstract class FaenorParser extends Parser
return defaultValue;
}
throw new NullPointerException();
}
public static boolean isNodeName(Node node, String name)
@ -98,7 +95,7 @@ public abstract class FaenorParser extends Parser
return node.getNodeName().equalsIgnoreCase(name);
}
public static Date getDate(String date) throws ParseException
public Date getDate(String date) throws ParseException
{
return DATE_FORMAT.parse(date);
}
@ -128,6 +125,10 @@ public abstract class FaenorParser extends Parser
return "faenor.Faenor" + name + "Parser";
}
/**
* @param node
* @param context
*/
@Override
public abstract void parseScript(Node node, ScriptContext context);
}
}

View File

@ -1,127 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.script.faenor;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import com.l2jmobius.gameserver.script.Parser;
import com.l2jmobius.gameserver.script.ParserFactory;
import com.l2jmobius.gameserver.script.ScriptEngine;
/**
* @author Luis Arias
*/
public class FaenorQuestParser extends FaenorParser
{
@Override
public void parseScript(Node questNode, ScriptContext context)
{
if (DEBUG)
{
System.out.println("Parsing Quest.");
}
final String questID = attribute(questNode, "ID");
for (Node node = questNode.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "DROPLIST"))
{
parseQuestDropList(node.cloneNode(true), questID);
}
else if (isNodeName(node, "DIALOG WINDOWS"))
{
// parseDialogWindows(node.cloneNode(true));
}
else if (isNodeName(node, "INITIATOR"))
{
// parseInitiator(node.cloneNode(true));
}
else if (isNodeName(node, "STATE"))
{
// parseState(node.cloneNode(true));
}
}
}
private void parseQuestDropList(Node dropList, String questID) throws NullPointerException
{
if (DEBUG)
{
System.out.println("Parsing Droplist.");
}
for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "DROP"))
{
parseQuestDrop(node.cloneNode(true), questID);
}
}
}
private void parseQuestDrop(Node drop, String questID)// throws NullPointerException
{
if (DEBUG)
{
System.out.println("Parsing Drop.");
}
int npcID;
int itemID;
int min;
int max;
int chance;
String[] states;
try
{
npcID = getInt(attribute(drop, "NpcID"));
itemID = getInt(attribute(drop, "ItemID"));
min = getInt(attribute(drop, "Min"));
max = getInt(attribute(drop, "Max"));
chance = getInt(attribute(drop, "Chance"));
states = (attribute(drop, "States")).split(",");
}
catch (final NullPointerException e)
{
throw new NullPointerException("Incorrect Drop Data");
}
if (DEBUG)
{
System.out.println("Adding Drop to NpcID: " + npcID);
}
bridge.addQuestDrop(npcID, itemID, min, max, chance, questID, states);
}
static class FaenorQuestParserFactory extends ParserFactory
{
@Override
public Parser create()
{
return (new FaenorQuestParser());
}
}
static
{
ScriptEngine.parserFactories.put(getParserName("Quest"), new FaenorQuestParserFactory());
}
}

View File

@ -17,177 +17,61 @@
package com.l2jmobius.gameserver.script.faenor;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.GameServer;
import com.l2jmobius.gameserver.script.Parser;
import com.l2jmobius.gameserver.script.ParserNotCreatedException;
import com.l2jmobius.gameserver.script.ScriptDocument;
import com.l2jmobius.gameserver.script.ScriptEngine;
import com.l2jmobius.gameserver.script.ScriptPackage;
import com.l2jmobius.gameserver.scripting.L2ScriptEngineManager;
import com.l2jmobius.gameserver.util.file.filter.XMLFilter;
/**
* @author Luis Arias
*/
public class FaenorScriptEngine extends ScriptEngine
{
static Logger _log = Logger.getLogger(GameServer.class.getName());
public final static String PACKAGE_DIRECTORY = "data/faenor/";
public final static boolean DEBUG = true;
private static final Logger _log = Logger.getLogger(FaenorScriptEngine.class.getName());
public static final String PACKAGE_DIRECTORY = "data/faenor/";
private static FaenorScriptEngine instance;
private LinkedList<ScriptDocument> scripts;
public static FaenorScriptEngine getInstance()
protected FaenorScriptEngine()
{
if (instance == null)
final File packDirectory = new File(Config.DATAPACK_ROOT, PACKAGE_DIRECTORY);
final File[] files = packDirectory.listFiles(new XMLFilter());
for (File file : files)
{
instance = new FaenorScriptEngine();
}
return instance;
}
private FaenorScriptEngine()
{
scripts = new LinkedList<>();
loadPackages();
parsePackages();
}
public void reloadPackages()
{
scripts = new LinkedList<>();
parsePackages();
}
private void loadPackages()
{
final File packDirectory = new File(Config.DATAPACK_ROOT, PACKAGE_DIRECTORY);// _log.sss(packDirectory.getAbsolutePath());
final FileFilter fileFilter = new FileFilter()
{
@Override
public boolean accept(File file)
try (InputStream in = new FileInputStream(file))
{
return file.getName().endsWith(".zip");
parseScript(new ScriptDocument(file.getName(), in), null);
}
};
final File[] files = packDirectory.listFiles(fileFilter);
if (files == null)
{
return;
}
ZipFile zipPack;
for (final File file : files)
{
try
catch (IOException e)
{
zipPack = new ZipFile(file);
_log.log(Level.WARNING, e.getMessage(), e);
}
catch (final ZipException e)
{
e.printStackTrace();
continue;
}
catch (final IOException e)
{
e.printStackTrace();
continue;
}
final ScriptPackage module = new ScriptPackage(zipPack);
final List<ScriptDocument> scrpts = module.getScriptFiles();
for (final ScriptDocument script : scrpts)
{
scripts.add(script);
}
try
{
zipPack.close();
}
catch (final IOException e)
{
}
}
}
public void orderScripts()
{
if (scripts.size() > 1)
{
for (int i = 0; i < scripts.size();)
{
if (scripts.get(i).getName().contains("NpcStatData"))
{
scripts.addFirst(scripts.remove(i));
}
else
{
i++;
}
}
}
}
public void parsePackages()
{
final L2ScriptEngineManager sem = L2ScriptEngineManager.getInstance();
final ScriptContext context = sem.getScriptContext("beanshell");
try
{
sem.eval("beanshell", "double log1p(double d) { return Math.log1p(d); }");
sem.eval("beanshell", "double pow(double d, double p) { return Math.pow(d,p); }");
for (final ScriptDocument script : scripts)
{
parseScript(script, context);
}
}
catch (final Exception e)
{
e.printStackTrace();
}
}
public void parseScript(ScriptDocument script, ScriptContext context)
{
if (DEBUG)
{
_log.fine("Parsing Script: " + script.getName());
}
final Node node = script.getDocument().getFirstChild();
final String parserClass = "faenor.Faenor" + node.getNodeName() + "Parser";
Node node = script.getDocument().getFirstChild();
String parserClass = "faenor.Faenor" + node.getNodeName() + "Parser";
Parser parser = null;
try
{
parser = createParser(parserClass);
}
catch (final ParserNotCreatedException e)
catch (ParserNotCreatedException e)
{
_log.warning("ERROR: No parser registered for Script: " + parserClass);
e.printStackTrace();
_log.log(Level.WARNING, "ERROR: No parser registered for Script: " + parserClass + ": " + e.getMessage(), e);
}
if (parser == null)
@ -199,29 +83,21 @@ public class FaenorScriptEngine extends ScriptEngine
try
{
parser.parseScript(node, context);
_log.fine(script.getName() + "Script Sucessfullty Parsed.");
_log.info(getClass().getSimpleName() + ": Loaded " + script.getName() + " successfully.");
}
catch (final Exception e)
catch (Exception e)
{
e.printStackTrace();
_log.warning("Script Parsing Failed.");
_log.log(Level.WARNING, "Script Parsing Failed: " + e.getMessage(), e);
}
}
@Override
public String toString()
public static FaenorScriptEngine getInstance()
{
if (scripts.isEmpty())
{
return "No Packages Loaded.";
}
String out = "Script Packages currently loaded:\n";
for (final ScriptDocument script : scripts)
{
out += script;
}
return out;
return SingletonHolder._instance;
}
}
private static class SingletonHolder
{
protected static final FaenorScriptEngine _instance = new FaenorScriptEngine();
}
}

View File

@ -1,143 +0,0 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.gameserver.script.faenor;
import java.util.Map;
import java.util.logging.Logger;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import com.l2jmobius.Config;
import com.l2jmobius.gameserver.script.IntList;
import com.l2jmobius.gameserver.script.Parser;
import com.l2jmobius.gameserver.script.ParserFactory;
import com.l2jmobius.gameserver.script.ScriptEngine;
import javolution.util.FastMap;
/**
* @author Luis Arias
*/
public class FaenorWorldDataParser extends FaenorParser
{
static Logger _log = Logger.getLogger(FaenorWorldDataParser.class.getName());
// Script Types
private final static String PET_DATA = "PetData";
@Override
public void parseScript(Node eventNode, ScriptContext context)
{
if (Config.DEBUG)
{
System.out.println("Parsing WorldData");
}
for (Node node = eventNode.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, PET_DATA))
{
parsePetData(node, context);
}
}
}
public class PetData
{
public int petID;
public int levelStart;
public int levelEnd;
Map<String, String> statValues;
public PetData()
{
statValues = new FastMap<>();
}
}
private void parsePetData(Node petNode, ScriptContext context)
{
final PetData petData = new PetData();
try
{
petData.petID = getInt(attribute(petNode, "ID"));
final int[] levelRange = IntList.parse(attribute(petNode, "Levels"));
petData.levelStart = levelRange[0];
petData.levelEnd = levelRange[1];
for (Node node = petNode.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "Stat"))
{
parseStat(node, petData);
}
}
bridge.addPetData(context, petData.petID, petData.levelStart, petData.levelEnd, petData.statValues);
}
catch (final Exception e)
{
petData.petID = -1;
_log.warning("Error in pet Data parser.");
e.printStackTrace();
}
}
private void parseStat(Node stat, PetData petData)
{
// if (Config.DEBUG) System.out.println("Parsing Pet Statistic.");
try
{
final String statName = attribute(stat, "Name");
for (Node node = stat.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "Formula"))
{
final String formula = parseForumla(node);
petData.statValues.put(statName, formula);
}
}
}
catch (final Exception e)
{
petData.petID = -1;
System.err.println("ERROR(parseStat):" + e.getMessage());
}
}
private String parseForumla(Node formulaNode)
{
return formulaNode.getTextContent().trim();
}
static class FaenorWorldDataParserFactory extends ParserFactory
{
@Override
public Parser create()
{
return (new FaenorWorldDataParser());
}
}
static
{
ScriptEngine.parserFactories.put(getParserName("WorldData"), new FaenorWorldDataParserFactory());
}
}