Esempio n. 1
0
        // Try process
        public static bool TryProcess(EnumSpec spec, VulkanSpec vkspec, Dictionary <string, EnumType> found,
                                      out EnumType?type)
        {
            type = null;

            // Check for alias type
            if (spec.Alias is not null)
            {
                if (!found.TryGetValue(spec.Alias.Name, out var alias))
                {
                    Program.PrintError($"Enum type '{spec.Name}' is aliased to unknown type '{spec.Alias.Name}'");
                    return(false);
                }

                // Return with alias
                type = new(spec, alias);
                return(true);
            }

            // Process entries
            List <Entry> entries = new();

            foreach (var value in spec.Values)
            {
                // Process name
                if (NameHelper.ConvertEnumValue(spec.Name, value.Name) is not string procName)
                {
                    Program.PrintError($"Failed to process enum value name '{value.Name}' for enum '{spec.Name}'");
                    return(false);
                }

                // Process value
                int procValue;
                if (value.ValueStr.StartsWith("0x"))
                {
                    if (!Int32.TryParse(value.ValueStr.Substring("0x".Length), NumberStyles.AllowHexSpecifier, null,
                                        out procValue))
                    {
                        Program.PrintError(
                            $"Failed to parse hex value '{value.ValueStr}' for enum value '{value.Name}'");
                        return(false);
                    }
                }
                else if (!Int32.TryParse(value.ValueStr, out procValue))
                {
                    Program.PrintError(
                        $"Failed to parse decimal value '{value.ValueStr}' for enum value '{value.Name}'");
                    return(false);
                }
                if (value.IsBitpos)
                {
                    procValue = 1 << procValue;
                }

                // Check for invalid duplicates (sometimes there are valid duplicates)
                var dup = entries.FirstOrDefault(ent => ent.Name == procName);
                if ((dup is not null) && (dup.Value != procValue))
                {
                    Program.PrintError($"Invalid duplicate enum value name '{procName}'");
                    return(false);
                }

                if (dup is null)
                {
                    entries.Add(new(procName, procValue));
                }
            }

            // Return
            type = new(spec, entries);
            return(true);
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            // Parse arguments, help/return if requested or error
            if (!ArgParse.Parse(args))
            {
                return;
            }
            if (ArgParse.Help)
            {
                ArgParse.PrintHelp();
                return;
            }

            // Check that the input file and output path are valid
            if (!File.Exists(ArgParse.InputFile))
            {
                PrintError($"Input file '{ArgParse.InputFile}' does not exist or is an invalid path.");
                return;
            }
            if (!Directory.Exists(ArgParse.OutputPath))
            {
                try {
                    Directory.CreateDirectory(ArgParse.OutputPath);
                }
                catch {
                    PrintError($"Failed to create output directory '{ArgParse.OutputPath}'");
                    return;
                }
            }

            // Load the specification
            VulkanSpec?vkspec = null;

#if DEBUG
            if (!VulkanSpec.TryLoad(ArgParse.InputFile, out vkspec))
            {
                PrintError("Failed to load specification file");
                return;
            }
#else
            try {
                if (!VulkanSpec.TryLoad(ArgParse.InputFile, out vkspec))
                {
                    PrintError("Failed to load specification file");
                    return;
                }
            }
            catch (Exception e) {
                PrintError($"Unhandled specification load exception");
                PrintError($"{e.GetType()} - {e.Message}");
            }
#endif // DEBUG

            // Process the specification
            ProcessedSpec?procspec = null;
#if DEBUG
            if (!ProcessedSpec.TryProcess(vkspec !, out procspec))
            {
                PrintError("Failed to process specification");
                return;
            }
#else
            try {
                if (!ProcessedSpec.TryProcess(vkspec !, out procspec))
                {
                    PrintError("Failed to process specification");
                    return;
                }
            }
            catch (Exception e) {
                PrintError($"Unhandled specification process exception");
                PrintError($"{e.GetType()} - {e.Message}");
            }
#endif // DEBUG

// Generate the specification
#if DEBUG
            if (!APIGenerator.Generate(procspec !))
            {
                PrintError("Failed to generate API");
                return;
            }
#else
            try {
                if (!APIGenerator.Generate(procspec !))
                {
                    PrintError("Failed to generate API");
                    return;
                }
            }
            catch (Exception e) {
                PrintError($"Unhandled api generation exception");
                PrintError($"{e.GetType()} - {e.Message}");
            }
#endif // DEBUG
        }