Skip to content

Commit 09490f7

Browse files
committed
More defensive name handling
1 parent e38a7fa commit 09490f7

4 files changed

Lines changed: 38 additions & 13 deletions

File tree

PrefPro/Configuration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public ConfigHolder GetOrDefault()
146146
{
147147
var ch = new ConfigHolder
148148
{
149-
Name = _prefPro.PlayerName,
149+
Name = _prefPro.PlayerName ?? "",
150150
FullName = NameSetting.FirstLast,
151151
FirstName = NameSetting.FirstOnly,
152152
LastName = NameSetting.LastOnly,

PrefPro/NameHandlerCache.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace PrefPro;
77
public class NameHandlerCache
88
{
99
private readonly Configuration _configuration;
10-
private string? _playerName;
10+
public string? PlayerName;
1111

1212
public HandlerConfig Config { get; private set; } = HandlerConfig.None;
1313

@@ -22,31 +22,36 @@ private void FrameworkOnUpdate(IFramework framework)
2222
{
2323
if (DalamudApi.ClientState.IsLoggedIn && DalamudApi.ClientState.LocalPlayer is { } localPlayer) {
2424
DalamudApi.Framework.Update -= FrameworkOnUpdate;
25-
_playerName = localPlayer.Name.TextValue;
25+
PlayerName = localPlayer.Name.TextValue;
2626
Refresh();
2727
}
2828
}
2929

3030
private void OnLogout(int type, int code)
3131
{
32-
if (_playerName != null) {
33-
_playerName = null;
32+
if (PlayerName != null) {
33+
PlayerName = null;
3434
Config = HandlerConfig.None;
3535
DalamudApi.Framework.Update += FrameworkOnUpdate;
3636
}
3737
}
3838

3939
public void Refresh()
4040
{
41-
if (_playerName != null) {
42-
Config = CreateConfig(_configuration, _playerName);
41+
if (PlayerName != null) {
42+
Config = CreateConfig(_configuration, PlayerName);
4343
}
4444
}
4545

4646
private static HandlerConfig CreateConfig(Configuration config, string playerName)
4747
{
4848
var data = new HandlerConfig();
4949

50+
if (string.IsNullOrEmpty(config.Name))
51+
{
52+
return HandlerConfig.None;
53+
}
54+
5055
if (config.Name != playerName)
5156
{
5257
data.ApplyFull = true;
@@ -78,7 +83,8 @@ private static HandlerConfig CreateConfig(Configuration config, string playerNam
7883

7984
private static string GetNameText(string playerName, string configName, NameSetting setting)
8085
{
81-
switch (setting) {
86+
switch (setting)
87+
{
8288
case NameSetting.FirstLast:
8389
return configName;
8490
case NameSetting.FirstOnly:

PrefPro/PluginUI.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ class PluginUI : IDisposable
2525

2626
private string _tmpFirstName = "";
2727
private string _tmpLastName = "";
28+
private bool _resetNames;
2829

2930
public bool SettingsVisible
3031
{
3132
get => _settingsVisible;
32-
// set => _settingsVisible = value;
3333
set
3434
{
3535
if (value)
3636
{
37-
var split = _configuration.Name.Split(' ');
38-
_tmpFirstName = split[0];
39-
_tmpLastName = split[1];
37+
_resetNames = true;
4038
}
4139
_settingsVisible = value;
4240
}
@@ -68,6 +66,27 @@ public void DrawSettingsWindow()
6866
ImGui.SetNextWindowSize(size, ImGuiCond.FirstUseEver);
6967
if (ImGui.Begin("PrefPro Config", ref _settingsVisible, ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.NoScrollbar | ImGuiWindowFlags.NoScrollWithMouse))
7068
{
69+
if (_prefPro.PlayerName is null || DalamudApi.ClientState.LocalPlayer is null)
70+
{
71+
ImGui.TextWrapped("Configuration is not available while logged out or in a loading screen.");
72+
ImGui.End();
73+
return;
74+
}
75+
76+
if (_configuration.Name == "")
77+
{
78+
DalamudApi.PluginLog.Debug($"Configuration name is empty, setting to current player name ({_prefPro.PlayerName}).");
79+
_configuration.Name = _prefPro.PlayerName;
80+
}
81+
82+
if (_resetNames)
83+
{
84+
var split = _configuration.Name.Split(' ');
85+
_tmpFirstName = split[0];
86+
_tmpLastName = split[1];
87+
_resetNames = false;
88+
}
89+
7190
var enabled = _configuration.Enabled;
7291
var currentGender = _configuration.Gender;
7392
var currentRace = _configuration.Race;

PrefPro/PrefPro.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public unsafe class PrefPro : IDalamudPlugin
4242

4343
public readonly NameHandlerCache NameHandlerCache;
4444

45-
public string PlayerName => DalamudApi.ClientState.LocalPlayer?.Name.ToString();
45+
public string? PlayerName => NameHandlerCache.PlayerName;
4646
public int PlayerGender => DalamudApi.ClientState.LocalPlayer?.Customize[(int)CustomizeIndex.Gender] ?? 0;
4747
public RaceSetting PlayerRace => (RaceSetting) (DalamudApi.ClientState.LocalPlayer?.Customize[(int)CustomizeIndex.Race] ?? 0);
4848
public TribeSetting PlayerTribe => (TribeSetting) (DalamudApi.ClientState.LocalPlayer?.Customize[(int)CustomizeIndex.Tribe] ?? 0);

0 commit comments

Comments
 (0)