From 6ee3464e360eebcc16a07a56e841083f0ec17539 Mon Sep 17 00:00:00 2001 From: Nahuel Rocchetti Date: Wed, 10 Jul 2024 00:46:55 -0300 Subject: [PATCH] Add VM Entity inspector --- Assets/Editor/OpenTS2/SimulatorEditor.cs | 8 +- Assets/Editor/OpenTS2/VMEntityWindow.cs | 136 +++++++++++++++++++ Assets/Editor/OpenTS2/VMEntityWindow.cs.meta | 11 ++ 3 files changed, 153 insertions(+), 2 deletions(-) create mode 100644 Assets/Editor/OpenTS2/VMEntityWindow.cs create mode 100644 Assets/Editor/OpenTS2/VMEntityWindow.cs.meta diff --git a/Assets/Editor/OpenTS2/SimulatorEditor.cs b/Assets/Editor/OpenTS2/SimulatorEditor.cs index c45667c..80d0306 100644 --- a/Assets/Editor/OpenTS2/SimulatorEditor.cs +++ b/Assets/Editor/OpenTS2/SimulatorEditor.cs @@ -32,14 +32,12 @@ namespace OpenTS2 { GUILayout.BeginVertical("box"); var globalName = ((SimAntics.VMGlobals)i).ToString(); - GUILayout.BeginHorizontal(); var editedGlobal = EditorGUILayout.TextField(globalName, vm.GlobalState[i].ToString(CultureInfo.InvariantCulture)); if (short.TryParse(editedGlobal,NumberStyles.Integer, CultureInfo.InvariantCulture, out var res)) { if (res != vm.GlobalState[i]) vm.SetGlobal((ushort)i, res); } - GUILayout.EndHorizontal(); GUILayout.EndVertical(); } EditorGUI.indentLevel--; @@ -54,10 +52,16 @@ namespace OpenTS2 GUILayout.BeginVertical("box"); GUILayout.Label($"{entity.ID} - {entity.ObjectDefinition.FileName}"); EditorGUILayout.Separator(); + GUILayout.BeginHorizontal(); if (GUILayout.Button("Kill")) { entity.Delete(); } + if (GUILayout.Button("Inspect")) + { + VMEntityWindow.Show(entity); + } + GUILayout.EndHorizontal(); GUILayout.EndVertical(); } EditorGUI.indentLevel--; diff --git a/Assets/Editor/OpenTS2/VMEntityWindow.cs b/Assets/Editor/OpenTS2/VMEntityWindow.cs new file mode 100644 index 0000000..bb03490 --- /dev/null +++ b/Assets/Editor/OpenTS2/VMEntityWindow.cs @@ -0,0 +1,136 @@ +using OpenTS2.Common; +using OpenTS2.Content; +using OpenTS2.Content.DBPF; +using OpenTS2.Files.Formats.DBPF; +using OpenTS2.SimAntics; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEditor; +using UnityEngine; + +namespace OpenTS2 +{ + public class VMEntityWindow : EditorWindow + { + public VMEntity Entity; + private ContentManager _contentManager; + private List _attributeNames; + private List _semiAttributeNames; + private Vector2 _scrollbarPosition = Vector2.zero; + private bool _showTemps = false; + private bool _showAttributes = false; + private bool _showSemiAttributes = false; + private bool _showObjectData = false; + public static void Show(VMEntity entity) + { + var window = CreateWindow(typeof(VMEntityWindow)); + window.Initialize(entity); + } + + public void Initialize(VMEntity entity) + { + _attributeNames = new List(); + _semiAttributeNames = new List(); + _contentManager = ContentManager.Instance; + Entity = entity; + titleContent = new GUIContent($"VM Entity: {Entity.ObjectDefinition.FileName} ({Entity.ID})"); + + var attrStringSet = _contentManager.GetAsset(new ResourceKey(0x100, Entity.PrivateGroupID, TypeIDs.STR)); + var semiAttrStringSet = _contentManager.GetAsset(new ResourceKey(0x100, Entity.SemiGlobalGroupID, TypeIDs.STR)); + + if (attrStringSet != null) + { + foreach (var strValue in attrStringSet.StringData.Strings[Languages.USEnglish]) + _attributeNames.Add(strValue.Value); + } + + if (semiAttrStringSet != null) + { + foreach (var strValue in semiAttrStringSet.StringData.Strings[Languages.USEnglish]) + _semiAttributeNames.Add(strValue.Value); + } + } + + private void DisplayEditableField(string name, ref short value) + { + var editedValue = EditorGUILayout.TextField(name, value.ToString(CultureInfo.InvariantCulture)); + if (short.TryParse(editedValue, NumberStyles.Integer, CultureInfo.InvariantCulture, out short res)) + { + if (res != value) + value = res; + } + } + + private void OnGUI() + { + _scrollbarPosition = EditorGUILayout.BeginScrollView(_scrollbarPosition); + + _showObjectData = EditorGUILayout.Foldout(_showObjectData, "Object Data"); + if (_showObjectData) + { + EditorGUI.indentLevel++; + for (var i = 0; i < Entity.ObjectData.Length; i++) + { + GUILayout.BeginVertical("box"); + DisplayEditableField(((VMObjectData)i).ToString(), ref Entity.ObjectData[i]); + GUILayout.EndVertical(); + } + EditorGUI.indentLevel--; + } + + _showAttributes = EditorGUILayout.Foldout(_showAttributes, "Attributes"); + if (_showAttributes) + { + EditorGUI.indentLevel++; + for (var i = 0; i < Entity.Attributes.Length; i++) + { + var attributeName = $"Attribute {i}"; + if (_attributeNames.Count > i && !string.IsNullOrEmpty(_attributeNames[i])) + { + attributeName = _attributeNames[i]; + } + GUILayout.BeginVertical("box"); + DisplayEditableField(attributeName, ref Entity.Attributes[i]); + GUILayout.EndVertical(); + } + EditorGUI.indentLevel--; + } + + _showSemiAttributes = EditorGUILayout.Foldout(_showSemiAttributes, "Semi-Attributes"); + if (_showSemiAttributes) + { + EditorGUI.indentLevel++; + for (var i = 0; i < Entity.SemiAttributes.Length; i++) + { + var attributeName = $"Attribute {i}"; + if (_semiAttributeNames.Count > i && !string.IsNullOrEmpty(_semiAttributeNames[i])) + { + attributeName = _semiAttributeNames[i]; + } + GUILayout.BeginVertical("box"); + DisplayEditableField(attributeName, ref Entity.SemiAttributes[i]); + GUILayout.EndVertical(); + } + EditorGUI.indentLevel--; + } + + _showTemps = EditorGUILayout.Foldout(_showTemps, "Temps"); + if (_showTemps) + { + EditorGUI.indentLevel++; + for (var i = 0; i < Entity.Temps.Length; i++) + { + GUILayout.BeginVertical("box"); + DisplayEditableField($"Temp {i}", ref Entity.Temps[i]); + GUILayout.EndVertical(); + } + EditorGUI.indentLevel--; + } + EditorGUILayout.EndScrollView(); + } + } +} diff --git a/Assets/Editor/OpenTS2/VMEntityWindow.cs.meta b/Assets/Editor/OpenTS2/VMEntityWindow.cs.meta new file mode 100644 index 0000000..136f0c5 --- /dev/null +++ b/Assets/Editor/OpenTS2/VMEntityWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0e76ea6bacd99f64980a82d87ea47c63 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: