first commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#if UNITY_2019_1_OR_NEWER
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Compilation;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SingularityGroup.HotReload.Editor {
|
||||
class DefaultCompileChecker : ICompileChecker {
|
||||
const string recompileFilePath = PackageConst.LibraryCachePath + "/recompile.txt";
|
||||
public bool hasCompileErrors { get; private set; }
|
||||
bool recompile;
|
||||
public DefaultCompileChecker() {
|
||||
CompilationPipeline.assemblyCompilationFinished += DetectCompileErrors;
|
||||
CompilationPipeline.compilationFinished += OnCompilationFinished;
|
||||
var currentSessionId = EditorAnalyticsSessionInfo.id;
|
||||
Task.Run(() => {
|
||||
try {
|
||||
var compileSessionId = File.ReadAllText(recompileFilePath);
|
||||
if(compileSessionId == currentSessionId.ToString()) {
|
||||
ThreadUtility.RunOnMainThread(() => {
|
||||
recompile = true;
|
||||
_onCompilationFinished?.Invoke();
|
||||
});
|
||||
}
|
||||
File.Delete(recompileFilePath);
|
||||
} catch(DirectoryNotFoundException) {
|
||||
//dir doesn't exist -> no recompile required
|
||||
} catch(FileNotFoundException) {
|
||||
//file doesn't exist -> no recompile required
|
||||
} catch(Exception ex) {
|
||||
Log.Warning("compile checker encountered issue: {0} {1}", ex.GetType().Name, ex.Message);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void DetectCompileErrors(string _, CompilerMessage[] messages) {
|
||||
for (int i = 0; i < messages.Length; i++) {
|
||||
if (messages[i].type == CompilerMessageType.Error) {
|
||||
hasCompileErrors = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
hasCompileErrors = false;
|
||||
}
|
||||
|
||||
void OnCompilationFinished(object _) {
|
||||
//Don't recompile on compile errors
|
||||
if(!hasCompileErrors) {
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(recompileFilePath));
|
||||
File.WriteAllText(recompileFilePath, EditorAnalyticsSessionInfo.id.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
Action _onCompilationFinished;
|
||||
public event Action onCompilationFinished {
|
||||
add {
|
||||
if(recompile && value != null) {
|
||||
value();
|
||||
}
|
||||
_onCompilationFinished += value;
|
||||
}
|
||||
remove {
|
||||
_onCompilationFinished -= value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab09f7c657e6ecb44b65dd9f8cfc3d9f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace SingularityGroup.HotReload.Editor {
|
||||
interface ICompileChecker {
|
||||
event Action onCompilationFinished;
|
||||
bool hasCompileErrors { get; }
|
||||
}
|
||||
|
||||
static class CompileChecker {
|
||||
internal static ICompileChecker Create() {
|
||||
#if UNITY_2019_1_OR_NEWER
|
||||
return new DefaultCompileChecker();
|
||||
#else
|
||||
return new LegacyCompileChecker();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 82bf36f2126bbd1498d4964272426e0f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,55 @@
|
||||
#if !UNITY_2019_1_OR_NEWER
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SingularityGroup.HotReload.Editor {
|
||||
class LegacyCompileChecker : ICompileChecker {
|
||||
const string timestampFilePath = PackageConst.LibraryCachePath + "/lastCompileTimestamp.txt";
|
||||
public bool hasCompileErrors { get; }
|
||||
const string assemblyPath = "Library/ScriptAssemblies";
|
||||
bool recompile;
|
||||
public LegacyCompileChecker() {
|
||||
Task.Run(() => {
|
||||
var info = new DirectoryInfo(assemblyPath);
|
||||
if(!info.Exists) {
|
||||
return;
|
||||
}
|
||||
var currentCompileTimestamp = default(DateTime);
|
||||
foreach (var file in info.GetFiles("*.dll")) {
|
||||
var fileWriteDate = file.LastWriteTimeUtc;
|
||||
if(fileWriteDate > currentCompileTimestamp) {
|
||||
currentCompileTimestamp = fileWriteDate;
|
||||
}
|
||||
}
|
||||
if(File.Exists(timestampFilePath)) {
|
||||
var lastTimestampStr = File.ReadAllText(timestampFilePath);
|
||||
var lastTimestamp = DateTime.ParseExact(lastTimestampStr, "o", CultureInfo.CurrentCulture).ToUniversalTime();
|
||||
if(currentCompileTimestamp > lastTimestamp) {
|
||||
ThreadUtility.RunOnMainThread(() => {
|
||||
recompile = true;
|
||||
_onCompilationFinished?.Invoke();
|
||||
});
|
||||
}
|
||||
}
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(timestampFilePath));
|
||||
File.WriteAllText(timestampFilePath, currentCompileTimestamp.ToString("o"));
|
||||
});
|
||||
}
|
||||
|
||||
Action _onCompilationFinished;
|
||||
public event Action onCompilationFinished {
|
||||
add {
|
||||
if(recompile && value != null) {
|
||||
value();
|
||||
}
|
||||
_onCompilationFinished += value;
|
||||
}
|
||||
remove {
|
||||
_onCompilationFinished -= value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f56ec68ce4b1fcc4b9c8ba5962d890f1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user