Skip to content

Commit

Permalink
Create Migration utility
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Jan 4, 2024
1 parent d97cd73 commit 0f3fed0
Show file tree
Hide file tree
Showing 9 changed files with 353 additions and 3 deletions.
16 changes: 16 additions & 0 deletions DefinitionMigrator/DefinitionMigrator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>AnyCPU;x64;x86;arm64</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ini-parser-netcore" Version="3.0.0" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions DefinitionMigrator/DefinitionParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

namespace DefinitionMigrator
{
internal class DefinitionParser
{
public ReadOnlyCollection<string> DriverDirectories;
public ReadOnlyCollection<string> AppDirectories;

public DefinitionParser(string DefinitionFile)
{
string content = File.ReadAllText(DefinitionFile);

IniParser.IniDataParser parser = new();
parser.Configuration.ThrowExceptionsOnError = false;
parser.Configuration.DuplicatePropertiesBehaviour = IniParser.Configuration.IniParserConfiguration.EDuplicatePropertiesBehaviour.AllowAndConcatenateValues;
parser.Configuration.AllowDuplicateSections = true;
parser.Configuration.AllowKeysWithoutSection = true;
parser.Configuration.CaseInsensitive = false;
parser.Configuration.SkipInvalidLines = true;
IniParser.IniData data = parser.Parse(content);

DriverDirectories = new ReadOnlyCollection<string>(data.Sections["Drivers"].Select(x => x.Key).ToList());
AppDirectories = new ReadOnlyCollection<string>(data.Sections["Apps"].Select(x => x.Key).ToList());
}
}
}
119 changes: 119 additions & 0 deletions DefinitionMigrator/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) The LumiaWOA and DuoWOA authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;

namespace DefinitionMigrator
{
internal static class Logging
{
public enum LoggingLevel
{
Information,
Warning,
Error
}

private static readonly object lockObj = new();

public static void ShowProgress(long CurrentProgress, long TotalProgress, DateTime startTime, bool DisplayRed)
{
DateTime now = DateTime.Now;
TimeSpan timeSoFar = now - startTime;

TimeSpan remaining = new(0);

try
{
remaining = TimeSpan.FromMilliseconds(timeSoFar.TotalMilliseconds / CurrentProgress * (TotalProgress - CurrentProgress));
}
catch { }

if (TotalProgress == 0)
{
TotalProgress = 1;
CurrentProgress = 1;
}

Log(string.Format("{0} {1:hh\\:mm\\:ss\\.f}", GetDismLikeProgBar((int)(CurrentProgress * 100 / TotalProgress)), remaining, remaining.TotalHours, remaining.Minutes, remaining.Seconds, remaining.Milliseconds), severity: DisplayRed ? LoggingLevel.Warning : LoggingLevel.Information, returnline: false);
}

private static string GetDismLikeProgBar(int perc)
{
int eqsLength = (int)((double)perc / 100 * 55);
string bases = new string('=', eqsLength) + new string(' ', 55 - eqsLength);
bases = bases.Insert(28, perc + "%");
if (perc == 100)
{
bases = bases[1..];
}
else if (perc < 10)
{
bases = bases.Insert(28, " ");
}

return "[" + bases + "]";
}

public static void Log(string message, LoggingLevel severity = LoggingLevel.Information, bool returnline = true)
{
lock (lockObj)
{
if (message?.Length == 0)
{
Console.WriteLine();
return;
}

ConsoleColor originalConsoleColor = Console.ForegroundColor;

string msg = "";

switch (severity)
{
case LoggingLevel.Warning:
msg = " Warning ";
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LoggingLevel.Error:
msg = " Error ";
Console.ForegroundColor = ConsoleColor.Red;
break;
case LoggingLevel.Information:
msg = "Information";
Console.ForegroundColor = originalConsoleColor;
break;
}

if (returnline)
{
Console.WriteLine(DateTime.Now.ToString("'['HH':'mm':'ss']'") + "[" + msg + "] " + message);
}
else
{
Console.Write("\r" + DateTime.Now.ToString("'['HH':'mm':'ss']'") + "[" + msg + "] " + message);
}

Console.ForegroundColor = originalConsoleColor;
}
}
}
}
131 changes: 131 additions & 0 deletions DefinitionMigrator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;

namespace DefinitionMigrator
{
internal class Program
{
private static void ConvertV1DefinitionsToV3(string dir)
{
IEnumerable<string> definitions = Directory.EnumerateFiles($"{dir}\\definitions", "*.txt", SearchOption.AllDirectories).ToArray();

foreach (string definition in definitions)
{
Logging.Log($"Processing {definition}");
string additionalFolder = "\\components\\ANYSOC\\Support\\Desktop\\SUPPORT.DESKTOP.POST_UPGRADE_ENABLEMENT";
IEnumerable<string> folders = File.ReadAllLines(definition).Where(x => !string.IsNullOrEmpty(x)).Select(x => dir + "\\" + x).Union(new string[] { dir + "\\" + additionalFolder });

IEnumerable<string> boundInfPackages = folders
.SelectMany(x => Directory.EnumerateFiles(x, "*.inf", SearchOption.AllDirectories))
.Where(x => x.EndsWith(".inf", StringComparison.InvariantCultureIgnoreCase))
.Order();

List<string> xmlLines = [.. boundInfPackages.Select(inf =>
{
string path = Path.GetDirectoryName(inf);
string name = Path.GetFileName(inf);
string xmlLine = " <DriverPackageFile Path=\"$(mspackageroot)" + path.Replace(dir, "") + "\" Name=\"" + name + "\" ID=\"" + Path.GetFileNameWithoutExtension(inf) + "\"/>";
return xmlLine;
}).Order()];

string start = "<FeatureManifest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/embedded/2004/10/ImageUpdate\" Revision=\"1\" SchemaVersion=\"1.3\">\r\n <Drivers>\r\n <BaseDriverPackages>";
string end = " </BaseDriverPackages>\r\n </Drivers>\r\n</FeatureManifest>";

xmlLines.Insert(0, start);
xmlLines.Add(end);

File.WriteAllLines(definition.Replace(".txt", ".xml"), xmlLines);
File.Delete(definition);
}
}

private static void ConvertV2DefinitionsToV3(string dir)
{
IEnumerable<string> definitions = Directory.EnumerateFiles($"{dir}\\definitions", "*.txt", SearchOption.AllDirectories).ToArray();

foreach (string definition in definitions)
{
Logging.Log($"Processing {definition}");
DefinitionParser definitionParser = new(definition);
IEnumerable<string> folders = definitionParser.DriverDirectories.Where(x => !string.IsNullOrEmpty(x)).Select(x => dir + "\\" + x);

IEnumerable<string> boundInfPackages = folders
.SelectMany(x => Directory.EnumerateFiles(x, "*.inf", SearchOption.AllDirectories))
.Where(x => x.EndsWith(".inf", StringComparison.InvariantCultureIgnoreCase))
.Order();

List<string> xmlLines = [.. boundInfPackages.Select(inf =>
{
string path = Path.GetDirectoryName(inf);
string name = Path.GetFileName(inf);
string xmlLine = " <DriverPackageFile Path=\"$(mspackageroot)" + path.Replace(dir, "") + "\" Name=\"" + name + "\" ID=\"" + Path.GetFileNameWithoutExtension(inf) + "\"/>";
return xmlLine;
}).Order()];

List<string> deps = GetAppPackages(dir, definitionParser.AppDirectories);
List<string> depXmlLines = [.. deps.Select(inf =>
{
string path = Path.GetDirectoryName(inf);
string name = Path.GetFileName(inf);
string xmlLine = " <PackageFile Path=\"$(mspackageroot)" + path.Replace(dir, "") + "\" Name=\"" + name + "\" ID=\"" + Path.GetFileNameWithoutExtension(inf) + "\"/>";
string licenseFile = Path.GetFileNameWithoutExtension(inf) + ".xml";
if (File.Exists(path + "\\" + licenseFile))
{
xmlLine = " <PackageFile Path=\"$(mspackageroot)" + path.Replace(dir, "") + "\" Name=\"" + name + "\" ID=\"" + Path.GetFileNameWithoutExtension(inf) + "\" LicenseFile=\"" + licenseFile + "\"/>";
}
return xmlLine;
}).Order()];

string start = "<FeatureManifest xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns=\"http://schemas.microsoft.com/embedded/2004/10/ImageUpdate\" Revision=\"1\" SchemaVersion=\"1.3\">\r\n <Drivers>\r\n <BaseDriverPackages>";
string mid = " </BaseDriverPackages>\r\n </Drivers>\r\n <AppX>\r\n <AppXPackages>";
string end = " </AppXPackages>\r\n </AppX>\r\n</FeatureManifest>";

xmlLines.Insert(0, start);
xmlLines.Add(mid);
xmlLines.AddRange(depXmlLines);
xmlLines.Add(end);

File.WriteAllLines(definition.Replace(".txt", ".xml"), xmlLines);
File.Delete(definition);
}
}

static void Main(string[] args)
{
ConvertV2DefinitionsToV3("C:\\Users\\Gus\\Documents\\GitHub\\SurfaceDuo-Drivers");
}

private static List<string> GetAppPackages(string DriverRepo, ReadOnlyCollection<string> appPaths)
{
List<string> deps = [];

foreach (string path in appPaths)
{
IEnumerable<string> appxs = Directory.EnumerateFiles($"{DriverRepo}\\{path}", "*.appx", SearchOption.AllDirectories)
.Where(x => x.EndsWith(".appx", StringComparison.InvariantCultureIgnoreCase));
IEnumerable<string> msixs = Directory.EnumerateFiles($"{DriverRepo}\\{path}", "*.msix", SearchOption.AllDirectories)
.Where(x => x.EndsWith(".msix", StringComparison.InvariantCultureIgnoreCase));
IEnumerable<string> appxbundles = Directory.EnumerateFiles($"{DriverRepo}\\{path}", "*.appxbundle", SearchOption.AllDirectories)
.Where(x => x.EndsWith(".appxbundle", StringComparison.InvariantCultureIgnoreCase));
IEnumerable<string> msixbundles = Directory.EnumerateFiles($"{DriverRepo}\\{path}", "*.msixbundle", SearchOption.AllDirectories)
.Where(x => x.EndsWith(".msixbundle", StringComparison.InvariantCultureIgnoreCase));

deps.AddRange(appxs);
deps.AddRange(msixs);
deps.AddRange(appxbundles);
deps.AddRange(msixbundles);
}

return deps;
}
}
}
32 changes: 32 additions & 0 deletions DefinitionMigrator/app.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.3" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
</application>
</compatibility>

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
24 changes: 24 additions & 0 deletions DriverUpdater.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,52 @@ VisualStudioVersion = 17.0.31710.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DriverUpdater", "DriverUpdater\DriverUpdater.csproj", "{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DefinitionMigrator", "DefinitionMigrator\DefinitionMigrator.csproj", "{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM64 = Debug|ARM64
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|ARM64 = Release|ARM64
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|ARM64.ActiveCfg = Debug|ARM64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|ARM64.Build.0 = Debug|ARM64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|x64.ActiveCfg = Debug|x64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|x64.Build.0 = Debug|x64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|x86.ActiveCfg = Debug|x86
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Debug|x86.Build.0 = Debug|x86
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|Any CPU.Build.0 = Release|Any CPU
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|ARM64.ActiveCfg = Release|ARM64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|ARM64.Build.0 = Release|ARM64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|x64.ActiveCfg = Release|x64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|x64.Build.0 = Release|x64
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|x86.ActiveCfg = Release|x86
{C2BD025D-5BDF-4922-8C4A-0E80897F5FE7}.Release|x86.Build.0 = Release|x86
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|ARM64.ActiveCfg = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|ARM64.Build.0 = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|x64.ActiveCfg = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|x64.Build.0 = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|x86.ActiveCfg = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Debug|x86.Build.0 = Debug|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|Any CPU.Build.0 = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|ARM64.ActiveCfg = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|ARM64.Build.0 = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|x64.ActiveCfg = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|x64.Build.0 = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|x86.ActiveCfg = Release|Any CPU
{11F965E1-EFB1-43EE-A99B-C8A1F15985B0}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion DriverUpdater/CksLicensing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private static int[] Locate(byte[] self, byte[] candidate)
list.Add(i);
}

return list.Count == 0 ? Empty : list.ToArray();
return list.Count == 0 ? Empty : [.. list];
}

private static bool IsMatch(byte[] array, int position, byte[] candidate)
Expand Down
Loading

0 comments on commit 0f3fed0

Please sign in to comment.