BrowserApplet.cs 3.4 KB

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