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")); File.Copy(Path.Combine(dirTmp,"extension_info.json"),Api.GetInternalFile("ExtensionInfo",$"{info.name}.json")); Directory.Move(Path.Combine(dirTmp,"ExtTMP","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(); } } } } } } 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(File.ReadAllText(filename)); } public static IEnumerable 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; } } } } } } }