fix issues with downloading files

This commit is contained in:
Gustave Monce
2024-09-01 12:45:46 +02:00
parent 29c6ab06d4
commit 91961b0e7b
5 changed files with 64 additions and 26 deletions
@@ -927,15 +927,16 @@ namespace WPinternals
uint length = uint.Parse(info.Length.ToString());
int offset = 0;
const int maximumBufferSize = 0x00240000;
int maximumBufferSize = (int)Info.WriteBufferSize;
uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize);
using FileStream MMOSFile = new(MMOSPath, FileMode.Open, FileAccess.Read, FileShare.Read);
for (int i = 1; i <= (uint)Math.Truncate((decimal)length / maximumBufferSize); i++)
for (int i = 0; i < chunkCount; i++)
{
Progress.IncreaseProgress(1);
byte[] data = new byte[maximumBufferSize];
MMOSFile.Read(data, 0, maximumBufferSize);
@@ -950,6 +951,7 @@ namespace WPinternals
byte[] data = new byte[length - offset];
MMOSFile.Read(data, 0, (int)(length - offset));
LoadMmosBinary(length, (uint)offset, false, data);
}
+12 -6
View File
@@ -116,12 +116,18 @@ namespace WPinternals
internal void SwitchToMmosContext()
{
byte[] Request = new byte[7];
ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "A");
ExecuteRawVoidMethod(Request);
ByteOperations.WriteAsciiString(Request, 0, $"{SwitchModeSignature}A");
byte[] Response = ExecuteRawMethod(Request);
if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU")
{
throw new NotSupportedException();
}
ResetDevice();
Dispose(true);
UInt16 Error = (UInt16)((Response[6] << 8) + Response[7]);
if (Error > 0)
{
throw new NotSupportedException("SwitchToPhoneInfoAppContext: Error 0x" + Error.ToString("X4"));
}
}
internal void SwitchToFlashAppContext()
@@ -131,7 +137,7 @@ namespace WPinternals
bool ModernFlashApp = info.VersionMajor >= 2;
byte[] Request = new byte[7];
ByteOperations.WriteAsciiString(Request, 0, SwitchModeSignature + "F"); // This will stop charging the phone
ByteOperations.WriteAsciiString(Request, 0, $"{SwitchModeSignature}F"); // This will stop charging the phone
byte[] Response = ExecuteRawMethod(Request);
if (ByteOperations.ReadAsciiString(Response, 0, 4) == "NOKU")
{
+1 -1
View File
@@ -723,7 +723,7 @@ namespace WPinternals
//_ = Client.DownloadFileAsync(Uri, Path.Combine(Folder, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath)), Client_DownloadProgressChanged, Client_DownloadFileCompleted);
Client = new(Folder, 4);
Client = new(Folder, 4, false);
_ = Client.DownloadAsync([new FileDownloadInformation(URL, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath), Size, null, null)], this);
}).Start();
+27 -4
View File
@@ -275,12 +275,11 @@ namespace WPinternals
if (tmpFileInfo.Length == downloadFile.FileSize)
{
/*//Decrypted file should match estimated bytes.
//Decrypted file should match estimated bytes.
//Imagine if it crashed during hashing, the file may be valid.
//So... lets rename this file so it can be verified.
File.Move(tempFilePath, filePath);
totalBytesRead = tmpFileInfo.Length;*/
File.Delete(tempFilePath);
totalBytesRead = tmpFileInfo.Length;
}
else if (tmpFileInfo.Length < downloadFile.FileSize)
{
@@ -384,8 +383,32 @@ namespace WPinternals
//just an assumption
if (verifyFiles)
{
_ = streamToWriteTo.Seek(0, SeekOrigin.Begin);
return await HashWithProgress(streamToWriteTo);
bool hashResult = await HashWithProgress(streamToWriteTo);
if (hashResult)
{
streamToWriteTo.Close();
File.Move(tempFilePath, filePath);
}
return hashResult;
}
else
{
streamToWriteTo.Close();
File.Move(tempFilePath, filePath);
downloadProgress?.Report(new FileDownloadStatus(downloadFile)
{
DownloadedBytes = totalBytesRead,
FileStatus = FileStatus.Completed
});
return true;
}
}
while (currRange < contentLength.Value)
+11 -4
View File
@@ -936,6 +936,8 @@ namespace WPinternals
string TempFolder = $@"{Environment.GetEnvironmentVariable("TEMP")}\WPInternals";
LumiaFlashAppModel FlashModel = (LumiaFlashAppModel)PhoneNotifier.CurrentModel;
Task.Run(() =>
{
ModeSwitchProgressWrapper("Initializing Flash...", null);
string MMOSPath = Path.Combine(TempFolder, Name);
@@ -944,18 +946,23 @@ namespace WPinternals
PhoneNotifier.NewDeviceArrived += NewDeviceArrived;
LumiaFlashAppPhoneInfo Info = FlashModel.ReadPhoneInfo();
FileInfo info = new(MMOSPath);
uint fileLength = uint.Parse(info.Length.ToString());
const int maximumBufferSize = 0x00240000;
uint chunkCount = (uint)Math.Truncate((decimal)fileLength / maximumBufferSize);
uint length = uint.Parse(info.Length.ToString());
int maximumBufferSize = (int)Info.WriteBufferSize;
uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize);
UIContext?.Post(d => SetWorkingStatus("Flashing Test Mode package...", MaxProgressValue: 100), null);
ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UIContext?.Post(d => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i), null));
ProgressUpdater progressUpdater = new(chunkCount + 1, (int i, TimeSpan? time) => UpdateWorkingStatus(null, CurrentProgressValue: (ulong)i));
FlashModel.FlashMMOS(MMOSPath, progressUpdater);
ModeSwitchProgressWrapper("And now booting phone to MMOS...", "If the phone stays on the lightning cog screen for a while, you may need to unplug and replug the phone to continue the boot process.");
});
}
private void SwitchFromPhoneInfoToMassStorageMode(bool Continuation = false)