Skip to content

Commit

Permalink
chore: update for 6.3/api8
Browse files Browse the repository at this point in the history
  • Loading branch information
zhudotexe committed Jan 17, 2023
1 parent 1c737d1 commit 733f7f1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ bin/
packages/
Paissa/Secrets.cs
logs/
packages.lock.json
58 changes: 16 additions & 42 deletions AutoSweep.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
<PropertyGroup>
<Title>autoSweep</Title>
<Authors>zhudotexe</Authors>
<TargetFramework>net6.0-windows</TargetFramework>
<TargetFramework>net7.0-windows</TargetFramework>
<LangVersion>9</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Deterministic>true</Deterministic>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<OutputPath>bin\$(Configuration)\</OutputPath>
<DalamudLibPath>$(appdata)\XIVLauncher\addon\Hooks\dev\</DalamudLibPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<AssemblyName>autoSweep</AssemblyName>
<AssemblyVersion>1.4.1.0</AssemblyVersion>
<AssemblyVersion>1.4.2.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup>
<DalamudLibPath>$(appdata)\XIVLauncher\addon\Hooks\dev\</DalamudLibPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand All @@ -38,71 +42,41 @@

<ItemGroup>
<Reference Include="Dalamud">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Dalamud.dll</HintPath>
<HintPath>$(DalamudLibPath)Dalamud.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ImGui.NET">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\ImGui.NET.dll</HintPath>
<HintPath>$(DalamudLibPath)ImGui.NET.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ImGuiScene">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\ImGuiScene.dll</HintPath>
<HintPath>$(DalamudLibPath)ImGuiScene.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Lumina">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Lumina.dll</HintPath>
<HintPath>$(DalamudLibPath)Lumina.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Lumina.Excel">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Lumina.Excel.dll</HintPath>
<HintPath>$(DalamudLibPath)Lumina.Excel.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>$(AppData)\XIVLauncher\addon\Hooks\dev\Newtonsoft.Json.dll</HintPath>
<HintPath>$(DalamudLibPath)Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="DalamudPackager" Version="2.1.8" />
<PackageReference Include="DalamudPackager" Version="2.1.10" />
<PackageReference Include="DebounceThrottle" Version="1.0.0" />
<PackageReference Include="ILRepack" Version="2.0.18" />
<PackageReference Include="JWT" Version="9.0.3" />
<PackageReference Include="WebSocketSharp.Standard" Version="1.0.3" />
</ItemGroup>

<ItemGroup>
<Content Include="autoSweep.json">
<Content Include="images\icon.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>false</Visible>
</Content>
</ItemGroup>


<Target Name="PostBuild" AfterTargets="Build">
<ItemGroup>
<InputAssemblies Include="$(TargetPath)" />
<InputAssemblies Include="$(TargetDir)*.dll" Exclude="$(TargetPath)" />
</ItemGroup>
<Exec Command="$(PkgILRepack)\tools\ILRepack.exe /union /lib:$(DalamudLibPath) /out:$(TargetDir)$(TargetFileName) @(InputAssemblies, ' ')" />
</Target>

<Target Name="PostBuild_Debug" AfterTargets="PostBuild" Condition="'$(Configuration)' == 'Debug'">
<ItemGroup>
<CleanAssemblies Include="$(TargetDir)*.dll" Exclude="$(TargetDir)$(TargetFileName)" />
</ItemGroup>
<Delete Files="@(CleanAssemblies)" />
</Target>

<Target Name="PostBuild_Release" AfterTargets="PostBuild" Condition=" '$(Configuration)' == 'Release' ">
<MakeDir Directories="$(TargetDir)out\" />
<Exec Command="copy $(TargetDir)$(TargetFileName) $(TargetDir)out\$(TargetFileName)" />
<ItemGroup>
<DeleteFiles Include="$(TargetDir)out\*.dll" Exclude="$(TargetDir)out\$(TargetFileName)" />
</ItemGroup>
<Delete Files="@(DeleteFiles)" />
<DalamudPackager ProjectDir="$(ProjectDir)" OutputPath="$(OutputPath)out\" AssemblyName="$(AssemblyName)" VersionComponents="4" MakeZip="true" />
<MakeDir Directories="$(TargetDir)out\$(AssemblyName)\images" />
<Exec Command="copy $(ProjectDir)\images\*.png $(TargetDir)out\$(AssemblyName)\images\*.png" />
</Target>

</Project>
70 changes: 35 additions & 35 deletions Paissa/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@
using Dalamud.Game.Gui;
using Dalamud.Logging;
using DebounceThrottle;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json;
using WebSocketSharp;

namespace AutoSweep.Paissa {
public class PaissaClient : IDisposable {
private readonly HttpClient http;
private WebSocket ws;
private readonly JwtEncoder encoder = new(new HMACSHA256Algorithm(), new JsonNetSerializer(), new JwtBase64UrlEncoder());
private bool disposed = false;
private string sessionToken;

// dalamud
private readonly ClientState clientState;
Expand All @@ -40,7 +37,6 @@ public class PaissaClient : IDisposable {
private const string wsRoute = "wss://paissadb.zhu.codes/ws";
#endif

private readonly byte[] secret = Encoding.UTF8.GetBytes(Secrets.JwtSecret);

public event EventHandler<PlotOpenedEventArgs> OnPlotOpened;
public event EventHandler<PlotUpdateEventArgs> OnPlotUpdate;
Expand All @@ -61,9 +57,9 @@ public void Dispose() {

// ==== Interface ====
/// <summary>
/// Fire and forget a POST request to register the current character's content ID.
/// Make a POST request to register the current character's content ID.
/// </summary>
public void Hello() {
public async Task Hello() {
PlayerCharacter player = clientState.LocalPlayer;
if (player == null)
return;
Expand All @@ -75,7 +71,12 @@ public void Hello() {
};
string content = JsonConvert.SerializeObject(charInfo);
PluginLog.Debug(content);
PostFireAndForget("/hello", content);
var response = await Post("/hello", content, false);
if (response.IsSuccessStatusCode) {
string respText = await response.Content.ReadAsStringAsync();
sessionToken = JsonConvert.DeserializeObject<HelloResponse>(respText).session_token;
PluginLog.Log("Completed PaissaDB HELLO");
}
}

/// <summary>
Expand Down Expand Up @@ -139,20 +140,33 @@ private void queueIngest(object data) {
});
}

private async void PostFireAndForget(string route, string content) {
await PostFireAndForget(route, content, 5);
private async void PostFireAndForget(string route, string content, bool auth = true, ushort retries = 5) {
await Post(route, content, auth, retries);
}

private async Task PostFireAndForget(string route, string content, ushort retries) {
private async Task<HttpResponseMessage> Post(string route, string content, bool auth = true, ushort retries = 5) {
HttpResponseMessage response = null;
PluginLog.Verbose(content);

for (var i = 0; i < retries; i++) {
var request = new HttpRequestMessage(HttpMethod.Post, $"{apiBase}{route}") {
Content = new StringContent(content, Encoding.UTF8, "application/json"),
Headers = {
Authorization = new AuthenticationHeaderValue("Bearer", GenerateJwt())
HttpRequestMessage request;
if (auth) {
if (sessionToken == null) {
PluginLog.LogWarning("Trying to send authed request but no session token!");
await Hello();
continue;
}
};
request = new HttpRequestMessage(HttpMethod.Post, $"{apiBase}{route}") {
Content = new StringContent(content, Encoding.UTF8, "application/json"),
Headers = {
Authorization = new AuthenticationHeaderValue("Bearer", sessionToken)
}
};
} else {
request = new HttpRequestMessage(HttpMethod.Post, $"{apiBase}{route}") {
Content = new StringContent(content, Encoding.UTF8, "application/json"),
};
}
try {
response = await http.SendAsync(request);
PluginLog.Debug($"{request.Method} {request.RequestUri} returned {response.StatusCode} ({response.ReasonPhrase})");
Expand All @@ -174,18 +188,20 @@ private async Task PostFireAndForget(string route, string content, ushort retrie
}

// todo better error handling
if (response == null)
if (response == null) {
chat.PrintError("There was an error connecting to PaissaDB.");
else if (!response.IsSuccessStatusCode)
} else if (!response.IsSuccessStatusCode) {
chat.PrintError($"There was an error connecting to PaissaDB: {response.ReasonPhrase}");
}
return response;
}


// ==== WebSocket ====
private void ReconnectWS() {
Task.Run(() => {
ws?.Close(1000);
ws = new WebSocket(GetWSRouteWithAuth());
ws = new WebSocket(wsRoute);
ws.OnOpen += OnWSOpen;
ws.OnMessage += OnWSMessage;
ws.OnClose += OnWSClose;
Expand Down Expand Up @@ -249,21 +265,5 @@ private void WSReconnectSoon() {
if (!disposed) ReconnectWS();
});
}


// ==== helpers ====
private string GenerateJwt() {
var payload = new Dictionary<string, object> {
{ "cid", clientState.LocalContentId },
{ "aud", "PaissaHouse" },
{ "iss", "PaissaDB" },
{ "iat", DateTimeOffset.Now.ToUnixTimeSeconds() }
};
return encoder.Encode(payload, secret);
}

private string GetWSRouteWithAuth() {
return $"{wsRoute}?jwt={GenerateJwt()}";
}
}
}
2 changes: 1 addition & 1 deletion Paissa/LotteryObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ long a8

PluginLog.LogDebug(
$"Got PlacardSaleInfo: PurchaseType={saleInfo.PurchaseType}, TenantType={saleInfo.TenantType}, available={saleInfo.AvailabilityType}, until={saleInfo.PhaseEndsAt}, numEntries={saleInfo.EntryCount}");
PluginLog.LogDebug($"unknown1={saleInfo.Unknown1}, unknown2={saleInfo.Unknown2}, unknown3={BitConverter.ToString(saleInfo.Unknown3)}");
PluginLog.LogDebug($"unknown1={saleInfo.Unknown1}, unknown2={saleInfo.Unknown2}, unknown3={saleInfo.Unknown3}, unknown4={BitConverter.ToString(saleInfo.Unknown4)}");
PluginLog.LogDebug(
$"housingType={housingType}, territoryTypeId={territoryTypeId}, wardId={wardId}, plotId={plotId}, apartmentNumber={apartmentNumber}, placardSaleInfoPtr={placardSaleInfoPtr}, a8={a8}");

Expand Down
6 changes: 6 additions & 0 deletions Paissa/Schema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class WSMessage {
public JObject Data { get; set; }
}

public class HelloResponse {
public string message { get; set; }
public double server_time { get; set; }
public string session_token { get; set; }
}

public class DistrictDetail {
public ushort district_id { get; set; }
public string name { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Paissa/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Utils {
// configuration constants
public const string CommandName = "/psweep";
public const string HouseCommandName = "/phouse";
public const int NumWardsPerDistrict = 24;
public const int NumWardsPerDistrict = 30;

public static uint TerritoryTypeIdToLandSetId(uint territoryTypeId) {
return territoryTypeId switch {
Expand Down
3 changes: 2 additions & 1 deletion Plugin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using AutoSweep.Paissa;
using AutoSweep.Structures;
using Dalamud.Data;
Expand Down Expand Up @@ -144,7 +145,7 @@ private void OnLogin(object _, EventArgs __) {
private void OnUpdateEvent(Framework f) {
if (clientNeedsHello && ClientState?.LocalPlayer != null && PaissaClient != null) {
clientNeedsHello = false;
PaissaClient.Hello();
Task.Run(async () => await PaissaClient.Hello());
}
}

Expand Down
14 changes: 8 additions & 6 deletions Structures/Lottery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ public class PlacardSaleInfo {
public TenantType TenantType; // 0x21
public AvailabilityType AvailabilityType; // 0x22
public byte Unknown1; // 0x23
public uint PhaseEndsAt; // 0x24 - 0x27
public uint Unknown2; // 0x28 - 0x2B
public uint EntryCount; // 0x2C - 0x2F
public byte[] Unknown3; // 0x30 - 0x3F
public uint Unknown2; // 0x24 - 0x27
public uint PhaseEndsAt; // 0x28 - 0x2B
public uint Unknown3; // 0x2C - 0x2F
public uint EntryCount; // 0x30 - 0x33
public byte[] Unknown4; // 0x34 - 0x4B

public static unsafe PlacardSaleInfo Read(IntPtr dataPtr) {
var saleInfo = new PlacardSaleInfo();
Expand All @@ -20,10 +21,11 @@ public static unsafe PlacardSaleInfo Read(IntPtr dataPtr) {
saleInfo.TenantType = (TenantType)binaryReader.ReadByte();
saleInfo.AvailabilityType = (AvailabilityType)binaryReader.ReadByte();
saleInfo.Unknown1 = binaryReader.ReadByte();
saleInfo.PhaseEndsAt = binaryReader.ReadUInt32();
saleInfo.Unknown2 = binaryReader.ReadUInt32();
saleInfo.PhaseEndsAt = binaryReader.ReadUInt32();
saleInfo.Unknown3 = binaryReader.ReadUInt32();
saleInfo.EntryCount = binaryReader.ReadUInt32();
saleInfo.Unknown3 = binaryReader.ReadBytes(16);
saleInfo.Unknown4 = binaryReader.ReadBytes(16);
return saleInfo;
}
}
Expand Down
3 changes: 1 addition & 2 deletions autoSweep.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
"housing"
],
"IconUrl": "https://raw.githubusercontent.com/zhudotexe/FFXIV_PaissaHouse/main/images/icon.png",
"Punchline": "Crowdsourced housing alerts and lottery tracking for all.",
"DalamudApiLevel": 7
"Punchline": "Crowdsourced housing alerts and lottery tracking for all."
}

0 comments on commit 733f7f1

Please sign in to comment.