Skip to content

Commit

Permalink
implement Codewalker.Core by @dexyfex - so user can skip "export/impo…
Browse files Browse the repository at this point in the history
…rt XML" step, and save file as .ymt
  • Loading branch information
grzybeek committed Jun 8, 2021
1 parent 9985f0f commit e1c0a7a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
8 changes: 7 additions & 1 deletion YMTEditor/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" IsEnabled="False" />
<Separator />
<MenuItem Header="_Open (YMT)" Click="OpenYMT_Click"/>
<MenuItem Header="_Save (YMT)" Click="SaveYMT_Click" />

<Separator />
<MenuItem Header="_Open (XML)" Click="OpenXML_Click"/>
<MenuItem Header="_Save" Click="SaveXML_Click" />
<MenuItem Header="_Save (XML)" Click="SaveXML_Click" />

<Separator />
<MenuItem Header="_Exit" IsEnabled="False" />
</MenuItem>
Expand Down
46 changes: 35 additions & 11 deletions YMTEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public partial class MainWindow : Window

public static ObservableCollection<ComponentData> Components;
public static ObservableCollection<PropData> Props;
private RpfFileEntry rpf;

public RpfFileEntry entry;

public MainWindow()
{
Expand Down Expand Up @@ -54,6 +55,7 @@ private void OpenXML_Click(object sender, RoutedEventArgs e)
if (result == true)
{
Components.Clear(); //so if we import another file when something is imported it will clear
Props.Clear();
string filename = xmlFile.FileName;
XMLHandler.LoadXML(filename);
_componentsMenu.IsEnabled = true;
Expand All @@ -66,7 +68,7 @@ private void OpenXML_Click(object sender, RoutedEventArgs e)

private void SaveXML_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog xmlFile = new OpenFileDialog
SaveFileDialog xmlFile = new SaveFileDialog
{
DefaultExt = ".ymt.xml",
Filter = "Codewalker YMT XML (*.ymt.xml)|*.ymt.xml"
Expand All @@ -85,7 +87,7 @@ private void OpenYMT_Click(object sender, RoutedEventArgs e)
OpenFileDialog ymtFile = new OpenFileDialog
{
DefaultExt = ".ymt",
Filter = "YMT (*.ymt)|*.ymt"
Filter = "Peds YMT (*.ymt)|*.ymt"
};
bool? result = ymtFile.ShowDialog();
if (result == true)
Expand All @@ -96,16 +98,39 @@ private void OpenYMT_Click(object sender, RoutedEventArgs e)
Components.Clear(); //so if we import another file when something is imported it will clear
Props.Clear();

//todo: loading from YMT file
PedFile ymt = new PedFile();
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);
}
}

private void SaveYMT_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog xmlFile = new SaveFileDialog
{
DefaultExt = ".ymt.xml",
Filter = "Peds YMT (*.ymt)|*.ymt"
};
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);

File.WriteAllBytes(filename, newYmtBytes);

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

Expand Down Expand Up @@ -175,7 +200,6 @@ private void Button_Click_AddProp(object sender, RoutedEventArgs e)
MessageBox.Show("Can't add more textures, limit is 26! (a-z)", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}


PropDrawable prop = Props.Where(p => p.propId == enumNumber).First().propList.Where(p => p.propIndex == index).First(); // get prop
int drawableToAddTexture = Props.Where(p => p.propId == enumNumber).First().propList.IndexOf(prop); //get index of our clicked prop in collection
Expand Down
26 changes: 25 additions & 1 deletion YMTEditor/XMLHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ class XMLHandler

public static void LoadXML(string filePath)
{
XDocument xmlFile = XDocument.Load(filePath);
XDocument xmlFile;
if (filePath.EndsWith(".xml"))
{
//loading *.ymt.xml
xmlFile = XDocument.Load(filePath);
}
else
{
//loading *.ymt
xmlFile = XDocument.Parse(filePath);
}

string usedPath = filePath;

CPedVariationInfo = xmlFile.Element("CPedVariationInfo").FirstAttribute.Value.ToString();
Expand Down Expand Up @@ -347,6 +358,19 @@ public static void SaveXML(string filePath)
MessageBox.Show("Saved to: " + filePath, "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
}

public static System.Xml.XmlDocument SaveYMT(string filePath)
{
XElement xmlFile = XML_Schema(filePath);
xmlFile.Save(filePath);

//create XmlDocument from XElement (codewalker.core requires XmlDocument)
var xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(xmlFile.CreateReader());

MessageBox.Show("Saved to: " + filePath, "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
return xmldoc;
}

public static String Number2String(int number, bool isCaps)
{
Char c = (Char)((isCaps ? 65 : 97) + (number));
Expand Down

0 comments on commit e1c0a7a

Please sign in to comment.