Add resource package support
This commit is contained in:
@ -303,10 +303,12 @@ namespace RageCoop.Server
|
||||
}
|
||||
internal void SendFile(Stream stream, string name, Client client,int id=default, Action<float> updateCallback = null)
|
||||
{
|
||||
|
||||
id = id ==default? NewFileID(): id ;
|
||||
stream.Seek(0, SeekOrigin.Begin);
|
||||
// Logger.Debug("1");
|
||||
id = id ==default? NewFileID(): id ;
|
||||
// Logger.Debug("2");
|
||||
var total = stream.Length;
|
||||
// Logger.Debug("3");
|
||||
if (GetResponse<Packets.FileTransferResponse>(client, new Packets.FileTransferRequest()
|
||||
{
|
||||
FileLength= total,
|
||||
|
@ -15,7 +15,7 @@ using System.Resources;
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Version information
|
||||
[assembly: AssemblyVersion("1.5.2.84")]
|
||||
[assembly: AssemblyFileVersion("1.5.2.84")]
|
||||
[assembly: AssemblyVersion("1.5.2.98")]
|
||||
[assembly: AssemblyFileVersion("1.5.2.98")]
|
||||
[assembly: NeutralResourcesLanguageAttribute( "en-US" )]
|
||||
|
||||
|
@ -22,9 +22,42 @@ namespace RageCoop.Server.Scripting
|
||||
Server = server;
|
||||
Logger=server.Logger;
|
||||
}
|
||||
private List<string> ClientResourceZips=new List<string>();
|
||||
private Dictionary<string,Func<Stream>> ClientResources=new();
|
||||
private Dictionary<string,Func<Stream>> ResourceStreams=new();
|
||||
private List<MemoryStream> MemStreams = new();
|
||||
public void LoadAll()
|
||||
{
|
||||
// Packages
|
||||
{
|
||||
var path = Path.Combine("Resources", "Packages");
|
||||
Directory.CreateDirectory(path);
|
||||
foreach (var pkg in Directory.GetFiles(path,"*.respkg",SearchOption.AllDirectories))
|
||||
{
|
||||
Logger?.Debug($"Adding resourece from package \"{Path.GetFileNameWithoutExtension(pkg)}\"");
|
||||
var pkgZip = new ZipFile(pkg);
|
||||
foreach (ZipEntry e in pkgZip)
|
||||
{
|
||||
if (!e.IsFile) { continue; }
|
||||
if (e.Name.StartsWith("Client") && e.Name.EndsWith(".res"))
|
||||
{
|
||||
var stream = pkgZip.GetInputStream(e).ToMemStream();
|
||||
MemStreams.Add(stream);
|
||||
ClientResources.Add(Path.GetFileNameWithoutExtension(e.Name), () => stream);
|
||||
Logger?.Debug("Resource added: "+ Path.GetFileNameWithoutExtension(e.Name));
|
||||
}
|
||||
else if (e.Name.StartsWith("Server") && e.Name.EndsWith(".res"))
|
||||
{
|
||||
var stream = pkgZip.GetInputStream(e).ToMemStream();
|
||||
MemStreams.Add(stream);
|
||||
ResourceStreams.Add(Path.GetFileNameWithoutExtension(e.Name), () => stream);
|
||||
Logger?.Debug("Resource added: " + Path.GetFileNameWithoutExtension(e.Name));
|
||||
}
|
||||
}
|
||||
pkgZip.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Client
|
||||
{
|
||||
var path = Path.Combine("Resources", "Client");
|
||||
@ -45,22 +78,21 @@ namespace RageCoop.Server.Scripting
|
||||
var zipPath = Path.Combine(tmpDir, Path.GetFileName(resourceFolder))+".res";
|
||||
try
|
||||
{
|
||||
using (ZipFile zip = ZipFile.Create(zipPath))
|
||||
{
|
||||
zip.BeginUpdate();
|
||||
foreach (var dir in Directory.GetDirectories(resourceFolder, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
zip.AddDirectory(dir[(resourceFolder.Length + 1)..]);
|
||||
}
|
||||
foreach (var file in Directory.GetFiles(resourceFolder, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
zip.Add(file, file[(resourceFolder.Length + 1)..]);
|
||||
}
|
||||
zip.CommitUpdate();
|
||||
zip.Close();
|
||||
ClientResourceZips.Add(zipPath);
|
||||
}
|
||||
}
|
||||
using ZipFile zip = ZipFile.Create(zipPath);
|
||||
zip.BeginUpdate();
|
||||
foreach (var dir in Directory.GetDirectories(resourceFolder, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
zip.AddDirectory(dir[(resourceFolder.Length + 1)..]);
|
||||
}
|
||||
foreach (var file in Directory.GetFiles(resourceFolder, "*", SearchOption.AllDirectories))
|
||||
{
|
||||
if (Path.GetFileName(file).CanBeIgnored()) { continue; }
|
||||
zip.Add(file, file[(resourceFolder.Length + 1)..]);
|
||||
}
|
||||
zip.CommitUpdate();
|
||||
zip.Close();
|
||||
ClientResources.Add(Path.GetFileNameWithoutExtension(zipPath), () => File.OpenRead(zipPath));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger?.Error($"Failed to pack client resource:{resourceFolder}");
|
||||
@ -71,7 +103,10 @@ namespace RageCoop.Server.Scripting
|
||||
var packed = Directory.GetFiles(path, "*.res", SearchOption.TopDirectoryOnly);
|
||||
if (packed.Length>0)
|
||||
{
|
||||
ClientResourceZips.AddRange(packed);
|
||||
foreach(var file in packed)
|
||||
{
|
||||
ClientResources.Add(Path.GetFileNameWithoutExtension(file),()=>File.OpenRead(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,26 +136,34 @@ namespace RageCoop.Server.Scripting
|
||||
Logger?.Error(ex);
|
||||
}
|
||||
}
|
||||
foreach (var resource in Directory.GetFiles(path, "*.res", SearchOption.TopDirectoryOnly))
|
||||
foreach (var res in Directory.GetFiles(path, "*.res", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
try
|
||||
if (!ResourceStreams.TryAdd(Path.GetFileNameWithoutExtension(res),()=>File.OpenRead(res)))
|
||||
{
|
||||
Logger?.Warning($"Resource \"{res}\" cannot be loaded, ignoring...");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
foreach(var res in ResourceStreams)
|
||||
{
|
||||
try
|
||||
{
|
||||
var name = Path.GetFileNameWithoutExtension(resource);
|
||||
var name = res.Key;
|
||||
if (LoadedResources.ContainsKey(name))
|
||||
{
|
||||
Logger?.Warning($"Resource \"{name}\" has already been loaded, ignoring...");
|
||||
continue;
|
||||
}
|
||||
Logger?.Info($"Loading resource: "+name);
|
||||
var r = ServerResource.LoadFromZip(resource, Path.Combine("Resources", "Temp", "Server"), dataFolder, Logger);
|
||||
var r = ServerResource.LoadFrom(res.Value(),name, Path.Combine("Resources", "Temp", "Server"), dataFolder, Logger);
|
||||
LoadedResources.Add(r.Name, r);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger?.Error($"Failed to load resource: {Path.GetFileNameWithoutExtension(resource)}");
|
||||
Logger?.Error($"Failed to load resource: {res.Key}");
|
||||
Logger?.Error(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Start scripts
|
||||
lock (LoadedResources)
|
||||
@ -172,21 +215,31 @@ namespace RageCoop.Server.Scripting
|
||||
}
|
||||
LoadedResources.Clear();
|
||||
}
|
||||
foreach(var s in MemStreams)
|
||||
{
|
||||
try
|
||||
{
|
||||
s.Close();
|
||||
s.Dispose();
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Logger?.Error("[Resources.CloseMemStream]",ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void SendTo(Client client)
|
||||
{
|
||||
Task.Run(() =>
|
||||
{
|
||||
|
||||
if (ClientResourceZips.Count!=0)
|
||||
if (ClientResources.Count!=0)
|
||||
{
|
||||
Logger?.Info($"Sending resources to client:{client.Username}");
|
||||
foreach (var rs in ClientResourceZips)
|
||||
foreach (var rs in ClientResources)
|
||||
{
|
||||
using (var fs = File.OpenRead(rs))
|
||||
{
|
||||
Server.SendFile(rs, Path.GetFileName(rs), client);
|
||||
}
|
||||
Logger?.Debug(rs.Key);
|
||||
Server.SendFile(rs.Value(),rs.Key+".res", client);
|
||||
}
|
||||
|
||||
Logger?.Info($"Resources sent to:{client.Username}");
|
||||
|
@ -81,10 +81,10 @@ namespace RageCoop.Server.Scripting
|
||||
}
|
||||
return r;
|
||||
}
|
||||
internal static ServerResource LoadFromZip(string zipPath, string tmpDir, string dataFolder, Logger logger = null)
|
||||
internal static ServerResource LoadFrom(Stream input,string name, string tmpDir, string dataFolder, Logger logger = null)
|
||||
{
|
||||
tmpDir=Path.Combine(tmpDir, Path.GetFileNameWithoutExtension(zipPath));
|
||||
new FastZip().ExtractZip(zipPath, tmpDir, null);
|
||||
tmpDir=Path.Combine(tmpDir, name);
|
||||
new FastZip().ExtractZip(input, tmpDir, FastZip.Overwrite.Always,null,null,null,true,true);
|
||||
return LoadFrom(tmpDir, dataFolder, logger, true);
|
||||
}
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user