Skip to content

Commit e6770d5

Browse files
committed
refactor: refactor
1 parent 58d97aa commit e6770d5

27 files changed

+1196
-728
lines changed

Editor/AssemblyRenamer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public static void Rename(string dll, string assemblyName)
3232
}
3333

3434
// Start compilation process.
35-
Core.LogDebug("Rename: Change assembly name\n command={0} {1}\n", psi.FileName, psi.Arguments);
35+
Logger.LogDebug("Rename: Change assembly name\n command={0} {1}\n", psi.FileName, psi.Arguments);
3636
var p = Process.Start(psi);
3737
p.Exited += (_, __) =>
3838
{
3939
if (p.ExitCode == 0)
40-
Core.LogDebug("Rename: success.\n" + p.StandardOutput.ReadToEnd());
40+
Logger.LogDebug("Rename: success.\n{0}", p.StandardOutput.ReadToEnd());
4141
else
42-
Core.LogException("Rename: failure.\n" + p.StandardError.ReadToEnd());
42+
Logger.LogException("Rename: failure.\n{0}\n\n{1}", p.StandardError.ReadToEnd(), p.StandardOutput.ReadToEnd());
4343
};
4444
p.EnableRaisingEvents = true;
4545

Editor/AutoImporter.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
14
using UnityEditor;
5+
using UnityEditor.Compilation;
26

37
namespace Coffee.CSharpCompilerSettings
48
{
5-
internal class AutoImporter: ScriptableSingleton<AutoImporter>
9+
internal class AutoImporter : ScriptableSingleton<AutoImporter>
610
{
711
public string assetPath;
812

913
[InitializeOnLoadMethod]
1014
private static void InitializeOnLoad()
1115
{
12-
if(string.IsNullOrEmpty(instance.assetPath)) return;
16+
if (string.IsNullOrEmpty(instance.assetPath)) return;
1317

1418
AssetDatabase.ImportAsset(instance.assetPath);
1519
instance.assetPath = null;
@@ -19,5 +23,36 @@ public static void ImportOnFinishedCompilation(string assetPath)
1923
{
2024
instance.assetPath = assetPath;
2125
}
26+
27+
public static void RequestPublishDll(string asmdefPath, string assemblyName)
28+
{
29+
Action<string, CompilerMessage[]> callback = null;
30+
31+
callback = (name, messages) =>
32+
{
33+
// This assembly is requested to publish?
34+
var compiledAssemblyName = Path.GetFileNameWithoutExtension(name);
35+
if (assemblyName != compiledAssemblyName)
36+
return;
37+
38+
CompilationPipeline.assemblyCompilationFinished -= callback;
39+
Logger.LogInfo("Assembly compilation finished: <b>{0} is requested to publish.</b>", compiledAssemblyName);
40+
41+
// No compilation error?
42+
if (messages.Any(x => x.type == CompilerMessageType.Error))
43+
return;
44+
45+
// Publish a dll to parent directory.
46+
var dst = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(asmdefPath)), compiledAssemblyName + ".dll");
47+
var src = "Library/ScriptAssemblies/" + Path.GetFileName(dst);
48+
Logger.LogInfo("<b>Publish assembly as dll:</b> " + dst);
49+
EditorUtils.CopyFileIfNeeded(Path.GetFullPath(src), Path.GetFullPath(dst));
50+
51+
EditorApplication.delayCall += () => AssetDatabase.ImportAsset(dst);
52+
};
53+
54+
CompilationPipeline.assemblyCompilationFinished += callback;
55+
Logger.LogInfo("<b><color=#22aa22>Request to publish dll:</color> {0}</b>", assemblyName);
56+
}
2257
}
2358
}

Editor/CSharpProjectModifier.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ private static string OnGeneratedCSProject(string path, string content)
1515
.Select(x => AssetDatabase.GUIDToAssetPath(x))
1616
.FirstOrDefault(x => Path.GetFileName(x) == assemblyName + ".asmdef");
1717

18-
Core.LogDebug("<color=orange>OnGeneratedCSProject</color> {0} -> {1} ({2})", path, assemblyName, asmdefPath);
18+
Logger.LogDebug("<color=orange>OnGeneratedCSProject</color> {0} -> {1} ({2})", path, assemblyName, asmdefPath);
1919
var setting = CscSettingsAsset.GetAtPath(asmdefPath) ?? CscSettingsAsset.instance;
2020

2121
// Modify define symbols.
2222
var defines = Regex.Match(content, "<DefineConstants>(.*)</DefineConstants>").Groups[1].Value.Split(';', ',');
23-
defines = Core.ModifyDefineSymbols(defines, setting.AdditionalSymbols);
23+
defines = Utils.ModifySymbols(defines, setting.AdditionalSymbols);
2424
var defineText = string.Join(";", defines);
2525
content = Regex.Replace(content, "<DefineConstants>(.*)</DefineConstants>", string.Format("<DefineConstants>{0}</DefineConstants>", defineText), RegexOptions.Multiline);
2626

27-
// Language version.
2827
if (!setting.UseDefaultCompiler)
28+
{
29+
// Language version.
2930
content = Regex.Replace(content, "<LangVersion>.*</LangVersion>", "<LangVersion>" + setting.LanguageVersion + "</LangVersion>", RegexOptions.Multiline);
31+
}
3032

3133
// Nullable.
3234
var value = setting.Nullable.ToString().ToLower();

Editor/CscSettingsProvider.cs

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.IO;
32
using UnityEditor;
43
using UnityEngine;
@@ -8,11 +7,13 @@ namespace Coffee.CSharpCompilerSettings
87
internal class CscSettingsProvider
98
{
109
private static SerializedObject serializedObject;
10+
private static SerializedProperty s_EnableLogging;
1111

1212
[SettingsProvider]
1313
private static SettingsProvider CreateSettingsProvider()
1414
{
1515
serializedObject = new SerializedObject(CscSettingsAsset.instance);
16+
s_EnableLogging = serializedObject.FindProperty("m_EnableLogging");
1617
var keywords = SettingsProvider.GetSearchKeywordsFromSerializedObject(serializedObject);
1718
return new SettingsProvider("Project/C# Compiler", SettingsScope.Project)
1819
{
@@ -24,51 +25,27 @@ private static SettingsProvider CreateSettingsProvider()
2425

2526
private static void OnGUI(string searchContext)
2627
{
27-
if (serializedObject == null)
28-
serializedObject = new SerializedObject(CscSettingsAsset.instance);
29-
30-
var spCompilerType = serializedObject.FindProperty("m_CompilerType");
31-
EditorGUILayout.PropertyField(spCompilerType);
28+
InspectorGUI.DrawCompilerPackage(serializedObject);
29+
EditorGUILayout.Space();
3230

33-
if (spCompilerType.intValue == (int) CompilerType.CustomPackage)
3431
{
35-
EditorGUI.indentLevel++;
36-
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_PackageName"));
37-
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_PackageVersion"));
38-
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_LanguageVersion"));
39-
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Nullable"));
40-
EditorGUI.indentLevel--;
4132
}
4233

4334
EditorGUILayout.Space();
4435

4536
EditorGUILayout.LabelField("Debug", EditorStyles.boldLabel);
46-
EditorGUILayout.PropertyField(serializedObject.FindProperty("m_EnableLogging"));
37+
EditorGUILayout.PropertyField(s_EnableLogging);
4738

4839
// Controls
49-
using (new EditorGUI.DisabledScope(!serializedObject.hasModifiedProperties))
50-
using (new GUILayout.HorizontalScope())
51-
{
52-
GUILayout.FlexibleSpace();
53-
54-
if (GUILayout.Button("Revert"))
55-
{
56-
serializedObject = new SerializedObject(CscSettingsAsset.instance);
57-
}
58-
59-
if (GUILayout.Button("Apply"))
40+
InspectorGUI.DrawControl(serializedObject.hasModifiedProperties,
41+
onRevert: () => { serializedObject = new SerializedObject(CscSettingsAsset.instance); },
42+
onApply: () =>
6043
{
6144
serializedObject.ApplyModifiedProperties();
6245
File.WriteAllText(CscSettingsAsset.k_SettingsPath, JsonUtility.ToJson(serializedObject.targetObject, true));
63-
RequestScriptCompilation();
46+
Utils.RequestCompilation();
6447
}
65-
}
66-
}
67-
68-
public static void RequestScriptCompilation()
69-
{
70-
Type.GetType("UnityEditor.Scripting.ScriptCompilation.EditorCompilationInterface, UnityEditor")
71-
.Call("DirtyAllScripts");
48+
);
7249
}
7350
}
7451
}

Editor/EditorUtils.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.IO;
2+
using System.Security.Cryptography;
3+
using System.Linq;
4+
5+
namespace Coffee.CSharpCompilerSettings
6+
{
7+
internal static class EditorUtils
8+
{
9+
/// <summary>
10+
/// Copy the file (over write).
11+
/// NOTE: If there are no changes to MD5, the file will not be copied.
12+
/// </summary>
13+
public static void CopyFileIfNeeded(string src, string dst)
14+
{
15+
if (IsSameContent(src, dst)) return;
16+
17+
Directory.CreateDirectory(Path.GetDirectoryName(dst));
18+
File.Copy(src, dst, true);
19+
}
20+
21+
/// <summary>
22+
/// Returns whether or not the two files are the same.
23+
/// </summary>
24+
private static bool IsSameContent(string file1, string file2)
25+
{
26+
if (file1 == file2) return true;
27+
if (string.IsNullOrEmpty(file1) || string.IsNullOrEmpty(file2) || !File.Exists(file1) || !File.Exists(file2)) return false;
28+
29+
using (var md5 = MD5.Create())
30+
using (var srcStream = File.OpenRead(file1))
31+
using (var dstStream = File.OpenRead(file2))
32+
return md5.ComputeHash(srcStream).SequenceEqual(md5.ComputeHash(dstStream));
33+
}
34+
}
35+
}

Plugins/CSharpCompilerSettings/CompilerType.cs.meta renamed to Editor/EditorUtils.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)