Пример #1
0
        /*
         * 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);
        }
Пример #2
0
        /*
         * Fills the given string collection with data from the field in the given packed file.
         */
        public static void FillFromPacked(SortedSet <string> result, PackedFile packed, string fieldName)
        {
            DBFile dbFile = PackedFileDbCodec.Decode(packed);

            foreach (DBRow entry in dbFile.Entries)
            {
                string toAdd = entry[fieldName].Value;
                if (toAdd != null)
                {
                    result.Add(toAdd);
                }
            }
        }
Пример #3
0
 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);
                }
            }
        }
Пример #5
0
        /*
         * Add data contained in the given db file to this one.
         */
        public void Import(DBFile file)
        {
            if (CurrentType.Name != file.CurrentType.Name)
            {
                throw new DBFileNotSupportedException
                          ("File type of imported DB doesn't match that of the currently opened one", this);
            }
            // check field type compatibility
            for (int i = 0; i < file.CurrentType.Fields.Count; i++)
            {
                if (file.CurrentType.Fields [i].TypeCode != CurrentType.Fields [i].TypeCode)
                {
                    throw new DBFileNotSupportedException
                              ("Data structure of imported DB doesn't match that of currently opened one at field " + i, this);
                }
            }
            DBFileHeader h = file.Header;

            Header      = new DBFileHeader(h.GUID, h.Version, h.EntryCount, h.HasVersionMarker);
            CurrentType = file.CurrentType;
            // this.entries = new List<List<FieldInstance>> ();
            entries.AddRange(file.entries);
            Header.EntryCount = (uint)entries.Count;
        }
Пример #6
0
 public BuildingModelFile(DBFile file)
 {
     fields = new List <List <FieldInstance> >(file.Entries);
     header = file.Header;
     // info = file.CurrentType;
 }
Пример #7
0
 /*
  * Create copy of the given db file.
  */
 public DBFile(DBFile toCopy) : this(toCopy.Header, toCopy.CurrentType)
 {
     Header = new DBFileHeader(toCopy.Header.GUID, toCopy.Header.Version, toCopy.Header.EntryCount, toCopy.Header.HasVersionMarker);
     // we need to create a new instance for every field so we don't write through to the old data
     toCopy.entries.ForEach(entry => entries.Add(new DBRow(toCopy.CurrentType, entry)));
 }
 public DBFileNotSupportedException(string message, DBFile file) : base(message)
 {
     DbFile = file;
 }
 public DBFileNotSupportedException(DBFile file) : this("DB File not supported", file)
 {
 }
 public DbException(string message, DBFile file) : base(message)
 {
     DbFile = file;
 }