MIF_E31221480/Assets/SCRIPT/ScriptSV Scrore&Nama/FileHandler.cs

233 lines
5.4 KiB
C#

using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Linq;
using System;
public static class FileHandler
{
public static void SaveToJSON<T>(List<T> toSave, string filename)
{
Debug.Log(GetPath(filename));
string content = JsonHelper.ToJson<T>(toSave.ToArray());
WriteFile(GetPath(filename), content);
}
public static List<T> ReadFromJSON<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;
}
private static string GetPath(string filename)
{
return Application.persistentDataPath + "/" + filename;
}
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;
}
}
/*using System.Collections.Generic;
using System.IO;
using UnityEngine;
public static class FileHandler
{
public static void SaveToJSON<T>(List<T> dataList, string fileName)
{
string json = JsonUtility.ToJson(new Wrapper<T> { list = dataList }, true);
string path = Path.Combine(Application.persistentDataPath, fileName);
File.WriteAllText(path, json);
}
public static List<T> ReadFromJSON<T>(string fileName)
{
string path = Path.Combine(Application.persistentDataPath, fileName);
if (!File.Exists(path))
return new List<T>();
string json = File.ReadAllText(path);
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.list ?? new List<T>();
}
[System.Serializable]
private class Wrapper<T>
{
public List<T> list;
}
}
/*
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Linq;
using System;
public static class FileHandler
{
public static void SaveToJSON<T>(List<T> toSave, string filename)
{
Debug.Log (GetPath (filename));
string content = JsonHelper.ToJson<T>(toSave.ToArray());
WriteFile(GetPath(filename), content);
}
public static void SaveToJSON<T>(T toSave, string filename)
{
string content = JsonUtility.ToJson (toSave);
WriteFile(GetPath(filename), content);
}
public static List<T> ReadListFromJSON<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 ReadFromJSON<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)
{
return Application.persistentDataPath + "/" + filename;
}
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;
}
}
*/