Guest User

Untitled

a guest
Sep 16th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.80 KB | None | 0 0
  1. C# compiler interprets static class as a namespace
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.IO;
  9. using System.Collections;
  10. using ProtoBuf;
  11. using UnnamedGameServer;
  12. using System.Threading;
  13.  
  14. namespace Unnamed_game
  15. {
  16. class Connection
  17. {
  18. public struct UserInformation
  19. {
  20. public string Username;
  21. public int UserID;
  22. }
  23.  
  24. static private Connection instance;
  25.  
  26. private Connection()
  27. {
  28. client = new TcpClient();
  29. header = new PacketHeader();
  30. _isLoggedCharacter = false;
  31. _isLoggedUser = false;
  32. magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
  33. }
  34.  
  35. // properties
  36. TcpClient client;
  37. PacketHeader header;
  38. Thread serverCommThread;
  39. byte[] magicByte;
  40. bool _isLoggedCharacter;
  41. CharacterInformation chInformation
  42. {
  43. get
  44. {
  45. return Player.Instance().Information;
  46. }
  47. }
  48. bool _isLoggedUser;
  49. public UserInformation Information;
  50.  
  51. // methods
  52. static public Connection Instance()
  53. {
  54. if(instance == null)
  55. {
  56. instance = new Connection();
  57. return instance;
  58. }
  59. else
  60. {
  61. return instance;
  62. }
  63. }
  64. /// <summary>
  65. /// Should never be used. Use the parameterless one to get the server address and information. It doesn't use try-catch, the exception handling should be done on Game1.Initialize()
  66. /// </summary>
  67. public void ConnectServer(IPEndPoint endPoint)
  68. {
  69. if (client.Connected)
  70. {
  71. return;
  72. }
  73. else
  74. {
  75. client.Connect(endPoint);
  76. serverCommThread = new Thread(HandleServerCommunication);
  77. serverCommThread.Start();
  78. }
  79. }
  80. public void ConnectServer()
  81. {
  82. this.ConnectServer(new IPEndPoint(UnnamedGameServer.SharedConstants.ServerInfo.ServerAddress, UnnamedGameServer.SharedConstants.ServerInfo.Port));
  83. }
  84. private void HandleServerCommunication()
  85. {
  86. if (client == null)
  87. {
  88. throw new Exception("The TcpClient is null");
  89. }
  90. else
  91. {
  92. // this doesn't work
  93. byte[] magicByte = SharedConstants.ServerInfo.MagicByte;
  94. // this works
  95. magicByte = UnnamedGameServer.SharedConstants.ServerInfo.MagicByte;
  96. }
  97. }
  98. private void SendPacket(ActionType actionType, object packetStruct)
  99. {
  100. try
  101. {
  102. header.ActionTypeNumber = (short)actionType;
  103. using (NetworkStream stream = client.GetStream())
  104. {
  105. stream.Write(magicByte, 0, magicByte.Length);
  106. Serializer.SerializeWithLengthPrefix<PacketHeader>(stream, header, PrefixStyle.Base128);
  107. switch (actionType)
  108. {
  109. case ActionType.Connect:
  110. Serializer.SerializeWithLengthPrefix<PacketConnect>(stream, (PacketConnect)packetStruct, PrefixStyle.Base128);
  111. break;
  112. }
  113. stream.Write(magicByte, 0, magicByte.Length);
  114. }
  115. }
  116. catch (Exception)
  117. {
  118. // error
  119. return;
  120. }
  121. }
  122. public void CreateNewCharacter(string characterName, CharacterClass chClass, CharacterRace chRace)
  123. {
  124. var info = new NewCharacterInfo();
  125. info.Name = characterName;
  126. info.OwnerUsername = Information.Username;
  127. info.Class = chClass;
  128. info.Race = chRace;
  129. CreateNewCharacter(info);
  130. }
  131. public void CreateNewCharacter(NewCharacterInfo info)
  132. {
  133. var packet = new PacketCreateNewCharacter();
  134. packet.chInfo = info;
  135. SendPacket(ActionType.CreateNewCharacter, packet);
  136. }
  137. public void ConnectUser(string username, string unhashedPassword)
  138. {
  139. var packet = new PacketConnect();
  140. packet.Username = username;
  141. packet.UnhashedPassword = unhashedPassword;
  142. ConnectUser(packet);
  143. }
  144. public void ConnectUser(PacketConnect packet)
  145. {
  146. if (_isLoggedCharacter || _isLoggedUser)
  147. {
  148. return;
  149. }
  150. else
  151. {
  152. SendPacket(ActionType.Connect, packet);
  153. }
  154. }
  155. public void ConnectCharacter(string characterName, short serverID)
  156. {
  157. var packet = new PacketLoginCharacter();
  158. packet.CharacterName = characterName;
  159. packet.ServerID = serverID;
  160. ConnectCharacter(packet);
  161. }
  162. public void ConnectCharacter(PacketLoginCharacter packet)
  163. {
  164. if (_isLoggedCharacter || !_isLoggedUser)
  165. {
  166. return;
  167. }
  168. else
  169. {
  170. SendPacket(ActionType.LoginCharacter, packet);
  171. }
  172. }
  173. public void Disconnect(PacketDisconnect packet)
  174. {
  175. if (!_isLoggedUser)
  176. {
  177. return;
  178. }
  179. else
  180. {
  181. SendPacket(ActionType.Disconnect, packet);
  182. }
  183. }
  184. }
  185. }
  186.  
  187. using System;
  188. using System.Collections.Generic;
  189. using System.Linq;
  190. using System.Text;
  191. using System.Net;
  192. using System.Net.Sockets;
  193. using System.Runtime.InteropServices;
  194.  
  195. namespace UnnamedGameServer
  196. {
  197. /// <summary>
  198. /// ALL CONSTANTS THAT ARE SHARED BY THE CLIENT AND THE SERVER ARE STORED HERE. DONT ADD MORE CLASSES THAT STORE CONSTANTS. Use GameConstants for game-only constants.
  199. /// </summary>
  200. public static class SharedConstants
  201. {
  202. /// <summary>
  203. /// Server information with port, IP and MagicByte
  204. /// </summary>
  205. public static class ServerInfo
  206. {
  207. public const short Port = 6483;
  208. public static readonly IPAddress ServerAddress = IPAddress.Loopback;
  209. public static byte[] MagicByte
  210. {
  211. get
  212. {
  213. return new byte[4] { 0xF1, 0x64, 0x83, 0xC4 };
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Character constants shared by client/server
  219. /// </summary>
  220. public static class CharacterConstants
  221. {
  222. public static class Movement
  223. {
  224. /// <summary>
  225. /// Coordinates per update
  226. /// </summary>
  227. public const float DefaultCoordinatePerUpdate = 8;
  228. public const float ModifierNormal = 1f;
  229. public const float ModifierFrozen = 0.8f;
  230. public const float ModifierSpeedBuff = 1.2f;
  231. public const float ModifierStop = 0f;
  232. }
  233. }
  234. /// <summary>
  235. /// Networking constants
  236. /// </summary>
  237. public static class Networking
  238. {
  239. public const int MilisecondsPerMovementUpdate = 100;
  240. public const ushort ActionTypeNonMetaActionStart = 0x0FFF + 1;
  241. /// <summary>
  242. /// What is the number of actions a non-logged user can perform
  243. /// </summary>
  244. public const ushort ActionTypeNonLoggedUser = 0x000F;
  245. }
  246. }
  247.  
  248. enum CharacterDirection
  249. {
  250. NoMovement = 0x00,
  251. Top = 0x01,
  252. TopRight = 0x02,
  253. TopLeft = 0x03,
  254. Right = 0x04,
  255. BottomRight = 0x05,
  256. Bottom = 0x06,
  257. BottomLeft = 0x07,
  258. Left = 0x08
  259. }
  260. enum CharacterStatus
  261. {
  262. Alive = 0x01,
  263. Dead = 0x02
  264. }
  265. enum CharacterClass
  266. {
  267. Mage = 0x01,
  268. Knight = 0x02
  269. }
  270. enum CharacterRace
  271. {
  272. Human = 0x01
  273. }
  274. enum CharacterType
  275. {
  276. Player = 0x01,
  277. GameMaster = 0x02
  278. }
  279. enum CharacterFaction
  280. {
  281. Newbie = 0x01,
  282. Army = 0x02,
  283. Chaos = 0x03
  284. }
  285. enum CharacterMovementStatus
  286. {
  287. Normal = 0x01,
  288. Frozen = 0x02,
  289. SpeedBuff = 0x03,
  290. Stop = 0x04
  291. }
  292. struct CharacterExperience
  293. {
  294. public CharacterExperience(short level, int experience)
  295. {
  296. Level = level;
  297. Experience = experience;
  298. }
  299.  
  300. public short Level;
  301. public int Experience;
  302. }
  303. }
  304.  
  305. namespace Unnamed_game
  306. {
  307. using UnamedGameServer;
  308. class Connection
  309.  
  310. using UnnamedGameServer;
  311.  
  312. class MyClass {
  313. byte[] b = SharedConstants.ServerInfo.MagicByte;
  314. }
Add Comment
Please, Sign In to add comment