first commit

This commit is contained in:
Stefano Rossi 2025-07-12 18:59:18 +02:00
commit aa44d3ad70
Signed by: chadmin
GPG key ID: 9EFA2130646BC893
1585 changed files with 277994 additions and 0 deletions

View file

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
#if !NET_UNITY_4_8
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.</summary>
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class NotNullWhenAttribute : Attribute
{
/// <summary>Initializes the attribute with the specified return value condition.</summary>
/// <param name="returnValue">
/// The return value condition. If the method returns this value, the associated parameter will not be null.
/// </param>
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
/// <summary>Gets the return value condition.</summary>
public bool ReturnValue { get; }
}
}
#endif

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: eebe08ae528d7724887b8d74bd2a5551
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static System.IO.Path;
#nullable enable
namespace Meryel.UnityCodeAssist.Editor
{
public static class CommonTools
{
public static string GetTagManagerFilePath()
{
var projectPath = GetProjectPathRaw();
var tagManagerPath = Combine(projectPath, "ProjectSettings/TagManager.asset");
return tagManagerPath;
}
public static string GetInputManagerFilePath()
{
var projectPath = GetProjectPathRaw();
var inputManagerPath = Combine(projectPath, "ProjectSettings/InputManager.asset");
return inputManagerPath;
}
public static string GetProjectPath()
{
var rawPath = GetProjectPathRaw();
var osPath = new OSPath(rawPath);
var unixPath = osPath.Unix;
var trimmed = unixPath.TrimEnd('\\', '/');
return trimmed;
}
/// <summary>
/// Get the path to the project folder.
/// </summary>
/// <returns>The project folder path</returns>
static string GetProjectPathRaw()
{
// Application.dataPath returns the path including /Assets, which we need to strip off
var path = UnityEngine.Application.dataPath;
var directory = new DirectoryInfo(path);
var parent = directory.Parent;
if (parent != null)
return parent.FullName;
return path;
}
public static int GetHashOfPath(string path)
{
var osPath = new OSPath(path);
var unixPath = osPath.Unix;
var trimmed = unixPath.TrimEnd('\\', '/');
var hash = trimmed.GetHashCode();
if (hash < 0) // Get rid of the negative values, so there will be no '-' char in file names
{
hash++;
hash = Math.Abs(hash);
}
return hash;
}
}
// https://github.com/dmitrynogin/cdsf/blob/master/Cds.Folders/OSPath.cs
internal class OSPath
{
public static readonly OSPath Empty = "";
public static bool IsWindows => DirectorySeparatorChar == '\\';
public OSPath(string text)
{
Text = text.Trim();
}
public static implicit operator OSPath(string text) => new OSPath(text);
public static implicit operator string(OSPath path) => path.Normalized;
public override string ToString() => Normalized;
protected string Text { get; }
public string Normalized => IsWindows ? Windows : Unix;
public string Windows => Text.Replace('/', '\\');
//public string Unix => Simplified.Text.Replace('\\', '/');
public string Unix => Text.Replace('\\', '/');
public OSPath Relative => Simplified.Text.TrimStart('/', '\\');
public OSPath Absolute => IsAbsolute ? this : "/" + Relative;
public bool IsAbsolute => IsRooted || HasVolume;
public bool IsRooted => Text.Length >= 1 && (Text[0] == '/' || Text[0] == '\\');
public bool HasVolume => Text.Length >= 2 && Text[1] == ':';
public OSPath Simplified => HasVolume ? Text.Substring(2) : Text;
public OSPath Parent => GetDirectoryName(Text);
public bool Contains(OSPath path) =>
Normalized.StartsWith(path);
public static OSPath operator +(OSPath left, OSPath right) =>
new OSPath(Combine(left, right.Relative));
public static OSPath operator -(OSPath left, OSPath right) =>
left.Contains(right)
? new OSPath(left.Normalized.Substring(right.Normalized.Length)).Relative
: left;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: cb6ae855b8059214095179e3a316467b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,31 @@
using System;
using System.Linq;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Configuration;
#nullable enable
namespace Meryel.UnityCodeAssist.Editor.Logger
{
public class DomainHashEnricher : ILogEventEnricher
{
static readonly int domainHash;
static DomainHashEnricher()
{
var guid = UnityEditor.GUID.Generate();
domainHash = guid.GetHashCode();
}
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty(
"DomainHash", domainHash));
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d1235c7b5b457d848833746026792ef7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,186 @@
using Serilog;
using Serilog.Core;
using UnityEngine;
using UnityEditor;
using System.Linq;
#nullable enable
namespace Meryel.UnityCodeAssist.Editor.Logger
{
[InitializeOnLoad]
public static class ELogger
{
public static event System.Action? OnVsInternalLogChanged;
// Change 'new LoggerConfiguration().MinimumLevel.Debug();' if you change these values
const Serilog.Events.LogEventLevel fileMinLevel = Serilog.Events.LogEventLevel.Debug;
const Serilog.Events.LogEventLevel outputWindowMinLevel = Serilog.Events.LogEventLevel.Information;
static LoggingLevelSwitch? fileLevelSwitch, outputWindowLevelSwitch;
//static bool IsInitialized { get; set; }
static ILogEventSink? _outputWindowSink;
static ILogEventSink? _memorySink;
public static string GetInternalLogContent() => _memorySink == null ? string.Empty : ((MemorySink)_memorySink).Export();
public static int GetErrorCountInInternalLog() => _memorySink == null ? 0 : ((MemorySink)_memorySink).ErrorCount;
public static int GetWarningCountInInternalLog() => _memorySink == null ? 0 : ((MemorySink)_memorySink).WarningCount;
public static string? FilePath { get; private set; }
public static string? VSFilePath { get; private set; }
//**-- make it work with multiple clients
static string? _vsInternalLog;
public static string? VsInternalLog
{
get => _vsInternalLog;
set
{
_vsInternalLog = value;
OnVsInternalLogChanged?.Invoke();
}
}
static ELogger()
{
var isFirst = false;
const string stateName = "isFirst";
if (!SessionState.GetBool(stateName, false))
{
isFirst = true;
SessionState.SetBool(stateName, true);
}
var projectPath = CommonTools.GetProjectPath();
var outputWindowSink = new System.Lazy<ILogEventSink>(() => new UnityOutputWindowSink(null));
Init(isFirst, projectPath, outputWindowSink);
if (isFirst)
LogHeader(Application.unityVersion, projectPath);
Serilog.Log.Debug("PATH: {Path}", projectPath);
}
static void LogHeader(string unityVersion, string solutionDir)
{
var os = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
var assisterVersion = Assister.Version;
var syncModel = Synchronizer.Model.Utilities.Version;
var hash = CommonTools.GetHashOfPath(solutionDir);
Serilog.Log.Debug(
"Beginning logging {OS}, Unity {U}, Unity Code Assist {A}, Communication Protocol {SM}, Project: '{Dir}', Project Hash: {Hash}",
os, unityVersion, assisterVersion, syncModel, solutionDir, hash);
}
static string GetFilePath(string solutionDir)
{
var solutionHash = CommonTools.GetHashOfPath(solutionDir);
var tempDir = System.IO.Path.GetTempPath();
var fileName = $"UCA_U_LOG_{solutionHash}_.TXT"; // hour code will be appended to the end of file, so add a trailing '_'
var filePath = System.IO.Path.Combine(tempDir, fileName);
return filePath;
}
static string GetVSFilePath(string solutionDir)
{
var solutionHash = CommonTools.GetHashOfPath(solutionDir);
var tempDir = System.IO.Path.GetTempPath();
var fileName = $"UCA_VS_LOG_{solutionHash}_.TXT"; // hour code will be appended to the end of file, so add a trailing '_'
var filePath = System.IO.Path.Combine(tempDir, fileName);
return filePath;
}
public static void Init(bool isFirst, string solutionDir, System.Lazy<ILogEventSink> outputWindowSink)
{
FilePath = GetFilePath(solutionDir);
VSFilePath = GetVSFilePath(solutionDir);
fileLevelSwitch = new LoggingLevelSwitch(fileMinLevel);
outputWindowLevelSwitch = new LoggingLevelSwitch(outputWindowMinLevel);
var config = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.With(new DomainHashEnricher());
const string outputTemplate = "{Timestamp:HH:mm:ss.fff} [U] [{Level:u3}] [{DomainHash}] {Message:lj}{NewLine}{Exception}";
config = config.WriteTo.PersistentFile(FilePath
, outputTemplate: outputTemplate
, shared: true
, persistentFileRollingInterval: PersistentFileRollingInterval.Day
, preserveLogFilename: true
, levelSwitch: fileLevelSwitch
, rollOnEachProcessRun: isFirst
);
_outputWindowSink ??= outputWindowSink.Value;
if (_outputWindowSink != null)
config = config.WriteTo.Sink(_outputWindowSink, outputWindowMinLevel, outputWindowLevelSwitch);
_memorySink ??= new MemorySink(outputTemplate);
config = config.WriteTo.Sink(_memorySink, fileMinLevel, null);
config = config.Destructure.With(new MyDestructuringPolicy());
Serilog.Log.Logger = config.CreateLogger();
//switchableLogger.Set(config.CreateLogger(), disposePrev: true);
OnOptionsChanged();
//IsInitialized = true;
}
public static void OnOptionsChanged()
{
// Since we don't use LogEventLevel.Fatal, we can use it for disabling sinks
var isLoggingToFile = OptionsIsLoggingToFile;
var targetFileLevel = isLoggingToFile ? fileMinLevel : Serilog.Events.LogEventLevel.Fatal;
if (fileLevelSwitch != null)
fileLevelSwitch.MinimumLevel = targetFileLevel;
var isLoggingToOutputWindow = OptionsIsLoggingToOutputWindow;
var targetOutputWindowLevel = isLoggingToOutputWindow ? outputWindowMinLevel : Serilog.Events.LogEventLevel.Fatal;
if (outputWindowLevelSwitch != null)
outputWindowLevelSwitch.MinimumLevel = targetOutputWindowLevel;
}
//**-- UI for these two
static bool OptionsIsLoggingToFile => true;
static bool OptionsIsLoggingToOutputWindow => true;
}
public class MyDestructuringPolicy : IDestructuringPolicy
{
// serilog cannot destruct StringArrayContainer by default, so do it manually
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Serilog.Events.LogEventPropertyValue? result)
{
if (value is Synchronizer.Model.StringArrayContainer sac)
{
var items = sac.Container.Select(item => propertyValueFactory.CreatePropertyValue(item, true));
result = new Serilog.Events.SequenceValue(items);
return true;
}
result = null;
return false;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 054dc6105589bce4081e205ac04bf169
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,131 @@
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
#nullable enable
namespace Meryel.UnityCodeAssist.Editor.Logger
{
//**--
// remove this in unity???
// need to serialize/deserialize data to survive domain reload, which will effect performance
// right now data is lost during domain reloads, which makes its function kinda useless
// or maybe move it to a external process like com.unity.process-server
public class MemorySink : ILogEventSink
{
readonly ConcurrentQueue<LogEvent> logs;
readonly ConcurrentQueue<LogEvent[]> warningLogs;
readonly ConcurrentQueue<LogEvent[]> errorLogs;
const int logsLimit = 30;
const int warningLimit = 5;
const int errorLimit = 3;
readonly string outputTemplate;
public MemorySink(string outputTemplate)
{
this.outputTemplate = outputTemplate;
logs = new ConcurrentQueue<LogEvent>();
warningLogs = new ConcurrentQueue<LogEvent[]>();
errorLogs = new ConcurrentQueue<LogEvent[]>();
}
public void Emit(LogEvent logEvent)
{
if (logEvent == null)
return;
logs.Enqueue(logEvent);
if (logs.Count > logsLimit)
logs.TryDequeue(out _);
if (logEvent.Level == LogEventLevel.Warning)
{
var warningAndLeadingLogs = logs.ToArray();
warningLogs.Enqueue(warningAndLeadingLogs);
if (warningLogs.Count > warningLimit)
warningLogs.TryDequeue(out _);
}
if (logEvent.Level == LogEventLevel.Error)
{
var errorAndLeadingLogs = logs.ToArray();
errorLogs.Enqueue(errorAndLeadingLogs);
if (errorLogs.Count > errorLimit)
errorLogs.TryDequeue(out _);
}
}
public bool HasError => !errorLogs.IsEmpty;
public bool HasWarning => !warningLogs.IsEmpty;
public int ErrorCount => errorLogs.Count;
public int WarningCount => warningLogs.Count;
public string Export()
{
IFormatProvider? formatProvider = null;
var formatter = new Serilog.Formatting.Display.MessageTemplateTextFormatter(
outputTemplate, formatProvider);
var result = string.Empty;
using (var outputStream = new MemoryStream())
{
var encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
using var output = new StreamWriter(outputStream, encoding);
if (!errorLogs.IsEmpty)
{
var errorArray = errorLogs.ToArray();
foreach (var error in errorArray)
{
foreach (var logEvent in error)
{
formatter.Format(logEvent, output);
}
}
}
if (!warningLogs.IsEmpty)
{
var warningArray = warningLogs.ToArray();
foreach (var warning in warningArray)
{
foreach (var logEvent in warning)
{
formatter.Format(logEvent, output);
}
}
}
if (!logs.IsEmpty)
{
var logArray = logs.ToArray();
foreach (var logEvent in logArray)
{
formatter.Format(logEvent, output);
}
}
output.Flush();
outputStream.Seek(0, SeekOrigin.Begin);
using var streamReader = new StreamReader(outputStream, encoding);
result = streamReader.ReadToEnd();
}
return result;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0fe13269bf1182142a9b46409339fb5b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,47 @@
using System;
using System.Linq;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Serilog.Configuration;
#nullable enable
namespace Meryel.UnityCodeAssist.Editor.Logger
{
public class UnityOutputWindowSink : ILogEventSink
{
private readonly IFormatProvider? _formatProvider;
public UnityOutputWindowSink(IFormatProvider? formatProvider)
{
_formatProvider = formatProvider;
}
public void Emit(LogEvent logEvent)
{
var message = logEvent.RenderMessage(_formatProvider);
switch (logEvent.Level)
{
case LogEventLevel.Verbose:
case LogEventLevel.Debug:
case LogEventLevel.Information:
UnityEngine.Debug.Log(message);
break;
case LogEventLevel.Warning:
UnityEngine.Debug.LogWarning(message);
break;
case LogEventLevel.Error:
case LogEventLevel.Fatal:
UnityEngine.Debug.LogError(message);
break;
default:
break;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3995562507d0dd147b1533d40e3de339
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: