DocumentEngine.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 java.io.File;
  17. import java.util.List;
  18. import java.util.Map;
  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<>();
  35. private final List<File> _skillFiles = new FastList<>();
  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. {
  45. hashFiles("data/stats/items/custom", _itemFiles);
  46. }
  47. hashFiles("data/stats/skills", _skillFiles);
  48. if (Config.CUSTOM_SKILLS_LOAD)
  49. {
  50. hashFiles("data/stats/skills/custom", _skillFiles);
  51. }
  52. }
  53. private void hashFiles(String dirname, List<File> hash)
  54. {
  55. File dir = new File(Config.DATAPACK_ROOT, dirname);
  56. if (!dir.exists())
  57. {
  58. _log.warning("Dir " + dir.getAbsolutePath() + " not exists");
  59. return;
  60. }
  61. File[] files = dir.listFiles(new XMLFilter());
  62. for (File f : files)
  63. {
  64. hash.add(f);
  65. }
  66. }
  67. public List<L2Skill> loadSkills(File file)
  68. {
  69. if (file == null)
  70. {
  71. _log.warning("Skill file not found.");
  72. return null;
  73. }
  74. DocumentSkill doc = new DocumentSkill(file);
  75. doc.parse();
  76. return doc.getSkills();
  77. }
  78. public void loadAllSkills(final Map<Integer, L2Skill> allSkills)
  79. {
  80. int count = 0;
  81. for (File file : _skillFiles)
  82. {
  83. List<L2Skill> s = loadSkills(file);
  84. if (s == null)
  85. {
  86. continue;
  87. }
  88. for (L2Skill skill : s)
  89. {
  90. allSkills.put(SkillTable.getSkillHashCode(skill), skill);
  91. count++;
  92. }
  93. }
  94. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
  95. }
  96. /**
  97. * Return created items
  98. * @return List of {@link L2Item}
  99. */
  100. public List<L2Item> loadItems()
  101. {
  102. List<L2Item> list = new FastList<>();
  103. for (File f : _itemFiles)
  104. {
  105. DocumentItem document = new DocumentItem(f);
  106. document.parse();
  107. list.addAll(document.getItemList());
  108. }
  109. return list;
  110. }
  111. private static class SingletonHolder
  112. {
  113. protected static final DocumentEngine _instance = new DocumentEngine();
  114. }
  115. }