Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- namespace JasonStorey
- {
- public static class FileHeaders
- {
- public static bool TryGetVersionFromFile(string filePath, string keyword, out int version)
- {
- version = 0;
- try
- {
- if (!TryReadFirstThreeBytes(filePath, out var fileBytes)) return false;
- var keywordMatches = fileBytes[0].Equals(KeywordToAsciiByte(keyword));
- version = BytesToInt(fileBytes[1], fileBytes[2]);
- return keywordMatches;
- }
- catch (Exception)
- {
- return false;
- }
- }
- public static bool UpdateFileVersion(string path, string keyword, int version)
- {
- try
- {
- if (TryGetVersionFromFile(path, keyword, out _))
- {
- var bytes = IntToBytes(version);
- UpdateVersionBytes(path, bytes[0], bytes[1]);
- return true;
- }
- WriteHeaderToStartOfFile(path, keyword, version);
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
- }
- #region plumbing
- static void WriteHeaderToStartOfFile(string filePath, string keyword, int version)
- {
- var intBytes = IntToBytes(version);
- byte[] bytes =
- {
- KeywordToAsciiByte(keyword),
- intBytes[0],
- intBytes[1]
- };
- WriteBytesToStartOfFile(bytes, filePath);
- }
- static void WriteBytesToStartOfFile(byte[] bytesToWrite, string filePath)
- {
- if (!File.Exists(filePath)) File.Create(filePath).Dispose();
- byte[] existingBytes = File.ReadAllBytes(filePath);
- byte[] allBytes = new byte[bytesToWrite.Length + existingBytes.Length];
- bytesToWrite.CopyTo(allBytes, 0);
- existingBytes.CopyTo(allBytes, bytesToWrite.Length);
- using var fileStream = new FileStream(filePath, FileMode.Create);
- fileStream.Write(allBytes, 0, allBytes.Length);
- }
- static void UpdateVersionBytes(string filePath, byte secondByte, byte thirdByte)
- {
- using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
- if (fileStream.Length < 3)
- throw new ArgumentException("The specified file does not have at least 3 bytes.", nameof(filePath));
- fileStream.Seek(1, SeekOrigin.Begin);
- fileStream.WriteByte(secondByte);
- fileStream.WriteByte(thirdByte);
- }
- static bool TryReadFirstThreeBytes(string filePath, out byte[] bytes)
- {
- bytes = new byte[3];
- if (!File.Exists(filePath)) return false;
- using var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- if (stream.Length < 3) return false;
- int bytesRead = 0;
- while (bytesRead < 3)
- {
- var count = stream.Read(bytes, bytesRead, 3 - bytesRead);
- if (count == 0)
- break;
- bytesRead += count;
- }
- return true;
- }
- static byte KeywordToAsciiByte(string word) => (byte)word.Sum(x => x);
- static int BytesToInt(byte highByte, byte lowByte) => (highByte << 8) | lowByte;
- static byte[] IntToBytes(int value)
- {
- byte[] bytes = new byte[2];
- bytes[0] = (byte)(value >> 8); // high byte
- bytes[1] = (byte)value; // low byte
- return bytes;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement