private ReorderableList GetReordableList(bool AsGenerator) { string Identifier = AsGenerator ? "Generator" : "PostProcessor"; var _List = new ReorderableList(serializedObject, serializedObject.FindProperty($"{Identifier}DefinitionList"), true, true, true, true); _List.drawElementCallback = (UnityEngine.Rect rect, int index, bool isActive, bool isFocused) => { var element = _List.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; GeneratorDefinition _Generator = element.objectReferenceValue as System.Object as GeneratorDefinition; PostProcessorDefinition _PostProcessor = element.objectReferenceValue as System.Object as PostProcessorDefinition; if (_Generator != null) { _Generator.Enabled = EditorGUI.Toggle(new UnityEngine.Rect(rect.x, rect.y, 20, EditorGUIUtility.singleLineHeight), _Generator.Enabled); } else if (_PostProcessor != null) { _PostProcessor.Enabled = EditorGUI.Toggle(new UnityEngine.Rect(rect.x, rect.y, 20, EditorGUIUtility.singleLineHeight), _PostProcessor.Enabled); } EditorGUI.ObjectField(new UnityEngine.Rect(rect.x + 20, rect.y, rect.width - 20, EditorGUIUtility.singleLineHeight), element, UnityEngine.GUIContent.none); }; _List.drawHeaderCallback = (UnityEngine.Rect rect) => { EditorGUI.LabelField(rect, $"{Identifier} Definition"); EditorGUI.LabelField(new UnityEngine.Rect(rect.width - 90, rect.y, rect.width, EditorGUIUtility.singleLineHeight), "Override Order"); if (AsGenerator) { _ProjectUnitDefinition.OverrideGeneratorOrder = EditorGUI.Toggle(new UnityEngine.Rect(rect.width + 6.45f, rect.y, 20, EditorGUIUtility.singleLineHeight), _ProjectUnitDefinition.OverrideGeneratorOrder); } else { _ProjectUnitDefinition.OverridePostProcessorOrder = EditorGUI.Toggle(new UnityEngine.Rect(rect.width + 6.45f, rect.y, 20, EditorGUIUtility.singleLineHeight), _ProjectUnitDefinition.OverridePostProcessorOrder); } }; return(_List); }
/// <summary> /// Parse a text file and build a configuration instance /// </summary> /// <returns>The parsed instance.</returns> /// <param name="path">The path to the file to read.</param> public static CLIServerConfiguration ParseTextFile(string path) { var cfg = new CLIServerConfiguration(); var line = string.Empty; var re = new System.Text.RegularExpressions.Regex(@"(?<comment>\#.*)|(\""(?<value>[^\""]*)\"")|((?<value>[^ ]*))"); var s1names = BuildPropertyMap <CLIServerConfiguration>(); var s2names = BuildPropertyMap <ServerConfig>(); var lineindex = 0; Dictionary <string, string> lastitemprops = null; using (var fs = File.OpenRead(path)) using (var sr = new StreamReader(fs, System.Text.Encoding.UTF8, true)) while ((line = sr.ReadLine()) != null) { var args = re.Matches(line) .Cast <System.Text.RegularExpressions.Match>() .Where(x => x.Groups["value"].Success && !string.IsNullOrWhiteSpace(x.Value)) .Select(x => x.Groups["value"].Value) .ToArray(); lineindex++; if (args.Length == 0) { continue; } var cmd = args.First(); if (s1names.ContainsKey(cmd)) { if (args.Length > 2) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } SetProperty(cfg, s1names[cmd], args.Skip(1).FirstOrDefault()); } else if (s2names.ContainsKey(cmd)) { if (args.Length > 2) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } cfg.ServerOptions[cmd] = args.Skip(1).FirstOrDefault(); } else if (string.Equals(cmd, "route", StringComparison.OrdinalIgnoreCase)) { if (args.Length == 2) { var route = new RouteDefinition() { Assembly = args.Skip(1).First() }; cfg.Routes.Add(route); lastitemprops = route.Options; } else { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } var route = new RouteDefinition() { Assembly = args.Skip(1).First(), Classname = args.Skip(2).First(), ConstructorArguments = args.Skip(3).ToList() }; cfg.Routes.Add(route); lastitemprops = route.Options; } } else if (string.Equals(cmd, "handler", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } var route = new RouteDefinition() { RoutePrefix = args.Skip(1).First(), Classname = args.Skip(2).First(), ConstructorArguments = args.Skip(3).ToList() }; cfg.Routes.Add(route); lastitemprops = route.Options; } else if (string.Equals(cmd, "module", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 2) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } var module = new ModuleDefinition() { Classname = args.Skip(1).First(), ConstructorArguments = args.Skip(2).ToList() }; cfg.Modules.Add(module); lastitemprops = module.Options; } else if (string.Equals(cmd, "postprocessor", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 2) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } var postprocessor = new PostProcessorDefinition() { Classname = args.Skip(1).First(), ConstructorArguments = args.Skip(2).ToList() }; cfg.PostProcessors.Add(postprocessor); lastitemprops = postprocessor.Options; } else if (string.Equals(cmd, "logger", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 2) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } var logger = new LoggerDefinition() { Classname = args.Skip(1).First(), ConstructorArguments = args.Skip(2).ToList() }; cfg.Loggers.Add(logger); lastitemprops = logger.Options; } else if (string.Equals(cmd, "set", StringComparison.OrdinalIgnoreCase)) { if (lastitemprops == null) { throw new Exception($"There was no active entry to set properties for in line {lineindex}"); } if (args.Length > 3) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } if (args.Length < 2) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } lastitemprops[args.Skip(1).First()] = args.Skip(2).FirstOrDefault(); } else if (string.Equals(cmd, "serve", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } if (args.Length > 3) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } var routearg = args.Skip(1).First(); if (routearg == string.Empty) { routearg = "/"; } if (!routearg.StartsWith("/", StringComparison.Ordinal)) { throw new Exception($"The route must start with a forward slash in line {lineindex}: {line}"); } while (routearg.Length > 1 && routearg.EndsWith("/", StringComparison.Ordinal)) { routearg = routearg.Substring(0, routearg.Length - 1); } var pathprefix = routearg; routearg = routearg == "/" ? "[/.*]" : $"[{routearg}(/.*)?]"; var route = new RouteDefinition() { RoutePrefix = routearg, Classname = typeof(Ceen.Httpd.Handler.FileHandler).AssemblyQualifiedName, ConstructorArguments = args.Skip(2).ToList() }; cfg.Routes.Add(route); lastitemprops = route.Options; lastitemprops.Add(nameof(Ceen.Httpd.Handler.FileHandler.PathPrefix), pathprefix); } else if (string.Equals(cmd, "redirect", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } if (args.Length > 3) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } var routearg = args.Skip(1).First(); var route = new RouteDefinition() { RoutePrefix = routearg, Classname = typeof(Ceen.Httpd.Handler.RedirectHandler).AssemblyQualifiedName, ConstructorArguments = args.Skip(2).ToList() }; cfg.Routes.Add(route); lastitemprops = route.Options; } else if (string.Equals(cmd, "mime", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } if (args.Length > 3) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } var key = args.Skip(1).First(); if (!string.IsNullOrEmpty(key) && key != "*" && !key.StartsWith(".", StringComparison.Ordinal)) { key = "." + key; } cfg.MimeTypes[key] = args.Skip(2).First(); } else if (string.Equals(cmd, "header", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 3) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } if (args.Length > 3) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } cfg.DefaultHeaders[args.Skip(1).First()] = args.Skip(2).First(); } else if (string.Equals(cmd, "index", StringComparison.OrdinalIgnoreCase)) { if (args.Length < 2) { throw new Exception($"Too few arguments in line {lineindex}: {line}"); } if (args.Length > 2) { throw new Exception($"Too many arguments in line {lineindex}: {line}"); } cfg.IndexDocuments.Add(args.Skip(1).First()); } else { throw new Exception($"Unable to parse the action \"{cmd}\" for line {lineindex}: \"{line}\""); } } cfg.Basepath = Path.GetFullPath(cfg.Basepath ?? "."); cfg.Assemblypath = string.Join(Path.PathSeparator.ToString(), (cfg.Assemblypath ?? "").Split(new char[] { Path.PathSeparator }).Select(x => Path.GetFullPath(Path.Combine(cfg.Basepath, ExpandEnvironmentVariables(x))))); return(cfg); }