public static async void HashAllAvatars() { if (cachedAvatars.Count != CustomAvatar.Plugin.Instance.AvatarLoader.Avatars.Count && !isCalculatingHashes) { isCalculatingHashes = true; Plugin.log.Debug($"Hashing avatars... {CustomAvatar.Plugin.Instance.AvatarLoader.Avatars.Count} avatars found"); try { cachedAvatars.Clear(); totalAvatarsCount = CustomAvatar.Plugin.Instance.AvatarLoader.Avatars.Count; calculatedHashesCount = 0; foreach (CustomAvatar.CustomAvatar avatar in CustomAvatar.Plugin.Instance.AvatarLoader.Avatars) { if (!avatar.IsLoaded) { AutoResetEvent wh = new AutoResetEvent(false); HMMainThreadDispatcher.instance.Enqueue(() => { avatar.Load(async(loadedAvatar, result) => { if (result == CustomAvatar.AvatarLoadResult.Completed) { string hash = await AvatarsHashCache.GetHashForAvatar(avatar).ConfigureAwait(false); cachedAvatars.Add(hash, avatar); } wh.Set(); }); }); await Task.Run(() => wh.WaitOne()).ConfigureAwait(false); calculatedHashesCount++; } else { string hash = await AvatarsHashCache.GetHashForAvatar(avatar).ConfigureAwait(false); cachedAvatars.Add(hash, avatar); calculatedHashesCount++; } } Plugin.log.Debug("All avatars hashed and loaded!"); HMMainThreadDispatcher.instance.Enqueue(() => { hashesCalculated?.Invoke(); }); } catch (Exception e) { Plugin.log.Error($"Unable to hash and load avatars! Exception: {e}"); } isCalculatingHashes = false; } }
public static async void HashAllAvatars() { totalAvatarsCount = Directory.GetFiles("CustomAvatars", "*.avatar").Length; if (totalAvatarsCount != cachedAvatars.Count && !isCalculatingHashes) { isCalculatingHashes = true; Plugin.log.Debug($"Hashing avatars... {totalAvatarsCount} avatars found"); try { cachedAvatars.Clear(); calculatedHashesCount = 0; AvatarManager.instance.GetAvatarsAsync( async(LoadedAvatar avatar) => { try { string calculatedHash = await AvatarsHashCache.GetHashForAvatar(avatar).ConfigureAwait(false); 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}"); } calculatedHashesCount++; }, (Exception ex) => { Plugin.log.Error($"Unable to load avatar! Exception: {ex}"); calculatedHashesCount++; }); while (totalAvatarsCount != calculatedHashesCount) { await Task.Delay(11); } Plugin.log.Debug("All avatars hashed and loaded!"); HMMainThreadDispatcher.instance.Enqueue(() => { hashesCalculated?.Invoke(); }); } catch (Exception e) { Plugin.log.Error($"Unable to hash and load avatars! Exception: {e}"); } isCalculatingHashes = false; } }