account: add Custom User Profiles support (#2227)

* Initial Impl

* Fix names

* remove useless ContentManager

* Support backgrounds and improve avatar loading

* Fix firmware checks

* Addresses gdkchan feedback
This commit is contained in:
Ac_K 2021-04-23 22:26:31 +02:00 committed by GitHub
parent 3e61fb0268
commit c46f6879ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 1286 additions and 41 deletions

View file

@ -1,41 +1,85 @@
using Ryujinx.Common;
using LibHac;
using LibHac.Fs;
using LibHac.Fs.Shim;
using Ryujinx.Common;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.FileSystem.Content;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Ryujinx.HLE.HOS.Services.Account.Acc
{
public class AccountManager
{
public static readonly UserId DefaultUserId = new UserId("00000000000000010000000000000000");
private readonly VirtualFileSystem _virtualFileSystem;
private readonly AccountSaveDataManager _accountSaveDataManager;
private ConcurrentDictionary<string, UserProfile> _profiles;
public UserProfile LastOpenedUser { get; private set; }
public AccountManager()
public AccountManager(VirtualFileSystem virtualFileSystem)
{
_virtualFileSystem = virtualFileSystem;
_profiles = new ConcurrentDictionary<string, UserProfile>();
UserId defaultUserId = new UserId("00000000000000010000000000000000");
byte[] defaultUserImage = EmbeddedResources.Read("Ryujinx.HLE/HOS/Services/Account/Acc/DefaultUserImage.jpg");
_accountSaveDataManager = new AccountSaveDataManager(_profiles);
AddUser(defaultUserId, "Player", defaultUserImage);
OpenUser(defaultUserId);
if (!_profiles.TryGetValue(DefaultUserId.ToString(), out _))
{
byte[] defaultUserImage = EmbeddedResources.Read("Ryujinx.HLE/HOS/Services/Account/Acc/DefaultUserImage.jpg");
AddUser("RyuPlayer", defaultUserImage, DefaultUserId);
OpenUser(DefaultUserId);
}
else
{
OpenUser(_accountSaveDataManager.LastOpened);
}
}
public void AddUser(UserId userId, string name, byte[] image)
public void AddUser(string name, byte[] image, UserId userId = new UserId())
{
if (userId.IsNull)
{
userId = new UserId(Guid.NewGuid().ToString().Replace("-", ""));
}
UserProfile profile = new UserProfile(userId, name, image);
_profiles.AddOrUpdate(userId.ToString(), profile, (key, old) => profile);
_accountSaveDataManager.Save(_profiles);
}
public void OpenUser(UserId userId)
{
if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
{
// TODO: Support multiple open users ?
foreach (UserProfile userProfile in GetAllUsers())
{
if (userProfile == LastOpenedUser)
{
userProfile.AccountState = AccountState.Closed;
break;
}
}
(LastOpenedUser = profile).AccountState = AccountState.Open;
_accountSaveDataManager.LastOpened = userId;
}
_accountSaveDataManager.Save(_profiles);
}
public void CloseUser(UserId userId)
@ -44,9 +88,117 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
{
profile.AccountState = AccountState.Closed;
}
_accountSaveDataManager.Save(_profiles);
}
public int GetUserCount()
public void OpenUserOnlinePlay(UserId userId)
{
if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
{
// TODO: Support multiple open online users ?
foreach (UserProfile userProfile in GetAllUsers())
{
if (userProfile == LastOpenedUser)
{
userProfile.OnlinePlayState = AccountState.Closed;
break;
}
}
profile.OnlinePlayState = AccountState.Open;
}
_accountSaveDataManager.Save(_profiles);
}
public void CloseUserOnlinePlay(UserId userId)
{
if (_profiles.TryGetValue(userId.ToString(), out UserProfile profile))
{
profile.OnlinePlayState = AccountState.Closed;
}
_accountSaveDataManager.Save(_profiles);
}
public void SetUserImage(UserId userId, byte[] image)
{
foreach (UserProfile userProfile in GetAllUsers())
{
if (userProfile.UserId == userId)
{
userProfile.Image = image;
break;
}
}
_accountSaveDataManager.Save(_profiles);
}
public void SetUserName(UserId userId, string name)
{
foreach (UserProfile userProfile in GetAllUsers())
{
if (userProfile.UserId == userId)
{
userProfile.Name = name;
break;
}
}
_accountSaveDataManager.Save(_profiles);
}
public void DeleteUser(UserId userId)
{
DeleteSaveData(userId);
_profiles.Remove(userId.ToString(), out _);
OpenUser(DefaultUserId);
_accountSaveDataManager.Save(_profiles);
}
private void DeleteSaveData(UserId userId)
{
SaveDataFilter saveDataFilter = new SaveDataFilter();
saveDataFilter.SetUserId(new LibHac.Fs.UserId((ulong)userId.High, (ulong)userId.Low));
Result result = _virtualFileSystem.FsClient.OpenSaveDataIterator(out SaveDataIterator saveDataIterator, SaveDataSpaceId.User, ref saveDataFilter);
if (result.IsSuccess())
{
Span<SaveDataInfo> saveDataInfo = stackalloc SaveDataInfo[10];
while (true)
{
saveDataIterator.ReadSaveDataInfo(out long readCount, saveDataInfo);
if (readCount == 0)
{
break;
}
for (int i = 0; i < readCount; i++)
{
// TODO: We use Directory.Delete workaround because DeleteSaveData softlock without, due to a bug in LibHac 0.12.0.
string savePath = Path.Combine(_virtualFileSystem.GetNandPath(), $"user/save/{saveDataInfo[i].SaveDataId:x16}");
string saveMetaPath = Path.Combine(_virtualFileSystem.GetNandPath(), $"user/saveMeta/{saveDataInfo[i].SaveDataId:x16}");
Directory.Delete(savePath, true);
Directory.Delete(saveMetaPath, true);
_virtualFileSystem.FsClient.DeleteSaveData(SaveDataSpaceId.User, saveDataInfo[i].SaveDataId);
}
}
}
}
internal int GetUserCount()
{
return _profiles.Count;
}
@ -56,7 +208,7 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
return _profiles.TryGetValue(userId.ToString(), out profile);
}
internal IEnumerable<UserProfile> GetAllUsers()
public IEnumerable<UserProfile> GetAllUsers()
{
return _profiles.Values;
}

View file

@ -0,0 +1,87 @@
using Ryujinx.Common.Configuration;
using Ryujinx.Common.Utilities;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Text.Json.Serialization;
namespace Ryujinx.HLE.HOS.Services.Account.Acc
{
class AccountSaveDataManager
{
private readonly string _profilesJsonPath = Path.Join(AppDataManager.BaseDirPath, "system", "Profiles.json");
private struct ProfilesJson
{
[JsonPropertyName("profiles")]
public List<UserProfileJson> Profiles { get; set; }
[JsonPropertyName("last_opened")]
public string LastOpened { get; set; }
}
private struct UserProfileJson
{
[JsonPropertyName("user_id")]
public string UserId { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("account_state")]
public AccountState AccountState { get; set; }
[JsonPropertyName("online_play_state")]
public AccountState OnlinePlayState { get; set; }
[JsonPropertyName("last_modified_timestamp")]
public long LastModifiedTimestamp { get; set; }
[JsonPropertyName("image")]
public byte[] Image { get; set; }
}
public UserId LastOpened { get; set; }
public AccountSaveDataManager(ConcurrentDictionary<string, UserProfile> profiles)
{
// TODO: Use 0x8000000000000010 system savedata instead of a JSON file if needed.
if (File.Exists(_profilesJsonPath))
{
ProfilesJson profilesJson = JsonHelper.DeserializeFromFile<ProfilesJson>(_profilesJsonPath);
foreach (var profile in profilesJson.Profiles)
{
UserProfile addedProfile = new UserProfile(new UserId(profile.UserId), profile.Name, profile.Image, profile.LastModifiedTimestamp);
profiles.AddOrUpdate(profile.UserId, addedProfile, (key, old) => addedProfile);
}
LastOpened = new UserId(profilesJson.LastOpened);
}
else
{
LastOpened = AccountManager.DefaultUserId;
}
}
public void Save(ConcurrentDictionary<string, UserProfile> profiles)
{
ProfilesJson profilesJson = new ProfilesJson()
{
Profiles = new List<UserProfileJson>(),
LastOpened = LastOpened.ToString()
};
foreach (var profile in profiles)
{
profilesJson.Profiles.Add(new UserProfileJson()
{
UserId = profile.Value.UserId.ToString(),
Name = profile.Value.Name,
AccountState = profile.Value.AccountState,
OnlinePlayState = profile.Value.OnlinePlayState,
LastModifiedTimestamp = profile.Value.LastModifiedTimestamp,
Image = profile.Value.Image,
});
}
File.WriteAllText(_profilesJsonPath, JsonHelper.Serialize(profilesJson, true));
}
}
}

View file

@ -8,31 +8,80 @@ namespace Ryujinx.HLE.HOS.Services.Account.Acc
public UserId UserId { get; }
public string Name { get; }
public long LastModifiedTimestamp { get; set; }
public byte[] Image { get; }
private string _name;
public long LastModifiedTimestamp { get; private set; }
public string Name
{
get => _name;
set
{
_name = value;
public AccountState AccountState { get; set; }
public AccountState OnlinePlayState { get; set; }
UpdateLastModifiedTimestamp();
}
}
public UserProfile(UserId userId, string name, byte[] image)
private byte[] _image;
public byte[] Image
{
get => _image;
set
{
_image = value;
UpdateLastModifiedTimestamp();
}
}
private AccountState _accountState;
public AccountState AccountState
{
get => _accountState;
set
{
_accountState = value;
UpdateLastModifiedTimestamp();
}
}
public AccountState _onlinePlayState;
public AccountState OnlinePlayState
{
get => _onlinePlayState;
set
{
_onlinePlayState = value;
UpdateLastModifiedTimestamp();
}
}
public UserProfile(UserId userId, string name, byte[] image, long lastModifiedTimestamp = 0)
{
UserId = userId;
Name = name;
Image = image;
LastModifiedTimestamp = 0;
Image = image;
AccountState = AccountState.Closed;
OnlinePlayState = AccountState.Closed;
UpdateTimestamp();
if (lastModifiedTimestamp != 0)
{
LastModifiedTimestamp = lastModifiedTimestamp;
}
else
{
UpdateLastModifiedTimestamp();
}
}
private void UpdateTimestamp()
private void UpdateLastModifiedTimestamp()
{
LastModifiedTimestamp = (long)(DateTime.Now - Epoch).TotalSeconds;
}

View file

@ -1,7 +1,6 @@
using Ryujinx.Common.Memory;
using Ryujinx.HLE.HOS.Services.Caps.Types;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.IO;
@ -19,11 +18,6 @@ namespace Ryujinx.HLE.HOS.Services.Caps
public CaptureManager(Switch device)
{
_sdCardPath = device.FileSystem.GetSdCardPath();
SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder()
{
Quality = 100
});
}
public ResultCode SetShimLibraryVersion(ServiceCtx context)

View file

@ -150,12 +150,9 @@ namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
return ResultCode.InvalidArgument;
}
if (context.Device.System.AccountManager.TryGetUser(userId, out UserProfile profile))
{
profile.OnlinePlayState = AccountState.Open;
}
Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
context.Device.System.AccountManager.OpenUserOnlinePlay(userId);
Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
return ResultCode.Success;
}
@ -171,12 +168,9 @@ namespace Ryujinx.HLE.HOS.Services.Friend.ServiceCreator
return ResultCode.InvalidArgument;
}
if (context.Device.System.AccountManager.TryGetUser(userId, out UserProfile profile))
{
profile.OnlinePlayState = AccountState.Closed;
}
context.Device.System.AccountManager.CloseUserOnlinePlay(userId);
Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString(), profile.OnlinePlayState });
Logger.Stub?.PrintStub(LogClass.ServiceFriend, new { UserId = userId.ToString() });
return ResultCode.Success;
}