using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Cthangover.Core.Mods;
using Cthangover.Mods.Map;
using Cthangover.Core.Utils;
namespace Cthangover.Tools.Services
{
///
/// (De)serialisation of world map zone files (mods/<mod>/map/*.json)
/// for the map editor. File access itself goes through
/// .
///
public static class MapFileService
{
/// Folder of map zone files inside a mod.
public const string MapDir = "map";
///
/// Parses the standard {"Items": [...]} envelope into zones.
/// Throws on malformed JSON so the editor
/// can show the error instead of silently opening an empty map.
///
public static List ParseZones(string json)
{
if (string.IsNullOrWhiteSpace(json))
return new List();
var data = JsonSerializer.Deserialize>(json, JsonOptionsProvider.ReadOptions);
return data?.Items ?? new List();
}
///
/// Serialises zones into the standard envelope with indentation and
/// without null fields, so hand-editing the result stays pleasant and
/// optional properties such as label_pos only appear when used.
///
public static string BuildJson(IEnumerable zones)
{
var data = new FileData { Items = zones?.ToList() ?? new List() };
return JsonSerializer.Serialize(data, JsonOptionsProvider.WriteIndentedIgnoreNullOptions);
}
}
}