using System;
using System.Collections.Generic;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using System.Threading.Tasks;
using System.Reflection;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Timelapse.Data;
namespace Timelapse.Api
{
[AttributeUsage(AttributeTargets.Class)]
// [Extension("Tesses","oiajifajdifajedifaji",ReleaseDate = "12/27/2021",Version ="1.0.0.0")]
public class ExtensionAttribute : Attribute
{
public ExtensionAttribute(string name,string id)
{
Name=name;
Id=id;
ReleaseDate="8/20/1992";
Version="1.0.0.0";
}
///
/// name
///
public string Name {get;set;}
///
/// something unique
///
public string Id {get;set;}
///
/// ex 8/20/1992
///
public string ReleaseDate {get;set;}
///
/// ex 1.0.0.0
///
public string Version {get;set;}
}
public class Extensions
{
public static string GetId(TimelapseExtension e)
{
return e.__id;
}
public static async Task OnGetPage(string extId,string subPage)
{
foreach(var item in Attributes)
{
if(item.Key.Id == extId)
{
return await item.Value.OnHandleRequest(subPage);
}
}
return new NotFoundResult();
}
public static Callbacks Callbacks {get;set;}
public static string UserData =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),".TimelapseNow");
public static string UserExtensions = Path.Combine(UserData,"Extensions");
public static string GlobalData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),"TimelapseNow");
public static string GlobalExtensions = Path.Combine(GlobalData,"Extensions");
public static void EnumerateExtensions()
{
TimelapseExtensions = new List();
Assemblies = new Dictionary();
Attributes = new Dictionary();
if(Directory.Exists(GlobalExtensions))
{
foreach(var item in Directory.GetDirectories(GlobalExtensions))
{
string name = Path.GetFileName(item);
string dll = Path.Combine(item,name + ".dll");
if(File.Exists(dll))
{
LoadDll(dll);
}
}
}
Directory.CreateDirectory(UserExtensions);
foreach(var item in Directory.GetDirectories(UserExtensions))
{
string name = Path.GetFileName(item);
string dll = Path.Combine(item,name + ".dll");
if(File.Exists(dll))
{
LoadDll(dll);
}
}
}
public static void LoadDll(string dll)
{
Assembly asm=Assembly.LoadFrom(dll);
foreach(var type in asm.GetTypes())
{
ExtensionAttribute attr=type.GetCustomAttribute();
if(typeof(TimelapseExtension).IsAssignableFrom(type) && attr != null)
{
Console.WriteLine("Before Create Instance");
//load assembly info
var item=(TimelapseExtension)Activator.CreateInstance(type);
Console.WriteLine("Affter Create Instance");
item.__id=attr.Id;
item.Name = attr.Name;
TimelapseExtensions.Add(item);
Attributes.Add(attr,item);
Console.WriteLine("Before OnInit");
item.OnInit();
Console.WriteLine("After OnInit");
}
}
Assemblies.Add(asm,dll);
}
public static Dictionary Assemblies {get;set;}
public static Dictionary Attributes {get;set;}
public static List TimelapseExtensions {get;set;}
public static async Task OnNewFrame(Image image)
{
foreach(var extension in TimelapseExtensions)
{
await extension.OnNewFrame(image);
}
}
public static void OnLoadProject(TimelapseProject project)
{
foreach(var extension in TimelapseExtensions)
{
extension.OnLoadProject(project);
}
}
public static void OnStopRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStopRecording();
}
}
public static void OnStartRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStartRecording();
}
}
public static void OnEnableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=true;
extension.OnEnableOneX();
}
}
public static void OnDisableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=false;
extension.OnEnableOneX();
}
}
}
}