Better support for user accounts (#349)

* Better support for user accounts

* Nits

* Check for invalid ids
This commit is contained in:
gdkchan
2018-08-14 19:02:42 -03:00
committed by GitHub
parent 17f54b5d78
commit 9ac5583513
25 changed files with 314 additions and 136 deletions

View File

@@ -0,0 +1,7 @@
namespace Ryujinx.HLE.OsHle.Services.Acc
{
static class AccErr
{
public const int UserNotFound = 100;
}
}

View File

@@ -1,7 +1,11 @@
using ChocolArm64.Memory;
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.SystemState;
using System.Collections.Generic;
using static Ryujinx.HLE.OsHle.ErrorCode;
namespace Ryujinx.HLE.OsHle.Services.Acc
{
class IAccountServiceForApplication : IpcService
@@ -27,49 +31,80 @@ namespace Ryujinx.HLE.OsHle.Services.Acc
public long GetUserCount(ServiceCtx Context)
{
Context.ResponseData.Write(0);
Context.Ns.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");
Context.ResponseData.Write(Context.Ns.Os.SystemState.GetUserCount());
return 0;
}
public long GetUserExistence(ServiceCtx Context)
{
Context.ResponseData.Write(1);
UserId Uuid = new UserId(
Context.RequestData.ReadInt64(),
Context.RequestData.ReadInt64());
Context.Ns.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");
Context.ResponseData.Write(Context.Ns.Os.SystemState.TryGetUser(Uuid, out _) ? 1 : 0);
return 0;
}
public long ListAllUsers(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");
return 0;
return WriteUserList(Context, Context.Ns.Os.SystemState.GetAllUsers());
}
public long ListOpenUsers(ServiceCtx Context)
{
Context.Ns.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");
return WriteUserList(Context, Context.Ns.Os.SystemState.GetOpenUsers());
}
private long WriteUserList(ServiceCtx Context, IEnumerable<UserProfile> Profiles)
{
long OutputPosition = Context.Request.RecvListBuff[0].Position;
long OutputSize = Context.Request.RecvListBuff[0].Size;
long Offset = 0;
foreach (UserProfile Profile in Profiles)
{
if ((ulong)Offset + 16 > (ulong)OutputSize)
{
break;
}
byte[] Uuid = Profile.Uuid.Bytes;
for (int Index = Uuid.Length - 1; Index >= 0; Index--)
{
Context.Memory.WriteByte(OutputPosition + Offset++, Uuid[Index]);
}
}
return 0;
}
public long GetLastOpenedUser(ServiceCtx Context)
{
Context.ResponseData.Write(1L);
Context.ResponseData.Write(0L);
UserProfile LastOpened = Context.Ns.Os.SystemState.LastOpenUser;
Context.Ns.Log.PrintStub(LogClass.ServiceAcc, "Stubbed.");
LastOpened.Uuid.Write(Context.ResponseData);
return 0;
}
public long GetProfile(ServiceCtx Context)
{
MakeObject(Context, new IProfile());
UserId Uuid = new UserId(
Context.RequestData.ReadInt64(),
Context.RequestData.ReadInt64());
if (!Context.Ns.Os.SystemState.TryGetUser(Uuid, out UserProfile Profile))
{
Context.Ns.Log.PrintWarning(LogClass.ServiceAcc, $"User 0x{Uuid} not found!");
return MakeError(ErrorModule.Account, AccErr.UserNotFound);
}
MakeObject(Context, new IProfile(Profile));
return 0;
}

View File

@@ -1,6 +1,7 @@
using ChocolArm64.Memory;
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.SystemState;
using Ryujinx.HLE.OsHle.Utilities;
using System.Collections.Generic;
using System.Text;
@@ -13,13 +14,17 @@ namespace Ryujinx.HLE.OsHle.Services.Acc
public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;
public IProfile()
private UserProfile Profile;
public IProfile(UserProfile Profile)
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
{ 0, Get },
{ 1, GetBase }
};
this.Profile = Profile;
}
public long Get(ServiceCtx Context)
@@ -32,20 +37,18 @@ namespace Ryujinx.HLE.OsHle.Services.Acc
Context.Memory.WriteInt32(Position, 0);
Context.Memory.WriteInt32(Position + 4, 1);
Context.Memory.WriteByte(Position + 8, 1);
Context.Memory.WriteInt64(Position + 8, 1);
return GetBase(Context);
}
public long GetBase(ServiceCtx Context)
{
ProfileBase ProfileBase = new ProfileBase(Context.Ns.Settings.User);
Profile.Uuid.Write(Context.ResponseData);
Context.ResponseData.Write(ProfileBase.UserId.ToBytes());
Context.ResponseData.Write(ProfileBase.Timestamp);
Context.ResponseData.Write(Profile.LastModifiedTimestamp);
int ByteCount = Encoding.UTF8.GetByteCount(ProfileBase.Username);
byte[] Username = StringUtils.GetFixedLengthBytes(ProfileBase.Username, 0x20, Encoding.UTF8);
byte[] Username = StringUtils.GetFixedLengthBytes(Profile.Name, 0x20, Encoding.UTF8);
Context.ResponseData.Write(Username);

View File

@@ -1,52 +0,0 @@
using Ryujinx.HLE.OsHle.Utilities;
using System;
using System.Linq;
namespace Ryujinx.HLE.OsHle.Services.Acc
{
struct ProfileBase
{
public UserId UserId;
public long Timestamp;
public string Username;
public ProfileBase(Profile User)
{
UserId = new UserId(User.UserId);
Username = User.Username;
Timestamp = ((DateTimeOffset)DateTime.Today).ToUnixTimeSeconds();
}
}
struct UserId
{
private readonly ulong LowBytes;
private readonly ulong HighBytes;
public UserId(string UserIdHex)
{
if (UserIdHex == null || UserIdHex.Length != 32 || !UserIdHex.All("0123456789abcdefABCDEF".Contains))
{
throw new ArgumentException("UserId is not a valid Hex string", "UserIdHex");
}
byte[] HexBytes = StringUtils.HexToBytes(UserIdHex);
LowBytes = BitConverter.ToUInt64(HexBytes, 8);
Array.Resize(ref HexBytes, 8);
HighBytes = BitConverter.ToUInt64(HexBytes, 0);
}
public byte[] ToBytes()
{
return BitConverter.GetBytes(HighBytes).Concat(BitConverter.GetBytes(LowBytes)).ToArray();
}
public override string ToString()
{
return BitConverter.ToString(ToBytes()).ToLower().Replace("-", string.Empty);
}
}
}

View File

@@ -3,7 +3,6 @@ using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc;
using System.Collections.Generic;
using static Ryujinx.HLE.OsHle.SystemStateMgr;
using static Ryujinx.HLE.OsHle.ErrorCode;
namespace Ryujinx.HLE.OsHle.Services.Am
@@ -58,7 +57,9 @@ namespace Ryujinx.HLE.OsHle.Services.Am
public long GetOperationMode(ServiceCtx Context)
{
OperationMode Mode = DockedMode ? OperationMode.Docked : OperationMode.Handheld;
OperationMode Mode = Context.Ns.Os.SystemState.DockedMode
? OperationMode.Docked
: OperationMode.Handheld;
Context.ResponseData.Write((byte)Mode);
@@ -67,7 +68,9 @@ namespace Ryujinx.HLE.OsHle.Services.Am
public long GetPerformanceMode(ServiceCtx Context)
{
Apm.PerformanceMode Mode = DockedMode ? Apm.PerformanceMode.Docked : Apm.PerformanceMode.Handheld;
Apm.PerformanceMode Mode = Context.Ns.Os.SystemState.DockedMode
? Apm.PerformanceMode.Docked
: Apm.PerformanceMode.Handheld;
Context.ResponseData.Write((int)Mode);

View File

@@ -1,6 +1,7 @@
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Handles;
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.SystemState;
using System.Collections.Generic;
using System.Text;

View File

@@ -1,4 +1,6 @@
using Ryujinx.HLE.Logging;
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.SystemState;
using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Friend
@@ -13,8 +15,35 @@ namespace Ryujinx.HLE.OsHle.Services.Friend
{
m_Commands = new Dictionary<int, ServiceProcessRequest>()
{
//...
{ 10601, DeclareCloseOnlinePlaySession },
{ 10610, UpdateUserPresence }
};
}
public long DeclareCloseOnlinePlaySession(ServiceCtx Context)
{
UserId Uuid = new UserId(
Context.RequestData.ReadInt64(),
Context.RequestData.ReadInt64());
if (Context.Ns.Os.SystemState.TryGetUser(Uuid, out UserProfile Profile))
{
Profile.OnlinePlayState = OpenCloseState.Closed;
}
return 0;
}
public long UpdateUserPresence(ServiceCtx Context)
{
UserId Uuid = new UserId(
Context.RequestData.ReadInt64(),
Context.RequestData.ReadInt64());
//TODO.
Context.Ns.Log.PrintStub(LogClass.ServiceFriend, "Stubbed.");
return 0;
}
}
}

View File

@@ -1,4 +1,5 @@
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.OsHle.SystemState;
using System.Collections.Generic;
namespace Ryujinx.HLE.OsHle.Services.Set

View File

@@ -1,5 +1,5 @@
using Ryujinx.HLE.OsHle.Ipc;
using Ryujinx.HLE.Settings;
using Ryujinx.HLE.OsHle.SystemState;
using System;
using System.Collections.Generic;
using System.IO;
@@ -75,7 +75,7 @@ namespace Ryujinx.HLE.OsHle.Services.Set
public static long GetColorSetId(ServiceCtx Context)
{
Context.ResponseData.Write((int)Context.Ns.Settings.ThemeColor);
Context.ResponseData.Write((int)Context.Ns.Os.SystemState.ThemeColor);
return 0;
}
@@ -84,7 +84,8 @@ namespace Ryujinx.HLE.OsHle.Services.Set
{
int ColorSetId = Context.RequestData.ReadInt32();
Context.Ns.Settings.ThemeColor = (ColorSet)ColorSetId;
Context.Ns.Os.SystemState.ThemeColor = (ColorSet)ColorSetId;
return 0;
}
@@ -121,6 +122,7 @@ namespace Ryujinx.HLE.OsHle.Services.Set
SettingBuffer = Encoding.ASCII.GetBytes(StringValue + "\0");
}
}
if (NxSetting is int IntValue)
{
SettingBuffer = BitConverter.GetBytes(IntValue);