Added support for multiple resources

This commit is contained in:
EntenKoeniq
2021-12-17 21:32:57 +01:00
parent 6e72cf9d27
commit 4510543c8e
4 changed files with 71 additions and 59 deletions

View File

@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<AssemblyVersion>1.10.1.0001</AssemblyVersion>
<AssemblyVersion>1.11.0.0001</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<RepositoryUrl>https://github.com/GTACOOP-R/GTACoop-R</RepositoryUrl>
</PropertyGroup>

View File

@ -1,4 +1,6 @@
namespace CoopServer
using System.Linq;
namespace CoopServer
{
public struct PlayerData
{
@ -14,9 +16,12 @@
LastPosition = CurrentPosition;
CurrentPosition = value;
if (Server.MainResource != null && !LVector3.Equals(CurrentPosition, LastPosition))
if (Server.Resources.Any() && !LVector3.Equals(CurrentPosition, LastPosition))
{
Server.MainResource.InvokePlayerPositionUpdate(this);
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerPositionUpdate(this);
}
}
}
}
@ -26,9 +31,12 @@
get => CurrentHealth;
set
{
if (CurrentHealth != value && Server.MainResource != null)
if (Server.Resources.Any() && CurrentHealth != value)
{
Server.MainResource.InvokePlayerHealthUpdate(this);
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerHealthUpdate(this);
}
}
CurrentHealth = value;

View File

@ -30,7 +30,7 @@ namespace CoopServer
public static NetServer MainNetServer;
public static Resource MainResource = null;
public static List<Resource> Resources = new();
public static Dictionary<Command, Action<CommandContext>> Commands;
public static readonly List<Client> Clients = new();
@ -171,12 +171,16 @@ namespace CoopServer
#endregion
}
if (!string.IsNullOrEmpty(MainSettings.Resource))
if (MainSettings.Resources.Any())
{
Commands = new();
MainSettings.Resources.ForEach(x =>
{
try
{
string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll";
Logging.Info($"Loading resource {resourcepath}...");
string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + x + ".dll";
Logging.Info($"Loading resource \"{x}.dll\"...");
Assembly asm = Assembly.LoadFrom(resourcepath);
Type[] types = asm.GetExportedTypes();
@ -189,11 +193,9 @@ namespace CoopServer
}
else
{
Commands = new();
if (Activator.CreateInstance(enumerable.ToArray()[0]) is ServerScript script)
{
MainResource = new(script);
Resources.Add(new(script));
}
else
{
@ -203,8 +205,9 @@ namespace CoopServer
}
catch (Exception e)
{
Logging.Error(e.Message);
Logging.Error(e.InnerException.Message);
}
});
}
Listen();
@ -427,8 +430,7 @@ namespace CoopServer
packet = new ModPacket();
packet.NetIncomingMessageToPacket(message);
ModPacket modPacket = (ModPacket)packet;
if (MainResource != null &&
MainResource.InvokeModPacketReceived(modPacket.NetHandle, modPacket.Target, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes))
if (Resources.Any(x => x.InvokeModPacketReceived(modPacket.NetHandle, modPacket.Target, modPacket.Mod, modPacket.CustomPacketID, modPacket.Bytes)))
{
// Was canceled
}
@ -504,14 +506,17 @@ namespace CoopServer
}
Logging.Warning("Server is shutting down!");
if (MainResource != null)
if (Resources.Any())
{
Resources.ForEach(x =>
{
// Waiting for resource...
while (!MainResource.ReadyToStop)
while (!x.ReadyToStop)
{
// 16 milliseconds to sleep to reduce CPU usage
Thread.Sleep(1000 / 60);
}
});
}
if (MainNetServer.Connections.Count > 0)
@ -617,10 +622,7 @@ namespace CoopServer
// Accept the connection and send back a new handshake packet with the connection ID
local.Approve(outgoingMessage);
if (MainResource != null)
{
MainResource.InvokePlayerHandshake(tmpClient);
}
Resources.ForEach(x => x.InvokePlayerHandshake(tmpClient));
}
// The connection has been approved, now we need to send all other players to the new player and the new player to all players
@ -666,9 +668,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
}
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerConnected(localClient);
Resources.ForEach(x => x.InvokePlayerConnected(localClient));
}
else
{
@ -703,9 +705,9 @@ namespace CoopServer
Clients.Remove(localClient);
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerDisconnected(localClient);
Resources.ForEach(x => x.InvokePlayerDisconnected(localClient));
}
else
{
@ -747,9 +749,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.Player);
});
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerUpdate(client);
Resources.ForEach(x => x.InvokePlayerUpdate(client));
}
}
@ -787,9 +789,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.Player);
});
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerUpdate(client);
Resources.ForEach(x => x.InvokePlayerUpdate(client));
}
}
@ -827,9 +829,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.Player);
});
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerUpdate(client);
Resources.ForEach(x => x.InvokePlayerUpdate(client));
}
}
@ -867,9 +869,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, x, NetDeliveryMethod.UnreliableSequenced, (int)ConnectionChannel.Player);
});
if (MainResource != null)
if (Resources.Any())
{
MainResource.InvokePlayerUpdate(client);
Resources.ForEach(x => x.InvokePlayerUpdate(client));
}
}
@ -878,7 +880,7 @@ namespace CoopServer
{
NetOutgoingMessage outgoingMessage;
if (MainResource != null)
if (Resources.Any())
{
if (packet.Message.StartsWith('/'))
{
@ -936,7 +938,7 @@ namespace CoopServer
return;
}
if (MainResource.InvokeChatMessage(packet.Username, packet.Message))
if (Resources.Any(x => x.InvokeChatMessage(packet.Username, packet.Message)))
{
return;
}

View File

@ -1,4 +1,6 @@
namespace CoopServer
using System.Collections.Generic;
namespace CoopServer
{
public class Settings
{
@ -6,7 +8,7 @@
public int MaxPlayers { get; set; } = 16;
public string Name { get; set; } = "GTACoop:R server";
public string WelcomeMessage { get; set; } = "Welcome on this server :)";
public string Resource { get; set; } = "";
public List<string> Resources { get; set; } = new List<string>();
public bool Allowlist { get; set; } = false;
public bool NpcsAllowed { get; set; } = true;
public bool ModsAllowed { get; set; } = false;