timelapse/Timelapse.Api/Class1.cs

214 lines
7.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
{
2021-12-30 04:07:31 +00:00
public static IReadOnlyDictionary<string,Type> Components {get {return _components;}}
internal static Dictionary<string,Type> _components = new Dictionary<string, Type>();
2021-12-28 07:25:39 +00:00
public static string GetId(TimelapseExtension e)
{
return e.__id;
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Callbacks back to TimelapseUI
/// </summary>
/// <value></value>
2021-12-28 07:25:39 +00:00
public static Callbacks Callbacks {get;set;}
2021-12-30 04:07:31 +00:00
/// <summary>
/// UserData
/// </summary>
/// <returns>TimelapseNow Folder for user</returns>
public static string UserData =Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"TimelapseNow");
/// <summary>
/// UserExtensions
/// </summary>
/// <returns>Location for Extensions</returns>
2021-12-28 07:25:39 +00:00
public static string UserExtensions = Path.Combine(UserData,"Extensions");
2021-12-30 04:07:31 +00:00
/// <summary>
/// Global Data for app
/// </summary>
/// <returns>Timelapse Folder for all users</returns>
2021-12-28 07:25:39 +00:00
public static string GlobalData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),"TimelapseNow");
2021-12-30 04:07:31 +00:00
/// <summary>
/// GlobalExtensions
/// </summary>
/// <returns>Location for extensions for all users</returns>
2021-12-28 07:25:39 +00:00
public static string GlobalExtensions = Path.Combine(GlobalData,"Extensions");
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this
/// </summary>
2021-12-28 07:25:39 +00:00
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);
}
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Only call this to load extension manually
/// </summary>
/// <param name="dll">path to dll</param>
2021-12-28 07:25:39 +00:00
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)
{
2021-12-28 13:47:09 +00:00
Console.WriteLine("Before Create Instance");
2021-12-28 07:25:39 +00:00
//load assembly info
2021-12-28 13:47:09 +00:00
var item=(TimelapseExtension)Activator.CreateInstance(type);
Console.WriteLine("Affter Create Instance");
2021-12-28 07:25:39 +00:00
item.__id=attr.Id;
item.Name = attr.Name;
TimelapseExtensions.Add(item);
Attributes.Add(attr,item);
2021-12-28 13:47:09 +00:00
Console.WriteLine("Before OnInit");
2021-12-28 07:25:39 +00:00
item.OnInit();
2021-12-28 13:47:09 +00:00
Console.WriteLine("After OnInit");
2021-12-28 07:25:39 +00:00
}
}
Assemblies.Add(asm,dll);
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Assemblies
/// </summary>
/// <value>Dictionary of assembly,string</value>
2021-12-28 07:25:39 +00:00
public static Dictionary<Assembly,string> Assemblies {get;set;}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Stores Attributes for extension
/// </summary>
/// <value>returns timelapseExtension based on attribute</value>
2021-12-28 07:25:39 +00:00
public static Dictionary<ExtensionAttribute,TimelapseExtension> Attributes {get;set;}
2021-12-30 04:07:31 +00:00
/// <summary>
/// List of extensions
/// </summary>
/// <value>List of extensions</value>
2021-12-28 07:25:39 +00:00
public static List<TimelapseExtension> TimelapseExtensions {get;set;}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static async Task OnNewFrame(Image<Rgb24> image)
{
foreach(var extension in TimelapseExtensions)
{
await extension.OnNewFrame(image);
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static void OnLoadProject(TimelapseProject project)
{
foreach(var extension in TimelapseExtensions)
{
extension.OnLoadProject(project);
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static void OnStopRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStopRecording();
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static void OnStartRecording()
{
foreach(var extension in TimelapseExtensions)
{
extension.OnStartRecording();
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static void OnEnableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=true;
extension.OnEnableOneX();
}
}
2021-12-30 04:07:31 +00:00
/// <summary>
/// Dont call this (this gets called from The UI)
/// </summary>
2021-12-28 07:25:39 +00:00
public static void OnDisableOneX()
{
foreach(var extension in TimelapseExtensions)
{
extension._onex=false;
extension.OnEnableOneX();
}
}
}
}