/// <summary> /// Opens the specified <see cref="FileFormats.ResIdxDbf"/>, adds all of its tables and records to the <see cref="DataSet"/> and adds the file's name, /// from <see cref="Path.GetFileName(string)"/>, as a new data source /// </summary> /// <param name="filePath"></param> /// <returns>false if <see cref="DbfFile.ReadStream(Stream)"/> returned false, true otherwise</returns> public static bool OpenStandardGameSet(this DataSet ds, string filePath) { using (FileStream fs = new FileStream(filePath, FileMode.Open)) { var defs = ResourceDefinitionReader.ReadDefinitions(fs, false); // Create a backup copy in the event Tables.Add() succeeds for one or more tables before // failing. This will allow us to just return false without mucking up the DataSet with // only some of the loaded tables. using (DataSet temp = new DataSet()) { temp.Merge(ds); foreach (KeyValuePair <string, uint> kv in defs) { fs.Position = kv.Value; //the DBF's offset value in the set file DbfFile file = new DbfFile(); if (file.ReadStream(fs) != true) { return(false); } file.DataTable.TableName = Path.GetFileNameWithoutExtension(kv.Key); file.DataTable.AddDataSource(Path.GetFileName((fs as FileStream)?.Name)); if (ds.Tables.Contains(file.DataTable.TableName)) { Logger.TraceEvent(TraceEventType.Error, 0, $"Failed to open standard game set due to a duplicate table: {file.DataTable.TableName} in {filePath}"); return(false); } else { temp.Tables.Add(file.DataTable); } } // only add the tables once we're sure there are no duplicates ds.Merge(temp); ds.AddDataSource(Path.GetFileName(filePath)); Logger.TraceInformation($"Opened standard game set from: {filePath}"); } } return(true); }
public static void AddDataTableFromNewSource(this DataSet ds, DataTable dt) { ds.AddDataSource(dt.GetDataSource()); ds.Tables.Add(dt); }