Skip to content

Commit 6eef530

Browse files
authored
fix nullpo player login & move "KnownSteamIds" to a separate file (#203)
* fix null reference * known steam id txt # Conflicts: # manifest.xml
1 parent d92e0bd commit 6eef530

5 files changed

Lines changed: 54 additions & 9 deletions

File tree

‎Essentials/Essentials.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
<Compile Include="GridFinder.cs" />
164164
<Compile Include="InfoCommand.cs" />
165165
<Compile Include="Commands\InfoModule.cs" />
166+
<Compile Include="KnownIdsStorage.cs" />
166167
<Compile Include="Patches\ChatMessagePatch.cs" />
167168
<Compile Include="Patches\SessionDownloadPatch.cs" />
168169
<Compile Include="PlayerAccountModule.cs" />

‎Essentials/EssentialsConfig.cs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class EssentialsConfig : ViewModel
2323
public EssentialsConfig()
2424
{
2525
AutoCommands.CollectionChanged += (sender, args) => OnPropertyChanged();
26-
KnownSteamIds.CollectionChanged += (sender, args) => OnPropertyChanged();
2726
InfoCommands.CollectionChanged += (sender, args) => OnPropertyChanged();
2827
}
2928

@@ -95,9 +94,6 @@ public int BackpackLimit
9594
set => SetValue(ref _backpackLimit, value);
9695
}
9796

98-
[Display(Visible=false)]
99-
public ObservableCollection<ulong> KnownSteamIds { get; } = new ObservableCollection<ulong>();
100-
10197
private bool _cutGameTags;
10298
[Display(Name = "Cut Game Tags", GroupName = "Client Join Tweaks", Order = 8, Description = "Cuts mods and blocks limits from matchmaking server info. Prevents from 'error downloading session settings'.")]
10399
public bool CutGameTags

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
5454
private HashSet<ulong> _motdOnce = new HashSet<ulong>();
5555
private PatchManager _pm;
5656
private PatchContext _context;
57-
57+
private KnownIdsStorage _knownIds;
5858

5959
public static EssentialsPlugin Instance { get; private set; }
6060
public PlayerAccountModule AccModule = new PlayerAccountModule();
@@ -77,6 +77,8 @@ public override void Init(ITorchBase torch)
7777
string path = Path.Combine(StoragePath, "Essentials.cfg");
7878
Log.Info($"Attempting to load config from {path}");
7979
_config = Persistent<EssentialsConfig>.Load(path);
80+
_knownIds = new KnownIdsStorage(Path.Combine(StoragePath, "Essentials.KnownSteamIds.txt"));
81+
_knownIds.Read();
8082
_sessionManager = Torch.Managers.GetManager<TorchSessionManager>();
8183
if (_sessionManager != null)
8284
_sessionManager.SessionStateChanged += SessionChanged;
@@ -304,10 +306,10 @@ public void SendMotd(MyPlayer player, bool onSessionChanged)
304306

305307
string name = player.Identity?.DisplayName ?? "player";
306308

307-
bool isNewUser = !Config.KnownSteamIds.Contains(id);
309+
bool isNewUser = !_knownIds.Contains(id);
308310
if (isNewUser)
309311
{
310-
Config.KnownSteamIds.Add(id);
312+
_knownIds.Add(id);
311313
}
312314

313315
if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)

‎Essentials/KnownIdsStorage.cs‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace Essentials
6+
{
7+
public sealed class KnownIdsStorage
8+
{
9+
readonly HashSet<ulong> _steamIds;
10+
readonly string _filePath;
11+
12+
public KnownIdsStorage(string filePath)
13+
{
14+
_filePath = filePath;
15+
_steamIds = new HashSet<ulong>();
16+
}
17+
18+
public void Read()
19+
{
20+
if (!File.Exists(_filePath)) return;
21+
22+
_steamIds.Clear();
23+
foreach (var line in File.ReadAllLines(_filePath))
24+
{
25+
if (ulong.TryParse(line.Trim(), out var steamId))
26+
{
27+
_steamIds.Add(steamId);
28+
}
29+
}
30+
}
31+
32+
public bool Contains(ulong steamId)
33+
{
34+
return _steamIds.Contains(steamId);
35+
}
36+
37+
public void Add(ulong steamId)
38+
{
39+
if (_steamIds.Add(steamId))
40+
{
41+
var lines = _steamIds.Select(s => $"{s}");
42+
File.WriteAllLines(_filePath, lines);
43+
}
44+
}
45+
}
46+
}

‎Essentials/PlayerAccountModule.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public void GenerateAccount(Torch.API.IPlayer player) {
153153
Account.KnownIps.Add(ip.ToString());
154154
}
155155

156-
if (Account.IdentityID == 0L && Account.Player != string.Empty) {
157-
Account.IdentityID = Utilities.GetIdentityByNameOrIds(Account.Player).IdentityId;
156+
if (Account.IdentityID == 0L && !string.IsNullOrEmpty(Account.Player)) {
157+
Account.IdentityID = Utilities.GetIdentityByNameOrIds(Account.Player)?.IdentityId ?? 0L;
158158
UpdatePlayerAccount(Account);
159159
}
160160
found = true;

0 commit comments

Comments
 (0)