mirror of
https://github.com/LorisYounger/VPet.git
synced 2024-08-30 18:42:36 +00:00
怎么P2P接口也是有问题的,到底啥接口能用
This commit is contained in:
parent
03f8da9b4c
commit
c6272cdd2a
@ -11,8 +11,9 @@ namespace VPet_Simulator.Windows.Interface;
|
||||
/// <summary>
|
||||
/// 多人模式传输的消息
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public struct MPMessage
|
||||
{
|
||||
{
|
||||
/// <summary>
|
||||
/// 消息类型
|
||||
/// </summary>
|
||||
@ -53,6 +54,6 @@ public struct MPMessage
|
||||
/// </summary>
|
||||
public ulong To;
|
||||
|
||||
public static string ConverTo(MPMessage data) => LPSConvert.SerializeObject(data).ToString();
|
||||
public static MPMessage ConverTo(string data) => LPSConvert.DeserializeObject<MPMessage>(new LPS(data));
|
||||
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)));
|
||||
}
|
||||
|
@ -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<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,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;
|
||||
/// <summary>
|
||||
/// 显示默认动画
|
||||
/// </summary>
|
||||
private void DisplayMPNomal()
|
||||
{
|
||||
Core.Save = GameSave_VPet.Load(new Line(lb.GetMemberData(friend, "save")));
|
||||
|
||||
if (DisplayGraph(LPSConvert.DeserializeObject<GraphInfo>(new LPS(lb.GetMemberData(friend, "display"))))) return;
|
||||
|
||||
Main.CountNomal++;
|
||||
Main.Display(GraphType.Default, AnimatType.Single, DisplayMPNomal);
|
||||
}
|
||||
public new bool Loaded = false;
|
||||
|
||||
/// <summary>
|
||||
/// 显示捏脸情况
|
||||
@ -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;
|
||||
|
@ -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<GraphInfo>(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)
|
||||
|
Loading…
Reference in New Issue
Block a user