Skip to content

Commit

Permalink
Many fixes and improvements to get it finally working
Browse files Browse the repository at this point in the history
  • Loading branch information
RenNagasaki committed Feb 17, 2025
1 parent 793a04b commit 294dea3
Show file tree
Hide file tree
Showing 15 changed files with 470 additions and 187 deletions.
6 changes: 5 additions & 1 deletion Echosync-Data/SyncMessages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ namespace Echosync_Data.Enums
public enum SyncMessages
{
Test,
Authenticate,
RequestAuthentication,
CreateChannel,
ConnectedPlayersChannel,
ConnectedPlayersNpc,
ConnectedPlayersDialogue,
ConnectedPlayersReady,
StartNpc,
EndNpc,
JoinDialogue,
ClickSuccess,
Click,
ClickForce,
ClickWait,
Expand Down
3 changes: 2 additions & 1 deletion Echosync-Server/Behaviours/EchosyncBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ protected override void OnMessage(MessageEventArgs e)
{
case SyncMessages.CreateChannel:
var channel = messageSplit[1];
var password = messageSplit[2];
if (_server.WebSocketServices.Hosts.ToList().Find(p => p.Path == $"/{channel}") == null)
{
_server.AddWebSocketService($"/{channel}", () => new EchosyncChannelBehaviour(_server, channel));
_server.AddWebSocketService($"/{channel}", () => new EchosyncChannelBehaviour(_server, channel, password));
LogHelper.Log("Main", $"User '{clientID}' created channel '{channel}'");
}
else
Expand Down
235 changes: 149 additions & 86 deletions Echosync-Server/Behaviours/EchosyncChannelBehaviour.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Echosync-Server/Echosync-Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>Echosync_Server</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>0.0.0.3</Version>
<Version>0.9.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
29 changes: 29 additions & 0 deletions Echosync-Server/UserState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Echosync_Server
{
public class UserState
{
public string WebSocketId { get; set; } = "";
public string NetworkId { get; set; } = "";
public string IpAdress { get; set; } = "";
public string UserName { get; set; } = "";
public string NpcId { get; set; } = "";
public string Channel { get; set; } = "";
public int DialogueCount { get; set; } = 0;
public bool Ready { get; set; } = false;

public UserState(string webSocketId, string networkId, string ipAdress, string userName, string channel)
{
this.WebSocketId = webSocketId;
this.NetworkId = networkId;
this.IpAdress = ipAdress;
this.UserName = userName;
this.Channel = channel;
}
}
}
4 changes: 4 additions & 0 deletions Echosync/DataClasses/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ public class Configuration : IPluginConfiguration
public bool SomePropertyToBeSavedAndWithADefault { get; set; } = true;
public bool Enabled { get; set; } = true;
public bool ConnectAtStart { get; set; } = false;
public bool OnlySpecialNPCs { get; set; } = false;
public bool WaitForNearbyUsers { get; set; } = false;
public float MaxPlayerDistance { get; set; } = 10f;
public string SyncServer { get; set; } = "wss://echosync.echotools.cloud";
public string SyncChannel { get; set; } = "";
public string SyncPassword { get; set; } = "";
public LogConfig logConfig { get; set; } = new LogConfig();

// the below exist just to make saving less cumbersome
Expand Down
8 changes: 4 additions & 4 deletions Echosync/DataClasses/LogConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public class LogConfig
public bool GeneralJumpToBottom { get; set; } = true;
#endregion
#region Rest
public bool ShowChatDebugLog { get; set; } = true;
public bool ShowChatErrorLog { get; set; } = true;
public bool ShowChatId0 { get; set; } = true;
public bool ChatJumpToBottom { get; set; } = true;
public bool ShowSyncDebugLog { get; set; } = true;
public bool ShowSyncErrorLog { get; set; } = true;
public bool ShowSyncId0 { get; set; } = true;
public bool SyncJumpToBottom { get; set; } = true;
#endregion
}
}
2 changes: 1 addition & 1 deletion Echosync/Echosync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Import Project="Dalamud.Plugin.Bootstrap.targets" />

<PropertyGroup>
<Version>0.1.0.1</Version>
<Version>0.9.0.0</Version>
<Description>Echosync</Description>
<PackageProjectUrl>https://github.com/RenNagasaki/Echosync</PackageProjectUrl>
<PackageLicenseExpression>AGPL-3.0-or-later</PackageLicenseExpression>
Expand Down
66 changes: 53 additions & 13 deletions Echosync/Helper/AddonTalkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using FFXIVClientStructs.FFXIV.Client.UI.Agent;
using System.Collections.Generic;
using System.Numerics;
using FFXIVClientStructs.FFXIV.Client.Game.Object;

namespace Echosync.Helper
{
Expand All @@ -25,14 +26,15 @@ private record struct AddonTalkState(string? Speaker, string? Text);
public static string ActiveNpcId = "";
public static string ActiveDialogue = "";
public static Vector2 AddonPos = new Vector2(0, 0);
public static float AddonWidth = 0;
public static float AddonScale = 1f;
private OnUpdateDelegate updateHandler;
private readonly ICondition condition;
private readonly IFramework framework;
private readonly IAddonLifecycle addonLifecycle;
private readonly IClientState clientState;
private readonly IObjectTable objects;
private readonly Configuration config;
private readonly IObjectTable objectTable;
private readonly Configuration configuration;
private readonly Plugin plugin;
public DateTime timeNextVoice = DateTime.Now;
private bool readySend = false;
Expand All @@ -47,8 +49,8 @@ public AddonTalkHelper(Plugin plugin, ICondition condition, IFramework framework
this.framework = framework;
this.addonLifecycle = addonLifecycle;
this.clientState = clientState;
this.config = config;
objects = objectTable;
this.configuration = config;
this.objectTable = objectTable;

HookIntoFrameworkUpdate();
}
Expand All @@ -61,14 +63,22 @@ private void HookIntoFrameworkUpdate()

private unsafe void OnPostDraw(AddonEvent type, AddonArgs args)
{
if (!config.Enabled) return;
if (!configuration.Enabled) return;
if (condition[ConditionFlag.OccupiedSummoningBell]) return;
if (configuration.OnlySpecialNPCs)
{
bool isSpecial = ((GameObject*)clientState.LocalPlayer.TargetObject.Address)->NamePlateIconId is not 0;

if (!isSpecial)
return;
}

var addonTalk = (AddonTalk*)args.Addon.ToPointer();
if (addonTalk != null)
{

AddonPos = new Vector2(addonTalk->GetX(), addonTalk->GetY());
AddonWidth = addonTalk->GetScaledWidth(false);
AddonScale = addonTalk->Scale;
var visible = addonTalk->AtkUnitBase.IsVisible;

Expand All @@ -85,13 +95,12 @@ private unsafe void OnPostDraw(AddonEvent type, AddonArgs args)
if (target != null)
{
ActiveNpcId = target.GameObjectId.ToString();
SyncClientHelper.CurrentEvent = LogHelper.EventId(MethodBase.GetCurrentMethod().Name, Enums.TextSource.Sync);
SyncClientHelper.CreateMessage(SyncMessages.StartNpc);
}
}
}
ActiveDialogue = dialogue;
SyncClientHelper.CurrentEvent = LogHelper.EventId(MethodBase.GetCurrentMethod().Name, Enums.TextSource.Sync);
SyncClientHelper.CreateMessage(SyncMessages.JoinDialogue, ActiveDialogue);
joinedDialogue = true;
if (!plugin.ReadyStateWindow.IsOpen)
plugin.ReadyStateWindow.Toggle();
Expand All @@ -111,24 +120,33 @@ private unsafe void OnPostDraw(AddonEvent type, AddonArgs args)
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Addon closed", SyncClientHelper.CurrentEvent);
SyncClientHelper.CreateMessage(SyncMessages.EndNpc);
LogHelper.End(MethodBase.GetCurrentMethod().Name, SyncClientHelper.CurrentEvent);
SyncClientHelper.CurrentEvent = null;
readySend = false;
SyncClientHelper.AllReady = false;
SyncClientHelper.ConnectedPlayersDialogue = 0;
SyncClientHelper.ConnectedPlayersReady = 0;
ActiveDialogue = "";
ActiveNpcId = "";
}

if (!visible)
{
if (plugin.ReadyStateWindow.IsOpen)
plugin.ReadyStateWindow.Toggle();
}
}
else
{
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Weird stuff happening", SyncClientHelper.CurrentEvent);
}
}

private unsafe void OnPreReceiveEvent(AddonEvent type, AddonArgs args)
{
if (!config.Enabled) return;
if (!configuration.Enabled) return;
if (condition[ConditionFlag.OccupiedSummoningBell]) return;
if (!condition[ConditionFlag.OccupiedInQuestEvent] && !condition[ConditionFlag.OccupiedInCutSceneEvent] && !condition[ConditionFlag.OccupiedInEvent]) return;
if (!SyncClientHelper.Connected || SyncClientHelper.ConnectedPlayersDialogue < 2) return;
if (!SyncClientHelper.Connected) return;
if (args is not AddonReceiveEventArgs receiveEventArgs) return;

LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Param: {receiveEventArgs.EventParam} Type: {receiveEventArgs.AtkEventType} B: {receiveEventArgs.AtkEvent}", SyncClientHelper.CurrentEvent);
Expand All @@ -137,20 +155,42 @@ private unsafe void OnPreReceiveEvent(AddonEvent type, AddonArgs args)
if (allowClick)
{
allowClick = false;
joinedDialogue = false;
LogHelper.End(MethodBase.GetCurrentMethod().Name, SyncClientHelper.CurrentEvent);
if (configuration.WaitForNearbyUsers)
{
var closePlayers = DalamudHelper.GetClosePlayers(SyncClientHelper.ConnectedPlayers, configuration.MaxPlayerDistance);

if (closePlayers > SyncClientHelper.ConnectedPlayersDialogue - 1)
{
receiveEventArgs.AtkEventType = 0;
LogHelper.Info(MethodBase.GetCurrentMethod().Name, $"Waiting for other players to start dialogue", SyncClientHelper.CurrentEvent);
}
else
{
joinedDialogue = false;
SyncClientHelper.CreateMessage(SyncMessages.ClickSuccess);
LogHelper.End(MethodBase.GetCurrentMethod().Name, SyncClientHelper.CurrentEvent);
SyncClientHelper.CurrentEvent = null;
}
}
else
{
joinedDialogue = false;
SyncClientHelper.CreateMessage(SyncMessages.ClickSuccess);
LogHelper.End(MethodBase.GetCurrentMethod().Name, SyncClientHelper.CurrentEvent);
SyncClientHelper.CurrentEvent = null;
}
return;
}

if (!readySend && joinedDialogue)
{
if (!(receiveEventArgs.AtkEventType == (byte)AtkEventType.InputReceived))
SyncClientHelper.CreateMessage(SyncMessages.Click, ActiveDialogue);
SyncClientHelper.CreateMessage(SyncMessages.Click);
readySend = true;
}
if (readySend && joinedDialogue && receiveEventArgs.AtkEventType == (byte)AtkEventType.InputReceived)
{
SyncClientHelper.CreateMessage(SyncMessages.ClickForce, ActiveDialogue);
SyncClientHelper.CreateMessage(SyncMessages.ClickForce);
}
}

Expand Down
53 changes: 48 additions & 5 deletions Echosync/Helper/DalamudHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,57 @@
using Dalamud.Interface.Textures.TextureWraps;
using Lumina.Data.Files;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
using Echosync.DataClasses;
using FFXIVClientStructs.FFXIV.Client.Game.Object;
using FFXIVClientStructs.FFXIV.Client.Game.UI;
using FFXIVClientStructs.FFXIV.Client.System.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Numerics;
namespace Echosync.Helper
{
public static class DalamudHelper
{
private static IObjectTable ObjectTable;
private static IClientState ClientState;
private static IFramework Framework;

public static void Setup(IObjectTable objectTable, IClientState clientState, IFramework framework)
{
ObjectTable = objectTable;
ClientState = clientState;
Framework = framework;
}

public static int GetClosePlayers(List<uint> connectedUsers, float maxPlayerDistance)
{
var result = 0;
Framework.RunOnFrameworkThread(() => { result = GetClosePlayersMainThread(connectedUsers, maxPlayerDistance); });

return result;
}
private static int GetClosePlayersMainThread(List<uint> connectedUsers, float maxPlayerDistance)
{
var closePlayers = 0;
foreach (var connectedUser in connectedUsers)
{
if (!SyncClientHelper.ConnectedPlayersNpc.Contains(connectedUser))
{
var playerObject = ObjectTable.SearchByEntityId(connectedUser);

if (playerObject != null)
{
var distance = ClientState.LocalPlayer.Position - playerObject.Position;
var combinedDistance = Math.Abs(distance.X) + Math.Abs(distance.Y) + Math.Abs(distance.Z);
if (combinedDistance < maxPlayerDistance)
{
closePlayers++;
}
}
}
}

return closePlayers;
}
}
}
24 changes: 12 additions & 12 deletions Echosync/Helper/LogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public static class LogHelper
private static int NextEventId = 1;
private static List<LogMessage> GeneralLogs = new List<LogMessage>();
public static List<LogMessage> GeneralLogsFiltered = new List<LogMessage>();
private static List<LogMessage> ChatLogs = new List<LogMessage>();
public static List<LogMessage> ChatLogsFiltered = new List<LogMessage>();
private static List<LogMessage> SyncLogs = new List<LogMessage>();
public static List<LogMessage> SyncLogsFiltered = new List<LogMessage>();

public static void Setup(IPluginLog log, Configuration config)
{
Expand Down Expand Up @@ -73,12 +73,12 @@ private static void SortLogEntry(LogMessage logMessage)
ConfigWindow.UpdateLogGeneralFilter = true;
break;
case TextSource.Sync:
ChatLogs.Add(logMessage);
SyncLogs.Add(logMessage);
if (logMessage.type == LogType.Info
|| logMessage.type == LogType.Debug && Config.logConfig.ShowChatDebugLog
|| logMessage.type == LogType.Error && Config.logConfig.ShowChatErrorLog)
ChatLogsFiltered.Add(logMessage);
ConfigWindow.UpdateLogChatFilter = true;
|| logMessage.type == LogType.Debug && Config.logConfig.ShowSyncDebugLog
|| logMessage.type == LogType.Error && Config.logConfig.ShowSyncErrorLog)
SyncLogsFiltered.Add(logMessage);
ConfigWindow.UpdateLogSyncFilter = true;
break;
}
}
Expand All @@ -99,11 +99,11 @@ public static List<LogMessage> RecreateLogList(TextSource textSource)
showId0 = true;
break;
case TextSource.Sync:
ChatLogsFiltered = new List<LogMessage>(ChatLogs);
logListFiltered = ChatLogsFiltered;
showDebug = Config.logConfig.ShowChatDebugLog;
showError = Config.logConfig.ShowChatErrorLog;
showId0 = Config.logConfig.ShowChatId0;
SyncLogsFiltered = new List<LogMessage>(SyncLogs);
logListFiltered = SyncLogsFiltered;
showDebug = Config.logConfig.ShowSyncDebugLog;
showError = Config.logConfig.ShowSyncErrorLog;
showId0 = Config.logConfig.ShowSyncId0;
break;
}
if (!showDebug)
Expand Down
Loading

0 comments on commit 294dea3

Please sign in to comment.