tytd-server/TYTD.Api/MyClass.cs

147 lines
4.3 KiB
C#

using System;
using TYTD.Server.Models;
using TYTD.Server.Functions;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
namespace TYTD
{
public abstract class Api : IDisposable
{
public Api()
{
}
public virtual void OnStart()
{
Console.WriteLine("Extension Loaded");
}
internal string storage;
protected string StorageLocation { get { return storage; } }
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; } } }
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<DownloadStartEventArgs> DownloadStart;
public event EventHandler<DownloadCompleteEventArgs> 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
{
internal static List<Api> apis = new List<Api>();
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();
}
}
internal static void Load(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);
Thread thrd = new Thread(new ThreadStart(api.OnStart));
thrd.Start();
}
}
}
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);
}
}
}
}
}