forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptCompilers.cs
More file actions
173 lines (148 loc) · 6.74 KB
/
Copy pathScriptCompilers.cs
File metadata and controls
173 lines (148 loc) · 6.74 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using UnityEditor.Scripting.Compilers;
using UnityEditor.Scripting.ScriptCompilation;
namespace UnityEditor.Scripting
{
internal struct SupportedLanguageStruct
{
public string extension;
public string languageName;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MonoIsland
{
public readonly BuildTarget _target;
public readonly bool _development_player;
public readonly bool _editor;
public readonly bool _allowUnsafeCode;
public readonly ApiCompatibilityLevel _api_compatibility_level;
public readonly string[] _files;
public readonly string[] _references;
public readonly string[] _defines;
public readonly string[] _responseFiles;
public readonly string _output;
public MonoIsland(BuildTarget target, ApiCompatibilityLevel api_compatibility_level, bool allowUnsafeCode, string[] files, string[] references, string[] defines, string output)
{
_target = target;
_development_player = false;
_editor = false;
_allowUnsafeCode = allowUnsafeCode;
_api_compatibility_level = api_compatibility_level;
_files = files;
_references = references;
_defines = defines;
_output = output;
_responseFiles = null;
}
public MonoIsland(BuildTarget target, bool editor, bool development_player, bool allowUnsafeCode, ApiCompatibilityLevel api_compatibility_level, string[] files, string[] references, string[] defines, string output)
{
_target = target;
_development_player = development_player;
_editor = editor;
_allowUnsafeCode = allowUnsafeCode;
_api_compatibility_level = api_compatibility_level;
_files = files;
_references = references;
_defines = defines;
_output = output;
_responseFiles = null;
}
public MonoIsland(BuildTarget target, bool editor, bool development_player, bool allowUnsafeCode, ApiCompatibilityLevel api_compatibility_level, string[] files, string[] references, string[] defines, string output, string[] responseFiles)
: this(target, editor, development_player, allowUnsafeCode, api_compatibility_level, files, references, defines, output)
{
_responseFiles = responseFiles;
}
public string GetExtensionOfSourceFiles()
{
return _files.Length > 0 ? ScriptCompilers.GetExtensionOfSourceFile(_files[0]) : "NA";
}
}
internal static class ScriptCompilers
{
internal static readonly List<SupportedLanguage> SupportedLanguages;
internal static readonly SupportedLanguage CSharpSupportedLanguage;
static ScriptCompilers()
{
SupportedLanguages = new List<SupportedLanguage>();
var types = new List<Type>();
types.Add(typeof(CSharpLanguage));
foreach (var t in types)
{
SupportedLanguages.Add((SupportedLanguage)Activator.CreateInstance(t));
}
CSharpSupportedLanguage = SupportedLanguages.Single(l => l.GetType() == typeof(CSharpLanguage));
}
internal static SupportedLanguageStruct[] GetSupportedLanguageStructs()
{
//we communicate with the runtime by xforming our SupportedLaanguage class to a struct, because that's
//just a lot easier to marshall between native and managed code.
return SupportedLanguages.Select(lang => new SupportedLanguageStruct
{
extension = lang.GetExtensionICanCompile(),
languageName = lang.GetLanguageName()
}).ToArray();
}
internal static void GetClassAndNamespace(string file, string definedSymbols, out string outClassName,
out string outNamespace)
{
if (string.IsNullOrEmpty(file)) throw new ArgumentException("Invalid file");
string extension = GetExtensionOfSourceFile(file);
foreach (var lang in SupportedLanguages)
{
if (lang.GetExtensionICanCompile() == extension)
{
lang.GetClassAndNamespace(file, definedSymbols, out outClassName, out outNamespace);
return;
}
}
throw new ApplicationException("Unable to find a suitable compiler");
}
internal static string GetNamespace(string file, string definedSymbols)
{
if (string.IsNullOrEmpty(file)) throw new ArgumentException("Invalid file");
string extension = GetExtensionOfSourceFile(file);
foreach (var lang in SupportedLanguages)
{
if (lang.GetExtensionICanCompile() == extension)
return lang.GetNamespace(file, definedSymbols);
}
throw new ApplicationException("Unable to find a suitable compiler");
}
internal static SupportedLanguage GetLanguageFromName(string name)
{
foreach (var lang in SupportedLanguages)
{
if (String.Equals(name, lang.GetLanguageName(), StringComparison.OrdinalIgnoreCase))
return lang;
}
throw new ApplicationException(string.Format("Script language '{0}' is not supported", name));
}
internal static SupportedLanguage GetLanguageFromExtension(string extension)
{
foreach (var lang in SupportedLanguages)
{
if (String.Equals(extension, lang.GetExtensionICanCompile(), StringComparison.OrdinalIgnoreCase))
return lang;
}
throw new ApplicationException(string.Format("Script file extension '{0}' is not supported", extension));
}
internal static ScriptCompilerBase CreateCompilerInstance(ScriptAssembly scriptAssembly, EditorScriptCompilationOptions options, string tempOutputDirectory)
{
if (scriptAssembly.Files.Length == 0) throw new ArgumentException("Cannot compile ScriptAssembly with no files");
return CSharpSupportedLanguage.CreateCompiler(scriptAssembly, options, tempOutputDirectory);
}
public static string GetExtensionOfSourceFile(string file)
{
var ext = Path.GetExtension(file).ToLower();
ext = ext.Substring(1); //strip dot
return ext;
}
}
}