AvatarWindow.cs 9.9 KB

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