Skip to content

Commit

Permalink
XML support improvements
Browse files Browse the repository at this point in the history
- Added ability to export/import Translokator to/from XML
- XML Reflection -> Objects will now use their correct type converters when converting to/from XML
- HashName now exports/imports to/from XML correctly
  • Loading branch information
Kamzik123 committed Nov 28, 2023
1 parent f84f626 commit 1e4f1aa
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 20 deletions.
23 changes: 22 additions & 1 deletion Mafia2Libs/Forms/TranslokatorEditor.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 38 additions & 4 deletions Mafia2Libs/Forms/TranslokatorEditor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using ResourceTypes.Translokator;
using System;
using System.IO;
using ResourceTypes.Translokator;
using System.Windows.Forms;
using Utils.Language;
using Utils.Helpers;

namespace Mafia2Tool.Forms
{
Expand Down Expand Up @@ -43,6 +42,12 @@ private void Localise()
private void LoadFile()
{
translokator = new TranslokatorLoader(file);

LoadData();
}

private void LoadData()
{
TranslokatorTree.Nodes.Clear();

TreeNode headerData = new TreeNode("Header Data");
Expand Down Expand Up @@ -402,6 +407,35 @@ private void ReloadButton_Click(object sender, EventArgs e)
private void CopyButton_Click(object sender, EventArgs e) => Copy();
private void PasteButton_Click(object sender, EventArgs e) => Paste();
private void SaveToolButton_Click(object sender, EventArgs e) => SaveFile();
private void ExitButton_Click(object sender, EventArgs e) => Close();
private void ExitButton_Click(object sender, EventArgs e) => Close();
private void Button_ExportXml_OnClick(object sender, System.EventArgs e)
{
SaveFileDialog saveFile = new SaveFileDialog();
saveFile.Filter = "XML|*.XML";

if (saveFile.ShowDialog() == DialogResult.OK)
{
translokator.ConvertToXML(saveFile.FileName);
}
}

private void Button_ImportXml_OnClick(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "XML|*.XML";
openFileDialog.Multiselect = false;
openFileDialog.CheckFileExists = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string FileToOpen = openFileDialog.FileName;
if (File.Exists(FileToOpen))
{
translokator.ConvertFromXML(FileToOpen);

LoadData();
}
}
}
}
}
51 changes: 40 additions & 11 deletions Mafia2Libs/ResourceTypes/FileTypes/Translokator/Translokator.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using System.Numerics;
using ResourceTypes.Actors;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using Vortice.Mathematics;
using System.Numerics;
using System.Xml.Linq;
using Utils.Extensions;
using Utils.VorticeUtils;
using Utils.Helpers.Reflection;
using Utils.Logging;
using Utils.StringHelpers;
using Utils.Types;
using Utils.Logging;
using ResourceTypes.Actors;
using Utils.VorticeUtils;
using Vortice.Mathematics;

namespace ResourceTypes.Translokator
{
Expand Down Expand Up @@ -139,7 +140,7 @@ public class Object
int numInstances;
Instance[] instances;

[Browsable(false)]
[Browsable(false), PropertyIgnoreByReflector]
public short NumInstance2 {
get { return numInstance2; }
set { numInstance2 = value; }
Expand All @@ -165,7 +166,7 @@ public float GridMin {
get { return gridMin; }
set { gridMin = value; }
}
[Browsable(false)]
[Browsable(false), PropertyIgnoreByReflector]
public int NumInstances {
get { return numInstances; }
set { numInstances = value; }
Expand Down Expand Up @@ -216,8 +217,8 @@ public struct ObjectGroupMetaInfo

public class TranslokatorLoader
{
public Grid[] Grids;
public ObjectGroup[] ObjectGroups;
public Grid[] Grids { get; set; }
public ObjectGroup[] ObjectGroups { get; set; }

int version;
int unk1;
Expand All @@ -236,11 +237,17 @@ public short Unk2 {
get { return unk2; }
set { unk2 = value; }
}
[PropertyIgnoreByReflector]
public BoundingBox Bounds {
get { return bounds; }
set { bounds = value; }
}

public TranslokatorLoader()
{

}

public TranslokatorLoader(FileInfo info)
{
using (BinaryReader reader = new BinaryReader(File.Open(info.FullName, FileMode.Open)))
Expand Down Expand Up @@ -686,6 +693,8 @@ private void InternalWriteToFile(BinaryWriter writer)
for (int x = 0; x < objectGroup.Objects.Length; x++)
{
Object obj = objectGroup.Objects[x];
obj.NumInstance2 = (short)obj.Instances.Length;
obj.NumInstances = obj.Instances.Length;
writer.Write(obj.NumInstance2);
writer.Write(obj.Unk02);
writer.Write(obj.Name.Hash);
Expand All @@ -711,5 +720,25 @@ private void InternalWriteToFile(BinaryWriter writer)
ObjectGroups[i] = objectGroup;
}
}

public void ConvertToXML(string Filename)
{
XElement Root = ReflectionHelpers.ConvertPropertyToXML(this);
Root.Save(Filename);
}

public void ConvertFromXML(string Filename)
{
XElement LoadedDoc = XElement.Load(Filename);
TranslokatorLoader FileContents = ReflectionHelpers.ConvertToPropertyFromXML<TranslokatorLoader>(LoadedDoc);

// Copy data taken from loaded XML
Grids = FileContents.Grids;
ObjectGroups = FileContents.ObjectGroups;
Version = FileContents.Version;
Unk1 = FileContents.Unk1;
Unk2 = FileContents.Unk2;
Bounds = FileContents.Bounds;
}
}
}
31 changes: 29 additions & 2 deletions Mafia2Libs/Utils/Helpers/Reflection/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using ResourceTypes.Prefab.Vehicle;
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using static System.Resources.ResXFileRef;

namespace Utils.Helpers.Reflection
{
Expand Down Expand Up @@ -157,7 +159,17 @@ public static T ConvertToPropertyFromXML<T>(XElement Node)
}
else
{
Info.SetValue(TypedObject, Convert.ChangeType(NodeContent, Info.PropertyType));
var props = TypeDescriptor.GetProperties(TypedObject);
var converter = props[Info.Name].Converter;

if (converter.CanConvertFrom(NodeContent.GetType()))
{
Info.SetValue(TypedObject, converter.ConvertFromInvariantString(NodeContent));
}
else
{
Info.SetValue(TypedObject, Convert.ChangeType(NodeContent, Info.PropertyType));
}
}
}
}
Expand Down Expand Up @@ -279,7 +291,17 @@ private static object InternalConvertProperty(XElement Node, Type ElementType)
}
else
{
Info.SetValue(TypedObject, Convert.ChangeType(NodeContent, Info.PropertyType));
var props = TypeDescriptor.GetProperties(TypedObject);
var converter = props[Info.Name].Converter;

if (converter.CanConvertFrom(NodeContent.GetType()))
{
Info.SetValue(TypedObject, converter.ConvertFromInvariantString(NodeContent));
}
else
{
Info.SetValue(TypedObject, Convert.ChangeType(NodeContent, Info.PropertyType));
}
}
}
}
Expand Down Expand Up @@ -366,11 +388,16 @@ private static void ConvertObject<TObject>(XElement Element, TObject PropertyDat
}
else
{
var props = TypeDescriptor.GetProperties(PropertyData);
var converter = props[Info.Name].Converter;

object info = PropertyData.GetType().GetProperty(Info.Name).GetValue(PropertyData);

// Sanity check for null
info = (info != null ? info : "");

info = converter.ConvertToString(info);

if (bForceAsAttribute)
{
Element.Add(new XAttribute(Info.Name, info));
Expand Down
5 changes: 3 additions & 2 deletions Mafia2Libs/Utils/Types/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
using Utils.StringHelpers;
using Utils.Models;
using System;
using Utils.Helpers.Reflection;

namespace Utils.Types
{
[TypeConverter(typeof(ExpandableObjectConverter))]
[TypeConverter(typeof(ExpandableObjectConverter)), PropertyClassAllowReflection]
public class HashName
{
ulong hash;
Expand All @@ -24,7 +25,7 @@ public string String {
set { Set(value); }
}

[ReadOnly(true)]
[ReadOnly(true), PropertyIgnoreByReflector]
public string Hex {
get { return string.Format("{0:X}", hash); }
}
Expand Down

0 comments on commit 1e4f1aa

Please sign in to comment.