Project update.

This commit is contained in:
MobiusDev
2015-12-31 23:53:41 +00:00
parent e0d681a17e
commit ad2bcd79be
4084 changed files with 83696 additions and 86998 deletions

View File

@ -0,0 +1,85 @@
/*
* 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;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* @author Luis Arias
*/
public class DateRange
{
protected static final Logger _log = Logger.getLogger(DateRange.class.getName());
private final Date _startDate, _endDate;
public DateRange(Date from, Date to)
{
_startDate = from;
_endDate = to;
}
public static DateRange parse(String dateRange, DateFormat format)
{
final String[] date = dateRange.split("-");
if (date.length == 2)
{
try
{
final Date start = format.parse(date[0]);
final Date end = format.parse(date[1]);
return new DateRange(start, end);
}
catch (ParseException e)
{
_log.log(Level.WARNING, "Invalid Date Format.", e);
}
}
return new DateRange(null, null);
}
public boolean isValid()
{
return (_startDate != null) && (_endDate != null) && _startDate.before(_endDate);
}
public boolean isWithinRange(Date date)
{
return (date.equals(_startDate) || date.after(_startDate)) //
&& (date.equals(_endDate) || date.before(_endDate));
}
public Date getEndDate()
{
return _endDate;
}
public Date getStartDate()
{
return _startDate;
}
@Override
public String toString()
{
return "DateRange: From: " + getStartDate() + " To: " + getEndDate();
}
}

View File

@ -0,0 +1,27 @@
/*
* 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;
/**
* @author Luis Arias
*/
public interface EngineInterface
{
public void addEventDrop(int[] items, int[] count, double chance, DateRange range);
public void onPlayerLogin(String message, DateRange range);
}

View File

@ -0,0 +1,79 @@
/*
* 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;
/**
* @author Zoey76
*/
public class EventDrop
{
private final int[] _itemIdList;
private final long _minCount;
private final long _maxCount;
private final int _dropChance;
public EventDrop(int[] itemIdList, long min, long max, int dropChance)
{
_itemIdList = itemIdList;
_minCount = min;
_maxCount = max;
_dropChance = dropChance;
}
public EventDrop(int itemId, long minCount, long maxCount, int dropChance)
{
_itemIdList = new int[]
{
itemId
};
_minCount = minCount;
_maxCount = maxCount;
_dropChance = dropChance;
}
/**
* @return the _itemId
*/
public int[] getItemIdList()
{
return _itemIdList;
}
/**
* @return the _minCount
*/
public long getMinCount()
{
return _minCount;
}
/**
* @return the _maxCount
*/
public long getMaxCount()
{
return _maxCount;
}
/**
* @return the _dropChance
*/
public int getDropChance()
{
return _dropChance;
}
}

View File

@ -0,0 +1,77 @@
/*
* 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;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptContext;
public class Expression
{
protected static final Logger _log = Logger.getLogger(Expression.class.getName());
private final ScriptContext _context;
@SuppressWarnings("unused")
private final String _lang;
@SuppressWarnings("unused")
private final String _code;
public static Expression create(ScriptContext context, String lang, String code)
{
try
{
return new Expression(context, lang, code);
}
catch (Exception e)
{
_log.log(Level.WARNING, "", e);
return null;
}
}
private Expression(ScriptContext pContext, String pLang, String pCode)
{
_context = pContext;
_lang = pLang;
_code = pCode;
}
public <T> void addDynamicVariable(String name, T value)
{
try
{
_context.setAttribute(name, value, ScriptContext.ENGINE_SCOPE);
}
catch (Exception e)
{
_log.log(Level.WARNING, "", e);
}
}
public void removeDynamicVariable(String name)
{
try
{
_context.removeAttribute(name, ScriptContext.ENGINE_SCOPE);
}
catch (Exception e)
{
_log.log(Level.WARNING, "", e);
}
}
}

View File

@ -0,0 +1,68 @@
/*
* 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;
/**
* @author Luis Arias
*/
public class IntList
{
public static int[] parse(String range)
{
if (range.contains("-"))
{
return getIntegerRange(range.split("-"));
}
else if (range.contains(","))
{
return getIntegerList(range.split(","));
}
final int[] list =
{
getInt(range)
};
return list;
}
private static int getInt(String number)
{
return Integer.parseInt(number);
}
private static int[] getIntegerList(String[] numbers)
{
final int[] list = new int[numbers.length];
for (int i = 0; i < list.length; i++)
{
list[i] = getInt(numbers[i]);
}
return list;
}
private static int[] getIntegerRange(String[] numbers)
{
final int min = getInt(numbers[0]);
final int max = getInt(numbers[1]);
final int[] list = new int[(max - min) + 1];
for (int i = 0; i < list.length; i++)
{
list[i] = min + i;
}
return list;
}
}

View File

@ -0,0 +1,29 @@
/*
* 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;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
/**
* @author Luis Arias
*/
public abstract class Parser
{
public abstract void parseScript(Node node, ScriptContext context);
}

View File

@ -0,0 +1,23 @@
/*
* 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;
public abstract class ParserFactory
{
public abstract Parser create();
}

View File

@ -0,0 +1,25 @@
/*
* 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;
public class ParserNotCreatedException extends Exception
{
public ParserNotCreatedException()
{
super("Parser could not be created!");
}
}

View File

@ -0,0 +1,92 @@
/*
* 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;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
*
*/
public class ScriptDocument
{
private static final Logger _log = Logger.getLogger(ScriptDocument.class.getName());
private Document _document;
private final String _name;
public ScriptDocument(String name, InputStream input)
{
_name = name;
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
final DocumentBuilder builder = factory.newDocumentBuilder();
_document = builder.parse(input);
}
catch (SAXException sxe)
{
// Error generated during parsing)
Exception x = sxe;
if (sxe.getException() != null)
{
x = sxe.getException();
}
_log.warning(getClass().getSimpleName() + ": " + x.getMessage());
}
catch (ParserConfigurationException pce)
{
// Parser with specified options can't be built
_log.log(Level.WARNING, "", pce);
}
catch (IOException ioe)
{
// I/O error
_log.log(Level.WARNING, "", ioe);
}
}
public Document getDocument()
{
return _document;
}
/**
* @return Returns the _name.
*/
public String getName()
{
return _name;
}
@Override
public String toString()
{
return _name;
}
}

View File

@ -0,0 +1,59 @@
/*
* 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;
import java.util.Hashtable;
import com.l2jmobius.gameserver.script.faenor.FaenorInterface;
/**
* @author Luis Arias
*/
public class ScriptEngine
{
protected EngineInterface _utils = FaenorInterface.getInstance();
public static final Hashtable<String, ParserFactory> parserFactories = new Hashtable<>();
protected static Parser createParser(String name) throws ParserNotCreatedException
{
ParserFactory s = parserFactories.get(name);
if (s == null) // shape not found
{
try
{
Class.forName("com.l2jmobius.gameserver.script." + name);
// By now the static block with no function would
// have been executed if the shape was found.
// the shape is expected to have put its factory
// in the hashtable.
s = parserFactories.get(name);
if (s == null) // if the shape factory is not there even now
{
throw (new ParserNotCreatedException());
}
}
catch (ClassNotFoundException e)
{
// We'll throw an exception to indicate that
// the shape could not be created
throw (new ParserNotCreatedException());
}
}
return (s.create());
}
}

View File

@ -0,0 +1,130 @@
/*
* 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;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import com.l2jmobius.Config;
/**
* @author Luis Arias
*/
public class ScriptPackage
{
private static final Logger _log = Logger.getLogger(ScriptPackage.class.getName());
private final List<ScriptDocument> _scriptFiles = new ArrayList<>();
private final List<String> _otherFiles = new ArrayList<>();
private final String _name;
public ScriptPackage(ZipFile pack)
{
_name = pack.getName();
addFiles(pack);
}
/**
* @return Returns the otherFiles.
*/
public List<String> getOtherFiles()
{
return _otherFiles;
}
/**
* @return Returns the scriptFiles.
*/
public List<ScriptDocument> getScriptFiles()
{
return _scriptFiles;
}
/**
* @param pack
*/
private void addFiles(ZipFile pack)
{
for (Enumeration<? extends ZipEntry> e = pack.entries(); e.hasMoreElements();)
{
final ZipEntry entry = e.nextElement();
if (entry.getName().endsWith(".xml"))
{
try
{
_scriptFiles.add(new ScriptDocument(entry.getName(), pack.getInputStream(entry)));
}
catch (IOException io)
{
_log.warning(getClass().getSimpleName() + ": " + io.getMessage());
}
}
else if (!entry.isDirectory())
{
_otherFiles.add(entry.getName());
}
}
}
/**
* @return Returns the name.
*/
public String getName()
{
return _name;
}
@Override
public String toString()
{
if (getScriptFiles().isEmpty() && getOtherFiles().isEmpty())
{
return "Empty Package.";
}
final StringBuilder out = new StringBuilder();
out.append("Package Name: ");
out.append(getName());
out.append(Config.EOL);
if (!getScriptFiles().isEmpty())
{
out.append("Xml Script Files..." + Config.EOL);
for (ScriptDocument script : getScriptFiles())
{
out.append(script.getName());
out.append(Config.EOL);
}
}
if (!getOtherFiles().isEmpty())
{
out.append("Other Files..." + Config.EOL);
for (String fileName : getOtherFiles())
{
out.append(fileName);
out.append(Config.EOL);
}
}
return out.toString();
}
}

View File

@ -0,0 +1,56 @@
/*
* 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;
/**
* @author -Nemesiss-
*/
public class ShortList
{
public static short[] parse(String range)
{
if (range.contains("-"))
{
return getShortList(range.split("-"));
}
else if (range.contains(","))
{
return getShortList(range.split(","));
}
final short[] list =
{
getShort(range)
};
return list;
}
private static short getShort(String number)
{
return Short.parseShort(number);
}
private static short[] getShortList(String[] numbers)
{
final short[] list = new short[numbers.length];
for (int i = 0; i < list.length; i++)
{
list[i] = getShort(numbers[i]);
}
return list;
}
}

View File

@ -0,0 +1,138 @@
/*
* 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.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import com.l2jmobius.gameserver.ThreadPoolManager;
import com.l2jmobius.gameserver.script.DateRange;
import com.l2jmobius.gameserver.script.IntList;
import com.l2jmobius.gameserver.script.Parser;
import com.l2jmobius.gameserver.script.ParserFactory;
import com.l2jmobius.gameserver.script.ScriptEngine;
/**
* @author Luis Arias
*/
public class FaenorEventParser extends FaenorParser
{
static Logger _log = Logger.getLogger(FaenorEventParser.class.getName());
private DateRange _eventDates = null;
@Override
public void parseScript(final Node eventNode, ScriptContext context)
{
final String ID = attribute(eventNode, "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.");
return;
}
if (_eventDates.getStartDate().after(currentDate))
{
_log.info("Event ID: (" + ID + ") is not active yet... Ignored.");
ThreadPoolManager.getInstance().scheduleGeneral(() -> parseEventDropAndMessage(eventNode), _eventDates.getStartDate().getTime() - currentDate.getTime());
return;
}
parseEventDropAndMessage(eventNode);
}
protected void parseEventDropAndMessage(Node eventNode)
{
for (Node node = eventNode.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "DropList"))
{
parseEventDropList(node);
}
else if (isNodeName(node, "Message"))
{
parseEventMessage(node);
}
}
}
private void parseEventMessage(Node sysMsg)
{
try
{
final String type = attribute(sysMsg, "Type");
final String message = attribute(sysMsg, "Msg");
if (type.equalsIgnoreCase("OnJoin"))
{
_bridge.onPlayerLogin(message, _eventDates);
}
}
catch (Exception e)
{
_log.log(Level.WARNING, "Error in event parser: " + e.getMessage(), e);
}
}
private void parseEventDropList(Node dropList)
{
for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling())
{
if (isNodeName(node, "AllDrop"))
{
parseEventDrop(node);
}
}
}
private void parseEventDrop(Node 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"));
_bridge.addEventDrop(items, count, chance, _eventDates);
}
catch (Exception e)
{
_log.log(Level.WARNING, "ERROR(parseEventDrop):" + e.getMessage(), e);
}
}
static class FaenorEventParserFactory extends ParserFactory
{
@Override
public Parser create()
{
return (new FaenorEventParser());
}
}
static
{
ScriptEngine.parserFactories.put(getParserName("Event"), new FaenorEventParserFactory());
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.List;
import java.util.logging.Logger;
import com.l2jmobius.gameserver.data.sql.impl.AnnouncementsTable;
import com.l2jmobius.gameserver.datatables.EventDroplist;
import com.l2jmobius.gameserver.model.announce.EventAnnouncement;
import com.l2jmobius.gameserver.script.DateRange;
import com.l2jmobius.gameserver.script.EngineInterface;
/**
* @author Luis Arias
*/
public class FaenorInterface implements EngineInterface
{
protected static final Logger _log = Logger.getLogger(FaenorInterface.class.getName());
public static FaenorInterface getInstance()
{
return SingletonHolder._instance;
}
public List<?> getAllPlayers()
{
return null;
}
@Override
public void addEventDrop(int[] items, int[] count, double chance, DateRange range)
{
EventDroplist.getInstance().addGlobalDrop(items, count, (int) (chance * 1000000), range);
}
@Override
public void onPlayerLogin(String message, DateRange validDateRange)
{
AnnouncementsTable.getInstance().addAnnouncement(new EventAnnouncement(validDateRange, message));
}
private static class SingletonHolder
{
protected static final FaenorInterface _instance = new FaenorInterface();
}
}

View File

@ -0,0 +1,134 @@
/*
* 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.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.script.ScriptContext;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.l2jmobius.gameserver.script.Parser;
/**
* @author Luis Arias
*/
public abstract class FaenorParser extends Parser
{
protected static FaenorInterface _bridge = FaenorInterface.getInstance();
protected final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US);
/*
* UTILITY FUNCTIONS
*/
public static String attribute(Node node, String attributeName)
{
return attribute(node, attributeName, null);
}
public static String element(Node node, String elementName)
{
return element(node, elementName, null);
}
public static String attribute(Node node, String attributeName, String defaultValue)
{
try
{
return node.getAttributes().getNamedItem(attributeName).getNodeValue();
}
catch (Exception e)
{
if (defaultValue != null)
{
return defaultValue;
}
throw new NullPointerException(e.getMessage());
}
}
public static String element(Node parentNode, String elementName, String defaultValue)
{
try
{
final NodeList list = parentNode.getChildNodes();
for (int i = 0; i < list.getLength(); i++)
{
final Node node = list.item(i);
if (node.getNodeName().equalsIgnoreCase(elementName))
{
return node.getTextContent();
}
}
}
catch (Exception e)
{
}
if (defaultValue != null)
{
return defaultValue;
}
throw new NullPointerException();
}
public static boolean isNodeName(Node node, String name)
{
return node.getNodeName().equalsIgnoreCase(name);
}
public Date getDate(String date) throws ParseException
{
return DATE_FORMAT.parse(date);
}
public static double getPercent(String percent)
{
return (Double.parseDouble(percent.split("%")[0]) / 100.0);
}
protected static int getInt(String number)
{
return Integer.parseInt(number);
}
protected static double getDouble(String number)
{
return Double.parseDouble(number);
}
protected static float getFloat(String number)
{
return Float.parseFloat(number);
}
protected static String getParserName(String name)
{
return "faenor.Faenor" + name + "Parser";
}
/**
* @param node
* @param context
*/
@Override
public abstract void parseScript(Node node, ScriptContext context);
}

View File

@ -0,0 +1,106 @@
/*
* 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.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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.script.Parser;
import com.l2jmobius.gameserver.script.ParserNotCreatedException;
import com.l2jmobius.gameserver.script.ScriptDocument;
import com.l2jmobius.gameserver.script.ScriptEngine;
import com.l2jmobius.util.file.filter.XMLFilter;
/**
* @author Luis Arias
*/
public class FaenorScriptEngine extends ScriptEngine
{
private static final Logger _log = Logger.getLogger(FaenorScriptEngine.class.getName());
public static final String PACKAGE_DIRECTORY = "faenor/";
protected FaenorScriptEngine()
{
final File packDirectory = new File(Config.DATAPACK_ROOT, PACKAGE_DIRECTORY);
final File[] files = packDirectory.listFiles(new XMLFilter());
if (files != null)
{
for (File file : files)
{
try (InputStream in = new FileInputStream(file))
{
parseScript(new ScriptDocument(file.getName(), in), null);
}
catch (IOException e)
{
_log.log(Level.WARNING, e.getMessage(), e);
}
}
}
}
public void parseScript(ScriptDocument script, ScriptContext context)
{
final Node node = script.getDocument().getFirstChild();
final String parserClass = "faenor.Faenor" + node.getNodeName() + "Parser";
Parser parser = null;
try
{
parser = createParser(parserClass);
}
catch (ParserNotCreatedException e)
{
_log.log(Level.WARNING, "ERROR: No parser registered for Script: " + parserClass + ": " + e.getMessage(), e);
}
if (parser == null)
{
_log.warning("Unknown Script Type: " + script.getName());
return;
}
try
{
parser.parseScript(node, context);
_log.info(getClass().getSimpleName() + ": Loaded " + script.getName() + " successfully.");
}
catch (Exception e)
{
_log.log(Level.WARNING, "Script Parsing Failed: " + e.getMessage(), e);
}
}
public static FaenorScriptEngine getInstance()
{
return SingletonHolder._instance;
}
private static class SingletonHolder
{
protected static final FaenorScriptEngine _instance = new FaenorScriptEngine();
}
}