DocumentEngine.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2004-2015 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.ArrayList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.logging.Logger;
  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 ArrayList<>();
  39. private final List<File> _skillFiles = new ArrayList<>();
  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. final File[] files = dir.listFiles(new XMLFilter());
  66. if (files != null)
  67. {
  68. for (File f : files)
  69. {
  70. hash.add(f);
  71. }
  72. }
  73. }
  74. public List<Skill> loadSkills(File file)
  75. {
  76. if (file == null)
  77. {
  78. _log.warning("Skill file not found.");
  79. return null;
  80. }
  81. DocumentSkill doc = new DocumentSkill(file);
  82. doc.parse();
  83. return doc.getSkills();
  84. }
  85. public void loadAllSkills(final Map<Integer, Skill> allSkills)
  86. {
  87. int count = 0;
  88. for (File file : _skillFiles)
  89. {
  90. List<Skill> s = loadSkills(file);
  91. if (s == null)
  92. {
  93. continue;
  94. }
  95. for (Skill skill : s)
  96. {
  97. allSkills.put(SkillData.getSkillHashCode(skill), skill);
  98. count++;
  99. }
  100. }
  101. _log.info(getClass().getSimpleName() + ": Loaded " + count + " Skill templates from XML files.");
  102. }
  103. /**
  104. * Return created items
  105. * @return List of {@link L2Item}
  106. */
  107. public List<L2Item> loadItems()
  108. {
  109. List<L2Item> list = new ArrayList<>();
  110. for (File f : _itemFiles)
  111. {
  112. DocumentItem document = new DocumentItem(f);
  113. document.parse();
  114. list.addAll(document.getItemList());
  115. }
  116. return list;
  117. }
  118. private static class SingletonHolder
  119. {
  120. protected static final DocumentEngine _instance = new DocumentEngine();
  121. }
  122. }