public static bool TryParse(string rawdatastring, string baseUrl, out PSO2File _pso2file) { string[] splitbuffer = null; if (rawdatastring.IndexOf(Microsoft.VisualBasic.ControlChars.Tab) > -1) { splitbuffer = rawdatastring.Split(_tabonly, 5, StringSplitOptions.RemoveEmptyEntries); } else if (rawdatastring.IndexOf(" ") > -1) { splitbuffer = rawdatastring.Split(_spaceonly, 5, StringSplitOptions.RemoveEmptyEntries); } if (splitbuffer != null) { if (splitbuffer.Length >= 4) { _pso2file = new PSO2File(splitbuffer[0], splitbuffer[2], splitbuffer[1], baseUrl); return(true); } else if (splitbuffer.Length == 3) { _pso2file = new PSO2File(splitbuffer[0], splitbuffer[1], splitbuffer[2], baseUrl); return(true); } else { _pso2file = null; return(false); } } else { _pso2file = null; return(false); } }
/// <summary> /// Download a <seealso cref="PSO2File"/> from the server /// </summary> /// <param name="file">Specify the file</param> /// <param name="outStream">Output stream</param> /// <param name="cancellationTokenSource">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns></returns> public async Task DownloadFileAsync(PSO2File file, Stream outStream, CancellationTokenSource cancellationTokenSource) { if (!outStream.CanWrite) { throw new ArgumentException("The stream should be writable", "outStream"); } if (cancellationTokenSource == null) { using (var response = await this.downloader.GetAsync(file.Url, HttpCompletionOption.ResponseHeadersRead)) if (response.IsSuccessStatusCode) { using (Stream srcStream = await response.Content.ReadAsStreamAsync()) { byte[] buffer = new byte[1024]; int readbyte = srcStream.Read(buffer, 0, buffer.Length); while (readbyte > 0) { outStream.Write(buffer, 0, readbyte); readbyte = srcStream.Read(buffer, 0, buffer.Length); } outStream.Flush(); } } } else { using (var response = await this.downloader.GetAsync(file.Url, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token)) if (response.IsSuccessStatusCode) { using (Stream srcStream = await response.Content.ReadAsStreamAsync()) if (!cancellationTokenSource.IsCancellationRequested) { byte[] buffer = new byte[1024]; int readbyte = srcStream.Read(buffer, 0, buffer.Length); while (readbyte > 0 && !cancellationTokenSource.IsCancellationRequested) { outStream.Write(buffer, 0, readbyte); readbyte = srcStream.Read(buffer, 0, buffer.Length); } outStream.Flush(); } } } }
public static bool TryParse(string rawdatastring, ClientVersionCheckResult baseUrl, out PSO2File _pso2file) { string[] splitbuffer = null; if (rawdatastring.IndexOf(Microsoft.VisualBasic.ControlChars.Tab) > -1) { splitbuffer = rawdatastring.Split(_tabonly, 5, StringSplitOptions.RemoveEmptyEntries); } else if (rawdatastring.IndexOf(" ") > -1) { splitbuffer = rawdatastring.Split(_spaceonly, 5, StringSplitOptions.RemoveEmptyEntries); } if (splitbuffer != null) { if (splitbuffer.Length >= 4) { if (StringHelper.IsEqual(splitbuffer[3], "m", true)) { _pso2file = new PSO2File(splitbuffer[0], splitbuffer[2], splitbuffer[1], baseUrl.MasterURL); } else { _pso2file = new PSO2File(splitbuffer[0], splitbuffer[2], splitbuffer[1], baseUrl.PatchURL); } return(true); } else if (splitbuffer.Length == 3) { _pso2file = new PSO2File(splitbuffer[0], splitbuffer[1], splitbuffer[2], baseUrl.PatchURL); return(true); } else { _pso2file = null; return(false); } } else { _pso2file = null; return(false); } }
/// <summary> /// Download a <seealso cref="PSO2File"/> from the server /// </summary> /// <param name="file">Specify the file</param> /// <param name="outStream">Output stream</param> /// <returns></returns> public Task DownloadFileAsync(PSO2File file, Stream outStream) => this.DownloadFileAsync(file, outStream, null);
/// <summary> /// Get the patch list(s) from a specific version /// </summary> /// <param name="version">Specific version to determine the patchlist</param> /// <param name="patchListType">Specify which patch lists will be downloaded</param> /// <returns></returns> public async Task <RemotePatchlist> GetPatchlistAsync(ClientVersionCheckResult version, PatchListType patchListType) { RemotePatchlist result = null; int total = 0, current = 0; if ((patchListType & PatchListType.Master) == PatchListType.Master) { total++; } if ((patchListType & PatchListType.Patch) == PatchListType.Patch) { total++; } if ((patchListType & PatchListType.LauncherList) == PatchListType.LauncherList) { total++; } if ((patchListType & PatchListType.Master) == PatchListType.Master) { using (var response = await this.downloader.GetAsync(UriHelper.URLConcat(version.MasterURL, DefaultValues.PatchInfo.called_patchlist), HttpCompletionOption.ResponseHeadersRead)) { if (response.IsSuccessStatusCode) { if (result == null) { result = new RemotePatchlist(); } using (var responseStream = await response.Content.ReadAsStreamAsync()) using (var sr = new StreamReader(responseStream)) { string currentline; while (!sr.EndOfStream) { currentline = sr.ReadLine(); if (!string.IsNullOrWhiteSpace(currentline)) { if (PSO2File.TryParse(currentline, version, out var _pso2file)) { result.AddOrUpdate(_pso2file); } } } } } } Interlocked.Increment(ref current); this.ProgressChanged?.Invoke(current, total); } if ((patchListType & PatchListType.Patch) == PatchListType.Patch) { using (var response = await this.downloader.GetAsync(UriHelper.URLConcat(version.PatchURL, DefaultValues.PatchInfo.called_patchlist), HttpCompletionOption.ResponseHeadersRead)) { if (response.IsSuccessStatusCode) { if (result == null) { result = new RemotePatchlist(); } using (var responseStream = await response.Content.ReadAsStreamAsync()) using (var sr = new StreamReader(responseStream)) { string currentline; while (!sr.EndOfStream) { currentline = sr.ReadLine(); if (!string.IsNullOrWhiteSpace(currentline)) { if (PSO2File.TryParse(currentline, version, out var _pso2file)) { result.AddOrUpdate(_pso2file); } } } } } } Interlocked.Increment(ref current); this.ProgressChanged?.Invoke(current, total); } if ((patchListType & PatchListType.LauncherList) == PatchListType.LauncherList) { using (var response = await this.downloader.GetAsync(UriHelper.URLConcat(version.PatchURL, DefaultValues.PatchInfo.file_launcher), HttpCompletionOption.ResponseHeadersRead)) { if (response.IsSuccessStatusCode) { if (result == null) { result = new RemotePatchlist(); } using (var responseStream = await response.Content.ReadAsStreamAsync()) using (var sr = new StreamReader(responseStream)) { string currentline; while (!sr.EndOfStream) { currentline = sr.ReadLine(); if (!string.IsNullOrWhiteSpace(currentline)) { if (PSO2File.TryParse(currentline, version, out var _pso2file)) { result.AddOrUpdate(_pso2file); } } } } } } Interlocked.Increment(ref current); this.ProgressChanged?.Invoke(current, total); } if (result == null) { throw new WebException(); } else { return(result); } }