Skip to content

Commit

Permalink
Unfinished: Downloader rework Part I
Browse files Browse the repository at this point in the history
  • Loading branch information
villermen committed May 2, 2017
1 parent 021c97a commit bb494cf
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 451 deletions.
366 changes: 18 additions & 348 deletions RuneScapeCacheTools/Cache/Downloader/DownloaderCache.cs

Large diffs are not rendered by default.

47 changes: 0 additions & 47 deletions RuneScapeCacheTools/Cache/Downloader/FileRequest.cs

This file was deleted.

49 changes: 49 additions & 0 deletions RuneScapeCacheTools/Cache/Downloader/HttpFileDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Villermen.RuneScapeCacheTools.Cache.Files;
using Villermen.RuneScapeCacheTools.Cache.RuneTek5;
using Villermen.RuneScapeCacheTools.Exceptions;
using Villermen.RuneScapeCacheTools.Extensions;

namespace Villermen.RuneScapeCacheTools.Cache.Downloader
{
public class HttpFileDownloader : IFileDownloader
{
private readonly string _baseUrl;

public HttpFileDownloader(string baseUrl = "http://content.runescape.com")
{
this._baseUrl = baseUrl;
}

public async Task<BinaryFile> DownloadFileAsync(Index index, int fileId, CacheFileInfo fileInfo)
{
var webRequest = WebRequest.CreateHttp($"{this._baseUrl}/ms?m=0&a={(int)index}&g={fileId}&c={fileInfo.Crc}&v={fileInfo.Version}");

using (var response = (HttpWebResponse)await webRequest.GetResponseAsync())
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new DownloaderException($"HTTP interface responded with status code: {response.StatusCode}.");
}

if (response.ContentLength != fileInfo.CompressedSize - 2)
{
throw new DownloaderException($"Downloaded file size {response.ContentLength} does not match expected {fileInfo.CompressedSize - 2}.");
}

var dataStream = new MemoryStream();
var dataWriter = new BinaryWriter(dataStream);

var responseReader = new BinaryReader(response.GetResponseStream());
dataWriter.Write(responseReader.ReadBytes((int)response.ContentLength));

// Append version
dataWriter.WriteUInt16BigEndian((ushort)fileInfo.Version);

return RuneTek5FileDecoder.DecodeFile(dataStream.ToArray(), fileInfo);
}
}
}
}
9 changes: 0 additions & 9 deletions RuneScapeCacheTools/Cache/Downloader/HttpFileRequest.cs

This file was deleted.

12 changes: 12 additions & 0 deletions RuneScapeCacheTools/Cache/Downloader/IFileDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Villermen.RuneScapeCacheTools.Cache.Files;

namespace Villermen.RuneScapeCacheTools.Cache.Downloader
{
public interface IFileDownloader
{
Task<BinaryFile> DownloadFileAsync(Index index, int fileId, CacheFileInfo fileInfo);
}
}
Loading

0 comments on commit bb494cf

Please sign in to comment.