Skip to content

Commit

Permalink
added update checker on launch
Browse files Browse the repository at this point in the history
fix metatool availComp
fix ymt's that have more <item> in components than defined in <availComp>
error pop-up on failed loading, it shouldn't close itself now
  • Loading branch information
grzybeek committed Jul 2, 2021
1 parent 7de5d9d commit 96a6499
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 48 deletions.
133 changes: 102 additions & 31 deletions YMTEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using CodeWalker.GameFiles;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Navigation;

namespace YMTEditor
Expand All @@ -30,6 +29,7 @@ public partial class MainWindow : Window

public MainWindow()
{

InitializeComponent();

_componentsMenu = (MenuItem)FindName("ComponentsMenu");
Expand All @@ -45,6 +45,8 @@ public MainWindow()

Props = new ObservableCollection<PropData>();
PropsItemsControl.ItemsSource = Props;

CheckForUpdates();
}

private void OpenNEW_Click(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -82,16 +84,26 @@ private void OpenXML_Click(object sender, RoutedEventArgs e)
ClearEverything(); //so if we import another file when something is imported it will clear

string filename = xmlFile.FileName;
XMLHandler.LoadXML(filename);
_componentsMenu.IsEnabled = true;
_componentsMenu.ToolTip = "Check/Uncheck components";
_propsMenu.IsEnabled = true;
_propsMenu.ToolTip = "Check/Uncheck props";
SetLogMessage("Loaded XML from path: " + filename);
try
{
XMLHandler.LoadXML(filename);
_componentsMenu.IsEnabled = true;
_componentsMenu.ToolTip = "Check/Uncheck components";
_propsMenu.IsEnabled = true;
_propsMenu.ToolTip = "Check/Uncheck props";
SetLogMessage("Loaded XML from path: " + filename);

fullName = Path.GetFileNameWithoutExtension(filename);
fullName = Path.GetFileNameWithoutExtension(filename); //removes .xml
fullName = Path.GetFileNameWithoutExtension(fullName); //removes .ymt

this.Title = "YMTEditor by grzybeek - editing " + fullName + ".ymt.xml";
}
catch (Exception)
{
ClearEverything();
MessageBox.Show("Failed to load XML YMT, please report it!\n\nReport it on github or discord: grzybeek#9100\nPlease include XML YMT you tried to load!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}

this.Title = "YMTEditor by grzybeek - editing " + fullName + ".ymt.xml";
}
}

Expand All @@ -106,9 +118,17 @@ private void SaveXML_Click(object sender, RoutedEventArgs e)
bool? result = xmlFile.ShowDialog();
if (result == true)
{
string filename = xmlFile.FileName;
XMLHandler.SaveXML(filename);
SetLogMessage("Saved XML to path: " + filename);
try
{
string filename = xmlFile.FileName;
XMLHandler.SaveXML(filename);
SetLogMessage("Saved XML to path: " + filename);
}
catch (Exception)
{
MessageBox.Show("Failed to save XML YMT, please report it!\n\nReport it on github or discord: grzybeek#9100\nPlease include XML YMT you tried to save!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
}

Expand All @@ -131,16 +151,25 @@ private void OpenYMT_Click(object sender, RoutedEventArgs e)
RpfFile.LoadResourceFile<PedFile>(ymt, ymtBytes, 2);
string xml = MetaXml.GetXml(ymt.Meta);

XMLHandler.LoadXML(xml);
_componentsMenu.IsEnabled = true;
_componentsMenu.ToolTip = "Check/Uncheck components";
_propsMenu.IsEnabled = true;
_propsMenu.ToolTip = "Check/Uncheck props";
SetLogMessage("Loaded YMT from path: " + filename);
try
{
XMLHandler.LoadXML(xml);
_componentsMenu.IsEnabled = true;
_componentsMenu.ToolTip = "Check/Uncheck components";
_propsMenu.IsEnabled = true;
_propsMenu.ToolTip = "Check/Uncheck props";
SetLogMessage("Loaded YMT from path: " + filename);

fullName = Path.GetFileNameWithoutExtension(filename);
fullName = Path.GetFileNameWithoutExtension(filename);

this.Title = "YMTEditor by grzybeek - editing " + fullName + ".ymt";
this.Title = "YMTEditor by grzybeek - editing " + fullName + ".ymt";
}
catch (Exception)
{
ClearEverything();
MessageBox.Show("Failed to load YMT, please report it!\n\nReport it on github or discord: grzybeek#9100\nPlease include YMT you tried to load!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
}

Expand All @@ -155,16 +184,25 @@ private void SaveYMT_Click(object sender, RoutedEventArgs e)
bool? result = xmlFile.ShowDialog();
if (result == true)
{
string filename = xmlFile.FileName;
System.Xml.XmlDocument newXml = XMLHandler.SaveYMT(filename);

PedFile ymt = new PedFile();
Meta meta = XmlMeta.GetMeta(newXml);
byte[] newYmtBytes = ResourceBuilder.Build(meta, 2);
try
{
string filename = xmlFile.FileName;
System.Xml.XmlDocument newXml = XMLHandler.SaveYMT(filename);

File.WriteAllBytes(filename, newYmtBytes);

SetLogMessage("Saved YMT to path: " + filename);
PedFile ymt = new PedFile();
Meta meta = XmlMeta.GetMeta(newXml);
byte[] newYmtBytes = ResourceBuilder.Build(meta, 2);

File.WriteAllBytes(filename, newYmtBytes);

SetLogMessage("Saved YMT to path: " + filename);

}
catch (Exception)
{
MessageBox.Show("Failed to save YMT, please report it!\n\nReport it on github or discord: grzybeek#9100\nPlease include YMT you tried to save!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}

}
}

Expand Down Expand Up @@ -639,5 +677,38 @@ private void ClearEverything()
Props.Clear();

}

//version compare taken and edited from https://github.com/smallo92/Ymap-YbnMover/blob/master/ymapmover/Startup.cs
private void CheckForUpdates()
{
Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion.ToString();

WebClient webclient = new WebClient();
Stream stream = webclient.OpenRead("https://raw.githubusercontent.com/grzybeek/YMTEditor/master/YMTEditor/version.txt");
StreamReader reader = new StreamReader(stream);

string githubVersion = reader.ReadToEnd().ToString();

if(version != githubVersion)
{
MessageBoxResult result = MessageBox.Show(this, "There is new version of YMTEditor available!\nYour version: " + version + "\nAvailable version: " + githubVersion + " \n\nDo you want to download it now?\n\nClick YES to open website and close editor\nClick NO to open editor", "New version available!", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (result == MessageBoxResult.Yes)
{
//probably better would be auto-updater but idk how to do it yet
Process.Start("https://github.com/grzybeek/YMTEditor/releases");
Environment.Exit(0);
}
else if (result == MessageBoxResult.No)
{
//open editor
}
}
else
{
//good version, open editor
}
}
}
}
8 changes: 4 additions & 4 deletions YMTEditor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
[assembly: AssemblyTitle("YMTEditor")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YMTEditor")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2021")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0")]
[assembly: AssemblyFileVersion("1.0")]
81 changes: 69 additions & 12 deletions YMTEditor/XMLHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static void LoadXML(string filePath)
//loading *.ymt
xmlFile = XDocument.Parse(filePath);
}

string usedPath = filePath;
CPedVariationInfo = xmlFile.Element("CPedVariationInfo").FirstAttribute != null
? xmlFile.Element("CPedVariationInfo").FirstAttribute.Value.ToString()
Expand All @@ -44,15 +45,38 @@ public static void LoadXML(string filePath)
//generate used components
foreach (var node in xmlFile.Descendants("availComp"))
{

var availComponents = node.Value.Split(' '); //split on space

if(availComponents[0].Length > 3)// that means we have weird availComp from metatool, for example: "00010203FF04FF...."
{
string s = availComponents[0].ToString();
List<String> temp = new List<String>();
int availlength = availComponents[0].Length;
int skipEvery = 2; //we have to split every 2 characters
for (int i = 0; i < availlength; i += skipEvery)
{
string a = s.Substring(i, skipEvery).Substring(1);

if(a == "F")
{
a = "255";
}

temp.Add(a);
}

availComponents = temp.ToArray();

}
int compId = 0; //components id's
int compIndex = 0; //order of our components in ymt
foreach(var comp in availComponents)
foreach (var comp in availComponents)
{
if(comp != "255")
if (comp != "255")
{
string _name = Enum.GetName(typeof(YMTTypes.ComponentNumbers), compId);
ComponentData componentName = new ComponentData(_name, compId, compIndex, new ObservableCollection<ComponentDrawable>()) { compHeader = _name.ToUpper()};
ComponentData componentName = new ComponentData(_name, compId, compIndex, new ObservableCollection<ComponentDrawable>()) { compHeader = _name.ToUpper() };
MainWindow.Components.Add(componentName);

MenuItem item = (MenuItem)MainWindow._componentsMenu.FindName(_name);
Expand All @@ -70,7 +94,7 @@ public static void LoadXML(string filePath)
foreach (var prop in xmlFile.Descendants("aPropMetaData").Elements("Item"))
{
int p_anchorId = Convert.ToInt32(prop.Element("anchorId").FirstAttribute.Value);

if (oldId != p_anchorId)
{
string _name = Enum.GetName(typeof(YMTTypes.PropNumbers), p_anchorId);
Expand All @@ -87,7 +111,13 @@ public static void LoadXML(string filePath)
//read components
int compItemIndex = 0; //order of our components in ymt
foreach (var node in xmlFile.Descendants("aComponentData3").Elements("Item"))
{
{
if (compItemIndex >= MainWindow.Components.Count())
{
//some ymt's have more <Item>'s in component section than defined in <availComp>, for example freemode male_heist or bikerdlc
//so just skip more than in availComp
return;
}
ComponentData _curComp = MainWindow.Components.ElementAt(compItemIndex); //current component (jbib/lowr/teef etc)
int _curCompDrawablesCount = 0; //count how many component has variations (000, 001, 002, etc)
int _curCompAvailTex = 0; // not used by game probably, total amount of textures component has (numAvailTex)
Expand Down Expand Up @@ -126,7 +156,31 @@ public static void LoadXML(string filePath)
{
string comphash_2FD08CEF = compInfo_node.Element("hash_2FD08CEF").Value.ToString(); //unknown usage
string comphash_FC507D28 = compInfo_node.Element("hash_FC507D28").Value.ToString(); //unknown usage
string[] comphash_07AE529D = compInfo_node.Element("hash_07AE529D").Value.Split(' '); //probably expressionMods(?) - used for heels for example

string[] comphash_07AE529D = null; //probably expressionMods(?) - used for heels for example
if (compInfo_node.Element("hash_07AE529D").Value.Length == 9) //normal "0 0 0 0 0"
{
comphash_07AE529D = compInfo_node.Element("hash_07AE529D").Value.Split(' ');
}
else if(compInfo_node.Element("hash_07AE529D").Value.Length == 10)//that means, we have weird metatool value "0000000000" without spaces
{
string s = compInfo_node.Element("hash_07AE529D").Value.ToString();
List<String> temp = new List<String>();
int stringlenght = s.Length;
int skipEvery = 2; //we have to split every 2 characters
for (int i = 0; i < stringlenght; i += skipEvery)
{
string a = s.Substring(i, skipEvery).Substring(1);
temp.Add(a);
}

comphash_07AE529D = temp.ToArray();
}
else //maybe there is other case and i don't about it, then just input zeros
{
comphash_07AE529D = new string[] { "0", "0", "0", "0", "0" };
}

int compflags = Convert.ToInt32(compInfo_node.Element("flags").FirstAttribute.Value); //unknown usage
string compinclusions = compInfo_node.Element("inclusions").Value.ToString(); //unknown usage
string compexclusions = compInfo_node.Element("exclusions").Value.ToString(); //unknown usage
Expand All @@ -138,9 +192,9 @@ public static void LoadXML(string filePath)

string _name = Enum.GetName(typeof(YMTTypes.ComponentNumbers), comphash_D12F579D);
int curCompIndex = ComponentData.GetComponentIndexByID(comphash_D12F579D);
if(curCompIndex != -1)
if (curCompIndex != -1)
{
MainWindow.Components.ElementAt(curCompIndex).compList.ElementAt(comphash_FA1F27BF).drawableInfo.Add(new ComponentInfo(comphash_2FD08CEF, comphash_FC507D28,
MainWindow.Components.ElementAt(curCompIndex).compList.ElementAt(comphash_FA1F27BF).drawableInfo.Add(new ComponentInfo(comphash_2FD08CEF, comphash_FC507D28,
comphash_07AE529D, compflags, compinclusions, compexclusions, comphash_6032815C, comphash_7E103C8B, comphash_D12F579D, comphash_FA1F27BF));
}
}
Expand Down Expand Up @@ -171,9 +225,9 @@ public static void LoadXML(string filePath)
}

PropData _curPropData = MainWindow.Props.Where(p => p.propId == p_anchorId).First();

PropDrawable _curPropDrawable = new PropDrawable(_curPropDrawableIndex, p_audioId, p_expressionMods, new ObservableCollection<PropTexture>(), p_renderFlag, p_propFlag, p_flag, p_anchorId, p_propId, p_hash);

int texturePropIndex = 0;
foreach (var texData in propMetaData.Descendants("texData").Elements("Item"))
{
Expand All @@ -195,8 +249,11 @@ public static void LoadXML(string filePath)
_curPropDrawableIndex++;
oldPropId = p_anchorId;
}

}



}

private static XElement XML_Schema(string filePath)
Expand Down
4 changes: 3 additions & 1 deletion YMTEditor/YMTEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@
<Install>false</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Resource Include="version.txt" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="AfterBuild">
<ItemGroup>
Expand Down

0 comments on commit 96a6499

Please sign in to comment.