Advertisement
Guest User

DLLHelper.cs

a guest
Jun 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.23 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Reflection;
  7. using System.IO;
  8. using System.IO.Compression;
  9.  
  10. namespace StoryEngine
  11. {
  12.   public static class DLLHelper
  13.   {
  14.     public static bool IsTypeInstantiable( Type T )
  15.     {
  16.       return ( T.IsPublic == true && T.IsInterface == false && T.IsAbstract == false );
  17.     }
  18.  
  19.     public static Assembly LoadAssemblyFromZipEntry( ZipArchiveEntry Entry )
  20.     {
  21.       return LoadAssemblyFromStream( Entry.Open() );
  22.     }
  23.  
  24.     public static Assembly LoadAssemblyFromStream( Stream AssemblyStream )
  25.     {
  26.       try
  27.       {
  28.         byte[] DLLBytes = new BinaryReader( AssemblyStream ).ReadBytes( ( int ) AssemblyStream.Length );
  29.  
  30.         return Assembly.ReflectionOnlyLoad( DLLBytes );
  31.       }
  32.       catch ( Exception e )
  33.       {
  34.         Logger.WriteException( e );
  35.  
  36.         return null;
  37.       }
  38.     }
  39.  
  40.     public static bool DoesAssemblyContainItemScripts( Assembly ToCheck )
  41.     {
  42.       try
  43.       {
  44.         bool ItemFound = false;
  45.  
  46.         Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
  47.         {
  48.           if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) == true )
  49.           {
  50.             ItemFound = true;
  51.           }
  52.         } );
  53.  
  54.         return ItemFound;
  55.       }
  56.       catch ( Exception e )
  57.       {
  58.         Logger.WriteException( e );
  59.  
  60.         return false;
  61.       }
  62.     }
  63.  
  64.     public static bool DoesAssemblyContainSituationScripts( Assembly ToCheck )
  65.     {
  66.       try
  67.       {
  68.         bool SituationFound = false;
  69.  
  70.         Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
  71.         {
  72.           if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) == true )
  73.           {
  74.             SituationFound = true;
  75.           }
  76.         } );
  77.  
  78.         return SituationFound;
  79.       }
  80.       catch ( Exception e )
  81.       {
  82.         Logger.WriteException( e );
  83.  
  84.         return false;
  85.       }
  86.     }
  87.  
  88.     public static bool DoesAssemblyContainSolutionScripts( Assembly ToCheck )
  89.     {
  90.       try
  91.       {
  92.         bool SolutionFound = false;
  93.  
  94.         Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
  95.           {
  96.             if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) == true )
  97.             {
  98.  
  99.               SolutionFound = true;
  100.             }
  101.           } );
  102.          
  103.           return SolutionFound;
  104.       }
  105.       catch ( Exception e )
  106.       {
  107.         Logger.WriteException( e );
  108.  
  109.         return false;
  110.       }
  111.     }
  112.  
  113.     public static bool DoesAssemblyContainChapterTransitionScripts( Assembly ToCheck )
  114.     {
  115.       try
  116.       {
  117.         bool ChTransitionFound = false;
  118.  
  119.         Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
  120.         {
  121.           if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) == true )
  122.           {
  123.             ChTransitionFound = true;
  124.           }
  125.         } );
  126.  
  127.         return ChTransitionFound;
  128.       }
  129.       catch ( Exception e )
  130.       {
  131.         Logger.WriteException( e );
  132.  
  133.         return false;
  134.       }
  135.     }
  136.  
  137.     public static Dictionary<string, SituationBase> LoadSituationsFromAssembly( Assembly LoadFrom )
  138.     {
  139.       try
  140.       {
  141.         Dictionary<string, SituationBase> Situations = new Dictionary<string, SituationBase>();
  142.  
  143.         if ( DoesAssemblyContainSituationScripts( LoadFrom ) == false )
  144.         {
  145.           return null;
  146.         }
  147.         else
  148.         {
  149.           Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
  150.           {
  151.             if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) )
  152.             {
  153.               SituationBase Situation = ( SituationBase ) Activator.CreateInstance( ExportedType );
  154.  
  155.               Situations.Add( Situation.Name(), Situation );
  156.             }
  157.           } );
  158.  
  159.           return Situations;
  160.         }
  161.       }
  162.       catch ( Exception e )
  163.       {
  164.         Logger.WriteException( e );
  165.  
  166.         return null;
  167.       }
  168.     }
  169.  
  170.     public static Dictionary<string, SolutionBase> LoadSolutionsFromAssembly( Assembly LoadFrom )
  171.     {
  172.       try
  173.       {
  174.         Dictionary<string, SolutionBase> Solutions = new Dictionary<string, SolutionBase>();
  175.  
  176.         if ( DoesAssemblyContainSolutionScripts( LoadFrom ) == false )
  177.         {
  178.           return null;
  179.         }
  180.         else
  181.         {
  182.           Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
  183.           {
  184.             if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) )
  185.             {
  186.               SolutionBase Solution = ( SolutionBase ) Activator.CreateInstance( ExportedType );
  187.  
  188.               Solutions.Add( Solution.Name(), Solution );
  189.             }
  190.           } );
  191.  
  192.           return Solutions;
  193.         }
  194.       }
  195.       catch ( Exception e )
  196.       {
  197.         Logger.WriteException( e );
  198.  
  199.         return null;
  200.       }
  201.     }
  202.  
  203.     public static Dictionary<string, ItemBase> LoadItemsFromAssembly( Assembly LoadFrom )
  204.     {
  205.       try
  206.       {
  207.         Dictionary<string, ItemBase> Items = new Dictionary<string, ItemBase>();
  208.  
  209.         if ( DoesAssemblyContainItemScripts( LoadFrom ) == false )
  210.         {
  211.           return null;
  212.         }
  213.         else
  214.         {
  215.           Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
  216.           {
  217.             if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) )
  218.             {
  219.               ItemBase Item = ( ItemBase ) Activator.CreateInstance( ExportedType );
  220.  
  221.               Items.Add( Item.Name(), Item );
  222.             }
  223.           } );
  224.  
  225.           return Items;
  226.         }
  227.       }
  228.       catch ( Exception e )
  229.       {
  230.         Logger.WriteException( e );
  231.  
  232.         return null;
  233.       }
  234.     }
  235.  
  236.     public static Dictionary<string, ChapterTransitionBase> LoadChTransitionsFromAssembly( Assembly LoadFrom )
  237.     {
  238.       try
  239.       {
  240.         Dictionary<string, ChapterTransitionBase> Transitions = new Dictionary<string, ChapterTransitionBase>();
  241.  
  242.         if ( DoesAssemblyContainChapterTransitionScripts( LoadFrom ) == false )
  243.         {
  244.           return null;
  245.         }
  246.         else
  247.         {
  248.           Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
  249.           {
  250.             if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) )
  251.             {
  252.               ChapterTransitionBase Transition = ( ChapterTransitionBase ) Activator.CreateInstance( ExportedType );
  253.  
  254.               Transitions.Add( Transition.Name(), Transition );
  255.             }
  256.           } );
  257.  
  258.           return Transitions;
  259.         }
  260.       }
  261.       catch ( Exception e )
  262.       {
  263.         Logger.WriteException( e );
  264.  
  265.         return null;
  266.       }
  267.     }
  268.  
  269.     public static Dictionary<string, SituationBase> LoadSituationsFromZipEntry( ZipArchiveEntry Entry )
  270.     {
  271.       try
  272.       {
  273.         Dictionary<string, SituationBase> Situations = new Dictionary<string, SituationBase>();
  274.         Assembly                          DLL        = LoadAssemblyFromZipEntry( Entry );
  275.  
  276.         if ( DoesAssemblyContainSituationScripts( DLL ) == false )
  277.         {
  278.           return null;
  279.         }
  280.         else
  281.         {
  282.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  283.           {
  284.             if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) == true )
  285.             {
  286.               SituationBase Situation = ( SituationBase ) Activator.CreateInstance( ExportedType );
  287.  
  288.               Situations.Add( Situation.Name(), Situation );
  289.             }
  290.           } );
  291.         }
  292.  
  293.         return Situations;
  294.       }
  295.       catch ( Exception e )
  296.       {
  297.         Logger.WriteException( e );
  298.  
  299.         return null;
  300.       }
  301.     }
  302.  
  303.     public static Dictionary<string, SolutionBase> LoadSolutionsFromZipEntry( ZipArchiveEntry Entry )
  304.     {
  305.       try
  306.       {
  307.         Dictionary<string, SolutionBase> Solutions = new Dictionary<string, SolutionBase>();
  308.         Assembly                         DLL       = LoadAssemblyFromZipEntry( Entry );
  309.  
  310.         if ( DoesAssemblyContainSolutionScripts( DLL ) == false )
  311.         {
  312.           return null;
  313.         }
  314.         else
  315.         {
  316.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  317.           {
  318.             if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) == true )
  319.             {
  320.               SolutionBase Solution = ( SolutionBase ) Activator.CreateInstance( ExportedType );
  321.  
  322.               Solutions.Add( Solution.Name(), Solution );
  323.             }
  324.           } );
  325.         }
  326.  
  327.         return Solutions;
  328.       }
  329.       catch ( Exception e )
  330.       {
  331.         Logger.WriteException( e );
  332.  
  333.         return null;
  334.       }
  335.     }
  336.  
  337.     public static Dictionary<string, ItemBase> LoadItemsFromZipEntry( ZipArchiveEntry Entry )
  338.     {
  339.       try
  340.       {
  341.         Dictionary<string, ItemBase> Items = new Dictionary<string, ItemBase>();
  342.         Assembly                     DLL   = LoadAssemblyFromZipEntry( Entry );
  343.  
  344.         if ( DoesAssemblyContainItemScripts( DLL ) == false )
  345.         {
  346.           return null;
  347.         }
  348.         else
  349.         {
  350.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  351.           {
  352.             if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) == true )
  353.             {
  354.               ItemBase Item = ( ItemBase ) Activator.CreateInstance( ExportedType );
  355.  
  356.               Items.Add( Item.Name(), Item );
  357.             }
  358.           } );
  359.         }
  360.  
  361.         return Items;
  362.       }
  363.       catch ( Exception e )
  364.       {
  365.         Logger.WriteException( e );
  366.  
  367.         return null;
  368.       }
  369.     }
  370.  
  371.     public static Dictionary<string, ChapterTransitionBase> LoadChTransitionsFromZipEntry( ZipArchiveEntry Entry )
  372.     {
  373.       try
  374.       {
  375.         Dictionary<string, ChapterTransitionBase> ChTransitions = new Dictionary<string, ChapterTransitionBase>();
  376.         Assembly                                  DLL           = LoadAssemblyFromZipEntry( Entry );
  377.  
  378.         if ( DoesAssemblyContainChapterTransitionScripts( DLL ) == false )
  379.         {
  380.           return null;
  381.         }
  382.         else
  383.         {
  384.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  385.           {
  386.             if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) == true )
  387.             {
  388.               ChapterTransitionBase Transition = ( ChapterTransitionBase ) Activator.CreateInstance( ExportedType );
  389.  
  390.               ChTransitions.Add( Transition.Name(), Transition );
  391.             }
  392.           } );
  393.         }
  394.  
  395.         return ChTransitions;
  396.       }
  397.       catch ( Exception e )
  398.       {
  399.         Logger.WriteException( e );
  400.  
  401.         return null;
  402.       }
  403.     }
  404.  
  405.     public static List<T> LoadAllExportedSubTypesFromZipEntry<T>( ZipArchiveEntry Entry )
  406.     {
  407.       try
  408.       {
  409.         if ( IsTypeInstantiable( typeof( T ) ) == false )
  410.         {
  411.           return null;
  412.         }
  413.         else
  414.         {
  415.           List<T>  Types = new List<T>();
  416.           Assembly DLL   = LoadAssemblyFromZipEntry( Entry );
  417.  
  418.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  419.           {
  420.             if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
  421.             {
  422.               Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
  423.             }
  424.           } );
  425.  
  426.           return Types;
  427.         }
  428.       }
  429.       catch ( Exception e )
  430.       {
  431.         Logger.WriteException( e );
  432.  
  433.         return null;
  434.       }
  435.     }
  436.  
  437.     public static List<T> LoadAllExportedSubTypesFromAssembly<T>( Assembly LoadFrom )
  438.     {
  439.       try
  440.       {
  441.         if ( IsTypeInstantiable( typeof( T ) ) == false )
  442.         {
  443.           return null;
  444.         }
  445.         else
  446.         {
  447.           List<T> Types = new List<T>();
  448.  
  449.           Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
  450.           {
  451.             if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
  452.             {
  453.               Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
  454.             }
  455.           } );
  456.  
  457.           return Types;
  458.         }
  459.       }
  460.       catch ( Exception e )
  461.       {
  462.         Logger.WriteException( e );
  463.  
  464.         return null;
  465.       }
  466.     }
  467.  
  468.     public static List<T> LoadAllExportedSubTypesFromStream<T>( Stream LoadFrom )
  469.     {
  470.       try
  471.       {
  472.         if ( IsTypeInstantiable( typeof( T ) ) == false )
  473.         {
  474.           return null;
  475.         }
  476.         else
  477.         {
  478.           List<T>  Types = new List<T>();
  479.           Assembly DLL   = LoadAssemblyFromStream( LoadFrom );
  480.  
  481.           Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
  482.           {
  483.             if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
  484.             {
  485.               Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
  486.             }
  487.           } );
  488.  
  489.           return Types;
  490.         }
  491.       }
  492.       catch ( Exception e )
  493.       {
  494.         Logger.WriteException( e );
  495.  
  496.         return null;
  497.       }
  498.     }
  499.   }
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement