Initial migration commit to .NET 7

Menu, sync and other stuff except resource system should be working.
We're far from finished
This commit is contained in:
Sardelka9515
2023-01-28 20:51:29 +08:00
parent 0112140f0e
commit cac2385c35
107 changed files with 36610 additions and 267320 deletions

View File

@ -203,12 +203,6 @@ namespace RageCoop.Client.Scripting
/// </summary>
public static Version CurrentVersion => Main.Version;
/// <summary>
/// Get a <see cref="Core.Logger" /> that RAGECOOP is currently using.
/// </summary>
/// <returns></returns>
public static Logger Logger => Main.Logger;
/// <summary>
/// Get all players indexed by their ID
/// </summary>

View File

@ -35,34 +35,34 @@ namespace RageCoop.Client.Scripting
API.RegisterCustomEventHandler(CustomEvents.WeatherTimeSync, WeatherTimeSync);
API.RegisterCustomEventHandler(CustomEvents.OnPlayerDied,
e => { Notification.Show($"~h~{e.Args[0]}~h~ died."); });
Task.Run(() =>
{
while (true)
{
if (_isHost)
API.QueueAction(() =>
{
unsafe
{
var time = World.CurrentTimeOfDay;
var weather1 = default(int);
var weather2 = default(int);
var percent2 = default(float);
Function.Call(Hash.GET_CURR_WEATHER_STATE, &weather1, &weather2, &percent2);
API.SendCustomEvent(CustomEvents.WeatherTimeSync, time.Hours, time.Minutes,
time.Seconds, weather1, weather2, percent2);
}
});
ThreadManager.CreateThread(() =>
{
while (!Main.IsUnloading)
{
if (_isHost)
API.QueueAction(() =>
{
unsafe
{
var time = World.CurrentTimeOfDay;
var weather1 = default(int);
var weather2 = default(int);
var percent2 = default(float);
Call(GET_CURR_WEATHER_STATE, &weather1, &weather2, &percent2);
API.SendCustomEvent(CustomEvents.WeatherTimeSync, time.Hours, time.Minutes,
time.Seconds, weather1, weather2, percent2);
}
});
Thread.Sleep(1000);
}
});
Thread.Sleep(1000);
}
},"BaseScript");
}
private static void WeatherTimeSync(CustomEventReceivedArgs e)
{
World.CurrentTimeOfDay = new TimeSpan((int)e.Args[0], (int)e.Args[1], (int)e.Args[2]);
Function.Call(Hash.SET_CURR_WEATHER_STATE, (int)e.Args[3], (int)e.Args[4], (float)e.Args[5]);
Call(SET_CURR_WEATHER_STATE, (int)e.Args[3], (int)e.Args[4], (float)e.Args[5]);
}
private static void SetDisplayNameTag(CustomEventReceivedArgs e)
@ -193,7 +193,7 @@ namespace RageCoop.Client.Scripting
if (returnType == TypeCode.Empty)
{
Function.Call(hash, arguments.ToArray());
Call(hash, arguments.ToArray());
return;
}
@ -205,59 +205,59 @@ namespace RageCoop.Client.Scripting
{
case TypeCode.Boolean:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<bool>(hash, arguments.ToArray()));
Call<bool>(hash, arguments.ToArray()));
break;
case TypeCode.Byte:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<byte>(hash, arguments.ToArray()));
Call<byte>(hash, arguments.ToArray()));
break;
case TypeCode.Char:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<char>(hash, arguments.ToArray()));
Call<char>(hash, arguments.ToArray()));
break;
case TypeCode.Single:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<float>(hash, arguments.ToArray()));
Call<float>(hash, arguments.ToArray()));
break;
case TypeCode.Double:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<double>(hash, arguments.ToArray()));
Call<double>(hash, arguments.ToArray()));
break;
case TypeCode.Int16:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<short>(hash, arguments.ToArray()));
Call<short>(hash, arguments.ToArray()));
break;
case TypeCode.Int32:
API.SendCustomEvent(CustomEvents.NativeResponse, id, Function.Call<int>(hash, arguments.ToArray()));
API.SendCustomEvent(CustomEvents.NativeResponse, id, Call<int>(hash, arguments.ToArray()));
break;
case TypeCode.Int64:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<long>(hash, arguments.ToArray()));
Call<long>(hash, arguments.ToArray()));
break;
case TypeCode.String:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<string>(hash, arguments.ToArray()));
Call<string>(hash, arguments.ToArray()));
break;
case TypeCode.UInt16:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<ushort>(hash, arguments.ToArray()));
Call<ushort>(hash, arguments.ToArray()));
break;
case TypeCode.UInt32:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<uint>(hash, arguments.ToArray()));
Call<uint>(hash, arguments.ToArray()));
break;
case TypeCode.UInt64:
API.SendCustomEvent(CustomEvents.NativeResponse, id,
Function.Call<ulong>(hash, arguments.ToArray()));
Call<ulong>(hash, arguments.ToArray()));
break;
}
}
@ -288,7 +288,7 @@ namespace RageCoop.Client.Scripting
case string stuff:
return stuff;
default:
return null;
return default;
}
}
}

View File

@ -4,10 +4,6 @@ using RageCoop.Core.Scripting;
namespace RageCoop.Client.Scripting
{
/// <summary>
/// Inherit from this class, constructor will be called automatically, but other scripts might have yet been loaded,
/// you should use <see cref="OnStart" />. to initiate your script.
/// </summary>
public abstract class ClientScript : Script
{
/// <summary>
@ -24,16 +20,5 @@ namespace RageCoop.Client.Scripting
/// Eqivalent of <see cref="ClientResource.Logger" /> in <see cref="CurrentResource" />
/// </summary>
public Logger Logger => CurrentResource.Logger;
/// <summary>
/// This method would be called from main thread, right after all script constructors are invoked.
/// </summary>
public abstract void OnStart();
/// <summary>
/// This method would be called from main thread right before the whole <see cref="System.AppDomain" /> is unloded but
/// prior to <see cref="GTA.Script.Aborted" />.
/// </summary>
public abstract void OnStop();
}
}

View File

@ -5,10 +5,8 @@ using System.IO;
using System.Linq;
using System.Reflection;
using ICSharpCode.SharpZipLib.Zip;
using RageCoop.Client.Loader;
using RageCoop.Core;
using RageCoop.Core.Scripting;
using SHVDN;
namespace RageCoop.Client.Scripting
{
@ -90,31 +88,19 @@ namespace RageCoop.Client.Scripting
Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories).Where(x => x.CanBeIgnored())
.ForEach(x => File.Delete(x));
// Load it in main thread
API.QueueActionAndWait(() =>
{
Main.QueueToMainThreadAndWait(() =>
{
foreach (var res in LoadedResources)
Directory.GetFiles(res.Value.ScriptsDirectory, "*.dll", SearchOption.AllDirectories)
.ForEach(x => ScriptDomain.CurrentDomain.StartScripts(x));
SetupScripts();
});
});
// TODO Core.ScheduleLoad()...
}
public void Unload()
{
StopScripts();
LoadedResources.Clear();
LoaderContext.RequestUnload();
// TODO Core.ScheduleUnload()...
}
private ClientResource Unpack(string zipPath, string dataFolderRoot)
{
var r = new ClientResource
{
Logger = API.Logger,
Logger = Logger,
Scripts = new List<ClientScript>(),
Name = Path.GetFileNameWithoutExtension(zipPath),
DataFolder = Path.Combine(dataFolderRoot, Path.GetFileNameWithoutExtension(zipPath)),
@ -147,7 +133,7 @@ namespace RageCoop.Client.Scripting
}
catch (Exception ex)
{
API.Logger.Warning(
Logger.Warning(
$"Failed to delete API assembly: {file}. This may or may cause some unexpected behaviours.\n{ex}");
}
@ -167,53 +153,5 @@ namespace RageCoop.Client.Scripting
LoadedResources.TryAdd(r.Name, r);
return r;
}
private void SetupScripts()
{
foreach (var s in GetClientScripts())
{
try
{
API.Logger.Debug("Starting script: " + s.GetType().FullName);
var script = (ClientScript)s;
if (LoadedResources.TryGetValue(Directory.GetParent(script.Filename).Name, out var r))
script.CurrentResource = r;
else
API.Logger.Warning("Failed to locate resource for script: " + script.Filename);
var res = script.CurrentResource;
script.CurrentFile = res?.Files.Values.Where(x =>
x.Name.ToLower() == script.Filename.Substring(res.ScriptsDirectory.Length + 1)
.Replace('\\', '/')).FirstOrDefault();
res?.Scripts.Add(script);
script.OnStart();
}
catch (Exception ex)
{
API.Logger.Error($"Failed to start {s.GetType().FullName}", ex);
}
API.Logger.Debug("Started script: " + s.GetType().FullName);
}
}
private void StopScripts()
{
foreach (var s in GetClientScripts())
try
{
API.Logger.Debug("Stopping script: " + s.GetType().FullName);
((ClientScript)s).OnStop();
}
catch (Exception ex)
{
API.Logger.Error($"Failed to stop {s.GetType().FullName}", ex);
}
}
public static object[] GetClientScripts()
{
return ScriptDomain.CurrentDomain.RunningScripts.Where(x =>
x.ScriptInstance.GetType().IsScript(typeof(ClientScript))).Select(x => x.ScriptInstance).ToArray();
}
}
}