client side of package downloader; incomplete
This commit is contained in:
@@ -30,7 +30,7 @@ namespace OpenRa.FileFormats
|
||||
public class Global
|
||||
{
|
||||
public string Map = "scm12ea.ini";
|
||||
public string[] Mods = {}; // filename:sha1 pairs.
|
||||
public string[] Packages = {}; // filename:sha1 pairs.
|
||||
public int OrderLatency = 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,13 +382,6 @@ namespace OpenRa.Game
|
||||
|
||||
// todo: if we don't have all the resources, we don't want to do this yet.
|
||||
|
||||
if (mapName != LobbyInfo.GlobalSettings.Map)
|
||||
{
|
||||
chat.AddLine(Color.White, "Debug",
|
||||
"Map change {0} -> {1}".F(mapName, session.GlobalSettings.Map));
|
||||
ChangeMap(LobbyInfo.GlobalSettings.Map);
|
||||
}
|
||||
|
||||
if (Game.orderManager.FramesAhead != LobbyInfo.GlobalSettings.OrderLatency
|
||||
&& !Game.orderManager.GameStarted)
|
||||
{
|
||||
@@ -396,6 +389,17 @@ namespace OpenRa.Game
|
||||
Game.chat.AddLine(Color.White, "Server",
|
||||
"Order lag is now {0} frames.".F(LobbyInfo.GlobalSettings.OrderLatency));
|
||||
}
|
||||
|
||||
PackageDownloader.SetPackageList(LobbyInfo.GlobalSettings.Packages);
|
||||
if (!PackageDownloader.IsIdle())
|
||||
return;
|
||||
|
||||
if (mapName != LobbyInfo.GlobalSettings.Map)
|
||||
{
|
||||
chat.AddLine(Color.White, "Debug",
|
||||
"Map change {0} -> {1}".F(mapName, session.GlobalSettings.Map));
|
||||
ChangeMap(LobbyInfo.GlobalSettings.Map);
|
||||
}
|
||||
}
|
||||
|
||||
public static void StartGame()
|
||||
|
||||
@@ -117,6 +117,7 @@
|
||||
<Compile Include="Orders\SellOrderGenerator.cs" />
|
||||
<Compile Include="Orders\ChronoshiftDestinationOrderGenerator.cs" />
|
||||
<Compile Include="Ore.cs" />
|
||||
<Compile Include="PackageDownloader.cs" />
|
||||
<Compile Include="PathSearch.cs" />
|
||||
<Compile Include="ProductionItem.cs" />
|
||||
<Compile Include="Orders\ReplayOrderSource.cs" />
|
||||
|
||||
@@ -60,6 +60,11 @@ namespace OpenRa.Game.Orders
|
||||
Game.SyncLobbyInfo(order.TargetString);
|
||||
break;
|
||||
}
|
||||
case "FileChunk":
|
||||
{
|
||||
PackageDownloader.ReceiveChunk(order.TargetString);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
|
||||
73
OpenRa.Game/PackageDownloader.cs
Normal file
73
OpenRa.Game/PackageDownloader.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using OpenRa.FileFormats;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace OpenRa.Game
|
||||
{
|
||||
static class PackageDownloader
|
||||
{
|
||||
static string[] allPackages = { };
|
||||
static List<string> missingPackages = new List<string>();
|
||||
static string currentPackage = null;
|
||||
static MemoryStream content = null;
|
||||
|
||||
public static void SetPackageList(string[] packages)
|
||||
{
|
||||
allPackages = packages;
|
||||
missingPackages = allPackages.Where(p => !HavePackage(p)).ToList();
|
||||
|
||||
if (currentPackage == null || !missingPackages.Contains(currentPackage))
|
||||
BeginDownload();
|
||||
}
|
||||
|
||||
public static void ReceiveChunk(string data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void BeginDownload()
|
||||
{
|
||||
if (missingPackages.Count == 0)
|
||||
{ // we're finished downloading resources!
|
||||
currentPackage = null;
|
||||
return;
|
||||
}
|
||||
|
||||
currentPackage = missingPackages[0];
|
||||
missingPackages.RemoveAt(0);
|
||||
|
||||
content = new MemoryStream();
|
||||
}
|
||||
|
||||
static void EndDownload()
|
||||
{
|
||||
File.WriteAllBytes(currentPackage.Split(':')[0], content.ToArray());
|
||||
currentPackage = null;
|
||||
|
||||
BeginDownload();
|
||||
}
|
||||
|
||||
public static bool IsIdle()
|
||||
{
|
||||
return currentPackage == null
|
||||
&& missingPackages.Count == 0;
|
||||
}
|
||||
|
||||
static bool HavePackage(string p)
|
||||
{
|
||||
var parts = p.Split(':');
|
||||
return File.Exists(parts[0]) && CalculateSHA1(parts[0]) == parts[1];
|
||||
}
|
||||
|
||||
public static string CalculateSHA1(string filename)
|
||||
{
|
||||
using (var csp = SHA1.Create())
|
||||
return new string(csp.ComputeHash(File.ReadAllBytes(filename))
|
||||
.SelectMany(a => a.ToString("x2")).ToArray());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user