Skip to content

Commit 03bfbfe

Browse files
committed
Merge branch 'master' of https://github.com/TorchAPI/Essentials
2 parents c68e9af + d431716 commit 03bfbfe

10 files changed

Lines changed: 221 additions & 69 deletions

‎Essentials/AutoCommand.cs‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class AutoCommand : ViewModel
3131
private string _name;
3232
private float _triggerRatio;
3333
private double _triggerCount;
34+
private bool _isRunning;
3435

3536
[XmlIgnore]
3637
public bool Completed { get; set; }
@@ -57,18 +58,6 @@ public string Name
5758
set => SetValue(ref _name, value);
5859
}
5960

60-
//[Display(Name = "Scheduled Time", GroupName = "Schedule", Description = "Sets a time of day for this command to be run. Format is HH:MM:SS. MUST use 24 hour format! Will be reset to zero if Interval is set.")]
61-
[Display(Visible = false)]
62-
public string ScheduledTime
63-
{
64-
get => _scheduledTime.ToString();
65-
set
66-
{
67-
_scheduledTime = TimeSpan.Parse(value);
68-
OnPropertyChanged();
69-
}
70-
}
71-
7261
[Display(Order = 2, Description = "Sets an interval/Time for this command to be repeated. Format is HH:MM:SS.")]
7362
public string Interval
7463
{
@@ -79,7 +68,6 @@ public string Interval
7968
OnPropertyChanged();
8069
if (CommandTrigger == Trigger.Timed)
8170
{
82-
//ScheduledTime = TimeSpan.Zero.ToString(); //I hate myself for this **FIXED!!!***
8371
_nextRun = DateTime.Now + _interval;
8472
}
8573

@@ -157,6 +145,7 @@ public void Update()
157145

158146
if (_currentStep < Steps.Count) return;
159147
_currentStep = 0;
148+
_cTokenSource?.Dispose();
160149
_nextRun = _trigger == Trigger.Scheduled
161150
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
162151
: _nextRun = DateTime.Now + _interval;
@@ -204,26 +193,53 @@ public override string ToString()
204193
}
205194
}
206195

196+
private CancellationTokenSource _cTokenSource;
207197

208198
/// <summary>
209199
/// Runs the command and all steps immediately, in a new thread
210200
/// </summary>
211-
internal void RunNow()
201+
internal async void RunNow()
212202
{
213-
Task.Run(() =>
203+
_cTokenSource = new CancellationTokenSource();
204+
var token = _cTokenSource.Token;
205+
_isRunning = true;
206+
var steps = new List<CommandStep>(Steps);
207+
await Task.Run(() =>
214208
{
215-
foreach (var step in Steps)
209+
foreach (var step in steps)
216210
{
211+
if (token.IsCancellationRequested)
212+
{
213+
break;
214+
}
217215
step.RunStep();
218216
Thread.Sleep(step.DelaySpan);
219217
}
220-
});
218+
}, token);
219+
220+
_cTokenSource.Dispose();
221+
_isRunning = false;
222+
}
223+
224+
internal void Cancel()
225+
{
226+
Log.Info($"Cancelling autocommand {_name}");
227+
_cTokenSource?.Cancel();
228+
_currentStep = 0;
229+
_nextRun = _trigger == Trigger.Scheduled
230+
? DateTime.Now.Date + _interval + TimeSpan.FromDays(1)
231+
: _nextRun = DateTime.Now + _interval;
221232
}
222233

223234
public override string ToString()
224235
{
225236
return $"{Name} : {_trigger.ToString()} : {Steps.Count}";
226237
}
238+
239+
internal bool IsRunning()
240+
{
241+
return _currentStep > 0 || _isRunning;
242+
}
227243
}
228244

229245
public enum Gtl
@@ -233,7 +249,6 @@ public enum Gtl
233249
Equal
234250
}
235251

236-
//TODO Remove Scheduled
237252
public enum Trigger
238253
{
239254
Disabled,

‎Essentials/AutoCommands.cs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@ public void Start()
4040

4141
private bool CanRun(AutoCommand command)
4242
{
43+
if (MySession.Static?.Ready == false) return false;
44+
4345
switch (command.CommandTrigger)
4446
{
4547
case Trigger.Disabled:
4648
return false;
4749
case Trigger.OnStart:
48-
if (command.Completed || MySession.Static?.Ready == false)break;
50+
if (command.Completed || command.IsRunning())break;
4951
command.Completed = true;
5052
command.RunNow();
51-
5253
break;
5354
case Trigger.Vote:
5455
break;
@@ -73,11 +74,11 @@ private bool CanRun(AutoCommand command)
7374

7475
if (command.Compare == GreaterThan)
7576
{
76-
return MySession.Static.Players.GetOnlinePlayerCount() > command.TriggerCount;
77+
return MySession.Static?.Players.GetOnlinePlayerCount() > command.TriggerCount;
7778
}
7879
else if (command.Compare == LessThan)
7980
{
80-
return MySession.Static.Players.GetOnlinePlayerCount() < command.TriggerCount;
81+
return MySession.Static?.Players.GetOnlinePlayerCount() < command.TriggerCount;
8182
}
8283
break;
8384

‎Essentials/Commands/AdminModule.cs‎

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,142 @@ public void ListPlayers()
6464
return;
6565
}
6666
StringBuilder sb = new StringBuilder();
67-
foreach(var player in MySession.Static.Players.GetOnlinePlayers())
67+
var players = MySession.Static.Players.GetOnlinePlayers();
68+
if (players.Count == 0)
69+
{
70+
Context.Respond("No Players Online");
71+
return;
72+
}
73+
74+
sb.AppendLine($"Found {players.Count} Players on server");
75+
foreach(var player in players)
6876
{
69-
sb.AppendLine();
7077
sb.AppendLine($"{player.DisplayName}");
78+
sb.AppendLine($">PlayerId: {player.Identity.IdentityId}");
79+
sb.AppendLine($">SteamId: {player.Id.SteamId}");
7180
}
7281
if (Context.Player == null)
7382
Context.Respond(sb.ToString());
7483
else if (Context?.Player?.SteamUserId > 0)
7584
{
76-
ModCommunication.SendMessageTo(new DialogMessage("List of Online Players", null, sb.ToString()), Context.Player.SteamUserId);
85+
ModCommunication.SendMessageTo(new DialogMessage("List of Online Players", $"{players.Count} players Online", sb.ToString()), Context.Player.SteamUserId);
7786
}
7887
}
7988

8089

81-
[Command("runauto", "Runs the auto command with the given name immediately")]
90+
[Command("runauto", "Runs the auto command with the given commandName immediately")]
8291
[Permission(MyPromoteLevel.Admin)]
8392
public void RunAuto(string name)
8493
{
8594
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(name));
8695
if (command == null)
8796
{
88-
Context.Respond($"Couldn't find an auto command with the name {name}");
97+
Context.Respond($"Couldn't find an auto command with the commandName {name}");
8998
return;
9099
}
91-
100+
Context.Respond($"Running the autocommand {command.Name}");
92101
command.RunNow();
93102
}
94103

104+
[Command("cancelauto", "Cancels the auto command with the given commandName immediately")]
105+
[Permission(MyPromoteLevel.Admin)]
106+
public void EndAuto(string commandName)
107+
{
108+
var command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(commandName));
109+
if (command == null)
110+
{
111+
Context.Respond($"Couldn't find an auto command with the commandName {commandName}");
112+
return;
113+
}
114+
Context.Respond($"AutoCommand {command.Name} cancelled");
115+
command.Cancel();
116+
}
117+
118+
[Command("cancelautobyindex", "Cancels the auto command with the given index immediately")]
119+
[Permission(MyPromoteLevel.Admin)]
120+
public void EndByOrder(int commandNumber = 0)
121+
{
122+
var commands = new List<AutoCommand>(EssentialsPlugin.Instance.Config.AutoCommands.Where(x=>x.IsRunning()));
123+
124+
if (commands.Count == 0)
125+
{
126+
Context.Respond("No active autocommand found");
127+
return;
128+
}
129+
130+
if (commandNumber < 1 || commandNumber > commands.Count)
131+
{
132+
Context.Respond($"{commandNumber} number is out of range");
133+
return;
134+
}
135+
var command = commands[commandNumber - 1];
136+
if (command == null)
137+
{
138+
Context.Respond($"Couldn't find an auto command from provided number");
139+
return;
140+
}
141+
Context.Respond($"AutoCommand {command.Name} cancelled");
142+
command.Cancel();
143+
}
144+
145+
[Command("listauto", "Lists all detected autocommands ")]
146+
[Permission(MyPromoteLevel.Admin)]
147+
public void ListAuto()
148+
{
149+
var commands = EssentialsPlugin.Instance.Config.AutoCommands;
150+
StringBuilder sb = new StringBuilder();
151+
int count = 1;
152+
153+
foreach (var command in commands)
154+
{
155+
sb.AppendLine((count++) + " " + command.Name);
156+
}
157+
158+
if (Context.Player == null)
159+
{
160+
Context.Respond("Current AutoCommands:");
161+
Context.Respond(sb.ToString());
162+
163+
}
164+
165+
else
166+
{
167+
ModCommunication.SendMessageTo(new DialogMessage("Current AutoCommands",$"Found {commands.Count} Commands",sb.ToString()), Context.Player.SteamUserId);
168+
}
169+
}
170+
171+
[Command("listrunningauto", "Lists all running autocommands ")]
172+
[Permission(MyPromoteLevel.Admin)]
173+
public void ListAutoRunning()
174+
{
175+
var commands = new List<AutoCommand>(EssentialsPlugin.Instance.Config.AutoCommands.Where(x=>x.IsRunning()));
176+
177+
if (commands.Count == 0)
178+
{
179+
Context.Respond("No active autocommand found");
180+
return;
181+
}
182+
StringBuilder sb = new StringBuilder();
183+
int count = 1;
184+
185+
foreach (var command in commands)
186+
{
187+
sb.AppendLine((count++) + " " + command.Name);
188+
}
189+
190+
if (Context.Player == null)
191+
{
192+
Context.Respond("Current AutoCommands:");
193+
Context.Respond(sb.ToString());
194+
195+
}
196+
197+
else
198+
{
199+
ModCommunication.SendMessageTo(new DialogMessage("Current AutoCommands",$"Found {commands.Count} Commands",sb.ToString()), Context.Player.SteamUserId);
200+
}
201+
}
202+
95203
[Command("set toolbar", "Makes your current toolbar the new default toolbar for new players.")]
96204
[Permission(MyPromoteLevel.Admin)]
97205
public void SetToolbar()

‎Essentials/Commands/BlocksModule.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void RemoveSubtype(string subtype)
9292
}
9393

9494
foreach (var x in toRemove)
95-
x.CubeGrid.RazeBlock(x.Position);
95+
x.CubeGrid?.RemoveBlock(x);
9696
Context.Respond($"Removed {toRemove.Count} blocks of subtype {subtype}.");
9797
}
9898

@@ -111,7 +111,7 @@ public void RemoveType(string type)
111111
}
112112

113113
foreach (var x in toRemove)
114-
x.CubeGrid.RazeBlock(x.Position);
114+
x.CubeGrid?.RemoveBlock(x);
115115
Context.Respond($"Removed {toRemove.Count} blocks of type {type}.");
116116
}
117117

@@ -211,7 +211,8 @@ public bool IsBlockTypeOf(MyFunctionalBlock block, BlockCategory category)
211211
case BlockCategory.Power:
212212
return block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Reactor) ||
213213
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_BatteryBlock) ||
214-
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_SolarPanel);
214+
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_SolarPanel) ||
215+
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_FueledPowerProducer);
215216
case BlockCategory.Production:
216217
return block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Assembler) ||
217218
block.BlockDefinition.Id.TypeId == typeof(MyObjectBuilder_Refinery) ||

‎Essentials/Essentials.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
<Compile Include="GridFinder.cs" />
165165
<Compile Include="InfoCommand.cs" />
166166
<Compile Include="Commands\InfoModule.cs" />
167+
<Compile Include="KnownIdsStorage.cs" />
167168
<Compile Include="Patches\ChatMessagePatch.cs" />
168169
<Compile Include="Patches\SessionDownloadPatch.cs" />
169170
<Compile Include="PlayerAccountModule.cs" />

‎Essentials/EssentialsConfig.cs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class EssentialsConfig : ViewModel
2323
public EssentialsConfig()
2424
{
2525
AutoCommands.CollectionChanged += (sender, args) => OnPropertyChanged();
26-
KnownSteamIds.CollectionChanged += (sender, args) => OnPropertyChanged();
2726
InfoCommands.CollectionChanged += (sender, args) => OnPropertyChanged();
2827
}
2928

@@ -95,9 +94,6 @@ public int BackpackLimit
9594
set => SetValue(ref _backpackLimit, value);
9695
}
9796

98-
[Display(Visible=false)]
99-
public ObservableCollection<ulong> KnownSteamIds { get; } = new ObservableCollection<ulong>();
100-
10197
private bool _cutGameTags;
10298
[Display(Name = "Cut Game Tags", GroupName = "Client Join Tweaks", Order = 8, Description = "Cuts mods and blocks limits from matchmaking server info. Prevents from 'error downloading session settings'.")]
10399
public bool CutGameTags

‎Essentials/EssentialsPlugin.cs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class EssentialsPlugin : TorchPluginBase, IWpfPlugin
5454
private HashSet<ulong> _motdOnce = new HashSet<ulong>();
5555
private PatchManager _pm;
5656
private PatchContext _context;
57-
57+
private KnownIdsStorage _knownIds;
5858

5959
public static EssentialsPlugin Instance { get; private set; }
6060
public PlayerAccountModule AccModule = new PlayerAccountModule();
@@ -77,6 +77,8 @@ public override void Init(ITorchBase torch)
7777
string path = Path.Combine(StoragePath, "Essentials.cfg");
7878
Log.Info($"Attempting to load config from {path}");
7979
_config = Persistent<EssentialsConfig>.Load(path);
80+
_knownIds = new KnownIdsStorage(Path.Combine(StoragePath, "Essentials.KnownSteamIds.txt"));
81+
_knownIds.Read();
8082
_sessionManager = Torch.Managers.GetManager<TorchSessionManager>();
8183
if (_sessionManager != null)
8284
_sessionManager.SessionStateChanged += SessionChanged;
@@ -304,10 +306,10 @@ public void SendMotd(MyPlayer player, bool onSessionChanged)
304306

305307
string name = player.Identity?.DisplayName ?? "player";
306308

307-
bool isNewUser = !Config.KnownSteamIds.Contains(id);
309+
bool isNewUser = !_knownIds.Contains(id);
308310
if (isNewUser)
309311
{
310-
Config.KnownSteamIds.Add(id);
312+
_knownIds.Add(id);
311313
}
312314

313315
if (!string.IsNullOrEmpty(Config.MotdUrl) && isNewUser && Config.NewUserMotdUrl)

0 commit comments

Comments
 (0)