mirror of
https://github.com/crskycode/GARbro.git
synced 2026-07-08 01:30:18 +08:00
Add FormatCatalog.SerializeSchemeJson
This commit is contained in:
@@ -33,6 +33,9 @@ using GameRes.Collections;
|
|||||||
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
using GameRes.Compression;
|
using GameRes.Compression;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Text;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
|
||||||
namespace GameRes
|
namespace GameRes
|
||||||
{
|
{
|
||||||
@@ -389,6 +392,74 @@ namespace GameRes
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class IncludeFieldsContractResolver : DefaultContractResolver
|
||||||
|
{
|
||||||
|
protected override JsonContract CreateContract (Type objectType)
|
||||||
|
{
|
||||||
|
var contract = base.CreateContract (objectType);
|
||||||
|
|
||||||
|
if (contract is JsonObjectContract objectContract)
|
||||||
|
{
|
||||||
|
objectContract.MemberSerialization = MemberSerialization.Fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
return contract;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ByteArrayToHexStringConverter : JsonConverter<byte[]>
|
||||||
|
{
|
||||||
|
public override byte[] ReadJson (JsonReader reader, Type objectType, byte[] existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteJson (JsonWriter writer, byte[] value, JsonSerializer serializer)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
{
|
||||||
|
writer.WriteNull ();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var s = ByteArrayToString (value);
|
||||||
|
writer.WriteValue (s);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string ByteArrayToString (byte[] input)
|
||||||
|
{
|
||||||
|
var sb = new StringBuilder (input.Length * 2);
|
||||||
|
foreach (var b in input)
|
||||||
|
sb.AppendFormat ("{0:X2}", b);
|
||||||
|
return sb.ToString ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SerializeSchemeJson (Stream output)
|
||||||
|
{
|
||||||
|
var db = new SchemeDataBase
|
||||||
|
{
|
||||||
|
Version = CurrentSchemeVersion,
|
||||||
|
SchemeMap = new Dictionary<string, ResourceScheme> (),
|
||||||
|
GameMap = m_game_map,
|
||||||
|
};
|
||||||
|
foreach (var format in Formats)
|
||||||
|
{
|
||||||
|
var scheme = format.Scheme;
|
||||||
|
if (null != scheme)
|
||||||
|
db.SchemeMap[format.Tag] = scheme;
|
||||||
|
}
|
||||||
|
var settings = new JsonSerializerSettings
|
||||||
|
{
|
||||||
|
ContractResolver = new IncludeFieldsContractResolver (),
|
||||||
|
Converters = { new ByteArrayToHexStringConverter () },
|
||||||
|
Formatting = Formatting.Indented,
|
||||||
|
};
|
||||||
|
var json = JsonConvert.SerializeObject (db, settings);
|
||||||
|
var sw = new StreamWriter (output, Encoding.UTF8);
|
||||||
|
sw.Write (json);
|
||||||
|
sw.Flush ();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Read text file <paramref name="filename"/> from data directory, performing <paramref name="process_line"/> action on each non-empty line.
|
/// Read text file <paramref name="filename"/> from data directory, performing <paramref name="process_line"/> action on each non-empty line.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user