Skip to content

Commit

Permalink
move env data access into EnvService
Browse files Browse the repository at this point in the history
  • Loading branch information
chirpxiv committed Sep 29, 2023
1 parent aac43f7 commit 93b9117
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 74 deletions.
74 changes: 73 additions & 1 deletion Ktisis/Env/EnvService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {

This comment has been minimized.

Copy link
@Fayti1703

Fayti1703 Sep 29, 2023

Member

void-returning methods shouldn't be named Get…, since that implies that you get a value out. I suggest renaming it to FetchSkyImage.

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<IDalamudTextureWrap?> 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<Dictionary<Weather, IDalamudTextureWrap?>> GetZoneWeatherAndIcons(ushort id, CancellationToken token) {
await Task.Yield();

PluginLog.Verbose($"Retrieving weather data for territory: {id}");

var result = new Dictionary<Weather, IDalamudTextureWrap?>();

var territory = Services.DataManager.GetExcelSheet<TerritoryType>()?.GetRow(id);
if (territory == null || token.IsCancellationRequested) return result;

var weatherRate = Services.DataManager.GetExcelSheet<WeatherRate>()?.GetRow(territory.WeatherRate);
if (token.IsCancellationRequested) return result;
var weatherSheet = Services.DataManager.GetExcelSheet<Weather>();
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;
}
}
}
79 changes: 6 additions & 73 deletions Ktisis/Interface/Windows/Workspace/Tabs/WorldTab.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -223,45 +196,5 @@ private static bool DrawSkySelect(ref uint skyId) {

return changed;
}

// Data

private static async Task<IDalamudTextureWrap?> 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<Dictionary<Weather, IDalamudTextureWrap?>> GetZoneWeatherAndIcons(ushort id, CancellationToken token) {
await Task.Yield();

PluginLog.Verbose($"Retrieving weather data for territory: {id}");

var result = new Dictionary<Weather, IDalamudTextureWrap?>();

var territory = Services.DataManager.GetExcelSheet<TerritoryType>()?.GetRow(id);
if (territory == null || token.IsCancellationRequested) return result;

var weatherRate = Services.DataManager.GetExcelSheet<WeatherRate>()?.GetRow(territory.WeatherRate);
if (token.IsCancellationRequested) return result;
var weatherSheet = Services.DataManager.GetExcelSheet<Weather>();
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;
}
}
}

0 comments on commit 93b9117

Please sign in to comment.