Merge pull request #8519 from pchote/editor-save-fix

Save maps with the correct MapClassification.
This commit is contained in:
Oliver Brakmann
2015-06-21 14:51:42 +02:00
3 changed files with 63 additions and 38 deletions

View File

@@ -68,26 +68,30 @@ namespace OpenRA.Mods.Common.Widgets.Logic
map.PlayerDefinitions = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length).ToMiniYaml(); map.PlayerDefinitions = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length).ToMiniYaml();
map.FixOpenAreas(modRules); map.FixOpenAreas(modRules);
var userMapFolder = Game.ModData.Manifest.MapFolders.First(f => f.Value == "User").Key; Action<string> afterSave = uid =>
{
// Ignore optional flag // HACK: Work around a synced-code change check.
if (userMapFolder.StartsWith("~")) // It's not clear why this is needed here, but not in the other places that load maps.
userMapFolder = userMapFolder.Substring(1); Game.RunAfterTick(() =>
{
var mapDir = Platform.ResolvePath(userMapFolder);
Directory.CreateDirectory(mapDir);
var tempLocation = Path.Combine(mapDir, "temp") + ".oramap";
map.Save(tempLocation); // TODO: load it right away and save later properly
var newMap = new Map(tempLocation);
Game.ModData.MapCache[newMap.Uid].UpdateFromMap(newMap, MapClassification.User);
ConnectionLogic.Connect(System.Net.IPAddress.Loopback.ToString(), ConnectionLogic.Connect(System.Net.IPAddress.Loopback.ToString(),
Game.CreateLocalServer(newMap.Uid), Game.CreateLocalServer(uid), "",
"", () => Game.LoadEditor(uid),
() => { Game.LoadEditor(newMap.Uid); },
() => { Game.CloseServer(); onExit(); }); () => { Game.CloseServer(); onExit(); });
onSelect(newMap.Uid); });
Ui.CloseWindow();
onSelect(uid);
};
Ui.OpenWindow("SAVE_MAP_PANEL", new WidgetArgs()
{
{ "onSave", afterSave },
{ "onExit", () => { Ui.CloseWindow(); onExit(); } },
{ "map", map },
{ "playerDefinitions", map.PlayerDefinitions },
{ "actorDefinitions", map.ActorDefinitions }
});
}; };
} }
} }

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
public class SaveMapLogic public class SaveMapLogic
{ {
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public SaveMapLogic(Widget widget, Action onExit, Map map, EditorActorLayer editorActorLayer) public SaveMapLogic(Widget widget, Action<string> onSave, Action onExit, Map map, List<MiniYamlNode> playerDefinitions, List<MiniYamlNode> actorDefinitions)
{ {
var title = widget.Get<TextFieldWidget>("TITLE"); var title = widget.Get<TextFieldWidget>("TITLE");
title.Text = map.Title; title.Text = map.Title;
@@ -50,17 +50,20 @@ namespace OpenRA.Mods.Common.Widgets.Logic
visibilityDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapVisibility, setupItem); visibilityDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapVisibility, setupItem);
} }
var directoryDropdown = widget.Get<DropDownButtonWidget>("DIRECTORY_DROPDOWN"); Func<string, string> makeMapDirectory = dir =>
{ {
var mapDirectories = Game.ModData.Manifest.MapFolders.Keys.Select(ff => var f = Platform.UnresolvePath(dir);
{
var f = Platform.UnresolvePath(ff);
if (f.StartsWith("~")) if (f.StartsWith("~"))
f = f.Substring(1); f = f.Substring(1);
return f; return f;
}).ToList(); };
var mapDirectories = Game.ModData.Manifest.MapFolders
.ToDictionary(kv => makeMapDirectory(kv.Key), kv => Enum<MapClassification>.Parse(kv.Value));
var directoryDropdown = widget.Get<DropDownButtonWidget>("DIRECTORY_DROPDOWN");
{
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) => Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{ {
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
@@ -70,15 +73,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return item; return item;
}; };
var mapDirectory = Platform.UnresolvePath(Path.GetDirectoryName(map.Path)); var mapDirectory = map.Path != null ? Platform.UnresolvePath(Path.GetDirectoryName(map.Path)) : null;
var initialDirectory = mapDirectories.FirstOrDefault(f => f == mapDirectory); var initialDirectory = mapDirectories.Keys.FirstOrDefault(f => f == mapDirectory);
if (initialDirectory == null) if (initialDirectory == null)
initialDirectory = mapDirectories.First(); initialDirectory = mapDirectories.Keys.First();
directoryDropdown.Text = initialDirectory; directoryDropdown.Text = initialDirectory;
directoryDropdown.OnClick = () => directoryDropdown.OnClick = () =>
directoryDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapDirectories, setupItem); directoryDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapDirectories.Keys, setupItem);
} }
var filename = widget.Get<TextFieldWidget>("FILENAME"); var filename = widget.Get<TextFieldWidget>("FILENAME");
@@ -101,7 +104,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
return item; return item;
}; };
typeDropdown.Text = Path.GetExtension(map.Path); typeDropdown.Text = map.Path != null ? Path.GetExtension(map.Path) : ".oramap";
if (string.IsNullOrEmpty(typeDropdown.Text)) if (string.IsNullOrEmpty(typeDropdown.Text))
typeDropdown.Text = fileTypes.First(t => t.Value == "").Key; typeDropdown.Text = fileTypes.First(t => t.Value == "").Key;
@@ -122,19 +125,34 @@ namespace OpenRA.Mods.Common.Widgets.Logic
map.Description = description.Text; map.Description = description.Text;
map.Author = author.Text; map.Author = author.Text;
map.Visibility = (MapVisibility)Enum.Parse(typeof(MapVisibility), visibilityDropdown.Text); map.Visibility = (MapVisibility)Enum.Parse(typeof(MapVisibility), visibilityDropdown.Text);
map.ActorDefinitions = editorActorLayer.Save();
map.PlayerDefinitions = editorActorLayer.Players.ToMiniYaml(); if (actorDefinitions != null)
map.ActorDefinitions = actorDefinitions;
if (playerDefinitions != null)
map.PlayerDefinitions = playerDefinitions;
map.RequiresMod = Game.ModData.Manifest.Mod.Id; map.RequiresMod = Game.ModData.Manifest.Mod.Id;
var combinedPath = Platform.ResolvePath(Path.Combine(directoryDropdown.Text, filename.Text + fileTypes[typeDropdown.Text])); var combinedPath = Platform.ResolvePath(Path.Combine(directoryDropdown.Text, filename.Text + fileTypes[typeDropdown.Text]));
// Invalidate the old map metadata
if (map.Uid != null && combinedPath == map.Path)
Game.ModData.MapCache[map.Uid].Invalidate();
map.Save(combinedPath); map.Save(combinedPath);
// Reload map to calculate new UID
map = new Map(combinedPath);
// Update the map cache so it can be loaded without restarting the game // Update the map cache so it can be loaded without restarting the game
Game.ModData.MapCache[map.Uid].UpdateFromMap(map, MapClassification.User); var classification = mapDirectories[directoryDropdown.Text];
Game.ModData.MapCache[map.Uid].UpdateFromMap(map, classification);
Console.WriteLine("Saved current map at {0}", combinedPath); Console.WriteLine("Saved current map at {0}", combinedPath);
Ui.CloseWindow(); Ui.CloseWindow();
onExit();
onSave(map.Uid);
}; };
} }
} }

View File

@@ -102,11 +102,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
saveMapButton.OnClick = () => saveMapButton.OnClick = () =>
{ {
hideMenu = true; hideMenu = true;
var editorActorLayer = world.WorldActor.Trait<EditorActorLayer>();
Ui.OpenWindow("SAVE_MAP_PANEL", new WidgetArgs() Ui.OpenWindow("SAVE_MAP_PANEL", new WidgetArgs()
{ {
{ "onSave", (Action<string>)(_ => hideMenu = false) },
{ "onExit", () => hideMenu = false }, { "onExit", () => hideMenu = false },
{ "map", world.Map }, { "map", world.Map },
{ "editorActorLayer", world.WorldActor.Trait<EditorActorLayer>() } { "playerDefinitions", editorActorLayer.Players.ToMiniYaml() },
{ "actorDefinitions", editorActorLayer.Save() }
}); });
}; };