FileStream changed to XmlWriter and XmlReader. Removed support for multiple resources

Thank to @oldnapalm #48
This commit is contained in:
EntenKoeniq 2021-12-28 13:51:36 +01:00
parent 73ac8694be
commit 77daa51c6a
4 changed files with 76 additions and 103 deletions

View File

@ -15,12 +15,9 @@ namespace CoopServer
LastPedHandle = CurrentPedHandle == default ? value : CurrentPedHandle;
CurrentPedHandle = value;
if (CurrentPedHandle != LastPedHandle && Server.Resources.Any())
if (CurrentPedHandle != LastPedHandle && Server.RunningResource != null)
{
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerPedHandleUpdate(Username);
}
Server.RunningResource.InvokePlayerPedHandleUpdate(Username);
}
}
}
@ -34,12 +31,9 @@ namespace CoopServer
LastVehicleHandle = CurrentVehicleHandle == default ? value : CurrentVehicleHandle;
CurrentVehicleHandle = value;
if (CurrentVehicleHandle != LastVehicleHandle && Server.Resources.Any())
if (CurrentVehicleHandle != LastVehicleHandle && Server.RunningResource != null)
{
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerPedHandleUpdate(Username);
}
Server.RunningResource.InvokePlayerPedHandleUpdate(Username);
}
}
}
@ -54,12 +48,9 @@ namespace CoopServer
LastPosition = CurrentPosition.Equals(default(LVector3)) ? value : CurrentPosition;
CurrentPosition = value;
if (Server.Resources.Any() && !LVector3.Equals(CurrentPosition, LastPosition))
if (Server.RunningResource != null && !LVector3.Equals(CurrentPosition, LastPosition))
{
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerPositionUpdate(Username);
}
Server.RunningResource.InvokePlayerPositionUpdate(Username);
}
}
}
@ -73,12 +64,9 @@ namespace CoopServer
LastHealth = CurrentHealth == default ? value : CurrentHealth;
CurrentHealth = value;
if (CurrentHealth != LastHealth && Server.Resources.Any())
if (CurrentHealth != LastHealth && Server.RunningResource != null)
{
foreach (Resource resource in Server.Resources)
{
resource.InvokePlayerHealthUpdate(Username);
}
Server.RunningResource.InvokePlayerHealthUpdate(Username);
}
}
}

View File

@ -31,7 +31,7 @@ namespace CoopServer
public static NetServer MainNetServer;
public static List<Resource> Resources = new();
public static Resource RunningResource = null;
public static Dictionary<Command, Action<CommandContext>> Commands;
public static readonly List<Client> Clients = new();
@ -172,43 +172,40 @@ namespace CoopServer
#endregion
}
if (MainSettings.Resources.Any())
if (!string.IsNullOrEmpty(MainSettings.Resource))
{
Commands = new();
MainSettings.Resources.ForEach(x =>
try
{
try
string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll";
Logging.Info($"Loading resource \"{MainSettings.Resource}.dll\"...");
Assembly asm = Assembly.LoadFrom(resourcepath);
Type[] types = asm.GetExportedTypes();
IEnumerable<Type> validTypes = types.Where(t => !t.IsInterface && !t.IsAbstract).Where(t => typeof(ServerScript).IsAssignableFrom(t));
Type[] enumerable = validTypes as Type[] ?? validTypes.ToArray();
if (!enumerable.Any())
{
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();
IEnumerable<Type> validTypes = types.Where(t => !t.IsInterface && !t.IsAbstract).Where(t => typeof(ServerScript).IsAssignableFrom(t));
Type[] enumerable = validTypes as Type[] ?? validTypes.ToArray();
if (!enumerable.Any())
Logging.Error("ERROR: No classes that inherit from ServerScript have been found in the assembly. Starting freeroam.");
}
else
{
if (Activator.CreateInstance(enumerable.ToArray()[0]) is ServerScript script)
{
Logging.Error("ERROR: No classes that inherit from ServerScript have been found in the assembly. Starting freeroam.");
RunningResource = new(script);
}
else
{
if (Activator.CreateInstance(enumerable.ToArray()[0]) is ServerScript script)
{
Resources.Add(new(script));
}
else
{
Logging.Warning("Could not create resource: it is null.");
}
Logging.Warning("Could not create resource: it is null.");
}
}
catch (Exception e)
{
Logging.Error(e.InnerException.Message);
}
});
}
catch (Exception e)
{
Logging.Error(e.InnerException.Message);
}
}
Listen();
@ -221,12 +218,9 @@ namespace CoopServer
while (!Program.ReadyToStop)
{
if (Resources.Count != 0)
if (RunningResource != null)
{
Resources.ForEach(x =>
{
x.InvokeTick(++CurrentTick);
});
RunningResource.InvokeTick(++CurrentTick);
}
NetIncomingMessage message;
@ -458,15 +452,12 @@ namespace CoopServer
packet.NetIncomingMessageToPacket(data);
bool resourceResult = false;
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x =>
if (RunningResource.InvokeModPacketReceived(packet.NetHandle, packet.Target, packet.Mod, packet.CustomPacketID, packet.Bytes))
{
if (x.InvokeModPacketReceived(packet.NetHandle, packet.Target, packet.Mod, packet.CustomPacketID, packet.Bytes))
{
resourceResult = true;
}
});
resourceResult = true;
}
}
if (!resourceResult && packet.Target != -1)
@ -542,17 +533,14 @@ namespace CoopServer
}
Logging.Warning("Server is shutting down!");
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x =>
// Waiting for resource...
while (!RunningResource.ReadyToStop)
{
// Waiting for resource...
while (!x.ReadyToStop)
{
// 16 milliseconds to sleep to reduce CPU usage
Thread.Sleep(1000 / 60);
}
});
// 16 milliseconds to sleep to reduce CPU usage
Thread.Sleep(1000 / 60);
}
}
if (MainNetServer.Connections.Count > 0)
@ -639,7 +627,10 @@ namespace CoopServer
// Accept the connection and send back a new handshake packet with the connection ID
local.Approve(outgoingMessage);
Resources.ForEach(x => x.InvokePlayerHandshake(tmpClient));
if (RunningResource != null)
{
RunningResource.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
@ -683,9 +674,9 @@ namespace CoopServer
MainNetServer.SendMessage(outgoingMessage, clients, NetDeliveryMethod.ReliableOrdered, 0);
}
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerConnected(localClient));
RunningResource.InvokePlayerConnected(localClient);
}
else
{
@ -720,9 +711,9 @@ namespace CoopServer
Clients.Remove(localClient);
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerDisconnected(localClient));
RunningResource.InvokePlayerDisconnected(localClient);
}
else
{
@ -766,9 +757,9 @@ namespace CoopServer
}
});
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerUpdate(client));
RunningResource.InvokePlayerUpdate(client);
}
}
@ -810,9 +801,9 @@ namespace CoopServer
}
});
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerUpdate(client));
RunningResource.InvokePlayerUpdate(client);
}
}
@ -851,9 +842,9 @@ namespace CoopServer
}
});
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerUpdate(client));
RunningResource.InvokePlayerUpdate(client);
}
}
@ -892,16 +883,16 @@ namespace CoopServer
}
});
if (Resources.Any())
if (RunningResource != null)
{
Resources.ForEach(x => x.InvokePlayerUpdate(client));
RunningResource.InvokePlayerUpdate(client);
}
}
// Send a message to targets or all players
private static void SendChatMessage(ChatMessagePacket packet, List<NetConnection> targets = null)
{
if (Resources.Any())
if (RunningResource != null)
{
if (packet.Message.StartsWith('/'))
{
@ -947,21 +938,9 @@ namespace CoopServer
return;
}
if (Resources.Any())
if (RunningResource.InvokeChatMessage(packet.Username, packet.Message))
{
bool resourceResult = false;
Resources.ForEach(x =>
{
if (x.InvokeChatMessage(packet.Username, packet.Message))
{
resourceResult = true;
}
});
if (resourceResult)
{
return;
}
return;
}
}

View File

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

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Linq;
using System.Collections.Generic;
@ -70,24 +71,31 @@ namespace CoopServer
{
XmlSerializer ser = new(typeof(T));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = ("\t"),
OmitXmlDeclaration = true
};
string path = AppContext.BaseDirectory + file;
T data;
if (File.Exists(path))
{
using (FileStream stream = File.OpenRead(path))
using (XmlReader stream = XmlReader.Create(path))
{
data = (T)ser.Deserialize(stream);
}
using (FileStream stream = new(path, FileMode.Truncate, FileAccess.ReadWrite))
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data);
}
}
else
{
using (FileStream stream = File.OpenWrite(path))
using (XmlWriter stream = XmlWriter.Create(path, settings))
{
ser.Serialize(stream, data = new T());
}