Document parsing classes moved to util.

This commit is contained in:
MobiusDevelopment
2020-11-03 22:11:38 +00:00
parent caf29354ac
commit 9903058be9
158 changed files with 19637 additions and 22684 deletions

View File

@@ -16,13 +16,21 @@
*/
package org.l2jmobius.gameserver.data.xml.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
import org.l2jmobius.gameserver.engines.DocumentEngine;
import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.util.file.filter.XMLFilter;
import org.l2jmobius.gameserver.model.skills.Skill;
import org.l2jmobius.gameserver.util.DocumentSkill;
/**
* Skill data.
@@ -34,23 +42,103 @@ public class SkillData
private final Map<Integer, Skill> _skills = new ConcurrentHashMap<>();
private final Map<Integer, Integer> _skillMaxLevel = new ConcurrentHashMap<>();
private final Set<Integer> _enchantable = ConcurrentHashMap.newKeySet();
private final List<File> _skillFiles = new ArrayList<>();
private static int count = 0;
protected SkillData()
{
processDirectory("data/stats/skills", _skillFiles);
if (Config.CUSTOM_SKILLS_LOAD)
{
processDirectory("data/stats/skills/custom", _skillFiles);
}
load();
}
public void reload()
private void processDirectory(String dirName, List<File> list)
{
load();
// Reload Skill Tree as well.
SkillTreeData.getInstance().load();
final File dir = new File(Config.DATAPACK_ROOT, dirName);
if (!dir.exists())
{
LOGGER.warning("Dir " + dir.getAbsolutePath() + " does not exist.");
return;
}
final File[] files = dir.listFiles(new XMLFilter());
for (File file : files)
{
list.add(file);
}
}
public List<Skill> loadSkills(File file)
{
if (file == null)
{
LOGGER.warning("Skill file not found.");
return null;
}
final DocumentSkill doc = new DocumentSkill(file);
doc.parse();
return doc.getSkills();
}
public void loadAllSkills(Map<Integer, Skill> allSkills)
{
if (Config.THREADS_FOR_LOADING)
{
final Collection<ScheduledFuture<?>> jobs = ConcurrentHashMap.newKeySet();
for (File file : _skillFiles)
{
jobs.add(ThreadPool.schedule(() ->
{
final List<Skill> skills = loadSkills(file);
if (skills == null)
{
return;
}
for (Skill skill : skills)
{
allSkills.put(SkillData.getSkillHashCode(skill), skill);
count++;
}
}, 0));
}
while (!jobs.isEmpty())
{
for (ScheduledFuture<?> job : jobs)
{
if ((job == null) || job.isDone() || job.isCancelled())
{
jobs.remove(job);
}
}
}
}
else
{
for (File file : _skillFiles)
{
final List<Skill> skills = loadSkills(file);
if (skills == null)
{
return;
}
for (Skill skill : skills)
{
allSkills.put(SkillData.getSkillHashCode(skill), skill);
count++;
}
}
}
LOGGER.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
}
private void load()
{
final Map<Integer, Skill> temp = new ConcurrentHashMap<>();
DocumentEngine.getInstance().loadAllSkills(temp);
loadAllSkills(temp);
_skills.clear();
_skills.putAll(temp);
@@ -79,6 +167,13 @@ public class SkillData
}
}
public void reload()
{
load();
// Reload Skill Tree as well.
SkillTreeData.getInstance().load();
}
/**
* Provides the skill hash
* @param skill The Skill to be hashed

View File

@@ -18,11 +18,16 @@ package org.l2jmobius.gameserver.datatables;
import static org.l2jmobius.gameserver.model.itemcontainer.Inventory.ADENA_ID;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -30,8 +35,8 @@ import java.util.logging.Logger;
import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.database.DatabaseFactory;
import org.l2jmobius.commons.util.file.filter.XMLFilter;
import org.l2jmobius.gameserver.data.xml.impl.EnchantItemHPBonusData;
import org.l2jmobius.gameserver.engines.DocumentEngine;
import org.l2jmobius.gameserver.enums.ItemLocation;
import org.l2jmobius.gameserver.instancemanager.IdManager;
import org.l2jmobius.gameserver.model.World;
@@ -47,6 +52,7 @@ import org.l2jmobius.gameserver.model.items.EtcItem;
import org.l2jmobius.gameserver.model.items.Item;
import org.l2jmobius.gameserver.model.items.Weapon;
import org.l2jmobius.gameserver.model.items.instance.ItemInstance;
import org.l2jmobius.gameserver.util.DocumentItem;
import org.l2jmobius.gameserver.util.GMAudit;
/**
@@ -100,27 +106,83 @@ public class ItemTable
private final Map<Integer, EtcItem> _etcItems = new HashMap<>();
private final Map<Integer, Armor> _armors = new HashMap<>();
private final Map<Integer, Weapon> _weapons = new HashMap<>();
/**
* @return a reference to this ItemTable object
*/
public static ItemTable getInstance()
{
return SingletonHolder.INSTANCE;
}
private final List<File> _itemFiles = new ArrayList<>();
protected ItemTable()
{
processDirectory("data/stats/items", _itemFiles);
if (Config.CUSTOM_ITEMS_LOAD)
{
processDirectory("data/stats/items/custom", _itemFiles);
}
load();
}
private void processDirectory(String dirName, List<File> list)
{
final File dir = new File(Config.DATAPACK_ROOT, dirName);
if (!dir.exists())
{
LOGGER.warning("Dir " + dir.getAbsolutePath() + " does not exist.");
return;
}
final File[] files = dir.listFiles(new XMLFilter());
for (File file : files)
{
list.add(file);
}
}
/**
* Return created items
* @return List of {@link Item}
*/
public Collection<Item> loadItems()
{
final Collection<Item> list = ConcurrentHashMap.newKeySet();
if (Config.THREADS_FOR_LOADING)
{
final Collection<ScheduledFuture<?>> jobs = ConcurrentHashMap.newKeySet();
for (File file : _itemFiles)
{
jobs.add(ThreadPool.schedule(() ->
{
final DocumentItem document = new DocumentItem(file);
document.parse();
list.addAll(document.getItemList());
}, 0));
}
while (!jobs.isEmpty())
{
for (ScheduledFuture<?> job : jobs)
{
if ((job == null) || job.isDone() || job.isCancelled())
{
jobs.remove(job);
}
}
}
}
else
{
for (File file : _itemFiles)
{
final DocumentItem document = new DocumentItem(file);
document.parse();
list.addAll(document.getItemList());
}
}
return list;
}
private void load()
{
int highest = 0;
_armors.clear();
_etcItems.clear();
_weapons.clear();
for (Item item : DocumentEngine.getInstance().loadItems())
for (Item item : loadItems())
{
if (highest < item.getId())
{
@@ -435,6 +497,11 @@ public class ItemTable
return _allTemplates.length;
}
public static ItemTable getInstance()
{
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder
{
protected static final ItemTable INSTANCE = new ItemTable();

View File

@@ -1,192 +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 org.l2jmobius.gameserver.engines;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Logger;
import org.l2jmobius.Config;
import org.l2jmobius.commons.concurrent.ThreadPool;
import org.l2jmobius.commons.util.file.filter.XMLFilter;
import org.l2jmobius.gameserver.data.xml.impl.SkillData;
import org.l2jmobius.gameserver.engines.items.DocumentItem;
import org.l2jmobius.gameserver.engines.skills.DocumentSkill;
import org.l2jmobius.gameserver.model.items.Item;
import org.l2jmobius.gameserver.model.skills.Skill;
/**
* @author mkizub
*/
public class DocumentEngine
{
private static final Logger LOGGER = Logger.getLogger(DocumentEngine.class.getName());
private final List<File> _itemFiles = new ArrayList<>();
private final List<File> _skillFiles = new ArrayList<>();
private static int count = 0;
protected DocumentEngine()
{
processDirectory("data/stats/items", _itemFiles);
if (Config.CUSTOM_ITEMS_LOAD)
{
processDirectory("data/stats/items/custom", _itemFiles);
}
processDirectory("data/stats/skills", _skillFiles);
if (Config.CUSTOM_SKILLS_LOAD)
{
processDirectory("data/stats/skills/custom", _skillFiles);
}
}
private void processDirectory(String dirName, List<File> list)
{
final File dir = new File(Config.DATAPACK_ROOT, dirName);
if (!dir.exists())
{
LOGGER.warning("Dir " + dir.getAbsolutePath() + " does not exist.");
return;
}
final File[] files = dir.listFiles(new XMLFilter());
for (File file : files)
{
list.add(file);
}
}
public List<Skill> loadSkills(File file)
{
if (file == null)
{
LOGGER.warning("Skill file not found.");
return null;
}
final DocumentSkill doc = new DocumentSkill(file);
doc.parse();
return doc.getSkills();
}
public void loadAllSkills(Map<Integer, Skill> allSkills)
{
if (Config.THREADS_FOR_LOADING)
{
final Collection<ScheduledFuture<?>> jobs = ConcurrentHashMap.newKeySet();
for (File file : _skillFiles)
{
jobs.add(ThreadPool.schedule(() ->
{
final List<Skill> skills = loadSkills(file);
if (skills == null)
{
return;
}
for (Skill skill : skills)
{
allSkills.put(SkillData.getSkillHashCode(skill), skill);
count++;
}
}, 0));
}
while (!jobs.isEmpty())
{
for (ScheduledFuture<?> job : jobs)
{
if ((job == null) || job.isDone() || job.isCancelled())
{
jobs.remove(job);
}
}
}
}
else
{
for (File file : _skillFiles)
{
final List<Skill> skills = loadSkills(file);
if (skills == null)
{
return;
}
for (Skill skill : skills)
{
allSkills.put(SkillData.getSkillHashCode(skill), skill);
count++;
}
}
}
LOGGER.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
}
/**
* Return created items
* @return List of {@link Item}
*/
public Collection<Item> loadItems()
{
final Collection<Item> list = ConcurrentHashMap.newKeySet();
if (Config.THREADS_FOR_LOADING)
{
final Collection<ScheduledFuture<?>> jobs = ConcurrentHashMap.newKeySet();
for (File file : _itemFiles)
{
jobs.add(ThreadPool.schedule(() ->
{
final DocumentItem document = new DocumentItem(file);
document.parse();
list.addAll(document.getItemList());
}, 0));
}
while (!jobs.isEmpty())
{
for (ScheduledFuture<?> job : jobs)
{
if ((job == null) || job.isDone() || job.isCancelled())
{
jobs.remove(job);
}
}
}
}
else
{
for (File file : _itemFiles)
{
final DocumentItem document = new DocumentItem(file);
document.parse();
list.addAll(document.getItemList());
}
}
return list;
}
private static class SingletonHolder
{
protected static final DocumentEngine INSTANCE = new DocumentEngine();
}
public static DocumentEngine getInstance()
{
return SingletonHolder.INSTANCE;
}
}

View File

@@ -1,30 +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 org.l2jmobius.gameserver.engines.items;
import org.l2jmobius.gameserver.model.StatSet;
import org.l2jmobius.gameserver.model.items.Item;
public class ItemDataHolder
{
int id;
String type;
String name;
StatSet set;
int currentLevel;
Item item;
}

View File

@@ -1,44 +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 org.l2jmobius.gameserver.engines.skills;
import java.util.ArrayList;
import java.util.List;
import org.l2jmobius.gameserver.model.StatSet;
import org.l2jmobius.gameserver.model.skills.Skill;
/**
* @author Mobius
*/
public class SkillDataHolder
{
public int id;
public String name;
public StatSet[] sets;
public StatSet[] enchsets1;
public StatSet[] enchsets2;
public StatSet[] enchsets3;
public StatSet[] enchsets4;
public StatSet[] enchsets5;
public StatSet[] enchsets6;
public StatSet[] enchsets7;
public StatSet[] enchsets8;
public int currentLevel;
public List<Skill> skills = new ArrayList<>();
public List<Skill> currentSkills = new ArrayList<>();
}

View File

@@ -14,7 +14,7 @@
* 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 org.l2jmobius.gameserver.engines;
package org.l2jmobius.gameserver.util;
import java.io.File;
import java.util.ArrayList;

View File

@@ -14,7 +14,7 @@
* 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 org.l2jmobius.gameserver.engines.items;
package org.l2jmobius.gameserver.util;
import java.io.File;
import java.lang.reflect.Constructor;
@@ -26,7 +26,6 @@ import java.util.logging.Level;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.l2jmobius.gameserver.engines.DocumentBase;
import org.l2jmobius.gameserver.model.StatSet;
import org.l2jmobius.gameserver.model.conditions.Condition;
import org.l2jmobius.gameserver.model.items.Item;
@@ -36,9 +35,18 @@ import org.l2jmobius.gameserver.model.items.Item;
*/
public class DocumentItem extends DocumentBase
{
private ItemDataHolder _currentItem = null;
private DocumentItemDataHolder _currentItem = null;
private final List<Item> _itemsInFile = new ArrayList<>();
private class DocumentItemDataHolder
{
int id;
String type;
StatSet set;
int currentLevel;
Item item;
}
/**
* @param file
*/
@@ -78,7 +86,7 @@ public class DocumentItem extends DocumentBase
{
try
{
_currentItem = new ItemDataHolder();
_currentItem = new DocumentItemDataHolder();
parseItem(d);
_itemsInFile.add(_currentItem.item);
resetTable();
@@ -93,7 +101,7 @@ public class DocumentItem extends DocumentBase
}
}
protected void parseItem(Node node) throws InvocationTargetException
private void parseItem(Node node) throws InvocationTargetException
{
Node n = node;
final int itemId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
@@ -101,7 +109,6 @@ public class DocumentItem extends DocumentBase
final String itemName = n.getAttributes().getNamedItem("name").getNodeValue();
_currentItem.id = itemId;
_currentItem.name = itemName;
_currentItem.type = className;
_currentItem.set = new StatSet();
_currentItem.set.set("item_id", itemId);

View File

@@ -14,7 +14,7 @@
* 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 org.l2jmobius.gameserver.engines.skills;
package org.l2jmobius.gameserver.util;
import java.io.File;
import java.util.ArrayList;
@@ -26,7 +26,6 @@ import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.l2jmobius.gameserver.data.xml.impl.EnchantSkillGroupsData;
import org.l2jmobius.gameserver.engines.DocumentBase;
import org.l2jmobius.gameserver.model.StatSet;
import org.l2jmobius.gameserver.model.conditions.Condition;
import org.l2jmobius.gameserver.model.skills.EffectScope;
@@ -37,15 +36,33 @@ import org.l2jmobius.gameserver.model.skills.Skill;
*/
public class DocumentSkill extends DocumentBase
{
private SkillDataHolder _currentSkill;
private DocumentSkillDataHolder _currentSkill;
private final List<Skill> _skillsInFile = new ArrayList<>();
private class DocumentSkillDataHolder
{
public int id;
public String name;
public StatSet[] sets;
public StatSet[] enchsets1;
public StatSet[] enchsets2;
public StatSet[] enchsets3;
public StatSet[] enchsets4;
public StatSet[] enchsets5;
public StatSet[] enchsets6;
public StatSet[] enchsets7;
public StatSet[] enchsets8;
public int currentLevel;
public List<Skill> skills = new ArrayList<>();
public List<Skill> currentSkills = new ArrayList<>();
}
public DocumentSkill(File file)
{
super(file);
}
private void setCurrentSkill(SkillDataHolder skill)
private void setCurrentSkill(DocumentSkillDataHolder skill)
{
_currentSkill = skill;
}
@@ -56,11 +73,6 @@ public class DocumentSkill extends DocumentBase
return _currentSkill.sets[_currentSkill.currentLevel];
}
public List<Skill> getSkills()
{
return _skillsInFile;
}
@Override
protected String getTableValue(String name)
{
@@ -100,7 +112,7 @@ public class DocumentSkill extends DocumentBase
{
if ("skill".equalsIgnoreCase(d.getNodeName()))
{
setCurrentSkill(new SkillDataHolder());
setCurrentSkill(new DocumentSkillDataHolder());
parseSkill(d);
_skillsInFile.addAll(_currentSkill.skills);
resetTable();
@@ -109,14 +121,14 @@ public class DocumentSkill extends DocumentBase
}
else if ("skill".equalsIgnoreCase(n.getNodeName()))
{
setCurrentSkill(new SkillDataHolder());
setCurrentSkill(new DocumentSkillDataHolder());
parseSkill(n);
_skillsInFile.addAll(_currentSkill.skills);
}
}
}
protected void parseSkill(Node node)
private void parseSkill(Node node)
{
Node n = node;
final NamedNodeMap attrs = n.getAttributes();
@@ -1655,4 +1667,9 @@ public class DocumentSkill extends DocumentBase
}
}
}
public List<Skill> getSkills()
{
return _skillsInFile;
}
}