109 lines
3.9 KiB
C#
109 lines
3.9 KiB
C#
namespace TimelapseApi;
|
|
|
|
using System.Reflection;
|
|
using Eto.Forms;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
using System.IO.Compression;
|
|
using Newtonsoft.Json;
|
|
|
|
public class ExtensionLoader
|
|
{
|
|
private static GuiData? _data;
|
|
public static GuiData Data {set{
|
|
_data=value;
|
|
}}
|
|
|
|
public static async Task InstallExtension(string path,bool add=false)
|
|
{
|
|
string dirTmp = Api.GetInternalFile("ExtTMP");
|
|
if(Directory.Exists(dirTmp))
|
|
{
|
|
Directory.Delete(dirTmp,true);
|
|
}
|
|
ZipFile.ExtractToDirectory(path,dirTmp);
|
|
var info=GetExtensionInfo(Path.Combine(dirTmp,"extension_info.json"));
|
|
if(info != null)
|
|
{
|
|
|
|
Directory.CreateDirectory(Api.GetInternalFile("ExtensionInfo"));
|
|
if(Directory.Exists(Api.GetInternalFile("ExtensionBinaries",info.name)))
|
|
{
|
|
Directory.Delete(Api.GetInternalFile("ExtensionBinaries",info.name),true);
|
|
}
|
|
File.Copy(Path.Combine(dirTmp,"extension_info.json"),Api.GetInternalFile("ExtensionInfo",$"{info.name}.json"),true);
|
|
|
|
Directory.Move(Path.Combine(dirTmp,"extension"),Api.GetInternalFile("ExtensionBinaries",info.name));
|
|
File.Move(Api.GetInternalFile("ExtensionBinaries",info.name,"extension.dll"),Api.GetInternalFile("ExtensionBinaries",info.name,$"{info.name}.dll"));
|
|
|
|
if(add)
|
|
{
|
|
string dir=Api.GetInternalFile("ExtensionBinaries",info.name);
|
|
string dirname=Path.GetFileName(dir);
|
|
string extname = Path.Combine(dir,$"{dirname}.dll");
|
|
|
|
var asm= Assembly.LoadFrom(extname);
|
|
foreach(var ext in asm.GetExportedTypes())
|
|
{
|
|
if(ext.IsAssignableTo(typeof(TimelapseExtension)))
|
|
{
|
|
var ext2= (TimelapseExtension?)Activator.CreateInstance(ext);
|
|
|
|
if(ext2 !=null && _data != null && _data.Instance != null)
|
|
{
|
|
var instance=_data.Instance;
|
|
ext2.FileName = dirname;
|
|
ext2.Instance=instance;
|
|
instance.Extensions.Add(ext2);
|
|
await ext2._Create();
|
|
instance.SetPriority();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
Directory.Delete(dirTmp,true);
|
|
}
|
|
public static ExtensionInfo? GetExtensionInfo(string filename)
|
|
{
|
|
if(!File.Exists(filename))
|
|
{
|
|
filename=Api.GetInternalFile("ExtensionInfo",$"{filename}.json");
|
|
if(!File.Exists(filename))
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
return JsonConvert.DeserializeObject<ExtensionInfo>(File.ReadAllText(filename));
|
|
}
|
|
public static IEnumerable<TimelapseExtension> GetTimelapseExtensions()
|
|
{
|
|
string dirtoitterateon= Api.GetInternalFile("ExtensionBinaries");
|
|
if(!Directory.Exists(dirtoitterateon)) yield break;
|
|
foreach(var dir in Directory.GetDirectories(dirtoitterateon))
|
|
{
|
|
//get dirname
|
|
string dirname = Path.GetFileName(dir);
|
|
string extname = Path.Combine(dir,$"{dirname}.dll");
|
|
|
|
if(File.Exists(extname))
|
|
{
|
|
var asm= Assembly.LoadFrom(extname);
|
|
foreach(var ext in asm.GetExportedTypes())
|
|
{
|
|
if(ext.IsAssignableTo(typeof(TimelapseExtension)))
|
|
{
|
|
var ext2= (TimelapseExtension?)Activator.CreateInstance(ext);
|
|
|
|
if(ext2 !=null)
|
|
{
|
|
ext2.FileName = dirname;
|
|
yield return ext2;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |