CrestCache.java 8.4 KB

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