简体   繁体   中英

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. 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. This server will also load plugins that also uses the 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.

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

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:

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

This should help load the DLL dynamically at runtime:

Can I load a .NET assembly at runtime and instantiate a type knowing only the name?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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