Skip to content

Commit 701bb3b

Browse files
committed
feat: add compiling options (deterministic and optimize) for CSharpCompilerSettings.dll
1 parent 3de5ec9 commit 701bb3b

File tree

7 files changed

+123
-50
lines changed

7 files changed

+123
-50
lines changed

Assets/CSharpCompilerSettings/AsmdefEx.Core.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#if UNITY_EDITOR && !PUBLISH_AS_DLL
21
using System;
32
using System.Collections;
43
using System.Diagnostics;
@@ -576,4 +575,3 @@ static Core()
576575
}
577576
}
578577
}
579-
#endif

Assets/CSharpCompilerSettings/Dev/CSharpCompilerSettings.Dev.cs

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
using System.IO;
2-
using System.Linq;
3-
using System.Security.Cryptography;
1+
using System.Linq;
42
using UnityEditor;
5-
using UnityEditor.Compilation;
63

74
namespace CSharpCompilierSettings
85
{
9-
[InitializeOnLoad]
106
internal static class Dev
117
{
12-
const string k_AssemblySrc = "Library/ScriptAssemblies/CSharpCompilerSettings.dll";
13-
const string k_AssemblyDst = "Packages/CSharpCompilerSettings/Plugins/CSharpCompilerSettings.dll";
14-
158
private const string k_DebugModeText = "Csc Settings/Debug Mode";
169
private const string k_DebugModeSymbol = "CSC_SETTINGS_DEBUG";
1710

@@ -67,43 +60,5 @@ private static void SwitchSymbol(string symbol)
6760
: symbols.Concat(new[] {symbol}).ToArray()
6861
);
6962
}
70-
71-
private static void CopyAssemblyToPackage(string assemblyPath, CompilerMessage[] messages)
72-
{
73-
if (k_AssemblySrc != assemblyPath || !messages.All(x => x.type != CompilerMessageType.Error)) return;
74-
75-
UnityEngine.Debug.LogFormat("OnAssemblyCompilationFinished: Copy {0} to {1}", k_AssemblySrc, k_AssemblyDst);
76-
CopyFileIfUpdated(k_AssemblySrc, k_AssemblyDst);
77-
}
78-
79-
public static void CopyFileIfUpdated(string src, string dst)
80-
{
81-
src = Path.GetFullPath(src);
82-
if (!File.Exists(src))
83-
return;
84-
85-
dst = Path.GetFullPath(dst);
86-
if (File.Exists(dst))
87-
{
88-
using (var srcFs = new FileStream(src, FileMode.Open))
89-
using (var dstFs = new FileStream(dst, FileMode.Open))
90-
using (var md5 = new MD5CryptoServiceProvider())
91-
{
92-
if (md5.ComputeHash(srcFs).SequenceEqual(md5.ComputeHash(dstFs)))
93-
return;
94-
}
95-
}
96-
97-
var dir = Path.GetDirectoryName(dst);
98-
if (!Directory.Exists(dir))
99-
Directory.CreateDirectory(dir);
100-
101-
File.Copy(src, dst, true);
102-
}
103-
104-
static Dev()
105-
{
106-
CompilationPipeline.assemblyCompilationFinished += CopyAssemblyToPackage;
107-
}
10863
}
10964
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Diagnostics;
2+
using System.IO;
3+
using System.Linq;
4+
using UnityEditor;
5+
using UnityEditor.Compilation;
6+
using UnityEngine;
7+
using Debug = UnityEngine.Debug;
8+
9+
namespace Coffee.CSharpCompilierSettings
10+
{
11+
internal class Recompiler : ScriptableSingleton<Recompiler>
12+
{
13+
private const string k_AssemblySrc = "Library/ScriptAssemblies/CSharpCompilerSettings.dll";
14+
private const string k_ResponseFileSrc = "Assets/CSharpCompilerSettings/rsp";
15+
private const string k_ResponseFileDst = "Temp/CSharpCompilerSettings.rsp";
16+
17+
[SerializeField] private bool requested;
18+
19+
[InitializeOnLoadMethod]
20+
private static void InitializeOnLoadMethod()
21+
{
22+
CompilationPipeline.assemblyCompilationFinished += RequestToRecompileIfNeeded;
23+
24+
if (!instance.requested) return;
25+
26+
instance.requested = false;
27+
Recompile();
28+
}
29+
30+
private static void RequestToRecompileIfNeeded(string assemblyPath, CompilerMessage[] messages)
31+
{
32+
if (k_AssemblySrc != assemblyPath || !messages.All(x => x.type != CompilerMessageType.Error)) return;
33+
34+
instance.requested = true;
35+
}
36+
37+
private static void Recompile()
38+
{
39+
var appContents = EditorApplication.applicationContentsPath;
40+
41+
// Update rsp file.
42+
var rsp = File.ReadAllText(k_ResponseFileSrc);
43+
rsp = rsp.Replace("${APP_CONTENTS}", appContents);
44+
File.WriteAllText(k_ResponseFileDst, rsp);
45+
46+
// Detect csc tool exe.
47+
var cscToolExe = appContents + "/Tools/RoslynNet46/csc.exe".Replace('/', Path.DirectorySeparatorChar);
48+
if (!File.Exists(cscToolExe))
49+
cscToolExe = appContents + "/Tools/Roslyn/csc.exe".Replace('/', Path.DirectorySeparatorChar);
50+
51+
// Create compilation process.
52+
var psi = new ProcessStartInfo
53+
{
54+
CreateNoWindow = true,
55+
UseShellExecute = false,
56+
RedirectStandardError = true,
57+
};
58+
59+
if (Application.platform == RuntimePlatform.WindowsEditor)
60+
{
61+
psi.FileName = Path.GetFullPath(cscToolExe);
62+
psi.Arguments = "/shared /noconfig @" + k_ResponseFileDst;
63+
}
64+
else
65+
{
66+
psi.FileName = Path.Combine(appContents, "MonoBleedingEdge/bin/mono");
67+
psi.Arguments = cscToolExe + " /noconfig @" + k_ResponseFileDst;
68+
}
69+
70+
// Start compilation process.
71+
Debug.LogFormat("Recompile: CSharpCompilerSettings.dll\n command={0} {1}\n", psi.FileName, psi.Arguments);
72+
var p = Process.Start(psi);
73+
p.Exited += (_, __) =>
74+
{
75+
if (p.ExitCode == 0)
76+
Debug.Log("Recompile: success.");
77+
else
78+
Debug.LogError("Recompile: failure.\n" + p.StandardError.ReadToEnd());
79+
};
80+
p.EnableRaisingEvents = true;
81+
}
82+
}
83+
}

Assets/CSharpCompilerSettings/Recompiler.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/CSharpCompilerSettings/rsp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/target:library
2+
/nowarn:0169
3+
/langversion:latest
4+
/out:"Packages/CSharpCompilerSettings/Plugins/CSharpCompilerSettings.dll"
5+
/nostdlib
6+
/deterministic
7+
/optimize+
8+
9+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.dll"
10+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.CoreModule.dll"
11+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.IMGUIModule.dll"
12+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.JSONSerializeModule.dll"
13+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/Managed/UnityEditor.dll"
14+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/MonoBleedingEdge/lib/mono/2.0-api/mscorlib.dll"
15+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/MonoBleedingEdge/lib/mono/2.0-api/System.dll"
16+
/r:"/Applications/Unity/Hub/Editor/2018.4.22f1/Unity.app/Contents/MonoBleedingEdge/lib/mono/2.0-api/System.Core.dll"
17+
18+
"Assets/CSharpCompilerSettings/AsmdefEx.Core.cs"
19+
"Assets/CSharpCompilerSettings/CSharpCompilerSettings.cs"

Assets/CSharpCompilerSettings/rsp.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProjectSettings/ProjectSettings.asset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ PlayerSettings:
511511
additionalIl2CppArgs:
512512
scriptingRuntimeVersion: 1
513513
apiCompatibilityLevelPerPlatform:
514-
Standalone: 3
514+
Standalone: 1
515515
m_RenderingPath: 1
516516
m_MobileRenderingPath: 1
517517
metroPackageName: CSharpCompilerSettings
@@ -591,7 +591,7 @@ PlayerSettings:
591591
facebookStatus: 1
592592
facebookXfbml: 0
593593
facebookFrictionlessRequests: 1
594-
apiCompatibilityLevel: 6
594+
apiCompatibilityLevel: 2
595595
cloudProjectId:
596596
framebufferDepthMemorylessMode: 0
597597
projectName:

0 commit comments

Comments
 (0)