CapersProject/Assets/02.Scripts/Editor/DifferencePopup.cs
2025-02-10 11:13:46 +09:00

188 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
namespace DDD.Editors
{
public class DifferencePopup : EditorWindow
{
private List<Difference> _differences;
private string _selectedExcelFile;
private string _jsonFolderPath;
private Vector2 _scrollPosition;
private const string ExcelFolderPath = "Assets/Resources/Excel";
public delegate void OnCloseHandler();
public event OnCloseHandler OnClose;
public static DifferencePopup ShowWindow(List<Difference> differences, string selectedExcelFile,
string jsonFolderPath)
{
DifferencePopup window = GetWindow<DifferencePopup>("비교창");
window._differences = differences;
window._selectedExcelFile = selectedExcelFile;
window._jsonFolderPath = jsonFolderPath;
return window;
}
private void OnGUI()
{
EditorGUILayout.LabelField("비교대상 : " + _selectedExcelFile, EditorStyles.boldLabel);
_scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition);
foreach (var difference in _differences)
{
EditorGUILayout.LabelField("속성: " + difference.PropertyName);
EditorGUILayout.LabelField("새로운 값: " + difference.NewValue);
EditorGUILayout.LabelField("기존의 값: " + difference.ExistingValue);
EditorGUILayout.Space();
}
EditorGUILayout.EndScrollView();
if (GUILayout.Button("저장"))
{
string excelPath = Path.Combine(ExcelFolderPath, _selectedExcelFile + ".xlsx");
string jsonPath = Path.Combine(_jsonFolderPath, _selectedExcelFile + ".json");
try
{
JArray newArray = ExcelToJsonConverter.ConvertExcelToJsonArray(excelPath);
File.WriteAllText(jsonPath, newArray.ToString());
//EditorUtility.DisplayDialog("Success", "Changes saved successfully.", "OK");
ExcelToJsonConverter.ToggleStates[_selectedExcelFile] = false;
OnClose?.Invoke();
this.Close();
bool anyToggled = false;
foreach (var toggleState in ExcelToJsonConverter.ToggleStates.Values)
{
if (toggleState)
{
anyToggled = true;
break;
}
}
if (anyToggled)
{
ExcelToJsonConverter.Instance.ProcessSelectedFiles();
}
}
catch (Exception e)
{
Debug.LogError($"저장에 실패했습니다: {e.Message}");
EditorUtility.DisplayDialog("실패", "저장에 실패했습니다. 콘솔을 확인해주세요.",
"확인");
}
}
if (GUILayout.Button("저장하지 않고 닫기"))
{
ExcelToJsonConverter.ToggleStates[_selectedExcelFile] = false;
OnClose?.Invoke();
this.Close();
bool anyToggled = false;
foreach (var toggleState in ExcelToJsonConverter.ToggleStates.Values)
{
if (toggleState)
{
anyToggled = true;
break;
}
}
if (anyToggled)
{
ExcelToJsonConverter.Instance.ProcessSelectedFiles();
}
}
}
private void OnDestroy()
{
ExcelToJsonConverter.ToggleStates[_selectedExcelFile] = false;
OnClose?.Invoke();
}
// private JArray ConvertExcelToJsonArray(string excelPath)
// {
// FileStream stream = File.Open(excelPath, FileMode.Open, FileAccess.Read);
// IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//
// DataSet result = excelReader.AsDataSet(new ExcelDataSetConfiguration()
// {
// ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
// {
// UseHeaderRow = true
// }
// });
//
// stream.Close();
// DataTable table = result.Tables[0];
//
// JArray jsonArray = new JArray();
// foreach (DataRow row in table.Rows)
// {
// JObject obj = new JObject();
// foreach (DataColumn column in table.Columns)
// {
// string cellValue = row[column].ToString();
// if (float.TryParse(cellValue, out float floatResult))
// {
// if (floatResult % 1 == 0) // If the value is a whole number
// {
// obj[column.ColumnName] = Convert.ToInt32(floatResult);
// }
// else
// {
// obj[column.ColumnName] = floatResult;
// }
// }
// else if (IsString(cellValue))
// {
// obj[column.ColumnName] = cellValue;
// }
// else
// {
// obj[column.ColumnName] = cellValue;
// }
// }
//
// jsonArray.Add(obj);
// }
//
// return jsonArray;
// }
private bool IsString(string value)
{
return value.Any(c => !char.IsDigit(c));
}
public class Difference
{
public int Index { get; set; }
public string PropertyName { get; set; }
public string NewValue { get; set; }
public string ExistingValue { get; set; }
public Difference(string propertyName, string newValue, string existingValue, int index)
{
PropertyName = propertyName;
NewValue = newValue;
ExistingValue = existingValue;
Index = index;
}
}
}
}