Skip to content

Commit

Permalink
[PR] New release
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfcomp authored Jul 1, 2024
2 parents 3788db5 + c931c14 commit d73af20
Show file tree
Hide file tree
Showing 16 changed files with 94 additions and 225 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/PRD17.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- master

env:
PUBLIC_NAME: DeepDungeonDex
PUBLIC_NAME: MonsterDex
INTERNAL_NAME: DeepDungeonDex
GITHUB_REPOSITORY_OWNER: $GITHUB_REPOSITORY_OWNER
GITHUB_SERVER_URL: $GITHUB_SERVER_URL
Expand Down
176 changes: 32 additions & 144 deletions DeepDungeonDex/Font/Font.cs
Original file line number Diff line number Diff line change
@@ -1,164 +1,52 @@
using System.IO;
using System.Runtime.InteropServices;
using Configuration = DeepDungeonDex.Models.Configuration;
using System.Drawing;
using System.IO;
using Dalamud.Interface.ManagedFontAtlas;

namespace DeepDungeonDex.Font;

internal class Font : IDisposable
public class Font : IDisposable
{
private readonly StorageHandler _handler;
private readonly IPluginLog _log;
private ImFontConfigPtr _fontCfg;
private ImFontConfigPtr _fontCfgMerge;
private GCHandle _ranges;
private GCHandle _jpRanges;
private GCHandle _krRanges;
private GCHandle _tcRanges;
private GCHandle _scRanges;
private (GCHandle, int) _regularFont;
private (GCHandle, int) _jpFont;
private (GCHandle, int) _krFont;
private (GCHandle, int) _tcFont;
private (GCHandle, int) _scFont;
private GCHandle _symRange = GCHandle.Alloc(
new ushort[] {
0xE020,
0xE0DB,
0,
},
GCHandleType.Pinned
);
internal static ImFontPtr RegularFont;
private readonly IFontAtlas _atlas;
internal static IFontHandle RegularFont;
internal float FontSize = 16;

public unsafe Font(StorageHandler handler, IPluginLog log)
public Font(IPluginLog log, IFontAtlas atlas)
{
_handler = handler;
_log = log;
SetUpRanges();
SetUpFonts();
_fontCfg = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()) { FontDataOwnedByAtlas = false };
_fontCfgMerge = new ImFontConfigPtr(ImGuiNative.ImFontConfig_ImFontConfig()) { FontDataOwnedByAtlas = false, MergeMode = true };
}

public byte[] GetResource(string path)
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)!;
using var memory = new MemoryStream();
stream.CopyTo(memory);
return memory.ToArray();
}

public ushort[] GetRange(string path)
{
using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(path)!;
using var reader = new BinaryReader(stream);
var tableDirectory = TableDirectory.Read(reader);
var tables = new List<TableRecord>();
for (var i = 0; i < tableDirectory.NumTables; i++)
{
tables.Add(TableRecord.Read(reader));
}
var cmap = tables.First(x => x.Tag == "cmap");
var cmapTable = CmapTable.Read(reader, cmap.Offset);
var cmapEncodingRecords = cmapTable.Subtables.First(x => x.PlatformID == 3 && x.EncodingID == 1);
var cmapEncodingOffset = cmapEncodingRecords.Offset + cmap.Offset;
var cmapFormat = (CmapFormat4)CmapFormat.Read(reader, cmapEncodingOffset);
var charCodes = new List<ushort>();
for (var i = 0; i < cmapFormat.SegCountX2 / 2; i++)
{
var first = cmapFormat.StartCodes[i];
var last = cmapFormat.EndCodes[i];
first = Math.Clamp(first, (ushort)1, (ushort)0xFFFF);
if (first > last)
continue;
last = Math.Clamp(last, (ushort)1, (ushort)0xFFFF);
if (first > last)
continue;
charCodes.Add(first);
charCodes.Add(last);
}
charCodes.Add(0);
return charCodes.ToArray();
}

public void SetUpRanges()
{
_ranges = GCHandle.Alloc(GetRange("DeepDungeonDex.Font.NotoSans-Regular.ttf"), GCHandleType.Pinned);
_jpRanges = GCHandle.Alloc(GetRange("DeepDungeonDex.Font.NotoSansJP-Regular.otf"), GCHandleType.Pinned);
_krRanges = GCHandle.Alloc(GetRange("DeepDungeonDex.Font.NotoSansKR-Regular.otf"), GCHandleType.Pinned);
_tcRanges = GCHandle.Alloc(GetRange("DeepDungeonDex.Font.NotoSansTC-Regular.otf"), GCHandleType.Pinned);
_scRanges = GCHandle.Alloc(GetRange("DeepDungeonDex.Font.NotoSansSC-Regular.otf"), GCHandleType.Pinned);
}

public void LoadFontFile(string path, ref (GCHandle, int) font)
{
if (font.Item1.IsAllocated)
font.Item1.Free();
var file = GetResource(path);
font = (GCHandle.Alloc(file, GCHandleType.Pinned), file.Length);
}

public void SetUpFonts()
{
var config = _handler.GetInstance<Configuration>()!;
LoadFontFile("DeepDungeonDex.Font.NotoSans-Regular.ttf", ref _regularFont);
SetUpSpecificFonts(config);
}

public void SetUpSpecificFonts(Configuration config)
{
FreeFonts(_jpFont, _krFont, _scFont, _tcFont);
LoadFontFile("DeepDungeonDex.Font.NotoSansJP-Regular.otf", ref _jpFont);
LoadFontFile("DeepDungeonDex.Font.NotoSansSC-Regular.otf", ref _scFont);
LoadFontFile("DeepDungeonDex.Font.NotoSansTC-Regular.otf", ref _tcFont);
LoadFontFile("DeepDungeonDex.Font.NotoSansKR-Regular.otf", ref _krFont);
}

public void FreeFonts(params (GCHandle, int)[] fonts)
{
foreach (var font in fonts)
_atlas = atlas;
RegularFont = _atlas.NewDelegateFontHandle(step =>
{
if (font.Item1.IsAllocated)
font.Item1.Free();
}
step.OnPreBuild(act =>
{
var conf = new SafeFontConfig
{
SizePx = FontSize
};
conf.MergeFont = act.AddFontFromStream(GetResourceStream("DeepDungeonDex.Font.NotoSans-Regular.ttf"), conf, false, "DeepDungeonDex-NotoSans-Regular");
conf.MergeFont = act.AddFontFromStream(GetResourceStream("DeepDungeonDex.Font.NotoSansJP-Regular.otf"), conf, false, "DeepDungeonDex-NotoSansJP-Regular");
conf.MergeFont = act.AddFontFromStream(GetResourceStream("DeepDungeonDex.Font.NotoSansKR-Regular.otf"), conf, false, "DeepDungeonDex-NotoSansKR-Regular");
conf.MergeFont = act.AddFontFromStream(GetResourceStream("DeepDungeonDex.Font.NotoSansTC-Regular.otf"), conf, false, "DeepDungeonDex-NotoSansTC-Regular");
conf.MergeFont = act.AddFontFromStream(GetResourceStream("DeepDungeonDex.Font.NotoSansSC-Regular.otf"), conf, false, "DeepDungeonDex-NotoSansSC-Regular");
conf.MergeFont = act.AddGameSymbol(conf);
conf.MergeFont = act.SetFontScaleMode(conf.MergeFont, FontScaleMode.UndoGlobalScale);
});
});
}

public void FreeGcHandles(params GCHandle[] handles)
public void RegisterNewBuild(float size)
{
foreach (var gcHandle in handles)
{
if (gcHandle.IsAllocated)
gcHandle.Free();
}
FontSize = size;
_atlas.BuildFontsAsync();
}

public ImFontPtr AddFont((GCHandle, int) font, float scale, ImFontConfigPtr cfg, GCHandle ranges)
public Stream GetResourceStream(string path)
{
return ImGui.GetIO().Fonts.AddFontFromMemoryTTF(font.Item1.AddrOfPinnedObject(), font.Item2, scale, cfg, ranges.AddrOfPinnedObject());
return Assembly.GetExecutingAssembly().GetManifestResourceStream(path)!;
}

public void BuildFonts(float scale)
public void Dispose()
{
var config = _handler.GetInstance<Configuration>()!;
RegularFont = AddFont(_regularFont, scale, _fontCfg, _ranges);
if (config.Locale == 1 || config.LoadAll)
AddFont(_jpFont, scale, _fontCfgMerge, _jpRanges);
if (config.Locale == 4 || config.LoadAll)
AddFont(_scFont, scale, _fontCfgMerge, _scRanges);
if (config.Locale == 5 || config.LoadAll)
AddFont(_tcFont, scale, _fontCfgMerge, _tcRanges);
if (config.Locale == 6 || config.LoadAll)
AddFont(_krFont, scale, _fontCfgMerge, _krRanges);
}

public unsafe void Dispose()
{
FreeFonts(_regularFont, _jpFont, _krFont, _scFont, _tcFont);
FreeGcHandles(_symRange, _ranges, _jpRanges, _krRanges, _tcRanges, _scRanges);

if (_fontCfg.NativePtr != null)
_fontCfg.Destroy();
if (_fontCfgMerge.NativePtr != null)
_fontCfgMerge.Destroy();
RegularFont.Dispose();
}
}
3 changes: 2 additions & 1 deletion DeepDungeonDex/Hooks/AddonAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Runtime.InteropServices;
using FFXIVClientStructs.FFXIV.Client.Game.InstanceContent;
using FFXIVClientStructs.FFXIV.Client.Graphics.Environment;
using ContentType = DeepDungeonDex.Storage.ContentType;

namespace DeepDungeonDex.Hooks;

Expand Down Expand Up @@ -42,7 +43,7 @@ private void CheckWeather()
if(_envManager->EnvScene == null)
return;

_envManager->EnvScene->WeatherIdsSpan.CopyTo(WeatherIds);
WeatherIds = _envManager->EnvScene->WeatherIds.ToArray();
}

private void CheckDirector()
Expand Down
29 changes: 12 additions & 17 deletions DeepDungeonDex/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Dalamud.Game.ClientState.Objects;
using Dalamud.Interface.ManagedFontAtlas;
using Dalamud.IoC;
using DeepDungeonDex.Hooks;
using DeepDungeonDex.Weather;
Expand All @@ -15,19 +16,16 @@ public class Main : IDalamudPlugin

internal static ConcurrentBag<IDisposable> Services = new();

public Main(DalamudPluginInterface pluginInterface)
public Main(IDalamudPluginInterface pluginInterface)
{
_provider = BuildProvider(this, pluginInterface);
_provider.GetRequiredService<Requests>();
_provider.GetRequiredService<StorageHandler>().GetInstance<Configuration>()!.OnSizeChange += pluginInterface.UiBuilder.RebuildFonts;
#pragma warning disable CS4014
_provider.GetRequiredService<CommandHandler>().AddCommand(new[] { "refresh", "clear" }, () => { RefreshData(); }, "Refreshes the data internally stored");
#pragma warning restore CS4014

var sys = LoadWindows();
pluginInterface.UiBuilder.Draw += sys.Draw;
pluginInterface.UiBuilder.BuildFonts += BuildFont;
pluginInterface.UiBuilder.RebuildFonts();
}

public async Task RefreshData()
Expand All @@ -37,22 +35,15 @@ public async Task RefreshData()
await req.RefreshLang(false);
}

public void BuildFont()
{
_provider.GetRequiredService<Font.Font>().BuildFonts(_provider.GetRequiredService<StorageHandler>().GetInstance<Configuration>()?.FontSizeScaled ?? 1f);
var conf = _provider.GetRequiredService<StorageHandler>().GetInstance<Configuration>();
conf?.OnChange?.Invoke(conf);
}

public void Dispose()
{
if (isDisposed)
return;
isDisposed = true;
_provider.GetRequiredService<WindowSystem>().DisposeAndRemoveAllWindows();
_provider.GetRequiredService<DalamudPluginInterface>().UiBuilder.Draw -= _provider.GetRequiredService<WindowSystem>().Draw;
_provider.GetRequiredService<StorageHandler>().GetInstance<Configuration>()!.OnSizeChange -= _provider.GetRequiredService<DalamudPluginInterface>().UiBuilder.RebuildFonts;
_provider.GetRequiredService<DalamudPluginInterface>().UiBuilder.BuildFonts -= BuildFont;
_provider.GetRequiredService<IDalamudPluginInterface>().UiBuilder.Draw -= _provider.GetRequiredService<WindowSystem>().Draw;
// _provider.GetRequiredService<StorageHandler>().GetInstance<Configuration>()!.OnSizeChange -= _provider.GetRequiredService<DalamudPluginInterface>().UiBuilder.RebuildFonts;
// _provider.GetRequiredService<DalamudPluginInterface>().UiBuilder.BuildFonts -= BuildFont;
_provider.DisposeDI();
_provider = null!;
foreach (var dalamudServiceIntermediate in Services)
Expand All @@ -78,8 +69,11 @@ public WindowSystem LoadWindows()
return sys;
}

private static ServiceProvider BuildProvider(Main main, DalamudPluginInterface pluginInterface)
private static ServiceProvider BuildProvider(Main main, IDalamudPluginInterface pluginInterface)
{
var fontAtlas =
pluginInterface.UiBuilder.CreateFontAtlas(FontAtlasAutoRebuildMode.Async, false, "DeepDungeonDexFontAtlas");

return new ServiceCollection()
.AddSingleton(pluginInterface)
.AddDalamudService<IFramework>()
Expand All @@ -91,6 +85,7 @@ private static ServiceProvider BuildProvider(Main main, DalamudPluginInterface p
.AddDalamudService<ITextureProvider>()
.AddDalamudService<IDataManager>()
.AddDalamudService<IPluginLog>()
.AddSingleton(fontAtlas)
.AddSingleton(new WindowSystem("DeepDungeonDex"))
.AddSingleton(main)
.AddSingleton(provider => ActivatorUtilities.CreateInstance<StorageHandler>(provider))
Expand Down Expand Up @@ -119,7 +114,7 @@ public static IServiceCollection AddDalamudService<T>(this IServiceCollection co
{
return collection.AddSingleton(provider =>
{
var k = new DalamudServiceIntermediate<T>(provider.GetRequiredService<DalamudPluginInterface>());
var k = new DalamudServiceIntermediate<T>(provider.GetRequiredService<IDalamudPluginInterface>());
return k.Service;
});
}
Expand Down Expand Up @@ -158,7 +153,7 @@ public DalamudServiceIntermediate(T service)
Service = service;
}

public DalamudServiceIntermediate(DalamudPluginInterface pluginInterface)
public DalamudServiceIntermediate(IDalamudPluginInterface pluginInterface)
{
pluginInterface.Inject(this);
Main.Services.Add(this);
Expand Down
9 changes: 0 additions & 9 deletions DeepDungeonDex/Models/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,16 @@ public class Configuration
public float Opacity { get; set; } = 1f;
public ContentType EnabledContentTypes { get; set; } = ContentType.DeepDungeon;

[JsonIgnore] public int PrevFontSize;
[JsonIgnore] public int PrevLocale;
[JsonIgnore] public float RemoveScaling => 1 / ImGui.GetIO().FontGlobalScale;
[JsonIgnore] public float FontSizeScaled => FontSize * RemoveScaling;
[JsonIgnore] public float WindowSizeScaled => Math.Max(FontSize / 16f, 1f) * RemoveScaling;
[JsonIgnore] public Action<Configuration>? OnChange { get; set; }
[JsonIgnore] public Action? OnSizeChange { get; set; }

public void Save(string path)
{
if (FontSize != PrevFontSize)
{
PrevFontSize = FontSize;
OnSizeChange?.Invoke();
}
if (PrevLocale != Locale && !LoadAll)
{
PrevLocale = Locale;
OnSizeChange?.Invoke();
}
OnChange?.Invoke(this);
var origPath = path;
Expand Down
4 changes: 3 additions & 1 deletion DeepDungeonDex/Requests/Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ public partial class Requests : IDisposable
private IPluginLog _log;
private bool _loadedOnce;
public HttpClient Client = new();
public const string BaseUrl = "https://raw.githubusercontent.com/wolfcomp/DeepDungeonDex/data";
public const string BaseUrl = "https://raw.githubusercontent.com/wolfcomp/MonsterDex/data";
public TimeSpan CacheTime = TimeSpan.FromHours(6);
public StorageHandler Handler;
public bool RequestingData { get; private set; }
public bool RequestingLang { get; private set; }
public bool IsRequesting => RequestingData || RequestingLang;
public Action? IsRequestDone = null;
#if RELEASE
private const bool Debug = false;
#else
Expand Down Expand Up @@ -92,6 +93,7 @@ public async Task RefreshFileList(bool continuous = true)
}

_loadedOnce = true;
IsRequestDone?.Invoke();

RefreshEnd:

Expand Down
7 changes: 6 additions & 1 deletion DeepDungeonDex/Storage/MobData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public string[][]? Description
public Threat Threat { get; set; }
public ContentType InstanceContentType { get; set; }
public Dictionary<uint, ElementalChangeTime> ElementalChangeTimes { get; set; } = new();
public Character.EurekaElement MutatedElementalType { get; set; }
public ElementalType MutatedElementalType { get; set; }
public bool IsMutation { get; set; }
public bool IsAaptation { get; set; }

Expand Down Expand Up @@ -178,6 +178,11 @@ public enum ElementalChangeTime : byte
Both = Night | Day
}

public enum ElementalType : byte
{

}

public static class MobDataExtensions
{
public static Mob? GetData(this MobData data, uint key)
Expand Down
Loading

0 comments on commit d73af20

Please sign in to comment.