131 lines
No EOL
4.3 KiB
C#
131 lines
No EOL
4.3 KiB
C#
using System;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
|
||
namespace ShiroginSDK.Runtime.Modules.Data.Scripts
|
||
{
|
||
/// <summary>
|
||
/// ShiroginSDK'nin tüm veri sınıflarının türediği temel sınıf.
|
||
/// JSON tabanlı kaydetme, yükleme ve silme işlemlerini sağlar.
|
||
/// </summary>
|
||
[Serializable]
|
||
public abstract class BaseData
|
||
{
|
||
/// <summary>
|
||
/// Verinin ilk kez oluşturulup oluşturulmadığını belirtir.
|
||
/// </summary>
|
||
[NonSerialized]
|
||
public bool IsInitialized;
|
||
|
||
/// <summary>
|
||
/// Her alt sınıf kendi benzersiz kaydetme anahtarını tanımlar.
|
||
/// Örnek: "player_data", "settings_data", "store_data"
|
||
/// </summary>
|
||
protected abstract string PrefsKey { get; }
|
||
|
||
/// <summary>
|
||
/// Verilerin kaydedileceği JSON dosya yolu.
|
||
/// </summary>
|
||
protected virtual string FilePath => Path.Combine(Application.persistentDataPath, $"{PrefsKey}.json");
|
||
|
||
/// <summary>
|
||
/// Veriyi kaydeder.
|
||
/// Eğer parametre true ise dosya oluşturur (JSON).
|
||
/// Eğer parametre false (varsayılan) ise PlayerPrefs'e kaydeder.
|
||
/// </summary>
|
||
public virtual void Save(bool saveToFile = false)
|
||
{
|
||
try
|
||
{
|
||
var json = JsonUtility.ToJson(this, false);
|
||
|
||
if (saveToFile)
|
||
{
|
||
// Dosya olarak kaydet
|
||
File.WriteAllText(FilePath, json);
|
||
Debug.Log($"[Data] Saved to FILE → {PrefsKey} ({FilePath})");
|
||
}
|
||
else
|
||
{
|
||
// PlayerPrefs olarak kaydet
|
||
PlayerPrefs.SetString(PrefsKey, json);
|
||
PlayerPrefs.Save();
|
||
Debug.Log($"[Data] Saved to PREFS → {PrefsKey}");
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"[Data] Save failed for {PrefsKey}: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Veriyi yükler; dosya yoksa veya PlayerPrefs’te varsa uygun yerden çeker.
|
||
/// </summary>
|
||
public virtual void Load()
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(FilePath))
|
||
{
|
||
var json = File.ReadAllText(FilePath);
|
||
JsonUtility.FromJsonOverwrite(json, this);
|
||
Debug.Log($"[Data] Loaded from FILE → {PrefsKey}");
|
||
}
|
||
else if (PlayerPrefs.HasKey(PrefsKey))
|
||
{
|
||
var json = PlayerPrefs.GetString(PrefsKey);
|
||
JsonUtility.FromJsonOverwrite(json, this);
|
||
Debug.Log($"[Data] Loaded from PREFS → {PrefsKey}");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning($"[Data] No saved data found for {PrefsKey}. Creating new file.");
|
||
IsInitialized = true;
|
||
Save(); // Default kaydet (PlayerPrefs’e)
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"[Data] Load failed for {PrefsKey}: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Veriyi tamamen siler (dosya + PlayerPrefs).
|
||
/// </summary>
|
||
public virtual void Delete()
|
||
{
|
||
try
|
||
{
|
||
if (File.Exists(FilePath))
|
||
File.Delete(FilePath);
|
||
|
||
if (PlayerPrefs.HasKey(PrefsKey))
|
||
PlayerPrefs.DeleteKey(PrefsKey);
|
||
|
||
Debug.Log($"[Data] Deleted → {PrefsKey}");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"[Data] Delete failed for {PrefsKey}: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// Verinin var olup olmadığını kontrol eder.
|
||
/// </summary>
|
||
public bool Exists()
|
||
{
|
||
return File.Exists(FilePath) || PlayerPrefs.HasKey(PrefsKey);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Dosya içeriğini JSON string olarak döndürür (debug amaçlı).
|
||
/// </summary>
|
||
public string ToJson(bool prettyPrint = true)
|
||
{
|
||
return JsonUtility.ToJson(this, prettyPrint);
|
||
}
|
||
}
|
||
} |