More inline scripts, shared classes

This commit is contained in:
Yoshi Askharoun
2025-07-20 02:03:16 -05:00
parent 77fe53fcc3
commit 4325bc3e73
8 changed files with 3059 additions and 152 deletions
+180 -42
View File
@@ -1,4 +1,6 @@
using Microsoft.Iris.Asm;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Iris.Asm;
using Microsoft.Iris.Debug.Data;
using Microsoft.Iris.DecompXml.Mock;
using Microsoft.Iris.Markup;
using System;
@@ -51,6 +53,12 @@ public partial class Decompiler
xExport.SetAttributeValue("Base", baseTypeName);
}
if (export is ClassTypeSchema classExport)
{
if (classExport.IsShared)
xExport.SetAttributeValue("Shared", true);
}
if (export.InitializePropertiesOffset is not uint.MaxValue)
AnalyzeMethodForInit(export.InitializePropertiesOffset, xExport, export, name + "_prop");
@@ -59,7 +67,7 @@ public partial class Decompiler
if (export.InitialEvaluateOffsets is { Length: > 0 })
{
XElement xScripts = new(_nsUix + "Scripts");
var xScripts = GetOrCreateElement(xExport, _nsUix + "Scripts");
foreach (var offset in export.InitialEvaluateOffsets)
{
@@ -69,8 +77,19 @@ public partial class Decompiler
XElement xScript = new(_nsUix + "Script", scriptText);
xScripts.Add(xScript);
}
}
xExport.Add(xScripts);
if (export.RefreshGroupOffsets is { Length: > 0 })
{
var xScripts = GetOrCreateElement(xExport, _nsUix + "Scripts");
foreach (var offset in export.RefreshGroupOffsets)
{
var scriptText = AnalyzeRefreshMethod(offset, export, $"{name}_rfsh_0x{offset:X}");
XElement xScript = new(_nsUix + "Script", scriptText);
xScripts.Add(xScript);
}
}
if (export.InitializeContentOffset is not uint.MaxValue)
@@ -99,6 +118,7 @@ public partial class Decompiler
{
Indent = true,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
Encoding = Encoding.UTF8,
};
StringBuilder sb = new();
@@ -132,6 +152,10 @@ public partial class Decompiler
stack.Push(null);
break;
case OpCode.PushThis:
stack.Push(elemToInit);
break;
case OpCode.ConstructObject:
var typeToCtor = _context.GetImportedType(instruction.Operands.ElementAt(0));
var xObj = new XElement(_context.GetXName(typeToCtor));
@@ -140,37 +164,7 @@ public partial class Decompiler
case OpCode.LookupSymbol:
var symbolIndex = (ushort)instruction.Operands.ElementAt(0).Value;
var symbol = initType.SymbolReferenceTable[symbolIndex];
stack.Push(symbol);
break;
case OpCode.MethodInvokeStatic:
var method = _context.GetImportedMethod(instruction.Operands.First());
int parameterCount = method.ParameterTypes.Length;
object[] parameters = new object[parameterCount];
for (parameterCount--; parameterCount >= 0; parameterCount--)
parameters[parameterCount] = stack.Pop();
var callExpression = new IrisMethodCallExpression(method, null, parameters.Select(IrisExpression.Wrap));
stack.Push(callExpression);
break;
case OpCode.PropertyGet:
case OpCode.PropertyGetPeek:
case OpCode.PropertyGetStatic:
var propToGet = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
var propTarget = instruction.OpCode switch
{
OpCode.PropertyGet => IrisExpression.Wrap(stack.Pop()),
OpCode.PropertyGetPeek => IrisExpression.Wrap(stack.Peek()),
_ => null,
};
var propertyGetExpression = new IrisPropertyExpression(propToGet, propTarget);
stack.Push(propertyGetExpression);
stack.Push(initType.SymbolReferenceTable[symbolIndex]);
break;
case OpCode.PropertyInitialize:
@@ -178,7 +172,11 @@ public partial class Decompiler
var newPropValue = stack.Pop();
var target = stack.Pop();
var xTarget = (XElement)ToXmlFriendlyObject(target);
var xTarget = ToXmlFriendlyObject(target) as XElement;
if (xTarget is null)
{
}
PropertyAssignOnXElement(xTarget, propertyToInit, IrisObject.Create(newPropValue, propertyToInit.PropertyType, _context));
@@ -196,21 +194,73 @@ public partial class Decompiler
.FirstOrDefault(s => s.Name == key)?
.Type;
var targetInstance = (XElement)stack.Peek();
var targetInstance = stack.Peek() as XElement;
if (targetInstance is null)
{
}
PropertyDictionaryAddOnXElement(targetInstance, targetDictProperty, IrisObject.Create(dictValue, dictValueType, _context), key);
break;
case OpCode.PropertyListAdd:
var targetListProperty = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
var valueToAdd = stack.Pop();
TypeSchema valueToAddType = null;
if (valueToAdd is SymbolReference symRef)
{
valueToAddType = initType.InheritableSymbolsTable?
.FirstOrDefault(s => s.Name == symRef.Symbol)?
.Type;
}
var valueToAddObj = IrisObject.Create(valueToAdd, valueToAddType, _context);
var targetInstance2 = (XElement)ToXmlFriendlyObject(stack.Peek());
PropertyListAddOnXElement(targetInstance2, targetListProperty, IrisObject.Create(valueToAdd, null, _context));
var targetListPropertyIndex = (ushort)instruction.Operands.First().Value;
if (targetListPropertyIndex != ushort.MaxValue)
{
var targetListProperty = _context.ImportTables.PropertyImports[targetListPropertyIndex];
if (valueToAddObj.Type is null)
{
var valueRuntimeType = targetListProperty.PropertyType.RuntimeType.GetGenericArguments().FirstOrDefault();
valueToAddType = _context.ImportTables.TypeImports.FirstOrDefault(t => t.RuntimeType == valueRuntimeType);
valueToAddObj = valueToAddObj with { Type = valueToAddType };
}
PropertyListAddOnXElement(targetInstance2, targetListProperty, valueToAddObj);
}
else
{
PropertyListAddOnXElement(targetInstance2, valueToAddObj);
}
break;
case OpCode.InitializeInstance:
case OpCode.JumpIfDictionaryContains:
case OpCode.ConstructListenerStorage:
var listenerCount = (ushort)instruction.Operands.First().Value;
// These instructions are inconsequential for determining how objects are initialized
break;
case OpCode.ConstructObjectParam:
case OpCode.MethodInvoke:
case OpCode.MethodInvokePeek:
case OpCode.MethodInvokeStatic:
case OpCode.MethodInvokePushLastParam:
case OpCode.MethodInvokeStaticPushLastParam:
case OpCode.PropertyGet:
case OpCode.PropertyGetPeek:
case OpCode.PropertyGetStatic:
case OpCode.Operation:
// These instructions only appear in initializers as inline expressions
if (!TryDecompileExpression(instruction, stack))
throw new NotImplementedException();
break;
case not OpCode.ReturnVoid:
Console.WriteLine($"Unsupported instruction: {instruction}");
break;
}
}
@@ -223,6 +273,69 @@ public partial class Decompiler
return stack;
}
private string AnalyzeRefreshMethod(uint startOffset, MarkupTypeSchema initType, string methodName = "")
{
var methodBody = _context.GetMethodBody(startOffset).ToArray();
Stack<object> stack = new();
for (int i = 0; i < methodBody.Length; i++)
{
var instruction = methodBody[i];
try
{
switch (instruction.OpCode)
{
case OpCode.Listen:
case OpCode.DestructiveListen:
var listenerIndex = (ushort)instruction.Operands.ElementAt(0).Value;
var listenerType = (ListenerType)(byte)instruction.Operands.ElementAt(1).Value;
var watchIndex = (ushort)instruction.Operands.ElementAt(2).Value;
var scriptId = (uint)instruction.Operands.ElementAt(3).Value;
var refreshOffset = uint.MaxValue;
if (instruction.OpCode is OpCode.DestructiveListen)
refreshOffset = (uint)instruction.Operands.ElementAt(4).Value;
MarkupTypeSchema markupTypeSchema = initType;
uint num = scriptId >> 27;
while (num != markupTypeSchema.TypeDepth)
markupTypeSchema = markupTypeSchema.MarkupTypeBase;
var scriptOffset = scriptId & 0x07FFFFFFU;
string watch = null;
InstructionObjectSource watchSource = InstructionObjectSource.Dynamic;
switch (listenerType)
{
case ListenerType.Property:
watch = _context.ImportTables.PropertyImports[watchIndex].Name;
watchSource = InstructionObjectSource.PropertyImports;
break;
case ListenerType.Event:
watch = _context.ImportTables.EventImports[watchIndex].Name;
watchSource = InstructionObjectSource.EventImports;
break;
case ListenerType.Symbol:
watch = initType.SymbolReferenceTable[watchIndex].Symbol;
watchSource = InstructionObjectSource.SymbolReference;
break;
}
//object handlerObj = stack.Peek();
break;
}
}
catch (Exception ex)
{
throw new Exception($"Failed to analyze instruction `{instruction}` @ 0x{instruction.Offset:X}, {methodName}[{i}]", ex);
}
}
return "";
}
private static XElement GetOrCreateElement(XElement parent, XName name)
{
var elem = parent.Element(name);
@@ -247,6 +360,8 @@ public partial class Decompiler
obj = irisObj.Object;
}
return obj switch
{
string str => str,
@@ -254,6 +369,8 @@ public partial class Decompiler
bool b => b ? "true" : "false",
IStringEncodable strEnc => strEnc.EncodeString(),
IrisExpression expr => '{' + expr.Decompile(_context) + '}',
ExpressionSyntax expr => FormatInlineExpression(expr),
SymbolReference symRef => '{' + symRef.Symbol + '}',
IFormattable formattable => formattable.ToString(null, CultureInfo.InvariantCulture),
Layout.ILayout layoutObj
@@ -295,7 +412,18 @@ public partial class Decompiler
{
case XElement xValue:
var xProperty = GetOrCreateElement(xTarget, _nsUix + property.Name);
xProperty.Add(xValue);
// Flatten collections
if (typeof(System.Collections.IList).IsAssignableFrom(value.Type.RuntimeType)
|| typeof(System.Collections.IDictionary).IsAssignableFrom(value.Type.RuntimeType))
{
xProperty.Add(xValue.Elements());
}
else
{
xProperty.Add(xValue);
}
return xProperty;
case string strValue:
@@ -311,7 +439,11 @@ public partial class Decompiler
private XElement PropertyListAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value)
{
var xList = GetOrCreateElement(xTarget, _nsUix + property.Name);
return PropertyListAddOnXElement(xList, value);
}
private XElement PropertyListAddOnXElement(XElement xList, IrisObject value)
{
object xValue = ToXmlFriendlyObject(value);
XElement xListEntry;
@@ -334,10 +466,16 @@ public partial class Decompiler
return xListEntry;
}
private XElement PropertyDictionaryAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value, string key)
private XElement PropertyDictionaryAddOnXElement(XElement xDictionary, IrisObject value, string key)
{
var xDictionaryEntry = PropertyListAddOnXElement(xTarget, property, value);
var xDictionaryEntry = PropertyListAddOnXElement(xDictionary, value);
xDictionaryEntry.SetAttributeValue("Name", key);
return xDictionaryEntry;
}
private XElement PropertyDictionaryAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value, string key)
{
var xDictionary = GetOrCreateElement(xTarget, _nsUix + property.Name);
return PropertyDictionaryAddOnXElement(xDictionary, value, key);
}
}