mirror of
https://github.com/LorisYounger/VPet.git
synced 2024-08-30 18:42:36 +00:00
使用P2P数据传输
This commit is contained in:
parent
c6272cdd2a
commit
c5047a7581
@ -11,7 +11,6 @@ namespace VPet_Simulator.Windows.Interface;
|
||||
/// <summary>
|
||||
/// 多人模式传输的消息
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public struct MPMessage
|
||||
{
|
||||
/// <summary>
|
||||
@ -19,6 +18,10 @@ public struct MPMessage
|
||||
/// </summary>
|
||||
public enum MSGType
|
||||
{
|
||||
/// <summary>
|
||||
/// 一般是出错或者空消息
|
||||
/// </summary>
|
||||
Empty,
|
||||
/// <summary>
|
||||
/// 聊天消息 (string)
|
||||
/// </summary>
|
||||
@ -43,16 +46,16 @@ public struct MPMessage
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
public MSGType Type;
|
||||
[Line] public MSGType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 消息内容
|
||||
/// </summary>
|
||||
public string Content;
|
||||
[Line] public string Content { get; set; }
|
||||
/// <summary>
|
||||
/// 被操作者 (显示动画用)
|
||||
/// </summary>
|
||||
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<MPMessage>(new LPS(Encoding.UTF8.GetString(data)));
|
||||
|
@ -118,20 +118,22 @@ public partial class MPFriends : WindowX
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
ImageSources.AddRange(mw.ImageSources);
|
||||
|
||||
|
||||
//加载所有MOD
|
||||
List<DirectoryInfo> Path = new List<DirectoryInfo>();
|
||||
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<DirectoryInfo> Path = new List<DirectoryInfo>();
|
||||
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"))
|
||||
|
@ -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<GraphInfo>(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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user