2024-09-29 15:59:14 +08:00
|
|
|
|
#if UNITY_EDITOR
|
2024-09-29 23:31:12 +08:00
|
|
|
|
using DCFApixels.DragonECS.Unity.Editors;
|
|
|
|
|
using DCFApixels.DragonECS.Unity.RefRepairer.Internal;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2024-09-29 23:31:12 +08:00
|
|
|
|
namespace DCFApixels.DragonECS.Unity.RefRepairer.Editors
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
[InitializeOnLoad]
|
2024-09-29 15:59:14 +08:00
|
|
|
|
[FilePath(EcsUnityConsts.LOCAL_CACHE_FOLDER + "/" + nameof(MetaIDRegistry) + ".prefs", FilePathAttribute.Location.ProjectFolder)]
|
|
|
|
|
internal class MetaIDRegistry : ScriptableSingleton<MetaIDRegistry>, ISerializationCallbackReceiver
|
|
|
|
|
{
|
|
|
|
|
#region [SerializeField]
|
2024-09-29 23:31:12 +08:00
|
|
|
|
[Serializable]
|
2024-09-29 15:59:14 +08:00
|
|
|
|
private struct Pair
|
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
public TypeDataSerializable key;
|
|
|
|
|
public string value;
|
|
|
|
|
public Pair(TypeDataSerializable key, string value)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
this.key = key;
|
|
|
|
|
this.value = value;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-29 23:31:12 +08:00
|
|
|
|
[SerializeField]
|
|
|
|
|
private Pair[] _typeKeyMetaIDPairsSerializable;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
#endregion
|
2024-09-29 23:31:12 +08:00
|
|
|
|
private Dictionary<TypeData, string> _typeKeyMetaIDPairs = new Dictionary<TypeData, string>();
|
|
|
|
|
private bool _isChanged = false;
|
|
|
|
|
|
|
|
|
|
public bool TryGetMetaID(TypeData key, out string metaID)
|
|
|
|
|
{
|
|
|
|
|
return _typeKeyMetaIDPairs.TryGetValue(key, out metaID);
|
|
|
|
|
}
|
2024-09-29 15:59:14 +08:00
|
|
|
|
|
|
|
|
|
|
2024-09-29 23:31:12 +08:00
|
|
|
|
static MetaIDRegistry()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.update += BeforeCompilation;
|
|
|
|
|
}
|
|
|
|
|
private static void BeforeCompilation()
|
|
|
|
|
{
|
|
|
|
|
EditorApplication.update -= BeforeCompilation;
|
|
|
|
|
instance.TryGetMetaID(default, out _);
|
|
|
|
|
instance.Update();
|
|
|
|
|
}
|
2024-09-29 15:59:14 +08:00
|
|
|
|
|
|
|
|
|
#region Update
|
2024-09-29 23:31:12 +08:00
|
|
|
|
public void Reinit()
|
|
|
|
|
{
|
|
|
|
|
_typeKeyMetaIDPairs.Clear();
|
|
|
|
|
Update();
|
|
|
|
|
}
|
2024-09-29 15:59:14 +08:00
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
var typeMetas = UnityEditorUtility._serializableTypeWithMetaIDMetas;
|
|
|
|
|
foreach (var meta in typeMetas)
|
|
|
|
|
{
|
2024-10-01 09:09:40 +08:00
|
|
|
|
var key = new TypeData(meta.Type);
|
2024-09-29 23:31:12 +08:00
|
|
|
|
var metaID = meta.MetaID;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
|
2024-09-30 19:40:50 +08:00
|
|
|
|
//Debug.LogWarning(type + " " + metaID);
|
|
|
|
|
|
|
|
|
|
if (_typeKeyMetaIDPairs.TryGetValue(key, out string keyMetaID))
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
if (keyMetaID != metaID)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairs[key] = null; //Таким образом помечаются моменты когда не однозначно какой идентификатор принадлежит этому имени
|
|
|
|
|
_isChanged = true;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-29 23:31:12 +08:00
|
|
|
|
else
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairs[key] = metaID;
|
|
|
|
|
_isChanged = true;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-29 23:31:12 +08:00
|
|
|
|
if (_isChanged)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_isChanged = false;
|
2024-09-30 19:40:50 +08:00
|
|
|
|
EditorUtility.SetDirty(this);
|
2024-09-29 23:31:12 +08:00
|
|
|
|
Save(true);
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ISerializationCallbackReceiver
|
|
|
|
|
void ISerializationCallbackReceiver.OnAfterDeserialize()
|
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairs.Clear();
|
|
|
|
|
foreach (var pair in _typeKeyMetaIDPairsSerializable)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
if (string.IsNullOrEmpty(pair.value) == false)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairs[pair.key] = pair.value;
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void ISerializationCallbackReceiver.OnBeforeSerialize()
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairsSerializable = new Pair[_typeKeyMetaIDPairs.Count];
|
|
|
|
|
foreach (var pair in _typeKeyMetaIDPairs)
|
2024-09-29 15:59:14 +08:00
|
|
|
|
{
|
2024-09-29 23:31:12 +08:00
|
|
|
|
_typeKeyMetaIDPairsSerializable[i++] = new Pair(pair.Key, pair.Value);
|
2024-09-29 15:59:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|