Handles retrieval of required assemblies for compiled scripts etc.
Esempio n. 1
0
        Assembly CompileFromSource(CodeDomProvider provider, string searchPattern)
        {
            var compilerParameters = new CompilerParameters();

            compilerParameters.GenerateExecutable = false;

            // Necessary for stack trace line numbers etc
            compilerParameters.IncludeDebugInformation = true;
            compilerParameters.GenerateInMemory = false;

            #if RELEASE
            if(!compilationParameters.ForceDebugInformation)
            {
                compilerParameters.GenerateInMemory = true;
                compilerParameters.IncludeDebugInformation = false;
            }
            #endif

            if(!compilerParameters.GenerateInMemory)
            {
                var assemblyPath = Path.Combine(PathUtils.TempFolder, string.Format("CompiledScripts_{0}.dll", searchPattern.Replace("*.", "")));

                if(File.Exists(assemblyPath))
                {
                    try
                    {
                        File.Delete(assemblyPath);
                    }
                    catch(Exception ex)
                    {
                        if(ex is UnauthorizedAccessException || ex is IOException)
                            assemblyPath = Path.ChangeExtension(assemblyPath, "_" + Path.GetExtension(assemblyPath));
                        else
                            throw;
                    }
                }

                compilerParameters.OutputAssembly = assemblyPath;
            }

            var scripts = new List<string>();
            var scriptsDirectory = PathUtils.ScriptsFolder;

            if(Directory.Exists(scriptsDirectory))
            {
                foreach (var script in Directory.GetFiles(scriptsDirectory, searchPattern, SearchOption.AllDirectories))
                    scripts.Add(script);
            }
            else
                Debug.LogAlways("Scripts directory could not be located");

            CompilerResults results;
            using(provider)
            {
                var referenceHandler = new AssemblyReferenceHandler();
                compilerParameters.ReferencedAssemblies.AddRange(referenceHandler.GetRequiredAssembliesFromFiles(scripts));

                results = provider.CompileAssemblyFromFile(compilerParameters, scripts.ToArray());
            }

            return ScriptCompiler.ValidateCompilation(results);
        }
Esempio n. 2
0
        Assembly CompileFromSource(CodeDomProvider provider, string searchPattern)
        {
            var compilerParameters = new CompilerParameters();

            compilerParameters.GenerateExecutable = false;

            // Necessary for stack trace line numbers etc
            compilerParameters.IncludeDebugInformation = true;
            compilerParameters.GenerateInMemory = false;

            #if RELEASE
            if(!compilationParameters.ForceDebugInformation)
            {
                compilerParameters.GenerateInMemory = true;
                compilerParameters.IncludeDebugInformation = false;
            }
            #endif

            if (!compilerParameters.GenerateInMemory)
            {
                var assemblyPath = Path.Combine(ProjectSettings.TempDirectory, string.Format("CompiledScripts_{0}.dll", searchPattern.Replace("*.", "")));

                if (File.Exists(assemblyPath))
                {
                    try
                    {
                        File.Delete(assemblyPath);
                    }
                    catch (Exception ex)
                    {
                        if (ex is UnauthorizedAccessException || ex is IOException)
                        {
                            int num = 1;
                            var split = assemblyPath.Split(new string[] { ".dll" }, StringSplitOptions.None);

                            while (File.Exists(assemblyPath))
                            {
                                assemblyPath = split.First() + num.ToString() + ".dll";
                                num++;
                            }
                        }
                        else
                            throw;
                    }
                }

                compilerParameters.OutputAssembly = assemblyPath;
            }

            var scripts = new List<string>();
            var scriptsDirectory = CryPak.ScriptsFolder;

            CVar cvar;
            if (CVar.TryGet("mono_scriptDirectory", out cvar))
            {
                var alternateScriptsDir = cvar.String;
                if (!string.IsNullOrEmpty(alternateScriptsDir))
                    scriptsDirectory = alternateScriptsDir;
            }

            if (Directory.Exists(scriptsDirectory))
            {
                foreach (var script in Directory.GetFiles(scriptsDirectory, searchPattern, SearchOption.AllDirectories))
                    scripts.Add(script);
            }
            else
                Debug.LogAlways("Scripts directory could not be located");

            if (scripts.Count == 0)
                return null;

            CompilerResults results;
            using (provider)
            {
                var referenceHandler = new AssemblyReferenceHandler();
                compilerParameters.ReferencedAssemblies.AddRange(referenceHandler.GetRequiredAssembliesFromFiles(scripts));

                results = provider.CompileAssemblyFromFile(compilerParameters, scripts.ToArray());
            }

            return ValidateCompilation(results);
        }