internal void GetAvatarInfosAsync(Action <AvatarInfo> success = null, Action <Exception> error = null, Action complete = null) { List <string> fileNames = GetAvatarFileNames(); int loadedCount = 0; foreach (string existingFile in _avatarInfos.Keys.ToList()) { if (!fileNames.Contains(existingFile)) { _avatarInfos.Remove(existingFile); } } foreach (string fileName in fileNames) { string fullPath = Path.Combine(kCustomAvatarsPath, fileName); if (_avatarInfos.ContainsKey(fileName) && _avatarInfos[fileName].IsForFile(fullPath)) { _logger.Trace($"Using cached information for '{fileName}'"); success?.Invoke(_avatarInfos[fileName]); if (++loadedCount == fileNames.Count) { complete?.Invoke(); } } else { SharedCoroutineStarter.instance.StartCoroutine(_avatarLoader.FromFileCoroutine(fullPath, (avatar) => { var info = new AvatarInfo(avatar); _avatarInfos.Add(fileName, info); success?.Invoke(info); }, (exception) => { error?.Invoke(exception); }, () => { if (++loadedCount == fileNames.Count) { complete?.Invoke(); } })); } } }
public async Task <LoadedAvatar?> LoadAvatar(string avatarFile) { TaskCompletionSource <LoadedAvatar?> tcs = new TaskCompletionSource <LoadedAvatar?>(); try { var coroutine = _avatarLoader.FromFileCoroutine(avatarFile, (LoadedAvatar avatar) => tcs.TrySetResult(avatar), e => tcs.TrySetException(e)); await IPA.Utilities.Async.Coroutines.AsTask(coroutine); if (!tcs.Task.IsCompleted) { var timeout = Task.Delay(10000); var task = await Task.WhenAny(tcs.Task, timeout); if (task == timeout) { Plugin.Log?.Warn($"Timeout exceeded trying to load avatar '{avatarFile}'"); tcs.TrySetCanceled(); return(null); } } LoadedAvatar?avatar = await tcs.Task; if (avatar == null) { Plugin.Log?.Warn($"Couldn't load avatar at '{avatarFile}'"); return(null); } try { string calculatedHash = await HashAvatar(avatar); if (!cachedAvatars.ContainsKey(calculatedHash)) { cachedAvatars.Add(calculatedHash, avatar); } Plugin.Log?.Debug($"Hashed avatar \"{avatar.descriptor.name}\"! Hash: {calculatedHash}"); } catch (Exception ex) { Plugin.Log?.Error($"Unable to hash avatar \"{avatar.descriptor.name}\"! Exception: {ex}"); Plugin.Log?.Debug(ex); } return(avatar); } catch (Exception ex) { Plugin.Log?.Error($"Unable to load avatar!"); Plugin.Log?.Debug(ex); } return(null); }