From a5277126e70000c4760c030d96ff2c2489dc2a65 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:10:06 +0800 Subject: [PATCH] remove recovery reference utils --- src/Editor/ReferenceButtonAttribute.cs | 20 -- src/RecoveryReferences.meta | 8 - src/RecoveryReferences/UnityEditorCache.cs | 309 ------------------ .../UnityEditorCache.cs.meta | 11 - .../EcsPipelineTemplateSO.cs | 102 ------ .../Templates/MonoEntityTemplate.cs | 108 ------ src/Utils/Reference.cs | 116 ------- src/Utils/Reference.cs.meta | 11 - 8 files changed, 685 deletions(-) delete mode 100644 src/RecoveryReferences.meta delete mode 100644 src/RecoveryReferences/UnityEditorCache.cs delete mode 100644 src/RecoveryReferences/UnityEditorCache.cs.meta delete mode 100644 src/Utils/Reference.cs delete mode 100644 src/Utils/Reference.cs.meta diff --git a/src/Editor/ReferenceButtonAttribute.cs b/src/Editor/ReferenceButtonAttribute.cs index f33e9b0..84877c0 100644 --- a/src/Editor/ReferenceButtonAttribute.cs +++ b/src/Editor/ReferenceButtonAttribute.cs @@ -30,7 +30,6 @@ namespace DCFApixels.DragonECS.Unity.Editors { protected override void OnInit() { - Type referenceBaseType = typeof(Reference<>); Type fieldType = fieldInfo.FieldType; if (fieldType.IsGenericType) { @@ -38,25 +37,12 @@ namespace DCFApixels.DragonECS.Unity.Editors { fieldType = fieldType.GetGenericTypeDefinition(); } - - if (fieldType == referenceBaseType) - { - _isReferenceWrapper = true; - return; - } } - _isReferenceWrapper = false; } - private bool _isReferenceWrapper = false; - public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { Init(); - if (_isReferenceWrapper) - { - property.Next(true); - } if (property.managedReferenceValue != null) { return EditorGUI.GetPropertyHeight(property, label, true); @@ -69,12 +55,6 @@ namespace DCFApixels.DragonECS.Unity.Editors protected override void DrawCustom(Rect position, SerializedProperty property, GUIContent label) { - if (_isReferenceWrapper) - { - property.Next(true); - label.text = property.displayName; - } - if (IsArrayElement) { label = UnityEditorUtility.GetLabelTemp(); diff --git a/src/RecoveryReferences.meta b/src/RecoveryReferences.meta deleted file mode 100644 index 4bd2ccd..0000000 --- a/src/RecoveryReferences.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 6a95e563d250e80489b71ecdd0392cf0 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/RecoveryReferences/UnityEditorCache.cs b/src/RecoveryReferences/UnityEditorCache.cs deleted file mode 100644 index 46edba3..0000000 --- a/src/RecoveryReferences/UnityEditorCache.cs +++ /dev/null @@ -1,309 +0,0 @@ -#if UNITY_EDITOR -using System; -using System.IO; -using UnityEditor; -using UnityEngine; - -namespace DCFApixels.DragonECS.Unity.Internal -{ - [Serializable] - internal class UnityEditorCacheBlock - { - [SerializeField] - private int _id; - - [SerializeField] - private int _startIndex; - - [SerializeField] - private string[] _jsons = new string[256]; - [SerializeField] - private byte _jsonsCount; - - [SerializeField] - private byte[] _recycledID = new byte[256]; - [SerializeField] - private byte _recycledIDCount = 0; - - [SerializeField] - private int _fullLength = 0; - - private UnityEditorCacheBlock() { } - public UnityEditorCacheBlock(int id) - { - _id = id; - } - - public int StartIndex - { - get { return _startIndex; } - } - public int Count - { - get { return _jsonsCount; } - } - public int FullLength - { - get { return _fullLength; } - } - - public void Set(string json, ref int id) - { - const int MASK = ~(byte.MaxValue); - byte slotID = (byte)id; - if (slotID == 0) - { - if (_recycledIDCount > 0) - { - id = _recycledID[_recycledIDCount--]; - } - else - { - id = _jsonsCount++; - } - id = (id & MASK) | slotID; - } - slotID--; - - string oldJson = _jsons[slotID]; - if (oldJson != null) - { - _fullLength -= oldJson.Length; - } - _fullLength += json.Length; - _jsons[slotID] = json; - } - public string Get(int id) - { - byte slotID = (byte)id; - _recycledID[_recycledIDCount++] = slotID; - return _jsons[slotID]; - } - - public bool IsValide() - { - return - _jsons != null && - _recycledID != null && - _jsons.Length == 256 && - _recycledID.Length == 256; - } - public void Reset() - { - _jsons = new string[256]; - _recycledID = new byte[256]; - _jsonsCount = 0; - _recycledIDCount = 0; - } - } - - [FilePath(_CACHE_BLOCKS_FOLDER + ".prefs", FilePathAttribute.Location.ProjectFolder)] - public class UnityEditorCache : ScriptableSingleton, ISerializationCallbackReceiver - { - private const string _CACHE_BLOCKS_FOLDER = EcsUnityConsts.LOCAL_CACHE_FOLDER + "/" + nameof(UnityEditorCache); - private const int _THRESHOLD_LENGTH = 2048; - - private static bool _isInit; - private static string _projectPath; - private static string _cachePath; - - private static void Init() - { - if (_isInit) { return; } - _projectPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')); - _cachePath = _projectPath + _CACHE_BLOCKS_FOLDER; - _isInit = true; - } - - [SerializeField] - private string[] _blockNames = new string[512]; - [SerializeField] - private int _blockNamesCount = 0; - - [SerializeField] - private int[] _recycledID = new int[512]; - [SerializeField] - private int _recycledIDCount = 0; - - public void Set(T data, ref int id) - { - Init(); - //try - { - int blockID; - if (id == -1) - { - if (_recycledIDCount > 0) - { - blockID = _recycledID[_recycledIDCount--]; - } - else - { - if (_blockNamesCount >= _blockNames.Length) - { - Array.Resize(ref _blockNames, _blockNamesCount << 1); - } - blockID = _blockNamesCount++; - } - id = blockID << 8; - } - else - { - blockID = (id >> 8) & 16_777_215; - if (blockID >= _blockNames.Length) - { - Array.Resize(ref _blockNames, blockID << 1); - } - } - - ref string blockName = ref _blockNames[blockID]; - string filePath; - if (string.IsNullOrEmpty(blockName)) - { - blockName = blockID.ToString(); - - filePath = Path.Combine(_cachePath, blockName); - string directoryName = _cachePath; - if (!Directory.Exists(directoryName)) - { - Directory.CreateDirectory(directoryName); - } - } - else - { - filePath = Path.Combine(_cachePath, blockName); - } - - var json = JsonUtility.ToJson(data); - UnityEditorCacheBlock block; - if (File.Exists(filePath)) - { - block = JsonUtility.FromJson(File.ReadAllText(filePath)); - } - else - { - block = new UnityEditorCacheBlock(blockID); - } - block.Set(json, ref id); - File.WriteAllText(filePath, JsonUtility.ToJson(block)); - - if (block.FullLength < _THRESHOLD_LENGTH) - { - if (_recycledIDCount >= _recycledID.Length) - { - Array.Resize(ref _recycledID, _recycledIDCount == 0 ? 64 : _recycledIDCount << 1); - } - _recycledID[_recycledIDCount++] = blockID; - } - Save(true); - } - //catch (Exception e) - //{ - // Reset(); - // throw e; - //} - } - public T Get(ref int id) - { - Init(); - //try - { - int blockID = (id >> 8) & 16_777_215; - id = -1; - - ref string blockName = ref _blockNames[blockID]; - string filePath; - if (string.IsNullOrEmpty(blockName)) - { - blockName = blockID.ToString(); - - filePath = Path.Combine(_cachePath, blockName); - string directoryName = _cachePath; - if (!Directory.Exists(directoryName)) - { - Directory.CreateDirectory(directoryName); - } - } - else - { - filePath = Path.Combine(_cachePath, blockName); - } - - UnityEditorCacheBlock block; - if (File.Exists(filePath)) - { - block = JsonUtility.FromJson(File.ReadAllText(filePath)); - } - else - { - block = new UnityEditorCacheBlock(blockID); - } - - string json = block.Get(id); - - - - if (block.FullLength < _THRESHOLD_LENGTH) - { - if (_recycledIDCount >= _recycledID.Length) - { - Array.Resize(ref _recycledID, _recycledIDCount == 0 ? 64 : _recycledIDCount << 1); - } - _recycledID[_recycledIDCount++] = blockID; - } - - - Save(true); - return json == null ? default : JsonUtility.FromJson(json); - } - //catch (Exception e) - //{ - // Reset(); - // throw e; - //} - } - - public void Save() - { - Save(true); - EcsDebug.PrintPass("Save Save Save"); - } - - private void Reset() - { - _blockNames = new string[512]; - _blockNamesCount = 0; - _recycledID = new int[512]; - _recycledIDCount = 0; - Save(true); - } - - void ISerializationCallbackReceiver.OnBeforeSerialize() - { - } - void ISerializationCallbackReceiver.OnAfterDeserialize() - { - } - - - - - #region TODO - //public int SetUnityObject(UnityObject obj) - //{ - // if (_jsonDatasCount >= _jsonDatas.Length) - // { - // Array.Resize(ref _jsonDatas, _jsonDatas.Length << 1); - // } - // - // _jsonDatas[_jsonDatasCount] = JsonUtility.ToJson(obj); - // return _jsonDatasCount++; - //} - //public void GetOverwriteUnityObject(UnityObject obj, int id) - //{ - // JsonUtility.FromJsonOverwrite(_jsonDatas[id], obj); - //} - #endregion - } -} -#endif \ No newline at end of file diff --git a/src/RecoveryReferences/UnityEditorCache.cs.meta b/src/RecoveryReferences/UnityEditorCache.cs.meta deleted file mode 100644 index 83f80b3..0000000 --- a/src/RecoveryReferences/UnityEditorCache.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 839f12827a9b59d4ea2cb945a98f3e6e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/Templates/EcsPipelineTemplate/EcsPipelineTemplateSO.cs b/src/Templates/EcsPipelineTemplate/EcsPipelineTemplateSO.cs index fc7038b..968dd4f 100644 --- a/src/Templates/EcsPipelineTemplate/EcsPipelineTemplateSO.cs +++ b/src/Templates/EcsPipelineTemplate/EcsPipelineTemplateSO.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Linq; -using UnityEditor; using UnityEngine; namespace DCFApixels.DragonECS @@ -147,106 +146,5 @@ namespace DCFApixels.DragonECS this.parameters = parameters; } } - - - - - - [SerializeField] - //[HideInInspector] - private int _saveID; - - [Serializable] - private struct SavedRecordList - { - public SavedRecord[] records; - public SavedRecordList(SavedRecord[] records) - { - this.records = records; - } - } - [Serializable] - private struct SavedRecord - { - public int index; - public string metaID; - public string recordJson; - public SavedRecord(int index, string metaID, string recordJson) - { - this.index = index; - this.metaID = metaID; - this.recordJson = recordJson; - } - } - - private void Awake() - { - Load(); - } - - private void Load() - { -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - if (_saveID != -1) - { - bool isChanged = false; - var savedRecords = UnityEditorCache.instance.Get(ref _saveID).records; - if (savedRecords != null) - { - for (int i = 0; i < savedRecords.Length; i++) - { - ref var savedRecord = ref savedRecords[i]; - if (savedRecord.index < _records.Length) - { - ref var record = ref _records[savedRecord.index]; - if (record.target == null && string.IsNullOrEmpty(savedRecord.metaID) == false) - { - record.target = RecoveryReferenceUtility.TryRecoverReference(savedRecord.metaID); - if (record.target == null) { return; } - JsonUtility.FromJsonOverwrite(savedRecord.recordJson, record.target); - isChanged = true; - } - } - } - } - if (isChanged) - { - EditorUtility.SetDirty(this); - } - } -#endif - } - private void Save() - { -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - if (_saveID == -1 || EditorApplication.isPlaying == false) - { - SavedRecord[] savedRecords = new SavedRecord[_records.Length]; - SavedRecordList list = new SavedRecordList(savedRecords); - for (int i = 0; i < _records.Length; i++) - { - ref var record = ref _records[i]; - string metaid = record.target.GetMeta().MetaID; - if (string.IsNullOrEmpty(metaid) == false) - { - savedRecords[i] = new SavedRecord(i, metaid, JsonUtility.ToJson(record.target)); - } - else - { - savedRecords[i] = default; - } - } - UnityEditorCache.instance.Set(list, ref _saveID); - //RecoveryReferencesCache.instance.Save(); - } -#endif - } - -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - private void OnValidate() - { - Save(); - } -#endif } } \ No newline at end of file diff --git a/src/Templates/EntityTemplate/Templates/MonoEntityTemplate.cs b/src/Templates/EntityTemplate/Templates/MonoEntityTemplate.cs index 65f538a..0b8ca84 100644 --- a/src/Templates/EntityTemplate/Templates/MonoEntityTemplate.cs +++ b/src/Templates/EntityTemplate/Templates/MonoEntityTemplate.cs @@ -1,121 +1,13 @@ using DCFApixels.DragonECS.Unity; using DCFApixels.DragonECS.Unity.Internal; using System; -using System.Collections.Generic; using UnityEngine; -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY -using UnityEditor; -#endif namespace DCFApixels.DragonECS { public abstract class MonoEntityTemplateBase : MonoBehaviour, ITemplate { - [SerializeField] - private int _saveID; public abstract void Apply(short worldID, int entityID); - - private static IComponentTemplate _fake = null; - protected virtual IList GetToRecover() { return null; } - protected virtual ref IComponentTemplate GetToRecoverSingle() { return ref _fake; } - - [Serializable] - private struct SavedRecordList - { - public SavedRecord[] records; - public SavedRecord singleRecord; - public SavedRecordList(SavedRecord[] templates) - { - this.records = templates; - singleRecord = default; - } - } - [Serializable] - private struct SavedRecord - { - public int index; - public string metaID; - public string recordJson; - public SavedRecord(int index, string metaID, string recordJson) - { - this.index = index; - this.metaID = metaID; - this.recordJson = recordJson; - } - } - protected void Save() - { -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - if (_saveID == -1 || EditorApplication.isPlaying == false) - { - var records = GetToRecover(); - var recordSingle = GetToRecoverSingle(); - - SavedRecord[] savedRecords = new SavedRecord[records.Count]; - SavedRecordList list = new SavedRecordList(savedRecords); - - for (int i = 0; i < records.Count; i++) - { - var record = records[i]; - string metaid = record.GetMeta().MetaID; - if (string.IsNullOrEmpty(metaid) == false) - { - savedRecords[i] = new SavedRecord(i, metaid, JsonUtility.ToJson(record)); - } - else - { - savedRecords[i] = default; - } - } - if (recordSingle != null) - { - string metaid = recordSingle.GetMeta().MetaID; - list.singleRecord = new SavedRecord(-1, metaid, JsonUtility.ToJson(recordSingle)); - } - UnityEditorCache.instance.Set(list, ref _saveID); - } -#endif - } - protected void Load() - { -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - if (_saveID != -1) - { - bool isChanged = false; - SavedRecordList list = UnityEditorCache.instance.Get(ref _saveID); - var savedRecords = list.records; - if (savedRecords != null) - { - var records = GetToRecover(); - for (int i = 0; i < savedRecords.Length; i++) - { - ref var savedRecord = ref savedRecords[i]; - if (savedRecord.index < records.Count) - { - var record = records[savedRecord.index]; - if (record == null && string.IsNullOrEmpty(savedRecord.metaID) == false) - { - record = RecoveryReferenceUtility.TryRecoverReference(savedRecord.metaID); - records[savedRecord.index] = record; - if (record == null) { return; } - JsonUtility.FromJsonOverwrite(savedRecord.recordJson, record); - isChanged = true; - } - } - } - } - ref var recordSingle = ref GetToRecoverSingle(); - if (string.IsNullOrEmpty(list.singleRecord.metaID)) - { - - } - if (isChanged) - { - EditorUtility.SetDirty(this); - } - } -#endif - } } [DisallowMultipleComponent] diff --git a/src/Utils/Reference.cs b/src/Utils/Reference.cs deleted file mode 100644 index 0807244..0000000 --- a/src/Utils/Reference.cs +++ /dev/null @@ -1,116 +0,0 @@ -//#define DISABLE_SERIALIZE_REFERENCE_RECOVERY -using System; -using System.Runtime.CompilerServices; -using UnityEngine; - -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY -using DCFApixels.DragonECS.Unity.Internal; -using System.Collections.Generic; -using UnityEditor; -#endif - -namespace DCFApixels.DragonECS -{ -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - [InitializeOnLoad] - internal static class RecoveryReferenceUtility - { - internal static bool _recompileAfterInitializationScope = false; - private static Dictionary _metaIDTypePairs; - - static RecoveryReferenceUtility() - { - _recompileAfterInitializationScope = true; - EditorApplication.update += BeforeCompilation; - - } - private static void BeforeCompilation() - { - _recompileAfterInitializationScope = false; - EditorApplication.update -= BeforeCompilation; - } - - - private static void InitRecoverCache() - { - if (_metaIDTypePairs != null) { return; } - _metaIDTypePairs = new Dictionary(); - - List types = new List(); - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - foreach (var type in assembly.GetTypes()) - { - if (type.TryGetAttribute(out MetaIDAttribute atr)) - { - _metaIDTypePairs.Add(atr.ID, type); - } - } - } - } - internal static T TryRecoverReference(string metaID) - { - InitRecoverCache(); - if (_metaIDTypePairs.TryGetValue(metaID, out Type type)) - { - return (T)Activator.CreateInstance(type); - } - return default; - } - } -#endif - - [Serializable] - public struct Reference -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - : ISerializationCallbackReceiver -#endif - { -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - [SerializeReference] - private T _value; - [SerializeField] - private string _json; - public T Value - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _value; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - set { _value = value; } - } -#else - [SerializeField] - public T Value; -#endif - public bool IsNull - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return Value == null; } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator T(Reference a) { return a.Value; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Reference(T a) { return new Reference() { Value = a }; } - -#if UNITY_EDITOR && !DISABLE_SERIALIZE_REFERENCE_RECOVERY - void ISerializationCallbackReceiver.OnAfterDeserialize() - { - if (_value == null && RecoveryReferenceUtility._recompileAfterInitializationScope && string.IsNullOrEmpty(_json) == false) - { - int indexof = _json.IndexOf(','); - _value = RecoveryReferenceUtility.TryRecoverReference(_json.Substring(0, indexof)); - if (_value == null) { return; } - JsonUtility.FromJsonOverwrite(_json.Substring(indexof + 1), _value); - } - _json = null; - } - void ISerializationCallbackReceiver.OnBeforeSerialize() - { - if (_value != null && EditorApplication.isPlaying == false) - { - _json = $"{_value.GetMeta().MetaID},{JsonUtility.ToJson(_value)}"; - } - } -#endif - } -} \ No newline at end of file diff --git a/src/Utils/Reference.cs.meta b/src/Utils/Reference.cs.meta deleted file mode 100644 index 783b168..0000000 --- a/src/Utils/Reference.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 6399ac2dd5218444b98f4dcf3a359611 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: