/// <summary> /// /// </summary> /// <param name="Build"></param> public void InitFromScript(ScriptLaunchMode Build) { ScriptInstance = Build; Name = Build.Name; foreach (PropertyInfo Info in Build.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (Info.PropertyType != typeof(string) && Info.PropertyType != typeof(int) && Info.PropertyType != typeof(float) && Info.PropertyType != typeof(bool)) { continue; } BuildLaunchVariable Arg = new BuildLaunchVariable(); Arg.InitFromScript(Build, Info); Variables.Add(Arg); } }
/// <summary> /// Evaluates all conditions and returns a list of launch modes that are valid for execution. /// </summary> /// <returns>List of launch modes valid for execution.</returns> public List <BuildLaunchMode> Compile() { List <BuildLaunchMode> Result = new List <BuildLaunchMode>(); // If we are defined by a script, compile it. if (ScriptSource.Length > 0) { try { ScriptOptions Options = ScriptOptions.Default .WithImports("System", "System.Text.RegularExpressions", "System.IO", "System.Math", "System.ComponentModel", "System.ComponentModel.DataAnnotations", "BuildSync.Core.Scripting", "BuildSync.Core.Utils") .WithReferences("System", "System.Data", "System.Windows.Forms", typeof(ScriptBuild).Assembly.Location) .WithAllowUnsafe(false) .WithOptimizationLevel(OptimizationLevel.Debug); Script <object> script = CSharpScript.Create(ScriptSource, Options); script.Compile(); using (var Stream = new MemoryStream()) { var EmitResult = script.GetCompilation().Emit(Stream); if (EmitResult.Success) { Stream.Seek(0, SeekOrigin.Begin); Assembly LoadedAssembly = Assembly.Load(Stream.ToArray()); foreach (Type Type in LoadedAssembly.GetTypes()) { if (typeof(ScriptLaunchMode).IsAssignableFrom(Type)) { ScriptLaunchMode LaunchMode = Activator.CreateInstance(Type) as ScriptLaunchMode; if (LaunchMode.IsAvailable) { BuildLaunchMode mode = new BuildLaunchMode(); mode.InitFromScript(LaunchMode); Result.Add(mode); } } } } else { string Errors = string.Join("\n", EmitResult.Diagnostics.Where(diagnostic => diagnostic.IsWarningAsError || diagnostic.Severity == DiagnosticSeverity.Error).Select(d => $"{d.Id}: {d.GetMessage()}")); Console.WriteLine("Failed to compile build settings script file."); Console.WriteLine(Errors); throw new InvalidOperationException("Failed to compile script: " + Errors); } } } catch (CompilationErrorException e) { string Errors = string.Join(Environment.NewLine, e.Diagnostics); Console.WriteLine("Failed to compile build settings script file."); Console.WriteLine(Errors); throw new InvalidOperationException("Failed to compile build settings script file with errors:\n\n" + Errors); } } else { foreach (BuildLaunchMode Mode in Modes) { if (Evaluate(Mode.Condition, null)) { BuildLaunchMode NewMode = Mode.DeepClone(); foreach (BuildLaunchVariable Var in NewMode.Variables) { Var.ConditionResult = Evaluate(Var.Condition, null); } Result.Add(NewMode); } } } return(Result); }
/// <summary> /// /// </summary> public void InitFromScript(ScriptLaunchMode Build, PropertyInfo PropInfo) { Internal = false; Condition = "true"; ConditionResult = true; ScriptProperty = PropInfo; Name = PropInfo.Name; FriendlyCategory = PropInfo.GetCustomAttribute <CategoryAttribute>()?.Category; FriendlyDescription = PropInfo.GetCustomAttribute <DescriptionAttribute>()?.Description; FriendlyName = PropInfo.GetCustomAttribute <DisplayNameAttribute>()?.DisplayName; if (PropInfo.PropertyType == typeof(string)) { Value = PropInfo.GetValue(Build) as string; DataType = BuildLaunchVariableDataType.String; } else if (PropInfo.PropertyType == typeof(int)) { Value = ((int)PropInfo.GetValue(Build)).ToString(); DataType = BuildLaunchVariableDataType.Int; } else if (PropInfo.PropertyType == typeof(float)) { Value = ((float)PropInfo.GetValue(Build)).ToString(); DataType = BuildLaunchVariableDataType.Float; } else if (PropInfo.PropertyType == typeof(bool)) { Value = ((bool)PropInfo.GetValue(Build)).ToString(); DataType = BuildLaunchVariableDataType.Bool; } RangeAttribute Range = PropInfo.GetCustomAttribute <RangeAttribute>(); if (Range != null) { Type MaxType = Range.Maximum.GetType(); Type MinType = Range.Minimum.GetType(); if (MaxType == typeof(int)) { MaxValue = (int)Range.Maximum; } if (MaxType == typeof(float)) { MaxValue = (float)Range.Maximum; } if (MaxType == typeof(double)) { MaxValue = (float)((double)Range.Maximum); } if (MinType == typeof(int)) { MinValue = (int)Range.Minimum; } if (MinType == typeof(float)) { MinValue = (float)Range.Minimum; } if (MinType == typeof(double)) { MinValue = (float)((double)Range.Minimum); } } OptionsAttribute Ops = PropInfo.GetCustomAttribute <OptionsAttribute>(); if (Ops != null) { foreach (string Val in Ops.Values) { Options.Add(Val); } } }