forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
186 lines (154 loc) · 5.46 KB
/
Copy pathUtils.cs
File metadata and controls
186 lines (154 loc) · 5.46 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using UnityEngine;
namespace kOS
{
public enum kOSKeys
{
LEFT = 37, UP = 38, RIGHT = 39, DOWN = 40,
DEL = 46,
F1 = 112, F2 = 113, F3 = 114, F4 = 115, F5 = 116, F6 = 117, F7 = 118, F8 = 119, F9 = 120, F10 = 121, F11 = 122, F12 = 123,
PGUP = 33, PGDN = 34, END = 35, HOME = 36, DELETE = 44, INSERT = 45,
BREAK = 19
}
public static class Utils
{
public static List<String> Split(String input, char delimiter, bool ignoreString)
{
input = input.Trim();
List<String> retList = new List<string>();
char[] inputChars = input.ToCharArray();
int start = 0;
for (int i = 0; i < input.Length; i++)
{
if (ignoreString && inputChars[i] == '"')
{
// Skip over strings
i = FindEndOfString(input, i + 1);
}
else if (inputChars[i] == delimiter)
{
retList.Add(input.Substring(start, i - start));
start = i + 1;
}
}
if (start < input.Length - 1)
{
retList.Add(input.Substring(start));
}
return retList;
}
// Find the next unescaped double quote
public static int FindEndOfString(String text, int start)
{
char[] input = text.ToCharArray();
for (int i = start; i < input.Count(); i++)
{
if (input[i] == '"' && input[i - 1] != '\\')
{
return i;
}
}
return -1;
}
public static int BraceMatch(String text, int start)
{
char[] input = text.ToCharArray();
int braceLevel = 0;
for (int i = start; i < input.Count(); i++)
{
if (input[i] == '{')
{
braceLevel++;
}
else if (input[i] == '}')
{
braceLevel--;
if (braceLevel == 0) return i;
}
else if (input[i] == '"')
{
i = FindEndOfString(text, i + 1);
}
}
return -1;
}
public static float ProspectForResource(String resourceName, List<Part> engines)
{
List<Part> visited = new List<Part>();
float total = 0;
foreach (var part in engines)
{
total += ProspectForResource(resourceName, part, ref visited);
}
return total;
}
public static float ProspectForResource(String resourceName, Part engine)
{
List<Part> visited = new List<Part>();
return ProspectForResource(resourceName, engine, ref visited);
}
public static float ProspectForResource(String resourceName, Part part, ref List<Part> visited)
{
float ret = 0;
if (visited.Contains(part))
{
return 0;
}
visited.Add(part);
foreach (PartResource resource in part.Resources)
{
if (resource.resourceName.ToLower() == resourceName.ToLower())
{
ret += (float)resource.amount;
}
}
foreach (AttachNode attachNode in part.attachNodes)
{
if (attachNode.attachedPart != null //if there is a part attached here
&& attachNode.nodeType == AttachNode.NodeType.Stack //and the attached part is stacked (rather than surface mounted)
&& (attachNode.attachedPart.fuelCrossFeed //and the attached part allows fuel flow
)
&& !(part.NoCrossFeedNodeKey.Length > 0 //and this part does not forbid fuel flow
&& attachNode.id.Contains(part.NoCrossFeedNodeKey))) // through this particular node
{
ret += ProspectForResource(resourceName, attachNode.attachedPart, ref visited);
}
}
return ret;
}
public static string[] ProcessParams(string input)
{
String buffer = "";
List<String> output = new List<string>();
for (var i = 0; i < input.Length; i++)
{
char c = input[i];
if (c == '\"')
{
var prevI = i;
i = Expression.FindEndOfString(input, i + 1);
buffer += input.Substring(prevI, i - prevI + 1);
}
else
{
if (c == ',')
{
output.Add(buffer.Trim());
buffer = "";
}
else
{
buffer += c;
}
}
}
if (buffer.Trim().Length > 0) output.Add(buffer.Trim());
return output.ToArray();
}
}
}