[skip ci] export player store data

This commit is contained in:
HolographicHat
2025-01-11 19:24:42 +08:00
parent 2e2be07161
commit 32ceae074e
7 changed files with 174 additions and 25 deletions

View File

@@ -1,5 +1,4 @@
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json;
using System.Text.Json.Serialization;
using Google.Protobuf;
using YaeAchievement.res;
@@ -27,6 +26,12 @@ public class AchievementAllDataNotify {
public List<AchievementItem> AchievementList { get; private init; } = [];
public static bool OnReceive(byte[] bytes) {
GlobalVars.AchievementDataCache.Write(bytes);
Export.Choose(ParseFrom(bytes));
return true;
}
public static AchievementAllDataNotify ParseFrom(byte[] bytes) {
using var stream = new CodedInputStream(bytes);
var data = new List<Dictionary<uint, uint>>();
@@ -36,7 +41,7 @@ public class AchievementAllDataNotify {
while ((tag = stream.ReadTag()) != 0) {
if ((tag & 7) == 2) { // is LengthDelimited
var dict = new Dictionary<uint, uint>();
using var eStream = new CodedInputStream(ReadRawBytes(stream, stream.ReadLength()));
using var eStream = stream.ReadLengthDelimitedAsStream();
try {
while ((tag = eStream.ReadTag()) != 0) {
if ((tag & 7) != 0) { // not VarInt
@@ -107,9 +112,6 @@ public class AchievementAllDataNotify {
};
}
[UnsafeAccessor(UnsafeAccessorKind.Method)]
private static extern byte[] ReadRawBytes(CodedInputStream stream, int size);
}
[JsonSerializable(typeof(AchievementAllDataNotify))]

View File

@@ -0,0 +1,60 @@
using System.Text.Json;
using Google.Protobuf;
using Proto;
namespace YaeAchievement.Parsers;
public class PlayerStoreNotify {
public uint WeightLimit { get; set; }
public StoreType StoreType { get; set; }
public List<Item> ItemList { get; set; } = [];
public static bool OnReceive(byte[] bytes) {
#if DEBUG
var ntf = ParseFrom(bytes);
File.WriteAllText("store_data.json", JsonSerializer.Serialize(ntf, new JsonSerializerOptions {
WriteIndented = true
}));
#endif
return true;
}
private static PlayerStoreNotify ParseFrom(byte[] bytes) {
using var stream = new CodedInputStream(bytes);
var ntf = new PlayerStoreNotify();
try {
uint tag;
while ((tag = stream.ReadTag()) != 0) {
var wireType = tag & 7;
switch (wireType) {
case 0: { // is VarInt
var value = stream.ReadUInt32();
if (value < 10) {
ntf.StoreType = (StoreType) value;
} else {
ntf.WeightLimit = value;
}
continue;
}
case 2: { // is LengthDelimited
using var eStream = stream.ReadLengthDelimitedAsStream();
while (eStream.PeekTag() != 0) {
ntf.ItemList.Add(Item.Parser.ParseFrom(eStream));
}
break;
}
}
}
} catch (InvalidProtocolBufferException) {
// ReSharper disable once LocalizableElement
Console.WriteLine("Parse failed");
File.WriteAllBytes("store_raw_data.bin", bytes);
Environment.Exit(0);
}
return ntf;
}
}