gutterball-3/Gutterball 3/Assets/Scripts/FileData.cs
SkunkStudios 0b195a0fc1 Custom Balls and Cosmic Alleys
Strike it big with amazing bowling action! Everything that made Gutterball 3D a hit is here - incredible 3D graphics, cool ball-rolling controls, hilarious characters - and more! Eight all-new alleys are waxed and ready for you to toss your choice of 85 unique bowling balls. As you play, earn points and cash and unlock alleys and balls. Even customize a ball with your own settings and images! Addictive bowling fun that will bowl you over!
2025-01-16 16:07:45 +07:00

109 lines
No EOL
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
public static class FileData
{
public static void SaveToSAV<T>(List<T> toSave, string filename)
{
Debug.Log(GetPath(filename));
string content = JsonHelper.ToJson<T>(toSave.ToArray());
WriteFile(GetPath(filename), content);
}
public static void SaveToSAV<T>(T toSave, string filename)
{
string content = JsonUtility.ToJson(toSave);
WriteFile(GetPath(filename), content);
}
public static List<T> ReadListFromSAV<T>(string filename)
{
string content = ReadFile(GetPath(filename));
if (string.IsNullOrEmpty(content) || content == "{}")
{
return new List<T>();
}
List<T> res = JsonHelper.FromJson<T>(content).ToList();
return res;
}
public static T ReadFromSAV<T>(string filename)
{
string content = ReadFile(GetPath(filename));
if (string.IsNullOrEmpty(content) || content == "{}")
{
return default(T);
}
T res = JsonUtility.FromJson<T>(content);
return res;
}
private static string GetPath(string filename)
{
Directory.CreateDirectory(Application.persistentDataPath + "/Save/");
return Application.persistentDataPath + "/Save/" + filename + ".sav";
}
private static void WriteFile(string path, string content)
{
FileStream fileStream = new FileStream(path, FileMode.Create);
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.Write(content);
}
}
private static string ReadFile(string path)
{
if (File.Exists(path))
{
using (StreamReader reader = new StreamReader(path))
{
string content = reader.ReadToEnd();
return content;
}
}
return "";
}
}
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}