/// <summary>Reverses the precompilation of a particular module. Restores the source files back into the Assets folder.</summary>
        public static void Reverse(string moduleName)
        {
            // Get the settings:
            Module settings = GetModule(moduleName);

            if (settings == null)
            {
                return;
            }

            // Get all the source the files:
            SourceFileSet src = settings.GetSourceFiles();

            // Copy them back to being in Assets again:
            if (!src.CopyTo("Assets"))
            {
                return;
            }

            // Delete the compiled Dll:
            settings.DeleteDll();

            // Back up the precompiled module:
            settings.Backup();

            // Inform the asset DB:
            AssetDatabase.Refresh();
        }
示例#2
0
        /// <summary>Recompiles this module.</summary>
        public void Compile()
        {
            // Clear exists:
            DllExists_ = -1;

            // Write out the settings:
            WriteSettings();

            // Get the file set:
            SourceFileSet files = GetFileSet();

            // Rename the files, adding a "preco" extension:
            files.Rename(true);

            if (Precompiler.Build(this))
            {
                Debug.Log("Precompiled " + Name);
            }
            else
            {
                // Revert the files:
                files.Rename(false);
            }

            // Inform the asset DB:
            AssetDatabase.Refresh();
        }
示例#3
0
        public SourceFileSet GetSourceFiles()
        {
            string path = SourcePath;

            // Copy All files back to their original locations. We'll use SourceFileSet again for this.
            SourceFileSet set = new SourceFileSet(path);

            // Add all the source files:
            set.Add(path);

            return(set);
        }
        /// <summary>Creates a precompile module for the given source paths. It's then precompiled, optionally in editor mode.</summary>
        /// <param name="paths">Paths to folders containing source code.</param>
        /// <param name="moduleName">A name which represents this group of files. This can be used to reverse the precompilation, or recompile.</param>
        /// <param name="editor">True if it should be precompiled in editor mode (with UNITY_EDITOR).</param>
        public static void Precompile(List <string> paths, string moduleName, bool editor)
        {
            // The target folder where we'll place all the source etc:
            string path = Path + "/" + moduleName;

            // Create the folder:
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                Directory.CreateDirectory(path + "/Source");
            }

            // Create the source file set:
            SourceFileSet files = new SourceFileSet("Assets");

            // Ignore/include "Editor" folders based on editor setting:
            if (!editor)
            {
                files.Ignore("Editor");
            }

            // Ignore SVN folders:
            files.Ignore(".svn");

            // Could ignore platform specific folders here too!

            // Collect all source files now:
            foreach (string sourcePath in paths)
            {
                files.Add(sourcePath);
            }

            // Copy the files out of the Assets folder into one where the source will no longer get compiled by Unity.
            if (!files.CopyTo(path + "/Source"))
            {
                return;
            }

            // Write some settings:
            File.WriteAllText(path + "/Settings.conf", "Editor=" + editor + ";");

            if (Build(editor, moduleName, files))
            {
                // Delete all the source files from the unity asset folder(s):
                files.Delete();

                Debug.Log("Precompiled " + moduleName);

                // Inform the asset DB:
                AssetDatabase.Refresh();
            }
        }
        /// <summary>Builds the given set of files into the given module using standard compiler args.</summary>
        public static bool Build(bool editor, string moduleName, SourceFileSet files)
        {
            string target = Path + "/" + moduleName + "/" + moduleName + ".dll";

            // Get the defines:
            string defines = GetFlags(editor);

            // Get a provider:
            CSharpCodeProvider compiler = new CSharpCodeProvider();

            CompilerParameters parameters = new CompilerParameters();

            parameters.GenerateInMemory   = false;
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly     = target;
            parameters.CompilerOptions    = defines + " /target:library";

            // Reference Unity:
            AddUnityDllPaths(editor, parameters.ReferencedAssemblies);

            // Build now:
            CompilerResults results = compiler.CompileAssemblyFromSource(parameters, files.ToSourceArray());

            if (results.Errors.Count > 0)
            {
                // We had errors! May just be warnings though, so let's check:

                foreach (CompilerError ce in results.Errors)
                {
                    if (ce.IsWarning)
                    {
                        Debug.LogWarning(ce.ToString());
                    }
                    else
                    {
                        Debug.LogError(ce.ToString());
                    }
                }

                if (!File.Exists(target))
                {
                    Debug.LogError("Error precompiling " + target + ".");
                    return(false);
                }
            }

            // Copy the DLL:
            File.Copy(target, "Assets/" + moduleName + ".dll", true);

            return(true);
        }
示例#6
0
        /// <summary>The full set of source files in this module.</summary>
        public SourceFileSet GetFileSet()
        {
            // Create the source file set:
            SourceFileSet files = new SourceFileSet();

            // Ignore "Editor" folders:
            files.Ignore("Editor");

            // Ignore SVN folders:
            files.Ignore(".svn");

            // Collect all source files now:
            foreach (string sourceDir in SourceFolders)
            {
                files.Add(sourceDir);
            }

            return(files);
        }
示例#7
0
        /// <summary>Reverts the precompilation of this module.</summary>
        public void Revert()
        {
            // Clear exists:
            DllExists_ = -1;

            // Delete the .dll:
            if (Precompiled)
            {
                File.Delete(DllPath);
            }

            // Go through the source files and rename any which are ".preco". Do that like so:
            SourceFileSet files = GetFileSet();

            // Rename:
            files.Rename(false);

            // Inform the asset DB:
            AssetDatabase.Refresh();
        }
        /// <summary>Re-compiles the given module, optionally with UNITY_EDITOR defined. Must have already been precompiled. You can find if it has been with GetModule.</summary>
        public static void Recompile(string moduleName, bool editorMode)
        {
            // Get the settings:
            Module settings = GetModule(moduleName);

            if (settings == null)
            {
                return;
            }

            // Get all the source the files:
            SourceFileSet src = settings.GetSourceFiles();

            // Build now:
            if (Build(editorMode, moduleName, src))
            {
                Debug.Log("Recompile of " + moduleName + " was a success!");

                // Inform the asset DB:
                AssetDatabase.Refresh();
            }
        }
示例#9
0
//--------------------------------------
		/// <summary>Builds the given set of files into the given module using standard compiler args.</summary>
		public static bool Build(bool editor,string moduleName,SourceFileSet files){
			
			string target=Path+"/"+moduleName+"/"+moduleName+".dll";
			
			// Get the defines:
			string defines=GetFlags(editor);
			
			// Get a provider:
			CSharpCodeProvider compiler=new CSharpCodeProvider();
			
			CompilerParameters parameters=new CompilerParameters();
			parameters.GenerateInMemory=false;
			parameters.GenerateExecutable=true;
			parameters.OutputAssembly=target;
			parameters.CompilerOptions=defines+" /target:library";
			
			// Reference Unity:
			AddUnityDllPaths(editor,parameters.ReferencedAssemblies);
			
			// Build now:
			CompilerResults results = compiler.CompileAssemblyFromSource(parameters,files.ToSourceArray());
			
			if(results.Errors.Count>0){
				// We had errors! May just be warnings though, so let's check:
				
				foreach(CompilerError ce in results.Errors){
					if(ce.IsWarning){
						Debug.LogWarning(ce.ToString());
					}else{
						Debug.LogError(ce.ToString());
					}
				}
				
				if(!File.Exists(target)){
					
					Debug.LogError("Error precompiling "+target+".");
					return false;
					
				}
				
			}
			
			// Copy the DLL:
			File.Copy(target,"Assets/"+moduleName+".dll",true);
			
			return true;
			
		}
		/// <summary>Creates a precompile module for the given source paths. It's then precompiled, optionally in editor mode.</summary>
		/// <param name="paths">Paths to folders containing source code.</param>
		/// <param name="moduleName">A name which represents this group of files. This can be used to reverse the precompilation, or recompile.</param>
		/// <param name="editor">True if it should be precompiled in editor mode (with UNITY_EDITOR).</param>
		public static void Precompile(List<string> paths,string moduleName,bool editor){
			
			// The target folder where we'll place all the source etc:
			string path=Path+"/"+moduleName;
			
			// Create the folder:
			if(!Directory.Exists(path)){
				Directory.CreateDirectory(path);
				Directory.CreateDirectory(path+"/Source");
			}
			
			// Create the source file set:
			SourceFileSet files=new SourceFileSet("Assets");
			
			// Ignore/include "Editor" folders based on editor setting:
			if(!editor){
				files.Ignore("Editor");
			}
			
			// Ignore SVN folders:
			files.Ignore(".svn");
			
			// Could ignore platform specific folders here too!
			
			// Collect all source files now:
			foreach(string sourcePath in paths){
				files.Add(sourcePath);
			}
			
			// Copy the files out of the Assets folder into one where the source will no longer get compiled by Unity.
			if(!files.CopyTo(path+"/Source")){
				return;
			}
			
			// Write some settings:
			File.WriteAllText(path+"/Settings.conf","Editor="+editor+";");
			
			if(Build(editor,moduleName,files)){
				
				// Delete all the source files from the unity asset folder(s):
				files.Delete();
				
				Debug.Log("Precompiled "+moduleName);
				
				// Inform the asset DB:
				AssetDatabase.Refresh();
				
			}
			
		}