using System; using TYTD.Server.Models; using TYTD.Server.Functions; using System.Threading; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Linq; using System.Text; using SimpleHttp; namespace TYTD { public abstract class Api : IDisposable { public virtual void OnStart() { Console.WriteLine("Extension Loaded"); } internal string storage; protected string StorageLocation { get { return storage; } } public virtual string Name { get { return Path.GetFileName(StorageLocation); } } private System.Timers.Timer t = null; protected void SetTimer(TimeSpan timespan) { if(t == null) { t = new System.Timers.Timer(); t.Elapsed += (sender, e) => { TimerElapsed(); }; } t.Interval = timespan.TotalMilliseconds; t.Start(); } public bool TimerEnabled { get { if (t != null) { return t.Enabled; } else { return false; } } set { if (t != null) { t.Enabled = value; } } } public virtual IEnumerable> GetUrls() { yield return new KeyValuePair("Home",$"api/Extensions/{Name}"); } protected virtual void TimerElapsed() { Console.WriteLine("Event Fired"); } public Api AddItem(string id) { Downloader.DownloadItem(id); return this; } internal void SendDLStart(object sender,DownloadStartEventArgs e) { if(DownloadStart != null) { DownloadStart.Invoke(sender, e); } } internal void SendDLComplete(object sender, DownloadCompleteEventArgs e) { if (DownloadComplete != null) { DownloadComplete.Invoke(sender, e); } } public event EventHandler DownloadStart; public event EventHandler DownloadComplete; public void Dispose() { if( t != null) { t.Dispose(); } } } public class DownloadCompleteEventArgs : EventArgs { public bool RegularFile { get; set; } public SavedVideo Video { get; set; } } public class DownloadStartEventArgs : EventArgs { public bool RegularFile { get; set; } public SavedVideo Video { get; set; } public bool Cancel { get; set; } } public static class ApiLoader { public static string Page { get; private set; } internal static List apis = new List(); internal static void DownloadStarted(object sender,DownloadStartEventArgs evt) { foreach(var api in apis) { api.SendDLStart(sender, evt); } } internal static void DownloadComplete(object sender, DownloadCompleteEventArgs evt) { foreach (var api in apis) { api.SendDLComplete(sender, evt); } } public static void Dispose() { foreach(var api in apis) { api.Dispose(); } } public static void GenerateTr(Api api,StringBuilder b) { string data = ""; string root = Path.Combine(Environment.CurrentDirectory, "config"); string fallbackIcon = Path.Combine(root, "default_icon.png"); string icon = Path.Combine(root, "apiicons", api.Name + ".png"); string icon2 = File.Exists(icon) ? icon : fallbackIcon; if (File.Exists(icon2)) { data = Convert.ToBase64String(File.ReadAllBytes(icon2)); } b.Append($"

{api.Name}

\"{api.Name}\""); foreach (var link in api.GetUrls()) { b.Append($"{link.Key}
"); } b.Append(""); Dictionary templating = new Dictionary(); templating.Add("Items", b.ToString()); Page= Templating.RenderFile(Path.Combine("WebSite", "extensions.html"),templating); } internal static void Load(string dll,string confpath) { var asm=Assembly.LoadFrom(dll); StringBuilder b = new StringBuilder(); foreach (var item in asm.GetTypes()) { if(typeof(Api).IsAssignableFrom(item)) { Api api = (Api)Activator.CreateInstance(item); api.storage = confpath; apis.Add(api); api.OnStart(); GenerateTr(api, b); } } } public static void Init() { string root = Path.Combine(Environment.CurrentDirectory,"config", "apidll"); string appconfroot = Path.Combine(Environment.CurrentDirectory,"config", "apistore"); foreach(var dir in Directory.GetDirectories(root)) { string name = Path.GetFileName(dir); string dllpath = Path.Combine(root, name, name + ".dll"); string confpath = Path.Combine(appconfroot, name); if(File.Exists(dllpath)) { Directory.CreateDirectory(confpath); Load(dllpath, confpath); } } } } }