/// <summary> /// Causes the Roslyn C# settings to be loaded and applied to the Roslyn compiler service. /// This requires the compiler service to be initialized otherwise it will do nothing. /// </summary> public void ApplyCompilerServiceSettings() { // Check for no compiler if (sharedCompiler == null) { return; } // Load the settings RoslynCSharp settings = RoslynCSharp.Settings; // Setup compiler values sharedCompiler.AllowUnsafe = settings.AllowUnsafeCode; sharedCompiler.AllowOptimize = settings.AllowOptimizeCode; sharedCompiler.AllowConcurrentCompile = settings.AllowConcurrentCompile; sharedCompiler.GenerateInMemory = settings.GenerateInMemory; sharedCompiler.GenerateSymbols = settings.GenerateSymbols; // NOT SUPPORTED ON MONO sharedCompiler.WarningLevel = settings.WarningLevel; sharedCompiler.LanguageVersion = settings.LanguageVersion; sharedCompiler.TargetPlatform = settings.TargetPlatform; // Setup reference paths sharedCompiler.ReferenceAssemblies.Clear(); foreach (string reference in settings.References) { sharedCompiler.ReferenceAssemblies.Add(AssemblyReference.FromNameOrFile(reference)); } // Setup define symbols sharedCompiler.DefineSymbols.Clear(); foreach (string define in settings.DefineSymbols) { sharedCompiler.DefineSymbols.Add(define); } }
/// <summary> /// Save the specified settings to a project asset. /// The asset will be saved to its loaded location or if this is the frist time creating the asset it will be placed in the folder 'Assets/Resources/'. /// Editor only method. /// </summary> /// <param name="settings">The settings to save</param> public static void SaveAsset(RoslynCSharp settings) { // Check for null settings if (settings == null) { Debug.LogWarning("Failed to save settings because they have been destroyed"); return; } if (AssetDatabase.Contains(settings) == true) { // Mark as changed EditorUtility.SetDirty(settings); AssetDatabase.SaveAssets(); } else { // Create the folder first if (AssetDatabase.IsValidFolder("Assets/Resources") == false) { AssetDatabase.CreateFolder("Assets", "Resources"); } // Create the asset AssetDatabase.CreateAsset(settings, "Assets/Resources/" + settingsName + ".asset"); } }
private ScriptAssembly RegisterAssembly(Assembly assembly, ScriptSecurityMode securityMode, CodeSecurityEngine securityEngine, bool isRuntimeCompiled, CompilationResult compileResult = null) { // Check for error if (assembly == null) { return(null); } // Reset report securityResult = null; // Create script assembly ScriptAssembly scriptAssembly = new ScriptAssembly(this, assembly, securityEngine, compileResult); // Check for ensure security mode bool performSecurityCheck = (securityMode == ScriptSecurityMode.EnsureSecurity); // Get value from settings if (securityMode == ScriptSecurityMode.UseSettings) { performSecurityCheck = RoslynCSharp.Settings.SecurityCheckCode; } // Check for security checks if (performSecurityCheck == true) { // Perform code validation if (scriptAssembly.SecurityCheckAssembly(RoslynCSharp.Settings.SecurityRestrictions, out securityResult) == false) { // Log the error RoslynCSharp.LogError(securityResult.GetSummaryText()); RoslynCSharp.LogError(securityResult.GetAllText(true)); // Dont load the assembly return(null); } else { RoslynCSharp.Log(securityResult.GetSummaryText()); } } // Mark as runtime compiled if (isRuntimeCompiled == true) { scriptAssembly.MarkAsRuntimeCompiled(); } lock (this) { // Register with domain this.loadedAssemblies.Add(scriptAssembly); } // Return result return(scriptAssembly); }
// Methods /// <summary> /// Load the settings from the resources folder. /// </summary> public static void LoadResources() { // Try to load from resources settings = Resources.Load <RoslynCSharp>(settingsName); // Check for error if (settings == null) { settings = CreateInstance <RoslynCSharp>(); Debug.LogWarningFormat("Failed to load settings asset '{0}' from resources. Default values will be used", settingsName); } }
public ScriptAssembly RegisterAssembly(ScriptAssembly scriptAssembly, ScriptSecurityMode securityMode) { // Check for error if (scriptAssembly == null) { return(null); } // Reset report securityResult = null; // Check for ensure security mode bool performSecurityCheck = (securityMode == ScriptSecurityMode.EnsureSecurity); // Get value from settings if (securityMode == ScriptSecurityMode.UseSettings) { performSecurityCheck = RoslynCSharp.Settings.SecurityCheckCode; } // Check for security checks if (performSecurityCheck == true) { CodeSecurityRestrictions restrictions = RoslynCSharp.Settings.SecurityRestrictions; // Use pinvoke option restrictions.AllowPInvoke = RoslynCSharp.Settings.AllowPInvoke; // Perform code validation if (scriptAssembly.SecurityCheckAssembly(restrictions, out securityResult) == false) { // Log the error RoslynCSharp.LogError(securityResult.GetSummaryText()); RoslynCSharp.LogError(securityResult.GetAllText(true)); // Dont load the assembly return(null); } else { RoslynCSharp.Log(securityResult.GetSummaryText()); } } lock (this) { // Register with domain this.loadedAssemblies.Add(scriptAssembly); } // Return result return(scriptAssembly); }
/// <summary> /// Log the last output of the Roslyn compiler to the Unity console. /// </summary> public void LogCompilerOutputToConsole() { // Check for no report if (compileResult == null) { return; } bool loggedHeader = false; // Simple function to only output the header when one or more errors, warnings or infos will be logged Action logHeader = () => { if (loggedHeader == false) { RoslynCSharp.Log("__Roslyn Compile Output__"); loggedHeader = true; } }; // Process report foreach (CompilationError error in compileResult.Errors) { if (error.IsError == true) { // Log as error logHeader(); RoslynCSharp.LogError(error.ToString()); } else if (error.IsWarning == true) { // Log as warning logHeader(); RoslynCSharp.LogWarning(error.ToString()); } else if (error.IsInfo == true) { logHeader(); RoslynCSharp.Log(error.ToString()); } } }
/// <summary> /// Creates a new <see cref="ScriptDomain"/> into which assemblies and scripts may be loaded. /// </summary> /// <returns>A new instance of <see cref="ScriptDomain"/></returns> public static ScriptDomain CreateDomain(string domainName, bool initCompiler = true, bool makeActiveDomain = true, AppDomain sandboxDomain = null) { // Create a new named domain ScriptDomain domain = new ScriptDomain(domainName, sandboxDomain); // Load the roslyn settings - do this now because the next load request could be from a worker thread RoslynCSharp.LoadResources(); // Check for compiler if (initCompiler == true) { domain.InitializeCompilerService(); } // Make domain active if (makeActiveDomain == true) { MakeDomainActive(domain); } return(domain); }