Пример #1
0
        /// <summary>
        /// Read header information
        /// </summary>
        /// <param name="jtr">JsonTextReader to use to parse the header</param>
        private void ReadHeader(JsonTextReader jtr)
        {
            // If the reader is invalid, skip
            if (jtr == null)
            {
                return;
            }

            // Read in the header and apply any new fields
            jtr.Read();
            JsonSerializer js     = new JsonSerializer();
            DatHeader      header = js.Deserialize <DatHeader>(jtr);

            Header.ConditionalCopy(header);
        }
Пример #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();
        }
Пример #3
0
        /// <summary>
        /// Overwrite local values from another DatHeader if not default
        /// </summary>
        /// <param name="datHeader">DatHeader to get the values from</param>
        public void ConditionalCopy(DatHeader datHeader)
        {
            if (!string.IsNullOrWhiteSpace(datHeader.FileName))
            {
                FileName = datHeader.FileName;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Name))
            {
                Name = datHeader.Name;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Description))
            {
                Description = datHeader.Description;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.RootDir))
            {
                RootDir = datHeader.RootDir;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Category))
            {
                Category = datHeader.Category;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Version))
            {
                Version = datHeader.Version;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Date))
            {
                Date = datHeader.Date;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Author))
            {
                Author = datHeader.Author;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Email))
            {
                Email = datHeader.Email;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Homepage))
            {
                Homepage = datHeader.Homepage;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Url))
            {
                Url = datHeader.Url;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Comment))
            {
                Comment = datHeader.Comment;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.HeaderSkipper))
            {
                HeaderSkipper = datHeader.HeaderSkipper;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Type))
            {
                Type = datHeader.Type;
            }

            if (datHeader.ForceMerging != MergingFlag.None)
            {
                ForceMerging = datHeader.ForceMerging;
            }

            if (datHeader.ForceNodump != NodumpFlag.None)
            {
                ForceNodump = datHeader.ForceNodump;
            }

            if (datHeader.ForcePacking != PackingFlag.None)
            {
                ForcePacking = datHeader.ForcePacking;
            }

            if (datHeader.DatFormat != 0x00)
            {
                DatFormat = datHeader.DatFormat;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Prefix))
            {
                Prefix = datHeader.Prefix;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.Postfix))
            {
                Postfix = datHeader.Postfix;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.AddExtension))
            {
                AddExtension = datHeader.AddExtension;
            }

            if (!string.IsNullOrWhiteSpace(datHeader.ReplaceExtension))
            {
                ReplaceExtension = datHeader.ReplaceExtension;
            }

            RemoveExtension = datHeader.RemoveExtension;
            InputDepot      = datHeader.InputDepot?.Clone() as DepotInformation;
            OutputDepot     = datHeader.OutputDepot?.Clone() as DepotInformation;
            GameName        = datHeader.GameName;
            Quotes          = datHeader.Quotes;
            UseRomName      = datHeader.UseRomName;
        }
Пример #4
0
        /// <summary>
        /// Parse an SabreDat XML DAT and return all found directories and files 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)
        {
            // Prepare all internal variables
            XmlReader xtr = filename.GetXmlTextReader();

            // If we got a null reader, just return
            if (xtr == null)
            {
                return;
            }

            // Otherwise, read the file to the end
            try
            {
                xtr.MoveToContent();
                while (!xtr.EOF)
                {
                    // We only want elements
                    if (xtr.NodeType != XmlNodeType.Element)
                    {
                        xtr.Read();
                        continue;
                    }

                    switch (xtr.Name)
                    {
                    case "header":
                        XmlSerializer xs     = new XmlSerializer(typeof(DatHeader));
                        DatHeader     header = xs.Deserialize(xtr.ReadSubtree()) as DatHeader;
                        Header.ConditionalCopy(header);
                        xtr.Skip();
                        break;

                    case "directory":
                        ReadDirectory(xtr.ReadSubtree(), filename, indexId);

                        // Skip the directory node now that we've processed it
                        xtr.Read();
                        break;

                    default:
                        xtr.Read();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Warning(ex, $"Exception found while parsing '{filename}'");
                if (throwOnError)
                {
                    xtr.Dispose();
                    throw ex;
                }

                // For XML errors, just skip the affected node
                xtr?.Read();
            }

            xtr.Dispose();
        }