BrowserApplet.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.HLE.HOS.Services.Am.AppletAE;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Ryujinx.HLE.HOS.Applets.Browser
  8. {
  9. internal class BrowserApplet : IApplet
  10. {
  11. public event EventHandler AppletStateChanged;
  12. private AppletSession _normalSession;
  13. private AppletSession _interactiveSession;
  14. private CommonArguments _commonArguments;
  15. private List<BrowserArgument> _arguments;
  16. private ShimKind _shimKind;
  17. public BrowserApplet(Horizon system) {}
  18. public ResultCode GetResult()
  19. {
  20. return ResultCode.Success;
  21. }
  22. public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
  23. {
  24. _normalSession = normalSession;
  25. _interactiveSession = interactiveSession;
  26. _commonArguments = IApplet.ReadStruct<CommonArguments>(_normalSession.Pop());
  27. Logger.Stub?.PrintStub(LogClass.ServiceAm, $"WebApplet version: 0x{_commonArguments.AppletVersion:x8}");
  28. ReadOnlySpan<byte> webArguments = _normalSession.Pop();
  29. (_shimKind, _arguments) = BrowserArgument.ParseArguments(webArguments);
  30. Logger.Stub?.PrintStub(LogClass.ServiceAm, $"Web Arguments: {_arguments.Count}");
  31. foreach (BrowserArgument argument in _arguments)
  32. {
  33. Logger.Stub?.PrintStub(LogClass.ServiceAm, $"{argument.Type}: {argument.GetValue()}");
  34. }
  35. if ((_commonArguments.AppletVersion >= 0x80000 && _shimKind == ShimKind.Web) || (_commonArguments.AppletVersion >= 0x30000 && _shimKind == ShimKind.Share))
  36. {
  37. List<BrowserOutput> result = new List<BrowserOutput>();
  38. result.Add(new BrowserOutput(BrowserOutputType.ExitReason, (uint)WebExitReason.ExitButton));
  39. _normalSession.Push(BuildResponseNew(result));
  40. }
  41. else
  42. {
  43. WebCommonReturnValue result = new WebCommonReturnValue()
  44. {
  45. ExitReason = WebExitReason.ExitButton,
  46. };
  47. _normalSession.Push(BuildResponseOld(result));
  48. }
  49. AppletStateChanged?.Invoke(this, null);
  50. return ResultCode.Success;
  51. }
  52. private byte[] BuildResponseOld(WebCommonReturnValue result)
  53. {
  54. using (MemoryStream stream = new MemoryStream())
  55. using (BinaryWriter writer = new BinaryWriter(stream))
  56. {
  57. writer.WriteStruct(result);
  58. return stream.ToArray();
  59. }
  60. }
  61. private byte[] BuildResponseNew(List<BrowserOutput> outputArguments)
  62. {
  63. using (MemoryStream stream = new MemoryStream())
  64. using (BinaryWriter writer = new BinaryWriter(stream))
  65. {
  66. writer.WriteStruct(new WebArgHeader
  67. {
  68. Count = (ushort)outputArguments.Count,
  69. ShimKind = _shimKind
  70. });
  71. foreach (BrowserOutput output in outputArguments)
  72. {
  73. output.Write(writer);
  74. }
  75. writer.Write(new byte[0x2000 - writer.BaseStream.Position]);
  76. return stream.ToArray();
  77. }
  78. }
  79. }
  80. }