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<Integer, byte[]>();
  41. private final FastMRUCache<Integer, byte[]> _cachePledgeLarge = new FastMRUCache<Integer, byte[]>();
  42. private final FastMRUCache<Integer, byte[]> _cacheAlly = new FastMRUCache<Integer, byte[]>();
  43. private int _loadedFiles;
  44. private long _bytesBuffLen;
  45. public static CrestCache getInstance()
  46. {
  47. return SingletonHolder._instance;
  48. }
  49. private 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. PreparedStatement statement = con.prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?");
  206. statement.setInt(1, newId);
  207. statement.setInt(2, clan.getClanId());
  208. statement.executeUpdate();
  209. statement.close();
  210. }
  211. catch (SQLException e)
  212. {
  213. _log.log(Level.WARNING, "Could not update the crest id:" + e.getMessage(), e);
  214. }
  215. finally
  216. {
  217. L2DatabaseFactory.close(con);
  218. }
  219. clan.setCrestId(newId);
  220. }
  221. else
  222. {
  223. _log.info("Clan Id: " + clanId + " does not exist in table.. deleting.");
  224. file.delete();
  225. }
  226. }
  227. }
  228. public float getMemoryUsage()
  229. {
  230. return ((float) _bytesBuffLen / 1048576);
  231. }
  232. public int getLoadedFiles()
  233. {
  234. return _loadedFiles;
  235. }
  236. public byte[] getPledgeCrest(int id)
  237. {
  238. return _cachePledge.get(id);
  239. }
  240. public byte[] getPledgeCrestLarge(int id)
  241. {
  242. return _cachePledgeLarge.get(id);
  243. }
  244. public byte[] getAllyCrest(int id)
  245. {
  246. return _cacheAlly.get(id);
  247. }
  248. public void removePledgeCrest(int id)
  249. {
  250. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + id + ".bmp");
  251. _cachePledge.remove(id);
  252. try
  253. {
  254. crestFile.delete();
  255. }
  256. catch (Exception e)
  257. {
  258. }
  259. }
  260. public void removePledgeCrestLarge(int id)
  261. {
  262. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + id + ".bmp");
  263. _cachePledgeLarge.remove(id);
  264. try
  265. {
  266. crestFile.delete();
  267. }
  268. catch (Exception e)
  269. {
  270. }
  271. }
  272. public void removeOldPledgeCrest(int id)
  273. {
  274. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Pledge_" + id + ".bmp");
  275. try
  276. {
  277. crestFile.delete();
  278. }
  279. catch (Exception e)
  280. {
  281. }
  282. }
  283. public void removeAllyCrest(int id)
  284. {
  285. File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + id + ".bmp");
  286. _cacheAlly.remove(id);
  287. try
  288. {
  289. crestFile.delete();
  290. }
  291. catch (Exception e)
  292. {
  293. }
  294. }
  295. public boolean savePledgeCrest(int newId, byte[] data)
  296. {
  297. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp");
  298. try (FileOutputStream out = new FileOutputStream(crestFile))
  299. {
  300. out.write(data);
  301. _cachePledge.getContentMap().put(newId, data);
  302. return true;
  303. }
  304. catch (IOException e)
  305. {
  306. _log.log(Level.INFO, "Error saving pledge crest" + crestFile + ":", e);
  307. return false;
  308. }
  309. }
  310. public boolean savePledgeCrestLarge(int newId, byte[] data)
  311. {
  312. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/Crest_Large_" + newId + ".bmp");
  313. try (FileOutputStream out = new FileOutputStream(crestFile))
  314. {
  315. out.write(data);
  316. _cachePledgeLarge.getContentMap().put(newId, data);
  317. return true;
  318. }
  319. catch (IOException e)
  320. {
  321. _log.log(Level.INFO, "Error saving Large pledge crest" + crestFile + ":", e);
  322. return false;
  323. }
  324. }
  325. public boolean saveAllyCrest(int newId, byte[] data)
  326. {
  327. final File crestFile = new File(Config.DATAPACK_ROOT, "data/crests/AllyCrest_" + newId + ".bmp");
  328. try (FileOutputStream out = new FileOutputStream(crestFile))
  329. {
  330. out.write(data);
  331. _cacheAlly.getContentMap().put(newId, data);
  332. return true;
  333. }
  334. catch (IOException e)
  335. {
  336. _log.log(Level.INFO, "Error saving ally crest" + crestFile + ":", e);
  337. return false;
  338. }
  339. }
  340. @SuppressWarnings("synthetic-access")
  341. private static class SingletonHolder
  342. {
  343. protected static final CrestCache _instance = new CrestCache();
  344. }
  345. }