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 virtual void SetProgress(double progress) { } public virtual void OnEnd() { } 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 SendDLInfoStart(object sender, DownloadInfoStartEventArgs e) { if (DownloadInfoStart != null) { DownloadInfoStart.Invoke(sender, e); } } internal void SendDLComplete(object sender, DownloadCompleteEventArgs e) { if (DownloadComplete != null) { DownloadComplete.Invoke(sender, e); } } public event EventHandler DownloadInfoStart; public event EventHandler DownloadStart; public event EventHandler DownloadComplete; public void Dispose() { if( t != null) { t.Dispose(); } OnEnd(); } } public class DownloadCompleteEventArgs : EventArgs { public bool RegularFile { get; set; } public SavedVideo Video { get; set; } } public class DownloadInfoStartEventArgs : EventArgs { 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 RenderFileOrDefault(string file,string defaultData,Dictionary arg) { return Templating.RenderString(ReadAllTextOrDefault(file, defaultData), arg); } public static string ReadAllTextOrDefault(string file,string defaultData) { if (File.Exists(file)) { return File.ReadAllText(file); } return defaultData; } 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); } } internal static void SetProgress(double progress) { foreach (var api in apis) { api.SetProgress(progress); } } internal static void DownloadInfoStart(object sender, DownloadInfoStartEventArgs evt) { foreach (var api in apis) { api.SendDLInfoStart(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(""); } internal static void Load(StringBuilder b,string dll,string confpath) { var asm=Assembly.LoadFrom(dll); 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() { StringBuilder b = new StringBuilder(); 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(b,dllpath, confpath); } } Dictionary templating = new Dictionary(); templating.Add("Items", b.ToString()); string combined= Path.Combine("WebSite", "extensions.html"); string template = "Extensions

Extensions

{Items}
NameUrls
"; template=ReadAllTextOrDefault(combined, template); Page = Templating.RenderString(template, templating); } } }