DocumentEngine.java 3.4 KB

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