ISystemSettingsServer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using ChocolArm64.Memory;
  2. using Ryujinx.Core.OsHle.Ipc;
  3. using Ryujinx.Core.Settings;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. namespace Ryujinx.Core.OsHle.Services.Set
  9. {
  10. class ISystemSettingsServer : IpcService
  11. {
  12. private Dictionary<int, ServiceProcessRequest> m_Commands;
  13. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
  14. public ISystemSettingsServer()
  15. {
  16. m_Commands = new Dictionary<int, ServiceProcessRequest>()
  17. {
  18. { 4, GetFirmwareVersion2 },
  19. { 23, GetColorSetId },
  20. { 24, SetColorSetId },
  21. { 38, GetSettingsItemValue }
  22. };
  23. }
  24. public static long GetFirmwareVersion2(ServiceCtx Context)
  25. {
  26. long ReplyPos = Context.Request.RecvListBuff[0].Position;
  27. long ReplySize = Context.Request.RecvListBuff[0].Size;
  28. byte MajorFWVersion = 0x03;
  29. byte MinorFWVersion = 0x00;
  30. byte MicroFWVersion = 0x00;
  31. byte Unknown = 0x00; //Build?
  32. int RevisionNumber = 0x0A;
  33. string Platform = "NX";
  34. string UnknownHex = "7fbde2b0bba4d14107bf836e4643043d9f6c8e47";
  35. string Version = "3.0.0";
  36. string Build = "NintendoSDK Firmware for NX 3.0.0-10.0";
  37. //http://switchbrew.org/index.php?title=System_Version_Title
  38. using (MemoryStream MS = new MemoryStream(0x100))
  39. {
  40. BinaryWriter Writer = new BinaryWriter(MS);
  41. Writer.Write(MajorFWVersion);
  42. Writer.Write(MinorFWVersion);
  43. Writer.Write(MicroFWVersion);
  44. Writer.Write(Unknown);
  45. Writer.Write(RevisionNumber);
  46. Writer.Write(Encoding.ASCII.GetBytes(Platform));
  47. MS.Seek(0x28, SeekOrigin.Begin);
  48. Writer.Write(Encoding.ASCII.GetBytes(UnknownHex));
  49. MS.Seek(0x68, SeekOrigin.Begin);
  50. Writer.Write(Encoding.ASCII.GetBytes(Version));
  51. MS.Seek(0x80, SeekOrigin.Begin);
  52. Writer.Write(Encoding.ASCII.GetBytes(Build));
  53. AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, MS.ToArray());
  54. }
  55. return 0;
  56. }
  57. public static long GetColorSetId(ServiceCtx Context)
  58. {
  59. Context.ResponseData.Write((int)Context.Ns.Settings.ThemeColor);
  60. return 0;
  61. }
  62. public static long SetColorSetId(ServiceCtx Context)
  63. {
  64. int ColorSetId = Context.RequestData.ReadInt32();
  65. Context.Ns.Settings.ThemeColor = (ColorSet)ColorSetId;
  66. return 0;
  67. }
  68. public static long GetSettingsItemValue(ServiceCtx Context)
  69. {
  70. long ClassPos = Context.Request.PtrBuff[0].Position;
  71. long ClassSize = Context.Request.PtrBuff[0].Size;
  72. long NamePos = Context.Request.PtrBuff[1].Position;
  73. long NameSize = Context.Request.PtrBuff[1].Size;
  74. long ReplyPos = Context.Request.ReceiveBuff[0].Position;
  75. long ReplySize = Context.Request.ReceiveBuff[0].Size;
  76. byte[] Class = AMemoryHelper.ReadBytes(Context.Memory, ClassPos, ClassSize);
  77. byte[] Name = AMemoryHelper.ReadBytes(Context.Memory, NamePos, NameSize);
  78. string AskedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(Name).Trim('\0');
  79. NxSettings.Settings.TryGetValue(AskedSetting, out object NxSetting);
  80. if (NxSetting != null)
  81. {
  82. byte[] SettingBuffer = new byte[ReplySize];
  83. if (NxSetting is string StringValue)
  84. {
  85. if (StringValue.Length + 1 > ReplySize)
  86. {
  87. Context.Ns.Log.PrintError(Logging.LogClass.ServiceSet, $"{AskedSetting} String value size is too big!");
  88. }
  89. else
  90. {
  91. SettingBuffer = Encoding.ASCII.GetBytes(StringValue + "\0");
  92. }
  93. }
  94. if (NxSetting is int IntValue)
  95. {
  96. SettingBuffer = BitConverter.GetBytes(IntValue);
  97. }
  98. else if (NxSetting is bool BoolValue)
  99. {
  100. SettingBuffer[0] = BoolValue ? (byte)1 : (byte)0;
  101. }
  102. else
  103. {
  104. throw new NotImplementedException(NxSetting.GetType().Name);
  105. }
  106. AMemoryHelper.WriteBytes(Context.Memory, ReplyPos, SettingBuffer);
  107. Context.Ns.Log.PrintDebug(Logging.LogClass.ServiceSet, $"{AskedSetting} set value: {NxSetting} as {NxSetting.GetType()}");
  108. }
  109. else
  110. {
  111. Context.Ns.Log.PrintError(Logging.LogClass.ServiceSet, $"{AskedSetting} not found!");
  112. }
  113. return 0;
  114. }
  115. }
  116. }