mirror of
https://github.com/Bragin-Stepan/project-entity.git
synced 2026-03-05 07:41:10 +00:00
init: add project
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0dfabea6753040bb886e08bc12f31679
|
||||
timeCreated: 1770822024
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bb5d55999e8cd9947b831a8be3baf413
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders
|
||||
{
|
||||
public abstract class DataProvider<TData> where TData : ISaveData
|
||||
{
|
||||
private readonly ISaveLoadService _saveLoadService;
|
||||
|
||||
private readonly List<IDataWriter<TData>> _writers = new();
|
||||
private readonly List<IDataReader<TData>> _readers = new();
|
||||
|
||||
private TData _data;
|
||||
|
||||
protected DataProvider(ISaveLoadService saveLoadService)
|
||||
{
|
||||
_saveLoadService = saveLoadService;
|
||||
}
|
||||
|
||||
public void RegisterWriter(IDataWriter<TData> writer)
|
||||
{
|
||||
if (_writers.Contains(writer))
|
||||
throw new ArgumentException(nameof(writer));
|
||||
|
||||
_writers.Add(writer);
|
||||
}
|
||||
|
||||
public void RegisterReader(IDataReader<TData> reader)
|
||||
{
|
||||
if (_readers.Contains(reader))
|
||||
throw new ArgumentException(nameof(reader));
|
||||
|
||||
_readers.Add(reader);
|
||||
}
|
||||
|
||||
public IEnumerator LoadAsync()
|
||||
{
|
||||
yield return _saveLoadService.Load<TData>(loadedData => _data = loadedData);
|
||||
|
||||
SendDataToReaders();
|
||||
}
|
||||
|
||||
public IEnumerator SaveAsync()
|
||||
{
|
||||
UpdateDataFromWriters();
|
||||
|
||||
yield return _saveLoadService.Save(_data);
|
||||
}
|
||||
|
||||
public IEnumerator ExistsAsync(Action<bool> onExistsResult)
|
||||
{
|
||||
yield return _saveLoadService.Exists<TData>(result => onExistsResult?.Invoke(result));
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_data = GetOriginData();
|
||||
|
||||
SendDataToReaders();
|
||||
}
|
||||
|
||||
protected abstract TData GetOriginData();
|
||||
|
||||
private void SendDataToReaders()
|
||||
{
|
||||
foreach (IDataReader<TData> reader in _readers)
|
||||
reader.ReadFrom(_data);
|
||||
}
|
||||
|
||||
private void UpdateDataFromWriters()
|
||||
{
|
||||
foreach (IDataWriter<TData> writer in _writers)
|
||||
writer.WriteTo(_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c6bf35dfa43cf024896bd6a93d0ef7d4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders
|
||||
{
|
||||
public interface IDataReader<TData> where TData : ISaveData
|
||||
{
|
||||
void ReadFrom(TData data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5635fd8b8a4db3247a083a80b115ff1c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataProviders
|
||||
{
|
||||
public interface IDataWriter<TData> where TData : ISaveData
|
||||
{
|
||||
void WriteTo(TData data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0c199eaeb90195b46b7ef35e77d2fc98
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ee31fbd4ba77de44ab91963e139c0c0e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataRepository
|
||||
{
|
||||
public interface IDataRepository
|
||||
{
|
||||
IEnumerator Read(string key, Action<string> onRead);
|
||||
IEnumerator Write(string key, string serializedData);
|
||||
IEnumerator Remove(string key);
|
||||
IEnumerator Exists(string key, Action<bool> onExistsResult);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 44db3bd62a6844b438133918ce7520bd
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataRepository
|
||||
{
|
||||
public class LocalFileDataRepository : IDataRepository
|
||||
{
|
||||
private readonly string _folderPath;
|
||||
private readonly string _saveFileExtension;
|
||||
|
||||
public LocalFileDataRepository(string folderPath, string saveFileExtension)
|
||||
{
|
||||
_folderPath = folderPath;
|
||||
_saveFileExtension = saveFileExtension;
|
||||
}
|
||||
|
||||
public IEnumerator Exists(string key, Action<bool> onExistsResult)
|
||||
{
|
||||
bool exists = File.Exists(FullPathFor(key));
|
||||
|
||||
onExistsResult?.Invoke(exists);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Read(string key, Action<string> onRead)
|
||||
{
|
||||
string text = File.ReadAllText(FullPathFor(key));
|
||||
|
||||
onRead?.Invoke(text);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Remove(string key)
|
||||
{
|
||||
File.Delete(FullPathFor(key));
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Write(string key, string serializedData)
|
||||
{
|
||||
File.WriteAllText(FullPathFor(key), serializedData);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
private string FullPathFor(string key)
|
||||
=> Path.Combine(_folderPath, key) + "." + _saveFileExtension;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1237330c119d323409a240bc05147dbb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.DataRepository
|
||||
{
|
||||
public class PlayerPrefsDataRepository : IDataRepository
|
||||
{
|
||||
public IEnumerator Exists(string key, Action<bool> onExistsResult)
|
||||
{
|
||||
bool exists = PlayerPrefs.HasKey(key);
|
||||
|
||||
onExistsResult?.Invoke(exists);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Read(string key, Action<string> onRead)
|
||||
{
|
||||
string text = PlayerPrefs.GetString(key);
|
||||
|
||||
onRead?.Invoke(text);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Remove(string key)
|
||||
{
|
||||
PlayerPrefs.DeleteKey(key);
|
||||
|
||||
yield break;
|
||||
}
|
||||
|
||||
public IEnumerator Write(string key, string serializedData)
|
||||
{
|
||||
PlayerPrefs.SetString(key, serializedData);
|
||||
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: adaa5e6dabd0ddc478a6c91a3f53645f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement
|
||||
{
|
||||
public interface ISaveData
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 94f8ecdf9df3d1549ae149d55a4d6654
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement
|
||||
{
|
||||
public interface ISaveLoadService
|
||||
{
|
||||
IEnumerator Load<TData>(Action<TData> onLoad) where TData : ISaveData;
|
||||
IEnumerator Save<TData>(TData data) where TData : ISaveData;
|
||||
IEnumerator Remove<TData>() where TData : ISaveData;
|
||||
IEnumerator Exists<TData>(Action<bool> onExistsResult) where TData : ISaveData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 307c92b53612c4c41845657f42d815d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6904ab4492a5aa242941e836a86fc46f
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.KeysStorage
|
||||
{
|
||||
public interface IDataKeysStorage
|
||||
{
|
||||
string GetKeyFor<TData>() where TData : ISaveData;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10eac07c628f6fc47b2a2965442dedd6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Assets._Project.Develop.Runtime.Utilities.SceneManagement;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.KeysStorage
|
||||
{
|
||||
public class MapDataKeysStorage : IDataKeysStorage
|
||||
{
|
||||
private readonly Dictionary<Type, string> Keys = new (MapDataKeys.Dictionary);
|
||||
|
||||
public string GetKeyFor<TData>() where TData : ISaveData => Keys[typeof(TData)];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ee6312a9f3693d4bbb60ffd58b21db7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2a7a36b7945f6484c9df1a4567fb9d67
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.Serializers
|
||||
{
|
||||
public interface IDataSerializer
|
||||
{
|
||||
string Serialize<TData>(TData data);
|
||||
|
||||
TData Deserialize<TData>(string serializedData);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 68c37667edb0fba42ba8ea048933f424
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,24 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement.Serializers
|
||||
{
|
||||
public class JsonSerializer : IDataSerializer
|
||||
{
|
||||
public TData Deserialize<TData>(string serializedData)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<TData>(serializedData, new JsonSerializerSettings
|
||||
{
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
});
|
||||
}
|
||||
|
||||
public string Serialize<TData>(TData data)
|
||||
{
|
||||
return JsonConvert.SerializeObject(data, new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.Indented,
|
||||
TypeNameHandling = TypeNameHandling.Auto,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c2f381e1811dfe344a6c25d6d1205239
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,29 @@
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.DataRepository;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.KeysStorage;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.Serializers;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement
|
||||
{
|
||||
public class SaveLoadFactory
|
||||
{
|
||||
public SaveLoadService CreateDefaultSaveLoad()
|
||||
{
|
||||
IDataRepository dataRepository;
|
||||
|
||||
if(RuntimePlatform.WebGLPlayer == Application.platform)
|
||||
{
|
||||
dataRepository = new PlayerPrefsDataRepository();
|
||||
}
|
||||
else
|
||||
{
|
||||
string saveFolderPath =
|
||||
$"{(Application.isEditor ? Application.dataPath : Application.persistentDataPath)}/Saves";
|
||||
|
||||
dataRepository = new LocalFileDataRepository(saveFolderPath, "json");
|
||||
}
|
||||
|
||||
return new SaveLoadService(new JsonSerializer(), new MapDataKeysStorage(), dataRepository);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3890ea90607b48308fbb899dec13672b
|
||||
timeCreated: 1770823825
|
||||
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.DataRepository;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.KeysStorage;
|
||||
using Assets._Project.Develop.Runtime.Utilities.DataManagement.Serializers;
|
||||
|
||||
namespace Assets._Project.Develop.Runtime.Utilities.DataManagement
|
||||
{
|
||||
public class SaveLoadService : ISaveLoadService
|
||||
{
|
||||
private readonly IDataSerializer _serializer;
|
||||
private readonly IDataKeysStorage _keysStorage;
|
||||
private readonly IDataRepository _repository;
|
||||
|
||||
public SaveLoadService(
|
||||
IDataSerializer serializer,
|
||||
IDataKeysStorage keysStorage,
|
||||
IDataRepository repository)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_keysStorage = keysStorage;
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public IEnumerator Exists<TData>(Action<bool> onExistsResult) where TData : ISaveData
|
||||
{
|
||||
string key = _keysStorage.GetKeyFor<TData>();
|
||||
|
||||
yield return _repository.Exists(key, result => onExistsResult?.Invoke(result));
|
||||
}
|
||||
|
||||
public IEnumerator Load<TData>(Action<TData> onLoad) where TData : ISaveData
|
||||
{
|
||||
string key = _keysStorage.GetKeyFor<TData>();
|
||||
|
||||
string serializedData = "";
|
||||
|
||||
yield return _repository.Read(key, result => serializedData = result);
|
||||
|
||||
TData data = _serializer.Deserialize<TData>(serializedData);
|
||||
|
||||
onLoad?.Invoke(data);
|
||||
}
|
||||
|
||||
public IEnumerator Remove<TData>() where TData : ISaveData
|
||||
{
|
||||
string key = _keysStorage.GetKeyFor<TData>();
|
||||
|
||||
yield return _repository.Remove(key);
|
||||
}
|
||||
|
||||
public IEnumerator Save<TData>(TData data) where TData : ISaveData
|
||||
{
|
||||
string serializedData = _serializer.Serialize(data);
|
||||
string key = _keysStorage.GetKeyFor<TData>();
|
||||
yield return _repository.Write(key, serializedData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6e65636005d26054dba538573f23a2b4
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user