using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using UnityEngine; public static class CommandSaveUtil { private static JsonSerializerSettings getSettings() { return new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Auto, ReferenceLoopHandling = ReferenceLoopHandling.Ignore // Prevents circular reference issues }; } public static bool saveLinkedList(LinkedList list, string saveFilePath) { if (list == null) { Debug.LogWarning("Cannot save null list"); return false; } try { string json = JsonConvert.SerializeObject(list, getSettings()); File.WriteAllText(saveFilePath, json); Debug.Log($"LinkedList saved successfully: {saveFilePath} ({list.Count} commands)"); return true; } catch (Exception e) { Debug.LogError($"Failed to save LinkedList: {e.Message}"); return false; } } public static LinkedList loadLinkedList(string saveFilePath) { if (!File.Exists(saveFilePath)) { Debug.LogWarning($"Save file not found at: {saveFilePath}"); return new LinkedList(); // Return empty list instead of null } try { string json = File.ReadAllText(saveFilePath); // Check if file is empty if (string.IsNullOrWhiteSpace(json)) { Debug.LogWarning("Save file is empty"); return new LinkedList(); } LinkedList list = JsonConvert.DeserializeObject>(json, getSettings()); Debug.Log($"LinkedList loaded successfully ({list?.Count ?? 0} commands)"); return list ?? new LinkedList(); } catch (Exception e) { Debug.LogError($"Failed to load LinkedList: {e.Message}"); return new LinkedList(); // Return empty list on error } } public static void deleteSaveFile(string saveFilePath) { try { if (File.Exists(saveFilePath)) { File.Delete(saveFilePath); Debug.Log("Save file deleted"); } } catch (Exception e) { Debug.LogError($"Failed to delete save file: {e.Message}"); } } public static bool hasSaveFile(string saveFilePath) { return File.Exists(saveFilePath); } }