forked from Cryru/Emotion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpScriptAsset.cs
More file actions
54 lines (45 loc) · 1.4 KB
/
Copy pathCSharpScriptAsset.cs
File metadata and controls
54 lines (45 loc) · 1.4 KB
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
#region Using
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Emotion.IO;
#endregion
namespace Emotion.Plugins.CSharpScripting
{
public class CSharpScriptAsset : Asset
{
public ScriptAssemblyContext Context { get; protected set; }
public Assembly Assembly { get; protected set; }
private static Dictionary<string, Assembly> _compiledAssemblies = new();
public CSharpScriptAsset()
{
// Constructor for AssetLoader
}
public CSharpScriptAsset(string source)
{
CreateFromString(source);
}
protected override void CreateInternal(ReadOnlyMemory<byte> data)
{
string source = Encoding.UTF8.GetString(data.Span).Replace("\r", "").Replace("\uFEFF", "").Replace("", "");
CreateFromString(source);
}
private void CreateFromString(string source)
{
Context = new ScriptAssemblyContext();
MemoryStream stream = CSharpScriptEngine.CompileCode(source);
if (stream != null)
{
Assembly = Context.LoadFromStream(stream);
_compiledAssemblies.Add(Name, Assembly);
}
}
protected override void DisposeInternal()
{
Assembly = null;
Context = null;
}
}
}