Newtonsoft.Json added. CoopSerializer updated. Bugfix for ModPackets. Interface renamed to COOPAPI. SendDataToServer() and GetLocalUsername() added
This commit is contained in:
@ -4,7 +4,7 @@ using System.Linq;
|
||||
|
||||
namespace CoopClient
|
||||
{
|
||||
public static class Interface
|
||||
public static class COOPAPI
|
||||
{
|
||||
#region DELEGATES
|
||||
public delegate void ConnectEvent(bool connected, long fromId, string reason = null);
|
||||
@ -120,6 +120,12 @@ namespace CoopClient
|
||||
return Main.CurrentVersion;
|
||||
}
|
||||
|
||||
// Send bytes to server
|
||||
public static void SendDataToServer(string mod, byte customID, byte[] bytes)
|
||||
{
|
||||
Main.MainNetworking.SendModData(-1, mod, customID, bytes);
|
||||
}
|
||||
|
||||
// Send bytes to all players
|
||||
public static void SendDataToAll(string mod, byte customID, byte[] bytes)
|
||||
{
|
||||
@ -132,6 +138,11 @@ namespace CoopClient
|
||||
Main.MainNetworking.SendModData(target, mod, customID, bytes);
|
||||
}
|
||||
|
||||
public static string GetLocalUsername()
|
||||
{
|
||||
return Main.MainSettings.Username;
|
||||
}
|
||||
|
||||
public static void Configure(string playerName, bool shareNpcsWithPlayers, int streamedNpcs, bool debug = false)
|
||||
{
|
||||
Main.MainSettings.Username = playerName;
|
@ -41,6 +41,9 @@
|
||||
<Reference Include="Lidgren.Network">
|
||||
<HintPath>..\Libs\Release\Lidgren.Network.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\Libs\Release\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="protobuf-net, Version=2.4.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\protobuf-net.2.4.6\lib\net40\protobuf-net.dll</HintPath>
|
||||
</Reference>
|
||||
@ -65,7 +68,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Chat.cs" />
|
||||
<Compile Include="Interface.cs" />
|
||||
<Compile Include="COOPAPI.cs" />
|
||||
<Compile Include="Entities\EntitiesNpc.cs" />
|
||||
<Compile Include="Entities\EntitiesPed.cs" />
|
||||
<Compile Include="Entities\EntitiesPlayer.cs" />
|
||||
|
@ -137,7 +137,7 @@ namespace CoopClient
|
||||
Main.MainMenu.ConnectedMenuSetting();
|
||||
#endif
|
||||
|
||||
Interface.Connected();
|
||||
COOPAPI.Connected();
|
||||
GTA.UI.Notification.Show("~g~Connected!");
|
||||
}
|
||||
break;
|
||||
@ -157,7 +157,7 @@ namespace CoopClient
|
||||
Main.MainMenu.DisconnectedMenuSetting();
|
||||
#endif
|
||||
|
||||
Interface.Disconnected(reason);
|
||||
COOPAPI.Disconnected(reason);
|
||||
GTA.UI.Notification.Show("~r~Disconnected: " + reason);
|
||||
break;
|
||||
}
|
||||
@ -219,7 +219,7 @@ namespace CoopClient
|
||||
packet.NetIncomingMessageToPacket(message);
|
||||
|
||||
ChatMessagePacket chatMessagePacket = (ChatMessagePacket)packet;
|
||||
if (!Interface.ChatMessageReceived(chatMessagePacket.Username, chatMessagePacket.Message))
|
||||
if (!COOPAPI.ChatMessageReceived(chatMessagePacket.Username, chatMessagePacket.Message))
|
||||
{
|
||||
Main.MainChat.AddMessage(chatMessagePacket.Username, chatMessagePacket.Message);
|
||||
}
|
||||
@ -233,7 +233,7 @@ namespace CoopClient
|
||||
packet = new ModPacket();
|
||||
packet.NetIncomingMessageToPacket(message);
|
||||
ModPacket modPacket = (ModPacket)packet;
|
||||
Interface.ModPacketReceived(modPacket.ID, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes);
|
||||
COOPAPI.ModPacketReceived(modPacket.ID, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@ -265,7 +265,7 @@ namespace CoopClient
|
||||
};
|
||||
|
||||
Main.Players.Add(packet.ID, player);
|
||||
Interface.Connected(packet.ID);
|
||||
COOPAPI.Connected(packet.ID);
|
||||
}
|
||||
|
||||
private void PlayerDisconnect(PlayerDisconnectPacket packet)
|
||||
@ -281,7 +281,7 @@ namespace CoopClient
|
||||
|
||||
player.PedBlip?.Delete();
|
||||
|
||||
Interface.Disconnected(packet.ID);
|
||||
COOPAPI.Disconnected(packet.ID);
|
||||
Main.Players.Remove(packet.ID);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
|
||||
using Lidgren.Network;
|
||||
using ProtoBuf;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using GTA.Math;
|
||||
|
||||
@ -919,12 +919,8 @@ namespace CoopClient
|
||||
return null;
|
||||
}
|
||||
|
||||
BinaryFormatter bf = new BinaryFormatter();
|
||||
using (MemoryStream ms = new MemoryStream())
|
||||
{
|
||||
bf.Serialize(ms, obj);
|
||||
return ms.ToArray();
|
||||
}
|
||||
string jsonString = JsonConvert.SerializeObject(obj);
|
||||
return System.Text.Encoding.ASCII.GetBytes(jsonString);
|
||||
}
|
||||
|
||||
public static T CDeserialize<T>(this byte[] bytes) where T : class
|
||||
@ -934,14 +930,8 @@ namespace CoopClient
|
||||
return null;
|
||||
}
|
||||
|
||||
using (MemoryStream memStream = new MemoryStream())
|
||||
{
|
||||
BinaryFormatter binForm = new BinaryFormatter();
|
||||
memStream.Write(bytes, 0, bytes.Length);
|
||||
memStream.Seek(0, SeekOrigin.Begin);
|
||||
T obj = (T)binForm.Deserialize(memStream);
|
||||
return obj;
|
||||
}
|
||||
var jsonString = System.Text.Encoding.ASCII.GetString(bytes);
|
||||
return JsonConvert.DeserializeObject<T>(jsonString);
|
||||
}
|
||||
|
||||
internal static T Deserialize<T>(this byte[] data) where T : new()
|
||||
|
BIN
Libs/Release/Newtonsoft.Json.dll
Normal file
BIN
Libs/Release/Newtonsoft.Json.dll
Normal file
Binary file not shown.
@ -16,6 +16,9 @@
|
||||
<Reference Include="Lidgren.Network">
|
||||
<HintPath>..\Libs\Release\Lidgren.Network.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json">
|
||||
<HintPath>..\Libs\Release\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
|
||||
using Lidgren.Network;
|
||||
using ProtoBuf;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
@ -852,10 +852,8 @@ namespace CoopServer
|
||||
return null;
|
||||
}
|
||||
|
||||
BinaryFormatter bf = new BinaryFormatter();
|
||||
using MemoryStream ms = new MemoryStream();
|
||||
bf.Serialize(ms, obj);
|
||||
return ms.ToArray();
|
||||
string jsonString = JsonConvert.SerializeObject(obj);
|
||||
return System.Text.Encoding.ASCII.GetBytes(jsonString);
|
||||
}
|
||||
|
||||
public static T CDeserialize<T>(this byte[] bytes) where T : class
|
||||
@ -865,12 +863,8 @@ namespace CoopServer
|
||||
return null;
|
||||
}
|
||||
|
||||
using MemoryStream memStream = new MemoryStream();
|
||||
BinaryFormatter binForm = new BinaryFormatter();
|
||||
memStream.Write(bytes, 0, bytes.Length);
|
||||
memStream.Seek(0, SeekOrigin.Begin);
|
||||
T obj = (T)binForm.Deserialize(memStream);
|
||||
return obj;
|
||||
var jsonString = System.Text.Encoding.ASCII.GetString(bytes);
|
||||
return JsonConvert.DeserializeObject<T>(jsonString);
|
||||
}
|
||||
|
||||
internal static T Deserialize<T>(this byte[] data) where T : new()
|
||||
|
@ -283,37 +283,36 @@ namespace CoopServer
|
||||
{
|
||||
packet = new ModPacket();
|
||||
packet.NetIncomingMessageToPacket(message);
|
||||
|
||||
ModPacket modPacket = (ModPacket)packet;
|
||||
if (MainResource != null)
|
||||
if (MainResource != null &&
|
||||
MainResource.InvokeModPacketReceived(modPacket.ID, modPacket.Target, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes))
|
||||
{
|
||||
if (MainResource.InvokeModPacketReceived(modPacket.ID, modPacket.Target, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes))
|
||||
{
|
||||
break;
|
||||
}
|
||||
// Was canceled
|
||||
}
|
||||
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
modPacket.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
||||
if (modPacket.Target != 0)
|
||||
else if (modPacket.Target != -1)
|
||||
{
|
||||
NetConnection target = MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == modPacket.Target);
|
||||
if (target.Equals(default(Client)))
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
modPacket.PacketToNetOutGoingMessage(outgoingMessage);
|
||||
|
||||
if (modPacket.Target != 0)
|
||||
{
|
||||
Logging.Error($"[ModPacket] target \"{modPacket.Target}\" not found!");
|
||||
NetConnection target = MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == modPacket.Target);
|
||||
if (target.Equals(default(Client)))
|
||||
{
|
||||
Logging.Error($"[ModPacket] target \"{modPacket.Target}\" not found!");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send back to target
|
||||
MainNetServer.SendMessage(outgoingMessage, target, NetDeliveryMethod.ReliableOrdered, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send back to target
|
||||
MainNetServer.SendMessage(outgoingMessage, target, NetDeliveryMethod.ReliableOrdered, 0);
|
||||
// Send back to all players
|
||||
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Send back to all players
|
||||
MainNetServer.SendMessage(outgoingMessage, MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, 0);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -10,6 +10,11 @@ using Lidgren.Network;
|
||||
|
||||
namespace CoopServer
|
||||
{
|
||||
public abstract class ServerScript
|
||||
{
|
||||
public API API { get; } = new();
|
||||
}
|
||||
|
||||
internal class Resource
|
||||
{
|
||||
private static Thread _mainThread;
|
||||
@ -59,7 +64,7 @@ namespace CoopServer
|
||||
Task<bool> task = new(() => _script.API.InvokeModPacketReceived(from, target, mod, customID, bytes));
|
||||
task.Start();
|
||||
task.Wait(5000);
|
||||
|
||||
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
@ -92,7 +97,7 @@ namespace CoopServer
|
||||
Task<bool> task = new(() => _script.API.InvokeChatMessage(username, message));
|
||||
task.Start();
|
||||
task.Wait(5000);
|
||||
|
||||
|
||||
return task.Result;
|
||||
}
|
||||
|
||||
@ -121,11 +126,6 @@ namespace CoopServer
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class ServerScript
|
||||
{
|
||||
public API API { get; } = new();
|
||||
}
|
||||
|
||||
public class API
|
||||
{
|
||||
#region DELEGATES
|
||||
|
Reference in New Issue
Block a user