Exemplo n.º 1
0
        private static async Task <bool> TryBuildTask(string dllPath)
        {
            Assembly loadedAss = LoadAssembly(dllPath);

            IEnumerable <Type> potentialBehaviourTypes = null;

            //We assume this is a testity assembly
            //If someone called this builder then they WANT a Testity generated dll either way.
            try
            {
                //load the EngineScriptComponentTypes
                potentialBehaviourTypes = LoadBehaviourTypes(loadedAss);
            }
            catch (ReflectionTypeLoadException e)
            {
                Console.Write(e.Message + " LoaderExceptions:");
                foreach (var sube in e.LoaderExceptions)
                {
                    Console.WriteLine(sube.Message);
                }

                Console.ReadKey();
                return(false);
            }

            List <Task <ClassFile> > monobehaviourClassCompilationResults = new List <Task <ClassFile> >(potentialBehaviourTypes.Count());

            //Create the type mapper
            List <ITypeRelationalMapper> mappers = new List <ITypeRelationalMapper>();

            mappers.Add(new StringTypeRelationalMapper());
            mappers.Add(new EngineTypeRelationalMapper());
            mappers.Add(new PrimitiveTypeRelationalMapper(new UnityPrimitiveTypeExclusion()));
            mappers.Add(new ActionTypeRelationalMapper());
            mappers.Add(new DefaultTypeRelationalMapper());
            UnityBuildProcessTypeRelationalMapper chainMapper = new UnityBuildProcessTypeRelationalMapper(mappers);

            //create the InitializationExpressionBuilder
            List <IInitializationExpressionBuilderProvider> initProviders = new List <IInitializationExpressionBuilderProvider>();

            initProviders.Add(new ComponentAdapterInitializationExpressionBuilderProvider(new UnityComponentAdapterParser()));
            initProviders.Add(new SameTypeInitializationExpressionBuilderProvider());
            initProviders.Add(new DefaultInitializationExpressionBuilderProvider());

            ChainInitializationExpressionBuilderProvider chainInitProvider = new ChainInitializationExpressionBuilderProvider(initProviders);

            //Create the serializedmember type exclusion service. Indicates what types should be excluded when serializing types
            SerializedMemberStepTypeExclusionService excluder = new SerializedMemberStepTypeExclusionService()
                                                                .AddExclusionRules(new ActionDelegateTypeExclusion(new Type[] { typeof(Action <>), typeof(Action <,>), typeof(Action <, ,>), typeof(Action <, , ,>) }));

            //TestityEventTracking service. It tracks all used TestityEvent types
            TestityGenericEventTracker tracker = new TestityGenericEventTracker();

            //Create build steps
            List <ITestityBuildStep> buildSteps = new List <ITestityBuildStep>();

            buildSteps.Add(new AddTestityBehaviourBaseClassMemberStep());
            buildSteps.Add(new AddSerializedMemberStep(chainMapper, new SerializedMemberParser(), excluder));
            buildSteps.Add(new AddTestityEventSerializedDelegateStep(new ActionTypeRelationalMapper(), tracker, new SerializedMemberParser()));
            buildSteps.Add(new AddMemberInitializationMethodStep(chainMapper, new SerializedMemberParser(), chainInitProvider));

            //Handle all the behaviour types
            //These are not the only types that need handling
            foreach (Type t in potentialBehaviourTypes)
            {
                Task <ClassFile> classCompile = Task.Factory.StartNew(() => new ClassFile(GenerateMonobehaviourClass(chainMapper, buildSteps, t), t.Name + "Script"));
                monobehaviourClassCompilationResults.Add(classCompile);
            }

            string outputPath = dllPath.TrimEnd(".dll".ToCharArray()) + @"MonoBehaviours\";

            if (!Directory.CreateDirectory(outputPath).Exists)
            {
                throw new InvalidOperationException("Failed to create MonoBehaviour directory.");
            }

            //write out all the MonoBehaviours
            while (monobehaviourClassCompilationResults.Count != 0)
            {
                Task <ClassFile> newFile = await Task.WhenAny(monobehaviourClassCompilationResults);

                WriteMonobehaviourToFile(outputPath, newFile.Result);

                //We should check for new genericTestityEvents after a class has been generated
                //This isn't that slow since it will usually be empty
                //TODO: Change this so it buffers them into another collection.
                //We want to handle them seperately and write them to a seperate folder to avoid collisions
                foreach (var kvp in tracker.GetAdditionsAndClear())
                {
                    Console.WriteLine("New File: " + kvp.Key);
                    Console.ReadKey();

                    Task <ClassFile> classCompile = Task.Factory.StartNew(() => new ClassFile(GenerateTestityGenericEventSerializableClass(kvp.Key, kvp.Value), kvp.Key));
                    monobehaviourClassCompilationResults.Add(classCompile);
                }

                //Remove the file. If you don't we're stuck in the loop forever
                monobehaviourClassCompilationResults.Remove(newFile);
            }

            return(true);
        }
Exemplo n.º 2
0
		private static async Task<bool> TryBuildTask(string dllPath)
		{
			Assembly loadedAss = LoadAssembly(dllPath);

			IEnumerable<Type> potentialBehaviourTypes = null;
			//We assume this is a testity assembly
			//If someone called this builder then they WANT a Testity generated dll either way.
			try
			{
				//load the EngineScriptComponentTypes
				potentialBehaviourTypes = LoadBehaviourTypes(loadedAss);
			}
			catch (ReflectionTypeLoadException e)
			{
				Console.Write(e.Message + " LoaderExceptions:");
				foreach (var sube in e.LoaderExceptions)
					Console.WriteLine(sube.Message);

				Console.ReadKey();
				return false;
			}

			List<Task<ClassFile>> monobehaviourClassCompilationResults = new List<Task<ClassFile>>(potentialBehaviourTypes.Count());

			//Create the type mapper
			List<ITypeRelationalMapper> mappers = new List<ITypeRelationalMapper>();
			mappers.Add(new StringTypeRelationalMapper());
			mappers.Add(new EngineTypeRelationalMapper());
			mappers.Add(new PrimitiveTypeRelationalMapper(new UnityPrimitiveTypeExclusion()));
			mappers.Add(new ActionTypeRelationalMapper());
			mappers.Add(new DefaultTypeRelationalMapper());
			UnityBuildProcessTypeRelationalMapper chainMapper = new UnityBuildProcessTypeRelationalMapper(mappers);

			//create the InitializationExpressionBuilder
			List<IInitializationExpressionBuilderProvider> initProviders = new List<IInitializationExpressionBuilderProvider>();
			initProviders.Add(new ComponentAdapterInitializationExpressionBuilderProvider(new UnityComponentAdapterParser()));
			initProviders.Add(new SameTypeInitializationExpressionBuilderProvider());
			initProviders.Add(new DefaultInitializationExpressionBuilderProvider());

			ChainInitializationExpressionBuilderProvider chainInitProvider = new ChainInitializationExpressionBuilderProvider(initProviders);

			//Create the serializedmember type exclusion service. Indicates what types should be excluded when serializing types
			SerializedMemberStepTypeExclusionService excluder = new SerializedMemberStepTypeExclusionService()
				.AddExclusionRules(new ActionDelegateTypeExclusion(new Type[] { typeof(Action<>), typeof(Action<,>), typeof(Action<,,>), typeof(Action<,,,>) }));

			//TestityEventTracking service. It tracks all used TestityEvent types
			TestityGenericEventTracker tracker = new TestityGenericEventTracker();

			//Create build steps
			List<ITestityBuildStep> buildSteps = new List<ITestityBuildStep>();
			buildSteps.Add(new AddTestityBehaviourBaseClassMemberStep());
			buildSteps.Add(new AddSerializedMemberStep(chainMapper, new SerializedMemberParser(), excluder));
			buildSteps.Add(new AddTestityEventSerializedDelegateStep(new ActionTypeRelationalMapper(), tracker, new SerializedMemberParser()));
			buildSteps.Add(new AddMemberInitializationMethodStep(chainMapper, new SerializedMemberParser(), chainInitProvider));

			//Handle all the behaviour types
			//These are not the only types that need handling
			foreach (Type t in potentialBehaviourTypes)
			{
				Task<ClassFile> classCompile = Task.Factory.StartNew(() => new ClassFile(GenerateMonobehaviourClass(chainMapper, buildSteps, t), t.Name + "Script"));
				monobehaviourClassCompilationResults.Add(classCompile);
			}

			string outputPath = dllPath.TrimEnd(".dll".ToCharArray()) + @"MonoBehaviours\";

			if (!Directory.CreateDirectory(outputPath).Exists)
				throw new InvalidOperationException("Failed to create MonoBehaviour directory.");
			
			//write out all the MonoBehaviours
			while(monobehaviourClassCompilationResults.Count != 0)
			{
				Task<ClassFile> newFile = await Task.WhenAny(monobehaviourClassCompilationResults);

				WriteMonobehaviourToFile(outputPath, newFile.Result);

				//We should check for new genericTestityEvents after a class has been generated
				//This isn't that slow since it will usually be empty
				//TODO: Change this so it buffers them into another collection.
				//We want to handle them seperately and write them to a seperate folder to avoid collisions
				foreach (var kvp in tracker.GetAdditionsAndClear())
				{
					Console.WriteLine("New File: " + kvp.Key);
					Console.ReadKey();

					Task<ClassFile> classCompile = Task.Factory.StartNew(() => new ClassFile(GenerateTestityGenericEventSerializableClass(kvp.Key, kvp.Value), kvp.Key));
					monobehaviourClassCompilationResults.Add(classCompile);
				}

				//Remove the file. If you don't we're stuck in the loop forever
				monobehaviourClassCompilationResults.Remove(newFile);
			}

			return true;
        }