Exemplo n.º 1
0
        /// <summary>
        /// Load addon config.
        /// </summary>
        /// <param name="addon">Addon.</param>
        public void LoadAddonConfig(IAddonClient <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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Compare configs.
 /// </summary>
 /// <param name="other"> Config.</param>
 /// <returns>Int.</returns>
 public int CompareTo(IAddonClient <IConfig> other) => 0;
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NPClient"/> class.
        /// </summary>
        /// <param name="plugin">Plugin class.</param>
        public NPClient(MainClass plugin)
        {
            NPManager.Singleton = this;
            this.plugin         = plugin;
            Logger = new PluginLogger();
            string pluginDir = Path.Combine(Paths.Plugins, "NetworkedPlugins");

            tokenPath = Path.Combine(pluginDir, $"NPToken_{Server.Port}.token");
            if (!Directory.Exists(pluginDir))
            {
                Directory.CreateDirectory(pluginDir);
            }
            if (!Directory.Exists(Path.Combine(pluginDir, "addons-" + Server.Port)))
            {
                Directory.CreateDirectory(Path.Combine(pluginDir, "addons-" + Server.Port));
            }
            remoteConfigsPath = Path.Combine(pluginDir, "remoteconfigs-" + Server.Port);
            if (!Directory.Exists(remoteConfigsPath))
            {
                Directory.CreateDirectory(remoteConfigsPath);
            }
            string[] addonsFiles = Directory.GetFiles(Path.Combine(pluginDir, "addons-" + Server.Port), "*.dll");
            Log.Info($"Loading {addonsFiles.Length} addons.");
            foreach (var file in addonsFiles)
            {
                Assembly a = Assembly.LoadFrom(file);
                try
                {
                    foreach (Type t in a.GetTypes().Where(type => !type.IsAbstract && !type.IsInterface))
                    {
                        if (!t.BaseType.IsGenericType || t.BaseType.GetGenericTypeDefinition() != typeof(NPAddonClient <>))
                        {
                            continue;
                        }

                        IAddonClient <IConfig> addon = null;

                        var constructor = t.GetConstructor(Type.EmptyTypes);
                        if (constructor != null)
                        {
                            addon = constructor.Invoke(null) as IAddonClient <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 IAddonClient <IConfig>;
                            }
                        }

                        if (addon == null)
                        {
                            continue;
                        }

                        var addonType = addon.GetType();
                        var prop      = addonType.GetProperty("DefaultPath", BindingFlags.Public | BindingFlags.Instance);
                        var field     = prop.GetBackingField();
                        field.SetValue(addon, Path.Combine(pluginDir, $"addons-{Server.Port}"));

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

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

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

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

                        ClientAddons.Add(addon.AddonId, addon);
                        LoadAddonConfig(addon);
                        if (!addon.Config.IsEnabled)
                        {
                            return;
                        }

                        Logger.Info($"Loading addon \"{addon.AddonName}\" ({addon.AddonVersion}) made by {addon.AddonAuthor}.");
                        addon.OnEnable();
                        Logger.Info($"Waiting to client connections..");
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error($"Failed loading addon {Path.GetFileNameWithoutExtension(file)}. {ex.ToString()}");
                }
            }
            Logger.Info($"Starting CLIENT network...");
            eventHandlers = new EventHandlers(this);
        }