From c5047a75812f09a14aa9c248494714982df76635 Mon Sep 17 00:00:00 2001 From: ZouJin Date: Mon, 18 Mar 2024 14:59:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8P2P=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BC=A0=E8=BE=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet-Simulator.Windows.Interface/MPMessage.cs | 11 +- .../MutiPlayer/MPFriends.xaml.cs | 39 +++--- .../MutiPlayer/winMutiPlayer.xaml.cs | 127 ++++++++++-------- 3 files changed, 99 insertions(+), 78 deletions(-) diff --git a/VPet-Simulator.Windows.Interface/MPMessage.cs b/VPet-Simulator.Windows.Interface/MPMessage.cs index 336727f..ac4658e 100644 --- a/VPet-Simulator.Windows.Interface/MPMessage.cs +++ b/VPet-Simulator.Windows.Interface/MPMessage.cs @@ -11,7 +11,6 @@ namespace VPet_Simulator.Windows.Interface; /// /// 多人模式传输的消息 /// -[Serializable] public struct MPMessage { /// @@ -19,6 +18,10 @@ public struct MPMessage /// public enum MSGType { + /// + /// 一般是出错或者空消息 + /// + Empty, /// /// 聊天消息 (string) /// @@ -43,16 +46,16 @@ public struct MPMessage /// /// 消息类型 /// - public MSGType Type; + [Line] public MSGType Type { get; set; } /// /// 消息内容 /// - public string Content; + [Line] public string Content { get; set; } /// /// 被操作者 (显示动画用) /// - public ulong To; + [Line] public ulong To { get; set; } 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 71d6b84..c4ce028 100644 --- a/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs +++ b/VPet-Simulator.Windows/MutiPlayer/MPFriends.xaml.cs @@ -118,20 +118,22 @@ 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,9 +207,9 @@ public partial class MPFriends : WindowX Main.EventTimer.Enabled = false; //清空资源 - Main.Resources = Application.Current.Resources; - Main.MsgBar.This.Resources = Application.Current.Resources; - Main.ToolBar.Resources = Application.Current.Resources; + //Main.Resources = Application.Current.Resources; + //Main.MsgBar.This.Resources = Application.Current.Resources; + //Main.ToolBar.Resources = Application.Current.Resources; Main.ToolBar.LoadClean(); LoadingText.Content = "正在加载游戏\n该步骤可能会耗时比较长\n请耐心等待".Translate(); @@ -216,13 +218,12 @@ public partial class MPFriends : WindowX DisplayGrid.Child = Main; - Main.SetMoveMode(mw.Set.AllowMove, mw.Set.SmartMove, mw.Set.SmartMoveInterval * 1000); - Main.SetLogicInterval(1500); + //Main.SetMoveMode(mw.Set.AllowMove, mw.Set.SmartMove, mw.Set.SmartMoveInterval * 1000); + //Main.SetLogicInterval(1500); if (mw.Set.MessageBarOutside) Main.MsgBar.SetPlaceOUT(); - Main.WorkCheck = mw.WorkCheck; - + //Main.WorkCheck = mw.WorkCheck; //添加捏脸动画(若有) if (Core.Graph.GraphConfig.Data.ContainsLine("pinch")) diff --git a/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs b/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs index 66be631..65981c5 100644 --- a/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs +++ b/VPet-Simulator.Windows/MutiPlayer/winMutiPlayer.xaml.cs @@ -19,7 +19,6 @@ using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; -using System.Windows.Shapes; using VPet_Simulator.Core; using VPet_Simulator.Windows.Interface; using static VPet_Simulator.Core.GraphInfo; @@ -109,59 +108,74 @@ public partial class winMutiPlayer : Window return result; } ulong owner; - public async void ShowLobbyInfo() + public void ShowLobbyInfo() { - lb.SetMemberData("save", mw.GameSavesData.GameSave.ToLine().ToString()); - lb.SetMemberData("onmod", mw.Set.FindLine("onmod")?.ToString() ?? "onmod"); - lb.SetMemberData("petgraph", mw.Set.PetGraph); + _ = Task.Run(async () => + { + lb.SetMemberData("save", mw.GameSavesData.GameSave.ToLine().ToString()); + lb.SetMemberData("onmod", mw.Set.FindLine("onmod")?.ToString() ?? "onmod"); + lb.SetMemberData("petgraph", mw.Set.PetGraph); - SteamMatchmaking.OnLobbyDataChanged += SteamMatchmaking_OnLobbyDataChanged; - SteamMatchmaking.OnLobbyMemberJoined += SteamMatchmaking_OnLobbyMemberJoined; - SteamMatchmaking.OnLobbyMemberLeave += SteamMatchmaking_OnLobbyMemberLeave; - hostName.Text = lb.Owner.Name; - owner = lb.Owner.Id.Value; - 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); - }; + SteamMatchmaking.OnLobbyDataChanged += SteamMatchmaking_OnLobbyDataChanged; + SteamMatchmaking.OnLobbyMemberJoined += SteamMatchmaking_OnLobbyMemberJoined; + SteamMatchmaking.OnLobbyMemberLeave += SteamMatchmaking_OnLobbyMemberLeave; + Steamworks.Data.Image? img = await lb.Owner.GetMediumAvatarAsync(); - //给自己动画添加绑定 - mw.Main.GraphDisplayHandler += Main_GraphDisplayHandler; - if (lb.Owner.IsMe) - { - hostPet.Text = mw.GameSavesData.GameSave.Name; - Title = "{0}的访客表".Translate(mw.GameSavesData.GameSave.Name); - } - //获取成员列表 - foreach (var v in lb.Members) - { - if (v.Id == SteamClient.SteamId) continue; - var mpf = new MPFriends(this, mw, lb, v); - MPFriends.Add(mpf); - mpf.Show(); - var mpuc = new MPUserControl(this, mpf); - MUUCList.Children.Add(mpuc); - MPUserControls.Add(mpuc); - if (v.Id == lb.Owner.Id) - _ = Task.Run(() => + Dispatcher.Invoke(() => + { + hostName.Text = lb.Owner.Name; + owner = lb.Owner.Id.Value; + lbLid.Text = lb.Id.Value.ToString("x"); + HostHead.Source = ConvertToImageSource(img.Value); + }); + + SteamNetworking.AllowP2PPacketRelay(true); + SteamNetworking.OnP2PSessionRequest = (steamid) => + { + SteamNetworking.AcceptP2PSessionWithUser(steamid); + }; + + //给自己动画添加绑定 + mw.Main.GraphDisplayHandler += Main_GraphDisplayHandler; + if (lb.Owner.IsMe) + { + Dispatcher.Invoke(() => { - //加载lobby传过来的数据 - while (!mpf.Loaded) - { - Thread.Sleep(500); - } - Dispatcher.Invoke(() => - { - Title = "{0}的访客表".Translate(mpf.Core.Save.Name); - hostPet.Text = mpf.Core.Save.Name; - }); + hostPet.Text = mw.GameSavesData.GameSave.Name; + Title = "{0}的访客表".Translate(mw.GameSavesData.GameSave.Name); }); - } - _ = Task.Run(LoopP2PPacket); + } + //获取成员列表 + foreach (var v in lb.Members) + { + if (v.Id == SteamClient.SteamId) continue; + var mpf = Dispatcher.Invoke(() => + { + var mpf = new MPFriends(this, mw, lb, v); + MPFriends.Add(mpf); + mpf.Show(); + var mpuc = new MPUserControl(this, mpf); + MUUCList.Children.Add(mpuc); + MPUserControls.Add(mpuc); + return mpf; + }); + if (v.Id == lb.Owner.Id) + _ = Task.Run(() => + { + //加载lobby传过来的数据 + while (!mpf.Loaded) + { + Thread.Sleep(500); + } + Dispatcher.Invoke(() => + { + Title = "{0}的访客表".Translate(mpf.Core.Save.Name); + hostPet.Text = mpf.Core.Save.Name; + }); + }); + } + LoopP2PPacket(); + }); } private void SteamMatchmaking_OnLobbyMemberLeave(Lobby lobby, Friend friend) @@ -194,7 +208,7 @@ public partial class winMutiPlayer : Window } MPMessage msg = new MPMessage(); msg.Type = MPMessage.MSGType.DispayGraph; - msg.Content = LPSConvert.SerializeObject(info).ToString(); + msg.Content = LPSConvert.GetObjectString(info, convertNoneLineAttribute: true); msg.To = SteamClient.SteamId.Value; byte[] data = MPMessage.ConverTo(msg); for (int i = 0; i < MPFriends.Count; i++) @@ -227,25 +241,28 @@ public partial class winMutiPlayer : Window } private void LoopP2PPacket() { - while (SteamNetworking.IsP2PPacketAvailable() && isOPEN) + while (SteamNetworking.IsP2PPacketAvailable()) { var packet = SteamNetworking.ReadP2PPacket(); if (packet.HasValue) { var From = packet.Value.SteamId; + var test = Encoding.UTF8.GetString(packet.Value.Data); 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); + TO.DisplayGraph((GraphInfo)LPSConvert.GetStringObject(MSG.Content, typeof(GraphInfo), convertNoneLineAttribute: true)); break; } } + Thread.Sleep(100); } - Thread.Sleep(100); - LoopP2PPacket(); + Thread.Sleep(1000); + if (isOPEN) + LoopP2PPacket(); + } private void Window_Closed(object sender, EventArgs e) {