get packs from server

This commit is contained in:
Anna 2022-09-07 23:13:55 -04:00
parent 12f571d59c
commit 926157d57b
6 changed files with 50 additions and 41 deletions

View File

@ -3,7 +3,7 @@ using System.Net.Http.Headers;
namespace OrangeGuidanceTomestone.Helpers;
internal static class ServerHelper {
internal static HttpRequestMessage GetRequest(string apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
internal static HttpRequestMessage GetRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
if (!tail.StartsWith('/')) {
tail = '/' + tail;
}
@ -18,12 +18,14 @@ internal static class ServerHelper {
}
}
req.Headers.Add("X-Api-Key", apiKey);
if (apiKey != null) {
req.Headers.Add("X-Api-Key", apiKey);
}
return req;
}
internal static async Task<HttpResponseMessage> SendRequest(string apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
internal static async Task<HttpResponseMessage> SendRequest(string? apiKey, HttpMethod method, string tail, string? contentType = null, HttpContent? content = null) {
var req = GetRequest(apiKey, method, tail, contentType, content);
return await new HttpClient().SendAsync(req, HttpCompletionOption.ResponseHeadersRead);
}

View File

@ -61,12 +61,9 @@
<PackageReference Include="DalamudPackager" Version="2.1.8"/>
<PackageReference Include="Fody" Version="6.6.3" PrivateAssets="all"/>
<PackageReference Include="Resourcer.Fody" Version="1.8.0" PrivateAssets="all"/>
<PackageReference Include="YamlDotNet" Version="12.0.0"/>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="../server/packs/*.yaml" LinkBase="packs"/>
<EmbeddedResource Remove="../server/packs/*_old*.yaml"/>
<EmbeddedResource Include="vfx/b0941trp1*_o.avfx"/>
<EmbeddedResource Include="img/sign_*.jpg"/>
</ItemGroup>

View File

@ -1,37 +1,29 @@
using Dalamud.Logging;
using YamlDotNet.Serialization.NamingConventions;
using Newtonsoft.Json;
using OrangeGuidanceTomestone.Helpers;
namespace OrangeGuidanceTomestone;
[Serializable]
public class Pack {
internal static Lazy<Pack[]> All { get; } = new(() => {
var des = new YamlDotNet.Serialization.DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
return new[] {
"ffxiv",
"elden-ring",
"bloodborne",
"dark-souls",
}
.Select(name => {
try {
return des.Deserialize<Pack>(Resourcer.Resource.AsStringUnChecked($"OrangeGuidanceTomestone.packs.{name}.yaml"));
} catch (Exception ex) {
PluginLog.LogError(ex, name);
return null;
}
})
.Where(pack => pack != null)
.ToArray()!;
});
internal static SemaphoreSlim AllMutex { get; } = new(1, 1);
internal static Pack[] All { get; set; } = Array.Empty<Pack>();
public string Name { get; init; }
public Guid Id { get; init; }
public string[] Templates { get; init; }
public string[] Conjunctions { get; init; }
public List<WordList> Words { get; init; }
internal static void UpdatePacks() {
Task.Run(async () => {
var resp = await ServerHelper.SendRequest(null, HttpMethod.Get, "/packs");
var json = await resp.Content.ReadAsStringAsync();
var packs = JsonConvert.DeserializeObject<Pack[]>(json)!;
await AllMutex.WaitAsync();
All = packs;
AllMutex.Release();
});
}
}
[Serializable]

View File

@ -89,6 +89,10 @@ internal class Settings : ITab {
}
private void DrawWriter(ref bool anyChanged, ref bool vfx) {
if (ImGui.Button("Refresh packs")) {
Pack.UpdatePacks();
}
var glyph = this.Plugin.Config.DefaultGlyph + 1;
if (ImGui.InputInt("Default glyph", ref glyph)) {
this.Plugin.Config.DefaultGlyph = Math.Min(4, Math.Max(0, glyph - 1));

View File

@ -38,6 +38,7 @@ internal class Write : ITab {
this.LoadSignImages();
this._glyph = this.Plugin.Config.DefaultGlyph;
Pack.UpdatePacks();
}
public void Dispose() {
@ -47,10 +48,29 @@ internal class Write : ITab {
}
public void Draw() {
var packPrev = Pack.All.Value[this._pack].Name;
Pack.AllMutex.Wait();
try {
this.DrawInner();
} finally {
Pack.AllMutex.Release();
}
}
private void DrawInner() {
if (Pack.All.Length == 0) {
ImGui.TextUnformatted("Please refresh the packs from the settings.");
return;
}
if (this._pack < 0 || this._pack >= Pack.All.Length) {
this._pack = 0;
}
var packPrev = Pack.All[this._pack].Name;
if (ImGui.BeginCombo("Pack", packPrev)) {
for (var i = 0; i < Pack.All.Value.Length; i++) {
var selPack = Pack.All.Value[i];
for (var i = 0; i < Pack.All.Length; i++) {
var selPack = Pack.All[i];
if (!ImGui.Selectable(selPack.Name)) {
continue;
}
@ -108,7 +128,7 @@ internal class Write : ITab {
ImGui.EndCombo();
}
var pack = Pack.All.Value[this._pack];
var pack = Pack.All[this._pack];
var lineHeight = ImGui.CalcTextSize("A").Y;
var imageHeight = lineHeight * 4;
@ -276,7 +296,7 @@ internal class Write : ITab {
this._part1 = -1;
}
var pack = Pack.All.Value[this._pack];
var pack = Pack.All[this._pack];
if (this._part1 == -1 || !pack.Templates[this._part1].Contains("{0}")) {
this._word1 = (-1, -1);
@ -296,7 +316,7 @@ internal class Write : ITab {
return false;
}
var pack = Pack.All.Value[this._pack];
var pack = Pack.All[this._pack];
var template1 = pack.Templates[this._part1];
var temp1Variable = template1.Contains("{0}");

6
client/packages.lock.json Executable file → Normal file
View File

@ -24,12 +24,6 @@
"NETStandard.Library": "1.6.1"
}
},
"YamlDotNet": {
"type": "Direct",
"requested": "[12.0.0, )",
"resolved": "12.0.0",
"contentHash": "sMp20zI63j7xVoor98vGeDxteLeyuAlM330L7XbXxMMPjLdP1mwoK/5Nn/8Vldc9hUMN5otkg88bVLEtQnPdaw=="
},
"Microsoft.NETCore.Platforms": {
"type": "Transitive",
"resolved": "1.1.0",