CrestCache.java 8.7 KB

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