forked from 91Act/curl-unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ProgressCallback; Added options to specify request range; Remov…
…ed auto created CurlMultiUpdater since it would cause unexpected behaviours when hit stop button in Unity
- Loading branch information
root
authored and
root
committed
Nov 9, 2019
1 parent
7e2fb7b
commit a156f1f
Showing
14 changed files
with
245 additions
and
166 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using CurlUnity; | ||
using System; | ||
using System.IO; | ||
using UnityEngine; | ||
|
||
public class SampleResumeDownload : MonoBehaviour | ||
{ | ||
public string m_link; | ||
public string m_localPath; | ||
|
||
private long m_downloadedBytes; | ||
private long m_nowBytes; | ||
private long m_totalBytes; | ||
|
||
private CurlMulti m_multi; | ||
|
||
void Start() | ||
{ | ||
m_multi = new CurlMulti(); | ||
ResumeDownload(); | ||
} | ||
|
||
async void ResumeDownload() | ||
{ | ||
var easy = new CurlEasy(); | ||
easy.uri = new Uri(m_link); | ||
easy.method = "HEAD"; | ||
easy.debug = true; | ||
|
||
if (await easy.PerformAsync() == CURLE.OK) | ||
{ | ||
if (easy.GetResponseHeader("Accept-Ranges") == "bytes") | ||
{ | ||
m_downloadedBytes = 0; | ||
if (File.Exists(m_localPath)) m_downloadedBytes = (new FileInfo(m_localPath)).Length; | ||
int.TryParse(easy.GetResponseHeader("Content-Length"), out var contentLength); | ||
if (contentLength != 0 && contentLength <= m_downloadedBytes) | ||
{ | ||
Debug.LogWarning($"Link {m_link} already downloaded at: {m_localPath}"); | ||
} | ||
else | ||
{ | ||
Debug.Log($"Start to download {m_link} in range {m_downloadedBytes}-{contentLength}"); | ||
easy.method = "GET"; | ||
easy.outputPath = m_localPath; | ||
easy.rangeStart = m_downloadedBytes; | ||
easy.rangeEnd = contentLength; | ||
easy.progressCallback = (dltotal, dlnow, ultotal, ulnow, _) => | ||
{ | ||
m_nowBytes = dlnow; | ||
m_totalBytes = dltotal; | ||
return 0; | ||
}; | ||
easy.performCallback = (result, _) => | ||
{ | ||
if (result == CURLE.OK) | ||
{ | ||
Debug.Log($"Link {m_link} downloaded"); | ||
} | ||
else | ||
{ | ||
Debug.LogWarning($"Failed to download link {m_link}"); | ||
} | ||
}; | ||
// Use multi perform so we could interrupt the download | ||
easy.MultiPerform(m_multi); | ||
} | ||
} | ||
else | ||
{ | ||
Debug.LogWarning($"Link {m_link} doesn't support Accept-Ranges"); | ||
} | ||
} | ||
else | ||
{ | ||
Debug.LogWarning($"Failed to get info of link {m_link}"); | ||
} | ||
} | ||
|
||
private void OnGUI() | ||
{ | ||
GUILayout.Label($"Download progress {m_nowBytes}-{m_totalBytes} ({m_downloadedBytes} already downloaded)"); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Assets/curl-unity/CurlTest.cs.meta → Assets/Samples/SampleResumeDownload.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Assets/Scenes/SampleScene.unity.meta → ...s/Samples/SampleResumeDownload.unity.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.