modular-avatar/Editor/ErrorReporting/ErrorElement.cs
bd_ ddbc3b164b
chore: restructure repository to put package at top-level (#477)
* chore: rearrange package structure to have the package at the root

* ci: update CI workflows

* ci: fixing workflow bugs

* ci: recurse building .zip package

* ci: more fixes

* ci: add back in the nadena.dev VPM repo

* ci: fix tests
2023-10-08 15:39:57 +09:00

104 lines
3.1 KiB
C#

using System;
using nadena.dev.modular_avatar.core.editor;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UIElements;
namespace nadena.dev.modular_avatar.editor.ErrorReporting
{
internal class ErrorElement : Box
{
private readonly ErrorLog log;
Texture2D GetIcon()
{
switch (log.reportLevel)
{
case ReportLevel.Info:
return EditorGUIUtility.FindTexture("d_console.infoicon");
case ReportLevel.Warning:
return EditorGUIUtility.FindTexture("d_console.warnicon");
default:
return EditorGUIUtility.FindTexture("d_console.erroricon");
}
}
public ErrorElement(ErrorLog log, ObjectRefLookupCache cache)
{
this.log = log;
AddToClassList("ErrorElement");
var tex = GetIcon();
if (tex != null)
{
var image = new Image();
image.image = tex;
Add(image);
}
var inner = new Box();
Add(inner);
var label = new Label(GetLabelText());
inner.Add(label);
foreach (var obj in log.referencedObjects)
{
var referenced = obj.Lookup(cache);
if (referenced != null)
{
inner.Add(new SelectionButton(obj.typeName, referenced));
}
}
if (!string.IsNullOrWhiteSpace(log.stacktrace))
{
var foldout = new Foldout();
foldout.text = Localization.S("error.stack_trace");
var field = new TextField();
field.value = log.stacktrace;
field.isReadOnly = true;
field.multiline = true;
foldout.Add(field);
foldout.value = false;
inner.Add(foldout);
}
}
private static GameObject FindObject(string path)
{
var scene = SceneManager.GetActiveScene();
foreach (var root in scene.GetRootGameObjects())
{
if (root.name == path) return root;
if (path.StartsWith(root.name + "/"))
{
return root.transform.Find(path.Substring(root.name.Length + 1))?.gameObject;
}
}
return null;
}
private string GetLabelText()
{
var objArray = new object[log.substitutions.Length];
for (int i = 0; i < log.substitutions.Length; i++)
{
objArray[i] = log.substitutions[i];
}
try
{
return string.Format(Localization.S(log.messageCode), objArray);
}
catch (FormatException e)
{
Debug.LogError("Error formatting message code: " + log.messageCode);
Debug.LogException(e);
return log.messageCode + "\n" + string.Join("\n", objArray);
}
}
}
}