CharacterSelect.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.clientpackets;
  16. import java.util.logging.Logger;
  17. import net.sf.l2j.Config;
  18. import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
  19. import net.sf.l2j.gameserver.network.L2GameClient.GameClientState;
  20. import net.sf.l2j.gameserver.serverpackets.ActionFailed;
  21. import net.sf.l2j.gameserver.serverpackets.CharSelected;
  22. import net.sf.l2j.gameserver.serverpackets.SSQInfo;
  23. /**
  24. * This class ...
  25. *
  26. * @version $Revision: 1.5.2.1.2.5 $ $Date: 2005/03/27 15:29:30 $
  27. */
  28. public class CharacterSelect extends L2GameClientPacket
  29. {
  30. private static final String _C__0D_CHARACTERSELECT = "[C] 0D CharacterSelect";
  31. private static Logger _log = Logger.getLogger(CharacterSelect.class.getName());
  32. // cd
  33. private int _charSlot;
  34. @SuppressWarnings("unused")
  35. private int _unk1; // new in C4
  36. @SuppressWarnings("unused")
  37. private int _unk2; // new in C4
  38. @SuppressWarnings("unused")
  39. private int _unk3; // new in C4
  40. @SuppressWarnings("unused")
  41. private int _unk4; // new in C4
  42. protected void readImpl()
  43. {
  44. _charSlot = readD();
  45. _unk1 = readH();
  46. _unk2 = readD();
  47. _unk3 = readD();
  48. _unk4 = readD();
  49. }
  50. protected void runImpl()
  51. {
  52. // if there is a playback.dat file in the current directory, it will
  53. // be sent to the client instead of any regular packets
  54. // to make this work, the first packet in the playback.dat has to
  55. // be a [S]0x21 packet
  56. // after playback is done, the client will not work correct and need to exit
  57. //playLogFile(getConnection()); // try to play log file
  58. // we should always be abble to acquire the lock
  59. // but if we cant lock then nothing should be done (ie repeated packet)
  60. if (this.getClient().getActiveCharLock().tryLock())
  61. {
  62. try
  63. {
  64. // should always be null
  65. // but if not then this is repeated packet and nothing should be done here
  66. if (this.getClient().getActiveChar() == null)
  67. {
  68. // The L2PcInstance must be created here, so that it can be attached to the L2GameClient
  69. if (Config.DEBUG)
  70. {
  71. _log.fine("selected slot:" + _charSlot);
  72. }
  73. //load up character from disk
  74. L2PcInstance cha = getClient().loadCharFromDisk(_charSlot);
  75. if (cha == null)
  76. {
  77. _log.severe("Character could not be loaded (slot:"+_charSlot+")");
  78. sendPacket(new ActionFailed());
  79. return;
  80. }
  81. if (cha.getAccessLevel() < 0)
  82. {
  83. cha.closeNetConnection();
  84. return;
  85. }
  86. cha.setClient(this.getClient());
  87. getClient().setActiveChar(cha);
  88. sendPacket(new SSQInfo());
  89. this.getClient().setState(GameClientState.IN_GAME);
  90. CharSelected cs = new CharSelected(cha, getClient().getSessionId().playOkID1);
  91. sendPacket(cs);
  92. }
  93. }
  94. finally
  95. {
  96. this.getClient().getActiveCharLock().unlock();
  97. }
  98. }
  99. }
  100. /*
  101. private void playLogFile(Connection connection)
  102. {
  103. long diff = 0;
  104. long first = -1;
  105. try
  106. {
  107. LineNumberReader lnr =
  108. new LineNumberReader(new FileReader("playback.dat"));
  109. String line = null;
  110. while ((line = lnr.readLine()) != null)
  111. {
  112. if (line.length() > 0 && line.substring(0, 1).equals("1"))
  113. {
  114. String timestamp = line.substring(0, 13);
  115. long time = Long.parseLong(timestamp);
  116. if (first == -1)
  117. {
  118. long start = System.currentTimeMillis();
  119. first = time;
  120. diff = start - first;
  121. }
  122. String cs = line.substring(14, 15);
  123. // read packet definition
  124. ByteArrayOutputStream bais = new ByteArrayOutputStream();
  125. while (true)
  126. {
  127. String temp = lnr.readLine();
  128. if (temp.length() < 53)
  129. {
  130. break;
  131. }
  132. String bytes = temp.substring(6, 53);
  133. StringTokenizer st = new StringTokenizer(bytes);
  134. while (st.hasMoreTokens())
  135. {
  136. String b = st.nextToken();
  137. int number = Integer.parseInt(b, 16);
  138. bais.write(number);
  139. }
  140. }
  141. if (cs.equals("S"))
  142. {
  143. //wait for timestamp and send packet
  144. int wait =
  145. (int) (time + diff - System.currentTimeMillis());
  146. if (wait > 0)
  147. {
  148. if (Config.DEBUG) _log.fine("waiting"+ wait);
  149. Thread.sleep(wait);
  150. }
  151. if (Config.DEBUG) _log.fine("sending:"+ time);
  152. byte[] data = bais.toByteArray();
  153. if (data.length != 0)
  154. {
  155. //connection.sendPacket(data);
  156. }
  157. else
  158. {
  159. if (Config.DEBUG) _log.fine("skipping broken data");
  160. }
  161. }
  162. else
  163. {
  164. // skip packet
  165. }
  166. }
  167. }
  168. }
  169. catch (FileNotFoundException f)
  170. {
  171. // should not happen
  172. }
  173. catch (Exception e)
  174. {
  175. _log.log(Level.SEVERE, "Error:", e);
  176. }
  177. }
  178. */
  179. /* (non-Javadoc)
  180. * @see net.sf.l2j.gameserver.clientpackets.ClientBasePacket#getType()
  181. */
  182. public String getType()
  183. {
  184. return _C__0D_CHARACTERSELECT;
  185. }
  186. }