public static Game FromFile(string filepath) { using (FileStream fs = new FileStream(filepath, FileMode.Open)) { string hash = fs.ReadString(); string owner = fs.ReadString(); string repo = fs.ReadString(); string token = fs.ReadString(); byte set = (byte)fs.ReadInt32(); Game game = new Game(filepath, token, owner, repo, hash); game.users = new User[fs.ReadInt32()]; for (int i = 0; i < game.users.Length; i++) game.users[i] = User.FromStream(fs); game.contributors = new string[fs.ReadInt32()]; for (int i = 0; i < game.contributors.Length; i++) game.contributors[i] = fs.ReadString(); game.rowCount = fs.ReadInt32(); game.commits = new CommitCollection(game); game.tableStart = fs.Position; game.rowSize = 40 + game.users.Length; for (game.tableIndex = 0; game.tableIndex < game.rowCount; game.tableIndex++) { fs.Seek(40, SeekOrigin.Current); bool stop = false; for (int i = 0; i < game.users.Length; i++) if (fs.ReadByte() == 0) { stop = true; fs.Seek(-(40 + i + 1), SeekOrigin.Current); break; } if (stop) break; } fs.Seek(game.tableStart + game.rowCount * game.rowSize, SeekOrigin.Begin); while (fs.Position < fs.Length) game.messages.Add(Message.FromStream(fs)); return game; } }
ReplayMetadata(FileStream fs, string path) { FilePath = path; // Read start marker if (fs.ReadInt32() != MetaStartMarker) throw new InvalidOperationException("Expected MetaStartMarker but found an invalid value."); // Read version var version = fs.ReadInt32(); if (version != MetaVersion) throw new NotSupportedException("Metadata version {0} is not supported".F(version)); // Read game info (max 100K limit as a safeguard against corrupted files) var data = fs.ReadString(Encoding.UTF8, 1024 * 100); GameInfo = GameInformation.Deserialize(data); }
private string CompressCss(IEnumerable<IPath> cssFiles) { StringBuilder sb = new StringBuilder(); foreach (var file in cssFiles) { using (FileStream fileStream = new FileStream(file.PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var content = fileStream.ReadString(); sb.AppendFormat("{0}\n", CSSMinify.Minify(Url, file.VirtualPath, Request.Url.AbsolutePath, content)); } } return sb.ToString(); }
public string Read() { if (this.Exists()) { using (FileStream fs = new FileStream(this.PhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)) { this.body = fs.ReadString(); } } return this.body; }
public static string ReadAsString(string filePath) { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { return fileStream.ReadString(); } }
public override MemoryStream CreateHeader(string[] files, string[] archiveFilenames, int blockSize, bool[] settings, out uint[] offsetList) { try { /* Let's get out settings now */ blockSize = 24; bool addFilename = settings[0]; bool addPixelFormat = settings[1]; bool addDimensions = settings[2]; bool addGlobalIndex = settings[3]; /* Let's figure out the metadata size, so we can create the header properly */ int metaDataSize = 2; if (addFilename) metaDataSize += 28; if (addPixelFormat) metaDataSize += 2; if (addDimensions) metaDataSize += 2; if (addGlobalIndex) metaDataSize += 4; /* Create the header now */ offsetList = new uint[files.Length]; MemoryStream header = new MemoryStream(Number.RoundUp(0xC + (files.Length * metaDataSize), blockSize)); header.Write(ArchiveHeader.GVM, 4); header.Write(header.Capacity); /* Set up format type */ byte formatType = 0x0; if (addFilename) formatType |= (1 << 3); if (addPixelFormat) formatType |= (1 << 2); if (addDimensions) formatType |= (1 << 1); if (addGlobalIndex) formatType |= (1 << 0); header.WriteByte(0x0); header.WriteByte(formatType); /* Write number of files */ header.Write(((ushort)files.Length).SwapEndian()); uint offset = (uint)header.Capacity + 8; /* Start writing the information in the header */ for (int i = 0; i < files.Length; i++) { using (FileStream data = new FileStream(files[i], FileMode.Open, FileAccess.Read)) { /* Make sure this is a GVR */ Images images = new Images(data, files[i]); if (images.Format != GraphicFormat.GVR) throw new IncorrectGraphicFormat(); /* Get the header offset */ int headerOffset = (data.ReadString(0x0, 4) == GraphicHeader.GVRT ? 0x0 : 0x10); offsetList[i] = offset; header.Write(((ushort)i).SwapEndian()); if (addFilename) header.Write(Path.GetFileNameWithoutExtension(archiveFilenames[i]), 27, 28); if (addPixelFormat) header.Write(data, headerOffset + 0xA, 2); if (addDimensions) { /* Get the width and height */ int width = (int)Math.Min(Math.Log(data.ReadUShort(headerOffset + 0xC).SwapEndian(), 2) - 2, 9); int height = (int)Math.Min(Math.Log(data.ReadUShort(headerOffset + 0xE).SwapEndian(), 2) - 2, 9); header.WriteByte(0x0); header.WriteByte((byte)((width << 4) | height)); } if (addGlobalIndex) { if (headerOffset == 0x0) header.Write(new byte[] {0x0, 0x0, 0x0, 0x0}); else header.Write(data, 0x8, 4); } offset += Number.RoundUp((uint)(data.Length - headerOffset), blockSize); } } return header; } catch (IncorrectGraphicFormat) { offsetList = null; return null; } catch { offsetList = null; return null; } }
/// <summary> /// Read in and validate the header of a city file. /// </summary> /// <param name="stream">File stream waiting at the header for instructions.</param> /// <returns>The length (in bytes) of the data portion of this file.</returns> private static int ParseHeader(FileStream reader) { // Case for too small of a file if (reader.Length < 12) throw new Exception("Unexpected file length."); // Read 12-byte header. string headChunk = reader.ReadString(); int dataLength = reader.Read4ByteInt(); string fileType = reader.ReadString(); // Make sure the header represents a valid city file. if (!headChunk.Equals(HEADERCHUNK) || !fileType.Equals(FILETYPE)) throw new Exception("Invalid SC2000 file or corrupted header."); return dataLength; }