DocumentEngine.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.engines;
  16. import gnu.trove.map.hash.TIntObjectHashMap;
  17. import java.io.File;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import javolution.util.FastList;
  21. import com.l2jserver.Config;
  22. import com.l2jserver.gameserver.datatables.SkillTable;
  23. import com.l2jserver.gameserver.engines.items.DocumentItem;
  24. import com.l2jserver.gameserver.engines.skills.DocumentSkill;
  25. import com.l2jserver.gameserver.model.items.L2Item;
  26. import com.l2jserver.gameserver.model.skills.L2Skill;
  27. import com.l2jserver.util.file.filter.XMLFilter;
  28. /**
  29. * @author mkizub
  30. */
  31. public class DocumentEngine
  32. {
  33. private static final Logger _log = Logger.getLogger(DocumentEngine.class.getName());
  34. private final List<File> _itemFiles = new FastList<File>();
  35. private final List<File> _skillFiles = new FastList<File>();
  36. public static DocumentEngine getInstance()
  37. {
  38. return SingletonHolder._instance;
  39. }
  40. protected DocumentEngine()
  41. {
  42. hashFiles("data/stats/items", _itemFiles);
  43. if (Config.CUSTOM_ITEMS_LOAD)
  44. hashFiles("data/stats/items/custom", _itemFiles);
  45. hashFiles("data/stats/skills", _skillFiles);
  46. if (Config.CUSTOM_SKILLS_LOAD)
  47. hashFiles("data/stats/skills/custom", _skillFiles);
  48. }
  49. private void hashFiles(String dirname, List<File> hash)
  50. {
  51. File dir = new File(Config.DATAPACK_ROOT, dirname);
  52. if (!dir.exists())
  53. {
  54. _log.warning("Dir " + dir.getAbsolutePath() + " not exists");
  55. return;
  56. }
  57. File[] files = dir.listFiles(new XMLFilter());
  58. for (File f : files)
  59. {
  60. hash.add(f);
  61. }
  62. }
  63. public List<L2Skill> loadSkills(File file)
  64. {
  65. if (file == null)
  66. {
  67. _log.warning("Skill file not found.");
  68. return null;
  69. }
  70. DocumentSkill doc = new DocumentSkill(file);
  71. doc.parse();
  72. return doc.getSkills();
  73. }
  74. public void loadAllSkills(final TIntObjectHashMap<L2Skill> allSkills)
  75. {
  76. int count = 0;
  77. for (File file : _skillFiles)
  78. {
  79. List<L2Skill> s = loadSkills(file);
  80. if (s == null)
  81. {
  82. continue;
  83. }
  84. for (L2Skill skill : s)
  85. {
  86. allSkills.put(SkillTable.getSkillHashCode(skill), skill);
  87. count++;
  88. }
  89. }
  90. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
  91. }
  92. /**
  93. * Return created items
  94. * @return List of {@link L2Item}
  95. */
  96. public List<L2Item> loadItems()
  97. {
  98. List<L2Item> list = new FastList<L2Item>();
  99. for (File f : _itemFiles)
  100. {
  101. DocumentItem document = new DocumentItem(f);
  102. document.parse();
  103. list.addAll(document.getItemList());
  104. }
  105. return list;
  106. }
  107. private static class SingletonHolder
  108. {
  109. protected static final DocumentEngine _instance = new DocumentEngine();
  110. }
  111. }