22using Elements . Core ;
33using FrooxEngine ;
44using HarmonyLib ;
5- using Leap . Unity ;
65using ResoniteModLoader ;
76using System . Collections . Generic ;
87using System . IO ;
98using System . Linq ;
10- using System . Security . Policy ;
119using System . Threading . Tasks ;
1210using UnityPackageImporter . Extractor ;
1311
@@ -17,7 +15,7 @@ public class UnityPackageImporter : ResoniteMod
1715{
1816 public override string Name => "UnityPackageImporter" ;
1917 public override string Author => "dfgHiatus, eia485, delta, Frozenreflex, benaclejames, 989onan" ;
20- public override string Version => "2.2 .0" ;
18+ public override string Version => "2.3 .0" ;
2119 public override string Link => "https://github.com/dfgHiatus/ResoniteUnityPackagesImporter" ;
2220
2321 internal const string UNITY_PACKAGE_EXTENSION = ".unitypackage" ;
@@ -70,7 +68,7 @@ public override void OnEngineInit()
7068 new Harmony ( "net.dfgHiatus.UnityPackageImporter" ) . PatchAll ( ) ;
7169 Config = GetConfiguration ( ) ;
7270 Directory . CreateDirectory ( cachePath ) ;
73- Engine . Current . RunPostInit ( AssetPatch ) ;
71+ Engine . Current . RunPostInit ( ( ) => AssetPatch ( "unitypackage" ) ) ;
7472 }
7573
7674 public static string [ ] DecomposeUnityPackage ( string file )
@@ -122,10 +120,60 @@ public static string[] DecomposeUnityPackage(string file)
122120 return dirsToImport . ToArray ( ) ;
123121 }
124122
125- private static void AssetPatch ( )
123+ private void AssetPatch ( string extension )
126124 {
127- var aExt = Traverse . Create ( typeof ( AssetHelper ) ) . Field < Dictionary < AssetClass , List < string > > > ( "associatedExtensions" ) ;
128- aExt . Value [ AssetClass . Model ] . Add ( "unitypackage" ) ; // Must not have a "." in the name anywhere!!- @989onan
125+ // Revised implementation using reflection to handle API changes
126+ // same fix as the svg import mod.
127+ try
128+ {
129+ Debug ( $ "Attempting to add { extension } support to import system") ;
130+
131+ // Get ImportExtension type via reflection since it's now a struct inside AssetHelper
132+ var assHelperType = typeof ( AssetHelper ) ;
133+ var importExtType = assHelperType . GetNestedType ( "ImportExtension" ,
134+ System . Reflection . BindingFlags . NonPublic ) ;
135+
136+ if ( importExtType == null )
137+ {
138+ Error ( "ImportExtension type not found. This mod is toast." ) ;
139+ return ;
140+ }
141+
142+ // Create an ImportExtension instance with reflection
143+ // Constructor args: (string ext, bool autoImport)
144+ var importExt = System . Activator . CreateInstance ( importExtType ,
145+ new object [ ] { extension , true } ) ;
146+
147+ // Get the associatedExtensions field via reflection
148+ var extensionsField = assHelperType . GetField ( "associatedExtensions" ,
149+ System . Reflection . BindingFlags . NonPublic | System . Reflection . BindingFlags . Static ) ;
150+
151+ if ( extensionsField == null )
152+ {
153+ Error ( "Could not find associatedExtensions field" ) ;
154+ return ;
155+ }
156+
157+ // Get the dictionary and add our extension to the Special asset class
158+ var extensions = extensionsField . GetValue ( null ) ;
159+ var dictType = extensions . GetType ( ) ;
160+ var specialValue = dictType . GetMethod ( "get_Item" ) . Invoke ( extensions , new object [ ] { AssetClass . Special } ) ;
161+
162+ if ( specialValue == null )
163+ {
164+ Error ( "Couldn't get Special asset class list" ) ;
165+ return ;
166+ }
167+
168+ // Add our ImportExtension to the list
169+ specialValue . GetType ( ) . GetMethod ( "Add" ) . Invoke ( specialValue , new [ ] { importExt } ) ;
170+
171+ Debug ( "{extension} import extension added successfully" ) ;
172+ }
173+ catch ( System . Exception ex )
174+ {
175+ Error ( $ "Failed to add { extension } to special import formats: { ex } ") ;
176+ }
129177 }
130178
131179 private static async Task Scanfiles ( List < string > hasUnityPackage , Slot slot , World world )
0 commit comments