From 9047c65d76605e7a59bc2aa3a2baefa1e66a766b Mon Sep 17 00:00:00 2001 From: Paulov Date: Mon, 1 May 2023 13:41:09 +0100 Subject: [PATCH] FMTProj compatibility with "other" games --- .../Windows/DefaultEditor.xaml.cs | 76 ++++++++++++++----- 1 file changed, 55 insertions(+), 21 deletions(-) diff --git a/FrostbiteModdingUI/Windows/DefaultEditor.xaml.cs b/FrostbiteModdingUI/Windows/DefaultEditor.xaml.cs index 4b04bdad..cef45de0 100644 --- a/FrostbiteModdingUI/Windows/DefaultEditor.xaml.cs +++ b/FrostbiteModdingUI/Windows/DefaultEditor.xaml.cs @@ -13,6 +13,7 @@ using FrostySdk.Frostbite; using FrostySdk.IO; using FrostySdk.Managers; +using FrostySdk.ModsAndProjects.Projects; using MahApps.Metro.Controls; using Microsoft.Win32; using Newtonsoft.Json; @@ -473,44 +474,77 @@ private async void btnProjectSave_Click(object sender, RoutedEventArgs e) await SaveProjectWithDialog(); } - private async Task SaveProjectWithDialog() + public virtual async Task SaveProjectWithDialog() { - loadingDialog.Update("Saving Project", "Sweeping up debris", 0); + loadingDialog.UpdateAsync("Saving Project", "Sweeping up debris", 0); + //borderLoading.Visibility = Visibility.Visible; //loadingDialog.Show(); await Task.Delay(100); // --------------------------------------------------------- // Remove chunks and actual unmodified files before writing - ChunkFileManager2022.CleanUpChunks(); - - loadingDialog.Update("", ""); - + //ChunkFileManager2022.CleanUpChunks(); + await loadingDialog.UpdateAsync("", ""); SaveFileDialog saveFileDialog = new SaveFileDialog(); - saveFileDialog.Filter = "Project files|*.fbproject"; + saveFileDialog.Filter = "FMTProject files|*.fmtproj|FBProject files|*.fbproject|All Files (*.*)|*.*"; var result = saveFileDialog.ShowDialog(); - if (result.HasValue && result.Value) - { - if (!string.IsNullOrEmpty(saveFileDialog.FileName)) - { - loadingDialog.Update("Saving Project", "Saving project to file", 0); - //loadingDialog.Show(); - await ProjectManagement.Project.SaveAsync(saveFileDialog.FileName, true); - - Log("Saved project successfully to " + saveFileDialog.FileName); + if (!result.HasValue || !result.Value) + return false; - UpdateWindowTitle(saveFileDialog.FileName); + if (string.IsNullOrEmpty(saveFileDialog.FileName)) + return false; - DiscordInterop.DiscordRpcClient.UpdateDetails("In Editor [" + GameInstanceSingleton.Instance.GAMEVERSION + "] - " + ProjectManagement.Project.DisplayName); + await loadingDialog.UpdateAsync("Saving Project", "Saving project to file"); + // FMT Project file type + if (saveFileDialog.FileName.EndsWith(".fmtproj", StringComparison.OrdinalIgnoreCase)) + { + //MessageBox.Show("FMTProj file type is EXPERIMENTAL. If concerned, use fbproject instead."); + // Same file -- Simple Update + if (ProjectManagement.Project is FMTProject fmtProj && fmtProj.Filename.Equals(saveFileDialog.FileName, StringComparison.OrdinalIgnoreCase)) + fmtProj.Update(); + // Different file -- Create new file and Update + else + { + var storedPreviousModSettings = ProjectManagement.Project.ModSettings.CloneJson(); + ProjectManagement.Project = new FMTProject(saveFileDialog.FileName); + ProjectManagement.Project.ModSettings.UpdateFromOtherModSettings(storedPreviousModSettings); + ((FMTProject)ProjectManagement.Project).Update(); } } - loadingDialog.Update("", ""); + // Legacy fbproject file type + else if (saveFileDialog.FileName.EndsWith(".fbproject", StringComparison.OrdinalIgnoreCase)) + { + // Same file -- Simple Update + if (ProjectManagement.Project is FrostbiteProject fbProj) + await fbProj.SaveAsync(saveFileDialog.FileName, true); + // Different file -- Create new file and Update + else + { + ModSettings modSettings = ProjectManagement.Project.ModSettings.CloneJson(); + ProjectManagement.Project = new FrostbiteProject(); + ProjectManagement.Project.ModSettings.Author = modSettings.Author; + ProjectManagement.Project.ModSettings.Description = modSettings.Description; + ProjectManagement.Project.ModSettings.Title = modSettings.Title; + ProjectManagement.Project.ModSettings.Version = modSettings.Version; + await ProjectManagement.Project.SaveAsync(saveFileDialog.FileName, true); + } + } + // Unknown File + else + { + Log("Unknown file type detected. Project failed to save to " + saveFileDialog.FileName); + return false; + } + + Log("Saved project successfully to " + saveFileDialog.FileName); + UpdateWindowTitle(saveFileDialog.FileName); + + await loadingDialog.UpdateAsync("", ""); - //loadingDialog.Close(); - //loadingDialog = null; return true; }