2022-05-31 19:35:01 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.IO;
|
2022-06-12 15:39:32 +08:00
|
|
|
|
using System.Linq;
|
2022-06-22 08:58:36 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2022-06-24 16:49:59 +08:00
|
|
|
|
using ICSharpCode.SharpZipLib.Zip;
|
2022-05-31 19:35:01 -08:00
|
|
|
|
|
2022-06-22 08:58:36 +08:00
|
|
|
|
[assembly: InternalsVisibleTo("RageCoop.Server")]
|
|
|
|
|
[assembly: InternalsVisibleTo("RageCoop.Client")]
|
2022-06-12 15:39:32 +08:00
|
|
|
|
namespace RageCoop.Core.Scripting
|
2022-05-31 19:35:01 -08:00
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
|
2022-06-12 15:39:32 +08:00
|
|
|
|
internal class ResourceLoader
|
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
protected List<string> ToIgnore = new List<string>
|
|
|
|
|
{
|
|
|
|
|
"RageCoop.Client.dll",
|
|
|
|
|
"RageCoop.Core.dll",
|
|
|
|
|
"RageCoop.Server.dll",
|
|
|
|
|
"ScriptHookVDotNet3.dll"
|
|
|
|
|
};
|
|
|
|
|
protected List<Resource> LoadedResources = new List<Resource>();
|
|
|
|
|
private string BaseScriptType;
|
2022-06-22 08:58:36 +08:00
|
|
|
|
public Logger Logger { get; set; }
|
|
|
|
|
public ResourceLoader(string baseType,Logger logger)
|
2022-06-11 18:41:10 +08:00
|
|
|
|
{
|
2022-06-12 17:11:14 +08:00
|
|
|
|
BaseScriptType = baseType;
|
2022-06-12 15:39:32 +08:00
|
|
|
|
Logger = logger;
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2022-06-24 16:49:59 +08:00
|
|
|
|
/// Load a resource from a directory.
|
2022-06-11 18:41:10 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Path of the directory.</param>
|
2022-06-24 16:49:59 +08:00
|
|
|
|
protected void LoadResource(string path,string dataFolderRoot)
|
2022-06-11 18:41:10 +08:00
|
|
|
|
{
|
|
|
|
|
var r = new Resource()
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Scripts = new List<Scriptable>(),
|
2022-06-23 14:10:16 +08:00
|
|
|
|
Name=Path.GetFileName(path),
|
2022-06-24 16:49:59 +08:00
|
|
|
|
DataFolder=Path.Combine(dataFolderRoot, Path.GetFileName(path))
|
2022-06-11 18:41:10 +08:00
|
|
|
|
};
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Directory.CreateDirectory(r.DataFolder);
|
|
|
|
|
foreach (var dir in Directory.GetDirectories(path, "*", SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
r.Files.Add(dir, new ResourceFile()
|
|
|
|
|
{
|
|
|
|
|
IsDirectory=true,
|
|
|
|
|
Name=dir.Substring(path.Length+1)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
foreach (var file in Directory.GetFiles(path,"*",SearchOption.AllDirectories))
|
|
|
|
|
{
|
|
|
|
|
var relativeName = file.Substring(path.Length+1);
|
|
|
|
|
var rfile = new ResourceFile()
|
|
|
|
|
{
|
|
|
|
|
GetStream=() => { return new FileStream(file, FileMode.Open, FileAccess.Read); },
|
|
|
|
|
IsDirectory=false,
|
|
|
|
|
Name=relativeName
|
|
|
|
|
};
|
|
|
|
|
if (file.EndsWith(".dll"))
|
|
|
|
|
{
|
|
|
|
|
LoadScriptsFromAssembly(rfile,file, r);
|
|
|
|
|
}
|
|
|
|
|
r.Files.Add(relativeName,rfile);
|
|
|
|
|
}
|
|
|
|
|
LoadedResources.Add(r);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Load a resource from a zip
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="file"></param>
|
|
|
|
|
protected void LoadResource(ZipFile file,string dataFolderRoot)
|
|
|
|
|
{
|
|
|
|
|
var r = new Resource()
|
2022-06-11 18:41:10 +08:00
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Scripts = new List<Scriptable>(),
|
|
|
|
|
Name=Path.GetFileNameWithoutExtension(file.Name),
|
|
|
|
|
DataFolder=Path.Combine(dataFolderRoot, Path.GetFileNameWithoutExtension(file.Name))
|
|
|
|
|
};
|
|
|
|
|
Directory.CreateDirectory(r.DataFolder);
|
|
|
|
|
|
|
|
|
|
foreach (ZipEntry entry in file)
|
|
|
|
|
{
|
|
|
|
|
ResourceFile rFile;
|
|
|
|
|
r.Files.Add(entry.Name, rFile=new ResourceFile()
|
|
|
|
|
{
|
|
|
|
|
Name=entry.Name,
|
|
|
|
|
IsDirectory=entry.IsDirectory,
|
|
|
|
|
});
|
|
|
|
|
if (!entry.IsDirectory)
|
|
|
|
|
{
|
|
|
|
|
rFile.GetStream=() => { return file.GetInputStream(entry); };
|
|
|
|
|
if (entry.Name.EndsWith(".dll"))
|
|
|
|
|
{
|
|
|
|
|
var tmp = Path.GetTempFileName();
|
|
|
|
|
var f = File.OpenWrite(tmp);
|
|
|
|
|
rFile.GetStream().CopyTo(f);
|
|
|
|
|
f.Close();
|
|
|
|
|
LoadScriptsFromAssembly(rFile, tmp, r, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
LoadedResources.Add(r);
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads scripts from the specified assembly file.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">The path to the assembly file to load.</param>
|
|
|
|
|
/// <returns><see langword="true" /> on success, <see langword="false" /> otherwise</returns>
|
2022-06-24 16:49:59 +08:00
|
|
|
|
private bool LoadScriptsFromAssembly(ResourceFile file,string path, Resource resource,bool shadowCopy=true)
|
2022-06-11 18:41:10 +08:00
|
|
|
|
{
|
|
|
|
|
lock (LoadedResources)
|
|
|
|
|
{
|
|
|
|
|
if (!IsManagedAssembly(path)) { return false; }
|
2022-06-24 16:49:59 +08:00
|
|
|
|
if (ToIgnore.Contains(file.Name)) { try { File.Delete(path); } catch { }; return false; }
|
2022-06-11 18:41:10 +08:00
|
|
|
|
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Logger?.Debug($"Loading assembly {file.Name} ...");
|
2022-06-11 18:41:10 +08:00
|
|
|
|
|
|
|
|
|
Assembly assembly;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
if (shadowCopy)
|
|
|
|
|
{
|
|
|
|
|
var temp = Path.GetTempFileName();
|
|
|
|
|
File.Copy(path, temp, true);
|
|
|
|
|
assembly = Assembly.LoadFrom(temp);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
assembly = Assembly.LoadFrom(path);
|
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Logger?.Error("Unable to load "+file.Name);
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Logger?.Error(ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 16:49:59 +08:00
|
|
|
|
return LoadScriptsFromAssembly(file,assembly, path, resource);
|
2022-06-11 18:41:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/// <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>
|
2022-06-24 16:49:59 +08:00
|
|
|
|
private bool LoadScriptsFromAssembly(ResourceFile rfile,Assembly assembly, string filename, Resource toload)
|
2022-06-11 18:41:10 +08:00
|
|
|
|
{
|
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Find all script types in the assembly
|
|
|
|
|
foreach (var type in assembly.GetTypes().Where(x => IsSubclassOf(x, BaseScriptType)))
|
|
|
|
|
{
|
|
|
|
|
ConstructorInfo constructor = type.GetConstructor(System.Type.EmptyTypes);
|
|
|
|
|
if (constructor != null && constructor.IsPublic)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// Invoke script constructor
|
2022-06-24 16:49:59 +08:00
|
|
|
|
var script = constructor.Invoke(null) as Scriptable;
|
|
|
|
|
script.CurrentResource = toload;
|
|
|
|
|
script.CurrentFile=rfile;
|
|
|
|
|
toload.Scripts.Add(script);
|
2022-06-11 18:41:10 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Logger?.Error($"Failed to load assembly {rfile.Name}: ");
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Logger?.Error(ex);
|
2022-06-12 15:39:32 +08:00
|
|
|
|
foreach (var e in ex.LoaderExceptions)
|
|
|
|
|
{
|
2022-06-11 18:41:10 +08:00
|
|
|
|
Logger?.Error(e);
|
2022-06-12 15:39:32 +08:00
|
|
|
|
}
|
2022-06-11 18:41:10 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 16:49:59 +08:00
|
|
|
|
Logger?.Info($"Loaded {count} script(s) in {rfile.Name}");
|
2022-06-11 18:41:10 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-31 19:35:01 -08:00
|
|
|
|
}
|