forked from aedenthorn/ValheimMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingSearcher.cs
127 lines (105 loc) · 5 KB
/
SettingSearcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Based on code made by MarC0 / ManlyMarco
// Copyright 2018 GNU General Public License v3.0
using System;
using BepInEx;
using BepInEx.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace ConfigurationManager
{
internal static class SettingSearcher
{
private static readonly ICollection<string> _updateMethodNames = new[]
{
"Update",
"FixedUpdate",
"LateUpdate",
"OnGUI"
};
public static void CollectSettings(out IEnumerable<SettingEntryBase> results, out List<string> modsWithoutSettings, bool showDebug)
{
modsWithoutSettings = new List<string>();
try
{
results = GetBepInExCoreConfig();
}
catch (Exception ex)
{
results = Enumerable.Empty<SettingEntryBase>();
BepInExPlugin.Logger.LogError(ex);
}
var allPlugins = Utilities.Utils.FindPlugins();
BepInExPlugin.Dbgl($"all plugins: {allPlugins.Length}");
foreach (var plugin in allPlugins)
{
if (plugin == null)
continue;
//BepInExPlugin.Dbgl(plugin.name);
if (plugin.Info.Metadata.GUID == "com.bepis.bepinex.configurationmanager" || plugin.enabled == false)
{
BepInExPlugin.Dbgl($"plugin: {plugin.Info.Metadata.Name} enabled {plugin.enabled}");
}
//BepInExPlugin.Dbgl($"plugin: {plugin.Info.Metadata.Name} enabled {plugin.enabled}");
var type = plugin.GetType();
var pluginInfo = plugin.Info.Metadata;
if (type.GetCustomAttributes(typeof(BrowsableAttribute), false).Cast<BrowsableAttribute>()
.Any(x => !x.Browsable))
{
BepInExPlugin.Dbgl($"{pluginInfo.Name} has no settings, skipping.");
modsWithoutSettings.Add(pluginInfo.Name);
continue;
}
var detected = new List<SettingEntryBase>();
detected.AddRange(GetPluginConfig(plugin).Cast<SettingEntryBase>());
int count = detected.FindAll(x => x.Browsable == false).Count;
if(count > 0)
{
BepInExPlugin.Dbgl($"{count} settings are not browseable, removing.");
detected.RemoveAll(x => x.Browsable == false);
}
if (!detected.Any())
{
BepInExPlugin.Dbgl($"{pluginInfo.Name} has no showable settings, skipping.");
modsWithoutSettings.Add(pluginInfo.Name);
}
// Allow to enable/disable plugin if it uses any update methods ------
if (showDebug && type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Any(x => _updateMethodNames.Contains(x.Name)))
{
// todo make a different class for it and fix access modifiers?
var enabledSetting = LegacySettingEntry.FromNormalProperty(plugin, type.GetProperty("enabled"), pluginInfo, plugin);
enabledSetting.DispName = "!Allow plugin to run on every frame";
enabledSetting.Description = "Disabling this will disable some or all of the plugin's functionality.\nHooks and event-based functionality will not be disabled.\nThis setting will be lost after game restart.";
enabledSetting.IsAdvanced = true;
detected.Add(enabledSetting);
}
if (detected.Any())
{
//BepInExPlugin.Dbgl($"Adding {pluginInfo.Name} to config manager.");
results = results.Concat(detected);
}
}
}
/// <summary>
/// Bepinex 5 config
/// </summary>
private static IEnumerable<SettingEntryBase> GetBepInExCoreConfig()
{
var coreConfigProp = typeof(ConfigFile).GetProperty("CoreConfig", BindingFlags.Static | BindingFlags.NonPublic);
if (coreConfigProp == null) throw new ArgumentNullException(nameof(coreConfigProp));
var coreConfig = (ConfigFile)coreConfigProp.GetValue(null, null);
var bepinMeta = new BepInPlugin("BepInEx", "BepInEx", typeof(BepInEx.Bootstrap.Chainloader).Assembly.GetName().Version.ToString());
return coreConfig
.Select(x => new ConfigSettingEntry(x.Value, null) { IsAdvanced = true, PluginInfo = bepinMeta })
.Cast<SettingEntryBase>();
}
/// <summary>
/// Used by bepinex 5 plugins
/// </summary>
private static IEnumerable<ConfigSettingEntry> GetPluginConfig(BaseUnityPlugin plugin)
{
return plugin.Config.Select(x => new ConfigSettingEntry(x.Value, plugin));
}
}
}