143 lines
5.7 KiB
C#
143 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace CtrEditor
|
|
{
|
|
public class DatosDeTrabajo
|
|
{
|
|
public Dictionary<string, string> Imagenes { get; private set; }
|
|
private MainViewModel _mainViewModel;
|
|
|
|
public DatosDeTrabajo()
|
|
{
|
|
Imagenes = new Dictionary<string, string>();
|
|
CargarImagenes(); // Inicializar la carga de imágenes basada en el directorio persistente
|
|
}
|
|
|
|
public void SetMainViewModel(MainViewModel mainViewModel)
|
|
{
|
|
_mainViewModel = mainViewModel;
|
|
}
|
|
|
|
public string ObtenerPathImagenConExtension(string key, string extension)
|
|
{
|
|
if (Imagenes.TryGetValue(key, out string imagePath))
|
|
{
|
|
string jsonPath = Path.ChangeExtension(imagePath, extension);
|
|
return jsonPath;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public string? ObtenerPathAllPages(string extension)
|
|
{
|
|
if (!string.IsNullOrEmpty(EstadoPersistente.Instance.directorio))
|
|
return Path.Combine(EstadoPersistente.Instance.directorio, "AllPages" + extension);
|
|
return null;
|
|
}
|
|
|
|
public void CargarImagenes()
|
|
{
|
|
Imagenes.Clear();
|
|
string folderPath = EstadoPersistente.Instance.directorio; // Usar directamente desde el Singleton
|
|
|
|
if (Directory.Exists(folderPath))
|
|
{
|
|
var archivosImagen = Directory.GetFiles(folderPath, "*.png"); // Ajustar para otros formatos si es necesario
|
|
foreach (var archivo in archivosImagen)
|
|
{
|
|
var nombreArchivo = Path.GetFileName(archivo);
|
|
if (!Imagenes.ContainsKey(nombreArchivo))
|
|
{
|
|
Imagenes[nombreArchivo] = archivo;
|
|
}
|
|
}
|
|
|
|
// Cargar datos de imágenes existentes desde archivos JSON
|
|
if (_mainViewModel != null)
|
|
{
|
|
CargarDatosImagenesExistentes();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CargarDatosImagenesExistentes()
|
|
{
|
|
var jsonSerializerSettings = GetJsonSerializerSettings();
|
|
|
|
foreach (var imageName in Imagenes.Keys)
|
|
{
|
|
string jsonPath = ObtenerPathImagenConExtension(imageName, ".json");
|
|
if (!string.IsNullOrEmpty(jsonPath) && File.Exists(jsonPath))
|
|
{
|
|
try
|
|
{
|
|
// Cargar datos completos del archivo JSON
|
|
string jsonString = File.ReadAllText(jsonPath);
|
|
var simulationData = Newtonsoft.Json.JsonConvert.DeserializeObject<SimulationData>(jsonString, jsonSerializerSettings);
|
|
|
|
// Cargar datos de imágenes si existen en el archivo
|
|
if (simulationData?.ImageDataDictionary != null)
|
|
{
|
|
foreach (var imageDataEntry in simulationData.ImageDataDictionary)
|
|
{
|
|
// Solo cargar si no existe ya en el diccionario principal
|
|
if (!_mainViewModel._imageDataDictionary.ContainsKey(imageDataEntry.Key))
|
|
{
|
|
_mainViewModel._imageDataDictionary[imageDataEntry.Key] = imageDataEntry.Value;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Compatibilidad con versiones anteriores (ImageCustomNames)
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
if (simulationData?.ImageCustomNames != null)
|
|
{
|
|
foreach (var customName in simulationData.ImageCustomNames)
|
|
{
|
|
var imageData = _mainViewModel.GetOrCreateImageData(customName.Key);
|
|
if (string.IsNullOrEmpty(imageData.CustomName)) // Solo actualizar si no tiene nombre personalizado
|
|
{
|
|
imageData.CustomName = customName.Value;
|
|
}
|
|
}
|
|
}
|
|
#pragma warning restore CS0618
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log del error pero no fallar la carga completa
|
|
System.Diagnostics.Debug.WriteLine($"Error al cargar datos de imagen desde {jsonPath}: {ex.Message}");
|
|
|
|
// Como fallback, crear una instancia vacía
|
|
_mainViewModel.GetOrCreateImageData(imageName);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Si no existe archivo JSON, crear instancia vacía
|
|
_mainViewModel.GetOrCreateImageData(imageName);
|
|
}
|
|
}
|
|
}
|
|
|
|
private Newtonsoft.Json.JsonSerializerSettings GetJsonSerializerSettings()
|
|
{
|
|
return new Newtonsoft.Json.JsonSerializerSettings
|
|
{
|
|
Formatting = Newtonsoft.Json.Formatting.Indented,
|
|
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
|
|
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto,
|
|
ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace,
|
|
ConstructorHandling = Newtonsoft.Json.ConstructorHandling.AllowNonPublicDefaultConstructor
|
|
};
|
|
}
|
|
}
|
|
}
|