public static void StartMWOConnection() { if (!Connected) { MW_Online.ShowLoading(); try { tcpServer = new TcpClient(); tcpServer.Connect(connectIp, connectPort); tcpServer.SendTimeout = 30000; MW_Online.InitializeCarSlots(); Connected = true; MsgWorker.StartWorker(); SendToServer(NickName); MW_Online.HideLoading(); } catch (Exception ex) { string m = String.Format("Connection error: {0}", ex.Message); Log.Print("MW-Online", m + "\r\n" + ex.ToString()); MW_Online.HideLoading(); Thread.Sleep(1000); GameDialog.ShowStableDialog(0, m); } } else { UI.ShowTextScreenMessage("Sorry, but you need to disconnect firstly.\nPress N to disconnect!"); } }
// This method is called when a key is released. public override void OnKeyUp(Keys key) { if (key == Keys.Enter || key == Keys.Escape) { if (GameDialog.shown) { new Thread(unPause).Start(); } } if (key == Keys.N && Game.isGameplayActive && Connection.Connected) { new Thread(showMm).Start(); } if (GameDialog.isInMn) { if (key == Keys.Up) { GameDialog.MenuIdx--; if (GameDialog.MenuIdx < 0) { GameDialog.MenuIdx = GameDialog.MenuText.Length; } GameDialog.ShowMenu(); } if (key == Keys.Down) { GameDialog.MenuIdx++; if (GameDialog.MenuIdx >= GameDialog.MenuText.Length) { GameDialog.MenuIdx = 0; } GameDialog.ShowMenu(); } return; } if (key == Keys.O) { //HOTPOSITION: X, Y, Z Angle = 0xcfec Speed = 15 // WriteMemory<unsigned char>(0x9B090C, 0x01, true); //PosC.Set(44 * 4 * 1, Player.Position.x, Player.Position.z); //MathFuncs.TeleportTo(new Vector3(GameMemory.memory.ReadFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_X + 44 * 4 * 1), GameMemory.memory.ReadFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_Y + 44 * 4 * 1), GameMemory.memory.ReadFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_Z + 44 * 4 * 1))); } if (key == Keys.P) { HideLoading(); } if (key == Keys.J) { Player.Position = new NFSScript.Math.Vector3(Player.Position.x, Player.Position.y + 10, Player.Position.z); } /* * if (key == Keys.U) * { * if (!StartWithGameplay) * { * new Thread(startGame).Start(); * } * * * } */ }
private static void MWOnlineSynchronize() { Thread.Sleep(1500); Log.Print("MW-Online", "Syncing with server!"); while (Connection.Connected) { while (PauseSync) { } try { if (!MathFuncs.PlayerToPoint(0.05f, Player.Position, SyncOld_P.oldPos)) { Connection.SendToServer(String.Format("PlayerPacket#{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#{8}#{9}", Player.Position.x, //1 Player.Position.y, //2 Player.Position.z, //3 Player.Rotation.x, //4 Player.Rotation.y, //5 Player.Rotation.z, //6 Player.Rotation.w, //7 GameMemory.memory.ReadFloat((IntPtr)0x9386F0) /*8*/, GameMemory.memory.ReadFloat((IntPtr)0x9386E8) /*9*/, GameMemory.memory.ReadFloat((IntPtr)NewAddresses.SPIN) )); } SyncOld_P.SetNewOld(Player.Position, Player.Rotation); Thread.Sleep(10);// 50 ms is more stable than 100 because its game, not only app } catch (Exception ex) { string m = String.Format("Sync error: {0}", ex.Message); Log.Print("MW-Online", m); GameDialog.ShowStableDialog(0, m); Connection.CloseConnection(); break; } } }
private static void msgWork() { try { Connection.srReceiver = new StreamReader(Connection.tcpServer.GetStream()); Log.Print("MW-Online", "Accepting messages from server!"); while (true) { try { string msg = Privatcom_Basic_Server.Base64.Decrypt(Connection.srReceiver.ReadLine()).Replace("~n~", "\n"); writer.WriteLine(msg); writer.Flush(); //Llog("MW-Online", "From server: " + msg); if (msg.StartsWith("SetAI#")) { string[] g = msg.Split('#'); int mem = int.Parse(g[1]); string t = g[2]; } if (msg.StartsWith("ShowUpdateForm")) { new UpdateForm().ShowDialog(); } if (msg.StartsWith("SetPos#")) { string[] s = msg.Split('#'); // x1#y2#z3 float x = float.Parse(s[1]); float y = float.Parse(s[2]); float z = float.Parse(s[3]); StreamWriter wr = new StreamWriter("TRACKS\\HotPositionL2RA.HOT"); wr.Write(String.Format("HOTPOSITION: {0}, {1}, {2} Angle = 0xcfec Speed = {3}", x, y, z, 0)); wr.Flush(); wr.Close(); wr.Dispose(); GameMemory.memory.WriteInt32((IntPtr)0x9B090C, 0x01); } if (msg.StartsWith("ShowListDialog#")) { string[] s = msg.Split('#'); // s[1] = response // s[2] = line~n~line2~n~line3 = lines GameDialog.MenuResponse = s[1]; GameDialog.MenuIdx = 0; GameDialog.MenuText = s[2].Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); GameDialog.ShowMenu(); } if (msg.StartsWith("Disconnect")) { Connection.CloseConnection(); GameDialog.ShowStableDialog(0, "Disconnected..."); } if (msg.StartsWith("ShowDialog#")) { string[] s = msg.Split('#'); // s[0] = ShowDialog // s[1] = Type. Ex: 1 // s[2] = Text. int type = int.Parse(s[1]); string text = s[2]; GameDialog.ShowStableDialog(type, text); } if (msg.StartsWith("ShowText#")) { string[] s = msg.Split('#'); // s[0] = ShowTtext // s[1] = Text string text = s[1]; UI.ShowTextScreenMessage(text); } if (msg.StartsWith("PlayerUpdate#") && !Sync.PauseSync) { writer.WriteLine(msg); writer.Flush(); string[] s = msg.Split('#'); // PlayerUpdate0#id1#posx2#posy3#posz4#rotx5#roty6#rotz7 int player_id = int.Parse(s[1]); float posx = float.Parse(s[2]); float posy = float.Parse(s[3]); float posz = float.Parse(s[4]); float rotx = float.Parse(s[5]); float roty = float.Parse(s[6]); float rotz = float.Parse(s[7]); float rotw = float.Parse(s[8]); float speedx = float.Parse(s[9]); float speedy = float.Parse(s[10]); float spn = float.Parse(s[11]); if (!float.IsNaN(posx) && !float.IsNaN(posy) && !float.IsNaN(posz) && !float.IsNaN(rotx) && !float.IsNaN(roty) && !float.IsNaN(rotz) && !float.IsNaN(speedx) && !float.IsNaN(speedy) && !float.IsNaN(spn)) { //Llog("MW-Online", "Updating player ID: " + player_id); // Llog("MW-Online", String.Format("PosX: {0}, PosY = {1}, PosZ = {2}", posx, posy, posz)); //Llog("MW-Online", String.Format("RotX: {0}, RotY = {1}, RotZ = {2}", rotx, roty, rotz)); // PosC.Set(44 * 4 * player_id, posx, posz); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_X + 44 * 4 * player_id, posx); //GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_Y + 44 * 4 * player_id, 0); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_POS_Z + 44 * 4 * player_id, posz); //GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_SPEED + 44 * 4 * player_id, spd); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_X_ROT + 44 * 4 * player_id, rotx); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_Y_ROT + 44 * 4 * player_id, roty); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_Z_ROT + 44 * 4 * player_id, rotz); GameMemory.memory.WriteFloat((IntPtr)MWAddresses.PlayerAddrs.STATIC_PLAYER_W_ROT + 44 * 4 * player_id, rotw); GameMemory.memory.WriteFloat((IntPtr)0x9386F0 + 44 * 4 * player_id, speedx); GameMemory.memory.WriteFloat((IntPtr)0x9386E8 + 44 * 4 * player_id, speedy); GameMemory.memory.WriteFloat((IntPtr)NewAddresses.SPIN + 44 * 4 * player_id, spn); } } if (msg.StartsWith("MeetUp#")) { string[] s = msg.Split('#'); UI.ShowTextScreenMessage(String.Format("MEET UP^^{0}^Drive there and evade the pursuit!", s[4])); } if (msg.StartsWith("911Call")) //Server wants to play the pursuit :D { Function.Call(MWFunctions.SHOW_RACE_COUNTDOWN); Thread.Sleep(4000); Function.Call(MWFunctions.FORCE_PURSUIT_START, 50, 5); } } catch (Exception ex) { string m = String.Format("Connection error: {0}", ex.Message); Log.Print("MW-Online", m); GameDialog.ShowStableDialog(0, m); Connection.CloseConnection(); break; } } } catch { } }