Skip to content

Commit

Permalink
WIP - Bi-directional Lua Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene-Sackers committed May 18, 2019
1 parent ce57119 commit e43cf88
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 55 deletions.
29 changes: 20 additions & 9 deletions src/StormworksLuaExtract/Helpers/ScriptExtractHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using StormworksLuaExtract.Models;

Expand All @@ -23,18 +24,28 @@ public static IEnumerable<LuaScript> 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 <object item, and once under a <c33 item
if (element != "object")
continue;

var objectId = match.Groups["id"].Value;
var script = match.Groups["script"].Value;
script = System.Net.WebUtility.HtmlDecode(script);

yield return new LuaScript(microcontrollerXmlFilePath, GetLuaFilePath(microcontrollerXmlFilePath, objectId), objectId, script);
yield return new LuaScript(microcontrollerXmlFilePath, GetLuaFilePath(microcontrollerXmlFilePath, objectId), objectId);
}
}

public static string GetScriptFromXmlFile(string xmlFile, string objectId)
{
string xml;
try
{
xml = FileHelper.NoTouchReadFile(xmlFile);
}
catch
{
return null;
}

var match = Regex.Match(xml, Statics.ObjectMatchPattern(objectId));
var script = match.Groups["script"].Value;

return WebUtility.HtmlDecode(script);
}

private static string GetLuaFilePath(string xmlFilePath, string scriptObjectId)
Expand Down
13 changes: 5 additions & 8 deletions src/StormworksLuaExtract/Models/LuaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ namespace StormworksLuaExtract.Models
{
public class LuaScript
{
public string MicrocontrollerXmlPath { get; }
public string VehicleXmlPath { get; }

public string LuaFilePath { get; }

public string ObjectId { get; }

public string Script { get; }
public string VehicleName { get; }

public string MicrocontrollerName { get; }

public LuaScript(string microcontrollerXmlPath, string luaFilePath, string objectId, string script)
public LuaScript(string vehicleXmlPath, string luaFilePath, string objectId)
{
MicrocontrollerXmlPath = microcontrollerXmlPath;
VehicleXmlPath = vehicleXmlPath;
LuaFilePath = luaFilePath;
ObjectId = objectId;
Script = script;

MicrocontrollerName = MicrocontrollerXmlPath.Split(Path.DirectorySeparatorChar).Last().Replace(".xml", string.Empty);
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 = Path.Join(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"Stormworks\data\vehicles");
public static readonly string MicrocontrollerPath = @"C:\Users\renes\Desktop\Test";//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
2 changes: 1 addition & 1 deletion src/StormworksLuaExtract/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static void Main()
var builder = new ContainerBuilder();

builder.RegisterType<ApplicationService>().AsSelf();
builder.RegisterType<MicrocontrollersWatchService>().AsSelf();
builder.RegisterType<VehiclesWatchService>().AsSelf();
builder.RegisterType<LocalLuaToXmlWriteService>().AsSelf();
builder.RegisterType<XmlToLocalLuaWriteService>().AsSelf();

Expand Down
36 changes: 26 additions & 10 deletions src/StormworksLuaExtract/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LuaScript> _luaScripts = new List<LuaScript>();

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))
{
Expand Down Expand Up @@ -60,19 +63,30 @@ 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)
{
var xmlFileScripts = ScriptExtractHelper.ExtractScriptsFromMicrocontrollerXml(xmlFilePath);
foreach (var script in xmlFileScripts)
{
_luaScripts.Add(script);
_xmlToLocalLuaWriteService.WriteMicrocontrollerLuaScriptsToFiles(script);
_xmlToLocalLuaWriteService.WriteVehicleLuaScriptToFile(script);
}
}

Expand All @@ -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);
}
}
Expand Down
23 changes: 10 additions & 13 deletions src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using StormworksLuaExtract.Helpers;
Expand All @@ -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;
}

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

Expand All @@ -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}.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,43 @@

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;
}

private async void MicrocontrollerXmlFileAdded(object sender, FileSystemEventArgs e)
{
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);
}
Expand Down
22 changes: 12 additions & 10 deletions src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}.");
}
}
}

0 comments on commit e43cf88

Please sign in to comment.