[英]How would I go about this? Different projects communicating? C#
好的,我不确定如何去解释这一点,也不知道如何去做,但是我将尝试逐步解释我想要什么。
我想制作一个包含例如EntitySpawnEvent对象的API。 它可能看起来像这样:
namespace ExampleAPI
{
class EntitySpawnEvent
{
private bool cancelled;
private Entity entity;
public EntitySpawnEvent(Entity entity)
{
this.entity = entity;
this.cancelled = false;
}
public void SetCancelled(bool cancelled)
{
this.cancelled = cancelled;
}
public bool IsCancelled()
{
return this.cancelled;
}
}
}
然后,我将拥有使用此API的服务器。 该服务器还将加载也使用API的插件。 服务器可能是这样的:
using System.Generics;
using ExampleAPI;
namespace ExampleServer
{
class Server
{
private List<Plugin> plugins;
public OnEnable()
{
LoadPlugins();
}
private void LoadPlugins()
{
// Loop through all "plugins" in the "/plugins" folder.
// Add them all to the list of plugins.
}
}
}
然后,当服务器想要生成实体时,它将事件抛出给所有插件,这些插件可以操纵事件的信息。 例如,是否取消事件。 插件的事件监听器可能如下所示:
using ExampleAPI;
namespace ExamplePlugin
{
class Plugin : EventListener
{
public void onEntitySpawn(EntitySpawnEvent event)
{
event.SetCancelled(true);
}
}
}
服务器将抛出以下内容:
using ExampleAPI;
namespace ExampleServer
{
class ExampleEventThrower
{
private Server server;
public ExampleEventThrower(Server server)
{
this.server = server;
SpawnEntity();
}
void SpawnEntity()
{
EntitySpawnEvent event = new EntitySpawnEvent(new Entity()); // Entity would also be part of the API
foreach (Plugin plugin in server.GetPlugins())
{
plugin.onEntitySpawn(event); // Here the plugin could manipulate the values of the EntitySpawnEvent
}
if (!event.IsCancelled())
{
// Spawn entity
}
}
}
}
当然,这些只是极其基本的代码示例,但它们应该有助于解释我想要的内容。
基本上,我想知道和做的事情如下:
我有一个导出服务器。 服务器有一个/ plugins文件夹。用户可以使用API制作自己的插件,将其导出并将其放在/ plugins文件夹中。服务器将加载插件,并让其修改所有事件等。
我的关键问题是,应如何导出和加载插件,以便它们可以操纵事件等? 我可以将它们导出为DDL吗? 我不知道。 我猜它有点类似于Bukkit的工作方式,但是Java包含了所有内容,您只需将其导出为.jar文件即可。
任何帮助将不胜感激。 谢谢!
没什么好看的...
听起来您想让插件从您知道的接口上运行,并在运行时加载插件。
这应该可以帮助您构建DLL:
http://msdn.microsoft.com/en-us/library/3707x96z.aspx
这应该有助于在运行时动态加载DLL:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.