90 lines
3.3 KiB
C#
Raw Normal View History

using LootDumpProcessor.Model.Output;
2023-08-12 19:08:38 +01:00
using LootDumpProcessor.Model.Output.LooseLoot;
using LootDumpProcessor.Model.Output.StaticContainer;
using LootDumpProcessor.Model.Processing;
2023-08-12 19:08:38 +01:00
using LootDumpProcessor.Serializers.Json;
using System.Collections.Generic;
2023-08-12 19:08:38 +01:00
namespace LootDumpProcessor.Process.Writer;
2023-08-12 19:08:38 +01:00
public class FileWriter : IWriter
{
private static readonly IJsonSerializer _jsonSerializer = JsonSerializerFactory.GetInstance();
private static readonly string _outputPath;
2023-08-12 19:08:38 +01:00
static FileWriter()
{
var path = LootDumpProcessorContext.GetConfig().WriterConfig.OutputLocation;
if (string.IsNullOrEmpty(path))
throw new Exception("Output directory must be set in WriterConfigs");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
_outputPath = path;
}
public void WriteAll(Dictionary<OutputFileType, object> dumpData)
{
foreach (var (key, value) in dumpData)
{
Write(key, value);
}
}
public void Write(
OutputFileType type,
object data
)
{
if (!Directory.Exists($"{_outputPath}\\loot"))
Directory.CreateDirectory($"{_outputPath}\\loot");
switch (type)
{
case OutputFileType.LooseLoot:
var looseLootData = (Dictionary<string, LooseLootRoot>)data;
foreach (var (key, value) in looseLootData)
{
foreach (var s in LootDumpProcessorContext.GetDirectoryMappings()[key].Name)
{
if (!Directory.Exists($@"{_outputPath}\locations\{s}"))
Directory.CreateDirectory($@"{_outputPath}\locations\{s}");
File.WriteAllText($@"{_outputPath}\locations\{s}\looseLoot.json",
2023-08-12 19:08:38 +01:00
_jsonSerializer.Serialize(value));
}
}
break;
case OutputFileType.StaticContainer:
var staticContainer = (Dictionary<string, MapStaticLoot>)data;
File.WriteAllText($@"{_outputPath}\loot\staticContainers.json",
2023-08-12 19:08:38 +01:00
_jsonSerializer.Serialize(staticContainer));
2023-08-12 19:08:38 +01:00
break;
case OutputFileType.StaticLoot:
var staticLootData = (Dictionary<string, Dictionary<string, StaticItemDistribution>>)data;
foreach (var (key, value) in staticLootData)
{
foreach (var s in LootDumpProcessorContext.GetDirectoryMappings()[key].Name)
{
if (!Directory.Exists($@"{_outputPath}\locations\{s}"))
Directory.CreateDirectory($@"{_outputPath}\locations\{s}");
File.WriteAllText($@"{_outputPath}\locations\{s}\staticLoot.json",
_jsonSerializer.Serialize(value));
}
}
2023-08-12 19:08:38 +01:00
break;
case OutputFileType.StaticAmmo:
var staticAmmo = (Dictionary<string, List<AmmoDistribution>>)data;
File.WriteAllText($@"{_outputPath}\loot\staticAmmo.json",
2023-08-12 19:08:38 +01:00
_jsonSerializer.Serialize(staticAmmo));
2023-08-12 19:08:38 +01:00
break;
default:
throw new ArgumentOutOfRangeException(nameof(type), type, null);
}
}
}