CrestCache.java 10 KB

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