简体   繁体   English

我将如何处理? 不同项目沟通? C#

[英]How would I go about this? Different projects communicating? C#

Okay, I'm not sure how to go about explaining this, nor how to do it, but I will try to explain what I want step-by-step. 好的,我不确定如何去解释这一点,也不知道如何去做,但是我将尝试逐步解释我想要什么。

I want to make an API that contains, for example an EntitySpawnEvent object. 我想制作一个包含例如EntitySpawnEvent对象的API。 It might look something like this: 它可能看起来像这样:

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;
        }
    }
}

Then I have I will have a server that uses this API. 然后,我将拥有使用此API的服务器。 This server will also load plugins that also uses the API. 该服务器还将加载也使用API​​的插件。 The server might be something like this: 服务器可能是这样的:

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.
        }
    }
}

Then later when the server wants to spawn an entity, it throws the event to all plugins, the plugins can manipulate the event's information. 然后,当服务器想要生成实体时,它将事件抛出给所有插件,这些插件可以操纵事件的信息。 For example, whether or not to cancel the event. 例如,是否取消事件。 The plugin's event listener could look something like this: 插件的事件监听器可能如下所示:

using ExampleAPI;

namespace ExamplePlugin
{
    class Plugin : EventListener
    {
        public void onEntitySpawn(EntitySpawnEvent event)
        {
            event.SetCancelled(true);
        }
    }
}

And the server would throw it something like this: 服务器将抛出以下内容:

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
            }
        }
    }
}

Of course these are just extremely basic code examples, but they should help explain what I want. 当然,这些只是极其基本的代码示例,但它们应该有助于解释我想要的内容。

Basically, what I want to know and do is the following: 基本上,我想知道和做的事情如下:

I have an exported Server. 我有一个导出服务器。 The Server have a /plugins folder The user can make their own plugins using the API, export them and put them in the /plugins folder The Server would load the plugin and let it modify all the events and such. 服务器有一个/ plugins文件夹。用户可以使用API​​制作自己的插件,将其导出并将其放在/ plugins文件夹中。服务器将加载插件,并让其修改所有事件等。

My key question is, how should the plugins be exported and loaded, so they can manipulate the events and such? 我的关键问题是,应如何导出和加载插件,以便它们可以操纵事件等? Do I export them as DDLs? 我可以将它们导出为DDL吗? I have no idea. 我不知道。 I guess it's sort of similar to the way Bukkit works, but there everything's in Java and you just export it is a .jar file. 我猜它有点类似于Bukkit的工作方式,但是Java包含了所有内容,您只需将其导出为.jar文件即可。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

So few things to take a look at... 没什么好看的...

It sounds like you want to have plugins run off of an interface that you know about, and load the plugins at runtime. 听起来您想让插件从您知道的接口上运行,并在运行时加载插件。

This should help you build the DLLs: 这应该可以帮助您构建DLL:

http://msdn.microsoft.com/en-us/library/3707x96z.aspx http://msdn.microsoft.com/en-us/library/3707x96z.aspx

This should help load the DLL dynamically at runtime: 这应该有助于在运行时动态加载DLL:

Can I load a .NET assembly at runtime and instantiate a type knowing only the name? 我可以在运行时加载.NET程序集并实例化仅知道名称的类型吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM