forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolume.cs
231 lines (183 loc) · 6.07 KB
/
Volume.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class Volume
{
public int Capacity = -1;
public String Name = "";
protected List<File> files = new List<File>();
public bool Renameable = true;
public virtual File GetByName(String name)
{
foreach (File p in files)
{
if (p.Filename.ToUpper() == name.ToUpper()) return p;
}
return null;
}
public virtual void AppendToFile(string name, string str)
{
File file = GetByName(name);
if (file == null)
{
file = new File(name);
}
file.Add(str);
SaveFile(file);
}
public virtual void DeleteByName(String name)
{
foreach (File p in files)
{
if (p.Filename.ToUpper() == name.ToUpper())
{
files.Remove(p);
return;
}
}
}
public virtual bool SaveFile(File file)
{
DeleteByName(file.Filename);
files.Add(file);
return true;
}
public virtual int GetFreeSpace() { return -1; }
public virtual bool IsRoomFor(File newFile) { return true; }
public virtual void LoadPrograms(List<File> programsToLoad) { }
public virtual ConfigNode Save(string nodeName) { return new ConfigNode(nodeName); }
public virtual List<FileInfo> GetFileList()
{
List<FileInfo> retList = new List<FileInfo>();
foreach (File file in files)
{
retList.Add(new FileInfo(file.Filename, file.GetSize()));
}
return retList;
}
public virtual bool CheckRange()
{
return true;
}
}
public class Archive : Volume
{
public string ArchiveFolder = GameDatabase.Instance.PluginDataFolder + "/Plugins/PluginData/Archive/";
private Vessel vessel;
public Archive(Vessel vessel)
{
this.vessel = vessel;
Renameable = false;
Name = "Archive";
loadAll();
}
public override bool IsRoomFor(File newFile)
{
return true;
}
private void loadAll()
{
Directory.CreateDirectory(ArchiveFolder);
// Attempt to migrate files from old archive drive
if (KSP.IO.File.Exists<File>(HighLogic.fetch.GameSaveFolder + "/arc"))
{
var reader = KSP.IO.BinaryReader.CreateForType<File>(HighLogic.fetch.GameSaveFolder + "/arc");
int fileCount = reader.ReadInt32();
for (int i = 0; i < fileCount; i++)
{
try
{
String filename = reader.ReadString();
String body = reader.ReadString();
File file = new File(filename);
file.Deserialize(body);
files.Add(file);
SaveFile(file);
}
catch (EndOfStreamException e)
{
break;
}
}
reader.Close();
KSP.IO.File.Delete<File>(HighLogic.fetch.GameSaveFolder + "/arc");
}
}
public override File GetByName(string name)
{
try
{
using (StreamReader infile = new StreamReader(ArchiveFolder + name + ".txt", true))
{
String fileBody = infile.ReadToEnd().Replace("\r\n", "\n") ;
File retFile = new File(name);
retFile.Deserialize(fileBody);
base.DeleteByName(name);
files.Add(retFile);
return retFile;
}
}
catch (Exception e)
{
return null;
}
}
public override bool SaveFile(File file)
{
base.SaveFile(file);
if (!CheckRange())
{
throw new kOSException("Volume is out of range.");
}
Directory.CreateDirectory(ArchiveFolder);
try
{
using (StreamWriter outfile = new StreamWriter(ArchiveFolder + file.Filename + ".txt", false))
{
String fileBody = file.Serialize();
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
// Only evil windows gets evil windows line breaks
fileBody = fileBody.Replace("\n", "\r\n");
}
outfile.Write(fileBody);
}
}
catch (Exception e)
{
return false;
}
return true;
}
public override void DeleteByName(string name)
{
System.IO.File.Delete(ArchiveFolder + name + ".txt");
}
public override List<FileInfo> GetFileList()
{
var retList = new List<FileInfo>();
try
{
foreach (var file in Directory.GetFiles(ArchiveFolder, "*.txt"))
{
var sysFileInfo = new System.IO.FileInfo(file);
var fileInfo = new kOS.FileInfo(sysFileInfo.Name.Substring(0, sysFileInfo.Name.Length - 4), (int)sysFileInfo.Length);
retList.Add(fileInfo);
}
}
catch (DirectoryNotFoundException e)
{
}
return retList;
}
public override bool CheckRange()
{
return (VesselUtils.GetDistanceToKerbinSurface(vessel) < VesselUtils.GetCommRange(vessel));
}
}
}