Some fixes

This commit is contained in:
Sardelka
2022-06-12 17:11:14 +08:00
parent 734788f310
commit 82cffea6ed
9 changed files with 17 additions and 286 deletions

View File

@ -36,7 +36,7 @@ namespace RageCoop.Client
internal static Core.Logging.Logger Logger = null; internal static Core.Logging.Logger Logger = null;
internal static ulong Ticked = 0; internal static ulong Ticked = 0;
internal static Scripting.Resources Resources = new Scripting.Resources(); internal static Scripting.Resources Resources=null;
private static List<Func<bool>> QueuedActions = new List<Func<bool>>(); private static List<Func<bool>> QueuedActions = new List<Func<bool>>();
/// <summary> /// <summary>
/// Don't use it! /// Don't use it!
@ -54,6 +54,7 @@ namespace RageCoop.Client
LogLevel=Settings.LogLevel, LogLevel=Settings.LogLevel,
#endif #endif
}; };
Resources = new Scripting.Resources();
// Required for some synchronization! // Required for some synchronization!
/*if (Game.Version < GameVersion.v1_0_1290_1_Steam) /*if (Game.Version < GameVersion.v1_0_1290_1_Steam)
{ {

View File

@ -9,6 +9,11 @@
<ProduceReferenceAssembly>False</ProduceReferenceAssembly> <ProduceReferenceAssembly>False</ProduceReferenceAssembly>
<GenerateDocumentationFile>False</GenerateDocumentationFile> <GenerateDocumentationFile>False</GenerateDocumentationFile>
<DocumentationFile></DocumentationFile> <DocumentationFile></DocumentationFile>
<DebugType>portable</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>True</Optimize>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DotNetZip" Version="1.16.0" /> <PackageReference Include="DotNetZip" Version="1.16.0" />

View File

@ -6,7 +6,7 @@ namespace RageCoop.Client.Scripting
{ {
internal class Resources:ResourceLoader internal class Resources:ResourceLoader
{ {
public Resources() : base(typeof(ClientScript), Main.Logger) { } public Resources() : base("RageCoop.Client.Scripting.ClientScript", Main.Logger) { }
private void StartAll() private void StartAll()
{ {
lock (LoadedResources) lock (LoadedResources)
@ -15,6 +15,7 @@ namespace RageCoop.Client.Scripting
{ {
foreach (var s in d.Scripts) foreach (var s in d.Scripts)
{ {
(s as ClientScript).CurrentDirectory=d.Directory;
Main.QueueAction(() => s.OnStart()); Main.QueueAction(() => s.OnStart());
} }
} }
@ -51,7 +52,7 @@ namespace RageCoop.Client.Scripting
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
foreach (var resource in Directory.GetDirectories(path)) foreach (var resource in Directory.GetDirectories(path))
{ {
Logger.Info($"Loading resource: {Path.GetFileName(resource)}"); Logger?.Info($"Loading resource: {Path.GetFileName(resource)}");
LoadResource(resource); LoadResource(resource);
} }
StartAll(); StartAll();

View File

@ -75,6 +75,7 @@ namespace RageCoop.Core.Logging
lock (Buffer) lock (Buffer)
{ {
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(),"\r\n"+ex.ToString(), Process.GetCurrentProcess().Id); string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(),"\r\n"+ex.ToString(), Process.GetCurrentProcess().Id);
// msg += string.Format("\r\n[{0}][{2}] [ERR] {1}", Date(), "\r\n"+ex.StackTrace, Process.GetCurrentProcess().Id);
Buffer+=msg+"\r\n"; Buffer+=msg+"\r\n";
} }

View File

@ -6,6 +6,7 @@
<AssemblyVersion>0.1</AssemblyVersion> <AssemblyVersion>0.1</AssemblyVersion>
<FileVersion>0.1</FileVersion> <FileVersion>0.1</FileVersion>
<Version>0.1</Version> <Version>0.1</Version>
<DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0|AnyCPU'">

View File

@ -11,7 +11,7 @@ namespace RageCoop.Core.Scripting
{ {
public string Name { get; set; } public string Name { get; set; }
public string Directory { get; set; } public string Directory { get; set; }
public List<IScriptable> Scripts { get; set; } public List<IScriptable> Scripts { get; set; }=new List<IScriptable>();
} }
internal class ResourceLoader internal class ResourceLoader
{ {
@ -25,9 +25,9 @@ namespace RageCoop.Core.Scripting
protected List<Resource> LoadedResources = new List<Resource>(); protected List<Resource> LoadedResources = new List<Resource>();
private string BaseScriptType; private string BaseScriptType;
public Logging.Logger Logger { get; set; } public Logging.Logger Logger { get; set; }
public ResourceLoader(Type basetype,Logging.Logger logger) public ResourceLoader(string baseType,Logging.Logger logger)
{ {
BaseScriptType = basetype.GetType().FullName; BaseScriptType = baseType;
Logger = logger; Logger = logger;
} }
/// <summary> /// <summary>

View File

@ -1,279 +0,0 @@
using System.IO;
using System.Linq;
using System.Collections.Generic;
using RageCoop.Core;
using Lidgren.Network;
/*
namespace RageCoop.Server.Obsolete
{
public static class DownloadManager
{
private static readonly List<long> _clientsToDelete = new();
private static readonly List<DownloadClient> _clients = new();
private static readonly List<DownloadFile> _files = new();
public static bool AnyFileExists = false;
public static void InsertClient(long nethandle)
{
lock (_clients)
{
_clients.Add(new DownloadClient(nethandle, new(_files)));
}
}
public static bool CheckForDirectoryAndFiles()
{
if (!Directory.Exists("clientside"))
{
return false;
}
string[] filePaths = Directory.GetFiles("clientside");
if (filePaths.Length == 0)
{
return false;
}
byte fileCount = 0;
foreach (string file in filePaths)
{
FileInfo fileInfo = new(file);
// ONLY JAVASCRIPT AND JSON FILES!
if (!new string[] { ".js", ".xml" }.Any(x => x == fileInfo.Extension))
{
Program.Logger.Warning("Only files with \"*.js\" and \"*.xml\" can be sent!");
continue;
}
const int MAX_BUFFER = 5120; // 5KB
byte[] buffer = new byte[MAX_BUFFER];
ushort bytesRead = 0;
bool fileCreated = false;
DownloadFile newFile = null;
using (FileStream fs = File.Open(file, FileMode.Open, FileAccess.Read))
using (BufferedStream bs = new(fs))
{
while ((bytesRead = (ushort)bs.Read(buffer, 0, MAX_BUFFER)) != 0) // Reading 5KB chunks at time
{
if (!fileCreated && (fileCreated = true))
{
newFile = new()
{
FileID = fileCount,
FileName = fileInfo.Name,
FileLength = fileInfo.Length,
FileChunks = new()
};
}
newFile.FileChunks.Add(buffer.Take(bytesRead).ToArray());
}
}
_files.Add(newFile);
fileCount++;
}
AnyFileExists = true;
return true;
}
public static void Tick()
{
lock (_clients)
{
lock (_clientsToDelete)
{
foreach (long nethandle in _clientsToDelete)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client != null)
{
client.Finish();
_clients.Remove(client);
}
}
_clientsToDelete.Clear();
}
_clients.ForEach(client =>
{
if (client.SendFiles())
{
lock (Server.Clients)
{
Client x = Util.GetClientByNetID(client.NetHandle);
if (x != null)
{
x.FilesReceived = true;
}
}
AddClientToRemove(client.NetHandle);
}
});
}
}
public static void RemoveClient(long nethandle)
{
lock (_clients)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client != null)
{
_clients.Remove(client);
}
}
}
/// <summary>
/// We try to remove the client when all files have been sent
/// </summary>
/// <param name="nethandle"></param>
/// <param name="id"></param>
public static void TryToRemoveClient(long nethandle, int id)
{
lock (_clients)
{
DownloadClient client = _clients.FirstOrDefault(x => x.NetHandle == nethandle);
if (client == null)
{
return;
}
if (!client.DownloadComplete() && client.IsCurrentFile(id))
{
client.FilePosition++;
client.FileDataPosition = 0;
}
if (client.DownloadComplete())
{
AddClientToRemove(client.NetHandle);
}
}
}
public static void AddClientToRemove(long nethandle)
{
lock (_clientsToDelete)
{
if (!_clientsToDelete.Contains(nethandle))
{
_clientsToDelete.Add(nethandle);
}
}
}
}
public class DownloadClient
{
public long NetHandle = 0;
private readonly List<DownloadFile> _files = null;
public int FilePosition = 0;
public int FileDataPosition = 0;
public DownloadClient(long nethandle, List<DownloadFile> files)
{
NetHandle = nethandle;
_files = files;
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == NetHandle);
if (conn == null)
{
DownloadManager.AddClientToRemove(NetHandle);
return;
}
_files.ForEach(file =>
{
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.FileTransferRequest()
{
ID = file.FileID,
FileName = file.FileName,
FileLength = file.FileLength
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
});
}
public void Finish()
{
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == NetHandle);
if (conn == null)
{
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.FileTransferComplete()
{
ID = 0x0
}.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
}
/// <summary>
///
/// </summary>
/// <returns>true if we are done otherwise false</returns>
public bool SendFiles()
{
DownloadFile file = _files[FilePosition];
Send(NetHandle, file);
if (FileDataPosition >= file.FileChunks.Count)
{
FilePosition++;
FileDataPosition = 0;
}
return DownloadComplete();
}
private void Send(long nethandle, DownloadFile file)
{
NetConnection conn = Server.MainNetServer.Connections.FirstOrDefault(x => x.RemoteUniqueIdentifier == nethandle);
if (conn == null)
{
DownloadManager.AddClientToRemove(NetHandle);
return;
}
NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage();
new Packets.FileTransferTick() { ID = file.FileID, FileChunk = file.FileChunks[FileDataPosition++] }.Pack(outgoingMessage);
Server.MainNetServer.SendMessage(outgoingMessage, conn, NetDeliveryMethod.ReliableOrdered, (byte)ConnectionChannel.File);
}
public bool IsCurrentFile(int id)
{
return _files[FilePosition].FileID == id;
}
public bool DownloadComplete()
{
return FilePosition >= _files.Count;
}
}
public class DownloadFile
{
public byte FileID { get; set; } = 0;
public string FileName { get; set; } = string.Empty;
public long FileLength { get; set; } = 0;
public List<byte[]> FileChunks { get; set; } = null;
}
}
*/

View File

@ -13,6 +13,7 @@
<PackageId>RAGECOOP-V</PackageId> <PackageId>RAGECOOP-V</PackageId>
<Authors>RAGECOOP</Authors> <Authors>RAGECOOP</Authors>
<Version>0.4</Version> <Version>0.4</Version>
<DebugType>embedded</DebugType>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -11,7 +11,7 @@ namespace RageCoop.Server.Scripting
{ {
internal class Resources : ResourceLoader internal class Resources : ResourceLoader
{ {
public Resources() : base(typeof(ServerScript), Program.Logger) { } public Resources() : base("RageCoop.Server.Scripting.ServerScript", Program.Logger) { }
public static bool HasClientResources = false; public static bool HasClientResources = false;
public void LoadAll() public void LoadAll()