Skip to content

Commit

Permalink
上传重构的项目
Browse files Browse the repository at this point in the history
1. 增加 ConfigOutput 以便控制输出。
2. 增加 ConfigReferenceCombineBehavior 配置引用合并策略。
3. 修改 ConfigReferenceLoadBehavior 配置主域及当前域的家在行为。
4. 修改 ConfigAssemblyLoadBehavior 配置当前域程序集的加载行为。
5. 修改 ConfigSameNameReferencesFilter 引用同名过滤策略。
  • Loading branch information
NMSAzulX committed Oct 31, 2023
1 parent 4bf2fa8 commit 72f546d
Show file tree
Hide file tree
Showing 425 changed files with 1,589 additions and 1,359 deletions.
11 changes: 9 additions & 2 deletions Natasha.sln
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scanner", "scanner", "{B7AA
scanner.sh = scanner.sh
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Natasha.CSharp.Compiler", "src\Natasha.CSharp\Natasha.CSharp.Compiler\Natasha.CSharp.Compiler.csproj", "{D7EDD106-B744-4E0C-9CCE-D88F29EBC983}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Natasha.CSharp.Compiler", "src\Natasha.CSharp\Natasha.CSharp.Compiler\Natasha.CSharp.Compiler.csproj", "{D7EDD106-B744-4E0C-9CCE-D88F29EBC983}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Natasha.CSharp.Template", "src\Natasha.CSharp\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj", "{84A54AF4-0683-48D4-B9D2-465B851E1EF1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Natasha.CSharp.Template", "src\Natasha.CSharp\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj", "{84A54AF4-0683-48D4-B9D2-465B851E1EF1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PluginFunctionUT", "test\ut\PluginFunctionUT\PluginFunctionUT.csproj", "{716B9CEC-8CA2-433F-9E9A-974E536C4099}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -355,6 +357,10 @@ Global
{84A54AF4-0683-48D4-B9D2-465B851E1EF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84A54AF4-0683-48D4-B9D2-465B851E1EF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84A54AF4-0683-48D4-B9D2-465B851E1EF1}.Release|Any CPU.Build.0 = Release|Any CPU
{716B9CEC-8CA2-433F-9E9A-974E536C4099}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{716B9CEC-8CA2-433F-9E9A-974E536C4099}.Debug|Any CPU.Build.0 = Debug|Any CPU
{716B9CEC-8CA2-433F-9E9A-974E536C4099}.Release|Any CPU.ActiveCfg = Release|Any CPU
{716B9CEC-8CA2-433F-9E9A-974E536C4099}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -422,6 +428,7 @@ Global
{B7AA9686-44B7-4170-82B2-BF8E3B892887} = {DD0B729A-C1D5-41E1-AE1B-FE66F4BC651E}
{D7EDD106-B744-4E0C-9CCE-D88F29EBC983} = {8C9B862A-B569-460C-8B74-E74C6DF0CAB3}
{84A54AF4-0683-48D4-B9D2-465B851E1EF1} = {8C9B862A-B569-460C-8B74-E74C6DF0CAB3}
{716B9CEC-8CA2-433F-9E9A-974E536C4099} = {F4622BCB-C287-4AFE-916B-8C138BD15A3D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3004E730-B231-40FA-B75C-58D7DDE17679}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Natasha.CSharp\Natasha.CSharp.csproj" />
<ProjectReference Include="..\..\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
</ItemGroup>

</Project>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,47 +22,59 @@ public sealed partial class AssemblyCSharpBuilder
private PluginLoadBehavior _compileAssemblyBehavior;
private Func<AssemblyName, AssemblyName, AssemblyLoadVersionResult>? _referencePickFunc;
private Func<IEnumerable<MetadataReference>, IEnumerable<MetadataReference>>? _referencesFilter;
private bool _combineReferences;

public AssemblyCSharpBuilder WithReferenceCombine()
{
_combineReferences = true;
return this;
}
public AssemblyCSharpBuilder WithoutReferenceCombine()

private CombineReferenceBehavior _combineReferenceBehavior;
/// <summary>
/// 配置编译所需的引用
/// </summary>
/// <param name="combineReferenceBehavior"></param>
/// <returns></returns>
public AssemblyCSharpBuilder ConfigReferenceCombineBehavior(CombineReferenceBehavior combineReferenceBehavior)
{
_combineReferences = false;
_combineReferenceBehavior = combineReferenceBehavior;
return this;
}

/// <summary>
/// 配置主域及当前域的加载行为, Default 使用主域引用, Custom 使用当前域引用
/// </summary>
/// <param name="loadBehavior"></param>
/// <returns></returns>
public AssemblyCSharpBuilder CompileWithReferenceLoadBehavior(PluginLoadBehavior loadBehavior)
public AssemblyCSharpBuilder ConfigReferenceLoadBehavior(PluginLoadBehavior loadBehavior)
{
_compileReferenceBehavior = loadBehavior;
return this;
}

/// <summary>
/// 配置当前域程序集的加载行为
/// </summary>
/// <param name="loadBehavior"></param>
/// <returns></returns>
public AssemblyCSharpBuilder CompileWithAssemblyLoadBehavior(PluginLoadBehavior loadBehavior)
public AssemblyCSharpBuilder ConfigAssemblyLoadBehavior(PluginLoadBehavior loadBehavior)
{
_compileAssemblyBehavior = loadBehavior;
return this;
}

public AssemblyCSharpBuilder CompileWithSameNameReferencesFilter(Func<AssemblyName, AssemblyName, AssemblyLoadVersionResult>? useAssemblyNameFunc = null)
/// <summary>
/// 配置引用同名过滤策略
/// </summary>
/// <param name="useAssemblyNameFunc"></param>
/// <returns></returns>
public AssemblyCSharpBuilder ConfigSameNameReferencesFilter(Func<AssemblyName, AssemblyName, AssemblyLoadVersionResult>? useAssemblyNameFunc = null)
{
_referencePickFunc = useAssemblyNameFunc;
return this;
}


public AssemblyCSharpBuilder CompileWithReferencesFilter(Func<IEnumerable<MetadataReference>, IEnumerable<MetadataReference>>? referencesFilter)
/// <summary>
/// 配置引用过滤策略
/// </summary>
/// <param name="referencesFilter"></param>
/// <returns></returns>
public AssemblyCSharpBuilder ConfigReferencesFilter(Func<IEnumerable<MetadataReference>, IEnumerable<MetadataReference>>? referencesFilter)
{
_referencesFilter = referencesFilter;
return this;
Expand Down Expand Up @@ -100,7 +112,7 @@ public CSharpCompilation GetAvailableCompilation(Func<CSharpCompilationOptions,
options = initOptionsFunc(options);
}
IEnumerable<MetadataReference> references;
if (_combineReferences)
if (_combineReferenceBehavior == CombineReferenceBehavior.CombineDefault)
{
references = Domain.GetReferences(_compileReferenceBehavior, _referencePickFunc);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#if MULTI
using System;
using System.IO;

/// <summary>
/// 程序集编译构建器-输出
/// </summary>
public sealed partial class AssemblyCSharpBuilder
{

public AssemblyCSharpBuilder UseNatashaFileOut(string? folder = null)
public AssemblyCSharpBuilder ConfigOutput(string? folder = null)
{
if (folder == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AssemblyCSharpBuilder(string assemblyName)
{
EnableSemanticHandler = true;
_semanticCheckIgnoreAccessibility = true;
_combineReferences = true;
_combineReferenceBehavior = CombineReferenceBehavior.CombineDefault;
_compileReferenceBehavior = PluginLoadBehavior.UseDefault;
_parsingBehavior = UsingLoadBehavior.None;
OutputFolder = GlobalOutputFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
using System.IO;
using System.Reflection;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Text;

[assembly: InternalsVisibleTo("PluginFunctionUT, PublicKey=002400000480000094000000060200000024000052534131000400000100010069acb31dd0d9918441d6ed2b49cd67ae17d15fd6ded4ccd2f99b4a88df8cddacbf72d5897bb54f406b037688d99f482ff1c3088638b95364ef614f01c3f3f2a2a75889aa53286865463fb1803876056c8b98ec57f0b3cf2b1185de63d37041ba08f81ddba0dccf81efcdbdc912032e8d2b0efa21accc96206c386b574b9d9cb8")]
namespace Natasha.CSharp.Component
{

//与元数据相关
//数据值与程序集及内存相关
public sealed class NatashaReferenceCache
Expand Down Expand Up @@ -38,6 +42,7 @@ public void AddReference(AssemblyName assemblyName, MetadataReference reference,
if (assemblyName.CompareWithDefault(oldAssemblyName, loadReferenceBehavior) == AssemblyLoadVersionResult.UseCustomer)
{
_referenceCache!.Remove(oldAssemblyName);
_referenceNameCache!.Remove(name);
}
else
{
Expand Down Expand Up @@ -95,13 +100,14 @@ internal HashSet<MetadataReference> CombineWithDefaultReferences(NatashaReferenc
var sets = new HashSet<MetadataReference>(_referenceCache.Values);
var excludeNods = new HashSet<MetadataReference>();
var defaultReferences = defaultCache._referenceCache;
var defaultNameReferences = defaultCache._referenceNameCache; ;
var defaultNameReferences = defaultCache._referenceNameCache;
if (loadBehavior != PluginLoadBehavior.None || useAssemblyNameFunc != null)
{
foreach (var item in _referenceNameCache)
{
if (defaultNameReferences.TryGetValue(item.Key, out var defaultAssemblyName))
{

AssemblyLoadVersionResult funcResult;
if (useAssemblyNameFunc != null)
{
Expand All @@ -128,7 +134,9 @@ internal HashSet<MetadataReference> CombineWithDefaultReferences(NatashaReferenc
}
}
}
//全部引用
sets.UnionWith(defaultReferences.Values);
//排除不符合的引用
sets.ExceptWith(excludeNods);
return sets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private void NatashaReferenceDomain_LoadAssemblyReferenceWithStream(Assembly ass
}
else
{
DefaultUsing.AddUsing(assembly);
UsingRecorder.Using(assembly);
}
//UsingRecorder.Using(assembly);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private unsafe static ParallelLoopResult InitUsingFromPath(IEnumerable<string> p

});
}
private unsafe static ParallelLoopResult InitReferenceFromPath(IEnumerable<string> paths)
internal unsafe static ParallelLoopResult InitReferenceFromPath(IEnumerable<string> paths)
{
var resolver = new PathAssemblyResolver(paths);
using var mlc = new MetadataLoadContext(resolver);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# if MULTI
using System;

using System.Threading.Tasks;

public static partial class NatashaManagement
{

/// <summary>
/// 获取系统域
/// </summary>
Expand Down Expand Up @@ -47,6 +46,11 @@ public static bool AddGlobalReference(Type type, PluginLoadBehavior loadBehavior
return true;
}

public static ParallelLoopResult AddGlobalReference(params string[] filePath)
{
return NatashaInitializer.InitReferenceFromPath(filePath);
}

/// <summary>
/// 移除元数据引用,编译需要元数据支持.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;</TargetFrameworks>
<Description>Natasha 的 C# 版编译器</Description>
<PackageId>DotNetCore.Natasha.CSharp.Compiler</PackageId>
<PackageReleaseNotes>升级到最新版.</PackageReleaseNotes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public AssemblyCSharpBuilder SetAssemblyName(string asmName)
AssemblyName = asmName;
return this;
}

public AssemblyCSharpBuilder ConfigOutput(string dllFilePath, string pdbFilePath, string? xmlFilePath = null)
{
DllFilePath = dllFilePath;
PdbFilePath = pdbFilePath;
if (xmlFilePath != null)
{
XmlFilePath = xmlFilePath;
}
return this;
}
#endregion

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public enum CombineReferenceBehavior
{
CombineDefault,
UseCurrent
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if MULTI
using System.Diagnostics;
using System.Text;

namespace Natasha.CSharp.Template
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;net7.0;</TargetFrameworks>
<Description>Natasha 的编译模板</Description>
<PackageId>DotNetCore.Natasha.CSharp.Template</PackageId>
<PackageReleaseNotes>升级到最新版.</PackageReleaseNotes>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;


internal static class StopwatchExtension
{
private static readonly ConcurrentDictionary<ScoreRange, ConsoleColor> _colorCache;
static StopwatchExtension()
{
_colorCache = new ConcurrentDictionary<ScoreRange, ConsoleColor>();
_colorCache[new ScoreRange(0, 20)] = ConsoleColor.Green;
_colorCache[new ScoreRange(20, 100)] = ConsoleColor.Cyan;
_colorCache[new ScoreRange(100, 500)] = ConsoleColor.Yellow;
_colorCache[new ScoreRange(500, 1000)] = ConsoleColor.Magenta;
_colorCache[new ScoreRange(1000, 100000)] = ConsoleColor.Red;
}
internal static void StopAndShowCategoreInfo(this Stopwatch stopwatch, string nodeName, string info, int level)
{
stopwatch.Stop();
ShowCategoreInfo(stopwatch, nodeName, info, level);
}


internal static void RestartAndShowCategoreInfo(this Stopwatch stopwatch, string nodeName, string info, int level)
{
stopwatch.Stop();
ShowCategoreInfo(stopwatch, nodeName, info, level);
stopwatch.Restart();
}



internal static void ShowCategoreInfo(Stopwatch stopwatch, string nodeName, string info, int level)
{
var color = Console.ForegroundColor;
foreach (var item in _colorCache)
{
if (item.Key.IsInRange(stopwatch.ElapsedMilliseconds))
{
Console.ForegroundColor = item.Value;
for (int i = 0; i < level; i += 1)
{
Console.Write("\t");
}
Console.WriteLine($"---{nodeName}\t{info} : {stopwatch.ElapsedMilliseconds}ms");
}
}
Console.ForegroundColor = color;
}



/// <summary>
/// 设置颜色等级
/// </summary>
/// <param name="stopwatch"></param>
/// <param name="scoreRange"></param>
/// <param name="color"></param>
/// <returns></returns>
internal static Stopwatch SetLevel(this Stopwatch stopwatch, ScoreRange scoreRange, ConsoleColor color)
{
_colorCache[scoreRange] = color;
return stopwatch;
}
}

/// <summary>
/// 分数模型
/// </summary>
internal class ScoreRange
{
private readonly long _min;
private readonly long _max;

internal ScoreRange(long min, long max)
{
_min = min;
_max = max;
}
/// <summary>
/// 判断得分是否在范围内
/// </summary>
/// <param name="score"></param>
/// <returns></returns>
internal bool IsInRange(long score)
{
return _min <= score && score <= _max;
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Natasha.CSharp.Template
using Natasha.CSharp.Template;

namespace Natasha.CSharp
{

public static class OopDefinedTypeExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Runtime.CompilerServices;

namespace Natasha.CSharp.Template
{
Expand Down
Loading

0 comments on commit 72f546d

Please sign in to comment.