Skip to content

MuharremTozan/Agentic-Duel / .agent/skills/unity-data-persistence

unity-data-persistence

Verified

Robust Save/Load system using Data Binding, Serializable DTOs, and swappable Data Service backends (File/JSON default). Supports per-entity GUID identity for saving multiple actors (player, inventory, enemies).

assets/code/FileDataService.cs.txt

View on GitHub
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Standard.Systems.Persistence {

    /// <summary>
    /// Concrete IDataService that persists GameData as JSON files at
    /// Application.persistentDataPath. Swap this with a CloudDataService
    /// without touching any other part of the system.
    /// </summary>
    public class FileDataService : IDataService {
        readonly ISerializer serializer;
        readonly string dataPath;
        readonly string fileExtension;

        public FileDataService (ISerializer serializer) {
            this.serializer  = serializer;
            this.dataPath    = Application.persistentDataPath;
            this.fileExtension = "json";
        }

        string GetPathToFile (string fileName) =>
            Path.Combine(dataPath, string.Concat(fileName, ".", fileExtension));

        public void Save (GameData data, bool overwrite = true) {
            string path = GetPathToFile(data.Name);

            if (!overwrite && File.Exists(path)) {
                throw new IOException(
                    $"[SaveLoad] Save file '{data.Name}.{fileExtension}' already exists.");
            }

            File.WriteAllText(path, serializer.Serialize(data));
            Debug.Log($"[SaveLoad] Status: Game Saved | Data: Name={data.Name}");
        }

        public GameData Load (string name) {
            string path = GetPathToFile(name);

            if (!File.Exists(path)) {
                throw new ArgumentException(
                    $"[SaveLoad] No save file found | Data: Name={name}");
            }

            Debug.Log($"[SaveLoad] Status: Game Loaded | Data: Name={name}");
            return serializer.Deserialize<GameData>(File.ReadAllText(path));
        }

        public void Delete (string name) {
            string path = GetPathToFile(name);
            if (File.Exists(path)) File.Delete(path);
        }

        public void DeleteAll () {
            foreach (string filePath in Directory.GetFiles(dataPath))
                File.Delete(filePath);
        }

        public IEnumerable<string> ListSaves () {
            foreach (string path in Directory.EnumerateFiles(dataPath)) {
                if (Path.GetExtension(path) == fileExtension)
                    yield return Path.GetFileNameWithoutExtension(path);
            }
        }
    }
}
receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementationStars: 322kUpdated Claude CodeCodexVerifiedInstall
requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirementsStars: 322kUpdated Claude CodeCodexVerifiedInstall
using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallbackStars: 322kUpdated Claude CodeCodexVerifiedInstall
verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions alwaysStars: 322kUpdated Claude CodeCodexVerifiedInstall