-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuaAsset.cs
106 lines (89 loc) · 2.84 KB
/
LuaAsset.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Minity.XLuaTools
{
public class LuaAsset : ScriptableObject
{
private static readonly Dictionary<string, LuaAsset> substitutions = new();
private static readonly Dictionary<string, List<LuaAsset>> activeLuaAssets = new();
[SerializeField]
private string Guid;
[SerializeField, HideInInspector]
private string _code;
[NonSerialized]
private string _substitution;
[NonSerialized]
private bool _isSubstitution = false;
internal event Action ReloadEvent;
public string Code => string.IsNullOrEmpty(_substitution) ? _code : _substitution;
private void OnEnable()
{
if (string.IsNullOrEmpty(Guid))
{
return;
}
if (substitutions.TryGetValue(Guid, out var substitution))
{
_substitution = substitution._code;
}
if (!activeLuaAssets.ContainsKey(Guid))
{
activeLuaAssets.Add(Guid, new List<LuaAsset>());
}
activeLuaAssets[Guid].Add(this);
}
private void OnDestroy()
{
if (string.IsNullOrEmpty(Guid))
{
return;
}
if (activeLuaAssets.TryGetValue(Guid, out var assetList))
{
assetList.Remove(this);
}
}
private void OnValidate()
{
#if UNITY_EDITOR
if (EditorApplication.isPlaying)
{
Debug.Log($"Code '{name}' has been updated and reloaded.");
}
#endif
ReloadEvent?.Invoke();
}
public void ApplySubstitution()
{
_isSubstitution = true;
if (!substitutions.TryAdd(Guid, this))
{
substitutions[Guid] = this;
}
if (activeLuaAssets.TryGetValue(Guid, out var assetList))
{
foreach (var asset in assetList)
{
if (asset._isSubstitution)
{
continue;
}
asset._substitution = _code;
asset.ReloadEvent?.Invoke();
}
}
}
public static LuaAsset Create(string code, string guid)
{
var asset = CreateInstance<LuaAsset>();
asset._code = code;
asset.Guid = guid;
return asset;
}
}
}