More stuff

This commit is contained in:
EntenKoeniq
2021-09-30 23:35:42 +02:00
parent dfc6682e20
commit b6b64eba53
8 changed files with 69 additions and 14 deletions

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace CoopClient
{
@ -64,6 +66,19 @@ namespace CoopClient
return Main.LocalClientID;
}
public static Dictionary<long, int?> GetAllPlayers()
{
Dictionary<long, int?> result = new Dictionary<long, int?>();
lock (Main.Players)
{
foreach (KeyValuePair<long, Entities.EntitiesPlayer> player in Main.Players.Where(x => x.Key != Main.LocalClientID))
{
result.Add(player.Key, player.Value.Character?.Handle);
}
}
return result;
}
public static bool IsMenuVisible()
{
#if NON_INTERACTIVE
@ -91,7 +106,13 @@ namespace CoopClient
// Send bytes to all players
public static void SendDataToAll(string mod, byte customID, byte[] bytes)
{
Main.MainNetworking.SendModData(mod, customID, bytes);
Main.MainNetworking.SendModData(0, mod, customID, bytes);
}
// Send bytes to target
public static void SendDataToPlayer(long target, string mod, byte customID, byte[] bytes)
{
Main.MainNetworking.SendModData(target, mod, customID, bytes);
}
public static void Configure(string playerName, bool shareNpcsWithPlayers, int streamedNpcs, bool debug = false)

View File

@ -795,12 +795,13 @@ namespace CoopClient
#endif
}
public void SendModData(string mod, byte customID, byte[] bytes)
public void SendModData(long target, string mod, byte customID, byte[] bytes)
{
NetOutgoingMessage outgoingMessage = Client.CreateMessage();
new ModPacket()
{
ID = Main.LocalClientID,
Target = target,
Mod = mod,
CustomPacketID = customID,
Bytes = bytes

View File

@ -204,12 +204,15 @@ namespace CoopClient
public long ID { get; set; }
[ProtoMember(2)]
public string Mod { get; set; }
public long Target { get; set; }
[ProtoMember(3)]
public byte CustomPacketID { get; set; }
public string Mod { get; set; }
[ProtoMember(4)]
public byte CustomPacketID { get; set; }
[ProtoMember(5)]
public byte[] Bytes { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
@ -229,6 +232,7 @@ namespace CoopClient
ModPacket data = message.ReadBytes(len).Deserialize<ModPacket>();
ID = data.ID;
Target = data.Target;
Mod = data.Mod;
CustomPacketID = data.CustomPacketID;
Bytes = data.Bytes;

View File

@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Timers;
using System.Linq;
using CoopServer;
@ -33,7 +34,7 @@ namespace FirstGameMode
API.RegisterCommands<Commands>();
}
private void OnModPacketReceived(long from, string mod, byte customID, byte[] bytes, CancelEventArgs args)
private void OnModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes, CancelEventArgs args)
{
if (mod != "FirstScript" || customID != 1)
{
@ -46,7 +47,13 @@ namespace FirstGameMode
SetPlayerTime setPlayerTime = bytes.CDeserialize<SetPlayerTime>();
// Find the client by 'from' and send the time back as a nativecall
API.GetAllClients().Find(x => x.ID == from).SendNativeCall(0x47C3B5848C3E45D8, setPlayerTime.Hours, setPlayerTime.Minutes, setPlayerTime.Seconds);
//API.GetAllClients().Find(x => x.ID == from).SendNativeCall(0x47C3B5848C3E45D8, setPlayerTime.Hours, setPlayerTime.Minutes, setPlayerTime.Seconds);
// Find the client by 'target' and send the time back as a nativecall
Client targetClient = API.GetAllClients().FirstOrDefault(x => x.ID == target);
targetClient.SendChatMessage($"New modpacket nativecall from \"{API.GetAllClients().FirstOrDefault(x => x.ID == from)?.Player.Username}\"");
targetClient.SendNativeCall(0x47C3B5848C3E45D8, setPlayerTime.Hours, setPlayerTime.Minutes, setPlayerTime.Seconds);
}
public static void RunningCommand(CommandContext ctx)

View File

@ -50,7 +50,7 @@ namespace FirstScript
}
CoopClient.Interface.SendDataToAll("FirstScript", 0, CoopClient.CoopSerializer.CSerialize(new TestPacketClass() { A = 5, B = 15 }));
//CoopClient.Interface.SendDataToAll("FirstScript", 1, CoopClient.CoopSerializer.CSerialize(new SetPlayerTimePacket() { Hours = 0, Minutes = 0, Seconds = 0 }));
CoopClient.Interface.SendDataToPlayer(CoopClient.Interface.GetLocalID(), "FirstScript", 1, CoopClient.CoopSerializer.CSerialize(new SetPlayerTimePacket() { Hours = 1, Minutes = 2, Seconds = 3 }));
}
private void OnModPacketReceived(long from, string mod, byte customID, byte[] bytes)

View File

@ -137,12 +137,15 @@ namespace CoopServer
public long ID { get; set; }
[ProtoMember(2)]
public string Mod { get; set; }
public long Target { get; set; }
[ProtoMember(3)]
public byte CustomPacketID { get; set; }
public string Mod { get; set; }
[ProtoMember(4)]
public byte CustomPacketID { get; set; }
[ProtoMember(5)]
public byte[] Bytes { get; set; }
public override void PacketToNetOutGoingMessage(NetOutgoingMessage message)
@ -162,6 +165,7 @@ namespace CoopServer
ModPacket data = message.ReadBytes(len).Deserialize<ModPacket>();
ID = data.ID;
Target = data.Target;
Mod = data.Mod;
CustomPacketID = data.CustomPacketID;
Bytes = data.Bytes;

View File

@ -291,14 +291,32 @@ namespace CoopServer
ModPacket modPacket = (ModPacket)packet;
if (GameMode != null)
{
if (GameMode.API.InvokeModPacketReceived(modPacket.ID, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes))
if (GameMode.API.InvokeModPacketReceived(modPacket.ID, modPacket.Target, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes))
{
break;
}
}
NetOutgoingMessage outgoingMessage;
NetConnection target;
if (modPacket.Target != 0)
{
target = MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == modPacket.Target);
if (target == null)
{
Logging.Error($"[ModPacket] target \"{modPacket.Target}\" not found!");
return;
}
// Send back to target
outgoingMessage = MainNetServer.CreateMessage();
modPacket.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, target, NetDeliveryMethod.ReliableOrdered, 0);
}
// Send back to all players
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
outgoingMessage = MainNetServer.CreateMessage();
modPacket.PacketToNetOutGoingMessage(outgoingMessage);
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
}

View File

@ -17,7 +17,7 @@ namespace CoopServer
#region DELEGATES
public delegate void ChatEvent(string username, string message, CancelEventArgs cancel);
public delegate void PlayerEvent(Client client);
public delegate void ModEvent(long from, string mod, byte customID, byte[] bytes, CancelEventArgs args);
public delegate void ModEvent(long from, long target, string mod, byte customID, byte[] bytes, CancelEventArgs args);
#endregion
#region EVENTS
@ -55,10 +55,10 @@ namespace CoopServer
OnPlayerPositionUpdate?.Invoke(Server.Clients.First(x => x.Player.Username == playerData.Username));
}
internal bool InvokeModPacketReceived(long from, string mod, byte customID, byte[] bytes)
internal bool InvokeModPacketReceived(long from, long target, string mod, byte customID, byte[] bytes)
{
CancelEventArgs args = new(false);
OnModPacketReceived?.Invoke(from, mod, customID, bytes, args);
OnModPacketReceived?.Invoke(from, target, mod, customID, bytes, args);
return args.Cancel;
}
#endregion