forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathILPostProcessCompiledAssembly.cs
More file actions
95 lines (73 loc) · 3.41 KB
/
Copy pathILPostProcessCompiledAssembly.cs
File metadata and controls
95 lines (73 loc) · 3.41 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
// 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 Unity.CompilationPipeline.Common.ILPostProcessing;
using UnityEditor;
using UnityEditor.Scripting.ScriptCompilation;
using UnityEngine.Profiling;
internal class ILPostProcessCompiledAssembly : ICompiledAssembly
{
readonly string m_AssemblyFilename;
readonly string m_OutputPath;
InMemoryAssembly m_InMemoryAssembly;
public ILPostProcessCompiledAssembly(ScriptAssembly scriptAssembly, string outputPath)
{
m_AssemblyFilename = scriptAssembly.Filename;
Name = Path.GetFileNameWithoutExtension(m_AssemblyFilename);
References = scriptAssembly.GetAllReferences();
Defines = scriptAssembly.Defines;
m_OutputPath = outputPath;
}
public ILPostProcessCompiledAssembly(EditorBuildRules.TargetAssembly targetAssembly, string outputPath, PrecompiledAssemblyProviderBase precompiledAssemblyProvider)
{
m_AssemblyFilename = targetAssembly.Filename;
Name = Path.GetFileNameWithoutExtension(m_AssemblyFilename);
var precompiledAssembliesDictionary = precompiledAssemblyProvider.GetPrecompiledAssembliesDictionary(true, BuildTargetGroup.Unknown, BuildTarget.Android);
var precompiledAssemblyReferences = targetAssembly.ExplicitPrecompiledReferences
.Where(x => precompiledAssembliesDictionary.ContainsKey(x))
.Select(x => precompiledAssembliesDictionary[x].Path);
var targetAssemblyReferences = targetAssembly.References.Select(a => a.FullPath(outputPath));
References = precompiledAssemblyReferences.Concat(targetAssemblyReferences).ToArray();
Defines = targetAssembly.Defines;
m_OutputPath = outputPath;
}
private InMemoryAssembly CreateOrGetInMemoryAssembly()
{
if (m_InMemoryAssembly != null)
{
return m_InMemoryAssembly;
}
byte[] peData = File.ReadAllBytes(Path.Combine(m_OutputPath, m_AssemblyFilename));
var pdbFileName = Path.GetFileNameWithoutExtension(m_AssemblyFilename) + ".pdb";
byte[] pdbData = File.ReadAllBytes(Path.Combine(m_OutputPath, pdbFileName));
m_InMemoryAssembly = new InMemoryAssembly(peData, pdbData);
return m_InMemoryAssembly;
}
public InMemoryAssembly InMemoryAssembly
{
get { return CreateOrGetInMemoryAssembly(); }
set { m_InMemoryAssembly = value; }
}
public string Name { get; set; }
public string[] References { get; set; }
public string[] Defines { get; private set; }
public void WriteAssembly()
{
Profiler.BeginSample("ILPostProcessCompiledAssembly.WriteAssembly");
if (m_InMemoryAssembly == null)
{
throw new ArgumentException("InMemoryAssembly has never been accessed or modified");
}
Profiler.BeginSample("ILPostProcessCompiledAssembly.WriteAssembly");
var assemblyPath = Path.Combine(m_OutputPath, m_AssemblyFilename);
var pdbFileName = Path.GetFileNameWithoutExtension(m_AssemblyFilename) + ".pdb";
var pdbPath = Path.Combine(m_OutputPath, pdbFileName);
File.WriteAllBytes(assemblyPath, InMemoryAssembly.PeData);
File.WriteAllBytes(pdbPath, InMemoryAssembly.PdbData);
Profiler.EndSample();
}
}