2020-06-23 00:32:07 +00:00
|
|
|
using Gtk;
|
|
|
|
using LibHac.Common;
|
|
|
|
using LibHac.Fs;
|
2020-09-01 20:08:59 +00:00
|
|
|
using LibHac.Fs.Fsa;
|
2020-06-23 00:32:07 +00:00
|
|
|
using LibHac.FsSystem;
|
2022-01-12 11:22:19 +00:00
|
|
|
using LibHac.Tools.Fs;
|
|
|
|
using LibHac.Tools.FsSystem;
|
|
|
|
using LibHac.Tools.FsSystem.NcaUtils;
|
2020-06-23 00:32:07 +00:00
|
|
|
using Ryujinx.Common.Configuration;
|
|
|
|
using Ryujinx.HLE.FileSystem;
|
2021-01-08 08:14:13 +00:00
|
|
|
using Ryujinx.Ui.Widgets;
|
2020-06-23 00:32:07 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
using GUI = Gtk.Builder.ObjectAttribute;
|
|
|
|
using JsonHelper = Ryujinx.Common.Utilities.JsonHelper;
|
|
|
|
|
2021-01-08 08:14:13 +00:00
|
|
|
namespace Ryujinx.Ui.Windows
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
|
|
|
public class DlcWindow : Window
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
private readonly VirtualFileSystem _virtualFileSystem;
|
|
|
|
private readonly string _titleId;
|
|
|
|
private readonly string _dlcJsonPath;
|
|
|
|
private readonly List<DownloadableContentContainer> _dlcContainerList;
|
2020-06-23 00:32:07 +00:00
|
|
|
|
|
|
|
#pragma warning disable CS0649, IDE0044
|
|
|
|
[GUI] Label _baseTitleInfoLabel;
|
|
|
|
[GUI] TreeView _dlcTreeView;
|
|
|
|
[GUI] TreeSelection _dlcTreeSelection;
|
|
|
|
#pragma warning restore CS0649, IDE0044
|
|
|
|
|
2021-01-08 08:14:13 +00:00
|
|
|
public DlcWindow(VirtualFileSystem virtualFileSystem, string titleId, string titleName) : this(new Builder("Ryujinx.Ui.Windows.DlcWindow.glade"), virtualFileSystem, titleId, titleName) { }
|
2020-06-23 00:32:07 +00:00
|
|
|
|
2022-12-06 22:00:25 +00:00
|
|
|
private DlcWindow(Builder builder, VirtualFileSystem virtualFileSystem, string titleId, string titleName) : base(builder.GetRawOwnedObject("_dlcWindow"))
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
|
|
|
builder.Autoconnect(this);
|
|
|
|
|
|
|
|
_titleId = titleId;
|
|
|
|
_virtualFileSystem = virtualFileSystem;
|
2020-08-30 16:51:53 +00:00
|
|
|
_dlcJsonPath = System.IO.Path.Combine(AppDataManager.GamesDirPath, _titleId, "dlc.json");
|
2020-06-23 00:32:07 +00:00
|
|
|
_baseTitleInfoLabel.Text = $"DLC Available for {titleName} [{titleId.ToUpper()}]";
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
_dlcContainerList = JsonHelper.DeserializeFromFile<List<DownloadableContentContainer>>(_dlcJsonPath);
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
_dlcContainerList = new List<DownloadableContentContainer>();
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 08:14:13 +00:00
|
|
|
_dlcTreeView.Model = new TreeStore(typeof(bool), typeof(string), typeof(string));
|
2020-06-23 00:32:07 +00:00
|
|
|
|
|
|
|
CellRendererToggle enableToggle = new CellRendererToggle();
|
|
|
|
enableToggle.Toggled += (sender, args) =>
|
|
|
|
{
|
|
|
|
_dlcTreeView.Model.GetIter(out TreeIter treeIter, new TreePath(args.Path));
|
|
|
|
bool newValue = !(bool)_dlcTreeView.Model.GetValue(treeIter, 0);
|
|
|
|
_dlcTreeView.Model.SetValue(treeIter, 0, newValue);
|
|
|
|
|
|
|
|
if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, treeIter))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
_dlcTreeView.Model.SetValue(childIter, 0, newValue);
|
|
|
|
}
|
|
|
|
while (_dlcTreeView.Model.IterNext(ref childIter));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_dlcTreeView.AppendColumn("Enabled", enableToggle, "active", 0);
|
|
|
|
_dlcTreeView.AppendColumn("TitleId", new CellRendererText(), "text", 1);
|
|
|
|
_dlcTreeView.AppendColumn("Path", new CellRendererText(), "text", 2);
|
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
foreach (DownloadableContentContainer dlcContainer in _dlcContainerList)
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
if (File.Exists(dlcContainer.ContainerPath))
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2021-11-29 18:21:27 +00:00
|
|
|
// The parent tree item has its own "enabled" check box, but it's the actual
|
|
|
|
// nca entries that store the enabled / disabled state. A bit of a UI inconsistency.
|
|
|
|
// Maybe a tri-state check box would be better, but for now we check the parent
|
|
|
|
// "enabled" box if all child NCAs are enabled. Usually fine since each nsp has only one nca.
|
2022-07-28 22:41:34 +00:00
|
|
|
bool areAllContentPacksEnabled = dlcContainer.DownloadableContentNcaList.TrueForAll((nca) => nca.Enabled);
|
|
|
|
TreeIter parentIter = ((TreeStore)_dlcTreeView.Model).AppendValues(areAllContentPacksEnabled, "", dlcContainer.ContainerPath);
|
|
|
|
using FileStream containerFile = File.OpenRead(dlcContainer.ContainerPath);
|
2021-11-29 18:21:27 +00:00
|
|
|
PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
|
|
|
|
_virtualFileSystem.ImportTickets(pfs);
|
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
foreach (DownloadableContentNca dlcNca in dlcContainer.DownloadableContentNcaList)
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2021-12-23 16:55:50 +00:00
|
|
|
using var ncaFile = new UniqueRef<IFile>();
|
|
|
|
|
2022-07-28 22:41:34 +00:00
|
|
|
pfs.OpenFile(ref ncaFile.Ref(), dlcNca.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
|
|
|
Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), dlcContainer.ContainerPath);
|
2021-11-29 18:21:27 +00:00
|
|
|
|
|
|
|
if (nca != null)
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
((TreeStore)_dlcTreeView.Model).AppendValues(parentIter, dlcNca.Enabled, nca.Header.TitleId.ToString("X16"), dlcNca.FullPath);
|
2021-11-29 18:21:27 +00:00
|
|
|
}
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-29 18:21:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// DLC file moved or renamed. Allow the user to remove it without crashing the whole dialog.
|
2022-07-28 22:41:34 +00:00
|
|
|
TreeIter parentIter = ((TreeStore)_dlcTreeView.Model).AppendValues(false, "", $"(MISSING) {dlcContainer.ContainerPath}");
|
2021-11-29 18:21:27 +00:00
|
|
|
}
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Nca TryCreateNca(IStorage ncaStorage, string containerPath)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return new Nca(_virtualFileSystem.KeySet, ncaStorage);
|
|
|
|
}
|
2021-01-08 08:14:13 +00:00
|
|
|
catch (Exception exception)
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2021-01-08 08:14:13 +00:00
|
|
|
GtkDialog.CreateErrorDialog($"{exception.Message}. Errored File: {containerPath}");
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddButton_Clicked(object sender, EventArgs args)
|
|
|
|
{
|
2021-09-14 21:52:08 +00:00
|
|
|
FileChooserNative fileChooser = new FileChooserNative("Select DLC files", this, FileChooserAction.Open, "Add", "Cancel")
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2021-09-14 21:52:08 +00:00
|
|
|
SelectMultiple = true
|
2020-06-23 00:32:07 +00:00
|
|
|
};
|
2021-09-14 21:52:08 +00:00
|
|
|
|
|
|
|
FileFilter filter = new FileFilter()
|
|
|
|
{
|
|
|
|
Name = "Switch Game DLCs"
|
|
|
|
};
|
|
|
|
filter.AddPattern("*.nsp");
|
|
|
|
|
|
|
|
fileChooser.AddFilter(filter);
|
2020-06-23 00:32:07 +00:00
|
|
|
|
|
|
|
if (fileChooser.Run() == (int)ResponseType.Accept)
|
|
|
|
{
|
|
|
|
foreach (string containerPath in fileChooser.Filenames)
|
|
|
|
{
|
|
|
|
if (!File.Exists(containerPath))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
using (FileStream containerFile = File.OpenRead(containerPath))
|
|
|
|
{
|
|
|
|
PartitionFileSystem pfs = new PartitionFileSystem(containerFile.AsStorage());
|
|
|
|
bool containsDlc = false;
|
|
|
|
|
|
|
|
_virtualFileSystem.ImportTickets(pfs);
|
|
|
|
|
|
|
|
TreeIter? parentIter = null;
|
|
|
|
|
|
|
|
foreach (DirectoryEntryEx fileEntry in pfs.EnumerateEntries("/", "*.nca"))
|
|
|
|
{
|
2021-12-23 16:55:50 +00:00
|
|
|
using var ncaFile = new UniqueRef<IFile>();
|
|
|
|
|
|
|
|
pfs.OpenFile(ref ncaFile.Ref(), fileEntry.FullPath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
2020-06-23 00:32:07 +00:00
|
|
|
|
2021-12-23 16:55:50 +00:00
|
|
|
Nca nca = TryCreateNca(ncaFile.Get.AsStorage(), containerPath);
|
2020-06-23 00:32:07 +00:00
|
|
|
|
|
|
|
if (nca == null) continue;
|
|
|
|
|
|
|
|
if (nca.Header.ContentType == NcaContentType.PublicData)
|
|
|
|
{
|
|
|
|
if ((nca.Header.TitleId & 0xFFFFFFFFFFFFE000).ToString("x16") != _titleId)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentIter ??= ((TreeStore)_dlcTreeView.Model).AppendValues(true, "", containerPath);
|
|
|
|
|
|
|
|
((TreeStore)_dlcTreeView.Model).AppendValues(parentIter.Value, true, nca.Header.TitleId.ToString("X16"), fileEntry.FullPath);
|
|
|
|
containsDlc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!containsDlc)
|
|
|
|
{
|
2021-03-18 22:44:39 +00:00
|
|
|
GtkDialog.CreateErrorDialog("The specified file does not contain DLC for the selected title!");
|
2020-06-23 00:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fileChooser.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoveButton_Clicked(object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
if (_dlcTreeSelection.GetSelected(out ITreeModel treeModel, out TreeIter treeIter))
|
|
|
|
{
|
|
|
|
if (_dlcTreeView.Model.IterParent(out TreeIter parentIter, treeIter) && _dlcTreeView.Model.IterNChildren(parentIter) <= 1)
|
|
|
|
{
|
|
|
|
((TreeStore)treeModel).Remove(ref parentIter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
((TreeStore)treeModel).Remove(ref treeIter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 22:51:49 +00:00
|
|
|
|
|
|
|
private void RemoveAllButton_Clicked(object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
List<TreeIter> toRemove = new List<TreeIter>();
|
|
|
|
|
|
|
|
if (_dlcTreeView.Model.GetIterFirst(out TreeIter iter))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
toRemove.Add(iter);
|
|
|
|
}
|
|
|
|
while (_dlcTreeView.Model.IterNext(ref iter));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (TreeIter i in toRemove)
|
|
|
|
{
|
|
|
|
TreeIter j = i;
|
|
|
|
((TreeStore)_dlcTreeView.Model).Remove(ref j);
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 00:32:07 +00:00
|
|
|
|
|
|
|
private void SaveButton_Clicked(object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
_dlcContainerList.Clear();
|
|
|
|
|
|
|
|
if (_dlcTreeView.Model.GetIterFirst(out TreeIter parentIter))
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (_dlcTreeView.Model.IterChildren(out TreeIter childIter, parentIter))
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
DownloadableContentContainer dlcContainer = new DownloadableContentContainer
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
ContainerPath = (string)_dlcTreeView.Model.GetValue(parentIter, 2),
|
|
|
|
DownloadableContentNcaList = new List<DownloadableContentNca>()
|
2020-06-23 00:32:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
dlcContainer.DownloadableContentNcaList.Add(new DownloadableContentNca
|
2020-06-23 00:32:07 +00:00
|
|
|
{
|
2022-07-28 22:41:34 +00:00
|
|
|
Enabled = (bool)_dlcTreeView.Model.GetValue(childIter, 0),
|
|
|
|
TitleId = Convert.ToUInt64(_dlcTreeView.Model.GetValue(childIter, 1).ToString(), 16),
|
|
|
|
FullPath = (string)_dlcTreeView.Model.GetValue(childIter, 2)
|
2020-06-23 00:32:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
while (_dlcTreeView.Model.IterNext(ref childIter));
|
|
|
|
|
|
|
|
_dlcContainerList.Add(dlcContainer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (_dlcTreeView.Model.IterNext(ref parentIter));
|
|
|
|
}
|
|
|
|
|
|
|
|
using (FileStream dlcJsonStream = File.Create(_dlcJsonPath, 4096, FileOptions.WriteThrough))
|
|
|
|
{
|
|
|
|
dlcJsonStream.Write(Encoding.UTF8.GetBytes(JsonHelper.Serialize(_dlcContainerList, true)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void CancelButton_Clicked(object sender, EventArgs args)
|
|
|
|
{
|
|
|
|
Dispose();
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 22:44:39 +00:00
|
|
|
}
|