/* * Retrieve data for the given field in the given table, using the given packed files. */ SortedSet <string> CollectValues(string tableName, string fieldName, IEnumerable <PackedFile> packedFiles) { SortedSet <string> result = null; #if DEBUG Console.WriteLine("Looking for {0}:{1} in {2}", tableName, fieldName, packedFiles); #endif // enable load from multiple files List <string> loadedFrom = new List <string>(); foreach (PackedFile packed in packedFiles) { string currentTable = DBFile.Typename(packed.FullPath); if (!packed.FullPath.StartsWith("db")) { continue; } if (currentTable.Equals(tableName)) { // load from several files, but not with the same name string fileName = Path.GetFileName(packed.FullPath); if (loadedFrom.Contains(fileName)) { continue; } if (result == null) { result = new SortedSet <string>(); } #if DEBUG Console.WriteLine("Found {0}:{1} in {2}", tableName, fieldName, packedFiles); #endif try { FillFromPacked(result, packed, fieldName); } catch { return(null); } loadedFrom.Add(fileName); // } else if (found) { // once we're past the files with the correct type, stop searching // break; } else { // we didn't find the right table type, but cache the PackedFile we created along the way List <PackedFile> cacheFiles; if (!typeToPackedCache.TryGetValue(currentTable, out cacheFiles)) { cacheFiles = new List <PackedFile>(); typeToPackedCache.Add(currentTable, cacheFiles); } cacheFiles.Add(packed); } } return(result); }
private void AddFromPacked(PackedFile packed) { if (packed.Size != 0) { string type = DBFile.Typename(packed.FullPath); DBFileHeader header = PackedFileDbCodec.readHeader(packed); int currentMaxVersion; if (!maxVersion.TryGetValue(type, out currentMaxVersion)) { currentMaxVersion = header.Version; } else { currentMaxVersion = Math.Max(header.Version, currentMaxVersion); } maxVersion[type] = currentMaxVersion; } }
// this could do with an update; since the transition to schema.xml, // we also know obsolete fields and can remove them, // and we can add fields in the middle instead of assuming they got appended. public void UpdatePackedFile(PackedFile packedFile) { string key = DBFile.Typename(packedFile.FullPath); if (DBTypeMap.Instance.IsSupported(key)) { PackedFileDbCodec codec = PackedFileDbCodec.FromFilename(packedFile.FullPath); int maxVersion = DBTypeMap.Instance.MaxVersion(key); DBFileHeader header = PackedFileDbCodec.readHeader(packedFile); if (header.Version < maxVersion) { // found a more recent db definition; read data from db file DBFile updatedFile = PackedFileDbCodec.Decode(packedFile); TypeInfo dbFileInfo = updatedFile.CurrentType; TypeInfo targetInfo = GetTargetTypeInfo(key, maxVersion); if (targetInfo == null) { throw new Exception(string.Format("Can't decide new structure for {0} version {1}.", key, maxVersion)); } // identify FieldInstances missing in db file for (int i = 0; i < targetInfo.Fields.Count; i++) { FieldInfo oldField = dbFileInfo[targetInfo.Fields[i].Name]; if (oldField == null) { foreach (List <FieldInstance> entry in updatedFile.Entries) { entry.Insert(i, targetInfo.Fields[i].CreateInstance()); } } } //updatedFile.Header.GUID = guid; updatedFile.Header.Version = maxVersion; packedFile.Data = codec.Encode(updatedFile); } } }