From e43cf8877f8f0a478884e1ffa1ea702b59e861d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sackers?= Date: Sat, 18 May 2019 14:02:53 +0200 Subject: [PATCH 1/2] WIP - Bi-directional Lua Update --- .../Helpers/ScriptExtractHelper.cs | 29 ++++++++++----- src/StormworksLuaExtract/Models/LuaScript.cs | 13 +++---- src/StormworksLuaExtract/Models/Statics.cs | 2 +- src/StormworksLuaExtract/Program.cs | 2 +- .../Services/ApplicationService.cs | 36 +++++++++++++------ .../Services/LocalLuaToXmlWriteService.cs | 23 ++++++------ ...atchService.cs => VehiclesWatchService.cs} | 17 +++++++-- .../Services/XmlToLocalLuaWriteService.cs | 22 ++++++------ 8 files changed, 89 insertions(+), 55 deletions(-) rename src/StormworksLuaExtract/Services/{MicrocontrollersWatchService.cs => VehiclesWatchService.cs} (64%) diff --git a/src/StormworksLuaExtract/Helpers/ScriptExtractHelper.cs b/src/StormworksLuaExtract/Helpers/ScriptExtractHelper.cs index 2d874cd..9cb6f43 100644 --- a/src/StormworksLuaExtract/Helpers/ScriptExtractHelper.cs +++ b/src/StormworksLuaExtract/Helpers/ScriptExtractHelper.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Net; using System.Text.RegularExpressions; using StormworksLuaExtract.Models; @@ -23,18 +24,28 @@ public static IEnumerable ExtractScriptsFromMicrocontrollerXml(string var matches = Regex.Matches(xml, Statics.ObjectMatchPattern()); foreach (Match match in matches) { - var element = match.Groups["element"].Value; - - // The script is printed twice in the XML, once under a ().AsSelf(); - builder.RegisterType().AsSelf(); + builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); builder.RegisterType().AsSelf(); diff --git a/src/StormworksLuaExtract/Services/ApplicationService.cs b/src/StormworksLuaExtract/Services/ApplicationService.cs index 42830c1..22b7fac 100644 --- a/src/StormworksLuaExtract/Services/ApplicationService.cs +++ b/src/StormworksLuaExtract/Services/ApplicationService.cs @@ -10,26 +10,29 @@ namespace StormworksLuaExtract.Services { public class ApplicationService { - private readonly MicrocontrollersWatchService _microcontrollersWatchService; + private readonly VehiclesWatchService _vehiclesWatchService; private readonly LocalLuaToXmlWriteService _localLuaToXmlWriteService; private readonly XmlToLocalLuaWriteService _xmlToLocalLuaWriteService; private readonly List _luaScripts = new List(); + private string _ignoreNextVehicleUpdatePath; + public ApplicationService( - MicrocontrollersWatchService microcontrollersWatchService, + VehiclesWatchService vehiclesWatchService, LocalLuaToXmlWriteService localLuaToXmlWriteService, XmlToLocalLuaWriteService xmlToLocalLuaWriteService) { - _microcontrollersWatchService = microcontrollersWatchService; + _vehiclesWatchService = vehiclesWatchService; _localLuaToXmlWriteService = localLuaToXmlWriteService; _xmlToLocalLuaWriteService = xmlToLocalLuaWriteService; - _microcontrollersWatchService.MicrocontrollerAdded += MicrocontrollerAdded; - _microcontrollersWatchService.MicrocontrollerDeleted += MicrocontrollerDeleted; + _vehiclesWatchService.MicrocontrollerAdded += VehicleAdded; + _vehiclesWatchService.MicrocontrollerDeleted += VehicleDeleted; + _vehiclesWatchService.MicrocontrollerChanged += VehicleChanged; } public void Run() { - _microcontrollersWatchService.StartWatching(); + _vehiclesWatchService.StartWatching(); if (!Directory.Exists(Statics.MicrocontrollerPath)) { @@ -60,11 +63,22 @@ private void WriteExistingMicrocontrollerScripts() AddMicrocontrollerXmlFile(xmlFilePath); } - private void MicrocontrollerAdded(string xmlfilepath) => + private void VehicleAdded(string xmlfilepath) => AddMicrocontrollerXmlFile(xmlfilepath); - private void MicrocontrollerDeleted(string xmlfilepath) => - _luaScripts.Where(s => s.MicrocontrollerXmlPath == xmlfilepath).ToList().ForEach(s => _luaScripts.Remove(s)); + private void VehicleChanged(string xmlfilepath) + { + if (xmlfilepath == _ignoreNextVehicleUpdatePath) + { + _ignoreNextVehicleUpdatePath = null; + return; + } + + _luaScripts.Where(s => s.VehicleXmlPath == xmlfilepath).ToList().ForEach(s => _xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(s)); + } + + private void VehicleDeleted(string xmlfilepath) => + _luaScripts.Where(s => s.VehicleXmlPath == xmlfilepath).ToList().ForEach(s => _luaScripts.Remove(s)); private void AddMicrocontrollerXmlFile(string xmlFilePath) { @@ -72,7 +86,7 @@ private void AddMicrocontrollerXmlFile(string xmlFilePath) foreach (var script in xmlFileScripts) { _luaScripts.Add(script); - _xmlToLocalLuaWriteService.WriteMicrocontrollerLuaScriptsToFiles(script); + _xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(script); } } @@ -86,6 +100,8 @@ private async void LocalLuaFileChanged(object sender, FileSystemEventArgs e) Console.WriteLine($"Lua file '{e.Name}' changed."); + _ignoreNextVehicleUpdatePath = luaScript.VehicleXmlPath; + _localLuaToXmlWriteService.WriteScriptToMicrocontroller(luaScript); } } diff --git a/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs b/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs index 29d9926..42603e3 100644 --- a/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs +++ b/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using System.Net; using System.Text.RegularExpressions; using StormworksLuaExtract.Helpers; @@ -14,9 +13,9 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript) { Console.WriteLine($"Local lua file '{luaScript.LuaFilePath}' object ID {luaScript.ObjectId} script changed."); - if (!File.Exists(luaScript.MicrocontrollerXmlPath)) + if (!File.Exists(luaScript.VehicleXmlPath)) { - Console.WriteLine($"Target microprocessor file '{luaScript.MicrocontrollerXmlPath}' not found."); + Console.WriteLine($"Target vehicle file '{luaScript.VehicleXmlPath}' not found."); return; } @@ -26,25 +25,23 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript) newScript = WebUtility.HtmlEncode(newScript); - var currentScripts = ScriptExtractHelper.ExtractScriptsFromMicrocontrollerXml(luaScript.MicrocontrollerXmlPath).ToList(); - var currentScript = currentScripts.FirstOrDefault(s => s.ObjectId == luaScript.ObjectId); - + var currentScript = ScriptExtractHelper.GetScriptFromXmlFile(luaScript.VehicleXmlPath, luaScript.ObjectId); if (currentScript == null) { - Console.WriteLine($"Failed to find the Lua object with ID {luaScript.ObjectId} in microprocessor file '{luaScript.MicrocontrollerXmlPath}'."); + Console.WriteLine($"Failed to find the Lua object with ID {luaScript.ObjectId} in vehicle file '{luaScript.VehicleXmlPath}'."); return; } - if (currentScript.Script == newScript) + if (currentScript == newScript) { - Console.WriteLine($"Script {luaScript.ObjectId} hasn't changed compared to the microcontroller XML."); + Console.WriteLine($"Script {luaScript.ObjectId} hasn't changed compared to the vehicle XML."); return; } - var currentXml = FileHelper.NoTouchReadFile(luaScript.MicrocontrollerXmlPath); + var currentXml = FileHelper.NoTouchReadFile(luaScript.VehicleXmlPath); // Backup - var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.MicrocontrollerName} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.xml"); + var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.VehicleName} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.xml"); if (!FileHelper.TryWriteFile(backupFilePath, currentXml)) return; @@ -54,10 +51,10 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript) var newXml = Regex.Replace(currentXml, pattern, "<${element} id=\"${id}\" script='" + newScript + "'>"); // Overwrite - if (!FileHelper.TryWriteFile(luaScript.MicrocontrollerXmlPath, newXml)) + if (!FileHelper.TryWriteFile(luaScript.VehicleXmlPath, newXml)) return; - Console.WriteLine($"Updated microcontroller {luaScript.MicrocontrollerName} XML with new script with ID {luaScript.ObjectId}."); + Console.WriteLine($"Updated vehicle {luaScript.VehicleName} XML with new script with ID {luaScript.ObjectId}."); } } } diff --git a/src/StormworksLuaExtract/Services/MicrocontrollersWatchService.cs b/src/StormworksLuaExtract/Services/VehiclesWatchService.cs similarity index 64% rename from src/StormworksLuaExtract/Services/MicrocontrollersWatchService.cs rename to src/StormworksLuaExtract/Services/VehiclesWatchService.cs index 3c4c849..0dd0925 100644 --- a/src/StormworksLuaExtract/Services/MicrocontrollersWatchService.cs +++ b/src/StormworksLuaExtract/Services/VehiclesWatchService.cs @@ -6,17 +6,19 @@ namespace StormworksLuaExtract.Services { - public class MicrocontrollersWatchService + public class VehiclesWatchService { public delegate void MicrocontrollerChangedHandler(string xmlFilePath); public event MicrocontrollerChangedHandler MicrocontrollerAdded; public event MicrocontrollerChangedHandler MicrocontrollerDeleted; + public event MicrocontrollerChangedHandler MicrocontrollerChanged; public void StartWatching() { var processorsXmlWatcher = new BufferedFileSystemWatcher(Statics.MicrocontrollerPath, "*.xml"); processorsXmlWatcher.Created += MicrocontrollerXmlFileAdded; + processorsXmlWatcher.Changed += MicrocontrollerXmlFileChanged; processorsXmlWatcher.Deleted += MicrocontrollerXmlFileDeleted; } @@ -24,14 +26,23 @@ private async void MicrocontrollerXmlFileAdded(object sender, FileSystemEventArg { await Task.Delay(Constants.ReadWriteTimeoutInMilliseconds); - Console.WriteLine($"Microcontroller XML file created: {e.Name}"); + Console.WriteLine($"Vehicle XML file created: {e.Name}"); MicrocontrollerAdded?.Invoke(e.FullPath); } + private async void MicrocontrollerXmlFileChanged(object sender, FileSystemEventArgs e) + { + await Task.Delay(Constants.ReadWriteTimeoutInMilliseconds); + + Console.WriteLine($"Microcontroller XML file changed: {e.Name}"); + + MicrocontrollerChanged?.Invoke(e.FullPath); + } + private void MicrocontrollerXmlFileDeleted(object sender, FileSystemEventArgs e) { - Console.WriteLine($"Microcontroller XML file deleted: {e.Name}"); + Console.WriteLine($"Vehicle XML file deleted: {e.Name}"); MicrocontrollerDeleted?.Invoke(e.FullPath); } diff --git a/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs b/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs index e305b73..0256314 100644 --- a/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs +++ b/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs @@ -7,32 +7,34 @@ namespace StormworksLuaExtract.Services { public class XmlToLocalLuaWriteService { - public void WriteMicrocontrollerLuaScriptsToFiles(LuaScript luaScript) + public void WriteVehicleLuaScriptToFile(LuaScript luaScript) { - Console.WriteLine($"Extracting Lua scripts from microcontroller '{luaScript.MicrocontrollerXmlPath}'"); - + Console.WriteLine($"Extracting Lua scripts from vehicle '{luaScript.VehicleXmlPath}'"); + + var vehicleXmlScript = ScriptExtractHelper.GetScriptFromXmlFile(luaScript.VehicleXmlPath, luaScript.ObjectId); + if (File.Exists(luaScript.LuaFilePath)) { - var currentScript = FileHelper.NoTouchReadFile(luaScript.LuaFilePath); + var currentLocalScript = FileHelper.NoTouchReadFile(luaScript.LuaFilePath); - if (currentScript == luaScript.Script) + if (currentLocalScript == vehicleXmlScript) { - Console.WriteLine($"Nothing changed for script {luaScript.ObjectId} from microcontroller {luaScript.MicrocontrollerName}."); + Console.WriteLine($"Nothing changed for script {luaScript.ObjectId} from vehicle {luaScript.VehicleName}."); return; } // Backup - var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.MicrocontrollerName}_{luaScript.ObjectId} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.lua"); - if (!FileHelper.TryWriteFile(backupFilePath, currentScript)) + var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.VehicleName}_{luaScript.ObjectId} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.lua"); + if (!FileHelper.TryWriteFile(backupFilePath, currentLocalScript)) return; Console.WriteLine($"Wrote backup to {backupFilePath}"); } - FileHelper.TryWriteFile(luaScript.LuaFilePath, luaScript.Script); + FileHelper.TryWriteFile(luaScript.LuaFilePath, vehicleXmlScript); - Console.WriteLine($"Wrote script {luaScript.ObjectId} from microcontroller {luaScript.MicrocontrollerName} to {luaScript.LuaFilePath}."); + Console.WriteLine($"Wrote script {luaScript.ObjectId} from vehicle {luaScript.VehicleName} to {luaScript.LuaFilePath}."); } } } From f13c90e308ab8adb3c185af2c68ca8fd76ef40e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Sackers?= Date: Sun, 19 May 2019 18:11:20 +0200 Subject: [PATCH 2/2] Added bi-directional Lua updates --- .../Helpers/BackupFileHelper.cs | 21 ++++++++ src/StormworksLuaExtract/Models/Constants.cs | 2 +- src/StormworksLuaExtract/Models/LuaScript.cs | 8 +-- src/StormworksLuaExtract/Models/Statics.cs | 2 +- .../Services/ApplicationService.cs | 51 +++++++++++++++---- .../Services/LocalLuaToXmlWriteService.cs | 7 +-- .../Services/XmlToLocalLuaWriteService.cs | 16 +++--- 7 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 src/StormworksLuaExtract/Helpers/BackupFileHelper.cs diff --git a/src/StormworksLuaExtract/Helpers/BackupFileHelper.cs b/src/StormworksLuaExtract/Helpers/BackupFileHelper.cs new file mode 100644 index 0000000..6186e49 --- /dev/null +++ b/src/StormworksLuaExtract/Helpers/BackupFileHelper.cs @@ -0,0 +1,21 @@ +using System; +using System.IO; +using StormworksLuaExtract.Models; + +namespace StormworksLuaExtract.Helpers +{ + public static class BackupFileHelper + { + public static bool BackupFile(string originalContent, string originalFileName) + { + var backupFileName = $"{Path.GetFileNameWithoutExtension(originalFileName)} {DateTime.Now:yyyy-MM-dd HH-mm-ss}{Path.GetExtension(originalFileName)}"; + var backupFilePath = Path.Join(Statics.LocalBackupDirectory, backupFileName); + var success = FileHelper.TryWriteFile(backupFilePath, originalContent); + + if (success) + Console.WriteLine($"Wrote backup to {backupFilePath}"); + + return success; + } + } +} diff --git a/src/StormworksLuaExtract/Models/Constants.cs b/src/StormworksLuaExtract/Models/Constants.cs index e85a229..37271ec 100644 --- a/src/StormworksLuaExtract/Models/Constants.cs +++ b/src/StormworksLuaExtract/Models/Constants.cs @@ -2,6 +2,6 @@ { public static class Constants { - public const int ReadWriteTimeoutInMilliseconds = 2000; + public const int ReadWriteTimeoutInMilliseconds = 1000; } } diff --git a/src/StormworksLuaExtract/Models/LuaScript.cs b/src/StormworksLuaExtract/Models/LuaScript.cs index 27f626c..538c888 100644 --- a/src/StormworksLuaExtract/Models/LuaScript.cs +++ b/src/StormworksLuaExtract/Models/LuaScript.cs @@ -7,19 +7,19 @@ public class LuaScript { public string VehicleXmlPath { get; } + public string VehicleName => Path.GetFileNameWithoutExtension(VehicleXmlPath); + public string LuaFilePath { get; } - public string ObjectId { get; } + public string LuaFileName => Path.GetFileName(LuaFilePath); - public string VehicleName { get; } + public string ObjectId { get; } public LuaScript(string vehicleXmlPath, string luaFilePath, string objectId) { VehicleXmlPath = vehicleXmlPath; LuaFilePath = luaFilePath; ObjectId = objectId; - - VehicleName = VehicleXmlPath.Split(Path.DirectorySeparatorChar).Last().Replace(".xml", string.Empty); } } } \ No newline at end of file diff --git a/src/StormworksLuaExtract/Models/Statics.cs b/src/StormworksLuaExtract/Models/Statics.cs index 38a8df0..c20514e 100644 --- a/src/StormworksLuaExtract/Models/Statics.cs +++ b/src/StormworksLuaExtract/Models/Statics.cs @@ -5,7 +5,7 @@ namespace StormworksLuaExtract.Models { public static class Statics { - public static readonly string MicrocontrollerPath = @"C:\Users\renes\Desktop\Test";//Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Stormworks\data\vehicles"); + public static readonly string MicrocontrollerPath = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Stormworks\data\vehicles"); public static readonly string LocalEditDirectory = Path.GetFullPath(@".\Workspace"); public static readonly string LocalBackupDirectory = Path.GetFullPath(@".\Backup"); diff --git a/src/StormworksLuaExtract/Services/ApplicationService.cs b/src/StormworksLuaExtract/Services/ApplicationService.cs index 22b7fac..bb65764 100644 --- a/src/StormworksLuaExtract/Services/ApplicationService.cs +++ b/src/StormworksLuaExtract/Services/ApplicationService.cs @@ -14,6 +14,7 @@ public class ApplicationService private readonly LocalLuaToXmlWriteService _localLuaToXmlWriteService; private readonly XmlToLocalLuaWriteService _xmlToLocalLuaWriteService; private readonly List _luaScripts = new List(); + private readonly List _ignoreNextLuaUpdatePaths = new List(); private string _ignoreNextVehicleUpdatePath; @@ -60,11 +61,14 @@ public void Run() private void WriteExistingMicrocontrollerScripts() { foreach (var xmlFilePath in Directory.GetFiles(Statics.MicrocontrollerPath, "*.xml")) - AddMicrocontrollerXmlFile(xmlFilePath); + AddVehicleXmlFile(xmlFilePath); } private void VehicleAdded(string xmlfilepath) => - AddMicrocontrollerXmlFile(xmlfilepath); + AddVehicleXmlFile(xmlfilepath); + + private void VehicleDeleted(string xmlfilepath) => + _luaScripts.Where(s => s.VehicleXmlPath == xmlfilepath).ToList().ForEach(s => _luaScripts.Remove(s)); private void VehicleChanged(string xmlfilepath) { @@ -74,16 +78,39 @@ private void VehicleChanged(string xmlfilepath) return; } - _luaScripts.Where(s => s.VehicleXmlPath == xmlfilepath).ToList().ForEach(s => _xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(s)); - } + var savedVehicleScripts = ScriptExtractHelper.ExtractScriptsFromMicrocontrollerXml(xmlfilepath).ToList(); - private void VehicleDeleted(string xmlfilepath) => - _luaScripts.Where(s => s.VehicleXmlPath == xmlfilepath).ToList().ForEach(s => _luaScripts.Remove(s)); + var previouslyExtractedScripts = _luaScripts.Where(ls => ls.VehicleXmlPath == xmlfilepath).ToList(); + var newScripts = savedVehicleScripts.Where(vs => previouslyExtractedScripts.All(nvs => nvs.ObjectId != vs.ObjectId)); + var deletedScripts = previouslyExtractedScripts.Where(vs => savedVehicleScripts.All(nvs => nvs.ObjectId != vs.ObjectId)).ToList(); + + foreach (var newScript in newScripts) + { + _luaScripts.Add(newScript); + _ignoreNextLuaUpdatePaths.Add(newScript.LuaFilePath); + _xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(newScript); + } + + foreach (var deletedScript in deletedScripts) + { + _luaScripts.Remove(deletedScript); + var deletedScriptContent = FileHelper.NoTouchReadFile(deletedScript.LuaFilePath); + if (BackupFileHelper.BackupFile(deletedScriptContent, deletedScript.LuaFileName)) + File.Delete(deletedScript.LuaFilePath); + } + + foreach (var existingScript in previouslyExtractedScripts.Except(deletedScripts)) + { + _ignoreNextLuaUpdatePaths.Add(existingScript.LuaFilePath); + if (!_xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(existingScript)) + _ignoreNextLuaUpdatePaths.Remove(existingScript.LuaFilePath); + } + } - private void AddMicrocontrollerXmlFile(string xmlFilePath) + private void AddVehicleXmlFile(string xmlFilePath) { - var xmlFileScripts = ScriptExtractHelper.ExtractScriptsFromMicrocontrollerXml(xmlFilePath); - foreach (var script in xmlFileScripts) + var luaScripts = ScriptExtractHelper.ExtractScriptsFromMicrocontrollerXml(xmlFilePath); + foreach (var script in luaScripts) { _luaScripts.Add(script); _xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(script); @@ -92,6 +119,12 @@ private void AddMicrocontrollerXmlFile(string xmlFilePath) private async void LocalLuaFileChanged(object sender, FileSystemEventArgs e) { + if (_ignoreNextLuaUpdatePaths.Contains(e.FullPath)) + { + _ignoreNextLuaUpdatePaths.Remove(e.FullPath); + return; + } + var luaScript = _luaScripts.FirstOrDefault(ls => ls.LuaFilePath == e.FullPath); if (luaScript == null) return; diff --git a/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs b/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs index 42603e3..91dbc44 100644 --- a/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs +++ b/src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs @@ -41,14 +41,11 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript) var currentXml = FileHelper.NoTouchReadFile(luaScript.VehicleXmlPath); // Backup - var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.VehicleName} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.xml"); - if (!FileHelper.TryWriteFile(backupFilePath, currentXml)) + if (!BackupFileHelper.BackupFile(currentXml, luaScript.VehicleName)) return; - Console.WriteLine($"Wrote backup to {backupFilePath}"); - var pattern = Statics.ObjectMatchPattern(luaScript.ObjectId); - var newXml = Regex.Replace(currentXml, pattern, "<${element} id=\"${id}\" script='" + newScript + "'>"); + var newXml = Regex.Replace(currentXml, pattern, ""); // Overwrite if (!FileHelper.TryWriteFile(luaScript.VehicleXmlPath, newXml)) diff --git a/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs b/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs index 0256314..f8bac9b 100644 --- a/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs +++ b/src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs @@ -7,7 +7,7 @@ namespace StormworksLuaExtract.Services { public class XmlToLocalLuaWriteService { - public void WriteVehicleLuaScriptToFile(LuaScript luaScript) + public bool WriteVehicleLuaScriptToFile(LuaScript luaScript) { Console.WriteLine($"Extracting Lua scripts from vehicle '{luaScript.VehicleXmlPath}'"); @@ -20,21 +20,19 @@ public void WriteVehicleLuaScriptToFile(LuaScript luaScript) if (currentLocalScript == vehicleXmlScript) { Console.WriteLine($"Nothing changed for script {luaScript.ObjectId} from vehicle {luaScript.VehicleName}."); - return; + return false; } // Backup - var backupFilePath = Path.Join(Statics.LocalBackupDirectory, $"{luaScript.VehicleName}_{luaScript.ObjectId} {DateTime.Now:yyyy-MM-dd HH-mm-ss}.lua"); - if (!FileHelper.TryWriteFile(backupFilePath, currentLocalScript)) - return; - - Console.WriteLine($"Wrote backup to {backupFilePath}"); + if (!BackupFileHelper.BackupFile($"{luaScript.VehicleName}_{luaScript.ObjectId}", luaScript.VehicleName)) + return false; } - - + FileHelper.TryWriteFile(luaScript.LuaFilePath, vehicleXmlScript); Console.WriteLine($"Wrote script {luaScript.ObjectId} from vehicle {luaScript.VehicleName} to {luaScript.LuaFilePath}."); + + return true; } } }