Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Reflection;
- using System.IO;
- using System.IO.Compression;
- namespace StoryEngine
- {
- public static class DLLHelper
- {
- public static bool IsTypeInstantiable( Type T )
- {
- return ( T.IsPublic == true && T.IsInterface == false && T.IsAbstract == false );
- }
- public static Assembly LoadAssemblyFromZipEntry( ZipArchiveEntry Entry )
- {
- return LoadAssemblyFromStream( Entry.Open() );
- }
- public static Assembly LoadAssemblyFromStream( Stream AssemblyStream )
- {
- try
- {
- byte[] DLLBytes = new BinaryReader( AssemblyStream ).ReadBytes( ( int ) AssemblyStream.Length );
- return Assembly.ReflectionOnlyLoad( DLLBytes );
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static bool DoesAssemblyContainItemScripts( Assembly ToCheck )
- {
- try
- {
- bool ItemFound = false;
- Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- ItemFound = true;
- }
- } );
- return ItemFound;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return false;
- }
- }
- public static bool DoesAssemblyContainSituationScripts( Assembly ToCheck )
- {
- try
- {
- bool SituationFound = false;
- Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- SituationFound = true;
- }
- } );
- return SituationFound;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return false;
- }
- }
- public static bool DoesAssemblyContainSolutionScripts( Assembly ToCheck )
- {
- try
- {
- bool SolutionFound = false;
- Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- SolutionFound = true;
- }
- } );
- return SolutionFound;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return false;
- }
- }
- public static bool DoesAssemblyContainChapterTransitionScripts( Assembly ToCheck )
- {
- try
- {
- bool ChTransitionFound = false;
- Parallel.ForEach( ToCheck.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- ChTransitionFound = true;
- }
- } );
- return ChTransitionFound;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return false;
- }
- }
- public static Dictionary<string, SituationBase> LoadSituationsFromAssembly( Assembly LoadFrom )
- {
- try
- {
- Dictionary<string, SituationBase> Situations = new Dictionary<string, SituationBase>();
- if ( DoesAssemblyContainSituationScripts( LoadFrom ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) )
- {
- SituationBase Situation = ( SituationBase ) Activator.CreateInstance( ExportedType );
- Situations.Add( Situation.Name(), Situation );
- }
- } );
- return Situations;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, SolutionBase> LoadSolutionsFromAssembly( Assembly LoadFrom )
- {
- try
- {
- Dictionary<string, SolutionBase> Solutions = new Dictionary<string, SolutionBase>();
- if ( DoesAssemblyContainSolutionScripts( LoadFrom ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) )
- {
- SolutionBase Solution = ( SolutionBase ) Activator.CreateInstance( ExportedType );
- Solutions.Add( Solution.Name(), Solution );
- }
- } );
- return Solutions;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, ItemBase> LoadItemsFromAssembly( Assembly LoadFrom )
- {
- try
- {
- Dictionary<string, ItemBase> Items = new Dictionary<string, ItemBase>();
- if ( DoesAssemblyContainItemScripts( LoadFrom ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) )
- {
- ItemBase Item = ( ItemBase ) Activator.CreateInstance( ExportedType );
- Items.Add( Item.Name(), Item );
- }
- } );
- return Items;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, ChapterTransitionBase> LoadChTransitionsFromAssembly( Assembly LoadFrom )
- {
- try
- {
- Dictionary<string, ChapterTransitionBase> Transitions = new Dictionary<string, ChapterTransitionBase>();
- if ( DoesAssemblyContainChapterTransitionScripts( LoadFrom ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) )
- {
- ChapterTransitionBase Transition = ( ChapterTransitionBase ) Activator.CreateInstance( ExportedType );
- Transitions.Add( Transition.Name(), Transition );
- }
- } );
- return Transitions;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, SituationBase> LoadSituationsFromZipEntry( ZipArchiveEntry Entry )
- {
- try
- {
- Dictionary<string, SituationBase> Situations = new Dictionary<string, SituationBase>();
- Assembly DLL = LoadAssemblyFromZipEntry( Entry );
- if ( DoesAssemblyContainSituationScripts( DLL ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SituationBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- SituationBase Situation = ( SituationBase ) Activator.CreateInstance( ExportedType );
- Situations.Add( Situation.Name(), Situation );
- }
- } );
- }
- return Situations;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, SolutionBase> LoadSolutionsFromZipEntry( ZipArchiveEntry Entry )
- {
- try
- {
- Dictionary<string, SolutionBase> Solutions = new Dictionary<string, SolutionBase>();
- Assembly DLL = LoadAssemblyFromZipEntry( Entry );
- if ( DoesAssemblyContainSolutionScripts( DLL ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( SolutionBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- SolutionBase Solution = ( SolutionBase ) Activator.CreateInstance( ExportedType );
- Solutions.Add( Solution.Name(), Solution );
- }
- } );
- }
- return Solutions;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, ItemBase> LoadItemsFromZipEntry( ZipArchiveEntry Entry )
- {
- try
- {
- Dictionary<string, ItemBase> Items = new Dictionary<string, ItemBase>();
- Assembly DLL = LoadAssemblyFromZipEntry( Entry );
- if ( DoesAssemblyContainItemScripts( DLL ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ItemBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- ItemBase Item = ( ItemBase ) Activator.CreateInstance( ExportedType );
- Items.Add( Item.Name(), Item );
- }
- } );
- }
- return Items;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static Dictionary<string, ChapterTransitionBase> LoadChTransitionsFromZipEntry( ZipArchiveEntry Entry )
- {
- try
- {
- Dictionary<string, ChapterTransitionBase> ChTransitions = new Dictionary<string, ChapterTransitionBase>();
- Assembly DLL = LoadAssemblyFromZipEntry( Entry );
- if ( DoesAssemblyContainChapterTransitionScripts( DLL ) == false )
- {
- return null;
- }
- else
- {
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( ChapterTransitionBase ) && IsTypeInstantiable( ExportedType ) == true )
- {
- ChapterTransitionBase Transition = ( ChapterTransitionBase ) Activator.CreateInstance( ExportedType );
- ChTransitions.Add( Transition.Name(), Transition );
- }
- } );
- }
- return ChTransitions;
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static List<T> LoadAllExportedSubTypesFromZipEntry<T>( ZipArchiveEntry Entry )
- {
- try
- {
- if ( IsTypeInstantiable( typeof( T ) ) == false )
- {
- return null;
- }
- else
- {
- List<T> Types = new List<T>();
- Assembly DLL = LoadAssemblyFromZipEntry( Entry );
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
- {
- Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
- }
- } );
- return Types;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static List<T> LoadAllExportedSubTypesFromAssembly<T>( Assembly LoadFrom )
- {
- try
- {
- if ( IsTypeInstantiable( typeof( T ) ) == false )
- {
- return null;
- }
- else
- {
- List<T> Types = new List<T>();
- Parallel.ForEach( LoadFrom.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
- {
- Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
- }
- } );
- return Types;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- public static List<T> LoadAllExportedSubTypesFromStream<T>( Stream LoadFrom )
- {
- try
- {
- if ( IsTypeInstantiable( typeof( T ) ) == false )
- {
- return null;
- }
- else
- {
- List<T> Types = new List<T>();
- Assembly DLL = LoadAssemblyFromStream( LoadFrom );
- Parallel.ForEach( DLL.GetTypes(), ( ExportedType ) =>
- {
- if ( ExportedType.BaseType == typeof( T ) && IsTypeInstantiable( ExportedType ) == true )
- {
- Types.Add( ( T ) Activator.CreateInstance( ExportedType ) );
- }
- } );
- return Types;
- }
- }
- catch ( Exception e )
- {
- Logger.WriteException( e );
- return null;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement