OldBlueWater/BlueWater/Assets/02.Scripts/Editor/DifferencePopup.cs

190 lines
6.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using ExcelDataReader;
using Newtonsoft.Json.Linq;
using UnityEditor;
using UnityEngine;
namespace _02.Scripts.Editor
{
public class DifferencePopup : EditorWindow
{
private List<Difference> differences;
private string selectedExcelFile;
private string jsonFolderPath;
private Vector2 scrollPosition;
private 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;
}
}
}
}