1+ using BepInEx ;
2+ using BepInEx . Bootstrap ;
3+ using BepInEx . Configuration ;
4+ using BepInEx . Logging ;
5+ using System ;
6+ using UnityEngine ;
7+
8+ namespace DrakiaXYZ . Helpers
9+ {
10+ internal class DependencyChecker
11+ {
12+ /// <summary>
13+ /// Check that all of the BepInDependency entries for the given pluginType are available and instantiated. This allows a
14+ /// plugin to validate that its dependent plugins weren't disabled post-dependency check (Such as for the wrong EFT version)
15+ /// </summary>
16+ /// <param name="Logger"></param>
17+ /// <param name="Info"></param>
18+ /// <param name="pluginType"></param>
19+ /// <param name="Config"></param>
20+ /// <returns></returns>
21+ public static bool ValidateDependencies ( ManualLogSource Logger , PluginInfo Info , Type pluginType , ConfigFile Config = null )
22+ {
23+ var noVersion = new Version ( "0.0.0" ) ;
24+ var dependencies = pluginType . GetCustomAttributes ( typeof ( BepInDependency ) , true ) as BepInDependency [ ] ;
25+
26+ foreach ( var dependency in dependencies )
27+ {
28+ PluginInfo pluginInfo ;
29+ if ( ! Chainloader . PluginInfos . TryGetValue ( dependency . DependencyGUID , out pluginInfo ) )
30+ {
31+ pluginInfo = null ;
32+ }
33+
34+ // If the plugin isn't found, or the instance isn't enabled, it means the required plugin failed to load
35+ if ( pluginInfo == null || pluginInfo . Instance ? . enabled == false )
36+ {
37+ string dependencyName = pluginInfo ? . Metadata . Name ?? dependency . DependencyGUID ;
38+ string dependencyVersion = "" ;
39+ if ( dependency . MinimumVersion > noVersion )
40+ {
41+ dependencyVersion = $ " v{ dependency . MinimumVersion } ";
42+ }
43+
44+ string errorMessage = $ "ERROR: This version of { Info . Metadata . Name } v{ Info . Metadata . Version } depends on { dependencyName } { dependencyVersion } , but it was not loaded.";
45+ Logger . LogError ( errorMessage ) ;
46+ Chainloader . DependencyErrors . Add ( errorMessage ) ;
47+
48+ if ( Config != null )
49+ {
50+ // This results in a bogus config entry in the BepInEx config file for the plugin, but it shouldn't hurt anything
51+ // We leave the "section" parameter empty so there's no section header drawn
52+ Config . Bind ( "" , "MissingDeps" , "" , new ConfigDescription (
53+ errorMessage , null , new ConfigurationManagerAttributes
54+ {
55+ CustomDrawer = ErrorLabelDrawer ,
56+ ReadOnly = true ,
57+ HideDefaultButton = true ,
58+ HideSettingName = true ,
59+ Category = null
60+ }
61+ ) ) ;
62+ }
63+
64+ return false ;
65+ }
66+ }
67+
68+ return true ;
69+ }
70+
71+ static void ErrorLabelDrawer ( ConfigEntryBase entry )
72+ {
73+ GUIStyle styleNormal = new GUIStyle ( GUI . skin . label ) ;
74+ styleNormal . wordWrap = true ;
75+ styleNormal . stretchWidth = true ;
76+
77+ GUIStyle styleError = new GUIStyle ( GUI . skin . label ) ;
78+ styleError . stretchWidth = true ;
79+ styleError . alignment = TextAnchor . MiddleCenter ;
80+ styleError . normal . textColor = Color . red ;
81+ styleError . fontStyle = FontStyle . Bold ;
82+
83+ // General notice that we're the wrong version
84+ GUILayout . BeginVertical ( ) ;
85+ GUILayout . Label ( entry . Description . Description , styleNormal , new GUILayoutOption [ ] { GUILayout . ExpandWidth ( true ) } ) ;
86+
87+ // Centered red disabled text
88+ GUILayout . Label ( "Plugin has been disabled!" , styleError , new GUILayoutOption [ ] { GUILayout . ExpandWidth ( true ) } ) ;
89+ GUILayout . EndVertical ( ) ;
90+ }
91+
92+ #pragma warning disable 0169 , 0414 , 0649
93+ internal sealed class ConfigurationManagerAttributes
94+ {
95+ public bool ? ShowRangeAsPercent ;
96+ public System . Action < BepInEx . Configuration . ConfigEntryBase > CustomDrawer ;
97+ public CustomHotkeyDrawerFunc CustomHotkeyDrawer ;
98+ public delegate void CustomHotkeyDrawerFunc ( BepInEx . Configuration . ConfigEntryBase setting , ref bool isCurrentlyAcceptingInput ) ;
99+ public bool ? Browsable ;
100+ public string Category ;
101+ public object DefaultValue ;
102+ public bool ? HideDefaultButton ;
103+ public bool ? HideSettingName ;
104+ public string Description ;
105+ public string DispName ;
106+ public int ? Order ;
107+ public bool ? ReadOnly ;
108+ public bool ? IsAdvanced ;
109+ public System . Func < object , string > ObjToStr ;
110+ public System . Func < string , object > StrToObj ;
111+ }
112+ }
113+ }
0 commit comments