Files
ZuneUIXTools/UIXC/IrisSourceRepository.cs
T

46 lines
1.3 KiB
C#
Raw Normal View History

2024-02-22 08:07:38 -06:00
using Errata;
using Microsoft.Iris;
using System.Diagnostics.CodeAnalysis;
namespace UIXC;
internal class IrisSourceRepository : ISourceRepository
{
private readonly Dictionary<string, Source?> _sourceCache;
public IrisSourceRepository(CompilerInput[] compilands, CompilerInput? sharedBinaryDataTable)
{
_sourceCache = new(compilands.Length + 1);
foreach (var compiland in compilands)
{
var id = compiland.SourceFileName;
2024-07-07 15:36:29 -05:00
// We can only read source from local files
if (id.Contains(Uri.SchemeDelimiter))
continue;
2024-02-22 08:07:38 -06:00
_sourceCache.Add(id, null);
}
if (sharedBinaryDataTable?.SourceFileName is not null)
_sourceCache.Add(sharedBinaryDataTable.Value.SourceFileName, null);
}
2024-07-07 15:36:29 -05:00
public bool TryGet(string uri, [NotNullWhen(true)] out Source? source)
2024-02-22 08:07:38 -06:00
{
2024-07-07 15:36:29 -05:00
var idStart = uri.IndexOf(Uri.SchemeDelimiter) + Uri.SchemeDelimiter.Length;
var id = uri[idStart..];
2024-02-22 08:07:38 -06:00
if (!_sourceCache.TryGetValue(id, out source))
2024-07-07 15:36:29 -05:00
return false;
2024-02-22 08:07:38 -06:00
if (source is null)
{
var sourceText = File.ReadAllText(id);
source = _sourceCache[id] = new(id, sourceText);
}
return true;
}
}