Skip to content

Commit

Permalink
Merge branch 'minify-lua'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rene-Sackers committed May 24, 2019
2 parents aef3e71 + e2d339d commit 1f75260
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/LuaMinifyApi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ http.createServer(function (req, res) {
res.end();
});

}).listen(8080);
}).listen(process.env.PORT);
42 changes: 42 additions & 0 deletions src/LuaMinifyApi/web.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<!-- Do not interfere with requests for node-inspector debugging -->
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^index.js\/debug[\/]?" />
</rule>

<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>

<!-- All other URLs are mapped to the node.js site entry point -->
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="index.js"/>
</rule>
</rules>
</rewrite>

<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>

<httpErrors existingResponse="PassThrough" />

<iisnode watchedFiles="web.config;*.js"/>
</system.webServer>
</configuration>
2 changes: 1 addition & 1 deletion src/StormworksLuaExtract.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2016
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StormworksLuaExtract", "StormworksLuaExtract\StormworksLuaExtract.csproj", "{2804097F-2B7C-4DE6-A506-9532BBE57B7D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StormworksLuaExtract", "StormworksLuaExtract\StormworksLuaExtract.csproj", "{2804097F-2B7C-4DE6-A506-9532BBE57B7D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
16 changes: 16 additions & 0 deletions src/StormworksLuaExtract/Helpers/ConsoleHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace StormworksLuaExtract.Helpers
{
public static class ConsoleHelper
{
public static void WriteWarning(object value)
{
var previousForegroundColor = Console.ForegroundColor;

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(value);
Console.ForegroundColor = previousForegroundColor;
}
}
}
6 changes: 6 additions & 0 deletions src/StormworksLuaExtract/Models/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
public static class Constants
{
public const int ReadWriteTimeoutInMilliseconds = 1000;

public const string MinifiedScriptSuffix = "--min";

public const int LuaMaximumLength = 4096;

public const string MinifyLuaApiEndpoint = "https://minify-lua-api.azurewebsites.net/";
}
}
7 changes: 7 additions & 0 deletions src/StormworksLuaExtract/Models/LuaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class LuaScript
{
public string VehicleXmlPath { get; }

public string VehicleXmlFileName => Path.GetFileName(VehicleXmlPath);

public string VehicleName => Path.GetFileNameWithoutExtension(VehicleXmlPath);

public string MicrocontrollerName { get; }
Expand All @@ -14,6 +16,10 @@ public class LuaScript

public string LuaFileName => Path.GetFileName(LuaFilePath);

public string MinifiedLuaPath { get; }

public string MinifiedLuaFileName => Path.GetFileName(MinifiedLuaPath);

public string ObjectId { get; }

public LuaScript(string microcontrollerName, string vehicleXmlPath, string luaFilePath, string objectId)
Expand All @@ -22,6 +28,7 @@ public LuaScript(string microcontrollerName, string vehicleXmlPath, string luaFi
VehicleXmlPath = vehicleXmlPath;
LuaFilePath = luaFilePath;
ObjectId = objectId;
MinifiedLuaPath = luaFilePath.Replace(".lua", ".min.lua");
}
}
}
1 change: 1 addition & 0 deletions src/StormworksLuaExtract/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void Main()
builder.RegisterType<VehiclesWatchService>().AsSelf();
builder.RegisterType<LocalLuaToXmlWriteService>().AsSelf();
builder.RegisterType<XmlToLocalLuaWriteService>().AsSelf();
builder.RegisterType<MinifyLuaService>().AsSelf();

var container = builder.Build();

Expand Down
7 changes: 5 additions & 2 deletions src/StormworksLuaExtract/Services/ApplicationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ private void VehicleChanged(string xmlfilepath)
{
_luaScripts.Remove(deletedScript);
var deletedScriptContent = FileHelper.NoTouchReadFile(deletedScript.LuaFilePath);
if (BackupFileHelper.BackupFile(deletedScriptContent, deletedScript.LuaFileName))
File.Delete(deletedScript.LuaFilePath);
if (!BackupFileHelper.BackupFile(deletedScriptContent, deletedScript.LuaFileName))
continue;

File.Delete(deletedScript.LuaFilePath);
File.Delete(deletedScript.MinifiedLuaPath);
}

foreach (var existingScript in previouslyExtractedScripts.Except(deletedScripts))
Expand Down
64 changes: 49 additions & 15 deletions src/StormworksLuaExtract/Services/LocalLuaToXmlWriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ namespace StormworksLuaExtract.Services
{
public class LocalLuaToXmlWriteService
{
private const int LuaMaximumLength = 4096;
private readonly MinifyLuaService _minifyLuaService;

public LocalLuaToXmlWriteService(MinifyLuaService minifyLuaService)
{
_minifyLuaService = minifyLuaService;
}

public void WriteScriptToMicrocontroller(LuaScript luaScript)
{
Expand All @@ -21,20 +26,13 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript)
return;
}

var newScript = FileHelper.NoTouchReadFile(luaScript.LuaFilePath);
if (newScript == null)
return;

newScript = WebUtility.HtmlEncode(newScript);
var processedScript = ProcessScript(luaScript);

if (processedScript == null)
return;

if (newScript.Length > LuaMaximumLength)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Warning! Script is too long: {newScript.Length}/{LuaMaximumLength}, not updating vehicle file.");
Console.ForegroundColor = ConsoleColor.Gray;
if (!FileHelper.TryWriteFile(luaScript.MinifiedLuaPath, processedScript))
return;
}

var currentScript = ScriptExtractHelper.GetScriptFromXmlFile(luaScript.VehicleXmlPath, luaScript.ObjectId);
if (currentScript == null)
Expand All @@ -43,7 +41,7 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript)
return;
}

if (currentScript == newScript)
if (currentScript == processedScript)
{
Console.WriteLine($"Script {luaScript.ObjectId} hasn't changed compared to the vehicle XML.");
return;
Expand All @@ -52,17 +50,53 @@ public void WriteScriptToMicrocontroller(LuaScript luaScript)
var currentXml = FileHelper.NoTouchReadFile(luaScript.VehicleXmlPath);

// Backup
if (!BackupFileHelper.BackupFile(currentXml, luaScript.VehicleName))
if (!BackupFileHelper.BackupFile(currentXml, luaScript.VehicleXmlFileName))
return;

processedScript = WebUtility.HtmlEncode(processedScript);

var pattern = Statics.ObjectMatchPattern(luaScript.ObjectId);
var newXml = Regex.Replace(currentXml, pattern, "<object id=\"${id}\" script='" + newScript + "'>");
var newXml = Regex.Replace(currentXml, pattern, "<object id=\"${id}\" script='" + processedScript + "'>");

// Overwrite
if (!FileHelper.TryWriteFile(luaScript.VehicleXmlPath, newXml))
return;

Console.WriteLine($"Updated vehicle {luaScript.VehicleName} XML with new script with ID {luaScript.ObjectId}.");
}

private string ProcessScript(LuaScript luaScript)
{
var newScript = FileHelper.NoTouchReadFile(luaScript.LuaFilePath);
if (newScript == null)
return null;

Console.WriteLine($"Script length: {newScript.Length}/{Constants.LuaMaximumLength}");

if (newScript.Length > Constants.LuaMaximumLength)
newScript = MinifyScript(newScript);

if (newScript == null)
return null;

return newScript;
}

private string MinifyScript(string script)
{
ConsoleHelper.WriteWarning($"Script is too long for game: {script.Length}/{Constants.LuaMaximumLength} characters, minifying script first.");

script = _minifyLuaService.Minify(script) + Constants.MinifiedScriptSuffix;

Console.WriteLine($"Length after minification: {script.Length}");

if (script.Length > Constants.LuaMaximumLength)
{
ConsoleHelper.WriteWarning("Minified script still too large for game! Not updating vehicle xml.");
return null;
}

return script;
}
}
}
29 changes: 29 additions & 0 deletions src/StormworksLuaExtract/Services/MinifyLuaService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Net.Http;
using System.Text;
using StormworksLuaExtract.Helpers;
using StormworksLuaExtract.Models;

namespace StormworksLuaExtract.Services
{
public class MinifyLuaService
{
public string Minify(string script)
{
using (var httpClient = new HttpClient())
{
var httpContent = new StringContent(script, Encoding.UTF8);
using (var response = httpClient.PostAsync(Constants.MinifyLuaApiEndpoint, httpContent).Result)
{
if (!response.IsSuccessStatusCode)
{
ConsoleHelper.WriteWarning("Failed to minify lua script.");
return null;
}

var minifiedScript = response.Content.ReadAsStringAsync().Result;
return minifiedScript;
}
}
}
}
}
12 changes: 10 additions & 2 deletions src/StormworksLuaExtract/Services/XmlToLocalLuaWriteService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,31 @@ public bool WriteVehicleLuaScriptToFile(LuaScript luaScript)
Console.WriteLine($"Extracting Lua scripts from vehicle '{luaScript.VehicleName}'");

var vehicleXmlScript = ScriptExtractHelper.GetScriptFromXmlFile(luaScript.VehicleXmlPath, luaScript.ObjectId);
var isMinifiedScript = vehicleXmlScript.EndsWith(Constants.MinifiedScriptSuffix);

if (File.Exists(luaScript.LuaFilePath))
{
var currentLocalScript = FileHelper.NoTouchReadFile(luaScript.LuaFilePath);
var currentLocalScript = FileHelper.NoTouchReadFile(isMinifiedScript ? luaScript.MinifiedLuaPath : luaScript.LuaFilePath);

if (currentLocalScript == vehicleXmlScript)
{
Console.WriteLine($"Nothing changed for script {luaScript.ObjectId} from vehicle {luaScript.VehicleName}.");
return false;
}

if (isMinifiedScript)
{
ConsoleHelper.WriteWarning($"Script {luaScript.ObjectId} in microcontroller {luaScript.MicrocontrollerName} in vehicle {luaScript.VehicleName} was changed, but it's a minified script. Not updating local file.");
return false;
}

// Backup
if (!BackupFileHelper.BackupFile($"{luaScript.VehicleName}_{luaScript.ObjectId}", luaScript.VehicleName))
if (!BackupFileHelper.BackupFile(currentLocalScript, luaScript.MinifiedLuaFileName))
return false;
}

FileHelper.TryWriteFile(luaScript.LuaFilePath, vehicleXmlScript);
FileHelper.TryWriteFile(luaScript.MinifiedLuaPath, vehicleXmlScript);

Console.WriteLine($"Wrote script {luaScript.ObjectId} from vehicle {luaScript.VehicleName} to {luaScript.LuaFileName}.");

Expand Down

0 comments on commit 1f75260

Please sign in to comment.