完善JSON读取和测试

This commit is contained in:
Longfeng Qin 2024-12-09 12:14:17 +08:00
parent 21d6942359
commit e8cfa042a0
4 changed files with 108 additions and 77 deletions

72
Server/Json/JsonTest.cs Normal file
View File

@ -0,0 +1,72 @@
using Newtonsoft.Json;
using PSO2SERVER.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PSO2SERVER.Json
{
public class JsonTest
{
public static void JsonReadTest()
{
// 测试数据文件路径
string jsonFilePath1 = "data\\maps\\lobby\\data.json";
string jsonFilePath2 = "data\\maps\\lobby\\events\\main_lobby_1\\all_events.json";
string jsonFilePath3 = "data\\item_attrs.json";
// 读取并反序列化 JSON 文件
var map = DeserializeJson<MapData>(jsonFilePath1);
var mapEvent = DeserializeJson<List<EventData>>(jsonFilePath2)?.FirstOrDefault();
var attributes = DeserializeJson<ItemAttributesRootObject>(jsonFilePath3);
// 输出 MapData 信息
if (map != null)
{
Logger.Write($"map_object ID: {map.Mapdata.map_object.ID}");
Logger.Write($"Zones[0].Name: {map.Zones[0].Name}");
Logger.Write($"Zones[0].Is_special_zone: {map.Zones[0].Is_special_zone}");
}
// 输出 EventData 信息
if (mapEvent != null)
{
Logger.Write($"map_event zone_id: {mapEvent.zone_id}");
Logger.Write($"map_event is_active: {mapEvent.is_active}");
Logger.Write($"map_event objName: {mapEvent.data.objName}");
}
// 输出 ItemAttributesPC 信息
if (attributes != null)
{
Logger.Write($"PC unk1: {attributes.PC.Unk1}");
ItemAttributesPC.LogWeapons(attributes.PC.Weapons);
ItemAttributesPC.LogData17(attributes.PC.Data17);
}
}
// 泛型方法:读取并反序列化 JSON 文件
public static T DeserializeJson<T>(string filePath)
{
if (!File.Exists(filePath))
{
Logger.Write($"文件不存在: {filePath}");
return default(T);
}
try
{
string json = File.ReadAllText(filePath);
return JsonConvert.DeserializeObject<T>(json);
}
catch (Exception ex)
{
Logger.Write($"错误:读取或反序列化文件 {filePath} 时发生异常: {ex.Message}");
return default(T);
}
}
}
}

View File

@ -73,6 +73,39 @@ namespace PSO2SERVER.Models
public List<Data19> Data19 { get; set; } = new List<Data19>();
[JsonProperty("data20")]
public List<Data20> Data20 { get; set; } = new List<Data20>();
public static void LogWeapons(List<WeaponAttrs> weapons)
{
if (weapons == null || weapons.Count == 0) return;
Logger.Write($"Weapons Count: {weapons.Count}");
foreach (var weapon in weapons)
{
Logger.Write($"Weapon ID: {weapon.Id}, Range Damage: {weapon.RangeDmg}, Gender: {weapon.Gender}");
Logger.Write($"weapon.Unk8 Length: {weapon.Unk8.Length}");
foreach (var un8 in weapon.Unk8)
{
Logger.Write($"Weapon un8: {un8}");
}
}
}
// 输出 Data17 列表信息
public static void LogData17(List<Data17> data17List)
{
if (data17List == null || data17List.Count == 0) return;
Logger.Write($"Data17 Count: {data17List.Count}");
foreach (var data17 in data17List)
{
Logger.Write($"data17.unk Length: {data17.unk.Length}");
foreach (var value in data17.unk)
{
Logger.Write($"data17 value: {value}");
}
}
}
}
// Vita ItemAttributes (Vita client)

View File

@ -92,83 +92,7 @@ namespace PSO2SERVER
Console.CancelKeyPress += Exit;
AppDomain.CurrentDomain.ProcessExit += Exit;
string jsonFilePath = "data\\maps\\lobby\\data.json";
// 读取 JSON 文件
string json = File.ReadAllText(jsonFilePath);
// 反序列化为 C# 对象
MapData map = JsonConvert.DeserializeObject<MapData>(json);
// 输出结果
Logger.Write($"map_object ID: {map.Mapdata.map_object.ID}");
Logger.Write($"Zones[0].Name: {map.Zones[0].Name}");
Logger.Write($"Zones[0].Is_special_zone: {map.Zones[0].Is_special_zone}");
string jsonFilePath2 = "data\\maps\\lobby\\events\\main_lobby_1\\all_events.json";
// 读取 JSON 文件
string json2 = File.ReadAllText(jsonFilePath2);
// 反序列化为 C# 对象
EventData map_event = JsonConvert.DeserializeObject<List<EventData>>(json2).FirstOrDefault();
// 输出结果
Logger.Write($"map_event zone_id: {map_event.zone_id}");
Logger.Write($"map_event is_active: {map_event.is_active}");
Logger.Write($"map_event objName: {map_event.data.objName}");
string jsonFilePath3 = "data\\item_attrs.json";
// Ensure the file exists
if (File.Exists(jsonFilePath3))
{
try
{
// Read the JSON file
string json3 = File.ReadAllText(jsonFilePath3);
// Deserialize the JSON data into ItemAttributesPC
ItemAttributesRootObject Attributes = JsonConvert.DeserializeObject<ItemAttributesRootObject>(json3);
// 输出 PC 的属性
Logger.Write($"PC unk1: {Attributes.PC.Unk1}");
// 输出武器列表
Logger.Write($"Weapons Count: {Attributes.PC.Weapons.Count}");
foreach (var weapon in Attributes.PC.Weapons)
{
Logger.Write($"Weapon ID: {weapon.Id}, Range Damage: {weapon.RangeDmg}, Gender: {weapon.Gender}");
Logger.Write($"weapon.Unk8 Length: {weapon.Unk8.Length}");
foreach (var un8 in weapon.Unk8)
{
Logger.Write($"Weapon un8: {un8}");
}
}
// 输出武器列表
Logger.Write($"Data17 Count: {Attributes.PC.Data17.Count}");
foreach (var data17 in Attributes.PC.Data17)
{
Logger.Write($"data17.unk Length: {data17.unk.Length}");
foreach (var value in data17.unk)
{
Logger.Write($"data17 value:{value}");
}
}
}
catch (Exception ex)
{
Logger.Write($"错误:读取或反序列化 JSON 文件时发生异常: {ex.Message}");
}
}
else
{
Logger.Write("JSON 文件不存在。");
}
JsonTest.JsonReadTest();
try
{
@ -370,5 +294,6 @@ namespace PSO2SERVER
// Save the configuration
Config.Save();
}
}
}

View File

@ -301,6 +301,7 @@
<Compile Include="Config.cs" />
<Compile Include="ConsoleSystem.cs" />
<Compile Include="Crypto\KeyLoader.cs" />
<Compile Include="Json\JsonTest.cs" />
<Compile Include="Json\QuestJson.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Models\BattleStats.cs" />