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

@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="lib" path="dist/libs/bsh-2.0b5.jar"/>
<classpathentry kind="lib" path="dist/libs/bsh-engine.jar"/>
<classpathentry kind="lib" path="dist/libs/c3p0-0.9.2-pre1.jar"/> <classpathentry kind="lib" path="dist/libs/c3p0-0.9.2-pre1.jar"/>
<classpathentry kind="lib" path="dist/libs/ecj-4.4.jar"/> <classpathentry kind="lib" path="dist/libs/ecj-4.4.jar"/>
<classpathentry kind="lib" path="dist/libs/java-engine-1.8.jar"/> <classpathentry kind="lib" path="dist/libs/java-engine-1.8.jar"/>

View File

@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<Event ID="Valentines Event" Active="14 Feb 2005-16 Feb 2005" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../xsd/faenor.xsd">
<Droplist>
<AllDrop Items="4209-4217" Count="1,1" Chance="5%" />
</Droplist>
<EventEnd>
<Take From="AllPlayers">
<Item ItemID="4209-4217" />
</Take>
</EventEnd>
<Message Type="OnJoin" Msg="Valentine's Event:\nCollect all 9 Hearts Blocks to recieve a reward!!\n(Quest Item.. All monster's have a chance to drop.)" />
<Npc ID="12371">
<Condition>
<Inventory Items="4209-4217" Count="1" Operation=">=" />
</Condition>
<Give>
<Item ItemID="4625" Count="1" />
<Item ItemID="1540" Count="10" />
<Item ItemID="728" Count="10" />
</Give>
<SystemMessage Msg="Congratulations. Receive your prize." />
<Take>
<Item Items="4209-4217" Count="1" Type="OfEach" />
</Take>
</Npc>
</Event>

View File

@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Event">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Droplist" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="AllDrop" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:attribute name="Items" type="xs:token" use="required" />
<xs:attribute name="Count" type="xs:token" use="required" />
<xs:attribute name="Chance" type="xs:token" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="EventEnd" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Take" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Item" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:attribute name="ItemID" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="From" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Message" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:attribute name="Msg" type="xs:normalizedString" use="required" />
<xs:attribute name="Type" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Npc" maxOccurs="1" minOccurs="0">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Condition" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Inventory" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:attribute name="Items" type="xs:token" use="required" />
<xs:attribute name="Count" type="xs:positiveInteger" use="required" />
<xs:attribute name="Operation" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Give" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Item" maxOccurs="unbounded" minOccurs="1">
<xs:complexType>
<xs:attribute name="ItemID" type="xs:positiveInteger" use="required" />
<xs:attribute name="Count" type="xs:positiveInteger" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SystemMessage" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:attribute name="Msg" type="xs:normalizedString" use="required" />
</xs:complexType>
</xs:element>
<xs:element name="Take" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element name="Item" maxOccurs="1" minOccurs="1">
<xs:complexType>
<xs:attribute name="Items" type="xs:token" use="required" />
<xs:attribute name="Count" type="xs:positiveInteger" use="required" />
<xs:attribute name="Type" type="xs:token" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:positiveInteger" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="Active" type="xs:normalizedString" use="required" />
<xs:attribute name="ID" type="xs:normalizedString" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>

Binary file not shown.

Binary file not shown.

View File

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

View File

@@ -17,50 +17,31 @@
package com.l2jmobius.gameserver.script.faenor; package com.l2jmobius.gameserver.script.faenor;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.logging.Logger;
import javax.script.ScriptContext;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.gameserver.Announcements; import com.l2jmobius.gameserver.Announcements;
import com.l2jmobius.gameserver.EventDroplist; import com.l2jmobius.gameserver.EventDroplist;
import com.l2jmobius.gameserver.model.L2DropCategory; import com.l2jmobius.gameserver.model.L2DropCategory;
import com.l2jmobius.gameserver.model.L2DropData; import com.l2jmobius.gameserver.model.L2DropData;
import com.l2jmobius.gameserver.model.L2PetData;
import com.l2jmobius.gameserver.script.DateRange; import com.l2jmobius.gameserver.script.DateRange;
import com.l2jmobius.gameserver.script.EngineInterface; import com.l2jmobius.gameserver.script.EngineInterface;
import com.l2jmobius.gameserver.script.Expression;
import com.l2jmobius.gameserver.templates.L2NpcTemplate; 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 public class FaenorInterface implements EngineInterface
{ {
private static FaenorInterface _instance; protected static final Logger _log = Logger.getLogger(FaenorInterface.class.getName());
public static FaenorInterface getInstance() public static FaenorInterface getInstance()
{ {
if (_instance == null) return SingletonHolder._instance;
{
_instance = new FaenorInterface();
}
return _instance;
} }
public FaenorInterface()
{
}
/*
* (non-Javadoc)
* @see com.l2jmobius.gameserver.script.EngineInterface#getAllPlayers()
*/
public List<?> getAllPlayers() public List<?> getAllPlayers()
{ {
// TODO Auto-generated method stub
return null; return null;
} }
@@ -144,7 +125,6 @@ public class FaenorInterface implements EngineInterface
maxCategory++; maxCategory++;
npc.addDropData(drop, maxCategory); npc.addDropData(drop, maxCategory);
} }
} }
/** /**
@@ -158,39 +138,10 @@ public class FaenorInterface implements EngineInterface
npc.addDropData(drop, category); 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 @Override
public void addEventDrop(int[] items, int[] count, double chance, DateRange range) 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 @Override
@@ -199,23 +150,8 @@ public class FaenorInterface implements EngineInterface
Announcements.getInstance().addEventAnnouncement(validDateRange, message); 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]; protected static final FaenorInterface _instance = new FaenorInterface();
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);
}
} }
} }

View File

@@ -34,10 +34,8 @@ import com.l2jmobius.gameserver.script.Parser;
*/ */
public abstract class FaenorParser extends Parser public abstract class FaenorParser extends Parser
{ {
protected static FaenorInterface bridge = FaenorInterface.getInstance(); protected static FaenorInterface _bridge = FaenorInterface.getInstance();
protected static DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US); protected final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US);
public final static boolean DEBUG = true;
/* /*
* UTILITY FUNCTIONS * UTILITY FUNCTIONS
@@ -58,7 +56,7 @@ public abstract class FaenorParser extends Parser
{ {
return node.getAttributes().getNamedItem(attributeName).getNodeValue(); return node.getAttributes().getNamedItem(attributeName).getNodeValue();
} }
catch (final Exception e) catch (Exception e)
{ {
if (defaultValue != null) if (defaultValue != null)
{ {
@@ -72,17 +70,17 @@ public abstract class FaenorParser extends Parser
{ {
try try
{ {
final NodeList list = parentNode.getChildNodes(); NodeList list = parentNode.getChildNodes();
for (int i = 0; i < list.getLength(); i++) for (int i = 0; i < list.getLength(); i++)
{ {
final Node node = list.item(i); Node node = list.item(i);
if (node.getNodeName().equalsIgnoreCase(elementName)) if (node.getNodeName().equalsIgnoreCase(elementName))
{ {
return node.getTextContent(); return node.getTextContent();
} }
} }
} }
catch (final Exception e) catch (Exception e)
{ {
} }
if (defaultValue != null) if (defaultValue != null)
@@ -90,7 +88,6 @@ public abstract class FaenorParser extends Parser
return defaultValue; return defaultValue;
} }
throw new NullPointerException(); throw new NullPointerException();
} }
public static boolean isNodeName(Node node, String name) public static boolean isNodeName(Node node, String name)
@@ -98,7 +95,7 @@ public abstract class FaenorParser extends Parser
return node.getNodeName().equalsIgnoreCase(name); 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); return DATE_FORMAT.parse(date);
} }
@@ -128,6 +125,10 @@ public abstract class FaenorParser extends Parser
return "faenor.Faenor" + name + "Parser"; return "faenor.Faenor" + name + "Parser";
} }
/**
* @param node
* @param context
*/
@Override @Override
public abstract void parseScript(Node node, ScriptContext context); 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; package com.l2jmobius.gameserver.script.faenor;
import java.io.File; import java.io.File;
import java.io.FileFilter; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.io.InputStream;
import java.util.List; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.script.ScriptContext; import javax.script.ScriptContext;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import com.l2jmobius.Config; import com.l2jmobius.Config;
import com.l2jmobius.gameserver.GameServer;
import com.l2jmobius.gameserver.script.Parser; import com.l2jmobius.gameserver.script.Parser;
import com.l2jmobius.gameserver.script.ParserNotCreatedException; import com.l2jmobius.gameserver.script.ParserNotCreatedException;
import com.l2jmobius.gameserver.script.ScriptDocument; import com.l2jmobius.gameserver.script.ScriptDocument;
import com.l2jmobius.gameserver.script.ScriptEngine; import com.l2jmobius.gameserver.script.ScriptEngine;
import com.l2jmobius.gameserver.script.ScriptPackage; import com.l2jmobius.gameserver.util.file.filter.XMLFilter;
import com.l2jmobius.gameserver.scripting.L2ScriptEngineManager;
/** /**
* @author Luis Arias * @author Luis Arias
*/ */
public class FaenorScriptEngine extends ScriptEngine public class FaenorScriptEngine extends ScriptEngine
{ {
static Logger _log = Logger.getLogger(GameServer.class.getName()); private static final Logger _log = Logger.getLogger(FaenorScriptEngine.class.getName());
public final static String PACKAGE_DIRECTORY = "data/faenor/"; public static final String PACKAGE_DIRECTORY = "data/faenor/";
public final static boolean DEBUG = true;
private static FaenorScriptEngine instance; protected FaenorScriptEngine()
private LinkedList<ScriptDocument> scripts;
public static FaenorScriptEngine getInstance()
{ {
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(); try (InputStream in = new FileInputStream(file))
}
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)
{ {
return file.getName().endsWith(".zip"); parseScript(new ScriptDocument(file.getName(), in), null);
} }
}; catch (IOException e)
final File[] files = packDirectory.listFiles(fileFilter);
if (files == null)
{
return;
}
ZipFile zipPack;
for (final File file : files)
{
try
{ {
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) public void parseScript(ScriptDocument script, ScriptContext context)
{ {
if (DEBUG) Node node = script.getDocument().getFirstChild();
{ String parserClass = "faenor.Faenor" + node.getNodeName() + "Parser";
_log.fine("Parsing Script: " + script.getName());
}
final Node node = script.getDocument().getFirstChild();
final String parserClass = "faenor.Faenor" + node.getNodeName() + "Parser";
Parser parser = null; Parser parser = null;
try try
{ {
parser = createParser(parserClass); parser = createParser(parserClass);
} }
catch (final ParserNotCreatedException e) catch (ParserNotCreatedException e)
{ {
_log.warning("ERROR: No parser registered for Script: " + parserClass); _log.log(Level.WARNING, "ERROR: No parser registered for Script: " + parserClass + ": " + e.getMessage(), e);
e.printStackTrace();
} }
if (parser == null) if (parser == null)
@@ -199,29 +83,21 @@ public class FaenorScriptEngine extends ScriptEngine
try try
{ {
parser.parseScript(node, context); 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.log(Level.WARNING, "Script Parsing Failed: " + e.getMessage(), e);
_log.warning("Script Parsing Failed.");
} }
} }
@Override public static FaenorScriptEngine getInstance()
public String toString()
{ {
if (scripts.isEmpty()) return SingletonHolder._instance;
{
return "No Packages Loaded.";
}
String out = "Script Packages currently loaded:\n";
for (final ScriptDocument script : scripts)
{
out += script;
}
return out;
} }
}
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());
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.util.file.filter;
import java.io.File;
import java.io.FileFilter;
/**
* Specialized {@link FileFilter} class.<br>
* Accepts files ending with ".xml" only.
* @author mrTJO
*/
public class XMLFilter implements FileFilter
{
@Override
public boolean accept(File f)
{
if ((f == null) || !f.isFile())
{
return false;
}
return f.getName().toLowerCase().endsWith(".xml");
}
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long