diff --git a/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs new file mode 100644 index 0000000..112d45a --- /dev/null +++ b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs @@ -0,0 +1,109 @@ +using System; +using System.Collections.Generic; +using CsvHelper; +using Studio23.SS2.DialogueSystem.Data; +using UnityEditor.Localization; +using UnityEditor.Localization.Plugins.CSV.Columns; +using UnityEngine; +using UnityEngine.Localization; +using UnityEngine.Localization.Metadata; +using UnityEngine.Localization.Tables; + + +/// +/// Writes custom DialogueGraph Fields . +/// +[Serializable] +public class DialogueGraphCustomColumns : CsvColumns, ISerializationCallbackReceiver +{ + private Dictionary IdToDialogueLineMap; + + private List ColNames = new() + { + "SpeakerName", + "Expression" + }; + + public DialogueGraphCustomColumns(Dictionary idToDialogueLineMap) + { + IdToDialogueLineMap = idToDialogueLineMap; + } + + /// + public override void WriteBegin(StringTableCollection collection, CsvWriter csvWriter) + { + var tables = collection.StringTables; + + foreach (var colName in ColNames) + { + csvWriter.WriteField(colName); + } + } + + /// + public override void ReadBegin(StringTableCollection collection, CsvReader csvReader) + { + //do nothing + } + + /// + public override void ReadEnd(StringTableCollection collection) + { + //do nothing + } + + /// + public override void ReadRow(SharedTableData.SharedTableEntry keyEntry, CsvReader reader) + { + //do nothing + } + + /// + public override void WriteRow(SharedTableData.SharedTableEntry keyEntry, IList tableEntries, + CsvWriter writer) + { + if (IdToDialogueLineMap.TryGetValue(keyEntry.Id, out var dialogueLineNodeBase)) + { + if (dialogueLineNodeBase.SpeakerData.Character != null) + { + writer.WriteField(dialogueLineNodeBase.SpeakerData.Character.CharacterName); + } + else + { + writer.WriteField(""); + } + + if (dialogueLineNodeBase.SpeakerData.Expression != null) + { + writer.WriteField(dialogueLineNodeBase.SpeakerData.Expression.ExpressionName); + } + else + { + writer.WriteField(""); + } + } + else + { + //write empty + foreach (var colName in ColNames) + { + writer.WriteField(""); + } + } + } + + /// + /// Sets the field names to their default values. + /// The default values are for + /// and + " Comments" for . + /// + public void OnBeforeSerialize() + { + //do nothing + } + + public void OnAfterDeserialize() + { + //do nothing + } +} \ No newline at end of file diff --git a/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs.meta b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs.meta new file mode 100644 index 0000000..844a676 --- /dev/null +++ b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphCustomColumns.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c3d674b468f246189eff18d8af2c7121 +timeCreated: 1722495499 \ No newline at end of file diff --git a/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphExportWindow.cs b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphExportWindow.cs index 254e627..3d6e6c6 100644 --- a/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphExportWindow.cs +++ b/Assets/Packages/com.studio23.ss2.dialoguesystem/Editor/DialogueGraphExportWindow.cs @@ -24,9 +24,7 @@ public class DialogueGraphExportWindow: EditorWindow { private DialogueGraph dialogueGraph; private List traversedLinesCache = new(); - - private bool ShouldExportGraphs = true; - private bool ShouldExportSpeakerData = false; + private bool ExportNodeData = true; [MenuItem("Studio-23/Dialogue-System/Dialogue Graph Export")] public static void ShowWindow() @@ -40,8 +38,8 @@ private void OnGUI() GUILayout.Label("Dialogue Graph Editor", EditorStyles.boldLabel); dialogueGraph = (DialogueGraph)EditorGUILayout.ObjectField("Dialogue Graph", dialogueGraph, typeof(DialogueGraph), false); - ShouldExportGraphs = EditorGUILayout.Toggle("Should Export Graphs", ShouldExportGraphs); - ShouldExportSpeakerData = EditorGUILayout.Toggle("Should Export Speaker Data", ShouldExportSpeakerData); + ExportNodeData = EditorGUILayout.Toggle("Export Node Data", ExportNodeData); + if (GUILayout.Button("Export graph")) { ExportSingleGraphToCSV(dialogueGraph); @@ -65,12 +63,24 @@ private void ExportSelected() { if (o is DialogueGraph graph) { - var filePath = Path.Combine(folderPath , $"{graph.name}.csv"); - ExportToCSV(graph, filePath); + try + { + var filePath = Path.Combine(folderPath , GetGraphCSVName(graph)); + ExportToCSV(graph, filePath); + } + catch (Exception e) + { + Debug.LogError($"Error Exporting graph {graph} \n {e}", graph); + } } } } + private static string GetGraphCSVName(DialogueGraph graph) + { + return $"{graph.name}.csv"; + } + private void ExportAll() { string folderPath = EditorUtility.OpenFolderPanel("Export graph", "", ""); @@ -78,8 +88,15 @@ private void ExportAll() return; foreach (var graph in GetAllGraphs()) { - var filePath = Path.Combine(folderPath , $"{graph.name}.csv"); - ExportToCSV(graph, filePath); + try + { + var filePath = Path.Combine(folderPath , GetGraphCSVName(graph)); + ExportToCSV(graph, filePath); + } + catch (Exception e) + { + Debug.LogError($"Error Exporting graph {graph} \n {e}", graph); + } } } @@ -97,7 +114,7 @@ public List GetAllGraphs() } private void ExportSingleGraphToCSV(DialogueGraph graph) { - string filePath = EditorUtility.SaveFilePanel("Export graph", "", $"{graph.name}.csv", "csv"); + string filePath = EditorUtility.SaveFilePanel("Export graph", "", GetGraphCSVName(graph), "csv"); if (string.IsNullOrEmpty(filePath)) return; ExportToCSV(graph, filePath); @@ -108,24 +125,13 @@ private void ExportToCSV(DialogueGraph graph, string filePath) { TraverseNodes(graph, HandleDialogueLineNodeTraversed); string folderPath = Path.GetDirectoryName(filePath); - if (ShouldExportGraphs) + if (File.Exists(filePath)) { - if (File.Exists(filePath)) - { - File.Delete(filePath); - } - - - if (traversedLinesCache.Count > 0) - { - ExportToCSV(graph,filePath, traversedLinesCache); - } + File.Delete(filePath); } - - if (ShouldExportSpeakerData) + if (traversedLinesCache.Count > 0) { - var speakerPath = Path.Combine(folderPath, $"{graph.name}_SpeakerData.csv"); - ExportSpeakerData(traversedLinesCache, speakerPath); + ExportToCSV(graph,filePath, traversedLinesCache); } } @@ -137,25 +143,45 @@ private static SharedTableData.SharedTableEntry GetTableEntry(DialogueLineNodeBa return table.SharedData.GetEntryFromReference(dialogue.DialogueLocalizedString.TableEntryReference); } - public static void ExportToCSV(DialogueGraph graph, string filePath, List dialogueLinesInOrder) + public void ExportToCSV(DialogueGraph graph, string filePath, List dialogueLinesInOrder) { ExportStringTable(graph, filePath, dialogueLinesInOrder); } - private static void ExportStringTable(DialogueGraph graph, string path, List dialogueLinesInOrder) + private void ExportStringTable(DialogueGraph graph, string path, List dialogueLinesInOrder) { - var dialogueGraphLineIds = dialogueLinesInOrder - .Where(l=> GetTableEntry(l) != null) - .Select(l => GetTableEntry(l).Id) - .ToHashSet(); + var dialogueGraphLineIds = new HashSet(); + foreach (var dialogueLineNodeBase in dialogueLinesInOrder) + { + var entry = GetTableEntry(dialogueLineNodeBase); + if (entry != null) + { + dialogueGraphLineIds.Add(entry.Id); + } + } // var csvColumnsList = ColumnMapping.CreateDefaultMapping(); var columnMappings = new List(); + columnMappings.Add(new KeyIdColumns { IncludeId = true, // Include the Id column field. IncludeSharedComments = false, // Include Shared comments. }); + + if (ExportNodeData) + { + var idToDialogueLineGraphMap = new Dictionary(); + foreach (var dialogueLineNodeBase in dialogueLinesInOrder) + { + var entry = GetTableEntry(dialogueLineNodeBase); + if (entry != null) + { + idToDialogueLineGraphMap.TryAdd(entry.Id, dialogueLineNodeBase); + } + } + columnMappings.Add(new DialogueGraphCustomColumns(idToDialogueLineGraphMap)); + } foreach (var locale in LocalizationSettings.AvailableLocales.Locales) { @@ -175,44 +201,10 @@ private static void ExportStringTable(DialogueGraph graph, string path, List - /// #TODO temporary measure till I figure out columnmapping - /// - /// - /// - public void ExportSpeakerData(List dialogueLinesInOrder, string path) - { - // StringBuilder to construct the CSV content - StringBuilder csvContent = new StringBuilder(); - - // Append the header - csvContent.AppendLine("Id,Speaker, Expression"); - - // Append each dialogue line - foreach (var line in dialogueLinesInOrder) - { - var entry = GetTableEntry(line); - if (entry != null) - { - if (line.SpeakerData.Character != null) - { - csvContent.AppendLine($"{entry.Id},{line.SpeakerData.Character.CharacterName}, {line.SpeakerData.Expression.name}"); - } - else - { - csvContent.AppendLine($"{entry.Id},,"); - } - } - } - - // Write the content to the file - File.WriteAllText(path, csvContent.ToString()); - } - /// /// Basically a clone of Unity's CSV Exporter /// We need to mimic it because our exported csv's @@ -225,7 +217,7 @@ public void ExportSpeakerData(List dialogueLinesInOrder, s /// /// /// - public static void ExportStringTable(TextWriter writer, StringTableCollection collection, IList columnMappings, List dialigueLinesInOrder, HashSet allowedIds) + public static void ExportStringTable(DialogueGraph graph, TextWriter writer, StringTableCollection collection, IList columnMappings, List dialigueLinesInOrder, HashSet allowedIds) { if (writer == null) throw new ArgumentNullException(nameof(writer)); @@ -236,7 +228,7 @@ public static void ExportStringTable(TextWriter writer, StringTableCollection co { try { - Debug.Log("Writing Headers"); + Debug.Log($"Writing Headers: {graph}", graph); foreach (var cell in columnMappings) { cell.WriteBegin(collection, csvWriter); @@ -280,7 +272,7 @@ public static void ExportStringTable(TextWriter writer, StringTableCollection co rows.Sort( (r1, r2) => IdToOrderMap[r1.KeyEntry.Id].CompareTo(IdToOrderMap[r2.KeyEntry.Id]) ); - Debug.Log($"Writing Contents: {rows.Count()} lines"); + Debug.Log($"Writing Contents({rows.Count()} lines): {graph} ", graph); foreach (var row in rows) { csvWriter.NextRecord(); @@ -300,7 +292,7 @@ public static void ExportStringTable(TextWriter writer, StringTableCollection co } catch (Exception e) { - Debug.Log("Failed Exporting.\n" + e.Message); + Debug.LogError($"Failed Exporting. {graph}\n" + e.Message, graph); throw; } }