Skip to content

Commit

Permalink
Complete Daybreak.GWCA implementation
Browse files Browse the repository at this point in the history
Fix metrics view
Upgrade dependencies
  • Loading branch information
AlexMacocian committed Oct 26, 2023
1 parent cb01dda commit d24b3cd
Show file tree
Hide file tree
Showing 47 changed files with 1,050 additions and 80 deletions.
1 change: 1 addition & 0 deletions Daybreak.GWCA/header/payloads/PathingTrapezoid.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using json = nlohmann::json;
namespace Daybreak {
struct PathingTrapezoid {
uint32_t Id;
uint32_t PathingMapId;
float XTL;
float XTR;
float XBL;
Expand Down
20 changes: 20 additions & 0 deletions Daybreak.GWCA/source/GameModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace Daybreak::Modules::GameModule {
volatile bool initialized = false;

std::string GetString(wchar_t* chars) {
if (!chars) {
return "";
}

std::string str(64, '\0');
auto length = std::wcstombs(&str[0], chars, 64);
str.resize(length);
Expand All @@ -41,6 +45,10 @@ namespace Daybreak::Modules::GameModule {
std::list<QuestMetadata> GetQuestLog() {
std::list<QuestMetadata> questMetadatas;
auto questLog = GW::QuestMgr::GetQuestLog();
if (!questLog) {
return questMetadatas;
}

for (auto& quest : *questLog) {
QuestMetadata metadata;
metadata.FromId = (uint32_t)quest.map_from;
Expand Down Expand Up @@ -88,6 +96,10 @@ namespace Daybreak::Modules::GameModule {
std::list<GW::AgentLiving> agentList;
const GW::AgentArray* agents_ptr = GW::Agents::GetAgentArray();
GW::AgentArray* agents = GW::Agents::GetAgentArray();
if (!agents) {
return agentList;
}

for (auto* a : *agents) {
const GW::AgentLiving* agent = a ? a->GetAsAgentLiving() : nullptr;
if (agent) {
Expand Down Expand Up @@ -132,6 +144,10 @@ namespace Daybreak::Modules::GameModule {
std::list<Temp::GWPlayer> players;
auto worldContext = GW::GetWorldContext();
Temp::GWPlayer *tempPlayerArray = (Temp::GWPlayer*)worldContext->players.m_buffer;
if (!tempPlayerArray) {
return players;
}

for (auto i = 0; i < worldContext->players.m_size; i++) {
if (tempPlayerArray->agent_id != 0) {
players.push_back(*tempPlayerArray);
Expand All @@ -145,6 +161,10 @@ namespace Daybreak::Modules::GameModule {
std::map<uint32_t, std::list<uint32_t>> GetSkillbars() {
std::map<uint32_t, std::list<uint32_t>> skillbarMap;
auto skillbars = GW::SkillbarMgr::GetSkillbarArray();
if (!skillbars) {
return skillbarMap;
}

for (auto& skillbar : *skillbars) {
std::list<uint32_t> skillList;
skillList.push_back((uint32_t)skillbar.skills[0].skill_id);
Expand Down
20 changes: 20 additions & 0 deletions Daybreak.GWCA/source/MainPlayerModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace Daybreak::Modules::MainPlayerModule {
volatile bool initialized = false;

std::string GetString(wchar_t* chars) {
if (!chars) {
return "";
}

std::string str(64, '\0');
auto length = std::wcstombs(&str[0], chars, 64);
str.resize(length);
Expand All @@ -41,6 +45,10 @@ namespace Daybreak::Modules::MainPlayerModule {
std::list<QuestMetadata> GetQuestLog() {
std::list<QuestMetadata> questMetadatas;
auto questLog = GW::QuestMgr::GetQuestLog();
if (!questLog) {
return questMetadatas;
}

for (auto& quest : *questLog) {
QuestMetadata metadata;
metadata.FromId = (uint32_t)quest.map_from;
Expand Down Expand Up @@ -88,6 +96,10 @@ namespace Daybreak::Modules::MainPlayerModule {
std::list<GW::AgentLiving> agentList;
const GW::AgentArray* agents_ptr = GW::Agents::GetAgentArray();
GW::AgentArray* agents = GW::Agents::GetAgentArray();
if (!agents) {
return agentList;
}

for (auto* a : *agents) {
const GW::AgentLiving* agent = a ? a->GetAsAgentLiving() : nullptr;
if (agent) {
Expand Down Expand Up @@ -132,6 +144,10 @@ namespace Daybreak::Modules::MainPlayerModule {
std::list<Temp::GWPlayer> players;
auto worldContext = GW::GetWorldContext();
Temp::GWPlayer *tempPlayerArray = (Temp::GWPlayer*)worldContext->players.m_buffer;
if (!tempPlayerArray) {
return players;
}

for (auto i = 0; i < worldContext->players.m_size; i++) {
if (tempPlayerArray->agent_id != 0) {
players.push_back(*tempPlayerArray);
Expand All @@ -145,6 +161,10 @@ namespace Daybreak::Modules::MainPlayerModule {
std::map<uint32_t, std::list<uint32_t>> GetSkillbars() {
std::map<uint32_t, std::list<uint32_t>> skillbarMap;
auto skillbars = GW::SkillbarMgr::GetSkillbarArray();
if (!skillbars) {
return skillbarMap;
}

for (auto& skillbar : *skillbars) {
std::list<uint32_t> skillList;
skillList.push_back((uint32_t)skillbar.skills[0].skill_id);
Expand Down
1 change: 1 addition & 0 deletions Daybreak.GWCA/source/PathingModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace Daybreak::Modules::PathingModule {
auto gwTrapezoid = gwPathingMap.trapezoids[j];
PathingTrapezoid trapezoid;
trapezoid.Id = gwTrapezoid.id;
trapezoid.PathingMapId = i;
trapezoid.XTL = gwTrapezoid.XTL;
trapezoid.XBL = gwTrapezoid.XBL;
trapezoid.XTR = gwTrapezoid.XTR;
Expand Down
2 changes: 1 addition & 1 deletion Daybreak.GWCA/source/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static DWORD WINAPI ThreadProc(LPVOID lpModule)
HMODULE hModule = static_cast<HMODULE>(lpModule);
#ifdef BUILD_TYPE_DEBUG
AllocConsole();
SetConsoleTitleA("PacketLogger Console");
SetConsoleTitleA("Daybreak.GWCA Console");
freopen_s(&stdout_proxy, "CONOUT$", "w", stdout);
freopen_s(&stderr_proxy, "CONOUT$", "w", stderr);
#endif
Expand Down
1 change: 1 addition & 0 deletions Daybreak.GWCA/source/payloads/PathingTrapezoid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Daybreak {
j = json
{
{"Id", p.Id},
{"PathingMapId", p.PathingMapId},
{"XTL", p.XTL},
{"XTR", p.XTR},
{"XBL", p.XBL},
Expand Down
9 changes: 5 additions & 4 deletions Daybreak/Converters/AttributeJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Daybreak.Models.Guildwars;
using Newtonsoft.Json;
using System;

namespace Daybreak.Converters;
Expand All @@ -25,9 +26,9 @@ public sealed class AttributeJsonConverter : JsonConverter

return namedAttribute;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Models.Guildwars.Attribute.TryParse(id.Value, out var parsedAttribute))
var id = reader.Value as long?;
if (id is not long ||
!Models.Guildwars.Attribute.TryParse((int)id.Value, out var parsedAttribute))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/CampaignJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class CampaignJsonConverter : JsonConverter

return namedCampaign;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Campaign.TryParse(id.Value, out var parsedCampaign))
var id = reader.Value as long?;
if (id is not long ||
!Campaign.TryParse((int)id.Value, out var parsedCampaign))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/ContinentJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class ContinentJsonConverter : JsonConverter

return namedContinent;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Continent.TryParse(id.Value, out var parsedContinent))
var id = reader.Value as long?;
if (id is not long ||
!Continent.TryParse((int)id.Value, out var parsedContinent))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/GuildwarsIconJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class GuildwarsIconJsonConverter : JsonConverter

return namedGuildwarsIcon;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!GuildwarsIcon.TryParse(id.Value, out var parsedGuildwarsIcon))
var id = reader.Value as long?;
if (id is not long ||
!GuildwarsIcon.TryParse((int)id.Value, out var parsedGuildwarsIcon))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/ItemBaseJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class ItemBaseJsonConverter : JsonConverter

return namedItem;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!ItemBase.TryParse(id.Value, modifiers: default, out var parsedItem))
var id = reader.Value as long?;
if (id is not long ||
!ItemBase.TryParse((int)id.Value, null, out var parsedItem))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/MapJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class MapJsonConverter : JsonConverter

return namedMap;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Map.TryParse(id.Value, out var parsedMap))
var id = reader.Value as long?;
if (id is not long ||
!Map.TryParse((int)id.Value, out var parsedMap))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/NpcJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public sealed class NpcJsonConverter : JsonConverter

return namedNpc;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Npc.TryParse(id.Value, out var parsedNpc))
var id = reader.Value as long?;
if (id is not long ||
!Npc.TryParse((int)id.Value, out var parsedNpc))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/ProfessionJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class ProfessionJsonConverter : JsonConverter

return namedProfession;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Profession.TryParse(id.Value, out var parsedProfession))
var id = reader.Value as long?;
if (id is not long ||
!Profession.TryParse((int)id.Value, out var parsedProfession))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/QuestJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class QuestJsonConverter : JsonConverter

return namedQuest;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Quest.TryParse(id.Value, out var parsedQuest))
var id = reader.Value as long?;
if (id is not long ||
!Quest.TryParse((int)id.Value, out var parsedQuest))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/RegionJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class RegionJsonConverter : JsonConverter

return namedRegion;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Region.TryParse(id.Value, out var parsedRegion))
var id = reader.Value as long?;
if (id is not long ||
!Region.TryParse((int)id.Value, out var parsedRegion))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/SkillJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class SkillJsonConverter : JsonConverter

return namedSkill;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Skill.TryParse(id.Value, out var parsedSkill))
var id = reader.Value as long?;
if (id is not long ||
!Skill.TryParse((int)id.Value, out var parsedSkill))
{
return default;
}
Expand Down
6 changes: 3 additions & 3 deletions Daybreak/Converters/TitleJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ public sealed class TitleJsonConverter : JsonConverter

return namedTitle;
case JsonToken.Integer:
var id = reader.ReadAsInt32();
if (id is not int ||
!Title.TryParse(id.Value, out var parsedTitle))
var id = reader.Value as long?;
if (id is not long ||
!Title.TryParse((int)id.Value, out var parsedTitle))
{
return default;
}
Expand Down
4 changes: 2 additions & 2 deletions Daybreak/Daybreak.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2045.28" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.2088.41" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.75" />
<PackageReference Include="NAudio" Version="2.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Plumsy" Version="1.1.0" />
Expand Down
58 changes: 29 additions & 29 deletions Daybreak/Services/ApplicationLauncher/ApplicationLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,38 +258,10 @@ public void RestartDaybreakAsNormalUser()
CloseHandle(clientHandle);
}

/*
* Run the actions one by one, to avoid injection issues
*/
foreach (var mod in mods)
{
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(this.launcherOptions.Value.ModStartupTimeout));
try
{
await mod.OnGuildwarsStarted(process, cts.Token);
}
catch (TaskCanceledException)
{
this.logger.LogError($"{mod.Name} timeout");
this.notificationService.NotifyError(
title: $"{mod.Name} timeout",
description: $"Mod timed out while processing {nameof(mod.OnGuildwarsStarted)}");
}
catch (Exception e)
{
this.KillGuildWarsProcess(process);
this.logger.LogError(e, $"{mod.Name} unhandled exception");
this.notificationService.NotifyError(
title: $"{mod.Name} exception",
description: $"Mod encountered exception of type {e.GetType().Name} while processing {nameof(mod.OnGuildwarsStarted)}");
return default;
}
}

var retries = 0;
while (retries < MaxRetries)
{
await Task.Delay(100);
await Task.Delay(1000);
retries++;
var gwProcess = Process.GetProcessesByName("gw").FirstOrDefault();
if (gwProcess is null && retries < MaxRetries)
Expand Down Expand Up @@ -322,6 +294,34 @@ public void RestartDaybreakAsNormalUser()
continue;
}

/*
* Run the actions one by one, to avoid injection issues
*/
foreach (var mod in mods)
{
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(this.launcherOptions.Value.ModStartupTimeout));
try
{
await mod.OnGuildwarsStarted(process, cts.Token);
}
catch (TaskCanceledException)
{
this.logger.LogError($"{mod.Name} timeout");
this.notificationService.NotifyError(
title: $"{mod.Name} timeout",
description: $"Mod timed out while processing {nameof(mod.OnGuildwarsStarted)}");
}
catch (Exception e)
{
this.KillGuildWarsProcess(process);
this.logger.LogError(e, $"{mod.Name} unhandled exception");
this.notificationService.NotifyError(
title: $"{mod.Name} exception",
description: $"Mod encountered exception of type {e.GetType().Name} while processing {nameof(mod.OnGuildwarsStarted)}");
return default;
}
}

return gwProcess;
}

Expand Down
Loading

0 comments on commit d24b3cd

Please sign in to comment.