Skip to content

Commit

Permalink
add console output when creating vhdx
Browse files Browse the repository at this point in the history
  • Loading branch information
nixxou committed Jul 28, 2023
1 parent aecb494 commit 8003fc4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions IsoEnablerForRPCS3/IsoEnablerForRPCS3.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<PackageReference Include="CliWrap" Version="3.6.4" />
<PackageReference Include="CommunityToolkit.WinUI.Notifications" Version="7.1.2" />
<PackageReference Include="DiscUtils.Iso9660" Version="0.16.13" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="7.0.0" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />
</ItemGroup>

Expand Down
27 changes: 23 additions & 4 deletions IsoEnablerForRPCS3/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CliWrap;
using CommunityToolkit.WinUI.Notifications;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using Microsoft.Win32.TaskScheduler;
using PS3IsoLauncher;
using System.Diagnostics;
Expand All @@ -11,8 +12,20 @@

internal class Program
{
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
[DllImport("kernel32.dll",
EntryPoint = "GetStdHandle",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll",
EntryPoint = "AllocConsole",
SetLastError = true,
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;

[DllImport("kernel32.dll")]
private static extern bool FreeConsole();
Expand Down Expand Up @@ -200,7 +213,6 @@ private static void Main(string[] args)
if (!hideThisArg) filteredArgs.Add(arg);
}
args = filteredArgs.ToArray();

if (isopath != "")
{
var targetProcess = Process.GetProcessesByName("rpcs3").FirstOrDefault(p => p.MainWindowTitle != "");
Expand Down Expand Up @@ -350,7 +362,14 @@ private static void Main(string[] args)
if (generatevhdx && VHDXTool.GameDirChanged.Count() > 0)
{
AllocConsole();

IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
Encoding encoding = System.Text.Encoding.GetEncoding(65001);
StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);


string TitreGenerate = "";
TitreGenerate = "Generate VHDX From : \n";
Expand Down
25 changes: 19 additions & 6 deletions IsoEnablerForRPCS3/VHDXTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public bool Mount(bool AsReadOnly = false)
string CmdMountFile = Path.Combine(ConfigDir, "mountcmd.txt");
string FileToMount = IsoFilePath;
if (AsReadOnly) FileToMount += ":ro";
File.WriteAllText(CmdMountFile, IsoFilePath);
File.WriteAllText(CmdMountFile, FileToMount);

Program.ExecuteTask("IsoEnablerMount");

Expand Down Expand Up @@ -449,13 +449,19 @@ public static void CreateVHDX(string rootPath, string target)
string resultat = "";
var listFreeDriveLetters = Enumerable.Range('A', 'Z' - 'A' + 1).Select(i => (Char)i + ":").Except(DriveInfo.GetDrives().Select(s => s.Name.Replace("\\", ""))).ToList();



Task.Run(async () => { resultat = await ExecuteProcess($"New-VHD -Path \"{target}\" -Dynamic -SizeBytes {totalSizeNeeded}MB"); }).Wait();

Console.WriteLine($"New-VHD -Path \"{target}\" -Dynamic -SizeBytes {totalSizeNeeded}MB");


if (!File.Exists(target)) throw new FileNotFoundException("error creating vhd");

Task.Run(async () => { resultat = await ExecuteProcess($"Mount-VHD -Path \"{target}\""); }).Wait();

Console.WriteLine($"Mount-VHD -Path \"{target}\"");

Task.Run(async () => { resultat = await ExecuteProcess($"$disknum=(get-vhd -path \"{target}\").DiskNumber; echo $disknum"); }).Wait();
resultat = resultat.Trim();
int numdisk = -1;
Expand All @@ -471,9 +477,13 @@ public static void CreateVHDX(string rootPath, string target)
}
if (numdisk <= 0) throw new Exception("Cant find mounted disk");

Console.WriteLine($"NumDisk = \"{numdisk}\"");

Task.Run(async () => { resultat = await ExecuteProcess($"Initialize-Disk {numdisk}"); }).Wait();
Console.WriteLine($"Initialize-Disk {numdisk}");
Thread.Sleep(1000);
Task.Run(async () => { resultat = await ExecuteProcess($"New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber {numdisk}"); }).Wait();
Console.WriteLine($"New-Partition -AssignDriveLetter -UseMaximumSize -DiskNumber {numdisk}");
Thread.Sleep(1000);
Task.Run(async () => { resultat = await ExecuteProcess($"$drive = (Get-Partition (Get-DiskImage -ImagePath \"{target}\").Number | Get-Volume).DriveLetter;echo $drive"); }).Wait();

Expand All @@ -489,33 +499,36 @@ public static void CreateVHDX(string rootPath, string target)
throw new Exception("Invalide drive letter");
return;
}
Console.WriteLine($"driveLetter = {driveLetter}");
Thread.Sleep(1000);

Task.Run(async () => { resultat = await ExecuteProcess($"Format-Volume -FileSystem NTFS -DriveLetter {driveLetter}"); }).Wait();
Console.WriteLine($"Format-Volume -FileSystem NTFS -DriveLetter {driveLetter}");
Thread.Sleep(1000);
Task.Run(async () => { resultat = await ExecuteProcess($"Set-Volume -DriveLetter {driveLetter} -NewFileSystemLabel PSNGAME"); }).Wait();
Console.WriteLine($"Set-Volume -DriveLetter {driveLetter} -NewFileSystemLabel PSNGAME");



Console.WriteLine($"Copy Content");
foreach (var dir in GameDirChanged)
{
string sourceDir = Path.Combine(rootPath, dir);
string targetDir = Path.Combine(driveLetter + @":/", dir);
Console.WriteLine($"Copy {sourceDir} to {targetDir}");

Directory.CreateDirectory(targetDir);
DirectoryCopy(sourceDir, targetDir, true);
}
foreach (var rap in GameRapChanged)
{
if (File.Exists(rap))
{
Console.WriteLine($"Copy {rap}");
File.Copy(rap, Path.Combine(driveLetter + @":/", Path.GetFileName(rap)));
}
}

SetDiskProperty(driveLetter.ToString(), true);
Thread.Sleep(2000);

Task.Run(async () => { resultat = await ExecuteProcess($"Dismount-VHD \"{target}\""); }).Wait();
Console.WriteLine($"Dismount-VHD \"{target}\"");
Thread.Sleep(1000);

}
Expand Down

0 comments on commit 8003fc4

Please sign in to comment.