Refactored namespace and project name.
This commit is contained in:
77
CFUtils/Serialization/ManifestConverter.cs
Normal file
77
CFUtils/Serialization/ManifestConverter.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using CFUtils.Serialization.Modpack;
|
||||
|
||||
namespace CFUtils.Serialization
|
||||
{
|
||||
internal class ManifestConverter : JsonConverter<Manifest>
|
||||
{
|
||||
PropertyInfo[] manifestProperties;
|
||||
Type manifestType;
|
||||
|
||||
public ManifestConverter()
|
||||
{
|
||||
manifestType = typeof(Manifest);
|
||||
manifestProperties = manifestType.GetProperties();
|
||||
}
|
||||
public override Manifest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
string[] propertyNames = new string[manifestProperties.Length];
|
||||
for (int i = 0; i < propertyNames.Length; i++) propertyNames[i] = manifestProperties[i].Name;
|
||||
Manifest result = new Manifest();
|
||||
HashSet<string> pendingProperties = new HashSet<string>(propertyNames);
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.TokenType == JsonTokenType.EndObject)
|
||||
{
|
||||
if (pendingProperties.Count != 0)
|
||||
{
|
||||
throw new JsonException("Manifest is missing data. Will not proceed.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if (reader.TokenType == JsonTokenType.PropertyName)
|
||||
{
|
||||
string propertyName = reader.GetString() ?? throw new JsonException();
|
||||
if (propertyNames.Contains(propertyName))
|
||||
{
|
||||
PropertyInfo property = manifestProperties[Array.IndexOf(propertyNames, propertyName)];
|
||||
pendingProperties.Remove(propertyName);
|
||||
property.SetValue(result, JsonSerializer.Deserialize(ref reader, property.PropertyType, options));
|
||||
}
|
||||
else if (Enum.GetNames(typeof(ManifestGames)).Contains(propertyName, StringComparer.CurrentCultureIgnoreCase))
|
||||
{
|
||||
Game game = JsonSerializer.Deserialize<Game>(ref reader);
|
||||
game.gameType = Enum.Parse<ManifestGames>(propertyName, true);
|
||||
result.game = game;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new JsonException();
|
||||
}
|
||||
}
|
||||
throw new JsonException(); // Reached end of stream and did not find ending token.
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, Manifest value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
writer.WritePropertyName(value.game.gameType.ToString().ToLower());
|
||||
JsonSerializer.Serialize(writer, value.game, typeof(Game), options);
|
||||
for (int i = 0; i < manifestProperties.Length; i++)
|
||||
{
|
||||
PropertyInfo property = manifestProperties[i];
|
||||
writer.WritePropertyName(property.Name);
|
||||
JsonSerializer.Serialize(writer, property.GetValue(value), property.PropertyType, options);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
}
|
7
CFUtils/Serialization/ManifestGames.cs
Normal file
7
CFUtils/Serialization/ManifestGames.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace CFUtils.Serialization
|
||||
{
|
||||
public enum ManifestGames
|
||||
{
|
||||
Minecraft
|
||||
}
|
||||
}
|
9
CFUtils/Serialization/Modpack/File.cs
Normal file
9
CFUtils/Serialization/Modpack/File.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace CFUtils.Serialization.Modpack
|
||||
{
|
||||
public struct File
|
||||
{
|
||||
public string ProjectId { get; set; }
|
||||
public string FileId { get; set; }
|
||||
public bool Required { get; set; }
|
||||
}
|
||||
}
|
12
CFUtils/Serialization/Modpack/Game.cs
Normal file
12
CFUtils/Serialization/Modpack/Game.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
namespace CFUtils.Serialization.Modpack
|
||||
{
|
||||
public struct Game
|
||||
{
|
||||
[JsonIgnore]
|
||||
public ManifestGames gameType;
|
||||
public string Version { get; set; }
|
||||
public IList<ModLoader> ModLoaders { get; set; }
|
||||
}
|
||||
}
|
19
CFUtils/Serialization/Modpack/Manifest.cs
Normal file
19
CFUtils/Serialization/Modpack/Manifest.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CFUtils.Serialization.Modpack
|
||||
{
|
||||
public struct Manifest
|
||||
{
|
||||
[JsonIgnore]
|
||||
public Game game;
|
||||
public string ManifestType { get; set; }
|
||||
public int ManifestVersion { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string Author { get; set; }
|
||||
public IList<File> Files { get; set; }
|
||||
public string Overrides { get; set; }
|
||||
}
|
||||
}
|
8
CFUtils/Serialization/Modpack/ModLoader.cs
Normal file
8
CFUtils/Serialization/Modpack/ModLoader.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CFUtils.Serialization.Modpack
|
||||
{
|
||||
public struct ModLoader
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public bool Primary { get; set; }
|
||||
}
|
||||
}
|
7
CFUtils/Serialization/ProfileReader.cs
Normal file
7
CFUtils/Serialization/ProfileReader.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace CFUtils.Serialization
|
||||
{
|
||||
internal class ProfileReader
|
||||
{
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user