Scripting?
This commit is contained in:
parent
b863b7037a
commit
a8a16fac61
@ -34,10 +34,9 @@ namespace RageCoop.Client
|
||||
#endif
|
||||
public static Chat MainChat = null;
|
||||
public static Stopwatch Counter = new Stopwatch();
|
||||
public static Core.Logging.Logger Logger = null;
|
||||
|
||||
public static ulong Ticked = 0;
|
||||
public static Loggger Logger=new Loggger("Scripts\\RageCoop\\RageCoop.Client.log");
|
||||
|
||||
private static List<Func<bool>> QueuedActions = new List<Func<bool>>();
|
||||
|
||||
/// <summary>
|
||||
@ -45,7 +44,16 @@ namespace RageCoop.Client
|
||||
/// </summary>
|
||||
public Main()
|
||||
{
|
||||
|
||||
Logger=new Core.Logging.Logger()
|
||||
{
|
||||
LogPath="Scripts\\RageCoop\\RageCoop.Client.log",
|
||||
UseConsole=false,
|
||||
#if DEBUG
|
||||
LogLevel = 0,
|
||||
#else
|
||||
LogLevel=Settings.LogLevel;
|
||||
#endif
|
||||
};
|
||||
// Required for some synchronization!
|
||||
/*if (Game.Version < GameVersion.v1_0_1290_1_Steam)
|
||||
{
|
||||
@ -55,7 +63,7 @@ namespace RageCoop.Client
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!_gameLoaded)
|
||||
{
|
||||
GTA.UI.Notification.Show("~r~Please update your GTA5 to v1.0.1290 or newer!", true);
|
||||
@ -71,11 +79,6 @@ namespace RageCoop.Client
|
||||
#if !NON_INTERACTIVE
|
||||
#endif
|
||||
MainChat = new Chat();
|
||||
#if DEBUG
|
||||
Logger.LogLevel = 0;
|
||||
#else
|
||||
Logger.LogLevel=Settings.LogLevel;
|
||||
#endif
|
||||
|
||||
Tick += OnTick;
|
||||
KeyDown += OnKeyDown;
|
||||
@ -122,7 +125,7 @@ namespace RageCoop.Client
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.Error(ex);
|
||||
Main.Logger.Error(ex);
|
||||
}
|
||||
|
||||
if (!DownloadManager.DownloadComplete)
|
||||
@ -332,7 +335,7 @@ namespace RageCoop.Client
|
||||
catch
|
||||
{
|
||||
GTA.UI.Notification.Show("~r~~h~CleanUpWorld() Error");
|
||||
Logger.Error($"CleanUpWorld(): ~r~Item {item.Value} cannot be deleted!");
|
||||
Main.Logger.Error($"CleanUpWorld(): ~r~Item {item.Value} cannot be deleted!");
|
||||
}
|
||||
}
|
||||
|
||||
@ -400,7 +403,7 @@ namespace RageCoop.Client
|
||||
// s+=$"\r\n{c.IsAiming} {c.IsJumping} {c.IsOnFire} {c.IsOnLadder} {c.IsRagdoll} {c.IsReloading} {c.IsShooting} {c.Speed}";
|
||||
}
|
||||
}
|
||||
Logger.Trace(s);
|
||||
Main.Logger.Trace(s);
|
||||
return s;
|
||||
}
|
||||
public static string DumpPlayers()
|
||||
@ -411,7 +414,7 @@ namespace RageCoop.Client
|
||||
|
||||
s+=$"\r\nID:{p.PedID} Username:{p.Username}";
|
||||
}
|
||||
Logger.Trace(s);
|
||||
Main.Logger.Trace(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
|
@ -44,11 +44,6 @@ namespace RageCoop.Client
|
||||
Flag = p.GetPedFlags(),
|
||||
Heading=p.Heading,
|
||||
};
|
||||
if (packet.Health==1&&packet.ID==Main.LocalPlayerID)
|
||||
{
|
||||
// Fake death
|
||||
packet.Health=0;
|
||||
}
|
||||
if (packet.Flag.HasFlag(PedDataFlags.IsAiming))
|
||||
{
|
||||
packet.AimCoords = p.GetAimCoord();
|
||||
|
@ -124,6 +124,7 @@
|
||||
<Compile Include="DevTools\DevTool.cs" />
|
||||
<Compile Include="Menus\Sub\DevToolMenu.cs" />
|
||||
<Compile Include="Scripting\ClientScript.cs" />
|
||||
<Compile Include="Scripting\Engine.cs" />
|
||||
<Compile Include="Util\MathExtensions.cs" />
|
||||
<Compile Include="Util\Util.cs" />
|
||||
<Compile Include="Util\VehicleExtensions.cs" />
|
||||
|
@ -6,12 +6,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace RageCoop.Client.Scripting
|
||||
{
|
||||
/// <summary>
|
||||
/// Inherit from this class, constructor will be called when the script is loaded.
|
||||
/// </summary>
|
||||
public abstract class ClientScript
|
||||
{
|
||||
public class Events
|
||||
{
|
||||
|
||||
}
|
||||
public abstract void Main();
|
||||
}
|
||||
}
|
||||
|
19
Client/Scripting/Engine.cs
Normal file
19
Client/Scripting/Engine.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Reflection;
|
||||
using System.IO;
|
||||
using System.CodeDom.Compiler;
|
||||
|
||||
namespace RageCoop.Client.Scripting
|
||||
{
|
||||
internal class Engine : Core.Scripting.ScriptingEngine
|
||||
{
|
||||
public Engine() : base(typeof(ClientScript), Main.Logger)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -96,7 +96,8 @@ namespace RageCoop.Client
|
||||
flags |= PedDataFlags.IsJumping;
|
||||
}
|
||||
|
||||
if (ped.IsRagdoll)
|
||||
// Fake death
|
||||
if (ped.IsRagdoll || (ped.Health==1 && ped.IsPlayer))
|
||||
{
|
||||
flags |= PedDataFlags.IsRagdoll;
|
||||
}
|
||||
|
@ -5,41 +5,26 @@ using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace RageCoop.Core
|
||||
namespace RageCoop.Core.Logging
|
||||
{
|
||||
public class Loggger
|
||||
public class Logger
|
||||
{
|
||||
|
||||
public string LogPath;
|
||||
private StreamWriter logWriter;
|
||||
private bool UseConsole=false;
|
||||
|
||||
/// <summary>
|
||||
/// 0:Trace, 1:Debug, 2:Info, 3:Warning, 4:Error
|
||||
/// </summary>
|
||||
public int LogLevel = 0;
|
||||
public string LogPath;
|
||||
public bool UseConsole = false;
|
||||
private static StreamWriter logWriter;
|
||||
|
||||
private string Buffer="";
|
||||
private Thread LoggerThread;
|
||||
|
||||
public Loggger(string path,bool overwrite=true)
|
||||
public Logger(bool overwrite=true)
|
||||
{
|
||||
|
||||
|
||||
LogPath=path;
|
||||
if (File.Exists(path)&&overwrite) { File.Delete(path); }
|
||||
Task.Run(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Flush();
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
public Loggger()
|
||||
{
|
||||
UseConsole=true;
|
||||
Task.Run(() =>
|
||||
if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); }
|
||||
LoggerThread=new Thread(() =>
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@ -47,6 +32,7 @@ namespace RageCoop.Core
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
});
|
||||
LoggerThread.Start();
|
||||
}
|
||||
|
||||
public void Info(string message)
|
154
Core/Scripting/ScriptingEngine.cs
Normal file
154
Core/Scripting/ScriptingEngine.cs
Normal file
@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RageCoop.Core.Logging;
|
||||
using System.Linq;
|
||||
|
||||
[assembly: InternalsVisibleTo("RageCoop.Server")]
|
||||
[assembly: InternalsVisibleTo("RageCoop.Client")]
|
||||
namespace RageCoop.Core.Scripting
|
||||
{
|
||||
internal class ScriptingEngine
|
||||
{
|
||||
private Type BaseScriptType;
|
||||
public Logger Logger { get; set; }
|
||||
public ScriptingEngine(Type baseScriptType,Logger logger)
|
||||
{
|
||||
BaseScriptType = baseScriptType;
|
||||
Logger = logger;
|
||||
}
|
||||
/// <summary>
|
||||
/// Loads scripts from the specified assembly file.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to the assembly file to load.</param>
|
||||
/// <returns><see langword="true" /> on success, <see langword="false" /> otherwise</returns>
|
||||
private bool LoadScriptsFromAssembly(string filename)
|
||||
{
|
||||
if (!IsManagedAssembly(filename))
|
||||
return false;
|
||||
|
||||
Logger?.Debug($"Loading assembly {Path.GetFileName(filename)} ...");
|
||||
|
||||
Assembly assembly;
|
||||
|
||||
try
|
||||
{
|
||||
assembly = Assembly.LoadFrom(filename);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.Error( "Unable to load "+Path.GetFileName(filename));
|
||||
Logger?.Error(ex);
|
||||
return false;
|
||||
}
|
||||
|
||||
return LoadScriptsFromAssembly(assembly, filename);
|
||||
}
|
||||
/// <summary>
|
||||
/// Loads scripts from the specified assembly object.
|
||||
/// </summary>
|
||||
/// <param name="filename">The path to the file associated with this assembly.</param>
|
||||
/// <param name="assembly">The assembly to load.</param>
|
||||
/// <returns><see langword="true" /> on success, <see langword="false" /> otherwise</returns>
|
||||
private bool LoadScriptsFromAssembly(Assembly assembly, string filename)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// Find all script types in the assembly
|
||||
foreach (var type in assembly.GetTypes().Where(x => IsSubclassOf(x,nameof(BaseScriptType))))
|
||||
{
|
||||
ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
|
||||
if (constructor != null && constructor.IsPublic)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Invoke script constructor
|
||||
constructor.Invoke(null);
|
||||
count++;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.Error($"Error occurred when loading script: {type.FullName}.");
|
||||
Logger?.Error(ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger?.Error($"Script {type.FullName} has an invalid contructor.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ReflectionTypeLoadException ex)
|
||||
{
|
||||
Logger?.Error($"Failed to load assembly {Path.GetFileName(filename)}: ");
|
||||
Logger?.Error(ex);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Logger?.Info($"Loaded {count.ToString()} script(s) in {Path.GetFileName(filename)}");
|
||||
return count != 0;
|
||||
}
|
||||
private bool IsManagedAssembly(string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (Stream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
|
||||
{
|
||||
if (file.Length < 64)
|
||||
return false;
|
||||
|
||||
using (BinaryReader bin = new BinaryReader(file))
|
||||
{
|
||||
// PE header starts at offset 0x3C (60). Its a 4 byte header.
|
||||
file.Position = 0x3C;
|
||||
uint offset = bin.ReadUInt32();
|
||||
if (offset == 0)
|
||||
offset = 0x80;
|
||||
|
||||
// Ensure there is at least enough room for the following structures:
|
||||
// 24 byte PE Signature & Header
|
||||
// 28 byte Standard Fields (24 bytes for PE32+)
|
||||
// 68 byte NT Fields (88 bytes for PE32+)
|
||||
// >= 128 byte Data Dictionary Table
|
||||
if (offset > file.Length - 256)
|
||||
return false;
|
||||
|
||||
// Check the PE signature. Should equal 'PE\0\0'.
|
||||
file.Position = offset;
|
||||
if (bin.ReadUInt32() != 0x00004550)
|
||||
return false;
|
||||
|
||||
// Read PE magic number from Standard Fields to determine format.
|
||||
file.Position += 20;
|
||||
var peFormat = bin.ReadUInt16();
|
||||
if (peFormat != 0x10b /* PE32 */ && peFormat != 0x20b /* PE32Plus */)
|
||||
return false;
|
||||
|
||||
// Read the 15th Data Dictionary RVA field which contains the CLI header RVA.
|
||||
// When this is non-zero then the file contains CLI data otherwise not.
|
||||
file.Position = offset + (peFormat == 0x10b ? 232 : 248);
|
||||
return bin.ReadUInt32() != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// This is likely not a valid assembly if any IO exceptions occur during reading
|
||||
return false;
|
||||
}
|
||||
}
|
||||
private bool IsSubclassOf(Type type, string baseTypeName)
|
||||
{
|
||||
for (Type t = type.BaseType; t != null; t = t.BaseType)
|
||||
if (t.FullName == baseTypeName)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -87,7 +87,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -98,13 +98,13 @@ namespace RageCoop.Server
|
||||
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
|
||||
if (userConnection == null)
|
||||
{
|
||||
Logging.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{NetID}\" not found!");
|
||||
Program.Logger.Error($"[Client->SendNativeCall(ulong hash, params object[] args)]: Connection \"{NetID}\" not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args != null && args.Length == 0)
|
||||
{
|
||||
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
||||
Program.Logger.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -120,7 +120,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -131,13 +131,13 @@ namespace RageCoop.Server
|
||||
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
|
||||
if (userConnection == null)
|
||||
{
|
||||
Logging.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{NetID}\" not found!");
|
||||
Program.Logger.Error($"[Client->SendNativeResponse(Action<object> callback, ulong hash, Type type, params object[] args)]: Connection \"{NetID}\" not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (args != null && args.Length == 0)
|
||||
{
|
||||
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
||||
Program.Logger.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing arguments!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -167,7 +167,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing return type!");
|
||||
Program.Logger.Error($"[Client->SendNativeCall(ulong hash, Dictionary<string, object> args)]: Missing return type!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -183,7 +183,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -192,7 +192,7 @@ namespace RageCoop.Server
|
||||
NetConnection userConnection = Server.MainNetServer.Connections.Find(x => x.RemoteUniqueIdentifier == NetID);
|
||||
if (userConnection == null)
|
||||
{
|
||||
Logging.Error($"[Client->SendCleanUpWorld()]: Connection \"{NetID}\" not found!");
|
||||
Program.Logger.Error($"[Client->SendCleanUpWorld()]: Connection \"{NetID}\" not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -223,7 +223,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,7 +231,7 @@ namespace RageCoop.Server
|
||||
{
|
||||
if (!FilesReceived)
|
||||
{
|
||||
Logging.Warning($"Player \"{Player.Username}\" doesn't have all the files yet!");
|
||||
Program.Logger.Warning($"Player \"{Player.Username}\" doesn't have all the files yet!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -254,7 +254,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
@ -43,7 +43,7 @@ namespace RageCoop.Server
|
||||
// ONLY JAVASCRIPT AND JSON FILES!
|
||||
if (!new string[] { ".js", ".xml" }.Any(x => x == fileInfo.Extension))
|
||||
{
|
||||
Logging.Warning("Only files with \"*.js\" and \"*.xml\" can be sent!");
|
||||
Program.Logger.Warning("Only files with \"*.js\" and \"*.xml\" can be sent!");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -1,79 +0,0 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace RageCoop.Server
|
||||
{
|
||||
public class Logging
|
||||
{
|
||||
private static readonly object _lock = new();
|
||||
|
||||
public static void Info(string message)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
string msg = string.Format("[{0}] [INFO] {1}", Date(), message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
Console.WriteLine(msg);
|
||||
Console.ResetColor();
|
||||
|
||||
using StreamWriter sw = new("log.txt", true);
|
||||
sw.WriteLine(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Warning(string message)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
string msg = string.Format("[{0}] [WARNING] {1}", Date(), message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine(msg);
|
||||
Console.ResetColor();
|
||||
|
||||
using StreamWriter sw = new("log.txt", true);
|
||||
sw.WriteLine(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Error(string message)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
string msg = string.Format("[{0}] [ERROR] {1}", Date(), message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(msg);
|
||||
Console.ResetColor();
|
||||
|
||||
using StreamWriter sw = new("log.txt", true);
|
||||
sw.WriteLine(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Debug(string message)
|
||||
{
|
||||
if (!Server.MainSettings.DebugMode)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
string msg = string.Format("[{0}] [DEBUG] {1}", Date(), message);
|
||||
|
||||
Console.ForegroundColor = ConsoleColor.Blue;
|
||||
Console.WriteLine(msg);
|
||||
Console.ResetColor();
|
||||
|
||||
using StreamWriter sw = new("log.txt", true);
|
||||
sw.WriteLine(msg);
|
||||
}
|
||||
}
|
||||
private static string Date()
|
||||
{
|
||||
return DateTime.Now.ToString();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,8 +9,14 @@ namespace RageCoop.Server
|
||||
class Program
|
||||
{
|
||||
public static bool ReadyToStop = false;
|
||||
public static Core.Logging.Logger Logger;
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Logger=new Core.Logging.Logger()
|
||||
{
|
||||
LogPath="RageCoop.Server.log",
|
||||
UseConsole=true,
|
||||
};
|
||||
try
|
||||
{
|
||||
#if DEBUG
|
||||
@ -45,7 +51,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error(e.InnerException?.Message ?? e.Message);
|
||||
Program.Logger.Error(e.InnerException?.Message ?? e.Message);
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
@ -40,11 +40,11 @@ namespace RageCoop.Server
|
||||
|
||||
public Server()
|
||||
{
|
||||
Logging.Info("================");
|
||||
Logging.Info($"Server bound to: 0.0.0.0:{MainSettings.Port}");
|
||||
Logging.Info($"Server version: {Assembly.GetCallingAssembly().GetName().Version}");
|
||||
Logging.Info($"Compatible RAGECOOP versions: {_compatibleVersion.Replace('_', '.')}.x");
|
||||
Logging.Info("================");
|
||||
Program.Logger.Info("================");
|
||||
Program.Logger.Info($"Server bound to: 0.0.0.0:{MainSettings.Port}");
|
||||
Program.Logger.Info($"Server version: {Assembly.GetCallingAssembly().GetName().Version}");
|
||||
Program.Logger.Info($"Compatible RAGECOOP versions: {_compatibleVersion.Replace('_', '.')}.x");
|
||||
Program.Logger.Info("================");
|
||||
|
||||
// 623c92c287cc392406e7aaaac1c0f3b0 = RAGECOOP
|
||||
NetPeerConfiguration config = new("623c92c287cc392406e7aaaac1c0f3b0")
|
||||
@ -60,26 +60,26 @@ namespace RageCoop.Server
|
||||
MainNetServer = new NetServer(config);
|
||||
MainNetServer.Start();
|
||||
|
||||
Logging.Info(string.Format("Server listening on {0}:{1}", config.LocalAddress.ToString(), config.Port));
|
||||
Program.Logger.Info(string.Format("Server listening on {0}:{1}", config.LocalAddress.ToString(), config.Port));
|
||||
|
||||
if (MainSettings.UPnP)
|
||||
{
|
||||
Logging.Info(string.Format("Attempting to forward port {0}", MainSettings.Port));
|
||||
Program.Logger.Info(string.Format("Attempting to forward port {0}", MainSettings.Port));
|
||||
|
||||
if (MainNetServer.UPnP.ForwardPort(MainSettings.Port, "RAGECOOP server"))
|
||||
{
|
||||
Logging.Info(string.Format("Server available on {0}:{1}", MainNetServer.UPnP.GetExternalIP().ToString(), config.Port));
|
||||
Program.Logger.Info(string.Format("Server available on {0}:{1}", MainNetServer.UPnP.GetExternalIP().ToString(), config.Port));
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("Port forwarding failed! Your router may not support UPnP.");
|
||||
Logging.Warning("If you and your friends can join this server, please ignore this error or set UPnP in Settings.xml to false!");
|
||||
Program.Logger.Error("Port forwarding failed! Your router may not support UPnP.");
|
||||
Program.Logger.Warning("If you and your friends can join this server, please ignore this error or set UPnP in Settings.xml to false!");
|
||||
}
|
||||
}
|
||||
|
||||
if (MainSettings.AnnounceSelf)
|
||||
{
|
||||
Logging.Info("Announcing to master server...");
|
||||
Program.Logger.Info("Announcing to master server...");
|
||||
|
||||
#region -- MASTERSERVER --
|
||||
new Thread(async () =>
|
||||
@ -107,7 +107,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.Error(ex.InnerException?.Message ?? ex.Message);
|
||||
Program.Logger.Error(ex.InnerException?.Message ?? ex.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -131,7 +131,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.Error($"MasterServer: {ex.Message}");
|
||||
Program.Logger.Error($"MasterServer: {ex.Message}");
|
||||
|
||||
// Sleep for 5s
|
||||
Thread.Sleep(5000);
|
||||
@ -140,18 +140,18 @@ namespace RageCoop.Server
|
||||
|
||||
if (response == null)
|
||||
{
|
||||
Logging.Error("MasterServer: Something went wrong!");
|
||||
Program.Logger.Error("MasterServer: Something went wrong!");
|
||||
}
|
||||
else if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
if (response.StatusCode == HttpStatusCode.BadRequest)
|
||||
{
|
||||
string requestContent = await response.Content.ReadAsStringAsync();
|
||||
Logging.Error($"MasterServer: [{(int)response.StatusCode}], {requestContent}");
|
||||
Program.Logger.Error($"MasterServer: [{(int)response.StatusCode}], {requestContent}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error($"MasterServer: [{(int)response.StatusCode}]");
|
||||
Program.Logger.Error($"MasterServer: [{(int)response.StatusCode}]");
|
||||
}
|
||||
}
|
||||
|
||||
@ -161,11 +161,11 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (HttpRequestException ex)
|
||||
{
|
||||
Logging.Error($"MasterServer: {ex.InnerException.Message}");
|
||||
Program.Logger.Error($"MasterServer: {ex.InnerException.Message}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logging.Error($"MasterServer: {ex.Message}");
|
||||
Program.Logger.Error($"MasterServer: {ex.Message}");
|
||||
}
|
||||
}).Start();
|
||||
#endregion
|
||||
@ -176,7 +176,7 @@ namespace RageCoop.Server
|
||||
try
|
||||
{
|
||||
string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll";
|
||||
Logging.Info($"Loading resource \"{MainSettings.Resource}.dll\"...");
|
||||
Program.Logger.Info($"Loading resource \"{MainSettings.Resource}.dll\"...");
|
||||
|
||||
Assembly asm = Assembly.LoadFrom(resourcepath);
|
||||
Type[] types = asm.GetExportedTypes();
|
||||
@ -185,7 +185,7 @@ namespace RageCoop.Server
|
||||
|
||||
if (!enumerable.Any())
|
||||
{
|
||||
Logging.Error("ERROR: No classes that inherit from ServerScript have been found in the assembly. Starting freeroam.");
|
||||
Program.Logger.Error("ERROR: No classes that inherit from ServerScript have been found in the assembly. Starting freeroam.");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -195,17 +195,17 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("Could not create resource: it is null.");
|
||||
Program.Logger.Warning("Could not create resource: it is null.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error(e.InnerException.Message);
|
||||
Program.Logger.Error(e.InnerException.Message);
|
||||
}
|
||||
}
|
||||
|
||||
Logging.Info("Searching for client-side files...");
|
||||
Program.Logger.Info("Searching for client-side files...");
|
||||
DownloadManager.CheckForDirectoryAndFiles();
|
||||
|
||||
Listen();
|
||||
@ -216,8 +216,8 @@ namespace RageCoop.Server
|
||||
|
||||
private void Listen()
|
||||
{
|
||||
Logging.Info("Listening for clients");
|
||||
Logging.Info("Please use CTRL + C if you want to stop the server!");
|
||||
Program.Logger.Info("Listening for clients");
|
||||
Program.Logger.Info("Please use CTRL + C if you want to stop the server!");
|
||||
|
||||
while (!Program.ReadyToStop)
|
||||
{
|
||||
@ -252,10 +252,10 @@ namespace RageCoop.Server
|
||||
{
|
||||
case NetIncomingMessageType.ConnectionApproval:
|
||||
{
|
||||
Logging.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
|
||||
Program.Logger.Info($"New incoming connection from: [{message.SenderConnection.RemoteEndPoint}]");
|
||||
if (message.ReadByte() != (byte)PacketTypes.Handshake)
|
||||
{
|
||||
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
|
||||
Program.Logger.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: Wrong packet!");
|
||||
message.SenderConnection.Deny("Wrong packet!");
|
||||
}
|
||||
else
|
||||
@ -272,7 +272,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
|
||||
Program.Logger.Info($"IP [{message.SenderConnection.RemoteEndPoint.Address}] was blocked, reason: {e.Message}");
|
||||
message.SenderConnection.Deny(e.Message);
|
||||
}
|
||||
}
|
||||
@ -500,7 +500,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning($"Player \"{client.Player.Username}\" attempted to trigger an unknown event! [{packet.EventName}]");
|
||||
Program.Logger.Warning($"Player \"{client.Player.Username}\" attempted to trigger an unknown event! [{packet.EventName}]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -536,7 +536,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Error("Unhandled Data / Packet type");
|
||||
Program.Logger.Error("Unhandled Data / Packet type");
|
||||
}
|
||||
break;
|
||||
}
|
||||
@ -551,17 +551,17 @@ namespace RageCoop.Server
|
||||
}
|
||||
break;
|
||||
case NetIncomingMessageType.ErrorMessage:
|
||||
Logging.Error(message.ReadString());
|
||||
Program.Logger.Error(message.ReadString());
|
||||
break;
|
||||
case NetIncomingMessageType.WarningMessage:
|
||||
Logging.Warning(message.ReadString());
|
||||
Program.Logger.Warning(message.ReadString());
|
||||
break;
|
||||
case NetIncomingMessageType.DebugMessage:
|
||||
case NetIncomingMessageType.VerboseDebugMessage:
|
||||
Logging.Debug(message.ReadString());
|
||||
Program.Logger.Debug(message.ReadString());
|
||||
break;
|
||||
default:
|
||||
Logging.Error(string.Format("Unhandled type: {0} {1} bytes {2} | {3}", message.MessageType, message.LengthBytes, message.DeliveryMethod, message.SequenceChannel));
|
||||
Program.Logger.Error(string.Format("Unhandled type: {0} {1} bytes {2} | {3}", message.MessageType, message.LengthBytes, message.DeliveryMethod, message.SequenceChannel));
|
||||
break;
|
||||
}
|
||||
|
||||
@ -572,7 +572,7 @@ namespace RageCoop.Server
|
||||
Thread.Sleep(1000 / 60);
|
||||
}
|
||||
|
||||
Logging.Warning("Server is shutting down!");
|
||||
Program.Logger.Warning("Server is shutting down!");
|
||||
if (RunningResource != null)
|
||||
{
|
||||
// Waiting for resource...
|
||||
@ -617,9 +617,9 @@ namespace RageCoop.Server
|
||||
|
||||
private void DisconnectAndLog(NetConnection senderConnection,PacketTypes type, Exception e)
|
||||
{
|
||||
Logging.Error($"Error receiving a packet of type {type}");
|
||||
Logging.Error(e.Message);
|
||||
Logging.Error(e.StackTrace);
|
||||
Program.Logger.Error($"Error receiving a packet of type {type}");
|
||||
Program.Logger.Error(e.Message);
|
||||
Program.Logger.Error(e.StackTrace);
|
||||
senderConnection.Disconnect(e.Message);
|
||||
}
|
||||
|
||||
@ -627,7 +627,7 @@ namespace RageCoop.Server
|
||||
// Before we approve the connection, we must shake hands
|
||||
private void GetHandshake(NetConnection local, Packets.Handshake packet)
|
||||
{
|
||||
Logging.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + local.RemoteEndPoint.Address.ToString() + "]");
|
||||
Program.Logger.Debug("New handshake from: [Name: " + packet.Username + " | Address: " + local.RemoteEndPoint.Address.ToString() + "]");
|
||||
|
||||
if (!packet.ModVersion.StartsWith(_compatibleVersion))
|
||||
{
|
||||
@ -685,7 +685,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
);;
|
||||
}
|
||||
Logging.Info($"HandShake sucess, Player:{packet.Username} PedID:{packet.PedID}");
|
||||
Program.Logger.Info($"HandShake sucess, Player:{packet.Username} PedID:{packet.PedID}");
|
||||
NetOutgoingMessage outgoingMessage = MainNetServer.CreateMessage();
|
||||
|
||||
// Create a new handshake packet
|
||||
@ -755,7 +755,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Info($"Player {localClient.Player.Username} connected!");
|
||||
Program.Logger.Info($"Player {localClient.Player.Username} connected!");
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(MainSettings.WelcomeMessage))
|
||||
@ -794,7 +794,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Info($"Player {localClient.Player.Username} disconnected! ID:{playerPedID}");
|
||||
Program.Logger.Info($"Player {localClient.Player.Username} disconnected! ID:{playerPedID}");
|
||||
}
|
||||
}
|
||||
|
||||
@ -987,7 +987,7 @@ namespace RageCoop.Server
|
||||
|
||||
SendChatMessage(packet.Username, packet.Message, targets);
|
||||
|
||||
Logging.Info(packet.Username + ": " + packet.Message);
|
||||
Program.Logger.Info(packet.Username + ": " + packet.Message);
|
||||
}
|
||||
|
||||
public static void SendChatMessage(string username, string message, List<NetConnection> targets = null)
|
||||
|
@ -258,7 +258,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,7 +279,7 @@ namespace RageCoop.Server
|
||||
|
||||
if (args != null && args.Length == 0)
|
||||
{
|
||||
Logging.Error($"[ServerScript->SendNativeCallToAll(ulong hash, params object[] args)]: args is not null!");
|
||||
Program.Logger.Error($"[ServerScript->SendNativeCallToAll(ulong hash, params object[] args)]: args is not null!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -295,7 +295,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
@ -341,7 +341,7 @@ namespace RageCoop.Server
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logging.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
Program.Logger.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user