-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingListManager.cs
68 lines (57 loc) · 1.99 KB
/
SettingListManager.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
using System.Collections.Generic;
using KSP.IO;
namespace FlightComputer
{
public abstract class SettingListManager
{
public string SettingsFile;
protected bool WriteNeeded;
protected LogManager Logger;
public abstract List<string> GetLinesToWrite();
public abstract void AddItem(string itemText = null);
public void LoadFromFile()
{
this.Logger.Log("Loading settings list from: " + this.SettingsFile);
if (File.Exists<FlightComputer>(this.SettingsFile))
{
string[] settingListLines = File.ReadAllLines<FlightComputer>(this.SettingsFile);
foreach (string line in settingListLines)
{
string currentLine = line.Trim();
// Skip all this jazz if the line is completely empty.
if (currentLine.Length == 0)
{
continue;
}
this.AddItem(currentLine);
}
}
else
{
this.Logger.Error("Unable to find specified settings list file.");
}
}
public void SaveToFile()
{
if (this.WriteNeeded)
{
this.Logger.Log("Saving settings list file.");
if (File.Exists<FlightComputer>(this.SettingsFile))
{
TextWriter file = File.CreateText<FlightComputer>(this.SettingsFile);
List<string> linesToWrite = this.GetLinesToWrite();
foreach (string line in linesToWrite)
{
file.WriteLine(line);
}
file.Close();
this.WriteNeeded = false;
}
else
{
this.Logger.Error("No setting list available to write to file.");
}
}
}
}
}