Allow only valid file types when saving maps in the editor

It will also allow saving to files and directories which have a dot in their name.
This commit is contained in:
Oliver Brakmann
2015-12-30 19:12:02 +01:00
parent d2b75d780d
commit 83ee267105

View File

@@ -19,6 +19,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
public class SaveMapLogic : ChromeLogic public class SaveMapLogic : ChromeLogic
{ {
enum MapFileType { Unpacked, OraMap }
struct MapFileTypeInfo
{
public string Extension;
public string UiLabel;
}
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public SaveMapLogic(Widget widget, Action<string> onSave, Action onExit, Map map, List<MiniYamlNode> playerDefinitions, List<MiniYamlNode> actorDefinitions) public SaveMapLogic(Widget widget, Action<string> onSave, Action onExit, Map map, List<MiniYamlNode> playerDefinitions, List<MiniYamlNode> actorDefinitions)
{ {
@@ -85,32 +93,33 @@ namespace OpenRA.Mods.Common.Widgets.Logic
directoryDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapDirectories.Keys, setupItem); directoryDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapDirectories.Keys, setupItem);
} }
var attr = File.GetAttributes(map.Path);
var mapIsUnpacked = attr.HasFlag(FileAttributes.Directory);
var filename = widget.Get<TextFieldWidget>("FILENAME"); var filename = widget.Get<TextFieldWidget>("FILENAME");
filename.Text = Path.GetFileNameWithoutExtension(map.Path); filename.Text = mapIsUnpacked ? Path.GetFileName(map.Path) : Path.GetFileNameWithoutExtension(map.Path);
var fileType = mapIsUnpacked ? MapFileType.Unpacked : MapFileType.OraMap;
var fileTypes = new Dictionary<string, string>() var fileTypes = new Dictionary<MapFileType, MapFileTypeInfo>()
{ {
{ ".oramap", ".oramap" }, { MapFileType.OraMap, new MapFileTypeInfo { Extension = ".oramap", UiLabel = ".oramap" } },
{ "(unpacked)", "" } { MapFileType.Unpacked, new MapFileTypeInfo { Extension = "", UiLabel = "(unpacked)" } }
}; };
var typeDropdown = widget.Get<DropDownButtonWidget>("TYPE_DROPDOWN"); var typeDropdown = widget.Get<DropDownButtonWidget>("TYPE_DROPDOWN");
{ {
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) => Func<KeyValuePair<MapFileType, MapFileTypeInfo>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
{ {
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
() => typeDropdown.Text == option, () => fileType == option.Key,
() => typeDropdown.Text = option); () => { typeDropdown.Text = option.Value.UiLabel; fileType = option.Key; });
item.Get<LabelWidget>("LABEL").GetText = () => option; item.Get<LabelWidget>("LABEL").GetText = () => option.Value.UiLabel;
return item; return item;
}; };
typeDropdown.Text = map.Path != null ? Path.GetExtension(map.Path) : ".oramap"; typeDropdown.Text = fileTypes[fileType].UiLabel;
if (string.IsNullOrEmpty(typeDropdown.Text))
typeDropdown.Text = fileTypes.First(t => t.Value == "").Key;
typeDropdown.OnClick = () => typeDropdown.OnClick = () =>
typeDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, fileTypes.Keys, setupItem); typeDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, fileTypes, setupItem);
} }
var close = widget.Get<ButtonWidget>("BACK_BUTTON"); var close = widget.Get<ButtonWidget>("BACK_BUTTON");
@@ -138,7 +147,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Create the map directory if required // Create the map directory if required
Directory.CreateDirectory(Platform.ResolvePath(directoryDropdown.Text)); Directory.CreateDirectory(Platform.ResolvePath(directoryDropdown.Text));
var combinedPath = Platform.ResolvePath(Path.Combine(directoryDropdown.Text, filename.Text + fileTypes[typeDropdown.Text])); var combinedPath = Platform.ResolvePath(Path.Combine(directoryDropdown.Text, filename.Text + fileTypes[fileType].Extension));
// Invalidate the old map metadata // Invalidate the old map metadata
if (map.Uid != null && combinedPath == map.Path) if (map.Uid != null && combinedPath == map.Path)