-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.cs
165 lines (129 loc) · 5.06 KB
/
Extensions.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
/* LICENSE
* This source code is copyrighted.
* All rights reserved.
* Copyright © Ryan Irecki 2013
*/
using System;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Kerbtown
{
public static class Extensions
{
private static ConfigNode _ktSave;
public static void WriteSetting(string settingName, string settingValue)
{
var fileInfo = new FileInfo(GetSaveFilePath());
// File doesn't exist, so we only need to create the file and write the value.
if (!fileInfo.Exists)
{
var rootConfigNode = new ConfigNode();
var persistenceNode = rootConfigNode.AddNode("KerbTownPersistence");
persistenceNode.AddValue(settingName, settingValue);
rootConfigNode.Save(fileInfo.FullName);
_ktSave = rootConfigNode;
return;
}
var rootNode = ConfigNode.Load(fileInfo.FullName);
_ktSave = rootNode.GetNode("KerbTownPersistence") ?? rootNode.AddNode("KerbTownPersistence");
if (_ktSave.HasValue(settingName))
{
_ktSave.SetValue(settingName, settingValue);
}
else
{
_ktSave.AddValue(settingName, settingValue);
}
rootNode.Save(fileInfo.FullName);
}
public static string ReadSetting(string settingName)
{
var fileInfo = new FileInfo(GetSaveFilePath());
if (!fileInfo.Exists) return null;
_ktSave = ConfigNode.Load(fileInfo.FullName).GetNode("KerbTownPersistence");
return _ktSave == null ? null : _ktSave.GetValue(settingName);
}
public static void WriteNode(ConfigNode newNode)
{
var fileInfo = new FileInfo(GetSaveFilePath());
ConfigNode persistenceNode;
if (!fileInfo.Exists)
{
var cNode = new ConfigNode();
persistenceNode = cNode.AddNode("KerbTownPersistence");
persistenceNode.AddNode(newNode);
cNode.Save(fileInfo.FullName);
_ktSave = cNode;
return;
}
_ktSave = ConfigNode.Load(fileInfo.FullName);
persistenceNode = _ktSave.GetNode("KerbTownPersistence");
var nodes = persistenceNode.GetNodes(newNode.name);
var rootID = newNode.GetValue("ID");
bool nodeSet = false;
foreach (var node in from node in nodes let nodeID = node.GetValue("ID") where nodeID == rootID select node)
{
persistenceNode.nodes.Remove(node);
persistenceNode.nodes.Add(newNode);
nodeSet = true;
}
if (nodeSet == false)
{
persistenceNode.nodes.Add(newNode);
}
_ktSave.RemoveNode("KerbTownPersistence");
_ktSave.AddNode(persistenceNode);
_ktSave.Save(GetSaveFilePath());
// Note: SetNode() isn't recursive on descendant ConfigNodes
}
public static ConfigNode ReadNode(string nodeName, string id = "")
{
var fileInfo = new FileInfo(GetSaveFilePath());
if (!fileInfo.Exists) return null;
_ktSave = ConfigNode.Load(fileInfo.FullName);
var persistenceNode = _ktSave.GetNode("KerbTownPersistence");
if (id == "")
return persistenceNode.GetNode(nodeName);
return persistenceNode.nodes.Cast<ConfigNode>().FirstOrDefault(node => node.GetValue("ID") == id);
}
public static string GetSaveFilePath()
{
return string.Format("{0}saves/{1}/KerbTown.cfg", KSPUtil.ApplicationRootPath, HighLogic.SaveFolder);
}
public static void PostScreenMessage(string message, float timeToShow = 3f)
{
ScreenMessages.PostScreenMessage(message, timeToShow, ScreenMessageStyle.UPPER_CENTER);
}
public static void LogError(string message)
{
Debug.LogError("KerbTown: " + message);
}
public static void LogWarning(string message)
{
Debug.LogWarning("KerbTown: " + message);
}
public static void LogInfo(string message)
{
Debug.Log("KerbTown: " + message);
}
public static int SecondLastIndex(this string str, char searchCharacter)
{
int lastIndex = str.LastIndexOf(searchCharacter);
if (lastIndex != -1)
{
return str.LastIndexOf(searchCharacter, lastIndex - 1);
}
return -1;
}
public static int SecondLastIndex(this string str, string searchString)
{
int lastIndex = str.LastIndexOf(searchString, StringComparison.Ordinal);
if (lastIndex != -1)
{
return str.LastIndexOf(searchString, lastIndex - 1, StringComparison.Ordinal);
}
return -1;
}
}
}