HipcManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Ryujinx.Horizon.Common;
  2. using Ryujinx.Horizon.Sdk.Sf.Cmif;
  3. using System;
  4. namespace Ryujinx.Horizon.Sdk.Sf.Hipc
  5. {
  6. partial class HipcManager : IServiceObject
  7. {
  8. private readonly ServerDomainSessionManager _manager;
  9. private readonly ServerSession _session;
  10. public HipcManager(ServerDomainSessionManager manager, ServerSession session)
  11. {
  12. _manager = manager;
  13. _session = session;
  14. }
  15. [CmifCommand(0)]
  16. public Result ConvertCurrentObjectToDomain(out int objectId)
  17. {
  18. objectId = 0;
  19. var domain = _manager.Domain.AllocateDomainServiceObject();
  20. if (domain == null)
  21. {
  22. return HipcResult.OutOfDomains;
  23. }
  24. bool succeeded = false;
  25. try
  26. {
  27. Span<int> objectIds = stackalloc int[1];
  28. Result result = domain.ReserveIds(objectIds);
  29. if (result.IsFailure)
  30. {
  31. return result;
  32. }
  33. objectId = objectIds[0];
  34. succeeded = true;
  35. }
  36. finally
  37. {
  38. if (!succeeded)
  39. {
  40. ServerDomainManager.DestroyDomainServiceObject(domain);
  41. }
  42. }
  43. domain.RegisterObject(objectId, _session.ServiceObjectHolder);
  44. _session.ServiceObjectHolder = new ServiceObjectHolder(domain);
  45. return Result.Success;
  46. }
  47. [CmifCommand(1)]
  48. public Result CopyFromCurrentDomain([MoveHandle] out int clientHandle, int objectId)
  49. {
  50. clientHandle = 0;
  51. if (_session.ServiceObjectHolder.ServiceObject is not DomainServiceObject domain)
  52. {
  53. return HipcResult.TargetNotDomain;
  54. }
  55. var obj = domain.GetObject(objectId);
  56. if (obj == null)
  57. {
  58. return HipcResult.DomainObjectNotFound;
  59. }
  60. Api.CreateSession(out int serverHandle, out clientHandle).AbortOnFailure();
  61. _manager.RegisterSession(serverHandle, obj).AbortOnFailure();
  62. return Result.Success;
  63. }
  64. [CmifCommand(2)]
  65. public Result CloneCurrentObject([MoveHandle] out int clientHandle)
  66. {
  67. return CloneCurrentObjectImpl(out clientHandle, _manager);
  68. }
  69. [CmifCommand(3)]
  70. public void QueryPointerBufferSize(out ushort size)
  71. {
  72. size = (ushort)_session.PointerBuffer.Size;
  73. }
  74. [CmifCommand(4)]
  75. public Result CloneCurrentObjectEx([MoveHandle] out int clientHandle, uint tag)
  76. {
  77. return CloneCurrentObjectImpl(out clientHandle, _manager.GetSessionManagerByTag(tag));
  78. }
  79. private Result CloneCurrentObjectImpl(out int clientHandle, ServerSessionManager manager)
  80. {
  81. clientHandle = 0;
  82. var clone = _session.ServiceObjectHolder.Clone();
  83. if (clone == null)
  84. {
  85. return HipcResult.DomainObjectNotFound;
  86. }
  87. Api.CreateSession(out int serverHandle, out clientHandle).AbortOnFailure();
  88. manager.RegisterSession(serverHandle, clone).AbortOnFailure();
  89. return Result.Success;
  90. }
  91. }
  92. }