forked from pellinor0/TweakScale
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
CheckEngine.cs
148 lines (126 loc) · 5.35 KB
/
CheckEngine.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
/*
This file is part of TweakScale /L
© 2018-2022 LisiasT
© 2015-2018 pellinor
© 2014 Gaius Godspeed and Biotronic
TweakScale /L is double licensed, as follows:
* SKL 1.0 : https://ksp.lisias.net/SKL-1_0.txt
* GPL 2.0 : https://www.gnu.org/licenses/gpl-2.0.txt
And you are allowed to choose the License that better suit your needs.
TweakScale /L is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You should have received a copy of the SKL Standard License 1.0
along with TweakScale /L. If not, see <https://ksp.lisias.net/SKL-1_0.txt>.
You should have received a copy of the GNU General Public License 2.0
along with TweakScale /L. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using KSPe;
namespace TweakScale.Sanitizer.Engine
{
public class Check
{
public class Job
{
public readonly string name;
public readonly string descriptionText;
public readonly string issueText;
public readonly string issueLink;
public readonly string supportLink;
public readonly string module;
public readonly string[] conflicts;
public readonly string[] dependencies;
public Job(ConfigNodeWithSteroids cn)
{
this.name = cn.GetValue("name");
this.descriptionText = cn.GetValue("description_text", "");
this.issueText = cn.GetValue("issue_text", "");
this.issueLink = System.Uri.UnescapeDataString(cn.GetValue("issue_link",""));
this.supportLink = System.Uri.UnescapeDataString(cn.GetValue("support_link",""));
this.module = cn.GetValue("module_affected");
this.conflicts = cn.GetValues("module_conflicting");
this.dependencies = cn.GetValues("module_dependency");
}
public string GetLogDescription(string defaultDesc)
{
string r = string.IsNullOrEmpty(this.descriptionText) ? defaultDesc : this.descriptionText;
if (!string.IsNullOrEmpty(this.issueText)) r += string.Format(". {0}", this.issueText);
if (!string.IsNullOrEmpty(this.issueLink)) r += string.Format("({0})" + this.issueLink);
return r;
}
public string GetScreenDescription(string defaultDesc)
{
string r = string.IsNullOrEmpty(this.descriptionText) ? defaultDesc : this.descriptionText;
if (!string.IsNullOrEmpty(this.supportLink)) r += string.Format(". Visit <a href=\"{0}\">this link</a> for further information", this.issueText);
return r;
}
}
public class Result
{
public readonly Job job;
public readonly AvailablePart availablePart;
public readonly Part prefab;
public string[] Conflicts { get; private set;}
public string[] MissingDependencies { get; private set;}
public bool IsProblematic => !(0 == this.Conflicts.Length && 0 == this.MissingDependencies.Length);
public Result(Job job, AvailablePart p, Part prefab)
{
this.job = job;
this.availablePart = p;
this.prefab = prefab;
}
public void Execute()
{
List<string> conflicts = new List<string>();
List<string> missing = new List<string>();
if(this.prefab.Modules.Contains("TweakScale")) // Better safe than sorry. Someone eventually will call this without TweakScale installed!
{
foreach(string module in this.job.conflicts) if(this.prefab.Modules.Contains(module))
conflicts.Add(module);
foreach(string module in this.job.dependencies) if(!this.prefab.Modules.Contains(module))
missing.Add(module);
}
this.Conflicts = conflicts.ToArray();
this.MissingDependencies = missing.ToArray();
}
public string ToLog()
{
if (0 != this.Conflicts.Length && 0 != this.MissingDependencies.Length)
return string.Format("{0} ({1}) passed the sanity check {2}", this.availablePart.name, this.availablePart.title, this.job.name);
return string.Format("{0} ({1}) failed the sanity check {2} due {3}", this.availablePart.name, this.availablePart.title, this.job.name, this.job.GetLogDescription(this.ToProblems()));
}
public string ToScreen()
{
if (0 != this.Conflicts.Length && 0 != this.MissingDependencies.Length)
return string.Format("{0} ({1}) passed the sanity check {2}", this.availablePart.name, this.availablePart.title, this.job.name);
return string.Format("{0} ({1}) failed the sanity check {2} due {3}", this.availablePart.name, this.availablePart.title, this.job.name, this.job.GetScreenDescription(this.ToProblems()));
}
public string ToProblems()
{
if (0 != this.Conflicts.Length && 0 != this.MissingDependencies.Length)
return this.ToConflicts() + " and " + this.ToMissingDependencies();
if (0 != this.Conflicts.Length)
return this.ToConflicts();
if (0 != this.MissingDependencies.Length)
return this.ToMissingDependencies();
return "";
}
private string ToMissingDependencies() =>
string.Format("have a conflict between {0} and {1}", this.job.module, string.Join(",", this.Conflicts));
private string ToConflicts() =>
string.Format("{0} didn't met dependency(ies) {1}", this.job.module, string.Join(",", this.MissingDependencies));
}
private static Check INSTANCE = null;
internal static Check Instance = INSTANCE??(INSTANCE = new Check());
protected Check() {}
public Result Execute(Job job, AvailablePart p, Part part)
{
Result r = new Result(job, p, part);
r.Execute();
Log.detail(r.ToLog());
return r;
}
}
}