-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathKtanePropellerModule.cs
More file actions
214 lines (195 loc) · 11.7 KB
/
KtanePropellerModule.cs
File metadata and controls
214 lines (195 loc) · 11.7 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using RT.Json;
using RT.PropellerApi;
using RT.Serialization;
using RT.Servers;
using RT.Util;
using RT.Util.Consoles;
using RT.Util.ExtensionMethods;
namespace KtaneWeb
{
public sealed partial class KtanePropellerModule : PropellerModuleBase<KtaneSettings>
{
public override string Name => "Repository of Manual Pages for Keep Talking and Nobody Explodes";
private KtaneWebConfig _config;
public override HttpResponse Handle(HttpRequest request) => new KtaneWebSession(_config).EnableAutomatic(request, session => _urlResolver.Handle(request));
public override void Init()
{
Log.Info($"KtaneWeb configuration file: {Settings.ConfigFile}");
#if DEBUG
if (string.IsNullOrWhiteSpace(Settings.ConfigFile))
{
var config = new KtaneWebConfig();
Console.WriteLine();
ConsoleUtil.WriteLine("It appears that you are running KtaneWeb for the first time.".Color(ConsoleColor.White));
tryAgain1:
ConsoleUtil.WriteLine(@"Please provide a location for the JSON settings file (for example: {0/DarkCyan}):".Color(ConsoleColor.Gray).Fmt(@"C:\Path\KtaneWeb.settings.json"));
var path = Console.ReadLine();
try
{
ClassifyJson.SerializeToFile(config, path);
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain1;
}
Console.WriteLine();
tryAgain2:
ConsoleUtil.WriteLine("Do you already have a local clone of the KtaneContent repository that you want the website to use?".Color(ConsoleColor.White));
Console.WriteLine("If yes, please type the full path to that repository. If no, just press Enter.");
var ktaneContent = Console.ReadLine();
var expectedSubfolders = "HTML,More,JSON,Icons".Split(',');
if (string.IsNullOrWhiteSpace(ktaneContent))
{
ConsoleUtil.WriteLine(@"In that case we will create a new clone. I can do that automatically if you have git installed (if you don’t, please abort now).".Color(ConsoleColor.White));
ConsoleUtil.WriteLine("This will take a long time as the repository is large.".Color(ConsoleColor.White));
Console.WriteLine();
tryAgain3:
ConsoleUtil.WriteLine("Please choose a path where you would like all the data stored (for example: {0/DarkCyan}):".Color(ConsoleColor.Gray).Fmt(@"C:\Path\KtaneContent"));
var cloneFolder = Console.ReadLine();
try
{
Directory.CreateDirectory(cloneFolder);
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain3;
}
try
{
config.BaseDir = Path.Combine(cloneFolder, "Public");
CommandRunner.Run("git", "clone", "https://github.com/Timwi/KtaneContent.git", config.BaseDir).Go();
config.MergedPdfsDir = Path.Combine(cloneFolder, "MergedPdfs");
Directory.CreateDirectory(config.MergedPdfsDir);
config.LogfilesDir = Path.Combine(cloneFolder, "Logfiles");
Directory.CreateDirectory(config.LogfilesDir);
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain2;
}
}
else if (expectedSubfolders.Any(s => !Directory.Exists(Path.Combine(ktaneContent, s))))
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {"That folder does not appear to contain KtaneContent.".Color(ConsoleColor.Red)}", null);
ConsoleUtil.WriteLine("(We’re looking for a folder that contains subfolders named: {0/DarkMagenta})".Color(ConsoleColor.Magenta).Fmt(expectedSubfolders.JoinString(", ")));
goto tryAgain2;
}
else
{
var p = ktaneContent;
while (p.EndsWith("\""))
p = Path.GetDirectoryName(p);
config.BaseDir = p;
p = Path.GetDirectoryName(p);
Console.WriteLine();
tryAgain4:
var logfiles = Path.Combine(p, "Logfiles");
ConsoleUtil.WriteLine("Please choose a path where you would like KtaneWeb to store logfiles uploaded through the Logfile Analyzer, or just press Enter to use the default ({0/DarkCyan}):".Color(ConsoleColor.Gray).Fmt(logfiles));
config.LogfilesDir = Console.ReadLine();
if (string.IsNullOrWhiteSpace(config.LogfilesDir))
{
ConsoleUtil.WriteLine("Using default: {0/DarkCyan}".Color(ConsoleColor.Gray).Fmt(logfiles));
config.LogfilesDir = logfiles;
}
try
{
Directory.CreateDirectory(config.LogfilesDir);
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain4;
}
Console.WriteLine();
tryAgain5:
var mergedPdfs = Path.Combine(p, "MergedPdfs");
ConsoleUtil.WriteLine("Please choose a path where you would like KtaneWeb to store merged PDFs, or just press Enter to use the default ({0/DarkCyan}):".Color(ConsoleColor.Gray).Fmt(mergedPdfs));
config.MergedPdfsDir = Console.ReadLine();
if (string.IsNullOrWhiteSpace(config.MergedPdfsDir))
{
ConsoleUtil.WriteLine("Using default: {0/DarkCyan}".Color(ConsoleColor.Gray).Fmt(mergedPdfs));
config.MergedPdfsDir = mergedPdfs;
}
try
{
Directory.CreateDirectory(config.MergedPdfsDir);
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain5;
}
var appPath = PathUtil.AppPathCombine("..", "..");
config.JavaScriptFile = Path.Combine(appPath, "Resources", "KtaneWeb.js");
config.CssFile = Path.Combine(appPath, "Resources", "KtaneWeb.css");
if (!File.Exists(config.JavaScriptFile) || !File.Exists(config.CssFile))
{
Console.WriteLine();
tryAgain6:
ConsoleUtil.WriteLine("Finally, please let me know where you placed the KtaneWeb source code (what you’re running right now):".Color(ConsoleColor.Gray));
appPath = Console.ReadLine();
config.JavaScriptFile = Path.Combine(appPath, "Resources", "KtaneWeb.js");
config.CssFile = Path.Combine(appPath, "Resources", "KtaneWeb.css");
if (!File.Exists(config.JavaScriptFile) || !File.Exists(config.CssFile))
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {"That does not look like the KtaneWeb source code folder.".Color(ConsoleColor.Red)}", null);
goto tryAgain6;
}
}
}
try
{
ClassifyJson.SerializeToFile(config, path);
Settings.ConfigFile = path;
SaveSettings();
}
catch (Exception e)
{
ConsoleUtil.WriteLine($"{"Problem:".Color(ConsoleColor.Magenta)} {e.Message.Color(ConsoleColor.Red)} {$"({e.GetType().FullName})".Color(ConsoleColor.DarkRed)}", null);
goto tryAgain1;
}
Console.WriteLine();
ConsoleUtil.WriteLine("That should be all set up for you now!".Color(ConsoleColor.Green));
ConsoleUtil.WriteLine("Feel free to browse the settings file we just created if you’re curious.".Color(ConsoleColor.DarkGreen));
ConsoleUtil.WriteLine(@"For automatic PDF generation, we are assuming that Google Chrome is at its default location; if not, please change it manually in the JSON file.".Color(ConsoleColor.DarkGreen));
Console.WriteLine();
Console.WriteLine();
}
#endif
var original = File.ReadAllText(Settings.ConfigFile);
_config = ClassifyJson.Deserialize<KtaneWebConfig>(JsonValue.Parse(original));
var rewrite = serializeConfig();
if (rewrite != original)
File.WriteAllText(Settings.ConfigFile, rewrite);
if (!string.IsNullOrWhiteSpace(_config.LogfilesDir))
Directory.CreateDirectory(_config.LogfilesDir);
if (!string.IsNullOrWhiteSpace(_config.PdfTempPath))
Directory.CreateDirectory(_config.PdfTempPath);
if (!string.IsNullOrWhiteSpace(_config.MergedPdfsDir))
Directory.CreateDirectory(_config.MergedPdfsDir);
_logfileFsHandler = new FileSystemHandler(_config.LogfilesDir, new FileSystemOptions { ResponseHeaderProcessor = (h, t) => h.AccessControlAllowOrigin = "*" });
generateTranslationCache();
generateModuleInfoCache();
InitUrlResolver();
}
private void saveConfig()
{
lock (_config)
File.WriteAllText(Settings.ConfigFile, serializeConfig());
}
private static bool customComparison(object a, object b) => a is not string && a is not ValueType && a is not KtaneSouvenirInfo &&
(a is Array aa && b is Array bb
? aa.Length == bb.Length && Enumerable.Range(0, aa.Length).All(i => customComparison(aa.GetValue(i), bb.GetValue(i)))
: Equals(a, b));
private string serializeConfig() => ClassifyJson.Serialize(_config, new ClassifyOptions { SerializationEqualityComparer = new CustomEqualityComparer<object>(customComparison) }).ToStringIndented();
// Make sure that this code matches the JavaScript equivalent exactly. Search for KtanePropellerModule.GetDefaultSortKey in KtaneWeb.js to find it
public static string GetDefaultSortKey(string moduleName) => Regex.Replace(moduleName, @"^The |[^-a-zA-Z0-9 ]", "", RegexOptions.IgnoreCase).ToUpperInvariant();
}
}