1 /** 2 Copyright: Copyright (c) 2015-2016 Andrey Penechko. 3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module pluginlib.interfaces; 8 9 import std.experimental.logger; 10 import std.conv : to; 11 import pluginlib; 12 13 import std.traits : moduleName; 14 mixin template IdAndSemverFrom(alias pinfoModule) 15 { 16 mixin("import pinfo = " ~ moduleName!pinfoModule ~ ";"); 17 override string id() @property { return pinfo.id; } 18 override string semver() @property { return pinfo.semver; } 19 } 20 21 /// Basic plugin interface 22 abstract class IPlugin 23 { 24 /// Unique identifier ("exampleplugin") 25 string id() @property; 26 /// Human readable name ("Example plugin") 27 //string name() @property; 28 /// Valid semver version string. i.e. 0.1.0-rc.1 29 string semver() @property; 30 /// Register resource managers provided by this plugin 31 void registerResourceManagers(void delegate(IResourceManager) registerResourceManager) {} 32 /// Get references to resource managers and call their methods. 33 /// Resources are loaded before preInit 34 void registerResources(IResourceManagerRegistry resmanRegistry) {} 35 /// Private initialization using resources loaded by resource managers 36 void preInit() {} 37 /// Get references to other plugins. Other plugins may call this plugin from now 38 void init(IPluginManager pluginman) {} 39 /// Called after init. Do something with data retrieved at previous stage 40 void postInit() {} 41 } 42 43 interface IPluginManager 44 { 45 /// Returns reference to plugin instance if pluginId was registered. 46 IPlugin findPlugin(TypeInfo pluginType); 47 IPlugin findPluginById(string pluginId); 48 49 /// Guaranteed to return not null reference to P 50 final P getPlugin(P)() 51 { 52 import std.exception : enforce; 53 import std.string : format; 54 IPlugin plugin = findPlugin(typeid(P)); 55 P exactPlugin = cast(P)plugin; 56 enforce(exactPlugin, format("Cannot find plugin '%s'", typeid(P))); 57 return exactPlugin; 58 } 59 } 60 61 /// Basic plugin interface 62 /// Resource manager version is the same as plugin's one that registered it 63 abstract class IResourceManager 64 { 65 /// Unique identifier ("config") 66 string id() @property; 67 /// Human readable name ("Config resource manager") 68 //string name() @property; 69 /// Load/create needed resources 70 void preInit() {} 71 /// Get references to other plugins 72 void init(IResourceManagerRegistry resmanRegistry) {} 73 /// Called after init. Do something with data retrieved at previous stage 74 void loadResources() {} 75 /// Called after loadResources. Do something with data retrieved at previous stage 76 void postInit() {} 77 } 78 79 interface IResourceManagerRegistry 80 { 81 /// Returns reference to ResourceManager instance if resmanId was registered 82 IResourceManager findResourceManager(TypeInfo rmType); 83 84 /// Guaranteed to return not null reference to RM 85 final RM getResourceManager(RM)() 86 { 87 import std.exception : enforce; 88 IResourceManager resman = findResourceManager(typeid(RM)); 89 RM exactResman = cast(RM)resman; 90 enforce(exactResman); 91 return exactResman; 92 } 93 }