/// <summary>
        /// Register command from addon.
        /// </summary>
        /// <param name="addon">Addon.</param>
        /// <param name="command">Command interface.</param>
        public void RegisterCommand(IAddonDedicated <IConfig, IConfig> addon, ICommand command)
        {
            if (addon.Commands[command.Type].ContainsKey(command.CommandName.ToUpper()))
            {
                Logger.Info($"[{command.Type}] Command \"{command.CommandName.ToUpper()}\" is already registered in addon \"{addon.AddonName}\"!");
                return;
            }

            addon.Commands[command.Type].Add(command.CommandName.ToUpper(), command);
            Logger.Info($"[{command.Type}] Command \"{command.CommandName.ToUpper()}\" registered in addon \"{addon.AddonName}\".");
        }
        /// <summary>
        /// Load addon config.
        /// </summary>
        /// <param name="addon">Addon.</param>
        public void LoadAddonConfig(IAddonDedicated <IConfig, IConfig> addon)
        {
            if (!Directory.Exists(addon.AddonPath))
            {
                Directory.CreateDirectory(addon.AddonPath);
            }

            if (!File.Exists(Path.Combine(addon.AddonPath, "config.yml")))
            {
                File.WriteAllText(Path.Combine(addon.AddonPath, "config.yml"), Serializer.Serialize(addon.Config));
            }

            var cfg = (IConfig)Deserializer.Deserialize(File.ReadAllText(Path.Combine(addon.AddonPath, "config.yml")), addon.Config.GetType());

            File.WriteAllText(Path.Combine(addon.AddonPath, "config.yml"), Serializer.Serialize(cfg));
            addon.Config.CopyProperties(cfg);
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Host"/> class.
        /// </summary>
        public Host()
        {
            Logger    = new ConsoleLogger();
            Singleton = this;

            NetDebug.Logger = new NetworkLogger();

            if (!File.Exists("config.yml"))
            {
                File.WriteAllText("config.yml", Serializer.Serialize(new DedicatedConfig()));
            }

            config = Deserializer.Deserialize <DedicatedConfig>(File.ReadAllText("config.yml"));

            if (!Directory.Exists("addons"))
            {
                Directory.CreateDirectory("addons");
            }

            if (!Directory.Exists("servers"))
            {
                Directory.CreateDirectory("servers");
            }

            string[] addonsFiles = Directory.GetFiles("addons", "*.dll");
            Logger.Info($"Loading {addonsFiles.Length} addons.");
            foreach (var file in addonsFiles)
            {
                Assembly a = Assembly.LoadFrom(file);
                try
                {
                    string addonID = string.Empty;
                    foreach (Type t in a.GetTypes().Where(type => !type.IsAbstract && !type.IsInterface))
                    {
                        if (!t.BaseType.IsGenericType || t.BaseType.GetGenericTypeDefinition() != typeof(NPAddonDedicated <,>))
                        {
                            continue;
                        }

                        IAddonDedicated <IConfig, IConfig> addon = null;
                        var constructor = t.GetConstructor(Type.EmptyTypes);
                        if (constructor != null)
                        {
                            addon = constructor.Invoke(null) as IAddonDedicated <IConfig, IConfig>;
                        }
                        else
                        {
                            var value = Array.Find(t.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), property => property.PropertyType == t)?.GetValue(null);

                            if (value != null)
                            {
                                addon = value as IAddonDedicated <IConfig, IConfig>;
                            }
                        }

                        if (addon == null)
                        {
                            continue;
                        }

                        var addonType = addon.GetType();
                        var property  = addonType.GetProperty("DefaultPath", BindingFlags.Public | BindingFlags.Instance);
                        var field     = property.GetBackingField();
                        field.SetValue(addon, Path.Combine("addons"));

                        property = addonType.GetProperty("AddonPath", BindingFlags.Public | BindingFlags.Instance);
                        field    = property.GetBackingField();
                        field.SetValue(addon, Path.Combine(addon.DefaultPath, addon.AddonName));

                        property = addonType.GetProperty("Manager", BindingFlags.Public | BindingFlags.Instance);
                        field    = property.GetBackingField();
                        field.SetValue(addon, this);

                        property = addonType.GetProperty("Logger", BindingFlags.Public | BindingFlags.Instance);
                        field    = property.GetBackingField();
                        field.SetValue(addon, Logger);

                        LoadAddonConfig(addon);

                        if (!addon.Config.IsEnabled)
                        {
                            return;
                        }

                        foreach (var type in a.GetTypes().Where(type => !type.IsAbstract && !type.IsInterface))
                        {
                            if (!type.BaseType.IsGenericType || type.BaseType.GetGenericTypeDefinition() != typeof(NPAddonHandler <>))
                            {
                                continue;
                            }

                            IAddonHandler <IConfig> addonHandler = null;
                            var constructor2 = type.GetConstructor(Type.EmptyTypes);
                            if (constructor != null)
                            {
                                addonHandler = constructor2.Invoke(null) as IAddonHandler <IConfig>;
                            }
                            else
                            {
                                var value = Array.Find(type.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public), property => property.PropertyType == type)?.GetValue(null);

                                if (value != null)
                                {
                                    addonHandler = value as IAddonHandler <IConfig>;
                                }
                            }

                            if (addonHandler == null)
                            {
                                continue;
                            }

                            var addonType2 = addonHandler.GetType();

                            var property2 = addonType2.GetProperty("Manager", BindingFlags.Public | BindingFlags.Instance);
                            var field2    = property2.GetBackingField();
                            field2.SetValue(addonHandler, this);

                            property2 = addonType2.GetProperty("Logger", BindingFlags.Public | BindingFlags.Instance);
                            field2    = property2.GetBackingField();
                            field2.SetValue(addonHandler, Logger);

                            property2 = addonType2.GetProperty("DefaultAddon", BindingFlags.Public | BindingFlags.Instance);
                            field2    = property2.GetBackingField();
                            field2.SetValue(addonHandler, addon);

                            LoadAddonConfig(addonHandler);

                            try
                            {
                                addonHandler.OnEnable();
                            }
                            catch (Exception ex)
                            {
                                Logger.Error($"Failed executing OnEnable in addon handler {addon.AddonName}. {ex.ToString()}");
                            }

                            if (DedicatedAddonHandlers.ContainsKey(addon.AddonId))
                            {
                                Logger.Error($"Addon with id \"{addon.AddonId}\" ({addon.AddonName}) is already registered!");
                                break;
                            }

                            foreach (var Cmdtype in a.GetTypes())
                            {
                                if (typeof(ICommand).IsAssignableFrom(Cmdtype))
                                {
                                    ICommand cmd = (ICommand)Activator.CreateInstance(Cmdtype);
                                    RegisterCommand(addon, cmd);
                                }
                            }


                            AddonAssemblies.Add(a, addon.AddonId);
                            DedicatedAddonHandlers.Add(addon.AddonId, addonHandler);
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error($"Failed loading addon {Path.GetFileNameWithoutExtension(file)}. {ex.ToString()}");
                }
            }

            Logger.Info($"Starting HOST network...");
            StartNetworkHost();
        }
 /// <summary>
 /// Compare configs.
 /// </summary>
 /// <param name="other"> Config.</param>
 /// <returns>Int.</returns>
 public int CompareTo(IAddonDedicated <IConfig, IConfig> other) => 0;