using System.Globalization; using System.Text; using System.Text.RegularExpressions; using EvilEngine.Mounts; namespace EvilEngine.Adapters.GoldSrc; public static partial class EngineBuild { public static readonly DateTime Epoch = new(1996, 10, 24); private static readonly byte[] Line = "Exe build: "u8.ToArray(); public static int Number(DateTime built) { return (built.Date - Epoch).Days; } public static DateTime? Stamp(IFileSource tree, VirtualPath engine) { if (!tree.TryOpenAccess(engine, out var access)) return null; using (access) { const int window = 1 << 20; const int tail = 64; var keep = tail + Line.Length; var buffer = new byte[window + keep]; long offset = 0; var carried = 0; while (offset < access.Length) { var read = access.Read(offset, buffer.AsSpan(carried, window)); if (read == 0) break; offset += read; var span = buffer.AsSpan(0, carried + read); for (var at = span.IndexOf(Line); at >= 0;) { var text = span[(at + Line.Length)..]; var end = text.IndexOf((byte)0); if (end < 0 && text.Length > tail) end = tail; if (end < 0) break; if (Parse(Encoding.ASCII.GetString(text[..end])) is { } stamp) return stamp; var next = span[(at + 1)..].IndexOf(Line); at = next < 0 ? -1 : at + 1 + next; } carried = Math.Min(keep, span.Length); span[^carried..].CopyTo(buffer); } } return null; } public static DateTime? Parse(string text) { var match = StampText().Match(text); if (!match.Success) return null; var written = $"{match.Groups[2].Value} {match.Groups[3].Value.PadLeft(2, '0')} {match.Groups[4].Value} {match.Groups[1].Value}"; return DateTime.TryParseExact(written, "MMM dd yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out var stamp) ? stamp : null; } [GeneratedRegex(@"^(\d{2}:\d{2}:\d{2}) ([A-Z][a-z]{2}) +(\d{1,2}) (\d{4})")] private static partial Regex StampText(); }