This repository was archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathMainService.Mode.cs
187 lines (178 loc) · 6.21 KB
/
MainService.Mode.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (C) 2016-2022 The Neo Project.
//
// The neo-cli is free software distributed under the MIT software
// license, see the accompanying file LICENSE in the main directory of
// the project or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.
using Neo.ConsoleService;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Akka.Util.Internal;
namespace Neo.CLI;
partial class MainService
{
/// <summary>
/// Process "mode list" command.
/// </summary>
[ConsoleCommand("mode list", Category = "Mode Commands")]
private void OnListModes()
{
try
{
Directory.GetDirectories("./Modes/").ForEach(p => ConsoleHelper.Info(p));
}
catch (IOException)
{
}
}
/// <summary>
/// Process "mode save" command
/// <param name="modeName">Mode name</param>
/// </summary>
[ConsoleCommand("mode save", Category = "Mode Commands")]
private void OnSaveMode(string modeName)
{
// if no mode name assigned
if (modeName is null)
{
ConsoleHelper.Error("No mode name assigned.");
return;
}
modeName = modeName.ToLower();
try
{
var targetMode = new DirectoryInfo($"Modes/{modeName}");
// Create the mode if it does not exist
if (!targetMode.Exists) Directory.CreateDirectory(targetMode.FullName);
// Get the config files of the node
MoveModeConfig(modeName.ToLower());
var plugins = new DirectoryInfo("./Plugins");
// Cache directories before we start copying
var dirs = plugins.GetDirectories();
// Save the Plugin files
foreach (var plugin in dirs)
{
foreach (var file in plugin.GetFiles().Where(p => p.Extension == ".json"))
{
file.CopyTo($"Modes/{modeName}/{plugin.Name}.json", true);
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// Process "mode delete" command
/// <param name="modeName">Mode name</param>
/// </summary>
[ConsoleCommand("mode delete", Category = "Mode Commands")]
private void OnDeleteMode(string modeName)
{
try
{
var dir = new DirectoryInfo($"Modes/{modeName.ToLower()}");
if (!dir.Exists)
return;
Directory.Delete(dir.FullName, true);
ConsoleHelper.Info("Mode ", modeName, " was deleted.");
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
/// <summary>
/// Load the target mode, plugin should be according to the mode,
/// if the mode contains the plugin, install the plugin, otherwise delete the plugin
/// </summary>
/// <param name="mode"> name of the mode</param>
/// <exception cref="DirectoryNotFoundException"> if the mode is not found</exception>
private Task LoadMode(string mode)
{
try
{
var dir = new DirectoryInfo($"./Modes/{mode.ToLower()}");
if (!dir.Exists)
throw new DirectoryNotFoundException($"Mode not found: {dir.FullName}");
// Process the plugin
var modePlugins = dir.GetFiles();
modePlugins.ForEach(async p =>
{
var pluginName = Path.GetFileNameWithoutExtension(p.Name);
// if the plugin does not exist, maybe consider install it
if (p.Name.Contains("config")) return;
if (!Directory.Exists($"Plugins/{pluginName}/"))
{
await InstallPluginAsync(pluginName);
}
File.Copy($"Modes/{mode.ToLower()}/{p.Name}",
$"Plugins/{pluginName}/config.json", true);
});
MoveModeConfig(mode.ToLower(), false);
// Get existing plugins and delete them if they are not in the mode
new DirectoryInfo("Plugins/").GetDirectories().ForEach(p =>
{
if (modePlugins.Any(k => string.Compare(Path.GetFileNameWithoutExtension(k.Name), p.Name, StringComparison.OrdinalIgnoreCase) == 0)) return;
if (!File.Exists($"Plugins/{p.Name}/config.json")) return;
try
{
ConsoleHelper.Info("Removing plugin ", p.Name);
Directory.Delete($"Plugins/{p.Name}", true);
}
catch
{
// ignored
}
});
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
return Task.CompletedTask;
}
/// save config.json and config.fs.json to the mode directory
/// <param name="mode"> name of the mode</param>
/// <param name="toMode"></param>
/// <exception cref="DirectoryNotFoundException"> if the mode is not found</exception>
private static void MoveModeConfig(string mode, bool toMode = true)
{
var modeDir = new DirectoryInfo($"./Modes/{mode.ToLower()}");
var configDir = new DirectoryInfo("./");
if (!modeDir.Exists)
throw new DirectoryNotFoundException($"Mode not found: {modeDir.FullName}");
try
{
if (toMode)
{
File.Copy(configDir.FullName + "/config.json",
modeDir.FullName + "/config.json", true);
File.Copy(configDir.FullName + "/config.fs.json",
modeDir.FullName + "/config.fs.json", true);
}
else
{
File.Copy(modeDir.FullName + "/config.json",
configDir.FullName + "/config.json", true);
File.Copy(modeDir.FullName + "/config.fs.json",
configDir.FullName + "/config.fs.json", true);
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}