11using System ;
22using System . IO ;
3+ using System . Linq ;
34using System . Reflection ;
45using Nomnom . UnityProjectPatcher . Editor . Steps ;
56using UnityEditor ;
7+ using UnityEditor . Build ;
8+ using UnityEditor . PackageManager ;
69using UnityEditor . SceneManagement ;
710using UnityEngine ;
811
@@ -15,6 +18,12 @@ public class PatcherWindow: EditorWindow {
1518
1619 private GUIStyle _leftAlignedGreyLabel ;
1720 private GUIStyle _rightAlignedGreyLabel ;
21+
22+ private bool _hasBepInExPackage ;
23+ private bool _hasBepInExFlag ;
24+ // private bool _hasGameWrapperPackage;
25+ private UPPatcherAttribute _foundPackageAttribute ;
26+ private PackageCollection _packageCollection ;
1827
1928 [ MenuItem ( "Tools/Unity Project Patcher/Open Window" ) ]
2029 private static void Open ( ) {
@@ -33,9 +42,8 @@ private void OnEnable() {
3342 Nomnom . UnityProjectPatcher . PatcherUtility . GetUserSettings ( ) ;
3443
3544 EditorApplication . delayCall += ( ) => {
36- var ( version , gameVersion ) = PatcherUtility . GetVersions ( ) ;
37- _patcherVersion = version ;
38- _gameWrapperVersion = gameVersion ;
45+ _packageCollection = null ;
46+ CheckPackages ( ) ;
3947 Repaint ( ) ;
4048 } ;
4149 _gameWrapperVersion = null ;
@@ -52,6 +60,29 @@ private void OnEnable() {
5260 _gameWrapperGuiFunction = gameWrapperOnGUIFunction ;
5361 }
5462
63+ private void OnFocus ( ) {
64+ Nomnom . UnityProjectPatcher . PatcherUtility . GetUserSettings ( ) ;
65+ CheckPackages ( ) ;
66+ }
67+
68+ private void CheckPackages ( ) {
69+ _packageCollection ??= PatcherUtility . GetPackages ( ) ;
70+
71+ var ( version , gameVersion ) = PatcherUtility . GetVersions ( _packageCollection ) ;
72+ _patcherVersion = version ;
73+ _gameWrapperVersion = gameVersion ;
74+
75+ // check packages
76+ _hasBepInExPackage = _packageCollection . Any ( x => x . name == "com.nomnom.unity-project-patcher-bepinex" ) ;
77+ _hasBepInExFlag = PlayerSettings . GetScriptingDefineSymbols ( NamedBuildTarget . Standalone ) . Contains ( "ENABLE_BEPINEX" ) ;
78+ _packageCollection = _packageCollection ;
79+ _foundPackageAttribute = PatcherUtility . GetGameWrapperAttribute ( ) ;
80+ // _hasGameWrapperPackage = false;
81+ // if (!string.IsNullOrEmpty(_foundPackageAttribute?.PackageName)) {
82+ // _hasGameWrapperPackage = _packageCollection.Any(x => x.name == _foundPackageAttribute.PackageName);
83+ // }
84+ }
85+
5586 private void OnGUI ( ) {
5687 if ( _leftAlignedGreyLabel == null ) {
5788 _leftAlignedGreyLabel = new GUIStyle ( EditorStyles . centeredGreyMiniLabel ) { alignment = TextAnchor . MiddleLeft } ;
@@ -87,6 +118,51 @@ private void OnGUI() {
87118
88119 if ( EditorApplication . isCompiling ) {
89120 EditorGUILayout . LabelField ( "Compiling..." , EditorStyles . centeredGreyMiniLabel ) ;
121+ GUI . enabled = false ;
122+ }
123+
124+ if ( ! _hasBepInExPackage ) {
125+ if ( GUILayout . Button ( "Install BepInEx" ) ) {
126+ EditorApplication . delayCall += ( ) => {
127+ _packageCollection = null ;
128+
129+ EditorUtility . DisplayProgressBar ( "Installing..." , "Installing BepInEx..." , 0.5f ) ;
130+
131+ var request = Client . Add ( "https://github.com/nomnomab/unity-project-patcher-bepinex.git" ) ;
132+ while ( ! request . IsCompleted ) { }
133+ if ( request . Status == StatusCode . Success ) {
134+ EditorUtility . DisplayDialog ( "Success!" , "BepInEx was installed successfully!" , "OK" ) ;
135+ } else {
136+ EditorUtility . DisplayDialog ( "Error" , $ "Failed to install BepInEx! [{ request . Error . errorCode } ] { request . Error . message } ", "OK" ) ;
137+ }
138+
139+ // enable ENABLE_BEPINEX
140+ var existingSymbols = PlayerSettings . GetScriptingDefineSymbols ( NamedBuildTarget . Standalone ) ;
141+ if ( ! existingSymbols . Contains ( "ENABLE_BEPINEX" ) ) {
142+ existingSymbols += ";ENABLE_BEPINEX" ;
143+ PlayerSettings . SetScriptingDefineSymbols ( NamedBuildTarget . Standalone , existingSymbols ) ;
144+ }
145+
146+ EditorUtility . ClearProgressBar ( ) ;
147+ } ;
148+ }
149+ } else {
150+ if ( _hasBepInExFlag && GUILayout . Button ( "Disable BepInEx" ) ) {
151+ EditorApplication . delayCall += ( ) => {
152+ PlayerSettings . SetScriptingDefineSymbols ( NamedBuildTarget . Standalone , PlayerSettings . GetScriptingDefineSymbols ( NamedBuildTarget . Standalone ) . Replace ( "ENABLE_BEPINEX" , "" ) ) ;
153+ } ;
154+ } else if ( ! _hasBepInExFlag && GUILayout . Button ( "Enable BepInEx" ) ) {
155+ EditorApplication . delayCall += ( ) => {
156+ PlayerSettings . SetScriptingDefineSymbols ( NamedBuildTarget . Standalone , PlayerSettings . GetScriptingDefineSymbols ( NamedBuildTarget . Standalone ) + ";ENABLE_BEPINEX" ) ;
157+ } ;
158+ }
159+ }
160+
161+ if ( ! _hasBepInExPackage && _foundPackageAttribute is not null && _foundPackageAttribute . RequiresBepInEx ) {
162+ EditorGUILayout . LabelField ( "Please install all packages!" , EditorStyles . centeredGreyMiniLabel ) ;
163+ EditorGUILayout . LabelField ( $ "bepinex: { ( _hasBepInExPackage ? "good!" : "missing!" ) } ") ;
164+ // EditorGUILayout.LabelField($"{_gameWrapperType.Name}: {(_hasGameWrapperPackage ? "good!" : "missing!")}");
165+ return ;
90166 }
91167
92168 if ( GUILayout . Button ( "Run Patcher" ) ) {
@@ -100,21 +176,30 @@ private void OnGUI() {
100176
101177 try {
102178 if ( string . IsNullOrEmpty ( userSettings . GameFolderPath ) || ! Directory . Exists ( Path . GetFullPath ( userSettings . GameFolderPath ) ) ) {
103- EditorUtility . DisplayDialog ( "Error" , "Please select a valid game folder!" , "Ok" ) ;
179+ EditorUtility . DisplayDialog ( "Error" , "Please select a valid game folder!\n \n Please fix this in your UPPatcherUserSettings asset!" , "Focus UPPatcherUserSettings" ) ;
180+ EditorUtility . FocusProjectWindow ( ) ;
181+ Selection . activeObject = userSettings ;
182+ EditorGUIUtility . PingObject ( userSettings ) ;
104183 return ;
105184 }
106185
107186 if ( string . IsNullOrEmpty ( userSettings . AssetRipperDownloadFolderPath ) ) {
108- EditorUtility . DisplayDialog ( "Error" , "Please select a valid asset ripper download location!" , "Ok" ) ;
187+ EditorUtility . DisplayDialog ( "Error" , "Please select a valid asset ripper download location!\n \n Please fix this in your UPPatcherUserSettings asset!" , "Focus UPPatcherUserSettings" ) ;
188+ EditorUtility . FocusProjectWindow ( ) ;
189+ Selection . activeObject = userSettings ;
190+ EditorGUIUtility . PingObject ( userSettings ) ;
109191 return ;
110192 }
111193
112194 if ( string . IsNullOrEmpty ( userSettings . AssetRipperExportFolderPath ) ) {
113- EditorUtility . DisplayDialog ( "Error" , "Please select a valid asset ripper export location!" , "Ok" ) ;
195+ EditorUtility . DisplayDialog ( "Error" , "Please select a valid asset ripper export location!\n \n Please fix this in your UPPatcherUserSettings asset!" , "Focus UPPatcherUserSettings" ) ;
196+ EditorUtility . FocusProjectWindow ( ) ;
197+ Selection . activeObject = userSettings ;
198+ EditorGUIUtility . PingObject ( userSettings ) ;
114199 return ;
115200 }
116201 } catch {
117- EditorUtility . DisplayDialog ( "Error" , "There is a bad path in the user settings!" , "Ok " ) ;
202+ EditorUtility . DisplayDialog ( "Error" , "There is a bad path in the user settings!\n \n Please fix this in your UPPatcherUserSettings asset! " , "Focus UPPatcherUserSettings " ) ;
118203 EditorUtility . FocusProjectWindow ( ) ;
119204 Selection . activeObject = userSettings ;
120205 EditorGUIUtility . PingObject ( userSettings ) ;
0 commit comments