Ejemplo n.º 1
0
        /// <inheritdoc/>
        public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
        {
            // Open a file reader
            Encoding             enc = filename.GetEncoding();
            SeparatedValueReader svr = new SeparatedValueReader(File.OpenRead(filename), enc)
            {
                Header           = true,
                Quotes           = true,
                Separator        = _delim,
                VerifyFieldCount = true,
            };

            // If we're somehow at the end of the stream already, we can't do anything
            if (svr.EndOfStream)
            {
                return;
            }

            // Read in the header
            svr.ReadHeader();

            // Loop through all of the data lines
            while (!svr.EndOfStream)
            {
                try
                {
                    // Get the current line, split and parse
                    svr.ReadNextLine();

                    // Create mapping dictionaries
                    Setter setter = new Setter();
                    setter.PopulateSettersFromList(svr.HeaderValues, svr.Line);

                    // Set DatHeader fields
                    DatHeader datHeader = new DatHeader();
                    setter.SetFields(datHeader);
                    Header.ConditionalCopy(datHeader);

                    // Set Machine and DatItem fields
                    if (setter.DatItemMappings.ContainsKey(DatItemField.Type))
                    {
                        DatItem datItem = DatItem.Create(setter.DatItemMappings[DatItemField.Type].AsItemType());
                        setter.SetFields(datItem);
                        datItem.Machine = new Machine();
                        setter.SetFields(datItem.Machine);
                        datItem.Source = new Source(indexId, filename);
                        ParseAddHelper(datItem, statsOnly);
                    }
                }
                catch (Exception ex) when(!throwOnError)
                {
                    string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";

                    logger.Error(ex, message);
                }
            }

            svr.Dispose();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parse a character-separated value DAT and return all found games and roms within
        /// </summary>
        /// <param name="filename">Name of the file to be parsed</param>
        /// <param name="indexId">Index ID for the DAT</param>
        /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
        /// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
        protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
        {
            // Open a file reader
            Encoding             enc = FileExtensions.GetEncoding(filename);
            SeparatedValueReader svr = new SeparatedValueReader(FileExtensions.TryOpenRead(filename), enc)
            {
                Header           = true,
                Quotes           = true,
                Separator        = _delim,
                VerifyFieldCount = true,
            };

            // If we're somehow at the end of the stream already, we can't do anything
            if (svr.EndOfStream)
            {
                return;
            }

            // Read in the header
            svr.ReadHeader();

            // Loop through all of the data lines
            while (!svr.EndOfStream)
            {
                try
                {
                    // Get the current line, split and parse
                    svr.ReadNextLine();

                    // Create mapping dictionary
                    var mappings = new Dictionary <Field, string>();

                    // Now we loop through and get values for everything
                    for (int i = 0; i < svr.HeaderValues.Count; i++)
                    {
                        Field  key   = svr.HeaderValues[i].AsField();
                        string value = svr.Line[i];
                        mappings[key] = value;
                    }

                    // Set DatHeader fields
                    DatHeader header = new DatHeader();
                    header.SetFields(mappings);
                    Header.ConditionalCopy(header);

                    // Set Machine and DatItem fields
                    if (mappings.ContainsKey(Field.DatItem_Type))
                    {
                        DatItem datItem = DatItem.Create(mappings[Field.DatItem_Type].AsItemType());
                        datItem.SetFields(mappings);
                        datItem.Source = new Source(indexId, filename);
                        ParseAddHelper(datItem);
                    }
                }
                catch (Exception ex)
                {
                    string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
                    logger.Error(ex, message);
                    if (throwOnError)
                    {
                        svr.Dispose();
                        throw new Exception(message, ex);
                    }
                }
            }

            svr.Dispose();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parse an AttractMode DAT and return all found games within
        /// </summary>
        /// <param name="filename">Name of the file to be parsed</param>
        /// <param name="indexId">Index ID for the DAT</param>
        /// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
        /// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
        protected override void ParseFile(string filename, int indexId, bool keep, bool throwOnError = false)
        {
            // Open a file reader
            Encoding             enc = FileExtensions.GetEncoding(filename);
            SeparatedValueReader svr = new SeparatedValueReader(FileExtensions.TryOpenRead(filename), enc)
            {
                Header           = true,
                Quotes           = false,
                Separator        = ';',
                VerifyFieldCount = true
            };

            // If we're somehow at the end of the stream already, we can't do anything
            if (svr.EndOfStream)
            {
                return;
            }

            // Read in the header
            svr.ReadHeader();

            // Header values should match
            // #Name;Title;Emulator;CloneOf;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons

            // Loop through all of the data lines
            while (!svr.EndOfStream)
            {
                try
                {
                    // Get the current line, split and parse
                    svr.ReadNextLine();

                    Rom rom = new Rom
                    {
                        Name       = "-",
                        Size       = Constants.SizeZero,
                        CRC        = Constants.CRCZero,
                        MD5        = Constants.MD5Zero,
                        SHA1       = Constants.SHA1Zero,
                        ItemStatus = ItemStatus.None,

                        Machine = new Machine
                        {
                            Name         = svr.Line[0],  // #Name
                            Description  = svr.Line[1],  // Title
                            CloneOf      = svr.Line[3],  // CloneOf
                            Year         = svr.Line[4],  // Year
                            Manufacturer = svr.Line[5],  // Manufacturer
                            Category     = svr.Line[6],  // Category
                            Players      = svr.Line[7],  // Players
                            Rotation     = svr.Line[8],  // Rotation
                            Control      = svr.Line[9],  // Control
                            Status       = svr.Line[10], // Status
                            DisplayCount = svr.Line[11], // DisplayCount
                            DisplayType  = svr.Line[12], // DisplayType
                            Comment      = svr.Line[15], // Extra
                            Buttons      = svr.Line[16], // Buttons
                        },

                        AltName  = svr.Line[13], // AltRomname
                        AltTitle = svr.Line[14], // AltTitle

                        Source = new Source
                        {
                            Index = indexId,
                            Name  = filename,
                        },
                    };

                    // Now process and add the rom
                    ParseAddHelper(rom);
                }
                catch (Exception ex)
                {
                    string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
                    logger.Error(ex, message);
                    if (throwOnError)
                    {
                        svr.Dispose();
                        throw new Exception(message, ex);
                    }
                }
            }

            svr.Dispose();
        }