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
{
enum MapFileType { Unpacked, OraMap }
struct MapFileTypeInfo
{
public string Extension;
public string UiLabel;
}
[ObjectCreator.UseCtor]
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);
}
var attr = File.GetAttributes(map.Path);
var mapIsUnpacked = attr.HasFlag(FileAttributes.Directory);
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" },
{ "(unpacked)", "" }
{ MapFileType.OraMap, new MapFileTypeInfo { Extension = ".oramap", UiLabel = ".oramap" } },
{ MapFileType.Unpacked, new MapFileTypeInfo { Extension = "", UiLabel = "(unpacked)" } }
};
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,
() => typeDropdown.Text == option,
() => typeDropdown.Text = option);
item.Get<LabelWidget>("LABEL").GetText = () => option;
() => fileType == option.Key,
() => { typeDropdown.Text = option.Value.UiLabel; fileType = option.Key; });
item.Get<LabelWidget>("LABEL").GetText = () => option.Value.UiLabel;
return item;
};
typeDropdown.Text = map.Path != null ? Path.GetExtension(map.Path) : ".oramap";
if (string.IsNullOrEmpty(typeDropdown.Text))
typeDropdown.Text = fileTypes.First(t => t.Value == "").Key;
typeDropdown.Text = fileTypes[fileType].UiLabel;
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");
@@ -138,7 +147,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
// Create the map directory if required
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
if (map.Uid != null && combinedPath == map.Path)