Skip to content

Commit 7a497ad

Browse files
committed
First commit.
0 parents  commit 7a497ad

File tree

109 files changed

+6915
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+6915
-0
lines changed

.gitignore

Whitespace-only changes.

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2023-08-20
4+
5+
- Release

CHANGELOG.md.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ChenPipi.CodeExecutor.Editor",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
6+
"GUID:2bafac87e7f4b9b418d9448d219b01ab"
7+
],
8+
"includePlatforms": [
9+
"Editor"
10+
],
11+
"excludePlatforms": [],
12+
"allowUnsafeCode": false,
13+
"overrideReferences": false,
14+
"precompiledReferences": [],
15+
"autoReferenced": true,
16+
"defineConstraints": [],
17+
"versionDefines": [],
18+
"noEngineReferences": false
19+
}

Editor/ChenPipi.CodeExecutor.Editor.asmdef.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Scripts.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Scripts/Attributes.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using JetBrains.Annotations;
5+
using UnityEditor;
6+
7+
namespace ChenPipi.CodeExecutor.Editor
8+
{
9+
10+
/// <summary>
11+
/// 用于注册 Code Executor 执行模式的 Attribute,提供了控制函数执行顺序的能力,来达到控制执行模式注册顺序的目的
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.Method)]
14+
[MeansImplicitUse]
15+
public class CodeExecutorRegistrationAttribute : Attribute
16+
{
17+
18+
/// <summary>
19+
/// 执行顺序(该值越小则函数越先执行)
20+
/// </summary>
21+
public int order { get; }
22+
23+
/// <param name="order">执行顺序(该值越小则函数越先执行)</param>
24+
public CodeExecutorRegistrationAttribute(int order = 10)
25+
{
26+
this.order = order;
27+
}
28+
29+
/// <summary>
30+
/// 注册器
31+
/// </summary>
32+
[InitializeOnLoadMethod]
33+
private static void Registrar()
34+
{
35+
List<(MethodInfo method, int order)> list = new List<(MethodInfo method, int order)>();
36+
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
37+
{
38+
foreach (Type type in assembly.GetTypes())
39+
{
40+
foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
41+
{
42+
object[] attributes = method.GetCustomAttributes(typeof(CodeExecutorRegistrationAttribute), false);
43+
if (attributes.Length == 0)
44+
{
45+
continue;
46+
}
47+
CodeExecutorRegistrationAttribute attribute = attributes[0] as CodeExecutorRegistrationAttribute;
48+
list.Add((method, attribute!.order));
49+
}
50+
}
51+
}
52+
53+
list.Sort((a, b) => a.order - b.order);
54+
55+
foreach ((MethodInfo method, int order) item in list)
56+
{
57+
item.method.Invoke(null, null);
58+
}
59+
}
60+
61+
}
62+
63+
}

Editor/Scripts/Attributes/CodeExecutorRegistrationAttribute.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/Scripts/CodeExecutorData.cs

+251
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace ChenPipi.CodeExecutor.Editor
6+
{
7+
8+
/// <summary>
9+
/// 数据
10+
/// </summary>
11+
public static class CodeExecutorData
12+
{
13+
14+
#region UserData
15+
16+
[Serializable]
17+
private class UserData
18+
{
19+
public int version = 0;
20+
public SnippetInfo newSnippet = new SnippetInfo();
21+
public List<SnippetInfo> snippets = new List<SnippetInfo>();
22+
}
23+
24+
private static UserData s_UserData;
25+
26+
private static UserData userData
27+
{
28+
get
29+
{
30+
if (s_UserData == null)
31+
{
32+
s_UserData = GetLocal();
33+
GenerateMapping();
34+
}
35+
return s_UserData;
36+
}
37+
}
38+
39+
#endregion
40+
41+
/// <summary>
42+
/// 新代码段
43+
/// </summary>
44+
public static SnippetInfo newSnippet
45+
{
46+
get => userData.newSnippet;
47+
}
48+
49+
#region Snippets
50+
51+
public static List<SnippetInfo> snippets => userData.snippets;
52+
53+
private static readonly Dictionary<string, SnippetInfo> s_GuidMap = new Dictionary<string, SnippetInfo>();
54+
55+
private static void GenerateMapping()
56+
{
57+
s_GuidMap.Clear();
58+
foreach (SnippetInfo snippetInfo in snippets)
59+
{
60+
s_GuidMap.Add(snippetInfo.guid, snippetInfo);
61+
}
62+
}
63+
64+
public static SnippetInfo GetSnippet(string guid)
65+
{
66+
if (string.IsNullOrEmpty(guid))
67+
{
68+
return null;
69+
}
70+
if (s_GuidMap.TryGetValue(guid, out SnippetInfo snippetInfo))
71+
{
72+
return snippetInfo;
73+
}
74+
return null;
75+
}
76+
77+
public static SnippetInfo AddSnippet(string code, string name = null, string mode = null)
78+
{
79+
if (string.IsNullOrEmpty(name))
80+
{
81+
name = "Unnamed";
82+
}
83+
long time = PipiUtility.GetTimestamp();
84+
SnippetInfo snippetInfo = new SnippetInfo()
85+
{
86+
guid = PipiUtility.NewGuid(),
87+
createTime = time,
88+
editTime = time,
89+
code = code,
90+
name = name,
91+
mode = mode,
92+
};
93+
snippets.Add(snippetInfo);
94+
s_GuidMap.Add(snippetInfo.guid, snippetInfo);
95+
return snippetInfo;
96+
}
97+
98+
public static void RemoveSnippet(string guid)
99+
{
100+
if (string.IsNullOrEmpty(guid))
101+
{
102+
return;
103+
}
104+
if (!s_GuidMap.TryGetValue(guid, out SnippetInfo snippetInfo))
105+
{
106+
return;
107+
}
108+
s_GuidMap.Remove(guid);
109+
snippets.Remove(snippetInfo);
110+
}
111+
112+
public static bool HasSnippet(string guid)
113+
{
114+
if (string.IsNullOrEmpty(guid))
115+
{
116+
return false;
117+
}
118+
return s_GuidMap.ContainsKey(guid);
119+
}
120+
121+
public static SnippetInfo GetSnippetWithName(string name, string mode = null)
122+
{
123+
if (string.IsNullOrEmpty(name))
124+
{
125+
return null;
126+
}
127+
foreach (SnippetInfo snippet in snippets)
128+
{
129+
if (snippet.MatchName(name) && (string.IsNullOrEmpty(mode) || snippet.MatchMode(mode)))
130+
{
131+
return snippet;
132+
}
133+
}
134+
return null;
135+
}
136+
137+
public static bool HasSnippetWithName(string name)
138+
{
139+
return (GetSnippetWithName(name) != null);
140+
}
141+
142+
#endregion
143+
144+
#region Basic Interface
145+
146+
/// <summary>
147+
/// 保存到本地
148+
/// </summary>
149+
public static void Save()
150+
{
151+
SetLocal(userData);
152+
}
153+
154+
/// <summary>
155+
/// 重新加载
156+
/// </summary>
157+
public static void Reload()
158+
{
159+
s_UserData = GetLocal();
160+
}
161+
162+
/// <summary>
163+
/// 重置
164+
/// </summary>
165+
public static void Reset()
166+
{
167+
SetLocal(s_UserData = new UserData());
168+
}
169+
170+
#endregion
171+
172+
#region Serialization & Deserialization
173+
174+
/// <summary>
175+
/// 本地序列化文件路径
176+
/// </summary>
177+
internal static readonly string SerializedFilePath = string.Format(CodeExecutorManager.LocalFilePathTemplate, "data");
178+
179+
/// <summary>
180+
/// 获取本地序列化的数据
181+
/// </summary>
182+
/// <returns></returns>
183+
private static UserData GetLocal()
184+
{
185+
return PipiUtility.GetLocal<UserData>(SerializedFilePath);
186+
}
187+
188+
/// <summary>
189+
/// 将数据序列化到本地
190+
/// </summary>
191+
/// <param name="value"></param>
192+
private static void SetLocal(UserData value)
193+
{
194+
PipiUtility.SetLocal(SerializedFilePath, value);
195+
}
196+
197+
#endregion
198+
199+
}
200+
201+
#region ItemInfo
202+
203+
/// <summary>
204+
/// 条目信息
205+
/// </summary>
206+
[Serializable]
207+
public class SnippetInfo
208+
{
209+
210+
public string guid = string.Empty;
211+
public long createTime = 0;
212+
public long editTime = 0;
213+
public bool top = false;
214+
public string name = string.Empty;
215+
public string code = string.Empty;
216+
public string mode = string.Empty;
217+
218+
public bool MatchGuid(string guid)
219+
{
220+
return this.guid.Equals(guid, StringComparison.OrdinalIgnoreCase);
221+
}
222+
223+
public bool MatchName(string name)
224+
{
225+
return this.name.Equals(name, StringComparison.OrdinalIgnoreCase);
226+
}
227+
228+
public bool MatchMode(string mode)
229+
{
230+
return this.mode.Equals(mode, StringComparison.OrdinalIgnoreCase);
231+
}
232+
233+
}
234+
235+
[Serializable]
236+
public class SnippetWrapper
237+
{
238+
public List<SnippetInfoSimplified> snippets = new List<SnippetInfoSimplified>();
239+
}
240+
241+
[Serializable]
242+
public class SnippetInfoSimplified
243+
{
244+
public string name = string.Empty;
245+
public string code = string.Empty;
246+
public string mode = string.Empty;
247+
}
248+
249+
#endregion
250+
251+
}

Editor/Scripts/CodeExecutorData.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)