Merge pull request #33 from LANCommander/powershell-enhancements

PowerShell Enhancements
net8.0
Pat Hartl 2023-11-17 01:18:41 -06:00 committed by GitHub
commit aff2e991ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
67 changed files with 2992 additions and 315 deletions

View File

@ -1,11 +1,13 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.Models;
using LANCommander.SDK.PowerShell;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace LANCommander.PlaynitePlugin
@ -116,6 +118,10 @@ namespace LANCommander.PlaynitePlugin
InstallDirectory = installDirectory,
};
RunInstallScript(installDirectory);
RunNameChangeScript(installDirectory);
RunKeyChangeScript(installDirectory);
InvokeOnInstalled(new GameInstalledEventArgs(installInfo));
}
else if (result.Canceled)
@ -130,5 +136,76 @@ namespace LANCommander.PlaynitePlugin
else if (result.Error != null)
throw result.Error;
}
private int RunInstallScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install));
return script.Execute();
}
return 0;
}
private int RunNameChangeScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.AddVariable("OldPlayerAlias", "");
script.AddVariable("NewPlayerAlias", Plugin.Settings.PlayerName);
script.UseFile(path);
return script.Execute();
}
return 0;
}
private int RunKeyChangeScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
var key = Plugin.LANCommanderClient.GetAllocatedKey(manifest.Id);
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.AddVariable("AllocatedKey", key);
script.UseFile(path);
return script.Execute();
}
return 0;
}
}
}

View File

@ -12,6 +12,7 @@
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -140,6 +141,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LANCommander.PowerShell\LANCommander.PowerShell.csproj">
<Project>{807943bf-0c7d-4ed3-8393-cfee64e3138c}</Project>
<Name>LANCommander.PowerShell</Name>
</ProjectReference>
<ProjectReference Include="..\LANCommander.SDK\LANCommander.SDK.csproj">
<Project>{4c2a71fd-a30b-4d62-888a-4ef843d8e506}</Project>
<Name>LANCommander.SDK</Name>

View File

@ -1,4 +1,6 @@
using LANCommander.PlaynitePlugin.Extensions;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell;
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Models;
@ -220,9 +222,9 @@ namespace LANCommander.PlaynitePlugin
if (args.Games.Count == 1 && args.Games.First().IsInstalled && !String.IsNullOrWhiteSpace(args.Games.First().InstallDirectory))
{
var nameChangeScriptPath = LANCommander.SDK.PowerShellRuntime.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.NameChange);
var keyChangeScriptPath = LANCommander.SDK.PowerShellRuntime.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.KeyChange);
var installScriptPath = LANCommander.SDK.PowerShellRuntime.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.Install);
var nameChangeScriptPath = ScriptHelper.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.NameChange);
var keyChangeScriptPath = ScriptHelper.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.KeyChange);
var installScriptPath = ScriptHelper.GetScriptFilePath(args.Games.First().InstallDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(nameChangeScriptPath))
{
@ -241,7 +243,8 @@ namespace LANCommander.PlaynitePlugin
{
var game = nameChangeArgs.Games.First();
LANCommander.SDK.PowerShellRuntime.RunScript(game.InstallDirectory, SDK.Enums.ScriptType.NameChange, $@"""{result.SelectedString}"" ""{oldName}""");
RunNameChangeScript(game.InstallDirectory, oldName, result.SelectedString);
LANCommanderClient.ChangeAlias(result.SelectedString);
}
}
@ -267,7 +270,7 @@ namespace LANCommander.PlaynitePlugin
if (String.IsNullOrEmpty(newKey))
PlayniteApi.Dialogs.ShowErrorMessage("There are no more keys available on the server.", "No Keys Available");
else
LANCommander.SDK.PowerShellRuntime.RunScript(keyChangeArgs.Games.First().InstallDirectory, SDK.Enums.ScriptType.KeyChange, $@"""{newKey}""");
RunKeyChangeScript(keyChangeArgs.Games.First().InstallDirectory, newKey);
}
else
{
@ -289,14 +292,10 @@ namespace LANCommander.PlaynitePlugin
Guid gameId;
if (Guid.TryParse(installArgs.Games.First().GameId, out gameId))
{
LANCommander.SDK.PowerShellRuntime.RunScript(installArgs.Games.First().InstallDirectory, SDK.Enums.ScriptType.Install);
}
RunInstallScript(installArgs.Games.First().InstallDirectory);
else
{
PlayniteApi.Dialogs.ShowErrorMessage("This game could not be found on the server. Your game may be corrupted.");
}
}
};
}
}
@ -391,6 +390,8 @@ namespace LANCommander.PlaynitePlugin
}
else
{
var oldName = Settings.PlayerName;
Settings.PlayerName = result.SelectedString;
Logger.Trace($"New player name of \"{Settings.PlayerName}\" has been set!");
@ -404,7 +405,17 @@ namespace LANCommander.PlaynitePlugin
Logger.Trace($"Running name change scripts across {games.Count} installed game(s)");
LANCommander.SDK.PowerShellRuntime.RunScripts(games.Select(g => g.InstallDirectory), SDK.Enums.ScriptType.NameChange, Settings.PlayerName);
foreach (var game in games)
{
var script = new PowerShellScript();
script.AddVariable("OldName", oldName);
script.AddVariable("NewName", Settings.PlayerName);
script.UseFile(ScriptHelper.GetScriptFilePath(game.InstallDirectory, SDK.Enums.ScriptType.NameChange));
script.Execute();
}
}
}
else
@ -523,5 +534,77 @@ namespace LANCommander.PlaynitePlugin
PlayniteApi.Database.Games.Update(game);
}
private int RunInstallScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Settings.InstallDirectory);
script.AddVariable("ServerAddress", Settings.ServerAddress);
script.UseFile(path);
return script.Execute();
}
return 0;
}
private int RunNameChangeScript(string installDirectory, string oldPlayerAlias, string newPlayerAlias)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Settings.InstallDirectory);
script.AddVariable("ServerAddress", Settings.ServerAddress);
script.AddVariable("OldPlayerAlias", oldPlayerAlias);
script.AddVariable("NewPlayerAlias", newPlayerAlias);
script.UseFile(path);
return script.Execute();
}
return 0;
}
private int RunKeyChangeScript(string installDirectory, string key = "")
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
if (String.IsNullOrEmpty(key))
key = LANCommanderClient.GetAllocatedKey(manifest.Id);
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Settings.InstallDirectory);
script.AddVariable("ServerAddress", Settings.ServerAddress);
script.AddVariable("AllocatedKey", key);
script.UseFile(path);
return script.Execute();
}
return 0;
}
}
}

View File

@ -1,4 +1,6 @@
using LANCommander.SDK.Enums;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell;
using Playnite.SDK;
using Playnite.SDK.Models;
using Playnite.SDK.Plugins;
@ -25,11 +27,35 @@ namespace LANCommander.PlaynitePlugin
{
var gameManager = new LANCommander.SDK.GameManager(Plugin.LANCommanderClient, Plugin.Settings.InstallDirectory);
try
{
var scriptPath = ScriptHelper.GetScriptFilePath(Game.InstallDirectory, SDK.Enums.ScriptType.Uninstall);
if (!String.IsNullOrEmpty(scriptPath) && File.Exists(scriptPath))
{
var manifest = ManifestHelper.Read(Game.InstallDirectory);
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", Game.InstallDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", Plugin.Settings.InstallDirectory);
script.AddVariable("ServerAddress", Plugin.Settings.ServerAddress);
script.UseFile(scriptPath);
script.Execute();
}
}
catch (Exception ex)
{
Logger.Error(ex, "There was an error running the uninstall script");
}
gameManager.Uninstall(Game.InstallDirectory);
}
catch (Exception ex)
{
Logger.Error(ex, "There was an error uninstalling the game");
}
InvokeOnUninstalled(new GameUninstalledEventArgs());

View File

@ -0,0 +1,64 @@
using LANCommander.PowerShell.Cmdlets;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
namespace LANCommander.PowerShell.Tests
{
[TestClass]
public class CmdletTests
{
[TestMethod]
public void ConvertToSerializedBase64ShouldBeDeserializable()
{
var testPhrase = "Hello world! This should be deserializable back to its original form.";
var encodingCmdlet = new ConvertToSerializedBase64Cmdlet()
{
Input = testPhrase
};
var encodingResults = encodingCmdlet.Invoke().OfType<string>().ToList();
Assert.AreEqual(1, encodingResults.Count);
var decodingCmdlet = new ConvertFromSerializedBase64Cmdlet()
{
Input = encodingResults.First()
};
var decodingResults = decodingCmdlet.Invoke().OfType<string>().ToList();
Assert.AreEqual(1, encodingResults.Count);
Assert.AreEqual(testPhrase, decodingResults.First());
}
[TestMethod]
[DataRow(640, 480, 640, 360, 16, 9)]
[DataRow(1024, 768, 1024, 576, 16, 9)]
[DataRow(1600, 1200, 1600, 900, 16, 9)]
[DataRow(1920, 1080, 1440, 1080, 4, 3)]
[DataRow(1366, 1024, 1024, 768, 4, 3)]
[DataRow(854, 480, 640, 480, 4, 3)]
public void ConvertAspectRatioShouldReturnCorrectBounds(int x1, int y1, int x2, int y2, int ratioX, int ratioY)
{
var aspectRatio = (double)ratioX / (double)ratioY;
var cmdlet = new ConvertAspectRatioCmdlet()
{
AspectRatio = aspectRatio,
Width = x1,
Height = y1
};
var output = cmdlet.Invoke().OfType<DisplayResolution>().ToList();
Assert.AreEqual(1, output.Count);
var bounds = output.First();
Assert.AreEqual(x2, bounds.Width);
Assert.AreEqual(y2, bounds.Height);
}
}
}

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{D7069A13-F0AA-4CBF-9013-4276F130A6DD}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LANCommander.PowerShell.Tests</RootNamespace>
<AssemblyName>LANCommander.PowerShell.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.2.10\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.2.2.10\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</ItemGroup>
<ItemGroup>
<Compile Include="Cmdlets.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LANCommander.PowerShell\LANCommander.PowerShell.csproj">
<Project>{807943bf-0c7d-4ed3-8393-cfee64e3138c}</Project>
<Name>LANCommander.PowerShell</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.2.2.10\build\net46\MSTest.TestAdapter.targets')" />
</Project>

View File

@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: AssemblyTitle("LANCommander.PowerShell.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LANCommander.PowerShell.Tests")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("d7069a13-f0aa-4cbf-9013-4276f130a6dd")]
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MSTest.TestAdapter" version="2.2.10" targetFramework="net462" />
<package id="MSTest.TestFramework" version="2.2.10" targetFramework="net462" />
</packages>

View File

@ -0,0 +1,45 @@
using System;
using System.Management.Automation;
namespace LANCommander.PowerShell.Cmdlets
{
public class DisplayResolution
{
public int Width { get; set; }
public int Height { get; set; }
}
[Cmdlet(VerbsData.Convert, "AspectRatio")]
[OutputType(typeof(string))]
public class ConvertAspectRatioCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0)]
public int Width { get; set; }
[Parameter(Mandatory = true, Position = 1)]
public int Height { get; set; }
[Parameter(Mandatory = true, Position = 2)]
public double AspectRatio { get; set; }
protected override void ProcessRecord()
{
var resolution = new DisplayResolution();
// Display is wider, pillar box
if ((Width / Height) < AspectRatio)
{
resolution.Width = (int)Math.Ceiling(Height * AspectRatio);
resolution.Height = Height;
}
// Letterbox
else
{
resolution.Width = Width;
resolution.Height = (int)Math.Ceiling(Width * (1 / AspectRatio));
}
WriteObject(resolution);
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsData.ConvertFrom, "SerializedBase64")]
[OutputType(typeof(object))]
public class ConvertFromSerializedBase64Cmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Input { get; set; }
protected override void ProcessRecord()
{
var xml = Encoding.UTF8.GetString(Convert.FromBase64String(Input));
WriteObject(PSSerializer.Deserialize(xml));
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsData.ConvertTo, "SerializedBase64")]
[OutputType(typeof(object))]
public class ConvertToSerializedBase64Cmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public object Input { get; set; }
protected override void ProcessRecord()
{
var output = Convert.ToBase64String(Encoding.UTF8.GetBytes(PSSerializer.Serialize(Input)));
WriteObject(output);
}
}
}

View File

@ -0,0 +1,40 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using System.Management.Automation;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsData.ConvertTo, "StringBytes")]
[OutputType(typeof(byte[]))]
public class ConvertToStringBytesCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Input { get; set; }
[Parameter]
public bool Utf16 { get; set; } = false;
[Parameter]
public bool BigEndian { get; set; } = false;
[Parameter]
public int MaxLength { get; set; } = 0;
protected override void ProcessRecord()
{
byte[] output;
if (MaxLength > 0 && Input.Length > MaxLength)
Input = Input.Substring(0, MaxLength);
if (Utf16 && BigEndian)
output = System.Text.Encoding.BigEndianUnicode.GetBytes(Input);
else if (Utf16)
output = System.Text.Encoding.Unicode.GetBytes(Input);
else
output = System.Text.Encoding.ASCII.GetBytes(Input);
WriteObject(output);
}
}
}

View File

@ -0,0 +1,30 @@
using System;
using System.IO;
using System.Management.Automation;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsData.Edit, "PatchBinary")]
[OutputType(typeof(string))]
public class EditPatchBinaryCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0)]
public long Offset { get; set; }
[Parameter(Mandatory = true, Position = 1)]
public byte[] Data { get; set; }
[Parameter(Mandatory = true, Position = 2)]
public string FilePath { get; set; }
protected override void ProcessRecord()
{
using (var writer = File.OpenWrite(FilePath))
{
writer.Seek(Offset, SeekOrigin.Begin);
writer.Write(Data, 0, Data.Length);
}
}
}
}

View File

@ -0,0 +1,19 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using System.Management.Automation;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsCommon.Get, "GameManifest")]
[OutputType(typeof(GameManifest))]
public class GetGameManifestCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Path { get; set; }
protected override void ProcessRecord()
{
WriteObject(ManifestHelper.Read(Path));
}
}
}

View File

@ -0,0 +1,18 @@
using System.Linq;
using System.Management.Automation;
using System.Windows.Forms;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsCommon.Get, "PrimaryDisplay")]
[OutputType(typeof(string))]
public class GetPrimaryDisplayCmdlet : Cmdlet
{
protected override void ProcessRecord()
{
var screens = Screen.AllScreens;
WriteObject(screens.First(s => s.Primary));
}
}
}

View File

@ -0,0 +1,132 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Windows.Forms;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsLifecycle.Install, "Game")]
[OutputType(typeof(string))]
public class InstallGameCmdlet : Cmdlet
{
[Parameter(Mandatory = true)]
public Client Client { get; set; }
[Parameter(Mandatory = true)]
public Guid Id { get; set; }
[Parameter(Mandatory = false)]
public string InstallDirectory { get; set; } = "C:\\Games";
protected override void ProcessRecord()
{
var gameManager = new GameManager(Client, InstallDirectory);
var game = Client.GetGame(Id);
var progress = new ProgressRecord(1, $"Installing {game.Title}", "Progress:");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
gameManager.OnArchiveExtractionProgress += (long position, long length) =>
{
// Only update a max of every 500ms
if (stopwatch.ElapsedMilliseconds > 500)
{
progress.PercentComplete = (int)Math.Ceiling((position / (decimal)length) * 100);
WriteProgress(progress);
stopwatch.Restart();
}
};
var installDirectory = gameManager.Install(Id);
stopwatch.Stop();
RunInstallScript(installDirectory);
RunNameChangeScript(installDirectory);
RunKeyChangeScript(installDirectory);
WriteObject(installDirectory);
}
private int RunInstallScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
script.UseFile(ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.Install));
return script.Execute();
}
return 0;
}
private int RunNameChangeScript(string installDirectory)
{
var user = Client.GetProfile();
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.NameChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
script.AddVariable("OldPlayerAlias", "");
script.AddVariable("NewPlayerAlias", user.UserName);
script.UseFile(path);
return script.Execute();
}
return 0;
}
private int RunKeyChangeScript(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
var path = ScriptHelper.GetScriptFilePath(installDirectory, SDK.Enums.ScriptType.KeyChange);
if (File.Exists(path))
{
var script = new PowerShellScript();
var key = Client.GetAllocatedKey(manifest.Id);
script.AddVariable("InstallDirectory", installDirectory);
script.AddVariable("GameManifest", manifest);
script.AddVariable("DefaultInstallDirectory", InstallDirectory);
script.AddVariable("ServerAddress", Client.BaseUrl);
script.AddVariable("AllocatedKey", key);
script.UseFile(path);
return script.Execute();
}
return 0;
}
}
}

View File

@ -0,0 +1,42 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.PowerShell;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Windows.Forms;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsLifecycle.Uninstall, "Game")]
[OutputType(typeof(string))]
public class UninstallGameCmdlet : Cmdlet
{
[Parameter(Mandatory = true)]
public string InstallDirectory { get; set; }
protected override void ProcessRecord()
{
var scriptPath = ScriptHelper.GetScriptFilePath(InstallDirectory, SDK.Enums.ScriptType.Uninstall);
if (!String.IsNullOrEmpty(scriptPath) && File.Exists(scriptPath))
{
var manifest = ManifestHelper.Read(InstallDirectory);
var script = new PowerShellScript();
script.AddVariable("InstallDirectory", InstallDirectory);
script.AddVariable("GameManifest", manifest);
script.UseFile(scriptPath);
script.Execute();
}
var gameManager = new GameManager(null, InstallDirectory);
gameManager.Uninstall(InstallDirectory);
}
}
}

View File

@ -0,0 +1,24 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using System.Management.Automation;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsCommunications.Write, "GameManifest")]
[OutputType(typeof(string))]
public class WriteGameManifestCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public string Path { get; set; }
[Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
public GameManifest Manifest { get; set; }
protected override void ProcessRecord()
{
var destination = ManifestHelper.Write(Manifest, Path);
WriteObject(destination);
}
}
}

View File

@ -0,0 +1,31 @@
using System.IO;
using System.Management.Automation;
using System.Text.RegularExpressions;
namespace LANCommander.PowerShell.Cmdlets
{
[Cmdlet(VerbsCommunications.Write, "ReplaceContentInFile")]
[OutputType(typeof(string))]
public class ReplaceContentInFileCmdlet : Cmdlet
{
[Parameter(Mandatory = true, Position = 0)]
public string Pattern { get; set; }
[Parameter(Mandatory = true, Position = 1)]
public string Substitution { get; set; }
[Parameter(Mandatory = true, Position = 2)]
public string FilePath { get; set; }
protected override void ProcessRecord()
{
var contents = File.ReadAllText(FilePath);
var regex = new Regex(Pattern, RegexOptions.Multiline);
var result = regex.Replace(contents, Substitution);
WriteObject(result);
}
}
}

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{807943BF-0C7D-4ED3-8393-CFEE64E3138C}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LANCommander.PowerShell</RootNamespace>
<AssemblyName>LANCommander.PowerShell</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\PowerShellStandard.Library.5.1.1\lib\net452\System.Management.Automation.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Cmdlets\ConvertTo-SerializedBase64.cs" />
<Compile Include="Cmdlets\ConvertFrom-SerializedBase64.cs" />
<Compile Include="Cmdlets\Edit-PatchBinary.cs" />
<Compile Include="Cmdlets\Uninstall-Game.cs" />
<Compile Include="Cmdlets\Install-Game.cs" />
<Compile Include="Cmdlets\Write-ReplaceContentInFile.cs" />
<Compile Include="Cmdlets\ConvertTo-StringBytes.cs" />
<Compile Include="Cmdlets\Get-PrimaryDisplay.cs" />
<Compile Include="Cmdlets\Convert-AspectRatio.cs" />
<Compile Include="Cmdlets\Get-GameManifest.cs" />
<Compile Include="Cmdlets\Write-GameManifest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="LANCommander.PowerShell.psd1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\LANCommander.SDK\LANCommander.SDK.csproj">
<Project>{4c2a71fd-a30b-4d62-888a-4ef843d8e506}</Project>
<Name>LANCommander.SDK</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Binary file not shown.

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("LANCommander.PowerShell")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LANCommander.PowerShell")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("807943bf-0c7d-4ed3-8393-cfee64e3138c")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="PowerShellStandard.Library" version="5.1.1" targetFramework="net472" />
</packages>

View File

@ -20,16 +20,22 @@ namespace LANCommander.SDK
private readonly RestClient ApiClient;
private AuthToken Token;
public string BaseUrl;
public Client(string baseUrl)
{
if (!String.IsNullOrWhiteSpace(baseUrl))
ApiClient = new RestClient(baseUrl);
BaseUrl = baseUrl;
if (!String.IsNullOrWhiteSpace(BaseUrl))
ApiClient = new RestClient(BaseUrl);
}
public Client(string baseUrl, ILogger logger)
{
if (!String.IsNullOrWhiteSpace(baseUrl))
ApiClient = new RestClient(baseUrl);
BaseUrl = baseUrl;
if (!String.IsNullOrWhiteSpace(BaseUrl))
ApiClient = new RestClient(BaseUrl);
Logger = logger;
}

View File

@ -91,36 +91,11 @@ namespace LANCommander.SDK
ScriptHelper.SaveScript(game, ScriptType.NameChange);
ScriptHelper.SaveScript(game, ScriptType.KeyChange);
try
{
PowerShellRuntime.RunScript(game, ScriptType.Install);
PowerShellRuntime.RunScript(game, ScriptType.NameChange, /* Plugin.Settings.PlayerName */ "");
var key = Client.GetAllocatedKey(game.Id);
PowerShellRuntime.RunScript(game, ScriptType.KeyChange, $"\"{key}\"");
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not execute post-install scripts");
}
return result.Directory;
}
public void Uninstall(string installDirectory)
{
var manifest = ManifestHelper.Read(installDirectory);
try
{
Logger?.LogTrace("Running uninstall script");
PowerShellRuntime.RunScript(installDirectory, ScriptType.Uninstall);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Error running uninstall script");
}
Logger?.LogTrace("Attempting to delete the install directory");

View File

@ -1,6 +1,7 @@
using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.Models;
using LANCommander.SDK.PowerShell;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
@ -126,7 +127,14 @@ namespace LANCommander.SDK
{
var registryImportFileContents = File.ReadAllText(registryImportFilePath);
PowerShellRuntime.RunCommand($"regedit.exe /s \"{registryImportFilePath}\"", registryImportFileContents.Contains("HKEY_LOCAL_MACHINE"));
var script = new PowerShellScript();
script.UseInline($"regedit.exe /s \"{registryImportFilePath}\"");
if (registryImportFileContents.Contains("HKEY_LOCAL_MACHINE"))
script.RunAsAdmin();
script.Execute();
}
#endregion
@ -199,7 +207,11 @@ namespace LANCommander.SDK
tempRegFiles.Add(tempRegFile);
}
PowerShellRuntime.RunCommand(exportCommand.ToString());
var script = new PowerShellScript();
script.UseInline(exportCommand.ToString());
script.Execute();
var exportFile = new StringBuilder();

View File

@ -30,7 +30,7 @@ namespace LANCommander.SDK.Helpers
return manifest;
}
public static void Write(GameManifest manifest, string installDirectory)
public static string Write(GameManifest manifest, string installDirectory)
{
var destination = GetPath(installDirectory);
@ -47,6 +47,8 @@ namespace LANCommander.SDK.Helpers
Logger?.LogTrace("Writing manifest file");
File.WriteAllText(destination, yaml);
return destination;
}
public static string GetPath(string installDirectory)

View File

@ -14,15 +14,24 @@ namespace LANCommander.SDK.Helpers
public static readonly ILogger Logger;
public static string SaveTempScript(Script script)
{
var tempPath = SaveTempScript(script.Contents);
Logger?.LogTrace("Wrote script {Script} to {Destination}", script.Name, tempPath);
return tempPath;
}
public static string SaveTempScript(string contents)
{
var tempPath = Path.GetTempFileName();
// PowerShell will only run scripts with the .ps1 file extension
File.Move(tempPath, tempPath + ".ps1");
Logger?.LogTrace("Writing script {Script} to {Destination}", script.Name, tempPath);
tempPath = tempPath + ".ps1";
File.WriteAllText(tempPath, script.Contents);
File.WriteAllText(tempPath, contents);
return tempPath;
}
@ -37,7 +46,7 @@ namespace LANCommander.SDK.Helpers
if (script.RequiresAdmin)
script.Contents = "# Requires Admin" + "\r\n\r\n" + script.Contents;
var filename = PowerShellRuntime.GetScriptFilePath(game, type);
var filename = GetScriptFilePath(game, type);
if (File.Exists(filename))
File.Delete(filename);
@ -46,5 +55,24 @@ namespace LANCommander.SDK.Helpers
File.WriteAllText(filename, script.Contents);
}
public static string GetScriptFilePath(Game game, ScriptType type)
{
return GetScriptFilePath(game.InstallDirectory, type);
}
public static string GetScriptFilePath(string installDirectory, 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" }
};
var filename = filenames[type];
return Path.Combine(installDirectory, filename);
}
}
}

View File

@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="PowerShellStandard.Library" Version="5.1.1" />
<PackageReference Include="RestSharp" Version="106.15.0" />
<PackageReference Include="SharpCompress" Version="0.34.1" />
<PackageReference Include="YamlDotNet" Version="5.4.0" />

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LANCommander.SDK.PowerShell
{
public class PowerShellArgument
{
public string Name { get; set; }
public object Value { get; set; }
public Type Type { get; set; }
public PowerShellArgument(string name, object value, Type type)
{
Name = name;
Value = value;
Type = type;
}
}
}

View File

@ -0,0 +1,184 @@
using LANCommander.SDK.Helpers;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace LANCommander.SDK.PowerShell
{
public class PowerShellScript
{
private string Contents { get; set; } = "";
private string WorkingDirectory { get; set; } = "";
private bool AsAdmin { get; set; } = false;
private bool ShellExecute { get; set; } = false;
private bool IgnoreWow64 { get; set; } = false;
private ICollection<PowerShellVariable> Variables { get; set; }
private Dictionary<string, string> Arguments { get; set; }
private List<string> Modules { get; set; }
private Process Process { get; set; }
public PowerShellScript()
{
Variables = new List<PowerShellVariable>();
Arguments = new Dictionary<string, string>();
Modules = new List<string>();
Process = new Process();
Process.StartInfo.FileName = "powershell.exe";
Process.StartInfo.RedirectStandardOutput = false;
AddArgument("ExecutionPolicy", "Unrestricted");
AddModule(Path.Combine(Environment.CurrentDirectory, "LANCommander.PowerShell.psd1"));
}
public PowerShellScript UseFile(string path)
{
Contents = File.ReadAllText(path);
return this;
}
public PowerShellScript UseInline(string contents)
{
Contents = contents;
return this;
}
public PowerShellScript UseWorkingDirectory(string path)
{
WorkingDirectory = path;
return this;
}
public PowerShellScript UseShellExecute()
{
ShellExecute = true;
return this;
}
public PowerShellScript AddVariable<T>(string name, T value)
{
Variables.Add(new PowerShellVariable(name, value, typeof(T)));
return this;
}
public PowerShellScript AddArgument<T>(string name, T value)
{
Arguments.Add(name, $"\"{value}\"");
return this;
}
public PowerShellScript AddArgument(string name, int value)
{
Arguments[name] = value.ToString();
return this;
}
public PowerShellScript AddArgument(string name, long value)
{
Arguments[name] = value.ToString();
return this;
}
public PowerShellScript AddModule(string path)
{
Modules.Add(path);
return this;
}
public PowerShellScript RunAsAdmin()
{
AsAdmin = true;
Process.StartInfo.Verb = "runas";
Process.StartInfo.UseShellExecute = true;
return this;
}
public PowerShellScript IgnoreWow64Redirection()
{
IgnoreWow64 = true;
return this;
}
public int Execute()
{
var scriptBuilder = new StringBuilder();
var wow64Value = IntPtr.Zero;
foreach (var module in Modules)
{
scriptBuilder.AppendLine($"Import-Module \"{module}\"");
}
foreach (var variable in Variables)
{
scriptBuilder.AppendLine($"${variable.Name} = ConvertFrom-SerializedBase64 \"{Serialize(variable.Value)}\"");
}
scriptBuilder.AppendLine(Contents);
var path = ScriptHelper.SaveTempScript(scriptBuilder.ToString());
AddArgument("File", path);
if (IgnoreWow64)
Wow64DisableWow64FsRedirection(ref wow64Value);
Process.StartInfo.Arguments = String.Join(" ", Arguments.Select((name, value) =>
{
return $"-{name} {value}";
}));
if (!String.IsNullOrEmpty(WorkingDirectory))
Process.StartInfo.WorkingDirectory = WorkingDirectory;
if (ShellExecute)
Process.StartInfo.UseShellExecute = true;
if (AsAdmin)
{
Process.StartInfo.Verb = "runas";
Process.StartInfo.UseShellExecute = true;
}
Process.Start();
Process.WaitForExit();
if (IgnoreWow64)
Wow64RevertWow64FsRedirection(ref wow64Value);
if (File.Exists(path))
File.Delete(path);
return Process.ExitCode;
}
public static string Serialize<T>(T input)
{
// Use the PowerShell serializer to generate XML for our input. Then convert to base64 so we can put it on one line.
return Convert.ToBase64String(Encoding.UTF8.GetBytes(System.Management.Automation.PSSerializer.Serialize(input)));
}
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64RevertWow64FsRedirection(ref IntPtr ptr);
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace LANCommander.SDK.PowerShell
{
public class PowerShellVariable
{
public string Name { get; set; }
public object Value { get; set; }
public Type Type { get; set; }
public PowerShellVariable(string name, object value, Type type)
{
Name = name;
Value = value;
Type = type;
}
}
}

View File

@ -1,173 +0,0 @@
using LANCommander.SDK.Enums;
using LANCommander.SDK.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace LANCommander.SDK
{
public static class PowerShellRuntime
{
public static readonly ILogger Logger;
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool Wow64RevertWow64FsRedirection(ref IntPtr ptr);
public static void RunCommand(string command, bool asAdmin = false)
{
Logger?.LogTrace($"Executing command `{command}` | Admin: {asAdmin}");
var tempScript = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".ps1");
Logger?.LogTrace($"Creating temp script at path {tempScript}");
File.WriteAllText(tempScript, command);
RunScript(tempScript, asAdmin);
File.Delete(tempScript);
}
public static int RunScript(string path, bool asAdmin = false, string arguments = null, string workingDirectory = null)
{
Logger?.LogTrace($"Executing script at path {path} | Admin: {asAdmin} | Arguments: {arguments}");
var wow64Value = IntPtr.Zero;
// Disable Wow64 redirection so we can hit areas of the registry absolutely
Wow64DisableWow64FsRedirection(ref wow64Value);
var process = new Process();
process.StartInfo.FileName = "powershell.exe";
process.StartInfo.Arguments = $@"-ExecutionPolicy Unrestricted -File ""{path}""";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = false;
if (arguments != null)
process.StartInfo.Arguments += " " + arguments;
if (workingDirectory != null)
process.StartInfo.WorkingDirectory = workingDirectory;
if (asAdmin)
{
process.StartInfo.Verb = "runas";
process.StartInfo.UseShellExecute = true;
}
process.Start();
process.WaitForExit();
Wow64RevertWow64FsRedirection(ref wow64Value);
return process.ExitCode;
}
public static void RunScript(Game game, ScriptType type, string arguments = null)
{
RunScript(game.InstallDirectory, type, arguments);
}
public static void RunScript(string installDirectory, ScriptType type, string arguments = null)
{
var path = GetScriptFilePath(installDirectory, type);
if (File.Exists(path))
{
var contents = File.ReadAllText(path);
if (contents.StartsWith("# Requires Admin"))
RunScript(path, true, arguments);
else
RunScript(path, false, arguments);
}
}
public static void RunScriptsAsAdmin(IEnumerable<string> paths, string arguments = null)
{
// Concatenate scripts
var sb = new StringBuilder();
Logger?.LogTrace("Concatenating scripts...");
foreach (var path in paths)
{
var contents = File.ReadAllText(path);
sb.AppendLine(contents);
Logger?.LogTrace($"Added {path}!");
}
Logger?.LogTrace("Done concatenating!");
if (sb.Length > 0)
{
var scriptPath = Path.GetTempFileName();
Logger?.LogTrace($"Creating temp script at path {scriptPath}");
File.WriteAllText(scriptPath, sb.ToString());
RunScript(scriptPath, true, arguments);
}
}
public static void RunScripts(IEnumerable<string> installDirectories, ScriptType type, string arguments = null)
{
List<string> scripts = new List<string>();
List<string> adminScripts = new List<string>();
foreach (var installDirectory in installDirectories)
{
var path = GetScriptFilePath(installDirectory, type);
if (!File.Exists(path))
continue;
var contents = File.ReadAllText(path);
if (contents.StartsWith("# Requires Admin"))
adminScripts.Add(path);
else
scripts.Add(path);
}
RunScriptsAsAdmin(adminScripts, arguments);
foreach (var script in scripts)
{
RunScript(script, false, arguments);
}
}
public static string GetScriptFilePath(Game game, ScriptType type)
{
return GetScriptFilePath(game.InstallDirectory, type);
}
public static string GetScriptFilePath(string installDirectory, 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" }
};
var filename = filenames[type];
return Path.Combine(installDirectory, filename);
}
}
}

View File

@ -2,6 +2,7 @@
using LANCommander.SDK.Extensions;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.Models;
using LANCommander.SDK.PowerShell;
using Microsoft.Extensions.Logging;
using SharpCompress.Common;
using SharpCompress.Readers;
@ -57,7 +58,7 @@ namespace LANCommander.SDK
var detectionScript = redistributable.Scripts.FirstOrDefault(s => s.Type == ScriptType.DetectInstall);
detectionScriptTempFile = ScriptHelper.SaveTempScript(detectionScript);
var detectionResult = PowerShellRuntime.RunScript(detectionScriptTempFile, detectionScript.RequiresAdmin);
var detectionResult = RunScript(detectionScriptTempFile, redistributable);
// Redistributable is not installed
if (detectionResult == 0)
@ -70,12 +71,12 @@ namespace LANCommander.SDK
{
extractTempPath = extractionResult.Directory;
PowerShellRuntime.RunScript(installScriptTempFile, installScript.RequiresAdmin, null, extractTempPath);
RunScript(installScriptTempFile, redistributable, installScript.RequiresAdmin, extractTempPath);
}
}
else
{
PowerShellRuntime.RunScript(installScriptTempFile, installScript.RequiresAdmin, null, extractTempPath);
RunScript(installScriptTempFile, redistributable, installScript.RequiresAdmin, extractTempPath);
}
}
}
@ -164,5 +165,20 @@ namespace LANCommander.SDK
return extractionResult;
}
private int RunScript(string path, Redistributable redistributable, bool requiresAdmin = false, string workingDirectory = "")
{
var script = new PowerShellScript();
script.AddVariable("Redistributable", redistributable);
script.UseWorkingDirectory(workingDirectory);
script.UseFile(path);
if (requiresAdmin)
script.RunAsAdmin();
return script.Execute();
}
}
}

View File

@ -11,6 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LANCommander.SDK", "LANComm
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LANCommander.PCGamingWiki", "LANCommander.PCGamingWiki\LANCommander.PCGamingWiki.csproj", "{2436B817-4475-4E70-9BB2-E1E7866DB79F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LANCommander.PowerShell", "LANCommander.PowerShell\LANCommander.PowerShell.csproj", "{807943BF-0C7D-4ED3-8393-CFEE64E3138C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LANCommander.PowerShell.Tests", "LANCommander.PowerShell.Tests\LANCommander.PowerShell.Tests.csproj", "{D7069A13-F0AA-4CBF-9013-4276F130A6DD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -33,6 +37,14 @@ Global
{2436B817-4475-4E70-9BB2-E1E7866DB79F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2436B817-4475-4E70-9BB2-E1E7866DB79F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2436B817-4475-4E70-9BB2-E1E7866DB79F}.Release|Any CPU.Build.0 = Release|Any CPU
{807943BF-0C7D-4ED3-8393-CFEE64E3138C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{807943BF-0C7D-4ED3-8393-CFEE64E3138C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{807943BF-0C7D-4ED3-8393-CFEE64E3138C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{807943BF-0C7D-4ED3-8393-CFEE64E3138C}.Release|Any CPU.Build.0 = Release|Any CPU
{D7069A13-F0AA-4CBF-9013-4276F130A6DD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D7069A13-F0AA-4CBF-9013-4276F130A6DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D7069A13-F0AA-4CBF-9013-4276F130A6DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D7069A13-F0AA-4CBF-9013-4276F130A6DD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,51 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace LANCommander.Migrations
{
/// <inheritdoc />
public partial class DeleteDeprecatedSnippets : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
string[] snippetsToRemove = new string[]
{
"Examples\\Replace Content In File.ps1",
"Examples\\Separate ASCII Bytes.ps1",
"Examples\\String to ASCII Bytes.ps1",
"Functions\\Get-43Resolution.ps1",
"Functions\\Get-AsciiBytes.ps1",
"Functions\\Patch-Binary.ps1",
"Functions\\Separate-AsciiBytes.ps1",
"Variables\\Display.ps1",
"Variables\\InstallDir.ps1",
"Variables\\NewName.ps1",
"Variables\\OldName.ps1",
};
foreach (var snippet in snippetsToRemove)
{
var path = Path.Combine("Snippets", snippet);
if (File.Exists(path))
File.Delete(path);
}
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$NewName = $args[0]' || char(13) || char(10), '')");
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$OldName = \"\"' || char(13) || char(10) || 'if ($args[1]) {' || char(13) || char(10) || char(9) || '$OldName = $args[1]' || char(13) || char(10) || '}' || char(13) || char(10), '')");
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$InstallDir = $PSScriptRoot' || char(13) || char(10), '')");
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$InstallDir', '$InstallDirectory')");
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$NewName', '$NewPlayerAlias')");
migrationBuilder.Sql("UPDATE Scripts SET Contents = REPLACE(Contents, '$OldName', '$OldPlayerAlias')");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@ -1 +1 @@
Copy-Item -Path "$InstallDir\<Source Path>" -Destination "$InstallDir\<Destination Path>" -Recurse
Copy-Item -Path "$InstallDirectory\<Source Path>" -Destination "$InstallDirectory\<Destination Path>" -Recurse

View File

@ -1 +1 @@
New-Item -ItemType Directory -Force -Path "$InstallDir\<Path>"
New-Item -ItemType Directory -Force -Path "$InstallDirectory\<Path>"

View File

@ -1,2 +1,2 @@
# Writes byte[] to a file at an offset
Patch-Binary -FilePath "$InstallDir\<File Path>" -Offset 0x00 -Data $bytes
Patch-Binary -FilePath "$InstallDirectory\<File Path>" -Offset 0x00 -Data $bytes

View File

@ -1 +1 @@
Remove-Item "$InstallDir\<DirectoryPath>" -Recurse -ErrorAction Ignore
Remove-Item "$InstallDirectory\<DirectoryPath>" -Recurse -ErrorAction Ignore

View File

@ -1 +1 @@
Rename-Item -Path "$InstallDir\<FilePath>" -NewName "$InstallDir\<NewName>"
Rename-Item -Path "$InstallDirectory\<FilePath>" -NewName "$InstallDirectory\<NewName>"

View File

@ -1,2 +0,0 @@
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '^game.setPlayerName "(.+)"' -Replacement "game.setPlayerName ""$NewName""" -FilePath "$InstallDir\<File Path>"

View File

@ -1,2 +0,0 @@
# Takes an input byte[] and separates it with 0x00 between each character
$bytes = Separate-AsciiBytes -Data $bytes

View File

@ -9,4 +9,4 @@
# WIN7RTM
# WIN8RTM
# See: https://ss64.com/nt/syntax-compatibility.html
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" -Name "$InstallDir\<Executable>" -Value "~ WINXPSP2 HIGHDPIAWARE" -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" -Name "$InstallDirectory\<Executable>" -Value "~ WINXPSP2 HIGHDPIAWARE" -Force

View File

@ -1,2 +0,0 @@
# Convert an input string to ASCII-encoded byte[]. Shorter strings will pad out to 12 bytes, longer strings will be trimmed.
$bytes = Get-AsciiBytes -InputString "Hello world!" -MaxLength 12

View File

@ -1,4 +1,4 @@
# Trim a string down to a specified amount of characters
if ($NewName.Length -gt 10) {
$NewName = $NewName.Substring(0, 10);
if ($NewPlayerAlias.Length -gt 10) {
$NewPlayerAlias = $NewPlayerAlias.Substring(0, 10);
}

View File

@ -1,2 +1,2 @@
# Write contents of a string to a file
Set-Content "$InstallDir\<File Path>" "Hello world!"
Set-Content "$InstallDirectory\<File Path>" "Hello world!"

View File

@ -0,0 +1,2 @@
# Bounds accessible via $Resolution.Height, $Resolution.Width
$Resolution = Convert-AspectRatio -Width 1280 -Height 800 -AspectRatio (4 / 3)

View File

@ -0,0 +1,2 @@
# Converts a string to a UTF16-encoded byte array. This looks like ASCII characters separated by 0x00 in most cases.
$bytes = ConvertTo-StringBytes -Input "Hello World!" -Utf16 1 -MaxLength 12

View File

@ -0,0 +1 @@
Edit-PatchBinary -FilePath "$InstallDirectory\<File Path>" -Offset 0x00 -Data $bytes

View File

@ -1,14 +0,0 @@
function Get-43Resolution([int]$Width, [int]$Height) {
$ratio = 4 / 3
if (($Width -gt $Height) -or ($Width -eq $Height)) {
return @{ Width = [math]::Round($ratio * $Height); Height = $Height }
}
if ($Width -lt $Height) {
return @{ Width = $Width; Height = [math]::Round($Width / $ratio) }
}
}
# Accessible via $Resolution.Height, $Resolution.Width
$Resolution = Get-43Resolution -Width 1280 -Height 800

View File

@ -1,30 +0,0 @@
function Get-AsciiBytes([string]$InputString, [int]$MaxLength)
{
if ($InputString.Length -gt $MaxLength)
{
$InputString = $InputString.Substring(0, $MaxLength)
}
$bytes = [System.Text.Encoding]::ASCII.GetBytes($InputString)
$array = @()
$count = 0
$extraPadding = $MaxLength - $bytes.Length
foreach ($byte in $bytes)
{
if ($count -lt $MaxLength)
{
$array += $byte
$count++
}
}
# Pad the end with 0x00 to meet our max length
for ($i = $count; $i -lt $MaxLength; $i++)
{
$array += 0x00
}
return $array
}

View File

@ -0,0 +1 @@
$manifest = Get-GameManifest "C:\\Games\\<Game Directory>"

View File

@ -0,0 +1,2 @@
# Bounds are accessible by $Resolution.Width and $Resolution.Height
$Resolution = Get-PrimaryDisplay

View File

@ -1,11 +0,0 @@
function Patch-Binary([byte[]]$Data, [int]$Offset, [string]$FilePath)
{
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
for ($i = 0; $i -lt $Data.Length; $i++)
{
$bytes[$Offset + $i] = $Data[$i]
}
[System.IO.File]::WriteAllBytes($FilePath, $bytes)
}

View File

@ -1,12 +0,0 @@
function Separate-AsciiBytes([byte[]]$Data)
{
$array = @()
foreach ($byte in $Data)
{
$array += $byte
$array += 0x00
}
return $array
}

View File

@ -1,5 +1,2 @@
function Write-ReplaceContentInFile([string]$Regex, [string]$Replacement, [string]$FilePath)
{
$content = (Get-Content $FilePath) -replace $Regex, $Replacement
[IO.File]::WriteAllLines($FilePath, $content)
}
# Use regex to replace text within a file. Quotes are escaped by double quoting ("")
Write-ReplaceContentInFile -Regex '^game.setPlayerName "(.+)"' -Replacement "game.setPlayerName ""$NewName""" -FilePath "$InstallDir\<File Path>"

View File

@ -0,0 +1 @@
$AllocatedKey

View File

@ -0,0 +1 @@
$DefaultInstallDirectory

View File

@ -1,3 +0,0 @@
# Accessible via $Display.Width and $Display.Height
Add-Type -AssemblyName System.Windows.Forms
$Display = ([System.Windows.Forms.Screen]::AllScreens | Where-Object Primary).Bounds

View File

@ -0,0 +1 @@
$OldPlayerAlias

View File

@ -0,0 +1 @@
$GameManifest

View File

@ -1 +0,0 @@
$InstallDir = $PSScriptRoot

View File

@ -0,0 +1 @@
$ServerAddress