-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFileSystem.cs
130 lines (102 loc) · 3.49 KB
/
FileSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace UnityParrot
{
public class FileSystem
{
private string m_basePath = "UnityParrot";
public static FileSystem Configuration = new FileSystem("UnityParrot\\Configuration");
public Func<string, string> Format = null;
public FileSystem() { }
public FileSystem(string basePath)
{
m_basePath = basePath;
Directory.CreateDirectory(m_basePath);
}
public static class Formats
{
public static string TimePrefix(string data)
{
var now = DateTime.Now;
var time = $"{now.Day}/{now.Month}/{now.Year} {now.Hour}:{now.Minute}:{now.Second}";
return $"{time} >> {data}";
}
}
public T LoadJson<T>(string fileName)
{
return JsonUtility.FromJson<T>(File.ReadAllText($"{m_basePath}\\{fileName}"));
//return JsonConvert.DeserializeObject<T>(File.ReadAllText($"{m_basePath}\\{fileName}"));
}
public T TryLoadJson<T>(string fileName)
{
if (!FileExists(fileName))
{
return default(T);
}
return LoadJson<T>(fileName);
}
public void SaveJson<T>(string fileName, T data)
{
File.WriteAllText($"{m_basePath}\\{fileName}", JsonUtility.ToJson(data, true));
//File.WriteAllText($"{m_basePath}\\{fileName}", JsonConvert.SerializeObject(data, Formatting.Indented));
}
public string LoadText(string fileName)
{
return File.ReadAllText($"{m_basePath}\\{fileName}");
}
public string[] LoadLines(string fileName)
{
return File.ReadAllLines($"{m_basePath}\\{fileName}");
}
public void SaveText(string fileName, string data, bool append = false)
{
string dataCopy = data;
if (Format != null)
{
dataCopy = Format(data);
}
string path = $"{m_basePath}\\{fileName}";
Directory.CreateDirectory(Path.GetDirectoryName(path));
if (append)
{
File.AppendAllText(path, dataCopy);
return;
}
File.WriteAllText(path, dataCopy);
}
public void SaveText(string fileName, string[] data, bool append = false)
{
string[] dataCopy = data;
if (Format != null)
{
for (int i = 0; i < dataCopy.Length; i++)
{
dataCopy[i] = Format(data[i]);
}
}
string path = $"{m_basePath}\\{fileName}";
Directory.CreateDirectory(Path.GetDirectoryName(path));
if (append)
{
File.AppendAllText(path, string.Join("\n", dataCopy));
return;
}
File.WriteAllLines(path, dataCopy);
}
public void SaveText(string fileName, IEnumerable<string> data, bool append = false)
{
SaveText($"{m_basePath}\\{fileName}", data.ToArray(), append);
}
public bool FileExists(string fileName)
{
return File.Exists($"{m_basePath}\\{fileName}");
}
public string GetFilePath(string fileName)
{
return $"{m_basePath}\\{fileName}";
}
}
}