/* * 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 . */ package com.l2jserver.gameserver.skills; import gnu.trove.TIntObjectHashMap; import java.io.File; import java.util.List; import java.util.Map; import java.util.logging.Logger; import com.l2jserver.Config; import com.l2jserver.gameserver.Item; import com.l2jserver.gameserver.datatables.SkillTable; import com.l2jserver.gameserver.model.L2Skill; import com.l2jserver.gameserver.templates.item.L2Armor; import com.l2jserver.gameserver.templates.item.L2EtcItem; import com.l2jserver.gameserver.templates.item.L2EtcItemType; import com.l2jserver.gameserver.templates.item.L2Item; import com.l2jserver.gameserver.templates.item.L2Weapon; import javolution.util.FastList; /** * @author mkizub */ public class SkillsEngine { protected static final Logger _log = Logger.getLogger(SkillsEngine.class.getName()); private List _armorFiles = new FastList(); private List _weaponFiles = new FastList(); private List _etcitemFiles = new FastList(); private List _skillFiles = new FastList(); public static SkillsEngine getInstance() { return SingletonHolder._instance; } private SkillsEngine() { hashFiles("data/stats/etcitem", _etcitemFiles); hashFiles("data/stats/armor", _armorFiles); hashFiles("data/stats/weapon", _weaponFiles); hashFiles("data/stats/skills", _skillFiles); } private void hashFiles(String dirname, List hash) { File dir = new File(Config.DATAPACK_ROOT, dirname); if (!dir.exists()) { _log.config("Dir " + dir.getAbsolutePath() + " not exists"); return; } File[] files = dir.listFiles(); for (File f : files) { if (f.getName().endsWith(".xml") && !f.getName().startsWith("custom")) hash.add(f); } File customfile = new File(Config.DATAPACK_ROOT, dirname + "/custom.xml"); if (customfile.exists()) hash.add(customfile); } public List loadSkills(File file) { if (file == null) { _log.config("Skill file not found."); return null; } DocumentSkill doc = new DocumentSkill(file); doc.parse(); return doc.getSkills(); } public void loadAllSkills(final TIntObjectHashMap allSkills) { int count = 0; for (File file : _skillFiles) { List s = loadSkills(file); if (s == null) continue; for (L2Skill skill : s) { allSkills.put(SkillTable.getSkillHashCode(skill), skill); count++; } } _log.config("SkillsEngine: Loaded " + count + " Skill templates from XML files."); } public List loadArmors(Map armorData) { List list = new FastList(); for (L2Item item : loadData(armorData, _armorFiles)) { list.add((L2Armor) item); } return list; } public List loadWeapons(Map weaponData) { List list = new FastList(); for (L2Item item : loadData(weaponData, _weaponFiles)) { list.add((L2Weapon) item); } return list; } public List loadItems(Map itemData) { List list = new FastList(); List xmlItem = new FastList(); for (L2Item item : loadData(itemData, _etcitemFiles)) { list.add((L2EtcItem) item); xmlItem.add(item.getItemId()); } for (Item item : itemData.values()) { if (!xmlItem.contains(item.id)) list.add(new L2EtcItem((L2EtcItemType) item.type, item.set)); } return list; } public List loadData(Map itemData, List files) { List list = new FastList(); for (File f : files) { DocumentItem document = new DocumentItem(itemData, f); document.parse(); list.addAll(document.getItemList()); } return list; } @SuppressWarnings("synthetic-access") private static class SingletonHolder { protected static final SkillsEngine _instance = new SkillsEngine(); } }