AvatarWindow.cs 10.0 KB

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