using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace StarterNG.Domain.Sceneries;
///
/// The scenario's own files - included after FirstInit - put in place of their
/// include line when they hold trainsets, as the old starter did. Decorations
/// and road traffic live there; inlined, their consists are listed, can be
/// started from, and the copy written for a start carries them instead of the
/// include, so they are not loaded twice.
///
///
/// Only the plain include path end form, or include path as the last
/// thing in the file, where the simulator runs out of words before it would
/// look for an end: an include with parameters needs them put in, and one
/// whose end is not on its line is read by the simulator in ways not worth guessing. One marked $optional stays too:
/// whether it is loaded is the player's to choose. Each inlined file is wrapped in
/// marker comments, which is how a trainset is told to belong to it.
///
public static class SceneryIncludes
{
public const string BeginMarker = "//$starter-include ";
public const string EndMarker = "//$starter-endinclude";
private static readonly Regex FirstInit = new(@"\bFirstInit\b", RegexOptions.IgnoreCase);
private static readonly Regex Include = new(
@"^[ \t]*include[ \t]+(\S+?)(?:[ \t]+end\b[^\r\n]*|[ \t]*(?://[^\r\n]*)?\s*\z)",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static readonly Regex HasTrainset = new(@"^[ \t]*trainset\b", RegexOptions.Multiline | RegexOptions.IgnoreCase);
private static readonly Regex BlockComment = new(@"/\*[\s\S]*?\*/");
private static readonly Regex Marked = new(
Regex.Escape(BeginMarker) + @"(\S+)[\s\S]*?" + Regex.Escape(EndMarker));
///
/// A file named as an include names it: its name as it is on disk, which the
/// marker carries, and its text - or null when there is none.
///
public static string Inline(string text, Func read)
{
var firstInit = FirstInit.Match(text);
if (!firstInit.Success)
return text;
string head = text[..firstInit.Index];
string tail = text[firstInit.Index..];
string newline = text.Contains("\r\n", StringComparison.Ordinal) ? "\r\n" : "\n";
var comments = BlockComment.Matches(tail).Select(m => (m.Index, End: m.Index + m.Length)).ToList();
return head + Include.Replace(tail, m =>
{
if (comments.Any(c => m.Index >= c.Index && m.Index < c.End))
return m.Value;
if (m.Value.Contains("$optional", StringComparison.OrdinalIgnoreCase))
return m.Value;
if (read(m.Groups[1].Value) is not var (file, content) || !HasTrainset.IsMatch(content))
return m.Value;
return BeginMarker + file + newline + content.TrimEnd() + newline + EndMarker;
});
}
/// The text without the inlined files: what the scenery itself says.
public static string WithoutInlined(string text) => Marked.Replace(text, "");
/// Where each inlined file sits in the text, by the name its include gave.
public static IReadOnlyList<(int Start, int End, string File)> Ranges(string text) =>
Marked.Matches(text).Select(m => (m.Index, m.Index + m.Length, m.Groups[1].Value)).ToList();
public static string? FileAt(IReadOnlyList<(int Start, int End, string File)> ranges, int index) =>
ranges.FirstOrDefault(r => index >= r.Start && index < r.End).File;
///
/// The text as bytes: each inlined file in the encoding it was read in, the rest in
/// . A name both a file put in place and one left to its include
/// spell alike then has the same bytes in both, as when the simulator reads them apart.
///
/// By the name the marker carries; one not listed goes in .
public static byte[] Encode(string text, Encoding own, IReadOnlyDictionary inlined)
{
using var bytes = new MemoryStream();
void Put(Encoding encoding, int start, int end) =>
bytes.Write(encoding.GetBytes(text, start, end - start));
int at = 0;
foreach (var (start, end, file) in Ranges(text))
{
Put(own, at, start);
Put(inlined.GetValueOrDefault(file, own), start, end);
at = end;
}
Put(own, at, text.Length);
return bytes.ToArray();
}
}