DragonECS-Unity/src/Templates/EcsPipelineTemplate/PipelineTemplateUtility.cs

114 lines
3.7 KiB
C#
Raw Normal View History

2024-06-30 01:12:21 +08:00
using DCFApixels.DragonECS.Unity.Internal;
using System;
using System.Collections.Generic;
using System.Linq;
2024-06-26 01:35:57 +08:00
using UnityEngine;
2024-10-18 17:22:02 +08:00
using static DCFApixels.DragonECS.Unity.PipelineTemplateUtility;
2024-06-26 01:35:57 +08:00
2024-10-18 17:22:02 +08:00
namespace DCFApixels.DragonECS.Unity
2024-06-26 01:35:57 +08:00
{
2024-10-18 17:22:02 +08:00
public interface IPipelineTemplate
2024-06-26 01:35:57 +08:00
{
2024-10-18 17:22:02 +08:00
ReadOnlySpan<string> Layers { get; }
ReadOnlySpan<Record> Records { get; }
bool Validate();
}
public static class PipelineTemplateUtility
{
internal static readonly string[] DefaultLayers = new string[]
2024-06-30 01:12:21 +08:00
{
EcsConsts.PRE_BEGIN_LAYER,
EcsConsts.BEGIN_LAYER,
EcsConsts.BASIC_LAYER,
EcsConsts.END_LAYER,
EcsConsts.POST_END_LAYER,
};
2024-10-18 17:22:02 +08:00
internal static bool ValidateLayers(ref string[] layers)
2024-06-30 01:12:21 +08:00
{
bool result = false;
2024-06-30 01:12:21 +08:00
Dictionary<string, int> builtinLayerIndexes = new Dictionary<string, int>();
2024-10-18 17:22:02 +08:00
foreach (var item in DefaultLayers)
2024-06-30 01:12:21 +08:00
{
builtinLayerIndexes.Add(item, -1);
}
2024-10-18 17:22:02 +08:00
List<string> newLayers = layers.Distinct().ToList();
2024-06-30 01:12:21 +08:00
for (int i = 0; i < newLayers.Count; i++)
{
var layer = newLayers[i];
if (builtinLayerIndexes.ContainsKey(layer))
{
builtinLayerIndexes[layer] = i;
}
}
int lastNewLayersCount = newLayers.Count;
foreach (var pair in builtinLayerIndexes)
{
2024-09-10 19:13:31 +08:00
if (pair.Value < 0)
2024-06-30 01:12:21 +08:00
{
newLayers.Add(pair.Key);
}
}
for (int i = lastNewLayersCount; i < newLayers.Count; i++)
{
builtinLayerIndexes[newLayers[i]] = i;
}
{
int i = 0;
foreach (var pair in builtinLayerIndexes.OrderBy(o => o.Value))
{
2024-10-18 17:22:02 +08:00
if (newLayers[pair.Value] != DefaultLayers[i])
{
2024-10-18 17:22:02 +08:00
newLayers[pair.Value] = DefaultLayers[i];
result = true;
}
2024-06-30 01:12:21 +08:00
i++;
}
}
2024-10-18 17:22:02 +08:00
layers = newLayers.ToArray();
return result;
2024-06-30 01:12:21 +08:00
}
2024-10-18 17:22:02 +08:00
internal static bool ValidateRecords(ref Record[] records)
2024-06-30 01:12:21 +08:00
{
return false;
2024-06-30 01:12:21 +08:00
}
2024-10-18 17:22:02 +08:00
public static EcsPipelineTemplate GenerateSerializableTemplate(this IPipelineTemplate self)
{
EcsPipelineTemplate result = new EcsPipelineTemplate();
result.layers = new string[self.Layers.Length];
for (int i = 0; i < self.Layers.Length; i++)
{
result.layers[i] = self.Layers[i];
}
result.records = new EcsPipelineTemplate.Record[self.Records.Length];
for (int i = 0; i < result.records.Length; i++)
{
var s = self.Records[i];
result.records[i] = new EcsPipelineTemplate.Record(s.target, s.parameters);
}
return result;
}
2024-06-30 01:12:21 +08:00
[Serializable]
2024-09-08 21:36:24 +08:00
public struct Record
2024-06-26 01:35:57 +08:00
{
[SerializeReference]
[ReferenceButton(true, typeof(IEcsModule), typeof(IEcsProcess))]
2024-09-16 19:31:01 +08:00
[ArrayElement]
public object target;// нельзя менять поярдок полей, иначе это поломает отрисовку в инспекторе изза применения property.Next(bool);
2024-09-08 21:36:24 +08:00
public AddParams parameters;
public Record(object target, AddParams parameters)
2024-06-26 01:35:57 +08:00
{
2024-09-08 21:36:24 +08:00
this.target = target;
this.parameters = parameters;
2024-06-26 01:35:57 +08:00
}
}
}
2024-10-18 17:22:02 +08:00
}