using System;
using System.IO;
using Newtonsoft.Json;
namespace AGVSimulator.Models
{
///
/// 시뮬레이터 환경 설정 클래스
///
public class SimulatorConfig
{
#region Properties
///
/// MapEditor 실행 파일 경로
///
public string MapEditorExecutablePath { get; set; } = string.Empty;
///
/// 마지막으로 로드한 맵 파일 경로
///
public string LastMapFilePath { get; set; } = string.Empty;
///
/// 설정 파일 자동 저장 여부
///
public bool AutoSave { get; set; } = true;
///
/// 프로그램 시작시 마지막 맵 파일을 자동으로 로드할지 여부
///
public bool AutoLoadLastMapFile { get; set; } = true;
#endregion
#region Static Methods
///
/// 설정 파일 기본 경로
///
private static string ConfigFilePath => Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"AGVSimulator",
"config.json");
///
/// 설정을 파일에서 로드
///
/// 로드된 설정 객체
public static SimulatorConfig Load()
{
try
{
if (File.Exists(ConfigFilePath))
{
var json = File.ReadAllText(ConfigFilePath);
return JsonConvert.DeserializeObject(json) ?? new SimulatorConfig();
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"설정 로드 실패: {ex.Message}");
}
return new SimulatorConfig();
}
///
/// 설정을 파일에 저장
///
/// 저장할 설정 객체
/// 저장 성공 여부
public static bool Save(SimulatorConfig config)
{
try
{
var directory = Path.GetDirectoryName(ConfigFilePath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
var json = JsonConvert.SerializeObject(config, Formatting.Indented);
File.WriteAllText(ConfigFilePath, json);
return true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"설정 저장 실패: {ex.Message}");
return false;
}
}
#endregion
#region Instance Methods
///
/// 현재 설정을 저장
///
/// 저장 성공 여부
public bool Save()
{
return Save(this);
}
///
/// MapEditor 실행 파일 경로 유효성 확인
///
/// 유효한 경로인지 여부
public bool IsMapEditorPathValid()
{
return !string.IsNullOrEmpty(MapEditorExecutablePath) &&
File.Exists(MapEditorExecutablePath);
}
///
/// 마지막 맵 파일이 존재하는지 확인
///
/// 마지막 맵 파일이 유효한지 여부
public bool HasValidLastMapFile()
{
return !string.IsNullOrEmpty(LastMapFilePath) && File.Exists(LastMapFilePath);
}
#endregion
}
}