From c6272cdd2a3eb61d6de3f9830204bfd2cdd8175e Mon Sep 17 00:00:00 2001 From: ZouJin Date: Mon, 18 Mar 2024 02:22:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=8E=E4=B9=88P2P=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B9=9F=E6=98=AF=E6=9C=89=E9=97=AE=E9=A2=98=E7=9A=84,?= =?UTF-8?q?=E5=88=B0=E5=BA=95=E5=95=A5=E6=8E=A5=E5=8F=A3=E8=83=BD=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet-Simulator.Windows.Interface/MPMessage.cs | 7 ++- .../MutiPlayer/MPFriends.xaml.cs | 45 +++++---------- .../MutiPlayer/winMutiPlayer.xaml.cs | 55 ++++++++++++++++--- 3 files changed, 66 insertions(+), 41 deletions(-) diff --git a/VPet-Simulator.Windows.Interface/MPMessage.cs b/VPet-Simulator.Windows.Interface/MPMessage.cs index 6313ec9..336727f 100644 --- a/VPet-Simulator.Windows.Interface/MPMessage.cs +++ b/VPet-Simulator.Windows.Interface/MPMessage.cs @@ -11,8 +11,9 @@ namespace VPet_Simulator.Windows.Interface; /// /// 多人模式传输的消息 /// +[Serializable] public struct MPMessage -{ +{ /// /// 消息类型 /// @@ -53,6 +54,6 @@ public struct MPMessage /// public ulong To; - public static string ConverTo(MPMessage data) => LPSConvert.SerializeObject(data).ToString(); - public static MPMessage ConverTo(string data) => LPSConvert.DeserializeObject(new LPS(data)); + public static byte[] ConverTo(MPMessage data) => Encoding.UTF8.GetBytes(LPSConvert.SerializeObject(data).ToString()); + public static MPMessage ConverTo(byte[] data) => LPSConvert.DeserializeObject(new LPS(Encoding.UTF8.GetString(data))); } diff --git a/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs b/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs index c0c37ae..71d6b84 100644 --- a/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs +++ b/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs @@ -58,7 +58,6 @@ public partial class MPFriends : WindowX mw.Windows.Add(this); try { - InitializeComponent(); //MGrid.Height = 500 * mw.Set.ZoomLevel; @@ -119,23 +118,20 @@ public partial class MPFriends : WindowX Close(); return; } - - - ImageSources.AddRange(mw.ImageSources); - - - //加载所有MOD - List Path = new List(); - Path.AddRange(new DirectoryInfo(mw.ModPath).EnumerateDirectories()); - - var workshop = mw.Set["workshop"]; - foreach (Sub ws in workshop) - { - Path.Add(new DirectoryInfo(ws.Name)); - } - Task.Run(async () => { + ImageSources.AddRange(mw.ImageSources); + + //加载所有MOD + List Path = new List(); + Path.AddRange(new DirectoryInfo(mw.ModPath).EnumerateDirectories()); + + var workshop = mw.Set["workshop"]; + foreach (Sub ws in workshop) + { + Path.Add(new DirectoryInfo(ws.Name)); + } + //加载lobby传过来的数据 string tmp = lb.GetMemberData(friend, "save"); while (string.IsNullOrEmpty(tmp)) @@ -205,7 +201,6 @@ public partial class MPFriends : WindowX Core.Graph = petloader.Graph(mw.Set.Resolution); Main = new Main(Core); - Main.DisplayNomal = DisplayMPNomal; Main.EventTimer.AutoReset = false; Main.EventTimer.Enabled = false; @@ -244,19 +239,7 @@ public partial class MPFriends : WindowX Loaded = true; })); } - public new bool Loaded = false; - /// - /// 显示默认动画 - /// - private void DisplayMPNomal() - { - Core.Save = GameSave_VPet.Load(new Line(lb.GetMemberData(friend, "save"))); - - if (DisplayGraph(LPSConvert.DeserializeObject(new LPS(lb.GetMemberData(friend, "display"))))) return; - - Main.CountNomal++; - Main.Display(GraphType.Default, AnimatType.Single, DisplayMPNomal); - } + public new bool Loaded = false; /// /// 显示捏脸情况 @@ -343,7 +326,7 @@ public partial class MPFriends : WindowX var img = Core.Graph.FindGraph(gi.Name, gi.Animat, Core.Save.Mode); if (img != null) { - Main.Display(img, DisplayMPNomal); + Main.Display(img); return true; } return false; diff --git a/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs b/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs index 8d807ce..66be631 100644 --- a/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs +++ b/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs @@ -123,6 +123,11 @@ public partial class winMutiPlayer : Window lbLid.Text = lb.Id.Value.ToString("x"); Steamworks.Data.Image? img = await lb.Owner.GetMediumAvatarAsync(); HostHead.Source = ConvertToImageSource(img.Value); + SteamNetworking.AllowP2PPacketRelay(true); + SteamNetworking.OnP2PSessionRequest = (steamid) => + { + SteamNetworking.AcceptP2PSessionWithUser(steamid); + }; //给自己动画添加绑定 mw.Main.GraphDisplayHandler += Main_GraphDisplayHandler; @@ -141,7 +146,6 @@ public partial class winMutiPlayer : Window var mpuc = new MPUserControl(this, mpf); MUUCList.Children.Add(mpuc); MPUserControls.Add(mpuc); - SteamNetworking.AcceptP2PSessionWithUser(v.Id); if (v.Id == lb.Owner.Id) _ = Task.Run(() => { @@ -157,6 +161,7 @@ public partial class winMutiPlayer : Window }); }); } + _ = Task.Run(LoopP2PPacket); } private void SteamMatchmaking_OnLobbyMemberLeave(Lobby lobby, Friend friend) @@ -175,6 +180,7 @@ public partial class winMutiPlayer : Window MPUserControls.Remove(mpuc); MUUCList.Children.Remove(mpuc); MPFriends.Remove(mpuc.mpf); + mpuc.mpf.Quit(); } } } @@ -186,14 +192,28 @@ public partial class winMutiPlayer : Window { return; } - lb.SetMemberData("display", LPSConvert.SerializeObject(info).ToString()); + MPMessage msg = new MPMessage(); + msg.Type = MPMessage.MSGType.DispayGraph; + msg.Content = LPSConvert.SerializeObject(info).ToString(); + msg.To = SteamClient.SteamId.Value; + byte[] data = MPMessage.ConverTo(msg); + for (int i = 0; i < MPFriends.Count; i++) + { + MPFriends v = MPFriends[i]; + SteamNetworking.SendP2PPacket(v.friend.Id, data); + } } private void SteamMatchmaking_OnLobbyMemberJoined(Lobby lobby, Friend friend) { if (lobby.Id == lb.Id) { - + var mpf = new MPFriends(this, mw, lb, friend); + MPFriends.Add(mpf); + mpf.Show(); + var mpuc = new MPUserControl(this, mpf); + MUUCList.Children.Add(mpuc); + MPUserControls.Add(mpuc); } } @@ -205,13 +225,32 @@ public partial class winMutiPlayer : Window } } - + private void LoopP2PPacket() + { + while (SteamNetworking.IsP2PPacketAvailable() && isOPEN) + { + var packet = SteamNetworking.ReadP2PPacket(); + if (packet.HasValue) + { + var From = packet.Value.SteamId; + var MSG = MPMessage.ConverTo(packet.Value.Data); + var TO = MPFriends.Find(x => x.friend.Id == MSG.To); + switch (MSG.Type) + { + case MPMessage.MSGType.DispayGraph: + var info = LPSConvert.DeserializeObject(new LPS(MSG.Content)); + TO.DisplayGraph(info); + break; + } + } + } + Thread.Sleep(100); + LoopP2PPacket(); + } private void Window_Closed(object sender, EventArgs e) { SteamMatchmaking.OnLobbyDataChanged -= SteamMatchmaking_OnLobbyDataChanged; mw.Main.GraphDisplayHandler -= Main_GraphDisplayHandler; - if (lb.Owner.Id == SteamClient.SteamId) - lb.SetData("leave", "true"); lb.Leave(); for (int i = 0; i < MPFriends.Count; i++) { @@ -219,14 +258,16 @@ public partial class winMutiPlayer : Window } mw.winMutiPlayer = null; } - + bool isOPEN = true; private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { if (!lb.Equals(default(Lobby))) if (MessageBoxX.Show("确定要关闭访客表吗?".Translate(), "离开游戏", MessageBoxButton.YesNo) != MessageBoxResult.Yes) { e.Cancel = true; + return; } + isOPEN = false; } private void swAllowJoin_Checked(object sender, RoutedEventArgs e)