CrestCache.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (C) 2004-2013 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.cache;
  20. import java.io.File;
  21. import java.io.FileFilter;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.RandomAccessFile;
  25. import java.sql.Connection;
  26. import java.sql.PreparedStatement;
  27. import java.sql.SQLException;
  28. import java.util.logging.Level;
  29. import java.util.logging.Logger;
  30. import javolution.util.FastMap;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.L2DatabaseFactory;
  33. import com.l2jserver.gameserver.datatables.ClanTable;
  34. import com.l2jserver.gameserver.idfactory.IdFactory;
  35. import com.l2jserver.gameserver.model.L2Clan;
  36. import com.l2jserver.util.file.filter.BMPFilter;
  37. import com.l2jserver.util.file.filter.OldPledgeFilter;
  38. /**
  39. * @author Layane
  40. */
  41. public class CrestCache
  42. {
  43. private static Logger _log = Logger.getLogger(CrestCache.class.getName());
  44. private final FastMRUCache<Integer, byte[]> _cachePledge = new FastMRUCache<>();
  45. private final FastMRUCache<Integer, byte[]> _cachePledgeLarge = new FastMRUCache<>();
  46. private final FastMRUCache<Integer, byte[]> _cacheAlly = new FastMRUCache<>();
  47. private int _loadedFiles;
  48. private long _bytesBuffLen;
  49. public static CrestCache getInstance()
  50. {
  51. return SingletonHolder._instance;
  52. }
  53. protected CrestCache()
  54. {
  55. convertOldPedgeFiles();
  56. reload();
  57. }
  58. public void reload()
  59. {
  60. final FileFilter filter = new BMPFilter();
  61. final File dir = new File(Config.DATAPACK_ROOT, "data/crests/");
  62. final File[] files = dir.listFiles(filter);
  63. byte[] content;
  64. synchronized (this)
  65. {
  66. _loadedFiles = 0;
  67. _bytesBuffLen = 0;
  68. _cachePledge.clear();
  69. _cachePledgeLarge.clear();
  70. _cacheAlly.clear();
  71. }
  72. FastMap<Integer, byte[]> _mapPledge = _cachePledge.getContentMap();
  73. FastMap<Integer, byte[]> _mapPledgeLarge = _cachePledgeLarge.getContentMap();
  74. FastMap<Integer, byte[]> _mapAlly = _cacheAlly.getContentMap();
  75. final L2Clan[] clans = ClanTable.getInstance().getClans();
  76. for (File file : files)
  77. {
  78. synchronized (this)
  79. {
  80. try (RandomAccessFile rfa = new RandomAccessFile(file, "r"))
  81. {
  82. content = new byte[(int) rfa.length()];
  83. rfa.readFully(content);
  84. boolean erase = true;
  85. int crestId = 0;
  86. if (file.getName().startsWith("Crest_Large_"))
  87. {
  88. crestId = Integer.parseInt(file.getName().substring(12, file.getName().length() - 4));
  89. if (Config.CLEAR_CREST_CACHE)
  90. {
  91. for (final L2Clan clan : clans)
  92. {
  93. if (clan.getCrestLargeId() == crestId)
  94. {
  95. erase = false;
  96. break;
  97. }
  98. }
  99. if (erase)
  100. {
  101. file.delete();
  102. continue;
  103. }
  104. }
  105. _mapPledgeLarge.put(crestId, content);
  106. }
  107. else if (file.getName().startsWith("Crest_"))
  108. {
  109. crestId = Integer.parseInt(file.getName().substring(6, file.getName().length() - 4));
  110. if (Config.CLEAR_CREST_CACHE)
  111. {
  112. for (final L2Clan clan : clans)
  113. {
  114. if (clan.getCrestId() == crestId)
  115. {
  116. erase = false;
  117. break;
  118. }
  119. }
  120. if (erase)
  121. {
  122. file.delete();
  123. continue;
  124. }
  125. }
  126. _mapPledge.put(crestId, content);
  127. }
  128. else if (file.getName().startsWith("AllyCrest_"))
  129. {
  130. crestId = Integer.parseInt(file.getName().substring(10, file.getName().length() - 4));
  131. if (Config.CLEAR_CREST_CACHE)
  132. {
  133. for (final L2Clan clan : clans)
  134. {
  135. if (clan.getAllyCrestId() == crestId)
  136. {
  137. erase = false;
  138. break;
  139. }
  140. }
  141. if (erase)
  142. {
  143. file.delete();
  144. continue;
  145. }
  146. }
  147. _mapAlly.put(crestId, content);
  148. }
  149. _loadedFiles++;
  150. _bytesBuffLen += content.length;
  151. }
  152. catch (Exception e)
  153. {
  154. _log.log(Level.WARNING, "Problem with crest bmp file " + e.getMessage(), e);
  155. }
  156. }
  157. }
  158. for (L2Clan clan : clans)
  159. {
  160. if (clan.getCrestId() != 0)
  161. {
  162. if (getPledgeCrest(clan.getCrestId()) == null)
  163. {
  164. _log.log(Level.INFO, "Removing non-existent crest for clan " + clan.getName() + " [" + clan.getClanId() + "], crestId:" + clan.getCrestId());
  165. clan.setCrestId(0);
  166. clan.changeClanCrest(0);
  167. }
  168. }
  169. if (clan.getCrestLargeId() != 0)
  170. {
  171. if (getPledgeCrestLarge(clan.getCrestLargeId()) == null)
  172. {
  173. _log.log(Level.INFO, "Removing non-existent large crest for clan " + clan.getName() + " [" + clan.getClanId() + "], crestLargeId:" + clan.getCrestLargeId());
  174. clan.setCrestLargeId(0);
  175. clan.changeLargeCrest(0);
  176. }
  177. }
  178. if (clan.getAllyCrestId() != 0)
  179. {
  180. if (getAllyCrest(clan.getAllyCrestId()) == null)
  181. {
  182. _log.log(Level.INFO, "Removing non-existent ally crest for clan " + clan.getName() + " [" + clan.getClanId() + "], allyCrestId:" + clan.getAllyCrestId());
  183. clan.setAllyCrestId(0);
  184. clan.changeAllyCrest(0, true);
  185. }
  186. }
  187. }
  188. _log.info("Cache[Crest]: " + String.format("%.3f", getMemoryUsage()) + "MB on " + getLoadedFiles() + " files loaded. (Forget Time: " + (_cachePledge.getForgetTime() / 1000) + "s , Capacity: " + _cachePledge.capacity() + ")");
  189. }
  190. public void convertOldPedgeFiles()
  191. {
  192. final File dir = new File(Config.DATAPACK_ROOT, "data/crests/");
  193. final File[] files = dir.listFiles(new OldPledgeFilter());
  194. for (File file : files)
  195. {
  196. int clanId = Integer.parseInt(file.getName().substring(7, file.getName().length() - 4));
  197. _log.info("Found old crest file \"" + file.getName() + "\" for clanId " + clanId);
  198. final L2Clan clan = ClanTable.getInstance().getClan(clanId);
  199. if (clan != null)
  200. {
  201. removeOldPledgeCrest(clan.getCrestId());
  202. int newId = IdFactory.getInstance().getNextId();
  203. file.renameTo(new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp"));
  204. _log.info("Renamed Clan crest to new format: Crest_" + newId + ".bmp");
  205. try (Connection con = L2DatabaseFactory.getInstance().getConnection();
  206. PreparedStatement ps = con.prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?"))
  207. {
  208. ps.setInt(1, newId);
  209. ps.setInt(2, clan.getClanId());
  210. ps.executeUpdate();
  211. }
  212. catch (SQLException e)
  213. {
  214. _log.log(Level.WARNING, "Could not update the crest id:" + e.getMessage(), e);
  215. }
  216. clan.setCrestId(newId);
  217. }
  218. else
  219. {
  220. _log.info("Clan Id: " + clanId + " does not exist in table.. deleting.");
  221. file.delete();
  222. }
  223. }
  224. }
  225. public float getMemoryUsage()
  226. {
  227. return ((float) _bytesBuffLen / 1048576);
  228. }
  229. public int getLoadedFiles()
  230. {
  231. return _loadedFiles;
  232. }
  233. public byte[] getPledgeCrest(int id)
  234. {
  235. return _cachePledge.get(id);
  236. }
  237. public byte[] getPledgeCrestLarge(int id)
  238. {
  239. return _cachePledgeLarge.get(id);
  240. }
  241. public byte[] getAllyCrest(int id)
  242. {
  243. return _cacheAlly.get(id);
  244. }
  245. public void removePledgeCrest(int id)
  246. {
  247. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + id + ".bmp");
  248. _cachePledge.remove(id);
  249. try
  250. {
  251. crestFile.delete();
  252. }
  253. catch (Exception e)
  254. {
  255. }
  256. }
  257. public void removePledgeCrestLarge(int id)
  258. {
  259. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + id + ".bmp");
  260. _cachePledgeLarge.remove(id);
  261. try
  262. {
  263. crestFile.delete();
  264. }
  265. catch (Exception e)
  266. {
  267. }
  268. }
  269. public void removeOldPledgeCrest(int id)
  270. {
  271. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Pledge_" + id + ".bmp");
  272. try
  273. {
  274. crestFile.delete();
  275. }
  276. catch (Exception e)
  277. {
  278. }
  279. }
  280. public void removeAllyCrest(int id)
  281. {
  282. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + id + ".bmp");
  283. _cacheAlly.remove(id);
  284. try
  285. {
  286. crestFile.delete();
  287. }
  288. catch (Exception e)
  289. {
  290. }
  291. }
  292. public boolean savePledgeCrest(int newId, byte[] data)
  293. {
  294. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp");
  295. try (FileOutputStream out = new FileOutputStream(crestFile))
  296. {
  297. out.write(data);
  298. _cachePledge.getContentMap().put(newId, data);
  299. return true;
  300. }
  301. catch (IOException e)
  302. {
  303. _log.log(Level.INFO, "Error saving pledge crest" + crestFile + ":", e);
  304. return false;
  305. }
  306. }
  307. public boolean savePledgeCrestLarge(int newId, byte[] data)
  308. {
  309. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + newId + ".bmp");
  310. try (FileOutputStream out = new FileOutputStream(crestFile))
  311. {
  312. out.write(data);
  313. _cachePledgeLarge.getContentMap().put(newId, data);
  314. return true;
  315. }
  316. catch (IOException e)
  317. {
  318. _log.log(Level.INFO, "Error saving Large pledge crest" + crestFile + ":", e);
  319. return false;
  320. }
  321. }
  322. public boolean saveAllyCrest(int newId, byte[] data)
  323. {
  324. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + newId + ".bmp");
  325. try (FileOutputStream out = new FileOutputStream(crestFile))
  326. {
  327. out.write(data);
  328. _cacheAlly.getContentMap().put(newId, data);
  329. return true;
  330. }
  331. catch (IOException e)
  332. {
  333. _log.log(Level.INFO, "Error saving ally crest" + crestFile + ":", e);
  334. return false;
  335. }
  336. }
  337. private static class SingletonHolder
  338. {
  339. protected static final CrestCache _instance = new CrestCache();
  340. }
  341. }