コード例 #1
0
        public void RegisterAttributes(Plugin plugin)
        {
            Type type = plugin.GetType();

            const BindingFlags allMembers = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            foreach (FieldInfo field in type.GetFields(allMembers))
            {
                LangOption langOption = field.GetCustomAttribute <LangOption>();
                if (langOption != null)
                {
                    string key = langOption.Key ?? PluginManager.ToUpperSnakeCase(field.Name);

                    string file = plugin.Details.langFile;
                    if (file == null)
                    {
                        PluginManager.Manager.Logger.Error("LANG_MANAGER", $"{plugin} is trying to register attribute lang {field.Name}, but does not have {nameof(PluginDetails.langFile)} in its {nameof(PluginDetails)} set.");
                        return;
                    }

                    if (string.IsNullOrWhiteSpace(key))
                    {
                        PluginManager.Manager.Logger.Error("LANG_MANAGER", $"{plugin} is trying to register attribute lang {field.Name}, but it has no valid key. Is the variable all underscores with no config key overload?");
                        continue;
                    }

                    if (field.FieldType != typeof(string))
                    {
                        PluginManager.Manager.Logger.Error("LANG_MANAGER", $"{plugin} is trying to register attribute lang {field.Name}, but the type ({field.FieldType}) is not a string.");
                        continue;
                    }

                    if (!RegisterTranslation(plugin, new LangSetting(key, (string)field.GetValue(plugin), file)))
                    {
                        // Failed register so it should not be registered to refresh every round restart.
                        PluginManager.Manager.Logger.Debug("LANG_MANAGER", $"Unable to register attribute translation {field.Name} from {plugin}.");
                        continue;
                    }

                    if (!langFields.ContainsKey(plugin))
                    {
                        langFields.Add(plugin, new Dictionary <string, FieldInfo>());
                    }

                    langFields[plugin].Add(key, field);
                }
            }
        }
コード例 #2
0
        public void RegisterAttributes(Plugin plugin)
        {
            Type type = plugin.GetType();

            const BindingFlags allMembers = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;

            foreach (FieldInfo field in type.GetFields(allMembers))
            {
                ConfigOption configOption = field.GetCustomAttribute <ConfigOption>();
                if (configOption != null)
                {
                    string prefix = plugin.Details.configPrefix;
                    if (prefix == null)
                    {
                        PluginManager.Manager.Logger.Error("CONFIG_MANAGER", $"{plugin} is trying to register attribute config {field.Name}, but does not have {nameof(PluginDetails.configPrefix)} in its {nameof(PluginDetails)} set.");
                        return;
                    }

                    string key = configOption.Key ?? PluginManager.ToUpperSnakeCase(field.Name);

                    if (string.IsNullOrWhiteSpace(key))
                    {
                        PluginManager.Manager.Logger.Error("CONFIG_MANAGER", $"{plugin} is trying to register attribute config {field.Name}, but it has no valid key. Is the variable all underscores with no config key overload?");
                        continue;
                    }

                    if (field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(LiveConfig <>))
                    {
                        string     realKey    = prefix + "_" + key;
                        LiveConfig liveConfig = (LiveConfig)field.GetValue(plugin);
                        if (!RegisterConfig(plugin, new ConfigSetting(realKey, liveConfig.DefaultValue, configOption.Randomized, configOption.PrimaryUser, configOption.Description)))
                        {
                            // Failed register so it should not be set.
                            field.SetValue(plugin, null);
                            continue;
                        }

                        liveConfig.ManagerInit(realKey, plugin);

                        continue;
                    }

                    if (!typeGetters.ContainsKey(field.FieldType))
                    {
                        PluginManager.Manager.Logger.Error("CONFIG_MANAGER", $"{plugin} is trying to register attribute config {field.Name}, but the type ({field.FieldType}) is not a config-allowed type.");
                        continue;
                    }

                    if (!RegisterConfig(plugin, new ConfigSetting(prefix + "_" + key, field.GetValue(plugin), configOption.Randomized, configOption.PrimaryUser, configOption.Description)))
                    {
                        // Failed register so it should not be registered to refresh every round restart.
                        PluginManager.Manager.Logger.Debug("CONFIG_MANAGER", $"Unable to register attribute config {field.Name} from {plugin}.");
                        continue;
                    }

                    if (!configFields.ContainsKey(plugin))
                    {
                        configFields.Add(plugin, new Dictionary <string, FieldInfo>());
                    }

                    configFields[plugin].Add(key, field);
                }
            }
        }