public static void ReadFromFile(IndexUnit indexPage, BinaryReader reader) { long initPos = reader.Seek(100L + (indexPage.UnitID * 0x1000L)); if (reader.ReadByte() != 2) { throw new FileDBException("PageID {0} is not a Index Page", new object[] { indexPage.UnitID }); } indexPage.NextUnitID = reader.ReadUInt32(); indexPage.NodeIndex = reader.ReadByte(); reader.Seek(initPos + 0x2eL); for (int i = 0; i <= indexPage.NodeIndex; i++) { IndexNode node = indexPage.Nodes[i]; node.ID = reader.ReadGuid(); node.IsDeleted = reader.ReadBoolean(); node.Right.Index = reader.ReadByte(); node.Right.PageID = reader.ReadUInt32(); node.Left.Index = reader.ReadByte(); node.Left.PageID = reader.ReadUInt32(); node.DataPageID = reader.ReadUInt32(); node.FileName = reader.ReadString(0x29); node.FileExtension = reader.ReadString(5); node.FileLength = reader.ReadUInt32(); } }
public IEnumerable<Guid> RetrieveChildren(Guid id) { List<Guid> children = new List<Guid>(); string file = GetChildrenFilename(id); if (!File.Exists(file)) return null; try { using (Stream fs = ReadFileStream(file)) { BinaryReader br = new BinaryReader(fs); lock (children) { var count = br.ReadInt32(); var itemsRead = 0; while (itemsRead < count) { children.Add(br.ReadGuid()); itemsRead++; } } } } catch (Exception e) { Logger.ReportException("Failed to retrieve children:", e); #if DEBUG throw; #else return null; #endif } return children.Count == 0 ? null : children; }
public static void ReadFromFile(IndexPage indexPage, BinaryReader reader) { // Seek the stream to the fist byte on page long initPos = reader.Seek(Header.HEADER_SIZE + ((long)indexPage.PageID * BasePage.PAGE_SIZE)); if (reader.ReadByte() != (byte)PageType.Index) throw new FileDBException("PageID {0} is not a Index Page", indexPage.PageID); indexPage.NextPageID = reader.ReadUInt32(); indexPage.NodeIndex = reader.ReadByte(); // Seek the stream to end of header data page reader.Seek(initPos + IndexPage.HEADER_SIZE); for (int i = 0; i <= indexPage.NodeIndex; i++) { var node = indexPage.Nodes[i]; node.ID = reader.ReadGuid(); node.IsDeleted = reader.ReadBoolean(); node.Right.Index = reader.ReadByte(); node.Right.PageID = reader.ReadUInt32(); node.Left.Index = reader.ReadByte(); node.Left.PageID = reader.ReadUInt32(); node.DataPageID = reader.ReadUInt32(); node.FileName = reader.ReadString(IndexNode.FILENAME_SIZE); node.FileExtension = reader.ReadString(IndexNode.FILE_EXTENSION_SIZE); node.FileLength = reader.ReadUInt32(); } }
public static DataPointToAuction Deserialize(Stream stream) { var reader = new BinaryReader(stream); var snapshot = new DataPointToAuction(); snapshot.Id = reader.ReadGuid(); snapshot.DataPointTimeStamp = reader.ReadDouble(); snapshot.DataPointRealmSlug = reader.ReadString(); snapshot.Auction = reader.ReadInt64(); snapshot.TimeLeft = reader.ReadString(); return snapshot; }
public static void ReadFromFile(IndexPage indexPage, BinaryReader reader) { // Seek the stream to the first byte on page long initPos = reader.SetReadPos(Header.FILE_START_HEADER_SIZE + (indexPage.PageID * BasePage.PAGE_SIZE)); if (reader.ReadByte() != (byte)PageType.Index) throw new FileDBException("PageID {0} is not a Index Page", indexPage.PageID); indexPage.NextPageID = reader.ReadUInt32(); indexPage.SetUsedNodeCount(reader.ReadByte()); // Seek the stream to end of header data page reader.SetReadPos(initPos + IndexPage.INDEX_HEADER_SIZE); //in this version //each node uses buffer= 16+1+1+4+1+4+4+8+4+2+36 =81 //IndexPage.NODES_PER_PAGE = 50; //so it use 81*50 = 4050 //IndexPage.INDEX_HEADER_SIZE =46 //so => 4050 +46 = 4096 //and each page has BasePage.PAGE_SIZE = 4096 => matched for (int i = 0; i <= indexPage.UsedNodeCount; i++) { var node = indexPage.Nodes[i]; node.ID = reader.ReadGuid(); //16 node.IsDeleted = reader.ReadBoolean(); //1 node.Right.Index = reader.ReadByte(); //1 node.Right.PageID = reader.ReadUInt32(); //4 node.Left.Index = reader.ReadByte(); //1 node.Left.PageID = reader.ReadUInt32();//4 node.DataPageID = reader.ReadUInt32();//4 node.FileMetaDataLength = reader.ReadUInt16();//2 node.FileLength = reader.ReadUInt32();//4 int filenameCount = reader.ReadByte(); //1 if (filenameCount > IndexNode.FILENAME_SIZE) { node.FileUrl = reader.ReadUtf8String(IndexNode.FILENAME_SIZE); } else { string filename = reader.ReadUtf8String(IndexNode.FILENAME_SIZE); node.FileUrl = filename.Substring(0, filenameCount); } } }
public SectorCollection Deserialize(BinaryReader reader) { var sectorCollectionMagic = reader.ReadUInt32(); if (sectorCollectionMagic != SECTOR_COLLECTION_MAGIC) { throw new FormatException("Expected Sector Collection Magic " + SECTOR_COLLECTION_MAGIC + " but read " + sectorCollectionMagic); } var sectorCount = reader.ReadUInt32(); var collection = new SectorCollection(); for (var i = 0; i < sectorCount; i++) { var guid = reader.ReadGuid(); var startInclusive = reader.ReadInt64(); var endExclusive = reader.ReadInt64(); var sector = (ISector)Activator.CreateInstance(sectorTypesByGuid[guid]); sector.Deserialize(reader); collection.AssignSector(new SectorRange(startInclusive, endExclusive), sector); } return collection; }
private static JToken ReadParameterValue(BinaryReader reader, ParameterMetadata parameterMetadata) { switch (parameterMetadata.DataType) { case DataType.Null: return null; case DataType.Byte: return (int) reader.ReadByte(); case DataType.Word: return reader.ReadUInt16(); case DataType.Dword: return reader.ReadUInt32(); case DataType.Qword: return reader.ReadUInt64(); case DataType.SignedByte: return reader.ReadSByte(); case DataType.SignedWord: return reader.ReadInt16(); case DataType.SignedDword: return reader.ReadInt32(); case DataType.SignedQword: return reader.ReadInt64(); case DataType.Single: return reader.ReadSingle(); case DataType.Double: return reader.ReadDouble(); case DataType.Boolean: return reader.ReadByte() != 0; case DataType.Guid: return reader.ReadGuid(); case DataType.UtfString: return reader.ReadUtfString(); case DataType.Binary: return reader.ReadBinary(); case DataType.Array: return new JArray(reader .ReadArray(r => ReadParameterValue(r, parameterMetadata.Children[0])) .Cast<object>() .ToArray()); case DataType.Object: return new JObject(parameterMetadata.Children .Select(p => new JProperty(p.Name, ReadParameterValue(reader, p))) .Cast<object>() .ToArray()); default: throw new NotSupportedException(parameterMetadata.DataType + " is not supported for parameters"); } }
private void RegisterDevice(byte[] data) { var registrationInfo = new DeviceRegistrationInfo(); using (var stream = new MemoryStream(data)) using (var reader = new BinaryReader(stream)) { registrationInfo.Id = reader.ReadGuid(); registrationInfo.Key = reader.ReadUtfString(); registrationInfo.Name = reader.ReadUtfString(); registrationInfo.ClassName = reader.ReadUtfString(); registrationInfo.ClassVersion = reader.ReadUtfString(); registrationInfo.Equipment = reader.ReadArray(ReadEquipmentInfo); registrationInfo.Notifications = reader.ReadArray(ReadNotificationInfo); registrationInfo.Commands = reader.ReadArray(ReadCommandInfo); } RegisterDeviceCore(registrationInfo); }
//public void Migrate25() //{ // //version 2.5 migration // Version current = new Version(_config.MBVersion); // if (current > new Version(2, 0) && current < new Version(2, 5)) // { // string sqliteDb = Path.Combine(ApplicationPaths.AppCachePath, "cache.db"); // string sqliteDll = Path.Combine(ApplicationPaths.AppConfigPath, "system.data.sqlite.dll"); // if (!_config.EnableExperimentalSqliteSupport) // //clean up any old sql db... // try // { // File.Delete(sqliteDb); // } // catch { } // Kernel.Init(_config); // Logger.ReportInfo("==== Migration Process Started..."); // var newRepo = Kernel.Instance.ItemRepository; // try // { // //var oldRepo = new MediaBrowser.Library.ItemRepository(); // //UpdateProgress("Preparing...", .03); // //Thread.Sleep(15000); //allow old repo to load // if (_config.EnableExperimentalSqliteSupport) // { // UpdateProgress("Backing up DB", .05); // Logger.ReportInfo("Attempting to backup cache db..."); // if (newRepo.BackupDatabase()) Logger.ReportInfo("Database backed up successfully"); // } // //UpdateProgress("PlayStates", .10); // ////newRepo.MigratePlayState(oldRepo); // //UpdateProgress("DisplayPrefs", .20); // //newRepo.MigrateDisplayPrefs(oldRepo); // UpdateProgress("Images", .01); // //MediaBrowser.Library.ImageManagement.ImageCache.Instance.DeleteResizedImages(); // if (_config.EnableExperimentalSqliteSupport) // { // //were already using SQL - our repo can migrate itself // UpdateProgress("Items", .80); // newRepo.MigrateItems(); // } // else // { // //need to go through the file-based repo and re-save // MediaBrowser.Library.Entities.BaseItem item; // int cnt = 0; // string[] cacheFiles = Directory.GetFiles(Path.Combine(ApplicationPaths.AppCachePath, "Items")); // double total = cacheFiles.Count(); // foreach (var file in cacheFiles) // { // UpdateProgress("Items", (double)(cnt / total)); // try // { // using (Stream fs = MediaBrowser.Library.Filesystem.ProtectedFileStream.OpenSharedReader(file)) // { // BinaryReader reader = new BinaryReader(fs); // item = Serializer.Deserialize<MediaBrowser.Library.Entities.BaseItem>(fs); // } // if (item != null) // { // Logger.ReportInfo("Migrating Item: " + item.Name); // newRepo.SaveItem(item); // if (item is Folder) // { // //need to save our children refs // var children = RetrieveChildrenOld(item.Id); // if (children != null) newRepo.SaveChildren(item.Id, children); // } // cnt++; // if (item is Video && (item as Video).RunningTime != null) // { // TimeSpan duration = TimeSpan.FromMinutes((item as Video).RunningTime.Value); // if (duration.Ticks > 0) // { // PlaybackStatus ps = newRepo.RetrievePlayState(item.Id); // decimal pctIn = Decimal.Divide(ps.PositionTicks, duration.Ticks) * 100; // if (pctIn > Kernel.Instance.ConfigData.MaxResumePct) // { // Logger.ReportInfo("Setting " + item.Name + " to 'Watched' based on last played position."); // ps.PositionTicks = 0; // newRepo.SavePlayState(ps); // } // } // } // } // } // catch (Exception e) // { // //this could fail if some items have already been refreshed before we migrated them // Logger.ReportException("Could not migrate item (probably just old data) " + file + e != null && e.InnerException != null ? " Inner Exception: " + e.InnerException.Message : "", e); // } // } // Logger.ReportInfo(cnt + " Items migrated successfully."); // } // } // catch (Exception e) // { // Logger.ReportException("Error in migration - will need to re-build cache.", e); // try // { // File.Delete(sqliteDb); // } // catch { } // } // UpdateProgress("Finishing up...",1); // try // { // Async.RunWithTimeout(newRepo.ShutdownDatabase, 30000); //be sure all writes are flushed // } // catch // { // Logger.ReportWarning("Timed out attempting to close out DB. Assuming all is ok and moving on..."); // } // } // else Logger.ReportInfo("Nothing to Migrate. Version is: " + _config.MBVersion); //} public IEnumerable<Guid> RetrieveChildrenOld(Guid id) { List<Guid> children = new List<Guid>(); string file = Path.Combine(Path.Combine(ApplicationPaths.AppCachePath, "Children"), id.ToString("N")); if (!File.Exists(file)) return null; try { using (Stream fs = MediaBrowser.Library.Filesystem.ProtectedFileStream.OpenSharedReader(file)) { BinaryReader br = new BinaryReader(fs); lock (children) { var count = br.ReadInt32(); var itemsRead = 0; while (itemsRead < count) { children.Add(br.ReadGuid()); itemsRead++; } } } } catch (Exception e) { Logger.ReportException("Failed to retrieve children:", e); return null; } return children.Count == 0 ? null : children; }
public static Upgrade Deserialize(Stream stream) { var reader = new BinaryReader(stream); var upgrade = new Upgrade(); upgrade.Id = reader.ReadGuid(); upgrade.UpgradeId = reader.ReadInt32(); upgrade.Auction = reader.ReadInt64(); return upgrade; }
public static Modifier Deserialize(Stream stream) { var reader = new BinaryReader(stream); var modifier = new Modifier(); modifier.Id = reader.ReadGuid(); modifier.TypeId = reader.ReadInt32(); modifier.Value = reader.ReadInt32(); modifier.Auction = reader.ReadInt64(); return modifier; }