140 lines
4.9 KiB
C#
140 lines
4.9 KiB
C#
using LANCommander.SDK.Enums;
|
|
using LANCommander.SDK.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LANCommander.SDK.Helpers
|
|
{
|
|
public static class ScriptHelper
|
|
{
|
|
public static readonly ILogger Logger;
|
|
|
|
public static async Task<string> SaveTempScriptAsync(Script script)
|
|
{
|
|
var tempPath = await SaveTempScriptAsync(script.Contents);
|
|
|
|
Logger?.LogTrace("Wrote script {Script} to {Destination}", script.Name, tempPath);
|
|
|
|
return tempPath;
|
|
}
|
|
|
|
public static async Task<string> SaveTempScriptAsync(string contents)
|
|
{
|
|
var tempPath = Path.GetTempFileName();
|
|
|
|
// PowerShell will only run scripts with the .ps1 file extension
|
|
File.Move(tempPath, tempPath + ".ps1");
|
|
|
|
tempPath = tempPath + ".ps1";
|
|
|
|
await File.WriteAllTextAsync(tempPath, contents);
|
|
|
|
return tempPath;
|
|
}
|
|
|
|
public static async Task SaveScriptAsync(Game game, Script script, string installDirectory)
|
|
{
|
|
var scriptContents = GetScriptContents(script);
|
|
|
|
if (!String.IsNullOrWhiteSpace(scriptContents))
|
|
{
|
|
var filename = GetScriptFilePath(installDirectory, game.Id, script.Type);
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(filename)))
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filename));
|
|
|
|
if (File.Exists(filename))
|
|
File.Delete(filename);
|
|
|
|
Logger?.LogTrace("Writing {ScriptType} script to {Destination}", script.Type, filename);
|
|
|
|
await File.WriteAllTextAsync(filename, scriptContents);
|
|
}
|
|
}
|
|
|
|
public static async Task SaveScriptAsync(Game game, Redistributable redistributable, Script script)
|
|
{
|
|
var scriptContents = GetScriptContents(script);
|
|
|
|
if (!String.IsNullOrWhiteSpace(scriptContents))
|
|
{
|
|
var fileName = GetScriptFilePath(game.InstallDirectory, redistributable.Id, script.Type);
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(fileName)))
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
|
|
|
|
if (File.Exists(fileName))
|
|
File.Delete(fileName);
|
|
|
|
Logger?.LogTrace("Writing {ScriptType} script to {Destination}", script.Type, fileName);
|
|
|
|
await File.WriteAllTextAsync(fileName, scriptContents);
|
|
}
|
|
}
|
|
|
|
public static async Task SaveScriptAsync(Tool tool, Script script, string installDirectory)
|
|
{
|
|
var scriptContents = GetScriptContents(script);
|
|
|
|
if (!String.IsNullOrWhiteSpace(scriptContents))
|
|
{
|
|
var filename = GetScriptFilePath(installDirectory, tool.Id, script.Type);
|
|
|
|
if (!Directory.Exists(Path.GetDirectoryName(filename)))
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filename));
|
|
|
|
if (File.Exists(filename))
|
|
File.Delete(filename);
|
|
|
|
Logger?.LogTrace("Writing {ScriptType} script to {Destination}", script.Type, filename);
|
|
|
|
await File.WriteAllTextAsync(filename, scriptContents);
|
|
}
|
|
}
|
|
|
|
public static string GetScriptContents(Script script)
|
|
{
|
|
if (script == null)
|
|
return String.Empty;
|
|
|
|
if (script.RequiresAdmin)
|
|
script.Contents = "#Requires -RunAsAdministrator" + "\r\n\r\n" + script.Contents;
|
|
|
|
return script.Contents;
|
|
}
|
|
|
|
public static string GetScriptFilePath(string installDirectory, Guid id, ScriptType type)
|
|
{
|
|
return GetScriptFilePath(installDirectory, id.ToString(), type);
|
|
}
|
|
|
|
public static string GetScriptFilePath(string installDirectory, string id, ScriptType type)
|
|
{
|
|
var filename = GetScriptFileName(type);
|
|
|
|
return Path.Combine(installDirectory, ".lancommander", id, filename);
|
|
}
|
|
|
|
public static string GetScriptFileName(ScriptType type)
|
|
{
|
|
Dictionary<ScriptType, string> filenames = new Dictionary<ScriptType, string>() {
|
|
{ ScriptType.Install, "Install.ps1" },
|
|
{ ScriptType.Uninstall, "Uninstall.ps1" },
|
|
{ ScriptType.NameChange, "ChangeName.ps1" },
|
|
{ ScriptType.KeyChange, "ChangeKey.ps1" },
|
|
{ ScriptType.DetectInstall, "DetectInstall.ps1" },
|
|
{ ScriptType.BeforeStart, "BeforeStart.ps1" },
|
|
{ ScriptType.AfterStop, "AfterStop.ps1" },
|
|
{ ScriptType.Package, "Package.ps1" },
|
|
{ ScriptType.RunWrapper, "RunWrapper.ps1" }
|
|
};
|
|
|
|
return filenames[type];
|
|
}
|
|
}
|
|
}
|