Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System.Linq;
- using System;
- using System.Reflection;
- using System.Collections.Generic;
- using System.IO;
- using System.Diagnostics;
- using Object = UnityEngine.Object;
- using Debug = UnityEngine.Debug;
- public abstract class Compiler
- {
- private const string BooCompilerName = "BooCompiler";
- private const string JSCompilerName = "UnityScriptCompiler";
- private const string CSCompilerName = "MonoCSharpCompiler";
- public BuildTarget BuildTarget { get; set; }
- public string Profile { get; set; }
- public string[] SourceFiles { get; set; }
- public string[] References { get; set; }
- public string[] Defines { get; set; }
- public string Output { get; set; }
- protected abstract Type CompilerType { get; }
- public void Compile()
- {
- Defines = Defines ?? new string[0];
- Profile = Profile ?? "2.0";
- var island = Activator.CreateInstance(Cache.MonoIslandType, BuildTarget, Profile, SourceFiles, References, Defines, Output);
- var compiler = Activator.CreateInstance(CompilerType, island);
- var w = Stopwatch.StartNew();
- var program = Cache.StartCompilerMethod.Invoke(compiler, null);
- w.Stop();
- PrintCompiledFiles();
- Debug.Log("Time took to compile: " + w.ElapsedMilliseconds + " ms");
- var errors = Cache.GetErrorOutputMethod.Invoke(program, null);
- var std = Cache.GetStdOutputMethod.Invoke(program, null);
- // Sometimes in JS compilation errors are not displayed in the error output, but instead in the GetAllOutput
- // so you might want to uncomment the next line, or even put some debug points and see for your self when things are not compiling
- // var all = program.GetType().GetMethod("GetAllOutput", BindingFlags.Instance | BindingFlags.Public).Invoke(program, null);
- // Debug.Log(all);
- Foreach((errors as IEnumerable<string>), Debug.LogError);
- Foreach((std as IEnumerable<string>), Debug.Log);
- // A lot of the times this doesn't really do what it should so you have to lose focus from Unity, and focus again...
- EditorApplication.delayCall += () =>
- {
- AssetDatabase.ImportAsset(Output);
- ProjectWindowUtil.ShowCreatedAsset(AssetDatabase.LoadAssetAtPath(Output, typeof(Object)));
- AssetDatabase.Refresh();
- };
- }
- private void PrintCompiledFiles()
- {
- Debug.Log("Compiled the following files:");
- string files = "";
- Foreach(SourceFiles, f => files += (f + ", "));
- files = files.Remove(files.LastIndexOf(','));
- Debug.Log(files);
- }
- public static T Create<T>(BuildTarget target, string profile, string[] sourceFiles, string[] references, string[] defines, string output) where T : Compiler, new()
- {
- return new T() { BuildTarget = target, Profile = profile, SourceFiles = sourceFiles, References = references, Defines = defines, Output = output };
- }
- public static T Create<T>(BuildTarget target, string[] sourceFiles, string[] references, string[] defines, string output) where T : Compiler, new()
- {
- return Create<T>(target, "2.0", sourceFiles, references, defines, output);
- }
- public static T Create<T>(BuildTarget target, string[] sourceFiles, string[] references, string output) where T : Compiler, new()
- {
- return Create<T>(target, sourceFiles, references, new string[0], output);
- }
- private static void Foreach<T>(IEnumerable<T> e, Action<T> act)
- {
- foreach (var item in e) act(item);
- }
- protected static class Cache
- {
- public static Assembly EditorAsm { get { return AssignIfNull(ref editorAsm, () => typeof(Editor).Assembly); } }
- public static Type[] EditorTypes { get { return AssignIfNull(ref editorTypes, () => EditorAsm.GetTypes()); } }
- public static Type MonoIslandType { get { return AssignIfNull(ref monoIslandType, () => EditorTypes.First(t => t.Name == "MonoIsland")); } }
- public static Type CsCompilerType { get { return AssignIfNull(ref csCompilerType, () => EditorTypes.First(t => t.Name == CSCompilerName)); } }
- public static Type JsCompilerType { get { return AssignIfNull(ref jsCompilerType, () => EditorTypes.First(t => t.Name == JSCompilerName)); } }
- public static Type BooCompilerType { get { return AssignIfNull(ref booCompilerType, () => EditorTypes.First(t => t.Name == BooCompilerName)); } }
- public static Type ProgramType { get { return AssignIfNull(ref programType, () => programType = EditorTypes.First(t => t.Name == "Program")); } }
- public static MethodInfo StartCompilerMethod { get { return AssignIfNull(ref startCompilerMethod, () => EditorTypes.First(t => t.Name == "ScriptCompilerBase").GetMethod("StartCompiler", BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null)); } }
- public static MethodInfo GetErrorOutputMethod { get { return AssignIfNull(ref getErrorOutputMethod, () => ProgramType.GetMethod("GetErrorOutput", BindingFlags.Instance | BindingFlags.Public)); } }
- public static MethodInfo GetStdOutputMethod { get { return AssignIfNull(ref getStdOutputMethod, () => ProgramType.GetMethod("GetStandardOutput", BindingFlags.Instance | BindingFlags.Public)); } }
- private static Assembly editorAsm;
- private static Type[] editorTypes;
- private static Type monoIslandType;
- private static Type csCompilerType;
- private static Type jsCompilerType;
- private static Type booCompilerType;
- private static Type programType;
- private static MethodInfo startCompilerMethod;
- private static MethodInfo getErrorOutputMethod;
- private static MethodInfo getStdOutputMethod;
- private static T AssignIfNull<T>(ref T value, Func<T> get)
- {
- if (value == null || value.Equals(null))
- value = get();
- return value;
- }
- }
- }
- public class CSCompiler : Compiler
- {
- protected override Type CompilerType
- {
- get { return Cache.CsCompilerType; }
- }
- }
- public class JSCompiler : Compiler
- {
- protected override Type CompilerType
- {
- get { return Cache.JsCompilerType; }
- }
- }
- public static class CompilerTest
- {
- public static string UnityEngineDllLocation
- {
- get { return typeof(GameObject).Assembly.Location; }
- }
- [MenuItem("CompilerTest/Compile CS")]
- public static void CompileCS()
- {
- var sources = Directory.GetFiles(@"Assets\CSs", "*.cs");
- var references = new string[]
- {
- UnityEngineDllLocation
- };
- var output = @"C:\Users\vexe\Desktop\Local DuneDashingDubai\Assets\csoutput.dll";
- Compiler.Create<CSCompiler>(BuildTarget.StandaloneWindows64, sources, references, output).Compile();
- }
- [MenuItem("CompilerTest/Compile JS")]
- public static void CompileJS()
- {
- var sources = Directory.GetFiles(@"Assets/JSs", "*.js", SearchOption.AllDirectories);
- var references = new string[]
- {
- UnityEngineDllLocation
- };
- var output = @"C:\Users\vexe\Desktop\Local DuneDashingDubai\Assets\jsoutput.dll";
- Compiler.Create<JSCompiler>(BuildTarget.StandaloneWindows64, sources, references, output).Compile();
- }
- [MenuItem("CompilerTest/Compile CS Seperate")]
- public static void CompileCSSeperate()
- {
- var sources = Directory.GetFiles(@"Assets\CSs", "*.cs");
- var references = new string[]
- {
- UnityEngineDllLocation
- };
- var output = @"C:\Users\vexe\Desktop\Local DuneDashingDubai\Assets\csoutput.dll";
- var compiler = new CSCompiler()
- {
- References = references,
- Output = output,
- BuildTarget = BuildTarget.StandaloneWindows64,
- };
- foreach (var file in sources)
- {
- compiler.SourceFiles = new string[] { file };
- compiler.Compile();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement