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 IReadOnlyDictionary Components {get {return _components;}}
internal static Dictionary _components = new Dictionary();
public static string GetId(TimelapseExtension e)
{
return e.__id;
}
///
/// Callbacks back to TimelapseUI
///
///
public static Callbacks Callbacks {get;set;}
///
/// UserData
///
/// TimelapseNow Folder for user
public static string UserData =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"TimelapseNow");
///
/// UserExtensions
///
/// Location for Extensions
public static string UserExtensions = Path.Combine(UserData,"Extensions");
///
/// Global Data for app
///
/// Timelapse Folder for all users
public static string GlobalData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),"TimelapseNow");
///
/// GlobalExtensions
///
/// Location for extensions for all users
public static string GlobalExtensions = Path.Combine(GlobalData,"Extensions");
///
/// Dont call this
///
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);
}
}
}
///
/// Only call this to load extension manually
///
/// path to 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);
}
///
/// Assemblies
///
/// Dictionary of assembly,string
public static Dictionary Assemblies {get;set;}
///
/// Stores Attributes for extension
///
/// returns timelapseExtension based on attribute
public static Dictionary Attributes {get;set;}
///
/// List of extensions
///
/// List of extensions
public static List TimelapseExtensions {get;set;}
///
/// Dont call this (this gets called from The UI)
///
public static async Task OnNewFrame(Image image)
{
foreach(var extension in TimelapseExtensions)
{
await extension.OnNewFrame(image);
}
}
///
/// Dont call this (this gets called from The UI)
///
public static void OnLoadProject(TimelapseProject project)
{
foreach(var extension in TimelapseExtensions)
{
extension.OnLoadProject(project);
}
}
///
/// Dont call this (this gets called from The UI)
///
public static void OnStopRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStopRecording();
}
}
///
/// Dont call this (this gets called from The UI)
///
public static void OnStartRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStartRecording();
}
}
///
/// Dont call this (this gets called from The UI)
///
public static void OnEnableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=true;
extension.OnEnableOneX();
}
}
///
/// Dont call this (this gets called from The UI)
///
public static void OnDisableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=false;
extension.OnEnableOneX();
}
}
}
}