Skip to content

Commit 2983766

Browse files
committed
feat: PTF Save
- Added basic PTF save that outputs correctly
1 parent 3a6408d commit 2983766

File tree

2 files changed

+101
-14
lines changed

2 files changed

+101
-14
lines changed

src/StudioCore/Core/Data/ResourceDescriptor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public ResourceDescriptor(string path)
6666
BaseName = Name.Split("__")[0];
6767
Postfix = Name.Split("__")[1];
6868
}
69+
else
70+
{
71+
BaseName = Name;
72+
}
6973

7074
var directory = $"{path}".Replace($"{Name}{Extension}", "");
7175

src/StudioCore/Editors/TableEditor/Framework/TableSaveHandler.cs

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.VisualBasic;
33
using StudioCore.Core.Data;
44
using StudioCore.Editors.TextEditor.Views;
5+
using StudioCore.Platform;
56
using System;
67
using System.Collections.Generic;
78
using System.IO;
@@ -62,18 +63,25 @@ public static void PackageAll()
6263
ManifestHandler.CreateManisfestIfMissing();
6364
}
6465

66+
private static List<string> ExcludedTables = new List<string>();
6567

6668
public static void ExportPTF()
6769
{
6870
var status = Warbox.TableEditor.FileSelectionView.GetSelectedDocumentStatus();
6971

72+
if(ExcludedTables.Contains(status.Name))
73+
{
74+
PlatformUtils.Instance.MessageBox("This table does not support patching.", "Warning", MessageBoxButtons.OK);
75+
return;
76+
}
77+
7078
foreach (var entry in DataHandler.Tables)
7179
{
7280
var vanillaEntry = DataHandler.Vanilla_Tables.Where(e => e.Key.Name == entry.Key.Name).FirstOrDefault();
7381

7482
if (entry.Key.Name == status.Name)
7583
{
76-
(bool, string, XDocument) result = BuildPatchDocument(entry.Key, entry.Value, vanillaEntry.Value);
84+
(bool, string, XDocument) result = BuildOutputDocument(entry.Key, entry.Value, vanillaEntry.Value);
7785
//(bool, string, XDocument) result = RemoveVanillaEntries(entry.Key, entry.Value, vanillaEntry.Value);
7886

7987
if(result.Item1 && result.Item3 != null)
@@ -130,27 +138,99 @@ private static void SaveTable(string saveDir, ResourceDescriptor resDesc, XDocum
130138
}
131139
}
132140

133-
private static Dictionary<string, List<RemovalTag>> Removals = new();
134-
135-
private static (bool, string, XDocument) BuildPatchDocument(ResourceDescriptor resDesc, XDocument baseDoc, XDocument vanillaDoc)
141+
private static (bool, string, XDocument) BuildOutputDocument(ResourceDescriptor resDesc, XDocument baseDoc, XDocument vanillaDoc)
136142
{
143+
var tableDef = TableDefinition.Definitions.Where(e => e.Attribute("Name").Value == resDesc.BaseName).FirstOrDefault();
144+
137145
var tempDoc = new XDocument(baseDoc);
146+
var outputDoc = new XDocument(baseDoc);
147+
148+
if (tableDef == null)
149+
{
150+
return (true, "Table is not defined in TableDefinitions.xml.", tempDoc);
151+
}
138152

139-
var databaseTier = tempDoc.Elements().ToList();
140-
var propertyGroupTier = tempDoc.Elements().Elements().ToList();
141-
var propertyEntryTier = tempDoc.Elements().Elements().Elements().ToList();
153+
// Clear all entries in the output doc
154+
XElement container = outputDoc.Elements().Elements().FirstOrDefault();
155+
if(container == null)
156+
{
157+
return (true, "Failed to find XML container element", tempDoc);
158+
}
159+
160+
container.Elements().Remove();
161+
162+
// This is used to link X entry (base) with Y entry (vanilla) so the attributes can then be compared
163+
var primaryKeyAttribute = tableDef.Attribute("RowNameKey");
164+
165+
// If the primary key is not defined at all, return.
166+
if (primaryKeyAttribute == null)
167+
{
168+
return (true, "RowNameKey is not set in TableDefinitions.xml for this table.", tempDoc);
169+
}
142170

143-
foreach (var entry in propertyEntryTier)
171+
// If the primary key is blank, this table cannot be supported.
172+
if(primaryKeyAttribute.Value == "")
144173
{
145-
// Attributes on this tier
146-
var attributes = entry.Attributes().ToList();
174+
return (true, "Patching is not supported for this table.", tempDoc);
175+
}
147176

148-
foreach (var attribute in attributes)
177+
// Find entries that should be added to output doc
178+
var baseEntries = tempDoc.Elements().Elements().Elements().ToList();
179+
var vanillaEntries = vanillaDoc.Elements().Elements().Elements().ToList();
180+
181+
foreach (var entry in baseEntries)
182+
{
183+
var keyAtttribute = entry.Attribute(primaryKeyAttribute.Value);
184+
185+
if (keyAtttribute == null)
186+
continue;
187+
188+
var primaryKey = keyAtttribute.Value;
189+
190+
// Get the vanilla entry based on the primary key
191+
var vanillaEntry = vanillaEntries.Where(
192+
e => e.Attribute(primaryKeyAttribute.Value) != null &&
193+
e.Attribute(primaryKeyAttribute.Value).Value == primaryKey).FirstOrDefault();
194+
195+
if (vanillaEntry == null)
196+
continue;
197+
198+
var addEntry = false;
199+
200+
// Element Value check
201+
if (entry.Value != vanillaEntry.Value)
149202
{
150-
var check = attribute.ToString();
151-
var stop = "";
203+
addEntry = true;
204+
// Add this entry to output doc
152205
}
153206

207+
// Attribute Value check
208+
var baseAttributes = entry.Attributes().ToList();
209+
var vanillaAttributes = vanillaEntry.Attributes().ToList();
210+
211+
foreach(var bAttribute in baseAttributes)
212+
{
213+
var attributeName = bAttribute.Name;
214+
var vanillaEqual = vanillaAttributes.Where(e => e.Name == attributeName).FirstOrDefault();
215+
216+
if (vanillaEqual == null)
217+
continue;
218+
219+
if(bAttribute.Value != vanillaEqual.Value)
220+
{
221+
addEntry = true;
222+
// Add this entry to output doc
223+
}
224+
}
225+
226+
// Add the entry to the output doc if there is a difference found
227+
if(addEntry)
228+
{
229+
container.Add(entry);
230+
}
231+
232+
// TODO: add checking for sub list and sub-sub list attributes so we can add the parent element if they differ
233+
/*
154234
// Inner tier 1
155235
var subListTier = entry.Elements().ToList();
156236
foreach (var subEntry in subListTier)
@@ -178,10 +258,13 @@ private static (bool, string, XDocument) BuildPatchDocument(ResourceDescriptor r
178258
}
179259
}
180260
}
261+
*/
181262
}
182263

183-
return (true, "", tempDoc);
264+
return (true, "", outputDoc);
184265
}
266+
267+
private static Dictionary<string, List<RemovalTag>> Removals = new();
185268

186269
// TODO: fix this so it works with nested elements
187270
private static (bool, string, XDocument) RemoveVanillaEntries(ResourceDescriptor resDesc, XDocument baseDoc, XDocument vanillaDoc)

0 commit comments

Comments
 (0)