private ConstantWriter GetConstantWriter(string originalNamespace, string name) { string foundNamespace = originalNamespace; string newNamespace = this.LookupNamespaceForName(name); if (!string.IsNullOrEmpty(newNamespace)) { foundNamespace = newNamespace; } if (!this.namespacesToConstantWriters.TryGetValue(foundNamespace, out ConstantWriter constantWriter)) { string partConstantsFile = Path.Combine(this.scraperOutputDir, $@"{foundNamespace}.constants.cs"); if (File.Exists(partConstantsFile)) { File.Delete(partConstantsFile); } constantWriter = new ConstantWriter(partConstantsFile, foundNamespace, this.constantsHeaderText, this.withAttributes); this.namespacesToConstantWriters.Add(foundNamespace, constantWriter); } return(constantWriter); }
public bool AddEnum(EnumObject enumObject) { if (enumObject.name == null || !enumObject.members.Any(m => m.value != null)) { // Skip if we have no name or values to write return(false); } this.EnsureStarted(); if (enumObject.flags) { this.writer.WriteLine( $" [Flags]"); } string type = enumObject.type ?? "uint"; bool forceUnsigned = enumObject.flags || (enumObject.type != null && (enumObject.type == "uint" || enumObject.type == "ulong" || enumObject.type == "ushort")); StringWriter enumBodyWriter = new StringWriter(); foreach (var member in enumObject.members.Where(m => m.value != null)) { var currentType = type; string valueText = ConstantWriter.FixIntValueText(forceUnsigned, ref currentType, member.value); enumBodyWriter.Write( $@" {member.name} = {valueText}, "); // Make the type more specific if it's signed if (currentType != type && (type != "int" && type != "long")) { type = currentType; } } if (enumObject.type != null) { type = enumObject.type; } string typePart = type != "int" ? $" : {type}" : string.Empty; this.writer.WriteLine( $@" public enum {enumObject.name}{typePart} {{"); this.writer.Write(enumBodyWriter.ToString()); this.writer.WriteLine( $@" }} "); return(true); }