Support content init, PLAD, PGET/P/T, LSYM, and serialization of complex objects with simple properties

This commit is contained in:
Yoshi Askharoun
2025-07-17 17:55:04 -05:00
parent 64f7a445eb
commit 2a76d02461
5 changed files with 241 additions and 75 deletions
+150 -71
View File
@@ -47,7 +47,11 @@ public class Decompiler
new XAttribute("Name", name), new XAttribute("Name", name),
new XAttribute("Base", baseTypeName)); new XAttribute("Base", baseTypeName));
AnalyzeMethodForInit(export.InitializePropertiesOffset, xExport); if (export.InitializePropertiesOffset is not uint.MaxValue)
AnalyzeMethodForInit(export.InitializePropertiesOffset, xExport, export, name + "_prop");
if (export.InitializeContentOffset is not uint.MaxValue)
AnalyzeMethodForInit(export.InitializeContentOffset, xExport, export, name + "_cont");
xRoot.Add(xExport); xRoot.Add(xExport);
} }
@@ -82,7 +86,7 @@ public class Decompiler
return sb.ToString(); return sb.ToString();
} }
private Stack<object> AnalyzeMethodForInit(uint startOffset, XElement elemToInit) private Stack<object> AnalyzeMethodForInit(uint startOffset, XElement elemToInit, MarkupTypeSchema initType, string methodName = "")
{ {
var methodBody = _context.GetMethodBody(startOffset); var methodBody = _context.GetMethodBody(startOffset);
@@ -92,60 +96,97 @@ public class Decompiler
{ {
var instruction = methodBody[i]; var instruction = methodBody[i];
switch (instruction.OpCode) try
{ {
case OpCode.PushConstant: switch (instruction.OpCode)
var constant = _context.GetConstant(instruction.Operands.First()); {
stack.Push(constant); case OpCode.PushConstant:
break; var constant = _context.GetConstant(instruction.Operands.First());
stack.Push(constant);
break;
case OpCode.PushNull: case OpCode.PushNull:
stack.Push(null); stack.Push(null);
break; break;
case OpCode.ConstructObject: case OpCode.ConstructObject:
var type = _context.GetImportedType(instruction.Operands.ElementAt(0)); var typeToCtor = _context.GetImportedType(instruction.Operands.ElementAt(0));
var xObj = new XElement(_context.GetXName(type)); var xObj = new XElement(_context.GetXName(typeToCtor));
stack.Push(new IrisObject(xObj, type)); stack.Push(new IrisObject(xObj, typeToCtor));
break; break;
case OpCode.MethodInvokeStatic: case OpCode.LookupSymbol:
var method = _context.GetImportedMethod(instruction.Operands.First()); var symbolIndex = (ushort)instruction.Operands.ElementAt(0).Value;
var symbol = initType.SymbolReferenceTable[symbolIndex];
stack.Push(symbol);
break;
int parameterCount = method.ParameterTypes.Length; case OpCode.MethodInvokeStatic:
object[] parameters = new object[parameterCount]; var method = _context.GetImportedMethod(instruction.Operands.First());
for (parameterCount--; parameterCount >= 0; parameterCount--)
parameters[parameterCount] = stack.Pop();
var callExpression = new IrisMethodCallExpression(method, null, parameters.Select(IrisExpression.ToExpression)); int parameterCount = method.ParameterTypes.Length;
object[] parameters = new object[parameterCount];
for (parameterCount--; parameterCount >= 0; parameterCount--)
parameters[parameterCount] = stack.Pop();
stack.Push(callExpression); var callExpression = new IrisMethodCallExpression(method, null, parameters.Select(IrisExpression.Wrap));
break;
case OpCode.PropertyInitialize: stack.Push(callExpression);
var property = _context.GetImportedProperty(instruction.Operands.ElementAt(0)); break;
var propValue = stack.Pop();
var target = stack.Pop(); case OpCode.PropertyGet:
var xTarget = (XElement)ToXmlFriendlyObject(target); case OpCode.PropertyGetPeek:
case OpCode.PropertyGetStatic:
var propToGet = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
PropertyAssignOnXElement(xTarget, property, IrisObject.Create(propValue, property.PropertyType, _context)); var propTarget = instruction.OpCode switch
{
OpCode.PropertyGet => IrisExpression.Wrap(stack.Pop()),
OpCode.PropertyGetPeek => IrisExpression.Wrap(stack.Peek()),
_ => null,
};
stack.Push(new IrisObject(xTarget, property.Owner)); var propertyGetExpression = new IrisPropertyExpression(propToGet, propTarget);
break; stack.Push(propertyGetExpression);
break;
case OpCode.PropertyDictionaryAdd: case OpCode.PropertyInitialize:
var targetProperty = _context.GetImportedProperty(instruction.Operands.ElementAt(0)); var propertyToInit = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
var newPropValue = stack.Pop();
var keyReference = instruction.Operands.ElementAt(1); var target = stack.Pop();
var key = _context.GetConstant(keyReference).Value.ToString(); var xTarget = (XElement)ToXmlFriendlyObject(target);
var dictValue = stack.Pop(); PropertyAssignOnXElement(xTarget, propertyToInit, IrisObject.Create(newPropValue, propertyToInit.PropertyType, _context));
var targetInstance = stack.Peek() as XElement; stack.Push(new IrisObject(xTarget, propertyToInit.Owner));
break;
PropertyDictionaryAddOnXElement(targetInstance, targetProperty, IrisObject.Create(dictValue, null, _context), key); case OpCode.PropertyDictionaryAdd:
break; var targetDictProperty = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
var keyReference = instruction.Operands.ElementAt(1);
var key = _context.GetConstant(keyReference).Value.ToString();
var dictValue = stack.Pop();
var targetInstance = (XElement)stack.Peek();
PropertyDictionaryAddOnXElement(targetInstance, targetDictProperty, IrisObject.Create(dictValue, null, _context), key);
break;
case OpCode.PropertyListAdd:
var targetListProperty = _context.GetImportedProperty(instruction.Operands.ElementAt(0));
var valueToAdd = stack.Pop();
var targetInstance2 = (XElement)ToXmlFriendlyObject(stack.Peek());
PropertyListAddOnXElement(targetInstance2, targetListProperty, IrisObject.Create(valueToAdd, null, _context));
break;
}
}
catch (Exception ex)
{
throw new Exception($"Failed to analyze instruction `{instruction}` @ 0x{instruction.Offset:X}, {methodName}[{i}]", ex);
} }
} }
@@ -165,50 +206,106 @@ public class Decompiler
return elem; return elem;
} }
private object ToXmlFriendlyObject(object obj) private object ToXmlFriendlyObject(object obj, TypeSchema type = null)
{ {
if (obj is Disassembler.RawConstantInfo rci) if (obj is Disassembler.RawConstantInfo rci)
{
obj = rci.Value; obj = rci.Value;
}
else if (obj is IrisObject irisObj) else if (obj is IrisObject irisObj)
{
obj = irisObj.Object; obj = irisObj.Object;
}
return obj switch return obj switch
{ {
string str => str, string str => str,
IStringEncodable strEnc => strEnc.EncodeString(),
null => "{null}", null => "{null}",
bool b => b ? "true" : "false",
IStringEncodable strEnc => strEnc.EncodeString(),
IrisExpression expr => '{' + expr.Decompile(_context) + '}', IrisExpression expr => '{' + expr.Decompile(_context) + '}',
Enum en => en.ToString(),
Layout.ILayout layoutObj
when Layout.PredefinedLayouts.TryConvertToString(layoutObj, out var layoutName)
=> layoutName,
XElement xElem => xElem, XElement xElem => xElem,
_ => throw new InvalidOperationException($"Cannot convert type '{obj.GetType().Name}' to an XML object") _ => SerializeToXml(obj)
}; };
} }
private XElement SerializeToXml(object obj)
{
var type = Disassembler.GuessTypeSchema(obj.GetType(), _context.LoadResult);
XElement xObj = new(_context.GetXName(type));
var defaultObj = type.ConstructDefault();
foreach (var prop in type.Properties)
{
var defaultPropValue = prop.GetValue(defaultObj);
var propValue = prop.GetValue(obj);
if (propValue == defaultPropValue || propValue.Equals(defaultPropValue))
continue;
PropertyAssignOnXElement(xObj, prop, new(propValue, prop.PropertyType));
}
return xObj;
}
private XObject PropertyAssignOnXElement(XElement xTarget, PropertySchema property, IrisObject value) private XObject PropertyAssignOnXElement(XElement xTarget, PropertySchema property, IrisObject value)
{ {
object xfValue = ToXmlFriendlyObject(value.Object); object xfValue = ToXmlFriendlyObject(value.Object, value.Type);
XObject xObject;
switch (xfValue) switch (xfValue)
{ {
case XElement xValue: case XElement xValue:
var xProperty = GetOrCreateElement(xTarget, property.Name); var xProperty = GetOrCreateElement(xTarget, property.Name);
xProperty.Add(xValue); xProperty.Add(xValue);
xObject = xProperty; return xProperty;
break;
case string strValue: case string strValue:
xObject = new XAttribute(property.Name, strValue); var xAttr = new XAttribute(property.Name, strValue);
break; xTarget.Add(xAttr);
return xAttr;
default: default:
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
}
xTarget.Add(xObject); private XElement PropertyListAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value)
return xObject; {
var xDictionary = GetOrCreateElement(xTarget, _nsUix + property.Name);
return PropertyListAddOnXElement(xDictionary, value);
}
private XElement PropertyListAddOnXElement(XElement xList, IrisObject value)
{
object xValue = ToXmlFriendlyObject(value.Object, value.Type);
XElement xListEntry;
switch (xValue)
{
case string strValue:
xListEntry = new(_context.GetXName(value.Type));
xListEntry.SetAttributeValue(value.Type.Name, strValue);
break;
case XElement xValueELem:
xListEntry = xValueELem;
break;
default:
throw new InvalidOperationException();
}
xList.Add(xListEntry);
return xListEntry;
} }
private XElement PropertyDictionaryAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value, string key) private XElement PropertyDictionaryAddOnXElement(XElement xTarget, PropertySchema property, IrisObject value, string key)
@@ -219,26 +316,8 @@ public class Decompiler
private XElement PropertyDictionaryAddOnXElement(XElement xDictionary, IrisObject value, string key) private XElement PropertyDictionaryAddOnXElement(XElement xDictionary, IrisObject value, string key)
{ {
object xValue = ToXmlFriendlyObject(value.Object); var xDictionaryEntry = PropertyListAddOnXElement(xDictionary, value);
XElement xDictionaryEntry;
switch (xValue)
{
case string strValue:
xDictionaryEntry = new(_context.GetXName(value.Type));
xDictionaryEntry.SetAttributeValue(value.Type.Name, strValue);
break;
case XElement xValueELem:
xDictionaryEntry = xValueELem;
break;
default:
throw new InvalidOperationException();
}
xDictionaryEntry.SetAttributeValue("Name", key); xDictionaryEntry.SetAttributeValue("Name", key);
xDictionary.Add(xDictionaryEntry);
return xDictionaryEntry; return xDictionaryEntry;
} }
} }
@@ -15,6 +15,7 @@ internal class IrisExpression : Expression
null => Constant(null), null => Constant(null),
Expression expr => expr, Expression expr => expr,
Disassembler.RawConstantInfo constantInfo => new IrisConstantExpression(constantInfo.Value, constantInfo.Type), Disassembler.RawConstantInfo constantInfo => new IrisConstantExpression(constantInfo.Value, constantInfo.Type),
Markup.SymbolReference symbolRef => Constant(symbolRef),
_ => throw new NotImplementedException($"Unable to wrap '{p}' in an expression") _ => throw new NotImplementedException($"Unable to wrap '{p}' in an expression")
}; };
@@ -0,0 +1,43 @@
using Microsoft.Iris.Markup;
using System.Linq.Expressions;
using System.Text;
namespace Microsoft.Iris.DecompXml.Mock;
internal class IrisPropertyExpression : IrisExpression, IReturnValueProvider
{
public IrisPropertyExpression(PropertySchema property, Expression? target)
{
Property = property;
Target = target;
}
public new PropertySchema Property { get; }
public sealed override ExpressionType NodeType => ExpressionType.MemberAccess;
public Expression? Target { get; }
public TypeSchema ReturnType => Property.PropertyType;
public override string Decompile(DecompileContext context)
{
StringBuilder sb = new();
if (Target is null)
{
var qfn = context.GetQualifiedName(Property.Owner);
sb.Append(qfn);
}
else
{
sb.Append(Decompile(Target, context));
}
sb.Append('.');
sb.Append(Property.Name);
return sb.ToString();
}
}
+2 -2
View File
@@ -132,7 +132,7 @@ AboutDialog_prop:
MINVT 0 MINVT 0
PINI 3 PINI 3
PDAD 0, @const4 PDAD 0, @const4
JMPD 0, 7, 98 JMPD 0, 7, 98
PSHC @const8 PSHC @const8
MINVT 0 MINVT 0
@@ -235,7 +235,7 @@ AboutDialogContentUI_cont:
PGETT 19 PGETT 19
PINI 18 PINI 18
INIT 28 INIT 28
PLAD 10 PLAD 10 ;
COBJ 13 COBJ 13
PSHC @const24 PSHC @const24
PINI 7 PINI 7
+45 -2
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-16"?> <?xml version="1.0" encoding="utf-16"?>
<UIX xmlns="http://schemas.microsoft.com/2007/uix" xmlns:dialog="res://UIXControls!Dialog.uix" xmlns:iris="assembly://UIX/Microsoft.Iris" xmlns:zuneUI="assembly://ZuneShell/ZuneUI" xmlns:system="assembly://System.Private.CoreLib/System"> <UIX xmlns="http://schemas.microsoft.com/2007/uix" xmlns:dialog="res://UIXControls!Dialog.uix" xmlns:iris="assembly://UIX/Microsoft.Iris" xmlns:zuneUI="assembly://ZuneShell/ZuneUI" xmlns:system="assembly://System.Private.CoreLib/System" xmlns:styles="res://ZuneShellResources!Styles.uix" xmlns:label="res://UIXControls!Label.uix" xmlns:linkButtons="res://ZuneShellResources!LinkButtons.uix">
<Class Name="AboutDialog" Base="dialog:Dialog"> <Class Name="AboutDialog" Base="dialog:Dialog">
<Properties> <Properties>
<String String="res://ZuneShellResources!AboutDialog.uix#AboutDialogContentUI" Name="ContentUI" /> <String String="res://ZuneShellResources!AboutDialog.uix#AboutDialogContentUI" Name="ContentUI" />
@@ -8,5 +8,48 @@
<system:String String="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_TITLE)}" Name="AccessibleDescription" /> <system:String String="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_TITLE)}" Name="AccessibleDescription" />
</Properties> </Properties>
</Class> </Class>
<UI Name="AboutDialogContentUI" Base="dialog:DialogContentUI" /> <UI Name="AboutDialogContentUI" Base="dialog:DialogContentUI">
<Content xmlns="">
<Panel MaximumSize="360, 0" xmlns="http://schemas.microsoft.com/2007/uix">
<Layout xmlns="">
<FlowLayout DefaultChildAlignment="Near" Orientation="Vertical" xmlns="http://schemas.microsoft.com/2007/uix" />
</Layout>
<Children>
<Panel Margins="10">
<Layout xmlns="">
<FlowLayout Orientation="Horizontal" xmlns="http://schemas.microsoft.com/2007/uix" />
</Layout>
<Children>
<Graphic Content="{styles:Styles.IconZuneAbout}" StretchingPolicy="Uniform" SizingPolicy="SizeToContent" />
<Panel Margins="10, 10, 0, 10">
<Layout xmlns="">
<DockLayout DefaultLayoutInput="Top" xmlns="http://schemas.microsoft.com/2007/uix" />
</Layout>
<Children>
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_COPYRIGHT_LINE1)}" Style="{styles:SharedStyles.DialogTextStyle}" />
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_COPYRIGHT_LINE2)}" Style="{styles:SharedStyles.DialogTextStyle}" />
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_COPYRIGHT_LINE3)}" Style="{styles:SharedStyles.DialogTextStyle}" />
<Panel Layout="HorizontalFlow">
<Children>
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_VERSION_HEADER)}" Style="{styles:SharedStyles.DialogTextStyle}" />
<label:Label Content="{zuneUI:ZuneShell.DefaultInstance.Management.BuildNumber}" Margins="5, 0, 0, 0" Style="{styles:SharedStyles.DialogTextStyle}" />
</Children>
</Panel>
<Panel Layout="HorizontalFlow">
<Children>
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_PRODUCT_ID)}" Style="{styles:SharedStyles.DialogTextStyle}" />
<label:Label Content="{zuneUI:SoftwareUpdates.PID}" Margins="5, 0, 0, 0" Style="{styles:SharedStyles.DialogTextStyle}" />
</Children>
</Panel>
<label:Label Name="GDIModeLabel" Visible="false" Style="{styles:SharedStyles.DialogTextStyle}" />
</Children>
</Panel>
</Children>
</Panel>
<label:Label Content="{zuneUI:Shell.LoadString(zuneUI:StringId.IDS_ABOUTDIALOG_WARNING)}" Style="{styles:SharedStyles.DialogTextStyle}" WordWrap="true" />
<linkButtons:ExternalLink Model="{Dialog.TechSupportLink}" ToolTipEnabled="false" TileMinSize="160, 16" Margins="0, 10, 0, 0" />
</Children>
</Panel>
</Content>
</UI>
</UIX> </UIX>