Skip to content

Commit

Permalink
fix: Download issues with ENOSW
Browse files Browse the repository at this point in the history
  • Loading branch information
gus33000 committed Sep 1, 2024
1 parent b03b561 commit 29c6ab0
Show file tree
Hide file tree
Showing 4 changed files with 811 additions and 143 deletions.
41 changes: 20 additions & 21 deletions WPinternals/Models/UEFIApps/LumiaFlashAppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -927,36 +927,35 @@ public void FlashMMOS(string MMOSPath, ProgressUpdater UpdaterPerChunk)
uint length = uint.Parse(info.Length.ToString());

int offset = 0;
const int maximumbuffersize = 0x00240000;
const int maximumBufferSize = 0x00240000;

uint totalcounts = (uint)Math.Truncate((decimal)length / maximumbuffersize);
uint chunkCount = (uint)Math.Truncate((decimal)length / maximumBufferSize);

using (FileStream MMOSFile = new(MMOSPath, FileMode.Open, FileAccess.Read))
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 = 1; i <= (uint)Math.Truncate((decimal)length / maximumbuffersize); i++)
{
Progress.IncreaseProgress(1);
byte[] data = new byte[maximumbuffersize];
MMOSFile.Read(data, 0, maximumbuffersize);
Progress.IncreaseProgress(1);
byte[] data = new byte[maximumBufferSize];
MMOSFile.Read(data, 0, maximumBufferSize);

LoadMmosBinary(length, (uint)offset, false, data);
LoadMmosBinary(length, (uint)offset, false, data);

offset += maximumbuffersize;
}

if (length - offset != 0)
{
Progress.IncreaseProgress(1);
offset += maximumBufferSize;
}

byte[] data = new byte[length - offset];
MMOSFile.Read(data, 0, (int)(length - offset));
LoadMmosBinary(length, (uint)offset, false, data);
}
if (length - offset != 0)
{
Progress.IncreaseProgress(1);

SwitchToMmosContext();
ResetPhone();
byte[] data = new byte[length - offset];
MMOSFile.Read(data, 0, (int)(length - offset));
LoadMmosBinary(length, (uint)offset, false, data);
}

SwitchToMmosContext();
ResetPhone();

LogFile.EndAction("FlashMMOS");
}

Expand Down
81 changes: 69 additions & 12 deletions WPinternals/ViewModels/DownloadsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
Expand Down Expand Up @@ -136,14 +135,30 @@ private void Notifier_NewDeviceArrived(ArrivalEventArgs Args)
internal static long GetFileLengthFromURL(string URL)
{
long Length = 0;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
req.Method = "HEAD";
req.ServicePoint.ConnectionLimit = 10;
using (WebResponse resp = req.GetResponse())

WebRequest webReq = WebRequest.Create(URL);

if (webReq is HttpWebRequest req)
{
long.TryParse(resp.Headers.Get("Content-Length"), out Length);
req.Method = "HEAD";
req.ServicePoint.ConnectionLimit = 10;
using (WebResponse resp = req.GetResponse())
{
long.TryParse(resp.Headers.Get("Content-Length"), out Length);
}
return Length;
}
return Length;
else if (webReq is FileWebRequest filereq)
{
webReq.Method = "HEAD";
using (WebResponse resp = webReq.GetResponse())
{
long.TryParse(resp.Headers.Get("Content-Length"), out Length);
}
return Length;
}

return 0;
}

internal static string GetFileNameFromURL(string URL)
Expand Down Expand Up @@ -672,7 +687,7 @@ internal enum DownloadStatus
Failed
};

internal class DownloadEntry : INotifyPropertyChanged
internal class DownloadEntry : INotifyPropertyChanged, IProgress<GeneralDownloadProgress>
{
private readonly SynchronizationContext UIContext;
public event PropertyChangedEventHandler PropertyChanged = delegate { };
Expand All @@ -681,7 +696,8 @@ internal class DownloadEntry : INotifyPropertyChanged
internal string URL;
internal string[] URLCollection;
internal string Folder;
internal HttpClient Client;
//internal HttpClient Client;
internal HttpDownloader Client;
internal long SpeedIndex = -1;
internal long[] Speeds = new long[10];
internal long LastBytesReceived;
Expand All @@ -703,11 +719,42 @@ internal DownloadEntry(string URL, string Folder, string[] URLCollection, Action
{
Size = DownloadsViewModel.GetFileLengthFromURL(URL);
Client = new HttpClient();
_ = Client.DownloadFileAsync(Uri, Path.Combine(Folder, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath)), Client_DownloadProgressChanged, Client_DownloadFileCompleted);
//Client = new HttpClient();
//_ = Client.DownloadFileAsync(Uri, Path.Combine(Folder, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath)), Client_DownloadProgressChanged, Client_DownloadFileCompleted);
Client = new(Folder, 4);
_ = Client.DownloadAsync([new FileDownloadInformation(URL, DownloadsViewModel.GetFileNameFromURL(Uri.LocalPath), Size, null, null)], this);
}).Start();
}

public void Report(GeneralDownloadProgress e)
{
foreach (FileDownloadStatus status in e.DownloadedStatus)
{
if (status == null)
{
continue;
}

if (status.FileStatus == FileStatus.Failed || status.FileStatus == FileStatus.Failed)
{
Client_DownloadFileCompleted(true);
}

if (status.FileStatus == FileStatus.Completed)
{
Client_DownloadFileCompleted(false);
}

if (status.FileStatus == FileStatus.Downloading)
{
Client_DownloadProgressChanged(new HttpClientDownloadProgress(status.DownloadedBytes, status.File.FileSize));
}
}
}

private void Client_DownloadFileCompleted(bool Error)
{
void Finish()
Expand All @@ -718,6 +765,9 @@ void Finish()
{
if (URLCollection?.Any(c => App.DownloadManager.DownloadList.Any(d => d.URL == c)) != true) // if there are no files left to download from this collection, then call the callback-function.
{
Client.Dispose();
Client = null;

string[] Files;
if (URLCollection == null)
{
Expand All @@ -738,7 +788,14 @@ void Finish()
}
}

UIContext?.Post(d => Finish(), null);
if (UIContext == null)
{
Finish();
}
else
{
UIContext?.Post(d => Finish(), null);
}
}

private void Client_DownloadProgressChanged(HttpClientDownloadProgress e)
Expand Down
Loading

0 comments on commit 29c6ab0

Please sign in to comment.