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
150 lines (128 loc) · 5.69 KB
/
Copy pathScriptCompilers.cs
File metadata and controls
150 lines (128 loc) · 5.69 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
// 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;
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 ApiCompatibilityLevel _api_compatibility_level;
public readonly string[] _files;
public readonly string[] _references;
public readonly string[] _defines;
public readonly string _output;
public MonoIsland(BuildTarget target, ApiCompatibilityLevel api_compatibility_level, string[] files, string[] references, string[] defines, string output)
{
_target = target;
_development_player = false;
_editor = false;
_api_compatibility_level = api_compatibility_level;
_files = files;
_references = references;
_defines = defines;
_output = output;
}
public MonoIsland(BuildTarget target, bool editor, bool development_player, ApiCompatibilityLevel api_compatibility_level, string[] files, string[] references, string[] defines, string output)
{
_target = target;
_development_player = development_player;
_editor = editor;
_api_compatibility_level = api_compatibility_level;
_files = files;
_references = references;
_defines = defines;
_output = output;
}
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));
types.Add(typeof(BooLanguage));
types.Add(typeof(UnityScriptLanguage));
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 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(MonoIsland island, bool buildingForEditor, BuildTarget targetPlatform, bool runUpdater)
{
if (island._files.Length == 0) throw new ArgumentException("Cannot compile MonoIsland with no files");
foreach (var lang in SupportedLanguages)
{
if (lang.GetExtensionICanCompile() == island.GetExtensionOfSourceFiles())
return lang.CreateCompiler(island, buildingForEditor, targetPlatform, runUpdater);
}
throw new ApplicationException(string.Format("Unable to find a suitable compiler for sources with extension '{0}' (Output assembly: {1})", island.GetExtensionOfSourceFiles(), island._output));
}
public static string GetExtensionOfSourceFile(string file)
{
var ext = Path.GetExtension(file).ToLower();
ext = ext.Substring(1); //strip dot
return ext;
}
}
}