AvatarWindow.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using Gtk;
  2. using LibHac.Common;
  3. using LibHac.Fs;
  4. using LibHac.Fs.Fsa;
  5. using LibHac.FsSystem;
  6. using LibHac.Ncm;
  7. using LibHac.Tools.FsSystem;
  8. using LibHac.Tools.FsSystem.NcaUtils;
  9. using Ryujinx.Common.Memory;
  10. using Ryujinx.HLE.FileSystem;
  11. using Ryujinx.Ui.Common.Configuration;
  12. using SixLabors.ImageSharp;
  13. using SixLabors.ImageSharp.Formats.Png;
  14. using SixLabors.ImageSharp.PixelFormats;
  15. using SixLabors.ImageSharp.Processing;
  16. using System;
  17. using System.Buffers.Binary;
  18. using System.Collections.Generic;
  19. using System.IO;
  20. using System.Reflection;
  21. using Image = SixLabors.ImageSharp.Image;
  22. namespace Ryujinx.Ui.Windows
  23. {
  24. public class AvatarWindow : Window
  25. {
  26. public byte[] SelectedProfileImage;
  27. public bool NewUser;
  28. private static Dictionary<string, byte[]> _avatarDict = new Dictionary<string, byte[]>();
  29. private ListStore _listStore;
  30. private IconView _iconView;
  31. private Button _setBackgroungColorButton;
  32. private Gdk.RGBA _backgroundColor;
  33. public AvatarWindow() : base($"Ryujinx {Program.Version} - Manage Accounts - Avatar")
  34. {
  35. Icon = new Gdk.Pixbuf(Assembly.GetAssembly(typeof(ConfigurationState)), "Ryujinx.Ui.Common.Resources.Logo_Ryujinx.png");
  36. CanFocus = false;
  37. Resizable = false;
  38. Modal = true;
  39. TypeHint = Gdk.WindowTypeHint.Dialog;
  40. SetDefaultSize(740, 400);
  41. SetPosition(WindowPosition.Center);
  42. Box vbox = new(Orientation.Vertical, 0);
  43. Add(vbox);
  44. ScrolledWindow scrolledWindow = new ScrolledWindow
  45. {
  46. ShadowType = ShadowType.EtchedIn
  47. };
  48. scrolledWindow.SetPolicy(PolicyType.Automatic, PolicyType.Automatic);
  49. Box hbox = new(Orientation.Horizontal, 0);
  50. Button chooseButton = new Button()
  51. {
  52. Label = "Choose",
  53. CanFocus = true,
  54. ReceivesDefault = true
  55. };
  56. chooseButton.Clicked += ChooseButton_Pressed;
  57. _setBackgroungColorButton = new Button()
  58. {
  59. Label = "Set Background Color",
  60. CanFocus = true
  61. };
  62. _setBackgroungColorButton.Clicked += SetBackgroungColorButton_Pressed;
  63. _backgroundColor.Red = 1;
  64. _backgroundColor.Green = 1;
  65. _backgroundColor.Blue = 1;
  66. _backgroundColor.Alpha = 1;
  67. Button closeButton = new Button()
  68. {
  69. Label = "Close",
  70. CanFocus = true
  71. };
  72. closeButton.Clicked += CloseButton_Pressed;
  73. vbox.PackStart(scrolledWindow, true, true, 0);
  74. hbox.PackStart(chooseButton, true, true, 0);
  75. hbox.PackStart(_setBackgroungColorButton, true, true, 0);
  76. hbox.PackStart(closeButton, true, true, 0);
  77. vbox.PackStart(hbox, false, false, 0);
  78. _listStore = new ListStore(typeof(string), typeof(Gdk.Pixbuf));
  79. _listStore.SetSortColumnId(0, SortType.Ascending);
  80. _iconView = new IconView(_listStore);
  81. _iconView.ItemWidth = 64;
  82. _iconView.ItemPadding = 10;
  83. _iconView.PixbufColumn = 1;
  84. _iconView.SelectionChanged += IconView_SelectionChanged;
  85. scrolledWindow.Add(_iconView);
  86. _iconView.GrabFocus();
  87. ProcessAvatars();
  88. ShowAll();
  89. }
  90. public static void PreloadAvatars(ContentManager contentManager, VirtualFileSystem virtualFileSystem)
  91. {
  92. if (_avatarDict.Count > 0)
  93. {
  94. return;
  95. }
  96. string contentPath = contentManager.GetInstalledContentPath(0x010000000000080A, StorageId.BuiltInSystem, NcaContentType.Data);
  97. string avatarPath = virtualFileSystem.SwitchPathToSystemPath(contentPath);
  98. if (!string.IsNullOrWhiteSpace(avatarPath))
  99. {
  100. using (IStorage ncaFileStream = new LocalStorage(avatarPath, FileAccess.Read, FileMode.Open))
  101. {
  102. Nca nca = new Nca(virtualFileSystem.KeySet, ncaFileStream);
  103. IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.ErrorOnInvalid);
  104. foreach (var item in romfs.EnumerateEntries())
  105. {
  106. // TODO: Parse DatabaseInfo.bin and table.bin files for more accuracy.
  107. if (item.Type == DirectoryEntryType.File && item.FullPath.Contains("chara") && item.FullPath.Contains("szs"))
  108. {
  109. using var file = new UniqueRef<IFile>();
  110. romfs.OpenFile(ref file.Ref, ("/" + item.FullPath).ToU8Span(), OpenMode.Read).ThrowIfFailure();
  111. using (MemoryStream stream = MemoryStreamManager.Shared.GetStream())
  112. using (MemoryStream streamPng = MemoryStreamManager.Shared.GetStream())
  113. {
  114. file.Get.AsStream().CopyTo(stream);
  115. stream.Position = 0;
  116. Image avatarImage = Image.LoadPixelData<Rgba32>(DecompressYaz0(stream), 256, 256);
  117. avatarImage.SaveAsPng(streamPng);
  118. _avatarDict.Add(item.FullPath, streamPng.ToArray());
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. private void ProcessAvatars()
  126. {
  127. _listStore.Clear();
  128. foreach (var avatar in _avatarDict)
  129. {
  130. _listStore.AppendValues(avatar.Key, new Gdk.Pixbuf(ProcessImage(avatar.Value), 96, 96));
  131. }
  132. _iconView.SelectPath(new TreePath(new int[] { 0 }));
  133. }
  134. private byte[] ProcessImage(byte[] data)
  135. {
  136. using (MemoryStream streamJpg = MemoryStreamManager.Shared.GetStream())
  137. {
  138. Image avatarImage = Image.Load(data, new PngDecoder());
  139. avatarImage.Mutate(x => x.BackgroundColor(new Rgba32((byte)(_backgroundColor.Red * 255),
  140. (byte)(_backgroundColor.Green * 255),
  141. (byte)(_backgroundColor.Blue * 255),
  142. (byte)(_backgroundColor.Alpha * 255))));
  143. avatarImage.SaveAsJpeg(streamJpg);
  144. return streamJpg.ToArray();
  145. }
  146. }
  147. private void CloseButton_Pressed(object sender, EventArgs e)
  148. {
  149. SelectedProfileImage = null;
  150. Close();
  151. }
  152. private void IconView_SelectionChanged(object sender, EventArgs e)
  153. {
  154. if (_iconView.SelectedItems.Length > 0)
  155. {
  156. _listStore.GetIter(out TreeIter iter, _iconView.SelectedItems[0]);
  157. SelectedProfileImage = ProcessImage(_avatarDict[(string)_listStore.GetValue(iter, 0)]);
  158. }
  159. }
  160. private void SetBackgroungColorButton_Pressed(object sender, EventArgs e)
  161. {
  162. using (ColorChooserDialog colorChooserDialog = new ColorChooserDialog("Set Background Color", this))
  163. {
  164. colorChooserDialog.UseAlpha = false;
  165. colorChooserDialog.Rgba = _backgroundColor;
  166. if (colorChooserDialog.Run() == (int)ResponseType.Ok)
  167. {
  168. _backgroundColor = colorChooserDialog.Rgba;
  169. ProcessAvatars();
  170. }
  171. colorChooserDialog.Hide();
  172. }
  173. }
  174. private void ChooseButton_Pressed(object sender, EventArgs e)
  175. {
  176. Close();
  177. }
  178. private static byte[] DecompressYaz0(Stream stream)
  179. {
  180. using (BinaryReader reader = new BinaryReader(stream))
  181. {
  182. reader.ReadInt32(); // Magic
  183. uint decodedLength = BinaryPrimitives.ReverseEndianness(reader.ReadUInt32());
  184. reader.ReadInt64(); // Padding
  185. byte[] input = new byte[stream.Length - stream.Position];
  186. stream.Read(input, 0, input.Length);
  187. long inputOffset = 0;
  188. byte[] output = new byte[decodedLength];
  189. long outputOffset = 0;
  190. ushort mask = 0;
  191. byte header = 0;
  192. while (outputOffset < decodedLength)
  193. {
  194. if ((mask >>= 1) == 0)
  195. {
  196. header = input[inputOffset++];
  197. mask = 0x80;
  198. }
  199. if ((header & mask) > 0)
  200. {
  201. if (outputOffset == output.Length)
  202. {
  203. break;
  204. }
  205. output[outputOffset++] = input[inputOffset++];
  206. }
  207. else
  208. {
  209. byte byte1 = input[inputOffset++];
  210. byte byte2 = input[inputOffset++];
  211. int dist = ((byte1 & 0xF) << 8) | byte2;
  212. int position = (int)outputOffset - (dist + 1);
  213. int length = byte1 >> 4;
  214. if (length == 0)
  215. {
  216. length = input[inputOffset++] + 0x12;
  217. }
  218. else
  219. {
  220. length += 2;
  221. }
  222. while (length-- > 0)
  223. {
  224. output[outputOffset++] = output[position++];
  225. }
  226. }
  227. }
  228. return output;
  229. }
  230. }
  231. }
  232. }