206 lines
5.2 KiB
C#
206 lines
5.2 KiB
C#
using System.IO;
|
|
using System.IO.Compression;
|
|
using System.Text;
|
|
using Superlazy;
|
|
using Superlazy.Loader;
|
|
using UnityEngine;
|
|
|
|
public interface IGameSaver
|
|
{
|
|
public void Init();
|
|
|
|
void Set(string key, SLEntity value);
|
|
|
|
SLEntity Get(string key, SLEntity defaultValue);
|
|
}
|
|
|
|
public class RuntimeSaver : IGameSaver
|
|
{
|
|
private SLEntity entity;
|
|
|
|
public void Init()
|
|
{
|
|
if (File.Exists(@"./Saves/Save.sav"))
|
|
{
|
|
var yaml = LoadCompressedTextFromFile(@"./Saves/Save.sav");
|
|
entity = YamlLoader.LoadYaml(yaml);
|
|
}
|
|
else
|
|
{
|
|
var text = PlayerPrefs.GetString("Save", "");
|
|
entity = YamlLoader.LoadYaml(text);
|
|
}
|
|
}
|
|
|
|
public SLEntity Get(string path, SLEntity defaultValue)
|
|
{
|
|
if (defaultValue == null) return entity.Get(path);
|
|
if (entity.Get(path)) return entity.Get(path);
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public void Set(string path, SLEntity value)
|
|
{
|
|
entity.Set(path, value);
|
|
SaveCompressedTextToFile(YamlLoader.SaveToYaml(entity), @"./Saves/Save.sav");
|
|
}
|
|
|
|
private string LoadCompressedTextFromFile(string filePath)
|
|
{
|
|
using var fileStream = File.OpenRead(filePath);
|
|
using var gzipStream = new GZipStream(fileStream, CompressionMode.Decompress);
|
|
using var resultStream = new MemoryStream();
|
|
gzipStream.CopyTo(resultStream);
|
|
var decompressedData = resultStream.ToArray();
|
|
return Encoding.UTF8.GetString(decompressedData);
|
|
}
|
|
|
|
private void SaveCompressedTextToFile(string text, string filePath)
|
|
{
|
|
// 압축할 텍스트를 UTF8 바이트 배열로 변환
|
|
var data = Encoding.UTF8.GetBytes(text);
|
|
|
|
// 경로가 없으면 디렉토리 생성
|
|
var directory = Path.GetDirectoryName(filePath);
|
|
if (!Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
// 파일 스트림 열고 압축된 데이터를 기록
|
|
using var fileStream = File.Create(filePath);
|
|
using var gzipStream = new GZipStream(fileStream, CompressionMode.Compress);
|
|
gzipStream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
|
|
public class RuntimePlayerPrefSaver : IGameSaver
|
|
{
|
|
private SLEntity entity;
|
|
|
|
public void Init()
|
|
{
|
|
var text = PlayerPrefs.GetString("SaveData", "");
|
|
entity = JsonLoader.LoadJson(text);
|
|
}
|
|
|
|
public SLEntity Get(string path, SLEntity defaultValue)
|
|
{
|
|
if (defaultValue == null) return entity.Get(path);
|
|
if (entity.Get(path)) return entity.Get(path);
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public void Set(string path, SLEntity value)
|
|
{
|
|
entity.Set(path, value);
|
|
PlayerPrefs.SetString("SaveData", JsonLoader.SaveToJson(entity, -1, true));
|
|
}
|
|
}
|
|
|
|
public class PlayerPrefOptionSaver : IGameSaver
|
|
{
|
|
private SLEntity entity;
|
|
|
|
public void Init()
|
|
{
|
|
var text = PlayerPrefs.GetString("Options", "");
|
|
entity = YamlLoader.LoadYaml(text);
|
|
}
|
|
|
|
public SLEntity Get(string path, SLEntity defaultValue)
|
|
{
|
|
if (defaultValue == null) return entity.Get(path);
|
|
if (entity.Get(path)) return entity.Get(path);
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public void Set(string path, SLEntity value)
|
|
{
|
|
entity.Set(path, value);
|
|
PlayerPrefs.SetString("Options", YamlLoader.SaveToYaml(entity));
|
|
}
|
|
}
|
|
|
|
public class FileSaver : IGameSaver
|
|
{
|
|
private SLEntity entity;
|
|
private readonly string filePath;
|
|
|
|
public FileSaver(string path)
|
|
{
|
|
filePath = path;
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
if (File.Exists(filePath))
|
|
{
|
|
using var file = File.OpenText(filePath);
|
|
var yaml = file.ReadToEnd();
|
|
entity = YamlLoader.LoadYaml(yaml);
|
|
}
|
|
else
|
|
{
|
|
entity = SLEntity.Empty;
|
|
}
|
|
}
|
|
|
|
public SLEntity Get(string path, SLEntity defaultValue)
|
|
{
|
|
if (defaultValue == null) return entity.Get(path);
|
|
if (entity.Get(path)) return entity.Get(path);
|
|
|
|
return defaultValue;
|
|
}
|
|
|
|
public void Set(string path, SLEntity value)
|
|
{
|
|
entity.Set(path, value);
|
|
var yaml = YamlLoader.SaveToYaml(entity);
|
|
File.WriteAllText(this.filePath, yaml);
|
|
}
|
|
}
|
|
|
|
// TODO: SLSystem과 통합
|
|
public static class SLGameSaver
|
|
{
|
|
private static IGameSaver saver = new DefaultSaver();
|
|
private static IGameSaver optionSaver = new DefaultSaver();
|
|
|
|
public static void Init(IGameSaver inst, IGameSaver optionInst)
|
|
{
|
|
saver = inst;
|
|
saver.Init();
|
|
|
|
optionSaver = optionInst;
|
|
optionSaver.Init();
|
|
}
|
|
|
|
public static void Set(string key, SLEntity value) => saver.Set(key, value);
|
|
|
|
public static SLEntity Get(string key, SLEntity deafaultValue = null) => saver.Get(key, deafaultValue);
|
|
|
|
public static void OptionSet(string key, SLEntity value) => optionSaver.Set(key, value);
|
|
|
|
public static SLEntity OptionGet(string key, SLEntity deafaultValue = null) => optionSaver.Get(key, deafaultValue);
|
|
}
|
|
|
|
public class DefaultSaver : IGameSaver
|
|
{
|
|
public void Init()
|
|
{
|
|
}
|
|
|
|
public SLEntity Get(string path, SLEntity defaultValue)
|
|
{
|
|
return defaultValue;
|
|
}
|
|
|
|
public void Set(string path, SLEntity value)
|
|
{
|
|
}
|
|
} |