actual freya branch
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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.l2jserver.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)
|
||||
{
|
||||
String[] date = dateRange.split("-");
|
||||
if (date.length == 2)
|
||||
{
|
||||
try
|
||||
{
|
||||
Date start = format.parse(date[0]);
|
||||
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);
|
||||
}
|
||||
|
||||
public boolean isWithinRange(Date date)
|
||||
{
|
||||
return date.after(_startDate) && date.before(_endDate);
|
||||
}
|
||||
|
||||
public Date getEndDate()
|
||||
{
|
||||
return _endDate;
|
||||
}
|
||||
|
||||
public Date getStartDate()
|
||||
{
|
||||
return _startDate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
import com.l2jserver.gameserver.Announcements;
|
||||
import com.l2jserver.gameserver.GameTimeController;
|
||||
import com.l2jserver.gameserver.RecipeController;
|
||||
import com.l2jserver.gameserver.datatables.CharNameTable;
|
||||
import com.l2jserver.gameserver.datatables.CharTemplateTable;
|
||||
import com.l2jserver.gameserver.datatables.ClanTable;
|
||||
import com.l2jserver.gameserver.datatables.ItemTable;
|
||||
import com.l2jserver.gameserver.datatables.LevelUpData;
|
||||
import com.l2jserver.gameserver.datatables.MapRegionTable;
|
||||
import com.l2jserver.gameserver.datatables.NpcTable;
|
||||
import com.l2jserver.gameserver.datatables.SkillTable;
|
||||
import com.l2jserver.gameserver.datatables.SkillTreesData;
|
||||
import com.l2jserver.gameserver.datatables.SpawnTable;
|
||||
import com.l2jserver.gameserver.datatables.TeleportLocationTable;
|
||||
import com.l2jserver.gameserver.idfactory.IdFactory;
|
||||
import com.l2jserver.gameserver.model.L2World;
|
||||
|
||||
/**
|
||||
* @author Luis Arias
|
||||
*/
|
||||
public interface EngineInterface
|
||||
{
|
||||
// * keep the references of Singletons to prevent garbage collection
|
||||
public CharNameTable charNametable = CharNameTable.getInstance();
|
||||
|
||||
public IdFactory idFactory = IdFactory.getInstance();
|
||||
public ItemTable itemTable = ItemTable.getInstance();
|
||||
|
||||
public SkillTable skillTable = SkillTable.getInstance();
|
||||
|
||||
public RecipeController recipeController = RecipeController.getInstance();
|
||||
|
||||
public SkillTreesData skillTreeTable = SkillTreesData.getInstance();
|
||||
public CharTemplateTable charTemplates = CharTemplateTable.getInstance();
|
||||
public ClanTable clanTable = ClanTable.getInstance();
|
||||
|
||||
public NpcTable npcTable = NpcTable.getInstance();
|
||||
|
||||
public TeleportLocationTable teleTable = TeleportLocationTable.getInstance();
|
||||
public LevelUpData levelUpData = LevelUpData.getInstance();
|
||||
public L2World world = L2World.getInstance();
|
||||
public SpawnTable spawnTable = SpawnTable.getInstance();
|
||||
public GameTimeController gameTimeController = GameTimeController.getInstance();
|
||||
public Announcements announcements = Announcements.getInstance();
|
||||
public MapRegionTable mapRegions = MapRegionTable.getInstance();
|
||||
|
||||
// public ArrayList getAllPlayers();
|
||||
// public Player getPlayer(String characterName);
|
||||
public void addQuestDrop(int npcID, int itemID, int min, int max, int chance, String questID, String[] states);
|
||||
|
||||
public void addEventDrop(int[] items, int[] count, double chance, DateRange range);
|
||||
|
||||
public void onPlayerLogin(String[] message, DateRange range);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
|
||||
import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
|
||||
|
||||
public class Expression
|
||||
{
|
||||
protected static final Logger _log = Logger.getLogger(Expression.class.getName());
|
||||
private final ScriptContext _context;
|
||||
|
||||
public static Object eval(String lang, String code)
|
||||
{
|
||||
try
|
||||
{
|
||||
return L2ScriptEngineManager.getInstance().eval(lang, code);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object eval(ScriptContext context, String lang, String code)
|
||||
{
|
||||
try
|
||||
{
|
||||
return L2ScriptEngineManager.getInstance().eval(lang, code, context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.l2jserver.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(","));
|
||||
}
|
||||
|
||||
int[] list =
|
||||
{
|
||||
getInt(range)
|
||||
};
|
||||
return list;
|
||||
}
|
||||
|
||||
private static int getInt(String number)
|
||||
{
|
||||
return Integer.parseInt(number);
|
||||
}
|
||||
|
||||
private static int[] getIntegerList(String[] numbers)
|
||||
{
|
||||
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)
|
||||
{
|
||||
int min = getInt(numbers[0]);
|
||||
int max = getInt(numbers[1]);
|
||||
int[] list = new int[(max - min) + 1];
|
||||
for (int i = 0; i < list.length; i++)
|
||||
{
|
||||
list[i] = min + i;
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.l2jserver.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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
public abstract class ParserFactory
|
||||
{
|
||||
public abstract Parser create();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
public class ParserNotCreatedException extends Exception
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public ParserNotCreatedException()
|
||||
{
|
||||
super("Parser could not be created!");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.l2jserver.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
|
||||
{
|
||||
protected static final Logger _log = Logger.getLogger(ScriptDocument.class.getName());
|
||||
private Document _document;
|
||||
private final String _name;
|
||||
|
||||
public ScriptDocument(String name, InputStream input)
|
||||
{
|
||||
_name = name;
|
||||
|
||||
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||
try
|
||||
{
|
||||
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();
|
||||
}
|
||||
x.printStackTrace();
|
||||
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
import java.util.Hashtable;
|
||||
|
||||
import com.l2jserver.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.l2jserver.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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
import javolution.util.FastList;
|
||||
|
||||
/**
|
||||
* @author Luis Arias
|
||||
*/
|
||||
public class ScriptPackage
|
||||
{
|
||||
private final List<ScriptDocument> _scriptFiles;
|
||||
private final List<String> _otherFiles;
|
||||
private final String _name;
|
||||
|
||||
public ScriptPackage(ZipFile pack)
|
||||
{
|
||||
_scriptFiles = new FastList<>();
|
||||
_otherFiles = new FastList<>();
|
||||
_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 scriptFiles The scriptFiles to set.
|
||||
*/
|
||||
private void addFiles(ZipFile pack)
|
||||
{
|
||||
for (Enumeration<? extends ZipEntry> e = pack.entries(); e.hasMoreElements();)
|
||||
{
|
||||
ZipEntry entry = e.nextElement();
|
||||
if (entry.getName().endsWith(".xml"))
|
||||
{
|
||||
try
|
||||
{
|
||||
ScriptDocument newScript = new ScriptDocument(entry.getName(), pack.getInputStream(entry));
|
||||
_scriptFiles.add(newScript);
|
||||
}
|
||||
catch (IOException e1)
|
||||
{
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
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.";
|
||||
}
|
||||
|
||||
String out = "Package Name: " + getName() + "\n";
|
||||
|
||||
if (!getScriptFiles().isEmpty())
|
||||
{
|
||||
out += "Xml Script Files...\n";
|
||||
for (ScriptDocument script : getScriptFiles())
|
||||
{
|
||||
out += script.getName() + "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (!getOtherFiles().isEmpty())
|
||||
{
|
||||
out += "Other Files...\n";
|
||||
for (String fileName : getOtherFiles())
|
||||
{
|
||||
out += fileName + "\n";
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.l2jserver.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(","));
|
||||
}
|
||||
|
||||
short[] list =
|
||||
{
|
||||
getShort(range)
|
||||
};
|
||||
return list;
|
||||
}
|
||||
|
||||
private static short getShort(String number)
|
||||
{
|
||||
return Short.parseShort(number);
|
||||
}
|
||||
|
||||
private static short[] getShortList(String[] numbers)
|
||||
{
|
||||
short[] list = new short[numbers.length];
|
||||
for (int i = 0; i < list.length; i++)
|
||||
{
|
||||
list[i] = getShort(numbers[i]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
/*
|
||||
* 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.l2jserver.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.l2jserver.gameserver.ThreadPoolManager;
|
||||
import com.l2jserver.gameserver.script.DateRange;
|
||||
import com.l2jserver.gameserver.script.IntList;
|
||||
import com.l2jserver.gameserver.script.Parser;
|
||||
import com.l2jserver.gameserver.script.ParserFactory;
|
||||
import com.l2jserver.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)
|
||||
{
|
||||
String ID = attribute(eventNode, "ID");
|
||||
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.fine("Parsing Event \"" + ID + "\"");
|
||||
}
|
||||
|
||||
_eventDates = DateRange.parse(attribute(eventNode, "Active"), DATE_FORMAT);
|
||||
|
||||
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)
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.fine("Parsing Event Message.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
String type = attribute(sysMsg, "Type");
|
||||
String[] message = attribute(sysMsg, "Msg").split("\n");
|
||||
|
||||
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)
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.fine("Parsing Droplist.");
|
||||
}
|
||||
|
||||
for (Node node = dropList.getFirstChild(); node != null; node = node.getNextSibling())
|
||||
{
|
||||
if (isNodeName(node, "AllDrop"))
|
||||
{
|
||||
parseEventDrop(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void parseEventDrop(Node drop)
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.fine("Parsing Drop.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
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);
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script.faenor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javolution.util.FastList;
|
||||
|
||||
import com.l2jserver.Config;
|
||||
import com.l2jserver.gameserver.Announcements;
|
||||
import com.l2jserver.gameserver.datatables.EventDroplist;
|
||||
import com.l2jserver.gameserver.model.L2DropCategory;
|
||||
import com.l2jserver.gameserver.model.L2DropData;
|
||||
import com.l2jserver.gameserver.script.DateRange;
|
||||
import com.l2jserver.gameserver.script.EngineInterface;
|
||||
import com.l2jserver.gameserver.templates.chars.L2NpcTemplate;
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
private FaenorInterface()
|
||||
{
|
||||
}
|
||||
|
||||
public List<?> getAllPlayers()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new Quest Drop to an NPC
|
||||
* @see com.l2jserver.gameserver.script.EngineInterface#addQuestDrop(int)
|
||||
*/
|
||||
@Override
|
||||
public void addQuestDrop(int npcID, int itemID, int min, int max, int chance, String questID, String[] states)
|
||||
{
|
||||
L2NpcTemplate npc = npcTable.getTemplate(npcID);
|
||||
if (npc == null)
|
||||
{
|
||||
throw new NullPointerException();
|
||||
}
|
||||
L2DropData drop = new L2DropData();
|
||||
drop.setItemId(itemID);
|
||||
drop.setMinDrop(min);
|
||||
drop.setMaxDrop(max);
|
||||
drop.setChance(chance);
|
||||
drop.setQuestID(questID);
|
||||
drop.addStates(states);
|
||||
addDrop(npc, drop, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new Drop to an NPC
|
||||
* @see com.l2jserver.gameserver.script.EngineInterface#addQuestDrop(int)
|
||||
*/
|
||||
public void addDrop(int npcID, int itemID, int min, int max, boolean sweep, int chance) throws NullPointerException
|
||||
{
|
||||
L2NpcTemplate npc = npcTable.getTemplate(npcID);
|
||||
if (npc == null)
|
||||
{
|
||||
if (Config.DEBUG)
|
||||
{
|
||||
_log.warning("Npc doesnt Exist");
|
||||
}
|
||||
throw new NullPointerException();
|
||||
}
|
||||
L2DropData drop = new L2DropData();
|
||||
drop.setItemId(itemID);
|
||||
drop.setMinDrop(min);
|
||||
drop.setMaxDrop(max);
|
||||
drop.setChance(chance);
|
||||
|
||||
addDrop(npc, drop, sweep);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new drop to an NPC. If the drop is sweep, it adds it to the NPC's Sweep category If the drop is non-sweep, it creates a new category for this drop.
|
||||
* @param npc
|
||||
* @param drop
|
||||
* @param sweep
|
||||
*/
|
||||
public void addDrop(L2NpcTemplate npc, L2DropData drop, boolean sweep)
|
||||
{
|
||||
if (sweep)
|
||||
{
|
||||
addDrop(npc, drop, -1);
|
||||
}
|
||||
else
|
||||
{
|
||||
int maxCategory = -1;
|
||||
|
||||
if (npc.getDropData() != null)
|
||||
{
|
||||
for (L2DropCategory cat : npc.getDropData())
|
||||
{
|
||||
if (maxCategory < cat.getCategoryType())
|
||||
{
|
||||
maxCategory = cat.getCategoryType();
|
||||
}
|
||||
}
|
||||
}
|
||||
maxCategory++;
|
||||
npc.addDropData(drop, maxCategory);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new drop to an NPC, in the specified category. If the category does not exist, it is created.
|
||||
* @param npc
|
||||
* @param drop
|
||||
* @param sweep
|
||||
*/
|
||||
public void addDrop(L2NpcTemplate npc, L2DropData drop, int category)
|
||||
{
|
||||
npc.addDropData(drop, category);
|
||||
}
|
||||
|
||||
public List<L2DropData> getQuestDrops(int npcID)
|
||||
{
|
||||
L2NpcTemplate npc = npcTable.getTemplate(npcID);
|
||||
if (npc == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
List<L2DropData> questDrops = new FastList<>();
|
||||
if (npc.getDropData() != null)
|
||||
{
|
||||
for (L2DropCategory cat : npc.getDropData())
|
||||
{
|
||||
for (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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerLogin(String[] message, DateRange validDateRange)
|
||||
{
|
||||
Announcements.getInstance().addEventAnnouncement(validDateRange, message);
|
||||
}
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final FaenorInterface _instance = new FaenorInterface();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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.l2jserver.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.l2jserver.gameserver.script.Parser;
|
||||
|
||||
/**
|
||||
* @author Luis Arias
|
||||
*/
|
||||
public abstract class FaenorParser extends Parser
|
||||
{
|
||||
protected static FaenorInterface _bridge = FaenorInterface.getInstance();
|
||||
protected static final DateFormat DATE_FORMAT = new SimpleDateFormat("dd MMM yyyy", Locale.US);
|
||||
|
||||
public final static boolean DEBUG = true;
|
||||
|
||||
/*
|
||||
* 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
|
||||
{
|
||||
NodeList list = parentNode.getChildNodes();
|
||||
for (int i = 0; i < list.getLength(); i++)
|
||||
{
|
||||
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 static 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 script
|
||||
*/
|
||||
@Override
|
||||
public abstract void parseScript(Node node, ScriptContext context);
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script.faenor;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.script.ScriptContext;
|
||||
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import com.l2jserver.gameserver.script.Parser;
|
||||
import com.l2jserver.gameserver.script.ParserFactory;
|
||||
import com.l2jserver.gameserver.script.ScriptEngine;
|
||||
|
||||
/**
|
||||
* @author Luis Arias
|
||||
*/
|
||||
public class FaenorQuestParser extends FaenorParser
|
||||
{
|
||||
protected static final Logger _log = Logger.getLogger(FaenorQuestParser.class.getName());
|
||||
|
||||
@Override
|
||||
public void parseScript(Node questNode, ScriptContext context)
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.info("Parsing Quest.");
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
_log.info("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)
|
||||
{
|
||||
_log.info("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 (NullPointerException e)
|
||||
{
|
||||
throw new NullPointerException("Incorrect Drop Data");
|
||||
}
|
||||
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.info("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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
/*
|
||||
* 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.l2jserver.gameserver.script.faenor;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
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.l2jserver.Config;
|
||||
import com.l2jserver.gameserver.script.Parser;
|
||||
import com.l2jserver.gameserver.script.ParserNotCreatedException;
|
||||
import com.l2jserver.gameserver.script.ScriptDocument;
|
||||
import com.l2jserver.gameserver.script.ScriptEngine;
|
||||
import com.l2jserver.gameserver.script.ScriptPackage;
|
||||
import com.l2jserver.gameserver.scripting.L2ScriptEngineManager;
|
||||
|
||||
/**
|
||||
* @author Luis Arias
|
||||
*/
|
||||
public class FaenorScriptEngine extends ScriptEngine
|
||||
{
|
||||
static Logger _log = Logger.getLogger(FaenorScriptEngine.class.getName());
|
||||
public final static String PACKAGE_DIRECTORY = "data/faenor/";
|
||||
public final static boolean DEBUG = true;
|
||||
|
||||
private LinkedList<ScriptDocument> _scripts;
|
||||
|
||||
public static FaenorScriptEngine getInstance()
|
||||
{
|
||||
return SingletonHolder._instance;
|
||||
}
|
||||
|
||||
private FaenorScriptEngine()
|
||||
{
|
||||
_scripts = new LinkedList<>();
|
||||
loadPackages();
|
||||
parsePackages();
|
||||
|
||||
}
|
||||
|
||||
public void reloadPackages()
|
||||
{
|
||||
_scripts = new LinkedList<>();
|
||||
parsePackages();
|
||||
}
|
||||
|
||||
private void loadPackages()
|
||||
{
|
||||
File packDirectory = new File(Config.DATAPACK_ROOT, PACKAGE_DIRECTORY);// _log.sss(packDirectory.getAbsolutePath());
|
||||
|
||||
FileFilter fileFilter = file -> file.getName().endsWith(".zip");
|
||||
|
||||
File[] files = packDirectory.listFiles(fileFilter);
|
||||
if (files == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ZipFile zipPack;
|
||||
|
||||
for (File file : files)
|
||||
{
|
||||
try
|
||||
{
|
||||
zipPack = new ZipFile(file);
|
||||
}
|
||||
catch (ZipException e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
continue;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
continue;
|
||||
}
|
||||
|
||||
ScriptPackage module = new ScriptPackage(zipPack);
|
||||
|
||||
List<ScriptDocument> scrpts = module.getScriptFiles();
|
||||
for (ScriptDocument script : scrpts)
|
||||
{
|
||||
_scripts.add(script);
|
||||
}
|
||||
try
|
||||
{
|
||||
zipPack.close();
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
}
|
||||
}
|
||||
/*
|
||||
* for (ScriptDocument script : scripts) { _log.sss("Script: "+script); } _log.sss("Sorting"); orderScripts(); for (ScriptDocument script : scripts) { _log.sss("Script: "+script); }
|
||||
*/
|
||||
}
|
||||
|
||||
public void orderScripts()
|
||||
{
|
||||
if (_scripts.size() > 1)
|
||||
{
|
||||
// ScriptDocument npcInfo = null;
|
||||
|
||||
for (int i = 0; i < _scripts.size();)
|
||||
{
|
||||
if (_scripts.get(i).getName().contains("NpcStatData"))
|
||||
{
|
||||
_scripts.addFirst(_scripts.remove(i));
|
||||
// scripts.set(i, scripts.get(0));
|
||||
// scripts.set(0, npcInfo);
|
||||
}
|
||||
else
|
||||
{
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void parsePackages()
|
||||
{
|
||||
L2ScriptEngineManager sem = L2ScriptEngineManager.getInstance();
|
||||
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 (ScriptDocument script : _scripts)
|
||||
{
|
||||
parseScript(script, context);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "", e);
|
||||
}
|
||||
}
|
||||
|
||||
public void parseScript(ScriptDocument script, ScriptContext context)
|
||||
{
|
||||
if (DEBUG)
|
||||
{
|
||||
_log.fine("Parsing Script: " + script.getName());
|
||||
}
|
||||
|
||||
Node node = script.getDocument().getFirstChild();
|
||||
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.fine(script.getName() + "Script Sucessfullty Parsed.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_log.log(Level.WARNING, "Script Parsing Failed: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
if (_scripts.isEmpty())
|
||||
{
|
||||
return "No Packages Loaded.";
|
||||
}
|
||||
|
||||
String out = "Script Packages currently loaded:\n";
|
||||
|
||||
for (ScriptDocument script : _scripts)
|
||||
{
|
||||
out += script;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@SuppressWarnings("synthetic-access")
|
||||
private static class SingletonHolder
|
||||
{
|
||||
protected static final FaenorScriptEngine _instance = new FaenorScriptEngine();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user