Created
July 30, 2018 17:16
-
-
Save DamianRyse/1bb893ab8eb7170e327666da1356838d to your computer and use it in GitHub Desktop.
Plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Reflection; | |
using System.Threading.Tasks; | |
using System.Web.Script.Serialization; | |
namespace TwitchBitch | |
{ | |
public interface IPlugin | |
{ | |
int Init(); | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class PluginDisplayNameAttribute : System.Attribute | |
{ | |
string displayName; | |
public PluginDisplayNameAttribute(string displayName) : base() | |
{ | |
this.displayName = displayName; | |
} | |
public override string ToString() | |
{ | |
return displayName; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class PluginDescriptionAttribute : System.Attribute | |
{ | |
string Description; | |
public PluginDescriptionAttribute(string Description) : base() | |
{ | |
this.Description = Description; | |
} | |
public override string ToString() | |
{ | |
return Description; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class PluginCopyrightAttribute : System.Attribute | |
{ | |
string Copyright; | |
public PluginCopyrightAttribute(string Copyright) : base() | |
{ | |
this.Copyright = Copyright; | |
} | |
public override string ToString() | |
{ | |
return Copyright; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class PluginVersionAttribute : System.Attribute | |
{ | |
string Version; | |
public PluginVersionAttribute(string Version) : base() | |
{ | |
this.Version = Version; | |
} | |
public override string ToString() | |
{ | |
return Version; | |
} | |
} | |
[AttributeUsage(AttributeTargets.Class)] | |
public class VerificationId : System.Attribute | |
{ | |
string Id; | |
public VerificationId(string Id) : base() | |
{ | |
this.Id = Id; | |
} | |
public override string ToString() | |
{ | |
return Id; | |
} | |
} | |
public class Plugin | |
{ | |
public static Plugin LoadPlugin(string Filename) | |
{ | |
string pluginDirectory = Directory.GetParent(Filename).FullName; | |
string filename = Filename.Replace(pluginDirectory + Path.DirectorySeparatorChar, ""); | |
if (File.Exists(Filename)) | |
{ | |
if (Filename.Substring(Filename.Length - 4, 4).ToLower() == ".dll") | |
{ | |
#region | |
Plugin newPlugin = null; | |
// Load assembly and get types | |
Assembly a = Assembly.LoadFrom(Filename); | |
System.Type[] types = a.GetTypes(); | |
// Look for IPlugin interface in the assembly | |
foreach (System.Type type in types) | |
{ | |
if (type.GetInterface("IPlugin") != null) | |
{ | |
if (type.GetCustomAttributes(typeof(PluginDisplayNameAttribute), false).Length != 1) | |
throw new Exception("Plugin display name is not supported."); | |
if (type.GetCustomAttributes(typeof(PluginDescriptionAttribute), false).Length != 1) | |
throw new Exception("Plugin description is not supported."); | |
if (type.GetCustomAttributes(typeof(PluginCopyrightAttribute), false).Length != 1) | |
throw new Exception("Plugin copyright is not supported."); | |
if (type.GetCustomAttributes(typeof(PluginVersionAttribute), false).Length != 1) | |
throw new Exception("Plugin version is not supported."); | |
if (type.GetCustomAttributes(typeof(VerificationId), false).Length != 1) | |
throw new Exception("Verification ID is not supported."); | |
Object o = Activator.CreateInstance(type); | |
string Name = type.GetCustomAttributes(typeof(PluginDisplayNameAttribute), false)[0].ToString(); | |
string Descr = type.GetCustomAttributes(typeof(PluginDescriptionAttribute), false)[0].ToString(); | |
string Copyright = type.GetCustomAttributes(typeof(PluginCopyrightAttribute), false)[0].ToString(); | |
string Version = type.GetCustomAttributes(typeof(PluginVersionAttribute), false)[0].ToString(); | |
string VerificationId = type.GetCustomAttributes(typeof(VerificationId), false)[0].ToString(); | |
newPlugin = new Plugin(o as IPlugin, Name, Descr, Copyright, Version, VerificationId); | |
} | |
} | |
#endregion | |
return newPlugin; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
IPlugin plugin; | |
public string Name { get; } | |
public string Description { get; } | |
public string Copyright { get; } | |
public string Version { get; } | |
public string VerificationID { get; } | |
public Plugin(IPlugin plugin, string name, string description, string copyright, string version, string verificationId) | |
{ | |
this.plugin = plugin; | |
Name = name; | |
Description = description.Replace("\n"," "); | |
if (Description.Length > 140) { Description = Description.Substring(0, 140); } | |
Copyright = copyright; | |
Version = version; | |
VerificationID = verificationId; | |
} | |
public int Init() | |
{ | |
return plugin.Init(); | |
} | |
} | |
public static class PluginInfoDevice | |
{ | |
public static class ClientConfig | |
{ | |
public static string Username | |
{ | |
get | |
{ | |
return Program.clientConfig.ClientDetails.Nickname; | |
} | |
} | |
public static string[] GetAdmininstrators() | |
{ | |
string[] array = new string[Program.clientConfig.ClientDetails.Administrators.Count]; | |
for (int i = 0; i < Program.clientConfig.ClientDetails.Administrators.Count; i++) | |
{ | |
array[i] = Program.clientConfig.ClientDetails.Administrators[i].Name; | |
} | |
return array; | |
} | |
public static string GetEmailAdress(string AdminUsername) | |
{ | |
foreach(ClientConfiguration.clientDetails.Administrator admin in Program.clientConfig.ClientDetails.Administrators) | |
{ | |
if(admin.Name.ToLower() == AdminUsername.ToLower()) | |
{ | |
return admin.Email; | |
} | |
} | |
return null; | |
} | |
public static string SaveFolder | |
{ | |
get | |
{ | |
return Directory.GetParent(Program.clientConfig.Filename).ToString(); | |
} | |
} | |
public static string HomeChannel | |
{ | |
get | |
{ | |
return Program.clientConfig.ClientDetails.HomeChannel; | |
} | |
} | |
} | |
public static class Connection | |
{ | |
public static void SendChatMessage(string Msg, string Channel) | |
{ | |
Program.con.SendChatMessage(Msg, Channel); | |
} | |
public static void SendData(string Msg) | |
{ | |
Program.con.SendData(Msg); | |
} | |
public static void SendMail(string To, string Subject, string Msg) | |
{ | |
Program.SendMail(To, Subject, Msg); | |
} | |
public static void JoinChannel(string Channel) | |
{ | |
Program.con.JoinChannel(Channel); | |
} | |
public static async Task<string> RetrieveJSON(string _URI, bool UseClientID=false) | |
{ | |
return await TwitchAPI.RetrieveJSON(_URI, UseClientID); | |
} | |
public static TwitchConnection GlobalConnection = Program.con; | |
} | |
public static class Misc | |
{ | |
public static string GetProgramVersion() | |
{ | |
return Program.__twbiVersion.ToString(); | |
} | |
} | |
public static class Plugins | |
{ | |
public static Plugin[] LoadedPlugins() | |
{ | |
return Program.Plugins.ToArray(); | |
} | |
public static bool IsVerified(Plugin plugin) | |
{ | |
foreach(Verification ver in VerifiedPlugins) | |
{ | |
if (ver.verificationId == plugin.VerificationID && ver.pluginName == plugin.Name) | |
return true; | |
} | |
return false; | |
} | |
internal struct Verification | |
{ | |
public string verificationId; | |
public string pluginName; | |
public DateTime verificationDate; | |
public Verification(string VerificationId, string PluginName, DateTime VerificationDate) | |
{ | |
this.verificationDate = VerificationDate; | |
this.verificationId = VerificationId; | |
this.pluginName = PluginName; | |
} | |
} | |
internal static Verification[] VerifiedPlugins = | |
{ | |
new Verification("5GUKN-zVfm0-0JuA6-mj8ES-oWk9P-96P4W","Visual Bitch",new DateTime(2017,10,6)), | |
new Verification("B4D9U-xbLkp-GJM25-6wWXv-2feAD-7rBHs","Basic Functions",new DateTime(2017,10,6)), | |
new Verification("0OH4E-wfTOh-iDN6R-duLu5-iNaL1-ZMa1X","Currency",new DateTime(2017,10,6)), | |
new Verification("PC3vq-09h80-ll4iD-JlwTW-5cryk-uNOO5","Custom Commands",new DateTime(2017,10,6)), | |
new Verification("mzr65-2K5QG-Ib2un-94N6A-dVhBv-WeLUm","MySQL Connection",new DateTime(2017,10,6)), | |
new Verification("rnQ6j-8x7ZU-q21Qg-L0VnD-ZsGtH-sb42F","Quotes",new DateTime(2017,10,6)), | |
new Verification("73C2J-q4A01-Lbx20-YoB71-nXR0R-5rs50","Statistics",new DateTime(2017,10,6)), | |
new Verification("4WVXl-TKrFc-2xf9p-7rXqD-2o9W4-224E9","Weather",new DateTime(2017,10,6)) | |
}; | |
} | |
public static class ExceptionCollector | |
{ | |
private static Exception[] Collection = new Exception[0]; | |
private static string logfile = Directory.GetParent(Program.clientConfig.Filename).ToString() + Path.DirectorySeparatorChar + "error.log"; | |
private static void _saveCollection() | |
{ | |
JavaScriptSerializer ser = new JavaScriptSerializer(); | |
try | |
{ | |
File.WriteAllText(logfile, ser.Serialize(Collection)); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine("*** ERROR writing ErrorCollection to file: " + ex.Message); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment