forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHarddisk.cs
92 lines (77 loc) · 2.28 KB
/
Harddisk.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class Harddisk : Volume
{
public Harddisk(int size)
{
this.Capacity = size;
}
public Harddisk(ConfigNode node)
{
Capacity = 10000;
foreach (String s in node.GetValues("capacity"))
{
Capacity = Int32.Parse(s);
}
foreach (String s in node.GetValues("volumeName"))
{
Name = s;
}
foreach (ConfigNode fileNode in node.GetNodes("file"))
{
files.Add(new File(fileNode));
}
}
public override bool SaveFile(File file)
{
if (!IsRoomFor(file)) return false;
return base.SaveFile(file);
}
public override int GetFreeSpace()
{
int totalOccupied = 0;
foreach (File p in files)
{
totalOccupied += p.GetSize();
}
return Math.Max(Capacity - totalOccupied, 0);
}
public override bool IsRoomFor(File newFile)
{
int totalOccupied = newFile.GetSize();
foreach (File existingFile in files)
{
// Consider only existing files that don't share a name with the proposed new file
// Because this could be an overwrite situation
if (existingFile.Filename != newFile.Filename)
{
totalOccupied += existingFile.GetSize();
}
}
return (Capacity - totalOccupied >= 0);
}
public override void LoadPrograms(List<File> programsToLoad)
{
foreach (File p in programsToLoad)
{
files.Add(p);
}
}
public override ConfigNode Save(string nodeName)
{
ConfigNode node = new ConfigNode(nodeName);
node.AddValue("capacity", Capacity);
node.AddValue("volumeName", Name);
foreach (File file in files)
{
node.AddNode(file.Save("file"));
}
return node;
}
}
}