private static List <Serialize.Category> GetHierarchy(Serialize.Category category) { if (category == Root) { return(new List <Serialize.Category>()); } var result = new List <Serialize.Category>() { category }; var parent = category.Parent; while (parent is not null) { if (parent == Root) { break; } result.Add(parent); parent = parent.Parent; } result.Reverse(); return(result); }
private static void Set(Queue <string> arguments) { if (arguments.Count == 0) { Debug.Warning("Nothing passed to set"); return; } var chainString = arguments.Dequeue(); if (arguments.Count == 0) { Debug.Warning("No value passed to set"); return; } var value = arguments.Dequeue(); var chain = chainString.Split('.'); Serialize.Category category = Root; FieldInfo? field = null; foreach (var arg in chain) { var key = arg.ToLowerInvariant(); if (category.Children.TryGetValue(key, out var subCategory)) { category = subCategory; } else if (category.Fields.TryGetValue(key, out field)) { if (arguments.Count != 0) { Debug.Warning($"Unknown Category: {string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{arg} (found matching field)"); return; } } else { Debug.Warning($"Unknown Field or Category: {string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{arg}"); return; } } if (field is null) { Debug.Warning($"Field not found: {chainString}"); return; } SetValue(field, value); }
private static void Query(Queue <string> arguments) { if (arguments.Count == 0) { DumpCategory(Root); return; } var chainString = arguments.Dequeue(); var chain = chainString.Split('.'); Serialize.Category category = Root; foreach (var arg in chain) { var key = arg.ToLowerInvariant(); if (category.Children.TryGetValue(key, out var subCategory)) { category = subCategory; } else if (category.Fields.TryGetValue(key, out var field)) { if (arguments.Count != 0) { Debug.Warning($"Unknown Category: {string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{key} (found matching field)"); return; } var fieldValue = field.GetValue(null); if (field.FieldType.IsEnum) { var validNames = string.Join(", ", Enum.GetNames(field.FieldType)); Debug.Info($"{string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{field.Name} = '{fieldValue}' ({field.FieldType.Name}: {validNames})"); } else { Debug.Info($"{string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{field.Name} = '{fieldValue}' ({field.FieldType.Name})"); } return; } else { Debug.Warning($"Unknown Category: {string.Join('.', GetHierarchy(category).Select(cat => cat.Name))}.{key}"); return; } } DumpCategory(category); }
private static void DumpCategory(Serialize.Category category) { var output = new StringBuilder(); if (category.Children.Count != 0) { output.AppendLine("Categories:"); int maxCategoryLength = int.MinValue; foreach (var subCategory in category.Children) { maxCategoryLength = Math.Max(maxCategoryLength, subCategory.Value.Name.Length); } foreach (var subCategory in category.Children) { var comment = subCategory.Value.Type.GetCustomAttribute <Attributes.CommentAttribute>(); output.AppendLine( comment is null ? $"\t{subCategory.Value.Name}" : $"\t{subCategory.Value.Name.PadRight(maxCategoryLength)} : {comment.Message}" ); } if (category.Fields.Count != 0) { output.AppendLine(); } } if (category.Fields.Count != 0) { output.AppendLine("Fields:"); int maxFieldLength = int.MinValue; foreach (var field in category.Fields) { maxFieldLength = Math.Max(maxFieldLength, field.Value.Name.Length); } foreach (var field in category.Fields) { var comment = field.Value.GetCustomAttribute <Attributes.CommentAttribute>(); output.AppendLine( comment is null ? $"\t{field.Value.Name}" : $"\t{field.Value.Name.PadRight(maxFieldLength)} : {comment.Message}" ); } } Debug.Info(output.ToString()); }