Make the plugin loading code cleaner, also make Plugin_Simple actually derive from Plugin, also make Plugin_Simple actually work.

This commit is contained in:
UnknownShadow200 2016-07-22 00:13:28 +10:00
parent 597c811aa4
commit 6bcced8415
4 changed files with 232 additions and 324 deletions

View File

@ -543,8 +543,8 @@
<Compile Include="Plugins\Events\ServerEvents.cs" />
<Compile Include="Plugins\IPluginEvent.cs" />
<Compile Include="Plugins\Manager\Plugin.Events.cs" />
<Compile Include="Plugins\Manager\Plugin_Manager.cs" />
<Compile Include="Plugins\Manager\Plugin_Simple.cs" />
<Compile Include="Plugins\Manager\PluginManager.cs" />
<Compile Include="Plugins\Manager\Plugin.cs" />
<Compile Include="Network\NetUtils.cs" />
<Compile Include="Network\Opcode.cs" />
<Compile Include="Network\Player.Networking.cs" />

85
Plugins/Manager/Plugin.cs Normal file
View File

@ -0,0 +1,85 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MCGalaxy {
/// <summary> This class provides for more advanced modification to MCGalaxy </summary>
public abstract partial class Plugin {
/// <summary> Use this to load all your events and everything you need. </summary>
/// <param name="startup">True if this was used from the server startup and not loaded from the command.</param>
public abstract void Load(bool startup);
/// <summary> Use this method to dispose of everything you used. </summary>
/// <param name="shutdown">True if this was used by the server shutting down and not a command.</param>
public abstract void Unload(bool shutdown);
/// <summary> This method is runned when a player does /help <pluginame>
/// Use it to show player's what this command is about. </summary>
/// <param name="p">Player who runned this command.</param>
public abstract void Help(Player p);
/// <summary> Name of the plugin. </summary>
public abstract string name { get; }
/// <summary> Your website. </summary>
public abstract string website { get; }
/// <summary> Oldest version of MCGalaxy the plugin is compatible with. </summary>
public abstract string MCGalaxy_Version { get; }
/// <summary> Version of your plugin. </summary>
public abstract int build { get; }
/// <summary> Message to display once plugin is loaded. </summary>
public abstract string welcome { get; }
/// <summary> The creator/author of this plugin. (Your name) </summary>
public abstract string creator { get; }
/// <summary> Whether or not to load this plugin at startup. </summary>
public abstract bool LoadAtStartup { get; }
}
public abstract class Plugin_Simple : Plugin {
/// <summary> This method is runned when a player does /help <pluginame>
/// Use it to show player's what this command is about. </summary>
/// <param name="p">Player who runned this command.</param>
public override void Help(Player p) {
Player.Message(p, "No help is available for this plugin.");
}
/// <summary> Your website. </summary>
public override string website { get { return "http://www.example.org"; } }
/// <summary> Version of your plugin. </summary>
public override int build { get { return 0; } }
/// <summary> Message to display once plugin is loaded. </summary>
public override string welcome { get { return "Plugin " + name + " loaded."; } }
/// <summary> Whether or not to load this plugin at startup. </summary>
public override bool LoadAtStartup { get { return true; } }
}
}

View File

@ -1,210 +1,145 @@
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MCGalaxy {
/// <summary> This class provides for more advanced modification to MCGalaxy </summary>
public abstract partial class Plugin {
#region Static Variables
/// <summary> List of all plugins. </summary>
public static List<Plugin> all = new List<Plugin>();
/// <summary> List of all simple plugins. </summary>
public static List<Plugin_Simple> all_simple = new List<Plugin_Simple>();
#endregion
#region Abstract
/// <summary> Use this to load all your events and everything you need. </summary>
/// <param name="startup">True if this was used from the server startup and not loaded from the command.</param>
public abstract void Load(bool startup);
/// <summary> Use this method to dispose of everything you used. </summary>
/// <param name="shutdown">True if this was used by the server shutting down and not a command.</param>
public abstract void Unload(bool shutdown);
/// <summary> This method is runned when a player does /help <pluginame>
/// Use it to show player's what this command is about. </summary>
/// <param name="p">Player who runned this command.</param>
public abstract void Help(Player p);
/// <summary> Name of the plugin. </summary>
public abstract string name { get; }
/// <summary> Your website. </summary>
public abstract string website { get; }
/// <summary> Oldest version of MCGalaxy the plugin is compatible with. </summary>
public abstract string MCGalaxy_Version { get; }
/// <summary> Version of your plugin. </summary>
public abstract int build { get; }
/// <summary> Message to display once plugin is loaded. </summary>
public abstract string welcome { get; }
/// <summary> The creator/author of this plugin. (Your name) </summary>
public abstract string creator { get; }
/// <summary> Whether or not to load this plugin at startup. </summary>
public abstract bool LoadAtStartup { get; }
#endregion
#region Plugin Find
/// <summary> Look to see if a plugin is loaded </summary>
/// <param name="name">The name of the plugin</param>
/// <returns>Returns the plugin (returns null if non is found)</returns>
public static Plugin Find(string name) {
List<Plugin> tempList = new List<Plugin>();
tempList.AddRange(all);
Plugin match = null; int matches = 0;
name = name.ToLower();
foreach (Plugin p in tempList) {
if (p.name.ToLower() == name) return p;
if (p.name.ToLower().Contains(name)) {
match = p; matches++;
}
}
return matches == 1 ? match : null;
}
#endregion
#region Loading/Unloading
/// <summary> Load a plugin </summary>
/// <param name="name">The name of the plugin.</param>
/// <param name="startup">Is this startup?</param>
public static void Load(string name, bool startup){
string creator = "";
string path = "plugins/" + name + ".dll";
try { throw new Exception();
Plugin instance = null;
Assembly lib = null;
using (FileStream fs = File.Open(path, FileMode.Open)) {
using (MemoryStream ms = new MemoryStream()) {
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fs.Read(buffer, 0, 1024)) > 0)
ms.Write(buffer, 0, read);
lib = Assembly.Load(ms.ToArray());
}
}
try
{
foreach (Type t in lib.GetTypes())
{
if (t.BaseType == typeof(Plugin))
{
instance = (Plugin)Activator.CreateInstance(t);
break;
}
}
}
catch { }
if (instance == null)
{
Server.s.Log("The plugin " + name + " couldn't be loaded!");
return;
}
String plugin_version = instance.MCGalaxy_Version;
if (!String.IsNullOrEmpty(plugin_version) && new Version(plugin_version) > Server.Version)
{
Server.s.Log("This plugin (" + instance.name + ") isn't compatible with this version of MCGalaxy!");
Thread.Sleep(1000);
if (Server.unsafe_plugin)
{
Server.s.Log("Will attempt to load!");
goto here;
}
else
return;
}
here:
Plugin.all.Add(instance);
creator = instance.creator;
if (instance.LoadAtStartup)
{
instance.Load(startup);
Server.s.Log("Plugin: " + instance.name + " loaded...build: " + instance.build);
}
else
Server.s.Log("Plugin: " + instance.name + " was not loaded, you can load it with /pload");
Server.s.Log(instance.welcome);
return;
} catch (FileNotFoundException) {
Plugin_Simple.Load(name, startup);
} catch (BadImageFormatException) {
Plugin_Simple.Load(name, startup);
} catch (PathTooLongException) {
} catch (FileLoadException) {
Plugin_Simple.Load(name, startup);
} catch (Exception e) {
try { Server.s.Log("Attempting a simple plugin!"); if (Plugin_Simple.Load(name, startup)) return; }
catch { }
Server.ErrorLog(e);
Server.s.Log("The plugin " + name + " failed to load!");
if (creator != "")
Server.s.Log("You can go bug " + creator + " about it.");
Thread.Sleep(1000);
}
}
/// <summary> Unload a plugin </summary>
/// <param name="p">The plugin to unload</param>
/// <param name="shutdown">Is this shutdown?</param>
public static void Unload(Plugin p, bool shutdown) {
try {
p.Unload(shutdown);
all.Remove(p);
Server.s.Log(p.name + " was unloaded.");
}
catch { Server.s.Log("An error occurred while unloading a plugin."); }
}
#endregion
#region Global Loading/Unloading
/// <summary> Unload all plugins </summary>
public static void Unload() {
all.ForEach(delegate(Plugin p) {
Unload(p, true);
});
}
/// <summary> Load all plugins </summary>
public static void Load() {
if (Directory.Exists("plugins")) {
foreach (string path in Directory.GetFiles("plugins", "*.dll")) {
string name = Path.GetFileNameWithoutExtension(path);
Load(name, true);
}
} else {
Directory.CreateDirectory("plugins");
}
// Load Internal Plugins
CTF.Setup temp = new CTF.Setup();
temp.Load(true);
Plugin.all_simple.Add(temp);
}
#endregion
}
}
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCGalaxy)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MCGalaxy {
/// <summary> This class provides for more advanced modification to MCGalaxy </summary>
public abstract partial class Plugin {
/// <summary> List of all plugins. </summary>
public static List<Plugin> all = new List<Plugin>();
/// <summary> Look to see if a plugin is loaded </summary>
/// <param name="name">The name of the plugin</param>
/// <returns>Returns the plugin (returns null if non is found)</returns>
public static Plugin Find(string name) {
List<Plugin> tempList = new List<Plugin>();
tempList.AddRange(all);
Plugin match = null; int matches = 0;
name = name.ToLower();
foreach (Plugin p in tempList) {
if (p.name.ToLower() == name) return p;
if (p.name.ToLower().Contains(name)) {
match = p; matches++;
}
}
return matches == 1 ? match : null;
}
/// <summary> Load a plugin </summary>
/// <param name="name">The name of the plugin.</param>
/// <param name="startup">Is this startup?</param>
public static void Load(string name, bool startup){
string creator = "";
string path = "plugins/" + name + ".dll";
try {
Plugin instance = null;
Assembly lib = null;
using (FileStream fs = File.Open(path, FileMode.Open)) {
using (MemoryStream ms = new MemoryStream()) {
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fs.Read(buffer, 0, 1024)) > 0)
ms.Write(buffer, 0, read);
lib = Assembly.Load(ms.ToArray());
}
}
try {
foreach (Type t in lib.GetTypes()) {
if (!t.IsSubclassOf(typeof(Plugin))) continue;
instance = (Plugin)Activator.CreateInstance(t);
break;
}
} catch { }
if (instance == null) {
Server.s.Log("The plugin " + name + " couldn't be loaded!");
return;
}
creator = instance.creator;
string ver = instance.MCGalaxy_Version;
if (!String.IsNullOrEmpty(ver) && new Version(ver) > Server.Version) {
Server.s.Log("This plugin (" + instance.name + ") isn't compatible with this version of MCGalaxy!");
Thread.Sleep(1000);
if (!Server.unsafe_plugin) return;
Server.s.Log("Will attempt to load!");
}
Plugin.all.Add(instance);
if (instance.LoadAtStartup) {
instance.Load(startup);
Server.s.Log("Plugin: " + instance.name + " loaded...build: " + instance.build);
} else {
Server.s.Log("Plugin: " + instance.name + " was not loaded, you can load it with /pload");
}
Server.s.Log(instance.welcome);
} catch (Exception e) {
Server.ErrorLog(e);
Server.s.Log("The plugin " + name + " failed to load!");
if (creator != "") Server.s.Log("You can go bug " + creator + " about it.");
Thread.Sleep(1000);
}
}
/// <summary> Unload a plugin </summary>
/// <param name="p">The plugin to unload</param>
/// <param name="shutdown">Is this shutdown?</param>
public static void Unload(Plugin p, bool shutdown) {
try {
p.Unload(shutdown);
all.Remove(p);
Server.s.Log(p.name + " was unloaded.");
} catch { Server.s.Log("An error occurred while unloading a plugin.");
}
}
/// <summary> Unload all plugins </summary>
public static void Unload() {
all.ForEach(delegate(Plugin p) {
Unload(p, true);
});
}
/// <summary> Load all plugins </summary>
public static void Load() {
if (Directory.Exists("plugins")) {
foreach (string path in Directory.GetFiles("plugins", "*.dll")) {
string name = Path.GetFileNameWithoutExtension(path);
Load(name, true);
}
} else {
Directory.CreateDirectory("plugins");
}
// Load Internal Plugins
CTF.Setup temp = new CTF.Setup();
temp.Load(true);
Plugin.all.Add(temp);
}
}
}

View File

@ -1,112 +0,0 @@
/*
Copyright 2011 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.IO;
using System.Reflection;
using System.Threading;
namespace MCGalaxy {
/// <summary> This is the same as a normal plugin, except it contains less info. </summary>
public abstract class Plugin_Simple {
#region Abstract
/// <summary> Use this to load all your events and everything you need. </summary>
/// <param name="startup">True if this was used from the server startup and not loaded from the command.</param>
public abstract void Load(bool startup);
/// <summary> Use this method to dispose of everything you used. </summary>
/// <param name="shutdown">True if this was used by the server shutting down and not a command.</param>
public abstract void Unload(bool shutdown);
/// <summary> Name of the plugin. </summary>
public abstract string name { get; }
/// <summary> The creator/author of this plugin. (Your name) </summary>
public abstract string creator { get; }
/// <summary> Oldest version of MCGalaxy the plugin is compatible with. </summary>
public abstract string MCGalaxy_Version { get; }
#endregion
#region Loading
/// <summary> Load a simple plugin </summary>
/// <param name="name">The name of the plugin.</param>
/// <param name="startup">Whether the server is starting up or not</param>
/// <returns>Whether the plugin loaded or not</returns>
public static bool Load(string name, bool startup)
{
string creator = "";
object instance = null;
Assembly lib = null;
string path = "plugins/" + name + ".dll";
using (FileStream fs = File.Open(path, FileMode.Open))
{
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[1024];
int read = 0;
while ((read = fs.Read(buffer, 0, 1024)) > 0)
ms.Write(buffer, 0, read);
lib = Assembly.Load(ms.ToArray());
ms.Close();
ms.Dispose();
}
}
try
{
foreach (Type t in lib.GetTypes())
{
if (t.BaseType == typeof(Plugin_Simple))
{
instance = Activator.CreateInstance(t);
break;
}
}
}
catch { }
if (instance == null)
{
Server.s.Log("The plugin " + name + " couldn't be loaded!");
return false;
}
String plugin_version = ((Plugin_Simple)instance).MCGalaxy_Version;
if (plugin_version != "" && new Version(plugin_version) != Server.Version)
{
Server.s.Log("This plugin (" + ((Plugin_Simple)instance).name + ") isn't compatible with this version of MCGalaxy!");
Thread.Sleep(1000);
if (Server.unsafe_plugin)
{
Server.s.Log("Will attempt to load!");
goto here;
}
else
{
return false;
}
}
here:
Plugin.all_simple.Add((Plugin_Simple)instance);
creator = ((Plugin_Simple)instance).creator;
((Plugin_Simple)instance).Load(startup);
Server.s.Log("Plugin: " + ((Plugin_Simple)instance).name + " loaded...");
Thread.Sleep(1000);
return true;
}
#endregion
}
}