AbstractHtmlPacket.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.network.serverpackets;
  20. import java.util.logging.Level;
  21. import com.l2jserver.gameserver.cache.HtmCache;
  22. import com.l2jserver.gameserver.enums.HtmlActionScope;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.util.Util;
  25. /**
  26. * @author HorridoJoho
  27. */
  28. public abstract class AbstractHtmlPacket extends L2GameServerPacket
  29. {
  30. public static final char VAR_PARAM_START_CHAR = '$';
  31. private final int _npcObjId;
  32. private String _html = null;
  33. private boolean _disabledValidation = false;
  34. protected AbstractHtmlPacket()
  35. {
  36. _npcObjId = 0;
  37. }
  38. protected AbstractHtmlPacket(int npcObjId)
  39. {
  40. if (npcObjId < 0)
  41. {
  42. throw new IllegalArgumentException();
  43. }
  44. _npcObjId = npcObjId;
  45. }
  46. protected AbstractHtmlPacket(String html)
  47. {
  48. _npcObjId = 0;
  49. setHtml(html);
  50. }
  51. protected AbstractHtmlPacket(int npcObjId, String html)
  52. {
  53. if (npcObjId < 0)
  54. {
  55. throw new IllegalArgumentException();
  56. }
  57. _npcObjId = npcObjId;
  58. setHtml(html);
  59. }
  60. public final void disableValidation()
  61. {
  62. _disabledValidation = true;
  63. }
  64. public final void setHtml(String html)
  65. {
  66. if (html.length() > 17200)
  67. {
  68. _log.log(Level.WARNING, "Html is too long! this will crash the client!", new Throwable());
  69. _html = html.substring(0, 17200);
  70. }
  71. if (!html.contains("<html"))
  72. {
  73. html = "<html><body>" + html + "</body></html>";
  74. }
  75. _html = html;
  76. }
  77. public final boolean setFile(String prefix, String path)
  78. {
  79. String content = HtmCache.getInstance().getHtm(prefix, path);
  80. if (content == null)
  81. {
  82. setHtml("<html><body>My Text is missing:<br>" + path + "</body></html>");
  83. _log.warning("missing html page " + path);
  84. return false;
  85. }
  86. setHtml(content);
  87. return true;
  88. }
  89. public final void replace(String pattern, String value)
  90. {
  91. _html = _html.replaceAll(pattern, value.replaceAll("\\$", "\\\\\\$"));
  92. }
  93. public final void replace(String pattern, boolean val)
  94. {
  95. replace(pattern, String.valueOf(val));
  96. }
  97. public final void replace(String pattern, int val)
  98. {
  99. replace(pattern, String.valueOf(val));
  100. }
  101. public final void replace(String pattern, long val)
  102. {
  103. replace(pattern, String.valueOf(val));
  104. }
  105. public final void replace(String pattern, double val)
  106. {
  107. replace(pattern, String.valueOf(val));
  108. }
  109. @Override
  110. public final void runImpl()
  111. {
  112. L2PcInstance player = getClient().getActiveChar();
  113. if (player == null)
  114. {
  115. return;
  116. }
  117. player.clearHtmlActions(getScope());
  118. if (_disabledValidation)
  119. {
  120. return;
  121. }
  122. Util.buildHtmlActionCache(player, getScope(), _npcObjId, _html);
  123. }
  124. public final int getNpcObjId()
  125. {
  126. return _npcObjId;
  127. }
  128. public final String getHtml()
  129. {
  130. return _html;
  131. }
  132. public abstract HtmlActionScope getScope();
  133. }