2015-06-19 22:11:48 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-06-19 22:37:57 -04:00
|
|
|
|
using System.IO;
|
2015-06-19 22:11:48 -04:00
|
|
|
|
using System.Reflection;
|
2015-06-19 22:37:57 -04:00
|
|
|
|
using ClassicalSharp.Commands;
|
2015-06-19 22:54:21 -04:00
|
|
|
|
using ClassicalSharp.Model;
|
2015-06-19 22:11:48 -04:00
|
|
|
|
|
2015-06-20 00:19:07 -04:00
|
|
|
|
namespace ClassicalSharp.Plugins {
|
2015-06-19 22:11:48 -04:00
|
|
|
|
|
|
|
|
|
public abstract class Plugin {
|
|
|
|
|
|
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
2015-06-19 22:37:57 -04:00
|
|
|
|
public abstract string Authors { get; }
|
|
|
|
|
|
2015-06-19 22:11:48 -04:00
|
|
|
|
public abstract string Description { get; }
|
|
|
|
|
|
2015-06-19 22:37:57 -04:00
|
|
|
|
public abstract IEnumerable<PluginModule> Modules { get; }
|
2015-06-19 22:11:48 -04:00
|
|
|
|
|
|
|
|
|
public static void LoadPlugin( string file, Game game ) {
|
|
|
|
|
Assembly assembly = Assembly.LoadFile( file );
|
|
|
|
|
Type[] types = assembly.GetTypes();
|
2015-06-20 00:19:07 -04:00
|
|
|
|
|
2015-06-19 22:11:48 -04:00
|
|
|
|
foreach( Type t in types ) {
|
|
|
|
|
if( t.IsAbstract || t.IsInterface || t.IsGenericType ) continue;
|
2015-06-19 22:37:57 -04:00
|
|
|
|
if( t.IsSubclassOf( typeof( Plugin ) ) ) {
|
2015-06-19 22:11:48 -04:00
|
|
|
|
Plugin p = (Plugin)Activator.CreateInstance( t );
|
2015-06-19 22:37:57 -04:00
|
|
|
|
Console.WriteLine( "Loading plugin: {0}, written by {1}", p.Name, p.Authors );
|
|
|
|
|
Console.WriteLine( "Plugin description: {0}", p.Description );
|
|
|
|
|
foreach( PluginModule module in p.Modules ) {
|
|
|
|
|
HandleModule( module, game );
|
|
|
|
|
}
|
2015-06-19 22:11:48 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-19 22:37:57 -04:00
|
|
|
|
|
|
|
|
|
static void HandleModule( PluginModule module, Game game ) {
|
|
|
|
|
switch( module.ModuleType ) {
|
|
|
|
|
case PluginModuleType.Command:
|
2015-06-20 00:19:07 -04:00
|
|
|
|
Command cmd = Utils.New<Command>( module.Type, game );
|
2015-06-19 22:37:57 -04:00
|
|
|
|
game.CommandManager.RegisterCommand( cmd );
|
|
|
|
|
break;
|
2015-06-19 22:54:21 -04:00
|
|
|
|
|
|
|
|
|
case PluginModuleType.EntityModel:
|
2015-06-20 00:19:07 -04:00
|
|
|
|
IModel model = Utils.New<IModel>( module.Type, game );
|
2015-06-19 22:54:21 -04:00
|
|
|
|
game.ModelCache.AddModel( model );
|
|
|
|
|
break;
|
2015-06-20 00:19:07 -04:00
|
|
|
|
|
|
|
|
|
case PluginModuleType.MapBordersRenderer:
|
|
|
|
|
game.MapBordersRendererTypes.Add( module.Type );
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PluginModuleType.EnvironmentRenderer:
|
|
|
|
|
game.EnvRendererTypes.Add( module.Type );
|
|
|
|
|
break;
|
|
|
|
|
|
2015-06-20 02:37:02 -04:00
|
|
|
|
case PluginModuleType.WeatherRenderer:
|
|
|
|
|
game.WeatherRendererTypes.Add( module.Type );
|
|
|
|
|
break;
|
|
|
|
|
|
2015-06-19 22:37:57 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-19 22:11:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-06-20 02:37:02 -04:00
|
|
|
|
public enum PluginModuleType {
|
|
|
|
|
Command,
|
|
|
|
|
EntityModel,
|
2015-06-20 00:19:07 -04:00
|
|
|
|
MapBordersRenderer,
|
2015-06-19 22:11:48 -04:00
|
|
|
|
EnvironmentRenderer,
|
|
|
|
|
WeatherRenderer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class PluginModule {
|
|
|
|
|
|
|
|
|
|
public PluginModuleType ModuleType;
|
|
|
|
|
|
2015-06-19 22:54:21 -04:00
|
|
|
|
public Type Type;
|
2015-06-19 22:11:48 -04:00
|
|
|
|
|
|
|
|
|
public PluginModule( PluginModuleType moduleType, Type implementation ) {
|
|
|
|
|
ModuleType = moduleType;
|
2015-06-19 22:54:21 -04:00
|
|
|
|
Type = implementation;
|
2015-06-19 22:11:48 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|