namespace StockSharp.Algo.Compilation;
using Ecng.Compilation;
using Ecng.Reflection;
///
/// Code info.
///
public class CodeInfo : NotifiableObject, IAsyncPersistable, IDisposable
{
private ICompilerContext _context;
///
/// Initializes a new instance of the .
///
public CodeInfo()
{
_assemblyReferences.Changed += OnReferencesChanged;
}
///
public void Dispose()
{
try
{
_assemblyReferences.Changed -= OnReferencesChanged;
var ctx = _context;
if (ctx is null)
return;
ctx.Dispose();
_context = null;
var compiler = Language.TryGetCompiler();
if (compiler is null)
return;
var cache = compiler.IsAssemblyPersistable ? ServicesRegistry.TryCompilerCache : null;
if (cache is null)
return;
cache.Remove(Language, GetSources(), GetRefNames());
}
finally
{
GC.SuppressFinalize(this);
}
}
private void OnReferencesChanged()
=> NotifyChanged(nameof(AssemblyReferences));
private Guid _id = Guid.NewGuid();
///
/// Identifier.
///
public Guid Id
{
get => _id;
set
{
if (_id == value)
return;
_id = value;
NotifyChanged();
}
}
private string _name;
///
/// Name.
///
public string Name
{
get => _name;
set
{
if (_name == value)
return;
_name = value;
NotifyChanged();
}
}
private string _text;
///
/// Code.
///
public string Text
{
get => _text;
set
{
if (_text == value)
return;
_text = value;
NotifyChanged();
}
}
private string _language = CodeExtensions.DefaultLanguage;
///
/// Code language.
///
public string Language
{
get => _language;
set
{
if (_language.EqualsIgnoreCase(value))
return;
if (_context is not null)
throw new InvalidOperationException("Language cannot be changed after compilation.");
_language = value.ThrowIfEmpty(nameof(value));
if (value.EqualsIgnoreCase(FileExts.FSharp))
_assemblyReferences.AddRange(CodeExtensions.FSharpReferences);
}
}
private readonly CachedSynchronizedSet _assemblyReferences = [.. CodeExtensions.DefaultReferences];
///
/// Assembly references.
///
public IList AssemblyReferences => _assemblyReferences;
private readonly CachedSynchronizedSet _projectReferences = [];
///
/// File references.
///
public IList ProjectReferences => _projectReferences;
private readonly CachedSynchronizedSet _nugetReferences = [];
///
/// NuGet references.
///
public IList NuGetReferences => _nugetReferences;
///
/// Object type.
///
public Type ObjectType { get; private set; }
///
/// The code is compilable.
///
public bool IsCompilable { get; private set; }
private string[] _extraSources;
///
/// Extra source codes.
///
public string[] ExtraSources
{
get => _extraSources;
set
{
if ((_extraSources is null && value is null) || (_extraSources is not null && value is not null && _extraSources.SequenceEqual(value)))
return;
_extraSources = value;
NotifyChanged();
}
}
private string _moduleName = "Strategy";
///
/// Module name.
///
public string ModuleName
{
get => _moduleName;
set => _moduleName = value;
}
///
/// Compiled event.
///
public event Action Compiled;
///
/// Last built assembly.
///
public byte[] Assembly { get; private set; }
///
/// Compile code.
///
/// Is type compatible.
/// Type name.
///
[Obsolete("Blocking sync-over-async wrapper. Use CompileAsync instead.")]
public IEnumerable Compile(Func isTypeCompatible = default, string typeName = default)
=> AsyncContext.Run(() => CompileAsync(isTypeCompatible, typeName, default));
private IEnumerable GetSources()
{
var sources = new[] { Text };
if (ExtraSources is not null)
sources = sources.Concat(ExtraSources);
return sources;
}
private string[] GetRefNames()
=> [.. _assemblyReferences.Cache.Concat(_projectReferences.Cache).Concat(_nugetReferences.Cache).Select(r => r.Name)];
///
/// Compile code.
///
/// Is type compatible.
/// Type name.
///
///
public async Task> CompileAsync(Func isTypeCompatible, string typeName, CancellationToken cancellationToken)
{
IsCompilable = false;
var errors = new List();
var compiler = Language.GetCompiler();
_context ??= compiler.CreateContext();
var cache = compiler.IsAssemblyPersistable ? ServicesRegistry.TryCompilerCache : null;
var sources = GetSources();
var refNames = GetRefNames();
Assembly asm = null;
byte[] asmBody = null;
if (cache?.TryGet(Language, sources, refNames, out asmBody) == true)
{
try
{
asm = _context.LoadFromBinary(asmBody);
}
catch (Exception ex)
{
ex.LogError();
cache.Remove(Language, sources, refNames);
}
}
if (asm is null)
{
(string name, byte[] body)[] refs;
try
{
refs = [.. (await _assemblyReferences.Cache.Concat(_projectReferences.Cache).Concat(_nugetReferences.Cache).ToValidRefImages(cancellationToken))];
}
catch (Exception ex)
{
ex.LogError();
return [ex.ToError()];
}
var result = await compiler.Compile(ModuleName, sources, refs, cancellationToken);
if (result.HasErrors())
return result.Errors;
errors.AddRange(result.Errors);
if (result is AssemblyCompilationResult asmRes)
asmBody = asmRes.AssemblyBody;
else
asmBody = null;
try
{
asm = result.GetAssembly(_context);
}
catch (Exception ex)
{
errors.Add(ex.ToError());
}
try
{
cache?.Add(Language, sources, refNames, asmBody);
}
catch (Exception ex)
{
ex.LogError();
}
}
if (asm is not null)
{
IsCompilable = true;
Assembly = asmBody;
try
{
ObjectType = asm.GetExportedTypes().TryFindType(isTypeCompatible, typeName);
}
catch (Exception ex)
{
errors.Add(ex.ToError());
}
try
{
Compiled?.Invoke();
}
catch (Exception ex)
{
errors.Add(ex.ToError());
}
}
return errors;
}
///
public async Task LoadAsync(SettingsStorage storage, CancellationToken cancellationToken)
{
Id = storage.GetValue(nameof(Id), Id);
Name = storage.GetValue(nameof(Name), Name);
Language = storage.GetValue(nameof(Language), Language);
Text = (storage.GetValue(nameof(Text), storage.GetValue("SourceCode"))?.Replace("ChartIndicatorDrawStyles", "DrawStyles"))?.SkipBom();
ExtraSources = storage.GetValue(nameof(ExtraSources), ExtraSources);
ModuleName = storage.GetValue(nameof(ModuleName), ModuleName);
_assemblyReferences.Clear();
var asmRefs = storage.GetValue>(nameof(AssemblyReferences)) ?? storage.GetValue>("References");
if (asmRefs is not null)
_assemblyReferences.AddRange(await asmRefs.LoadAllAsync(cancellationToken));
// TODO 2025-02-04 Remove 1 year later
var oldLogging = _assemblyReferences.Cache.FirstOrDefault(r => r.FileName.EqualsIgnoreCase("StockSharp.Logging.dll"));
if (oldLogging is not null)
{
_assemblyReferences.Remove(oldLogging);
_assemblyReferences.Add(new() { FileName = "Ecng.Logging.dll" });
}
_nugetReferences.Clear();
if (storage.ContainsKey(nameof(NuGetReferences)))
_nugetReferences.AddRange(await storage.GetValue>(nameof(NuGetReferences)).LoadAllAsync(cancellationToken));
_projectReferences.Clear();
if (storage.ContainsKey(nameof(ProjectReferences)))
{
foreach (var projId in storage.GetValue>(nameof(ProjectReferences)))
{
if (CodeExtensions.TryGetProjectReference(projId, out var projRef))
_projectReferences.Add(projRef);
}
}
}
///
public async Task SaveAsync(SettingsStorage storage, CancellationToken cancellationToken)
{
storage
.Set(nameof(Id), Id)
.Set(nameof(Name), Name)
.Set(nameof(Language), Language)
.Set(nameof(ExtraSources), ExtraSources)
.Set(nameof(Text), Text)
.Set(nameof(ModuleName), ModuleName)
.Set(nameof(AssemblyReferences), await _assemblyReferences.Cache.SaveAllAsync(cancellationToken))
.Set(nameof(NuGetReferences), await _nugetReferences.Cache.SaveAllAsync(cancellationToken))
.Set(nameof(ProjectReferences), _projectReferences.Cache.Select(r => r.Id).ToArray())
;
}
}