Program.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using DiscordRPC;
  2. using Ryujinx.Audio;
  3. using Ryujinx.Common.Logging;
  4. using Ryujinx.Graphics.Gal;
  5. using Ryujinx.Graphics.Gal.OpenGL;
  6. using Ryujinx.HLE;
  7. using Ryujinx.Profiler;
  8. using System;
  9. using System.IO;
  10. using System.Linq;
  11. namespace Ryujinx
  12. {
  13. class Program
  14. {
  15. public static DiscordRpcClient DiscordClient;
  16. public static RichPresence DiscordPresence;
  17. public static string ApplicationDirectory => AppDomain.CurrentDomain.BaseDirectory;
  18. static void Main(string[] args)
  19. {
  20. Console.Title = "Ryujinx Console";
  21. IGalRenderer renderer = new OglRenderer();
  22. IAalOutput audioOut = InitializeAudioEngine();
  23. Switch device = new Switch(renderer, audioOut);
  24. Configuration.Load(Path.Combine(ApplicationDirectory, "Config.jsonc"));
  25. Configuration.Configure(device);
  26. Profile.Initialize();
  27. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  28. AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
  29. if (device.System.State.DiscordIntegrationEnabled)
  30. {
  31. DiscordClient = new DiscordRpcClient("568815339807309834");
  32. DiscordPresence = new RichPresence
  33. {
  34. Assets = new Assets
  35. {
  36. LargeImageKey = "ryujinx",
  37. LargeImageText = "Ryujinx is an emulator for the Nintendo Switch"
  38. }
  39. };
  40. DiscordClient.Initialize();
  41. DiscordClient.SetPresence(DiscordPresence);
  42. }
  43. if (args.Length == 1)
  44. {
  45. if (Directory.Exists(args[0]))
  46. {
  47. string[] romFsFiles = Directory.GetFiles(args[0], "*.istorage");
  48. if (romFsFiles.Length == 0)
  49. {
  50. romFsFiles = Directory.GetFiles(args[0], "*.romfs");
  51. }
  52. if (romFsFiles.Length > 0)
  53. {
  54. Logger.PrintInfo(LogClass.Application, "Loading as cart with RomFS.");
  55. device.LoadCart(args[0], romFsFiles[0]);
  56. }
  57. else
  58. {
  59. Logger.PrintInfo(LogClass.Application, "Loading as cart WITHOUT RomFS.");
  60. device.LoadCart(args[0]);
  61. }
  62. }
  63. else if (File.Exists(args[0]))
  64. {
  65. switch (Path.GetExtension(args[0]).ToLowerInvariant())
  66. {
  67. case ".xci":
  68. Logger.PrintInfo(LogClass.Application, "Loading as XCI.");
  69. device.LoadXci(args[0]);
  70. break;
  71. case ".nca":
  72. Logger.PrintInfo(LogClass.Application, "Loading as NCA.");
  73. device.LoadNca(args[0]);
  74. break;
  75. case ".nsp":
  76. case ".pfs0":
  77. Logger.PrintInfo(LogClass.Application, "Loading as NSP.");
  78. device.LoadNsp(args[0]);
  79. break;
  80. default:
  81. Logger.PrintInfo(LogClass.Application, "Loading as homebrew.");
  82. device.LoadProgram(args[0]);
  83. break;
  84. }
  85. }
  86. else
  87. {
  88. Logger.PrintWarning(LogClass.Application, "Please specify a valid XCI/NCA/NSP/PFS0/NRO file");
  89. }
  90. }
  91. else
  92. {
  93. Logger.PrintWarning(LogClass.Application, "Please specify the folder with the NSOs/IStorage or a NSO/NRO.");
  94. }
  95. if (device.System.State.DiscordIntegrationEnabled)
  96. {
  97. if (File.ReadAllLines(Path.Combine(ApplicationDirectory, "RPsupported.dat")).Contains(device.System.TitleID))
  98. {
  99. DiscordPresence.Assets.LargeImageKey = device.System.TitleID;
  100. }
  101. string state = device.System.TitleID;
  102. if (state == null)
  103. {
  104. state = "Ryujinx";
  105. }
  106. else
  107. {
  108. state = state.ToUpper();
  109. }
  110. string details = "Idling";
  111. if (device.System.TitleName != null)
  112. {
  113. details = $"Playing {device.System.TitleName}";
  114. }
  115. DiscordPresence.Details = details;
  116. DiscordPresence.State = state;
  117. DiscordPresence.Assets.LargeImageText = device.System.TitleName;
  118. DiscordPresence.Assets.SmallImageKey = "ryujinx";
  119. DiscordPresence.Assets.SmallImageText = "Ryujinx is an emulator for the Nintendo Switch";
  120. DiscordPresence.Timestamps = new Timestamps(DateTime.UtcNow);
  121. DiscordClient.SetPresence(DiscordPresence);
  122. }
  123. using (GlScreen screen = new GlScreen(device, renderer))
  124. {
  125. screen.MainLoop();
  126. Profile.FinishProfiling();
  127. device.Dispose();
  128. }
  129. audioOut.Dispose();
  130. Logger.Shutdown();
  131. DiscordClient.Dispose();
  132. }
  133. private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  134. {
  135. Logger.Shutdown();
  136. DiscordClient.Dispose();
  137. }
  138. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  139. {
  140. var exception = e.ExceptionObject as Exception;
  141. Logger.PrintError(LogClass.Emulation, $"Unhandled exception caught: {exception}");
  142. if (e.IsTerminating)
  143. {
  144. Logger.Shutdown();
  145. DiscordClient.Dispose();
  146. }
  147. }
  148. /// <summary>
  149. /// Picks an <see cref="IAalOutput"/> audio output renderer supported on this machine
  150. /// </summary>
  151. /// <returns>An <see cref="IAalOutput"/> supported by this machine</returns>
  152. private static IAalOutput InitializeAudioEngine()
  153. {
  154. if (SoundIoAudioOut.IsSupported)
  155. {
  156. return new SoundIoAudioOut();
  157. }
  158. else if (OpenALAudioOut.IsSupported)
  159. {
  160. return new OpenALAudioOut();
  161. }
  162. else
  163. {
  164. return new DummyAudioOut();
  165. }
  166. }
  167. }
  168. }