@@ -528,10 +528,10 @@ private void EmitCpblk(MethodDefinition method, ILProcessor gen)
528528 //gen.Emit(OpCodes.Bne_Un_S, cpblk);
529529 gen . Emit ( OpCodes . Unaligned , ( byte ) 1 ) ; // unaligned to 1
530530 gen . Append ( cpblk ) ;
531-
531+
532532 }
533533
534- private List < string > GetSharpDXAttributes ( MethodDefinition method )
534+ private List < string > GetSharpDXAttributes ( MethodDefinition method )
535535 {
536536 var attributes = new List < string > ( ) ;
537537 foreach ( var customAttribute in method . CustomAttributes )
@@ -550,10 +550,8 @@ private List<string> GetSharpDXAttributes(MethodDefinition method)
550550 /// Patches the method.
551551 /// </summary>
552552 /// <param name="method">The method.</param>
553- bool PatchMethod ( MethodDefinition method )
553+ void PatchMethod ( MethodDefinition method )
554554 {
555- bool isSharpJit = false ;
556-
557555 var attributes = this . GetSharpDXAttributes ( method ) ;
558556 if ( attributes . Contains ( "SharpDX.ModuleInit" ) )
559557 {
@@ -605,87 +603,60 @@ bool PatchMethod(MethodDefinition method)
605603 previousInstruction = instruction ;
606604 instruction = instructions [ i ] ;
607605
608- if ( instruction . OpCode == OpCodes . Call && instruction . Operand is MethodReference )
606+ if ( instruction . OpCode == OpCodes . Call
607+ && instruction . Operand is MethodReference )
609608 {
610609 var methodDescription = ( MethodReference ) instruction . Operand ;
611610
612- if ( methodDescription is MethodDefinition )
613- {
614- foreach ( var customAttribute in ( ( MethodDefinition ) methodDescription ) . CustomAttributes )
615- {
616- if ( customAttribute . AttributeType . FullName == typeof ( ObfuscationAttribute ) . FullName )
617- {
618- foreach ( var arg in customAttribute . Properties )
619- {
620- if ( arg . Name == "Feature" && arg . Argument . Value != null )
621- {
622- var customValue = arg . Argument . Value . ToString ( ) ;
623- if ( customValue . StartsWith ( "SharpJit." ) )
624- {
625- isSharpJit = true ;
626- break ;
627- }
628- }
629- }
630- }
631- if ( isSharpJit ) break ;
632- }
633- }
634611
635- if ( ! isSharpJit )
612+ if ( methodDescription . Name . StartsWith ( "Calli" ) && methodDescription . DeclaringType . Name == "LocalInterop" )
636613 {
637- if ( methodDescription . Name . StartsWith ( "Calli" ) && methodDescription . DeclaringType . Name == "LocalInterop" )
614+ var callSite = new CallSite ( methodDescription . ReturnType ) { CallingConvention = MethodCallingConvention . StdCall } ;
615+ // Last parameter is the function ptr, so we don't add it as a parameter for calli
616+ // as it is already an implicit parameter for calli
617+ for ( int j = 0 ; j < methodDescription . Parameters . Count - 1 ; j ++ )
638618 {
639- var callSite = new CallSite ( methodDescription . ReturnType ) { CallingConvention = MethodCallingConvention . StdCall } ;
640- // Last parameter is the function ptr, so we don't add it as a parameter for calli
641- // as it is already an implicit parameter for calli
642- for ( int j = 0 ; j < methodDescription . Parameters . Count - 1 ; j ++ )
643- {
644- var parameterDefinition = methodDescription . Parameters [ j ] ;
645- callSite . Parameters . Add ( parameterDefinition ) ;
646- }
619+ var parameterDefinition = methodDescription . Parameters [ j ] ;
620+ callSite . Parameters . Add ( parameterDefinition ) ;
621+ }
647622
648- // Create calli Instruction
649- var callIInstruction = ilProcessor . Create ( OpCodes . Calli , callSite ) ;
623+ // Create calli Instruction
624+ var callIInstruction = ilProcessor . Create ( OpCodes . Calli , callSite ) ;
650625
651- // Replace instruction
652- ilProcessor . Replace ( instruction , callIInstruction ) ;
653- }
654- else if ( methodDescription . DeclaringType . Name == "Interop" )
626+ // Replace instruction
627+ ilProcessor . Replace ( instruction , callIInstruction ) ;
628+ }
629+ else if ( methodDescription . DeclaringType . Name == "Interop" )
630+ {
631+ if ( methodDescription . FullName . Contains ( "Fixed" ) )
655632 {
656- if ( methodDescription . FullName . Contains ( "Fixed" ) )
633+ if ( methodDescription . Parameters [ 0 ] . ParameterType . IsArray )
657634 {
658- if ( methodDescription . Parameters [ 0 ] . ParameterType . IsArray )
659- {
660- ReplaceFixedArrayStatement ( method , ilProcessor , instruction ) ;
661- }
662- else
663- {
664- ReplaceFixedStatement ( method , ilProcessor , instruction ) ;
665- }
635+ ReplaceFixedArrayStatement ( method , ilProcessor , instruction ) ;
666636 }
667- else if ( methodDescription . Name . StartsWith ( "ReadInline" ) )
637+ else
668638 {
669- this . ReplaceReadInline ( method , ilProcessor , instruction ) ;
670- }
671- else if ( methodDescription . Name . StartsWith ( "CopyInline" ) || methodDescription . Name . StartsWith ( "WriteInline" ) )
672- {
673- this . ReplaceCopyInline ( method , ilProcessor , instruction ) ;
674- }
675- else if ( methodDescription . Name . StartsWith ( "SizeOf" ) )
676- {
677- this . ReplaceSizeOfStructGeneric ( method , ilProcessor , instruction ) ;
639+ ReplaceFixedStatement ( method , ilProcessor , instruction ) ;
678640 }
679641 }
642+ else if ( methodDescription . Name . StartsWith ( "ReadInline" ) )
643+ {
644+ this . ReplaceReadInline ( method , ilProcessor , instruction ) ;
645+ }
646+ else if ( methodDescription . Name . StartsWith ( "CopyInline" ) || methodDescription . Name . StartsWith ( "WriteInline" ) )
647+ {
648+ this . ReplaceCopyInline ( method , ilProcessor , instruction ) ;
649+ }
650+ else if ( methodDescription . Name . StartsWith ( "SizeOf" ) )
651+ {
652+ this . ReplaceSizeOfStructGeneric ( method , ilProcessor , instruction ) ;
653+ }
680654 }
681655 }
682656 }
683657 }
684- return isSharpJit ;
685658 }
686659
687- bool containsSharpJit ;
688-
689660 /// <summary>
690661 /// Patches the type.
691662 /// </summary>
@@ -694,11 +665,10 @@ void PatchType(TypeDefinition type)
694665 {
695666 // Patch methods
696667 foreach ( var method in type . Methods )
697- if ( PatchMethod ( method ) )
698- containsSharpJit = true ;
668+ PatchMethod ( method ) ;
699669
700670 // LocalInterop will be removed after the patch only for non SharpJit code
701- if ( ! containsSharpJit && type . Name == "LocalInterop" )
671+ if ( type . Name == "LocalInterop" )
702672 classToRemoveList . Add ( type ) ;
703673
704674 // Patch nested types
@@ -776,7 +746,7 @@ public bool PatchFile(string file)
776746 // Query the target framework in order to resolve correct assemblies and type forwarding
777747 var targetFrameworkAttr = assembly . CustomAttributes . FirstOrDefault (
778748 attribute => attribute . Constructor . FullName . Contains ( "System.Runtime.Versioning.TargetFrameworkAttribute" ) ) ;
779- if ( targetFrameworkAttr != null && targetFrameworkAttr . ConstructorArguments . Count > 0 &&
749+ if ( targetFrameworkAttr != null && targetFrameworkAttr . ConstructorArguments . Count > 0 &&
780750 targetFrameworkAttr . ConstructorArguments [ 0 ] . Value != null )
781751 {
782752 var targetFramework = new FrameworkName ( targetFrameworkAttr . ConstructorArguments [ 0 ] . Value . ToString ( ) ) ;
@@ -785,7 +755,7 @@ public bool PatchFile(string file)
785755 targetFramework . Identifier ,
786756 targetFramework . Version ) ;
787757 netcoreAssemblyPath = Path . Combine ( ProgramFilesx86 ( ) , netcoreAssemblyPath ) ;
788- if ( Directory . Exists ( netcoreAssemblyPath ) )
758+ if ( Directory . Exists ( netcoreAssemblyPath ) )
789759 {
790760 resolver . AddSearchDirectory ( netcoreAssemblyPath ) ;
791761 }
@@ -794,7 +764,7 @@ public bool PatchFile(string file)
794764 // Import void* and int32
795765 voidType = assembly . MainModule . TypeSystem . Void . Resolve ( ) ;
796766 voidPointerType = new PointerType ( assembly . MainModule . Import ( voidType ) ) ;
797- intType = assembly . MainModule . Import ( assembly . MainModule . TypeSystem . Int32 . Resolve ( ) ) ;
767+ intType = assembly . MainModule . Import ( assembly . MainModule . TypeSystem . Int32 . Resolve ( ) ) ;
798768
799769 // Remove CompilationRelaxationsAttribute
800770 for ( int i = 0 ; i < assembly . CustomAttributes . Count ; i ++ )
@@ -822,7 +792,7 @@ public bool PatchFile(string file)
822792 // Update Check file
823793 fileTime . UpdateCheckFile ( checkFile ) ;
824794 //fileTimeInteropBuilder.UpdateCheckFile(checkInteropBuilderFile);
825-
795+
826796 Log ( "SharpDX patch done for assembly [{0}]" , file ) ;
827797 return true ;
828798 }
@@ -844,7 +814,7 @@ static void Main(string[] args)
844814 string file = args [ 0 ] ;
845815 var program = new InteropApp ( ) ;
846816 program . PatchFile ( file ) ;
847- }
817+ }
848818 catch ( Exception ex )
849819 {
850820 Console . WriteLine ( ex ) ;
@@ -896,7 +866,7 @@ public void UpdateCheckFile(string checkFile)
896866 /// <returns>true if the file exist and has the same LastWriteTime </returns>
897867 public bool CheckFileUpToDate ( string checkfile )
898868 {
899- return File . Exists ( checkfile ) && File . GetLastWriteTime ( checkfile ) == LastWriteTime ;
869+ return File . Exists ( checkfile ) && File . GetLastWriteTime ( checkfile ) == LastWriteTime ;
900870 }
901871
902872 public void UpdateFile ( string file )
0 commit comments