timelapse/Timelapse.Api/Class1.cs

161 lines
5.6 KiB
C#
Raw Normal View History

2021-12-28 07:25:39 +00:00
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";
}
///<summary>
/// name
///</summary>
public string Name {get;set;}
///<summary>
/// something unique
///</summary>
public string Id {get;set;}
///<summary>
/// ex 8/20/1992
///</summary>
public string ReleaseDate {get;set;}
///<summary>
/// ex 1.0.0.0
///</summary>
public string Version {get;set;}
}
public class Extensions
{
public static string GetId(TimelapseExtension e)
{
return e.__id;
}
public static async Task<IActionResult> 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<TimelapseExtension>();
Assemblies = new Dictionary<Assembly, string>();
Attributes = new Dictionary<ExtensionAttribute, TimelapseExtension>();
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<ExtensionAttribute>();
if(typeof(TimelapseExtension).IsAssignableFrom(type) && attr != null)
{
//load assembly info
var item=(TimelapseExtension)Activator.CreateInstance(typeof(TimelapseExtension));
item.__id=attr.Id;
item.Name = attr.Name;
TimelapseExtensions.Add(item);
Attributes.Add(attr,item);
item.OnInit();
}
}
Assemblies.Add(asm,dll);
}
public static Dictionary<Assembly,string> Assemblies {get;set;}
public static Dictionary<ExtensionAttribute,TimelapseExtension> Attributes {get;set;}
public static List<TimelapseExtension> TimelapseExtensions {get;set;}
public static async Task OnNewFrame(Image<Rgb24> 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();
}
}
}
}