You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.9 KiB
62 lines
1.9 KiB
using System.Collections.Generic; |
|
using System.Linq; |
|
using gaemstone.SourceGen.Utility; |
|
using Microsoft.CodeAnalysis; |
|
using Microsoft.CodeAnalysis.CSharp; |
|
using Microsoft.CodeAnalysis.CSharp.Syntax; |
|
|
|
namespace gaemstone.SourceGen.Structure; |
|
|
|
public class ModuleEntityInfo : TypeEntityInfo |
|
{ |
|
public bool IsPartial { get; } |
|
public bool HasLifetimeInterface { get; } |
|
|
|
public ModuleEntityInfo(ISymbol symbol) |
|
: base(symbol) |
|
{ |
|
var classDecl = (TypeDeclarationSyntax)Symbol.DeclaringSyntaxReferences.First().GetSyntax(); |
|
IsPartial = classDecl.Modifiers.Any(t => t.IsKind(SyntaxKind.PartialKeyword)); |
|
|
|
HasLifetimeInterface = Symbol.AllInterfaces.Any(i => |
|
i.GetFullName(FullNameStyle.NoGeneric) == "gaemstone.IModuleLifetime"); |
|
|
|
IsBuiltIn = Has("BuiltIn"); |
|
} |
|
|
|
protected override IEnumerable<Diagnostic> ValidateSelf() |
|
{ |
|
foreach (var diag in base.ValidateSelf()) yield return diag; |
|
|
|
if (!IsPartial) yield return Diagnostic.Create( |
|
Descriptors.ModuleMustBePartial, Location); |
|
|
|
if (IsBuiltIn && (EntityPath == null)) yield return Diagnostic.Create( |
|
Descriptors.BuiltInModuleMustHavePath, Location); |
|
} |
|
|
|
public IEnumerable<string> GetDependencies() |
|
{ |
|
foreach (var (relation, target) in RelationsToAdd) |
|
if (relation.GetFullName(FullNameStyle.NoGeneric) == "flecs.core.DependsOn") |
|
yield return GetModulePath(target); |
|
} |
|
|
|
public string GetModulePath() |
|
=> GetModulePath(Symbol); |
|
|
|
private static string GetModulePath(ISymbol module) |
|
{ |
|
var pathAttr = module.GetAttributes().FirstOrDefault(attr => |
|
attr.AttributeClass!.GetFullName() == "gaemstone.ECS.PathAttribute"); |
|
var path = pathAttr?.ConstructorArguments.FirstOrDefault().Value as string; |
|
|
|
var isAbsolute = (path?.FirstOrDefault() == '/'); |
|
if (isAbsolute) return path!; |
|
|
|
var fullPath = module.GetFullName().Replace('.', '/'); |
|
return (path != null) |
|
? $"/{fullPath[..(fullPath.LastIndexOf('/'))]}/{path}" |
|
: $"/{fullPath}"; |
|
} |
|
}
|
|
|