예제 #1
0
    public static T Load <T>(string identifier, Func <T> defaultValue, YHSaveGamePath path = YHSaveGamePath.PersistentDataPath)
    {
        if (string.IsNullOrEmpty(identifier))
        {
            throw new System.ArgumentNullException(nameof(identifier));
        }
        string filePath = "";

        if (!IsFilePath(identifier))
        {
            switch (path)
            {
            case YHSaveGamePath.PersistentDataPath:
                filePath = $"{persistentDataPath}/{identifier}";
                break;

            case YHSaveGamePath.DataPath:
                filePath = $"{dataPath}/{identifier}";
                break;
            }
        }
        else
        {
            filePath = identifier;
        }
        if (File.Exists(filePath))
        {
            Stream stream = File.OpenRead(filePath);
            YHSaveGameJsonSerializer serializer = new YHSaveGameJsonSerializer();
            var result = serializer.Deserialize <T>(stream);
            stream.Dispose();
            stream.Close();
            return(result);
        }
        else
        {
            var result = defaultValue.Invoke();
            Save(identifier, result, path);
            return(result);
        }
    }
예제 #2
0
    public static void Save <T>(string identifier, T obj, YHSaveGamePath path = YHSaveGamePath.PersistentDataPath)
    {
        if (string.IsNullOrEmpty(identifier))
        {
            throw new System.ArgumentNullException(nameof(identifier));
        }
        string filePath = "";

        if (!IsFilePath(identifier))
        {
            switch (path)
            {
            case YHSaveGamePath.PersistentDataPath:
                filePath = $"{persistentDataPath}/{identifier}";
                break;

            case YHSaveGamePath.DataPath:
                filePath = $"{dataPath}/{identifier}";
                break;
            }
        }
        else
        {
            filePath = identifier;
        }
        Stream stream = File.Create(filePath);

        if (obj == null)
        {
            obj = default(T);
        }
        Directory.CreateDirectory(Path.GetDirectoryName(filePath) ?? throw new InvalidOperationException());
        YHSaveGameJsonSerializer serializer = new YHSaveGameJsonSerializer();

        serializer.Serialize(obj, stream);
        stream.Dispose();
    }