CrestCache.java 11 KB

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