2020-09-27 15:00:38 -07:00
|
|
|
|
using LibHac.Common;
|
|
|
|
|
using LibHac.Fs;
|
|
|
|
|
using LibHac.Fs.Fsa;
|
|
|
|
|
using LibHac.FsSystem;
|
2022-01-12 03:22:19 -08:00
|
|
|
|
using LibHac.Tools.FsSystem;
|
|
|
|
|
using LibHac.Tools.FsSystem.NcaUtils;
|
2020-09-27 15:00:38 -07:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.HLE.FileSystem;
|
|
|
|
|
using Ryujinx.HLE.HOS.Services.Am.AppletAE;
|
|
|
|
|
using Ryujinx.HLE.HOS.SystemState;
|
|
|
|
|
using System;
|
2021-03-18 16:04:49 -07:00
|
|
|
|
using System.Collections.Generic;
|
2020-09-27 15:00:38 -07:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.HLE.HOS.Applets.Error
|
|
|
|
|
{
|
|
|
|
|
internal class ErrorApplet : IApplet
|
|
|
|
|
{
|
|
|
|
|
private const long ErrorMessageBinaryTitleId = 0x0100000000000801;
|
|
|
|
|
|
|
|
|
|
private Horizon _horizon;
|
|
|
|
|
private AppletSession _normalSession;
|
|
|
|
|
private CommonArguments _commonArguments;
|
|
|
|
|
private ErrorCommonHeader _errorCommonHeader;
|
|
|
|
|
private byte[] _errorStorage;
|
|
|
|
|
|
|
|
|
|
public event EventHandler AppletStateChanged;
|
|
|
|
|
|
|
|
|
|
public ErrorApplet(Horizon horizon)
|
|
|
|
|
{
|
|
|
|
|
_horizon = horizon;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-01 15:38:13 -07:00
|
|
|
|
public ResultCode Start(AppletSession normalSession, AppletSession interactiveSession)
|
2020-09-27 15:00:38 -07:00
|
|
|
|
{
|
|
|
|
|
_normalSession = normalSession;
|
|
|
|
|
_commonArguments = IApplet.ReadStruct<CommonArguments>(_normalSession.Pop());
|
|
|
|
|
|
|
|
|
|
Logger.Info?.PrintMsg(LogClass.ServiceAm, $"ErrorApplet version: 0x{_commonArguments.AppletVersion:x8}");
|
|
|
|
|
|
|
|
|
|
_errorStorage = _normalSession.Pop();
|
|
|
|
|
_errorCommonHeader = IApplet.ReadStruct<ErrorCommonHeader>(_errorStorage);
|
|
|
|
|
_errorStorage = _errorStorage.Skip(Marshal.SizeOf(typeof(ErrorCommonHeader))).ToArray();
|
|
|
|
|
|
|
|
|
|
switch (_errorCommonHeader.Type)
|
|
|
|
|
{
|
|
|
|
|
case ErrorType.ErrorCommonArg:
|
|
|
|
|
{
|
|
|
|
|
ParseErrorCommonArg();
|
|
|
|
|
|
2021-03-18 16:04:49 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ErrorType.ApplicationErrorArg:
|
|
|
|
|
{
|
|
|
|
|
ParseApplicationErrorArg();
|
|
|
|
|
|
2020-09-27 15:00:38 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: throw new NotImplementedException($"ErrorApplet type {_errorCommonHeader.Type} is not implemented.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AppletStateChanged?.Invoke(this, null);
|
|
|
|
|
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private (uint module, uint description) HexToResultCode(uint resultCode)
|
|
|
|
|
{
|
|
|
|
|
return ((resultCode & 0x1FF) + 2000, (resultCode >> 9) & 0x3FFF);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string SystemLanguageToLanguageKey(SystemLanguage systemLanguage)
|
|
|
|
|
{
|
|
|
|
|
return systemLanguage switch
|
|
|
|
|
{
|
|
|
|
|
SystemLanguage.Japanese => "ja",
|
|
|
|
|
SystemLanguage.AmericanEnglish => "en-US",
|
|
|
|
|
SystemLanguage.French => "fr",
|
|
|
|
|
SystemLanguage.German => "de",
|
|
|
|
|
SystemLanguage.Italian => "it",
|
|
|
|
|
SystemLanguage.Spanish => "es",
|
|
|
|
|
SystemLanguage.Chinese => "zh-Hans",
|
|
|
|
|
SystemLanguage.Korean => "ko",
|
|
|
|
|
SystemLanguage.Dutch => "nl",
|
|
|
|
|
SystemLanguage.Portuguese => "pt",
|
|
|
|
|
SystemLanguage.Russian => "ru",
|
|
|
|
|
SystemLanguage.Taiwanese => "zh-HansT",
|
|
|
|
|
SystemLanguage.BritishEnglish => "en-GB",
|
|
|
|
|
SystemLanguage.CanadianFrench => "fr-CA",
|
|
|
|
|
SystemLanguage.LatinAmericanSpanish => "es-419",
|
|
|
|
|
SystemLanguage.SimplifiedChinese => "zh-Hans",
|
|
|
|
|
SystemLanguage.TraditionalChinese => "zh-Hant",
|
2021-10-28 14:06:45 -07:00
|
|
|
|
SystemLanguage.BrazilianPortuguese => "pt-BR",
|
2020-09-27 15:00:38 -07:00
|
|
|
|
_ => "en-US"
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 14:06:45 -07:00
|
|
|
|
private static string CleanText(string value)
|
2020-09-27 15:00:38 -07:00
|
|
|
|
{
|
2021-10-28 14:06:45 -07:00
|
|
|
|
return Regex.Replace(value, @"[^\u0000\u0009\u000A\u000D\u0020-\uFFFF]..", "").Replace("\0", "");
|
2020-09-27 15:00:38 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetMessageText(uint module, uint description, string key)
|
|
|
|
|
{
|
|
|
|
|
string binaryTitleContentPath = _horizon.ContentManager.GetInstalledContentPath(ErrorMessageBinaryTitleId, StorageId.NandSystem, NcaContentType.Data);
|
|
|
|
|
|
|
|
|
|
using (LibHac.Fs.IStorage ncaFileStream = new LocalStorage(_horizon.Device.FileSystem.SwitchPathToSystemPath(binaryTitleContentPath), FileAccess.Read, FileMode.Open))
|
|
|
|
|
{
|
|
|
|
|
Nca nca = new Nca(_horizon.Device.FileSystem.KeySet, ncaFileStream);
|
|
|
|
|
IFileSystem romfs = nca.OpenFileSystem(NcaSectionType.Data, _horizon.FsIntegrityCheckLevel);
|
|
|
|
|
string languageCode = SystemLanguageToLanguageKey(_horizon.State.DesiredSystemLanguage);
|
2021-10-28 14:06:45 -07:00
|
|
|
|
string filePath = $"/{module}/{description:0000}/{languageCode}_{key}";
|
2020-09-27 15:00:38 -07:00
|
|
|
|
|
|
|
|
|
if (romfs.FileExists(filePath))
|
|
|
|
|
{
|
2021-12-23 08:55:50 -08:00
|
|
|
|
using var binaryFile = new UniqueRef<IFile>();
|
|
|
|
|
|
|
|
|
|
romfs.OpenFile(ref binaryFile.Ref(), filePath.ToU8Span(), OpenMode.Read).ThrowIfFailure();
|
|
|
|
|
StreamReader reader = new StreamReader(binaryFile.Get.AsStream(), Encoding.Unicode);
|
2020-09-27 15:00:38 -07:00
|
|
|
|
|
|
|
|
|
return CleanText(reader.ReadToEnd());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string[] GetButtonsText(uint module, uint description, string key)
|
|
|
|
|
{
|
|
|
|
|
string buttonsText = GetMessageText(module, description, key);
|
|
|
|
|
|
|
|
|
|
return (buttonsText == "") ? null : buttonsText.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ParseErrorCommonArg()
|
|
|
|
|
{
|
|
|
|
|
ErrorCommonArg errorCommonArg = IApplet.ReadStruct<ErrorCommonArg>(_errorStorage);
|
|
|
|
|
|
|
|
|
|
uint module = errorCommonArg.Module;
|
|
|
|
|
uint description = errorCommonArg.Description;
|
|
|
|
|
|
|
|
|
|
if (_errorCommonHeader.MessageFlag == 0)
|
|
|
|
|
{
|
|
|
|
|
(module, description) = HexToResultCode(errorCommonArg.ResultCode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string message = GetMessageText(module, description, "DlgMsg");
|
2021-10-28 14:06:45 -07:00
|
|
|
|
|
2020-09-27 15:00:38 -07:00
|
|
|
|
if (message == "")
|
|
|
|
|
{
|
|
|
|
|
message = "An error has occured.\n\n"
|
|
|
|
|
+ "Please try again later.\n\n"
|
|
|
|
|
+ "If the problem persists, please refer to the Ryujinx website.\n"
|
|
|
|
|
+ "www.ryujinx.org";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] buttons = GetButtonsText(module, description, "DlgBtn");
|
|
|
|
|
|
|
|
|
|
bool showDetails = _horizon.Device.UiHandler.DisplayErrorAppletDialog($"Error Code: {module}-{description:0000}", "\n" + message, buttons);
|
|
|
|
|
if (showDetails)
|
|
|
|
|
{
|
|
|
|
|
message = GetMessageText(module, description, "FlvMsg");
|
|
|
|
|
buttons = GetButtonsText(module, description, "FlvBtn");
|
|
|
|
|
|
|
|
|
|
_horizon.Device.UiHandler.DisplayErrorAppletDialog($"Details: {module}-{description:0000}", "\n" + message, buttons);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 16:04:49 -07:00
|
|
|
|
private void ParseApplicationErrorArg()
|
|
|
|
|
{
|
|
|
|
|
ApplicationErrorArg applicationErrorArg = IApplet.ReadStruct<ApplicationErrorArg>(_errorStorage);
|
|
|
|
|
|
|
|
|
|
byte[] messageTextBuffer = new byte[0x800];
|
|
|
|
|
byte[] detailsTextBuffer = new byte[0x800];
|
2021-11-01 15:38:13 -07:00
|
|
|
|
|
|
|
|
|
applicationErrorArg.MessageText.ToSpan().CopyTo(messageTextBuffer);
|
|
|
|
|
applicationErrorArg.DetailsText.ToSpan().CopyTo(detailsTextBuffer);
|
2021-03-18 16:04:49 -07:00
|
|
|
|
|
|
|
|
|
string messageText = Encoding.ASCII.GetString(messageTextBuffer.TakeWhile(b => !b.Equals(0)).ToArray());
|
|
|
|
|
string detailsText = Encoding.ASCII.GetString(detailsTextBuffer.TakeWhile(b => !b.Equals(0)).ToArray());
|
|
|
|
|
|
|
|
|
|
List<string> buttons = new List<string>();
|
|
|
|
|
|
|
|
|
|
// TODO: Handle the LanguageCode to return the translated "OK" and "Details".
|
|
|
|
|
|
|
|
|
|
if (detailsText.Trim() != "")
|
|
|
|
|
{
|
|
|
|
|
buttons.Add("Details");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buttons.Add("OK");
|
|
|
|
|
|
|
|
|
|
bool showDetails = _horizon.Device.UiHandler.DisplayErrorAppletDialog($"Error Number: {applicationErrorArg.ErrorNumber}", "\n" + messageText, buttons.ToArray());
|
|
|
|
|
if (showDetails)
|
|
|
|
|
{
|
|
|
|
|
buttons.RemoveAt(0);
|
|
|
|
|
|
|
|
|
|
_horizon.Device.UiHandler.DisplayErrorAppletDialog($"Error Number: {applicationErrorArg.ErrorNumber} (Details)", "\n" + detailsText, buttons.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 15:00:38 -07:00
|
|
|
|
public ResultCode GetResult()
|
|
|
|
|
{
|
|
|
|
|
return ResultCode.Success;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|