-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnalyzer.Profile.cs
176 lines (134 loc) · 7.33 KB
/
Analyzer.Profile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using lib.remnant2.saves.Model.Properties;
using lib.remnant2.saves.Model;
using System.Text.RegularExpressions;
using Serilog;
using Serilog.Events;
using SerilogTimings.Extensions;
using SerilogTimings;
using lib.remnant2.analyzer.SaveLocation;
namespace lib.remnant2.analyzer;
public partial class Analyzer
{
[GeneratedRegex("Archetype_(?<archetype>[a-zA-Z]+)")]
private static partial Regex RegexArchetype();
public static string GetProfileStringCombined(string? folderPath = null)
{
return string.Join(", ", GetProfileStrings(folderPath));
}
public static string[] GetProfileStrings(string? folderPath = null)
{
ILogger performance = Log.Logger
.ForContext(Log.Category, Log.Performance)
.ForContext("SourceContext", "Analyzer:Profile");
Operation qpOperation = performance.OperationAt(LogEventLevel.Debug).Begin($"Quick profile {folderPath}");
string folder = folderPath ?? SaveUtils.GetSaveFolder();
string profilePath = SaveUtils.GetSavePath(folder, "profile")!;
List<string> result = [];
Operation operation = performance.OperationAt(LogEventLevel.Debug).Begin("Load Save file");
SaveFile profileSf = ReadWithRetry(profilePath);
operation.Complete();
operation = performance.OperationAt(LogEventLevel.Debug).Begin("Get quick profile data");
ArrayProperty ap = (ArrayProperty)profileSf.SaveData.Objects[0].Properties!.Lookup
.Single(x => x.Key == "Characters").Value.Value!;
for (int index = 0; index < ap.Items.Count; index++)
{
object? item = ap.Items[index];
ObjectProperty ch = (ObjectProperty)item!;
if (ch.ClassName == null) continue;
UObject character = ch.Object!;
character.Properties!.Lookup.TryGetValue("Archetype", out Property? archetypeProperty);
character.Properties!.Lookup.TryGetValue("SecondaryArchetype", out Property? secondaryArchetypeProperty);
string? archPath = archetypeProperty?.ToStringValue();
string? secondaryArchPath = secondaryArchetypeProperty?.ToStringValue();
Regex regexArchetype = RegexArchetype();
string archetype = regexArchetype.Match(archPath ?? "").Groups["archetype"].Value;
string secondaryArchetype = regexArchetype.Match(secondaryArchPath ?? "").Groups["archetype"].Value;
Property? characterData = character.Properties!.Lookup
.SingleOrDefault(x => x.Key == "CharacterData").Value;
int objectCount = 0;
// Can be null after initial character creation usually it is overwritten
// with a proper save immediately after
if (characterData != null)
{
List<Component>? components = ((SaveData)((StructProperty)characterData.Value!).Value!)
.Objects[0]
.Components;
object? itemsProperty =
components?.SingleOrDefault(x => x.ComponentKey == "Inventory")?
.Properties?.Lookup.SingleOrDefault(x => x.Key == "Items").Value.Value;
IEnumerable<string?>? itemIds = (itemsProperty as ArrayStructProperty)?.Items.Select(x => GetInventoryItemMinimal((PropertyBag)x!)).Where( x => x.Quantity is not 0).Select(x => x.ProfileId);
object? traitsProperty =
components?.SingleOrDefault(x => x.ComponentKey == "Traits")?
.Properties?.Lookup.SingleOrDefault(x => x.Key == "Traits").Value.Value;
IEnumerable<string?>? traitIds = (traitsProperty as ArrayStructProperty)?.Items.Select(x => (((PropertyBag)x!).Lookup.SingleOrDefault(y => y.Key == "TraitBP").Value.Value as ObjectProperty)?.ClassName);
IEnumerable<string?>? all = itemIds?.Union(traitIds ?? []);
objectCount = all?.Count(x => x!=null && Utils.ItemAcquiredFilter(x)) ?? 0;
}
if (string.IsNullOrEmpty(archetype))
{
archetype = "Unknown";
}
result.Add(archetype + (string.IsNullOrEmpty(secondaryArchetype) ? "" : $", {secondaryArchetype}") +
$" ({objectCount})");
}
operation.Complete();
qpOperation.Complete();
return [.. result];
}
public static void CheckBuildNumber(string? folderPath = null)
{
ILogger logger = Log.Logger
.ForContext(Log.Category, "Analyze")
.ForContext("SourceContext", "Analyzer:Profile");
ILogger notifier = Log.Logger
.ForContext(Log.Category, "Analyze")
.ForContext("RemnantNotificationType", "Warning")
.ForContext("SourceContext", "Analyzer:Profile");
ILogger performance = Log.Logger
.ForContext(Log.Category, Log.Performance)
.ForContext("SourceContext", "Analyzer:Profile");
Operation qpOperation = performance.OperationAt(LogEventLevel.Debug).Begin($"Check build number {folderPath}");
string folder = folderPath ?? SaveUtils.GetSaveFolder();
string profilePath = SaveUtils.GetSavePath(folder,"profile")!;
List<string> result = [];
Operation operation = performance.OperationAt(LogEventLevel.Debug).Begin("Load Save file");
SaveFile profileSf = ReadWithRetry(profilePath);
operation.Complete();
if (profileSf.FileHeader.BuildNumber < BuildLevel)
{
notifier.Warning($"Profile save build number is {profileSf.FileHeader.BuildNumber}, supported build number is {BuildLevel}, Analyser might not work correctly, please update the game and touch the stone with each character");
}
ArrayProperty ap = (ArrayProperty)profileSf.SaveData.Objects[0].Properties!.Lookup
.Single(x => x.Key == "Characters").Value.Value!;
for (int index = 0; index < ap.Items.Count; index++)
{
object? item = ap.Items[index];
ObjectProperty ch = (ObjectProperty)item!;
if (ch.ClassName == null) continue;
operation = performance.BeginOperation($"Character {result.Count + 1} (save_{index}) save load");
string? savePath = SaveUtils.GetSavePath(folder, $"save_{index}");
if (savePath == null)
{
logger.Information($"Could not find save for index {index}");
continue;
}
SaveFile sf;
try
{
sf = ReadWithRetry(savePath);
}
catch (IOException e)
{
logger.Information($"Could not load {savePath}, {e}");
continue;
}
if (sf.FileHeader.BuildNumber < BuildLevel)
{
notifier.Warning($"Character {result.Count + 1} (save_{index}) save build number is {sf.FileHeader.BuildNumber}, supported build number is {BuildLevel}, Analyser might not work correctly, please update the game and touch the stone with each character");
}
result.Add(sf.FileHeader.BuildNumber.ToString());
operation.Complete();
}
qpOperation.Complete();
}
}