Skip to content

Commit

Permalink
Added bi-directional Lua updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene-Sackers committed May 19, 2019
1 parent e43cf88 commit f13c90e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 29 deletions.
21 changes: 21 additions & 0 deletions src/StormworksLuaExtract/Helpers/BackupFileHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
2 changes: 1 addition & 1 deletion src/StormworksLuaExtract/Models/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class Constants
{
public const int ReadWriteTimeoutInMilliseconds = 2000;
public const int ReadWriteTimeoutInMilliseconds = 1000;
}
}
8 changes: 4 additions & 4 deletions src/StormworksLuaExtract/Models/LuaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/StormworksLuaExtract/Models/Statics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
51 changes: 42 additions & 9 deletions src/StormworksLuaExtract/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class ApplicationService
private readonly LocalLuaToXmlWriteService _localLuaToXmlWriteService;
private readonly XmlToLocalLuaWriteService _xmlToLocalLuaWriteService;
private readonly List<LuaScript> _luaScripts = new List<LuaScript>();
private readonly List<string> _ignoreNextLuaUpdatePaths = new List<string>();

private string _ignoreNextVehicleUpdatePath;

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<object id=\"${id}\" script='" + newScript + "'>");

// Overwrite
if (!FileHelper.TryWriteFile(luaScript.VehicleXmlPath, newXml))
Expand Down
16 changes: 7 additions & 9 deletions src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}'");

Expand All @@ -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;
}
}
}

0 comments on commit f13c90e

Please sign in to comment.