Program.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.Initalize();
  27. AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
  28. AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
  29. if (device.System.State.DiscordIntergrationEnabled == true)
  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.DiscordIntergrationEnabled == true)
  96. {
  97. if (File.ReadAllLines(Path.Combine(ApplicationDirectory, "RPsupported.dat")).Contains(device.System.TitleID))
  98. {
  99. DiscordPresence.Assets.LargeImageKey = device.System.TitleID;
  100. }
  101. DiscordPresence.Details = $"Playing {device.System.TitleName}";
  102. DiscordPresence.State = device.System.TitleID.ToUpper();
  103. DiscordPresence.Assets.LargeImageText = device.System.TitleName;
  104. DiscordPresence.Assets.SmallImageKey = "ryujinx";
  105. DiscordPresence.Assets.SmallImageText = "Ryujinx is an emulator for the Nintendo Switch";
  106. DiscordPresence.Timestamps = new Timestamps(DateTime.UtcNow);
  107. DiscordClient.SetPresence(DiscordPresence);
  108. }
  109. using (GlScreen screen = new GlScreen(device, renderer))
  110. {
  111. screen.MainLoop();
  112. Profile.FinishProfiling();
  113. device.Dispose();
  114. }
  115. audioOut.Dispose();
  116. Logger.Shutdown();
  117. DiscordClient.Dispose();
  118. }
  119. private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
  120. {
  121. Logger.Shutdown();
  122. DiscordClient.Dispose();
  123. }
  124. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  125. {
  126. var exception = e.ExceptionObject as Exception;
  127. Logger.PrintError(LogClass.Emulation, $"Unhandled exception caught: {exception}");
  128. if (e.IsTerminating)
  129. {
  130. Logger.Shutdown();
  131. DiscordClient.Dispose();
  132. }
  133. }
  134. /// <summary>
  135. /// Picks an <see cref="IAalOutput"/> audio output renderer supported on this machine
  136. /// </summary>
  137. /// <returns>An <see cref="IAalOutput"/> supported by this machine</returns>
  138. private static IAalOutput InitializeAudioEngine()
  139. {
  140. if (SoundIoAudioOut.IsSupported)
  141. {
  142. return new SoundIoAudioOut();
  143. }
  144. else if (OpenALAudioOut.IsSupported)
  145. {
  146. return new OpenALAudioOut();
  147. }
  148. else
  149. {
  150. return new DummyAudioOut();
  151. }
  152. }
  153. }
  154. }