Наследование: IReadWritePackage
Пример #1
0
 public SaveDirectory(Folder folder, MapClassification classification)
 {
     Folder = folder;
     DisplayName = Platform.UnresolvePath(Folder.Name);
     Classification = classification;
 }
Пример #2
0
        public SaveMapLogic(Widget widget, ModData modData, Action<string> onSave, Action onExit,
			Map map, List<MiniYamlNode> playerDefinitions, List<MiniYamlNode> actorDefinitions)
        {
            var title = widget.Get<TextFieldWidget>("TITLE");
            title.Text = map.Title;

            var author = widget.Get<TextFieldWidget>("AUTHOR");
            author.Text = map.Author;

            var visibilityPanel = Ui.LoadWidget("MAP_SAVE_VISIBILITY_PANEL", null, new WidgetArgs());
            var visOptionTemplate = visibilityPanel.Get<CheckboxWidget>("VISIBILITY_TEMPLATE");
            visibilityPanel.RemoveChildren();

            foreach (MapVisibility visibilityOption in Enum.GetValues(typeof(MapVisibility)))
            {
                // To prevent users from breaking the game only show the 'Shellmap' option when it is already set.
                if (visibilityOption == MapVisibility.Shellmap && !map.Visibility.HasFlag(visibilityOption))
                    continue;

                var checkbox = (CheckboxWidget)visOptionTemplate.Clone();
                checkbox.GetText = () => visibilityOption.ToString();
                checkbox.IsChecked = () => map.Visibility.HasFlag(visibilityOption);
                checkbox.OnClick = () => map.Visibility ^= visibilityOption;
                visibilityPanel.AddChild(checkbox);
            }

            var visibilityDropdown = widget.Get<DropDownButtonWidget>("VISIBILITY_DROPDOWN");
            visibilityDropdown.OnMouseDown = _ =>
            {
                visibilityDropdown.RemovePanel();
                visibilityDropdown.AttachPanel(visibilityPanel);
            };

            var writableDirectories = new List<SaveDirectory>();
            SaveDirectory selectedDirectory = null;

            var directoryDropdown = widget.Get<DropDownButtonWidget>("DIRECTORY_DROPDOWN");
            {
                Func<SaveDirectory, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
                {
                    var item = ScrollItemWidget.Setup(template,
                        () => selectedDirectory == option,
                        () => selectedDirectory = option);
                    item.Get<LabelWidget>("LABEL").GetText = () => option.DisplayName;
                    return item;
                };

                foreach (var kv in modData.MapCache.MapLocations)
                {
                    var folder = kv.Key as Folder;
                    if (folder == null)
                        continue;

                    try
                    {
                        using (var fs = File.Create(Path.Combine(folder.Name, ".testwritable"), 1, FileOptions.DeleteOnClose))
                        {
                            // Do nothing: we just want to test whether we can create the file
                        }

                        writableDirectories.Add(new SaveDirectory(folder, kv.Value));
                    }
                    catch
                    {
                        // Directory is not writable
                    }
                }

                if (map.Package != null)
                    selectedDirectory = writableDirectories.FirstOrDefault(k => k.Folder.Contains(map.Package.Name));

                // Prioritize MapClassification.User directories over system directories
                if (selectedDirectory == null)
                    selectedDirectory = writableDirectories.OrderByDescending(kv => kv.Classification).First();

                directoryDropdown.GetText = () => selectedDirectory == null ? "" : selectedDirectory.DisplayName;
                directoryDropdown.OnClick = () =>
                    directoryDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, writableDirectories, setupItem);
            }

            var mapIsUnpacked = map.Package != null && (map.Package is Folder || map.Package is ZipFolder);

            var filename = widget.Get<TextFieldWidget>("FILENAME");
            filename.Text = map.Package == null ? "" : mapIsUnpacked ? Path.GetFileName(map.Package.Name) : Path.GetFileNameWithoutExtension(map.Package.Name);
            var fileType = mapIsUnpacked ? MapFileType.Unpacked : MapFileType.OraMap;

            var fileTypes = new Dictionary<MapFileType, MapFileTypeInfo>()
            {
                { MapFileType.OraMap, new MapFileTypeInfo { Extension = ".oramap", UiLabel = ".oramap" } },
                { MapFileType.Unpacked, new MapFileTypeInfo { Extension = "", UiLabel = "(unpacked)" } }
            };

            var typeDropdown = widget.Get<DropDownButtonWidget>("TYPE_DROPDOWN");
            {
                Func<KeyValuePair<MapFileType, MapFileTypeInfo>, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
                {
                    var item = ScrollItemWidget.Setup(template,
                        () => fileType == option.Key,
                        () => { typeDropdown.Text = option.Value.UiLabel; fileType = option.Key; });
                    item.Get<LabelWidget>("LABEL").GetText = () => option.Value.UiLabel;
                    return item;
                };

                typeDropdown.Text = fileTypes[fileType].UiLabel;

                typeDropdown.OnClick = () =>
                    typeDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, fileTypes, setupItem);
            }

            var close = widget.Get<ButtonWidget>("BACK_BUTTON");
            close.OnClick = () => { Ui.CloseWindow(); onExit(); };

            var save = widget.Get<ButtonWidget>("SAVE_BUTTON");
            save.OnClick = () =>
            {
                if (string.IsNullOrEmpty(filename.Text))
                    return;

                map.Title = title.Text;
                map.Author = author.Text;

                if (actorDefinitions != null)
                    map.ActorDefinitions = actorDefinitions;

                if (playerDefinitions != null)
                    map.PlayerDefinitions = playerDefinitions;

                map.RequiresMod = modData.Manifest.Id;

                var combinedPath = Platform.ResolvePath(Path.Combine(selectedDirectory.Folder.Name, filename.Text + fileTypes[fileType].Extension));

                // Invalidate the old map metadata
                if (map.Uid != null && map.Package != null && map.Package.Name == combinedPath)
                    modData.MapCache[map.Uid].Invalidate();

                try
                {
                    var package = map.Package as IReadWritePackage;
                    if (package == null || package.Name != combinedPath)
                    {
                        selectedDirectory.Folder.Delete(combinedPath);
                        if (fileType == MapFileType.OraMap)
                            package = ZipFile.Create(combinedPath, selectedDirectory.Folder);
                        else
                            package = new Folder(combinedPath);
                    }

                    map.Save(package);
                }
                catch
                {
                    Console.WriteLine("Failed to save map at {0}", combinedPath);
                }

                // Update the map cache so it can be loaded without restarting the game
                modData.MapCache[map.Uid].UpdateFromMap(map.Package, selectedDirectory.Folder, selectedDirectory.Classification, null, map.Grid.Type);

                Console.WriteLine("Saved current map at {0}", combinedPath);
                Ui.CloseWindow();

                onSave(map.Uid);
            };
        }
Пример #3
0
        static Dictionary<string, ModMetadata> ValidateMods()
        {
            var ret = new Dictionary<string, ModMetadata>();
            foreach (var pair in GetCandidateMods())
            {
                IReadOnlyPackage package = null;
                try
                {
                    if (Directory.Exists(pair.Second))
                        package = new Folder(pair.Second);
                    else
                    {
                        try
                        {
                            package = new ZipFile(null, pair.Second);
                        }
                        catch
                        {
                            throw new InvalidDataException(pair.Second + " is not a valid mod package");
                        }
                    }

                    if (!package.Contains("mod.yaml"))
                    {
                        package.Dispose();
                        continue;
                    }

                    var yaml = new MiniYaml(null, MiniYaml.FromStream(package.GetStream("mod.yaml")));
                    var nd = yaml.ToDictionary();
                    if (!nd.ContainsKey("Metadata"))
                    {
                        package.Dispose();
                        continue;
                    }

                    var metadata = FieldLoader.Load<ModMetadata>(nd["Metadata"]);
                    metadata.Id = pair.First;
                    metadata.Package = package;

                    if (nd.ContainsKey("RequiresMods"))
                        metadata.RequiresMods = nd["RequiresMods"].ToDictionary(my => my.Value);
                    else
                        metadata.RequiresMods = new Dictionary<string, string>();

                    if (nd.ContainsKey("ContentInstaller"))
                        metadata.Content = FieldLoader.Load<ContentInstaller>(nd["ContentInstaller"]);

                    // Mods in the support directory and oramod packages (which are listed later
                    // in the CandidateMods list) override mods in the main install.
                    ret[pair.First] = metadata;
                }
                catch (Exception ex)
                {
                    if (package != null)
                        package.Dispose();
                    Console.WriteLine("An exception occurred when trying to load ModMetadata for `{0}`:".F(pair.First));
                    Console.WriteLine(ex.Message);
                }
            }

            return ret;
        }