diff --git a/Ktisis/Env/EnvService.cs b/Ktisis/Env/EnvService.cs index b7175f24d..8be7f2271 100644 --- a/Ktisis/Env/EnvService.cs +++ b/Ktisis/Env/EnvService.cs @@ -1,6 +1,16 @@ -using Ktisis.Events; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; + +using Dalamud.Interface.Internal; +using Dalamud.Logging; + +using Ktisis.Events; using Ktisis.Interop.Hooks; +using Lumina.Excel.GeneratedSheets; + namespace Ktisis.Env { public static class EnvService { public static float? TimeOverride; @@ -27,5 +37,67 @@ private static void OnGPoseChange(bool state) { SkyOverride = null; } } + + // Data + + private static uint CurSky = uint.MaxValue; + + public static readonly object SkyLock = new(); + public static IDalamudTextureWrap? SkyTex; + + public static void GetSkyImage(uint sky) { + if (sky == CurSky) return; + + CurSky = sky; + GetSkyboxTex(CurSky).ContinueWith(result => { + if (result.Exception != null) { + PluginLog.Error(result.Exception.ToString()); + return; + } + + lock (SkyLock) { + SkyTex?.Dispose(); + SkyTex = result.Result; + } + }); + } + + private static async Task GetSkyboxTex(uint skyId) { + await Task.Yield(); + PluginLog.Verbose($"Retrieving skybox texture: {skyId:000}"); + return Services.Textures.GetTextureFromGame($"bgcommon/nature/sky/texture/sky_{skyId:000}.tex"); + } + + public static async Task> GetZoneWeatherAndIcons(ushort id, CancellationToken token) { + await Task.Yield(); + + PluginLog.Verbose($"Retrieving weather data for territory: {id}"); + + var result = new Dictionary(); + + var territory = Services.DataManager.GetExcelSheet()?.GetRow(id); + if (territory == null || token.IsCancellationRequested) return result; + + var weatherRate = Services.DataManager.GetExcelSheet()?.GetRow(territory.WeatherRate); + if (token.IsCancellationRequested) return result; + var weatherSheet = Services.DataManager.GetExcelSheet(); + if (weatherRate == null || weatherSheet == null || token.IsCancellationRequested) return result; + + var data = weatherRate.UnkData0.ToList(); + data.Sort((a, b) => a.Weather - b.Weather); + + foreach (var rate in data) { + if (token.IsCancellationRequested) break; + if (rate.Weather <= 0 || rate.Rate == 0) continue; + + var weather = weatherSheet.GetRow((uint)rate.Weather); + if (weather == null) continue; + + var icon = Services.Textures.GetIcon((uint)weather.Icon); + result.TryAdd(weather, icon); + } + + return result; + } } } diff --git a/Ktisis/Interface/Windows/Workspace/Tabs/WorldTab.cs b/Ktisis/Interface/Windows/Workspace/Tabs/WorldTab.cs index 6b7b8113d..3fa7bf605 100644 --- a/Ktisis/Interface/Windows/Workspace/Tabs/WorldTab.cs +++ b/Ktisis/Interface/Windows/Workspace/Tabs/WorldTab.cs @@ -1,8 +1,6 @@ using System; -using System.Linq; using System.Numerics; using System.Threading; -using System.Threading.Tasks; using System.Collections.Generic; using Dalamud.Interface.Internal; @@ -45,7 +43,7 @@ private static void CheckData() { TokenSource?.Cancel(); TokenSource = source; - GetZoneWeatherAndIcons(territory, token).ContinueWith(result => { + EnvService.GetZoneWeatherAndIcons(territory, token).ContinueWith(result => { if (result.Exception != null) { PluginLog.Error($"Failed to load weather data:\n{result.Exception}"); return; @@ -170,47 +168,22 @@ private static void DrawWeatherLabel(Weather weather, IDalamudTextureWrap? icon, } // Sky - - private static uint CurSky; - - private static readonly object SkyLock = new(); - private static IDalamudTextureWrap? SkyTex; - - private static void GetSkyImage(uint sky) { - if (sky == CurSky) return; - - CurSky = sky; - GetSkyboxTex(CurSky).ContinueWith(result => { - if (result.Exception != null) { - PluginLog.Error(result.Exception.ToString()); - return; - } - - lock (SkyLock) { - SkyTex?.Dispose(); - SkyTex = result.Result; - } - }); - } private static bool DrawSkySelect(ref uint skyId) { - GetSkyImage(skyId); + EnvService.GetSkyImage(skyId); var innerSpace = ImGui.GetStyle().ItemInnerSpacing.Y; var height = ImGui.GetFrameHeight() * 2 + innerSpace; var buttonSize = new Vector2(height, height); - - //var button = false; - lock (SkyLock) { - if (SkyTex != null) - ImGui.Image(SkyTex.ImGuiHandle, buttonSize); + + lock (EnvService.SkyLock) { + if (EnvService.SkyTex != null) + ImGui.Image(EnvService.SkyTex.ImGuiHandle, buttonSize); } ImGui.SameLine(); - var avail = ImGui.GetContentRegionAvail().X; - ImGui.SetCursorPosY(ImGui.GetCursorPosY() + innerSpace); ImGui.BeginGroup(); @@ -223,45 +196,5 @@ private static bool DrawSkySelect(ref uint skyId) { return changed; } - - // Data - - private static async Task GetSkyboxTex(uint skyId) { - await Task.Yield(); - PluginLog.Verbose($"Retrieving skybox texture: {skyId:000}"); - return Services.Textures.GetTextureFromGame($"bgcommon/nature/sky/texture/sky_{skyId:000}.tex"); - } - - private static async Task> GetZoneWeatherAndIcons(ushort id, CancellationToken token) { - await Task.Yield(); - - PluginLog.Verbose($"Retrieving weather data for territory: {id}"); - - var result = new Dictionary(); - - var territory = Services.DataManager.GetExcelSheet()?.GetRow(id); - if (territory == null || token.IsCancellationRequested) return result; - - var weatherRate = Services.DataManager.GetExcelSheet()?.GetRow(territory.WeatherRate); - if (token.IsCancellationRequested) return result; - var weatherSheet = Services.DataManager.GetExcelSheet(); - if (weatherRate == null || weatherSheet == null || token.IsCancellationRequested) return result; - - var data = weatherRate.UnkData0.ToList(); - data.Sort((a, b) => a.Weather - b.Weather); - - foreach (var rate in data) { - if (token.IsCancellationRequested) break; - if (rate.Weather <= 0 || rate.Rate == 0) continue; - - var weather = weatherSheet.GetRow((uint)rate.Weather); - if (weather == null) continue; - - var icon = Services.Textures.GetIcon((uint)weather.Icon); - result.TryAdd(weather, icon); - } - - return result; - } } }