Properly use the virtual filesystem for map loading and saving.
This commit is contained in:
@@ -492,7 +492,7 @@ namespace OpenRA
|
||||
|
||||
public void Save(IReadWritePackage toPackage)
|
||||
{
|
||||
MapFormat = 8;
|
||||
MapFormat = SupportedMapFormat;
|
||||
|
||||
var root = new List<MiniYamlNode>();
|
||||
var fields = new[]
|
||||
@@ -534,10 +534,12 @@ namespace OpenRA
|
||||
root.Add(new MiniYamlNode("Notifications", null, NotificationDefinitions));
|
||||
root.Add(new MiniYamlNode("Translations", null, TranslationDefinitions));
|
||||
|
||||
// Saving to a new package: copy over all the content from the map
|
||||
if (Package != null && toPackage != Package)
|
||||
foreach (var file in Package.Contents)
|
||||
toPackage.Update(file, Package.GetStream(file).ReadAllBytes());
|
||||
|
||||
// Update the package with the new map data
|
||||
var s = root.WriteToString();
|
||||
toPackage.Update("map.yaml", Encoding.UTF8.GetBytes(s));
|
||||
toPackage.Update("map.bin", SaveBinaryData());
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace OpenRA
|
||||
public sealed class MapCache : IEnumerable<MapPreview>, IDisposable
|
||||
{
|
||||
public static readonly MapPreview UnknownMap = new MapPreview(null, MapGridType.Rectangular, null);
|
||||
public readonly IReadOnlyDictionary<IReadOnlyPackage, MapClassification> MapLocations;
|
||||
|
||||
readonly Cache<string, MapPreview> previews;
|
||||
readonly ModData modData;
|
||||
readonly SheetBuilder sheetBuilder;
|
||||
@@ -41,6 +43,36 @@ namespace OpenRA
|
||||
var gridType = Exts.Lazy(() => modData.Manifest.Get<MapGrid>().Type);
|
||||
previews = new Cache<string, MapPreview>(uid => new MapPreview(uid, gridType.Value, this));
|
||||
sheetBuilder = new SheetBuilder(SheetType.BGRA);
|
||||
|
||||
// Enumerate map directories
|
||||
var mapLocations = new Dictionary<IReadOnlyPackage, MapClassification>();
|
||||
foreach (var kv in modData.Manifest.MapFolders)
|
||||
{
|
||||
var name = kv.Key;
|
||||
var classification = string.IsNullOrEmpty(kv.Value)
|
||||
? MapClassification.Unknown : Enum<MapClassification>.Parse(kv.Value);
|
||||
|
||||
IReadOnlyPackage package;
|
||||
var optional = name.StartsWith("~");
|
||||
if (optional)
|
||||
name = name.Substring(1);
|
||||
|
||||
try
|
||||
{
|
||||
package = modData.ModFiles.OpenPackage(name);
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (optional)
|
||||
continue;
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
mapLocations.Add(package, classification);
|
||||
}
|
||||
|
||||
MapLocations = new ReadOnlyDictionary<IReadOnlyPackage, MapClassification>(mapLocations);
|
||||
}
|
||||
|
||||
public void LoadMaps()
|
||||
@@ -49,33 +81,33 @@ namespace OpenRA
|
||||
if (!modData.Manifest.Contains<MapGrid>())
|
||||
return;
|
||||
|
||||
// Expand the dictionary (dir path, dir type) to a dictionary of (map path, dir type)
|
||||
var mapPaths = modData.Manifest.MapFolders.SelectMany(kv =>
|
||||
FindMapsIn(modData.ModFiles, kv.Key).ToDictionary(p => p, p => string.IsNullOrEmpty(kv.Value)
|
||||
? MapClassification.Unknown : Enum<MapClassification>.Parse(kv.Value)));
|
||||
|
||||
var mapGrid = modData.Manifest.Get<MapGrid>();
|
||||
foreach (var path in mapPaths)
|
||||
var mapGrid = Game.ModData.Manifest.Get<MapGrid>();
|
||||
foreach (var kv in MapLocations)
|
||||
{
|
||||
IReadOnlyPackage package;
|
||||
try
|
||||
foreach (var map in kv.Key.Contents)
|
||||
{
|
||||
using (new Support.PerfTimer(path.Key))
|
||||
IReadOnlyPackage mapPackage = null;
|
||||
try
|
||||
{
|
||||
package = modData.ModFiles.OpenPackage(path.Key);
|
||||
var uid = Map.ComputeUID(package);
|
||||
previews[uid].UpdateFromMap(package, path.Value, modData.Manifest.MapCompatibility, mapGrid.Type);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (package != null)
|
||||
package.Dispose();
|
||||
using (new Support.PerfTimer(map))
|
||||
{
|
||||
mapPackage = modData.ModFiles.OpenPackage(map, kv.Key);
|
||||
if (mapPackage == null)
|
||||
continue;
|
||||
|
||||
Console.WriteLine("Failed to load map: {0}", path);
|
||||
Console.WriteLine("Details: {0}", e);
|
||||
Log.Write("debug", "Failed to load map: {0}", path);
|
||||
Log.Write("debug", "Details: {0}", e);
|
||||
var uid = Map.ComputeUID(mapPackage);
|
||||
previews[uid].UpdateFromMap(mapPackage, kv.Key, kv.Value, modData.Manifest.MapCompatibility, mapGrid.Type);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (mapPackage != null)
|
||||
mapPackage.Dispose();
|
||||
Console.WriteLine("Failed to load map: {0}", map);
|
||||
Console.WriteLine("Details: {0}", e);
|
||||
Log.Write("debug", "Failed to load map: {0}", map);
|
||||
Log.Write("debug", "Details: {0}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,31 +155,6 @@ namespace OpenRA
|
||||
new Download(url, _ => { }, onInfoComplete);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> FindMapsIn(FileSystem.FileSystem context, string dir)
|
||||
{
|
||||
string[] noMaps = { };
|
||||
|
||||
// Ignore optional flag
|
||||
if (dir.StartsWith("~"))
|
||||
dir = dir.Substring(1);
|
||||
|
||||
// HACK: We currently only support maps loaded from Folders
|
||||
// This is a temporary workaround that resolves the filesystem paths to a system directory
|
||||
IReadOnlyPackage package;
|
||||
string filename;
|
||||
if (context.TryGetPackageContaining(dir, out package, out filename))
|
||||
dir = Path.Combine(package.Name, filename);
|
||||
else if (Directory.Exists(Platform.ResolvePath(dir)))
|
||||
dir = Platform.ResolvePath(dir);
|
||||
else
|
||||
return noMaps;
|
||||
|
||||
var dirsWithMaps = Directory.GetDirectories(dir)
|
||||
.Where(d => Directory.GetFiles(d, "map.yaml").Any() && Directory.GetFiles(d, "map.bin").Any());
|
||||
|
||||
return dirsWithMaps.Concat(Directory.GetFiles(dir, "*.oramap"));
|
||||
}
|
||||
|
||||
void LoadAsyncInternal()
|
||||
{
|
||||
Log.Write("debug", "MapCache.LoadAsyncInternal started");
|
||||
|
||||
@@ -60,6 +60,7 @@ namespace OpenRA
|
||||
|
||||
public readonly string Uid;
|
||||
public IReadOnlyPackage Package { get; private set; }
|
||||
IReadOnlyPackage parentPackage;
|
||||
|
||||
public string Title { get; private set; }
|
||||
public string Type { get; private set; }
|
||||
@@ -116,7 +117,7 @@ namespace OpenRA
|
||||
Visibility = MapVisibility.Lobby;
|
||||
}
|
||||
|
||||
public void UpdateFromMap(IReadOnlyPackage p, MapClassification classification, string[] mapCompatibility, MapGridType gridType)
|
||||
public void UpdateFromMap(IReadOnlyPackage p, IReadOnlyPackage parent, MapClassification classification, string[] mapCompatibility, MapGridType gridType)
|
||||
{
|
||||
Dictionary<string, MiniYaml> yaml;
|
||||
using (var yamlStream = p.GetStream("map.yaml"))
|
||||
@@ -128,6 +129,7 @@ namespace OpenRA
|
||||
}
|
||||
|
||||
Package = p;
|
||||
parentPackage = parent;
|
||||
GridType = gridType;
|
||||
Class = classification;
|
||||
|
||||
@@ -270,11 +272,7 @@ namespace OpenRA
|
||||
return;
|
||||
|
||||
Status = MapStatus.Downloading;
|
||||
var baseMapPath = Platform.ResolvePath("^", "maps", Game.ModData.Manifest.Mod.Id);
|
||||
|
||||
// Create the map directory if it doesn't exist
|
||||
if (!Directory.Exists(baseMapPath))
|
||||
Directory.CreateDirectory(baseMapPath);
|
||||
var mapInstallPackage = new Folder(Platform.ResolvePath("^", "maps", Game.ModData.Manifest.Mod.Id));
|
||||
|
||||
var modData = Game.ModData;
|
||||
new Thread(() =>
|
||||
@@ -282,7 +280,7 @@ namespace OpenRA
|
||||
// Request the filename from the server
|
||||
// Run in a worker thread to avoid network delays
|
||||
var mapUrl = Game.Settings.Game.MapRepository + Uid;
|
||||
var mapPath = string.Empty;
|
||||
var mapFilename = string.Empty;
|
||||
try
|
||||
{
|
||||
var request = WebRequest.Create(mapUrl);
|
||||
@@ -296,11 +294,11 @@ namespace OpenRA
|
||||
return;
|
||||
}
|
||||
|
||||
mapPath = System.IO.Path.Combine(baseMapPath, res.Headers["Content-Disposition"].Replace("attachment; filename = ", ""));
|
||||
mapFilename = res.Headers["Content-Disposition"].Replace("attachment; filename = ", "");
|
||||
}
|
||||
|
||||
Action<DownloadProgressChangedEventArgs> onDownloadProgress = i => { DownloadBytes = i.BytesReceived; DownloadPercentage = i.ProgressPercentage; };
|
||||
Action<AsyncCompletedEventArgs, bool> onDownloadComplete = (i, cancelled) =>
|
||||
Action<DownloadDataCompletedEventArgs, bool> onDownloadComplete = (i, cancelled) =>
|
||||
{
|
||||
download = null;
|
||||
|
||||
@@ -313,15 +311,16 @@ namespace OpenRA
|
||||
return;
|
||||
}
|
||||
|
||||
Log.Write("debug", "Downloaded map to '{0}'", mapPath);
|
||||
mapInstallPackage.Update(mapFilename, i.Result);
|
||||
Log.Write("debug", "Downloaded map to '{0}'", mapFilename);
|
||||
Game.RunAfterTick(() =>
|
||||
{
|
||||
using (var package = modData.ModFiles.OpenPackage(mapPath))
|
||||
UpdateFromMap(package, MapClassification.User, null, GridType);
|
||||
var package = modData.ModFiles.OpenPackage(mapFilename, mapInstallPackage);
|
||||
UpdateFromMap(package, mapInstallPackage, MapClassification.User, null, GridType);
|
||||
});
|
||||
};
|
||||
|
||||
download = new Download(mapUrl, mapPath, onDownloadProgress, onDownloadComplete);
|
||||
download = new Download(mapUrl, onDownloadProgress, onDownloadComplete);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -353,5 +352,13 @@ namespace OpenRA
|
||||
Package = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
Invalidate();
|
||||
var deleteFromPackage = parentPackage as IReadWritePackage;
|
||||
if (deleteFromPackage != null)
|
||||
deleteFromPackage.Delete(Package.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user