Files
RAGECOOP-V/Client/Scripts/Scripting/Resources.cs

111 lines
3.6 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-10-09 23:35:30 +08:00
using System.Collections.Concurrent;
2022-07-01 12:22:31 +08:00
using System.Collections.Generic;
2022-07-20 17:50:01 +08:00
using System.IO;
using System.Linq;
using System.Reflection;
2022-10-23 19:02:39 +08:00
using ICSharpCode.SharpZipLib.Zip;
using RageCoop.Core;
using RageCoop.Core.Scripting;
namespace RageCoop.Client.Scripting
{
2022-07-20 17:50:01 +08:00
internal class Resources
{
2022-10-19 19:07:29 +08:00
public static string TempPath;
2022-10-23 19:02:39 +08:00
2023-02-12 22:06:57 +08:00
internal readonly ConcurrentDictionary<string, ClientResource> LoadedResources = new();
2022-10-23 19:02:39 +08:00
2022-10-19 19:07:29 +08:00
static Resources()
{
TempPath = Path.Combine(Path.GetTempPath(), "RageCoop");
2022-10-23 19:02:39 +08:00
if (Directory.Exists(TempPath))
try
{
Directory.Delete(TempPath, true);
}
catch
{
}
2022-10-19 19:07:29 +08:00
TempPath = CoreUtils.GetTempDirectory(TempPath);
Directory.CreateDirectory(TempPath);
}
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public void Load(string path, string[] zips)
{
LoadedResources.Clear();
foreach (var zip in zips)
{
var zipPath = Path.Combine(path, zip);
2023-02-13 20:44:50 +08:00
Log?.Info($"Loading resource: {Path.GetFileNameWithoutExtension(zip)}");
2022-10-09 23:35:30 +08:00
Unpack(zipPath, Path.Combine(path, "Data"));
2022-07-20 17:50:01 +08:00
}
2022-10-23 19:02:39 +08:00
Directory.GetFiles(path, "*.dll", SearchOption.AllDirectories).Where(x => x.CanBeIgnored())
2023-02-12 22:06:57 +08:00
.ForEach(File.Delete);
2023-02-13 17:51:18 +08:00
// TODO: Core.ScheduleLoad()...
2022-07-20 17:50:01 +08:00
}
2022-10-23 19:02:39 +08:00
2022-07-20 17:50:01 +08:00
public void Unload()
{
2023-02-13 17:51:18 +08:00
// TODO: Core.ScheduleUnload()...
}
2022-07-01 13:54:18 +08:00
2022-10-19 19:07:29 +08:00
private ClientResource Unpack(string zipPath, string dataFolderRoot)
2022-07-20 17:50:01 +08:00
{
2022-10-23 19:02:39 +08:00
var r = new ClientResource
2022-07-20 17:50:01 +08:00
{
2022-10-09 23:35:30 +08:00
Name = Path.GetFileNameWithoutExtension(zipPath),
2022-10-10 16:45:58 +08:00
DataFolder = Path.Combine(dataFolderRoot, Path.GetFileNameWithoutExtension(zipPath)),
2022-10-23 19:02:39 +08:00
ScriptsDirectory = Path.Combine(TempPath, Path.GetFileNameWithoutExtension(zipPath))
2022-07-20 17:50:01 +08:00
};
Directory.CreateDirectory(r.DataFolder);
2022-10-10 16:45:58 +08:00
var scriptsDir = r.ScriptsDirectory;
2022-10-23 19:02:39 +08:00
if (Directory.Exists(scriptsDir))
Directory.Delete(scriptsDir, true);
else if (File.Exists(scriptsDir)) File.Delete(scriptsDir);
2022-10-10 16:45:58 +08:00
Directory.CreateDirectory(scriptsDir);
2022-10-09 23:35:30 +08:00
2022-10-10 16:45:58 +08:00
new FastZip().ExtractZip(zipPath, scriptsDir, null);
2022-10-23 19:02:39 +08:00
2022-07-01 12:22:31 +08:00
2022-10-10 16:45:58 +08:00
foreach (var dir in Directory.GetDirectories(scriptsDir, "*", SearchOption.AllDirectories))
2022-10-23 19:02:39 +08:00
r.Files.Add(dir, new ResourceFile
2022-07-20 17:50:01 +08:00
{
2022-10-09 23:35:30 +08:00
IsDirectory = true,
2022-10-10 16:45:58 +08:00
Name = dir.Substring(scriptsDir.Length + 1).Replace('\\', '/')
2022-10-09 23:35:30 +08:00
});
2022-10-10 16:45:58 +08:00
foreach (var file in Directory.GetFiles(scriptsDir, "*", SearchOption.AllDirectories))
2022-07-20 17:50:01 +08:00
{
2022-10-15 13:52:49 +08:00
if (Path.GetFileName(file).CanBeIgnored())
{
try
{
File.Delete(file);
}
catch (Exception ex)
{
2023-02-13 20:44:50 +08:00
Log.Warning(
2022-10-23 19:02:39 +08:00
$"Failed to delete API assembly: {file}. This may or may cause some unexpected behaviours.\n{ex}");
2022-10-15 13:52:49 +08:00
}
2022-10-23 19:02:39 +08:00
2022-10-15 13:52:49 +08:00
continue;
}
2022-10-23 19:02:39 +08:00
2022-10-10 16:45:58 +08:00
var relativeName = file.Substring(scriptsDir.Length + 1).Replace('\\', '/');
2023-02-13 17:51:18 +08:00
var rfile = new ClientFile
2022-07-20 17:50:01 +08:00
{
2022-10-09 23:35:30 +08:00
IsDirectory = false,
Name = relativeName
};
r.Files.Add(relativeName, rfile);
2022-07-20 17:50:01 +08:00
}
2022-07-01 12:22:31 +08:00
LoadedResources.TryAdd(r.Name, r);
2022-10-19 19:07:29 +08:00
return r;
}
2022-07-20 17:50:01 +08:00
}
2022-10-23 19:02:39 +08:00
}