diff --git a/src/Alex/Graphics/Models/BoneMatrices.cs b/src/Alex/Graphics/Models/BoneMatrices.cs index 46e8e2d4d..0286d4d58 100644 --- a/src/Alex/Graphics/Models/BoneMatrices.cs +++ b/src/Alex/Graphics/Models/BoneMatrices.cs @@ -69,7 +69,7 @@ public void ApplyMovement() if (posData.IsValid) { - _positionData = _positionData.WithValues(_position, posData.Target * new Vector3(-1f, 1f, 1f), posData.TargetTime); + _positionData = _positionData.WithValues(_position, posData.Target * new Vector3(1f, 1f, 1f), posData.TargetTime); // (-1, 1, 1) for fixed LBSG npc _tempPositionData.Reset(); } diff --git a/src/Alex/Gui/Forms/BedrockFormManager.cs b/src/Alex/Gui/Forms/BedrockFormManager.cs index c69e364b4..3affca53b 100644 --- a/src/Alex/Gui/Forms/BedrockFormManager.cs +++ b/src/Alex/Gui/Forms/BedrockFormManager.cs @@ -1,3 +1,4 @@ +using System; using Alex.Net; using Alex.Net.Bedrock; using Alex.Worlds.Multiplayer.Bedrock; @@ -9,7 +10,7 @@ namespace Alex.Gui.Forms { - public class BedrockFormManager + public class BedrockFormManager : IDisposable { private static readonly ILogger Log = LogManager.GetCurrentClassLogger(); @@ -79,5 +80,15 @@ public void CloseAll() if (active != null) Hide(active.FormId); } + + /// + public void Dispose() + { + FormImage.ClearCache(); + + _activeForm?.Dispose(); + GuiManager?.Dispose(); + InputManager?.Dispose(); + } } } \ No newline at end of file diff --git a/src/Alex/Gui/Forms/FormImage.cs b/src/Alex/Gui/Forms/FormImage.cs index 1cbde915c..9c1f4da80 100644 --- a/src/Alex/Gui/Forms/FormImage.cs +++ b/src/Alex/Gui/Forms/FormImage.cs @@ -1,8 +1,12 @@ using System; using System.Collections.Concurrent; +using System.IO; using System.Net; +using System.Net.Http; using System.Threading; +using System.Threading.Tasks; using Alex.Common.Utils; +using Microsoft.Xna.Framework; using RocketUI; using NLog; using SixLabors.ImageSharp.PixelFormats; @@ -20,36 +24,77 @@ public class FormImage : RocketControl public FormImage(string url) { Image = url; + } + + private bool _didLoad = false; + private SemaphoreSlim _loadingLock = new SemaphoreSlim(1); + /// + protected override void OnDraw(GuiSpriteBatch graphics, GameTime gameTime) + { + base.OnDraw(graphics, gameTime); + + if (!_didLoad) + { + _ = LoadAsync(); + } + } - ThreadPool.QueueUserWorkItem( - (o) => + private async Task LoadAsync() + { + if (!await _loadingLock.WaitAsync(0)) + return; + + try + { + var path = Image; + if (!_cache.TryGetValue(path, out var data)) { - try - { - byte[] imageData = _cache.GetOrAdd( - Image, (path) => - { - using (WebClient wc = new WebClient()) - { - var data = wc.DownloadData(path); - - return data; - } - }); - - var image = SixLabors.ImageSharp.Image.Load(imageData); - TextureUtils.BitmapToTexture2DAsync( - this, Alex.Instance.GraphicsDevice, image, texture => - { - Background = (TextureSlice2D)texture; - image.Dispose(); - }, $"FormImage - {url}"); - } - catch (Exception ex) - { - Log.Error(ex, $"Could not convert image!"); - } - }); + HttpClient httpClient = new HttpClient(); + data = await httpClient.GetByteArrayAsync(path); + _cache.TryAdd(path, data); + } + + if (data == null) + return; + + using (MemoryStream ms = new MemoryStream(data)) + { + var image = await SixLabors.ImageSharp.Image.LoadAsync(ms); + + TextureUtils.BitmapToTexture2DAsync( + this, Alex.Instance.GraphicsDevice, image, texture => + { + Background = (TextureSlice2D) texture; + image?.Dispose(); + }, $"FormImage - {path}"); + } + } + catch (Exception ex) + { + Log.Error(ex, $"Could not convert image!"); + } + finally + { + _didLoad = true; + _loadingLock.Release(); + } + } + + /// + protected override void Dispose(bool disposing) + { + if (disposing) + { + _loadingLock?.Dispose(); + _loadingLock = null; + } + + base.Dispose(disposing); + } + + public static void ClearCache() + { + _cache?.Clear(); } } } \ No newline at end of file diff --git a/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs b/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs index 072946a64..ca2993e6e 100755 --- a/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs +++ b/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs @@ -776,7 +776,7 @@ public void HandleMcpeNetworkChunkPublisherUpdate(McpeNetworkChunkPublisherUpdat message.coordinates.X, message.coordinates.Y, message.coordinates.Z); Client.ChunkPublisherRadius = message.radius; - Log.Info($"ChunkPublisherUpdate: radius={message.radius} coordinates={message.coordinates}"); + //Log.Info($"ChunkPublisherUpdate: radius={message.radius} coordinates={message.coordinates}"); } public void HandleMcpeBiomeDefinitionList(McpeBiomeDefinitionList message) diff --git a/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs b/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs index 6f8469123..4d3394ab3 100644 --- a/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs +++ b/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs @@ -347,7 +347,9 @@ protected virtual void OnSpawn() { } public override void Dispose() { //World?.Ticker?.UnregisterTicked(this); - + FormManager?.Dispose(); + FormManager = null; + base.Dispose(); Client.Dispose(); }