0.2.0-dev Download ZIP
75 lines ·2.5 KB Raw
| 1 | using System.Globalization; |
| 2 | using System.Text; |
| 3 | using System.Text.RegularExpressions; |
| 4 | using EvilEngine.Mounts; |
| 5 | |
| 6 | namespace EvilEngine.Adapters.GoldSrc; |
| 7 | |
| 8 | public static partial class EngineBuild |
| 9 | { |
| 10 | public static readonly DateTime Epoch = new(1996, 10, 24); |
| 11 | |
| 12 | private static readonly byte[] Line = "Exe build: "u8.ToArray(); |
| 13 | |
| 14 | public static int Number(DateTime built) |
| 15 | { |
| 16 | return (built.Date - Epoch).Days; |
| 17 | } |
| 18 | |
| 19 | public static DateTime? Stamp(IFileSource tree, VirtualPath engine) |
| 20 | { |
| 21 | if (!tree.TryOpenAccess(engine, out var access)) |
| 22 | return null; |
| 23 | using (access) |
| 24 | { |
| 25 | const int window = 1 << 20; |
| 26 | const int tail = 64; |
| 27 | var keep = tail + Line.Length; |
| 28 | var buffer = new byte[window + keep]; |
| 29 | long offset = 0; |
| 30 | var carried = 0; |
| 31 | while (offset < access.Length) |
| 32 | { |
| 33 | var read = access.Read(offset, buffer.AsSpan(carried, window)); |
| 34 | if (read == 0) |
| 35 | break; |
| 36 | offset += read; |
| 37 | var span = buffer.AsSpan(0, carried + read); |
| 38 | for (var at = span.IndexOf(Line); at >= 0;) |
| 39 | { |
| 40 | var text = span[(at + Line.Length)..]; |
| 41 | var end = text.IndexOf((byte)0); |
| 42 | if (end < 0 && text.Length > tail) |
| 43 | end = tail; |
| 44 | if (end < 0) |
| 45 | break; |
| 46 | if (Parse(Encoding.ASCII.GetString(text[..end])) is { } stamp) |
| 47 | return stamp; |
| 48 | var next = span[(at + 1)..].IndexOf(Line); |
| 49 | at = next < 0 ? -1 : at + 1 + next; |
| 50 | } |
| 51 | |
| 52 | carried = Math.Min(keep, span.Length); |
| 53 | span[^carried..].CopyTo(buffer); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return null; |
| 58 | } |
| 59 | |
| 60 | public static DateTime? Parse(string text) |
| 61 | { |
| 62 | var match = StampText().Match(text); |
| 63 | if (!match.Success) |
| 64 | return null; |
| 65 | var written = |
| 66 | $"{match.Groups[2].Value} {match.Groups[3].Value.PadLeft(2, '0')} {match.Groups[4].Value} {match.Groups[1].Value}"; |
| 67 | return DateTime.TryParseExact(written, "MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture, |
| 68 | DateTimeStyles.None, out var stamp) |
| 69 | ? stamp |
| 70 | : null; |
| 71 | } |
| 72 | |
| 73 | [GeneratedRegex(@"^(\d{2}:\d{2}:\d{2}) ([A-Z][a-z]{2}) +(\d{1,2}) (\d{4})")] |
| 74 | private static partial Regex StampText(); |
| 75 | } |