using System; using System.Collections.Generic; using System.IO; using Superlazy.Loader; using UnityEngine; namespace Superlazy { public class SLWindowsDataReloader : MonoBehaviour { private FileSystemWatcher watcher; public void Awake() { watcher = new FileSystemWatcher("..\\Data", "*.yaml") { NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size }; watcher.Changed += FileChanged; watcher.Filter = "*.yaml"; watcher.IncludeSubdirectories = true; watcher.EnableRaisingEvents = true; } public void OnApplicationQuit() { watcher.Dispose(); } private void FileChanged(object sender, FileSystemEventArgs e) { try { var file = File.OpenText(e.FullPath); var json = file.ReadToEnd(); var newData = SLEntity.Empty; YamlLoader.LoadYaml(newData, json); foreach (var data in newData) { foreach (var item in data) { // TODO: SLSystem 순환참조 제거 ReWrite(data.ID + "." + item.ID, SLSystem.Data[data.ID][item.ID], item); } } } catch (Exception exception) { SLLog.Error($"Cannot Parse {e.FullPath}. \n{exception.Message}"); } } private void ReWrite(string itemID, SLEntity oldData, SLEntity item) { var removes = new List(); foreach (var old in oldData) { if (item[old.ID] == false) { removes.Add(old.ID); } } foreach (var remove in removes) { oldData[remove] = false; SLLog.Info($"Reroad Data {itemID}.{remove} : remove"); } foreach (var newItem in item) { if (newItem.IsValue) { if (newItem != oldData[newItem.ID]) { oldData[newItem.ID] = newItem; SLLog.Info($"Reroad Data {itemID}.{newItem.ID} : {newItem}"); } } else { ReWrite(itemID + "." + newItem.ID, oldData[newItem.ID], newItem); } } } } }