Skip to content
This repository was archived by the owner on Jun 27, 2024. It is now read-only.

Commit 4d1584b

Browse files
committed
Bug fixes
1 parent 1cbdd77 commit 4d1584b

16 files changed

Lines changed: 334 additions & 135 deletions

src/Commands/CommandOpenMod.cs

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Linq;
6+
using System.Reflection;
67
using OpenMod.Installer.RocketMod.Jobs;
78
using SDG.Unturned;
89

@@ -11,6 +12,7 @@ namespace OpenMod.Installer.RocketMod.Commands
1112
public class CommandOpenMod : IRocketCommand
1213
{
1314
private static readonly List<IJob> s_Jobs = new List<IJob>();
15+
private static readonly List<CommandWindowInputted> s_RocketModComandWindowsDelegates = new List<CommandWindowInputted>();
1416
private static CommandStep s_CurrentStep;
1517

1618
public void Execute(IRocketPlayer caller, string[] command)
@@ -35,8 +37,9 @@ public void Execute(IRocketPlayer caller, string[] command)
3537
}
3638

3739
Reset();
38-
Logger.Log("Starting OpenMod installation.");
39-
Logger.Log("Type \"cancel\" to cancel installation.");
40+
Logger.Log("Starting OpenMod installation..", ConsoleColor.DarkCyan);
41+
Logger.Log("Visit https://openmod.github.io/openmod-docs for information.", ConsoleColor.DarkCyan);
42+
Logger.Log("Type \"cancel\" to cancel.", ConsoleColor.Cyan);
4043

4144
BindCommandInput();
4245

@@ -50,20 +53,23 @@ public void Execute(IRocketPlayer caller, string[] command)
5053
private void BindCommandInput()
5154
{
5255
CommandWindow.onCommandWindowInputted += OnCommandWindowInputted;
56+
RemoveRocketModConsoleInput();
5357
}
5458

5559
private void UnbindCommandInput()
5660
{
5761
CommandWindow.onCommandWindowInputted -= OnCommandWindowInputted;
62+
RestoreRocketModConsoleInput();
5863
}
5964

6065
private void OnCommandWindowInputted(string text, ref bool shouldexecutecommand)
6166
{
67+
shouldexecutecommand = false;
6268
text = text.Trim();
6369

6470
if (text.Equals("cancel", StringComparison.OrdinalIgnoreCase))
6571
{
66-
Logger.Log("Aborting OpenMod installation.");
72+
Logger.Log("OpenMod installation aborted.", ConsoleColor.DarkRed);
6773
Reset();
6874
return;
6975
}
@@ -77,31 +83,92 @@ private void OnCommandWindowInputted(string text, ref bool shouldexecutecommand)
7783
{
7884
PerformMigration();
7985
}
86+
else
87+
{
88+
PrintStep(s_CurrentStep);
89+
}
8090
}
81-
catch (InvalidOperationException)
91+
catch (InvalidChoiceException ex)
8292
{
83-
Logger.Log($"Invalid choice: {text}.");
93+
Logger.Log($"Invalid choice: {ex.Choice}. Type \"cancel\" to cancel OpenMod installation.", ConsoleColor.Red);
8494
PrintStep(s_CurrentStep);
8595
}
8696
}
8797

98+
private void PrintStep(CommandStep step)
99+
{
100+
Logger.Log(string.Empty);
101+
Logger.Log(step.Text, ConsoleColor.DarkCyan);
102+
103+
foreach (var choice in step.Choices)
104+
{
105+
Logger.Log($" {choice.Name}: {choice.Description}", ConsoleColor.Cyan);
106+
}
107+
}
108+
88109
private void PerformMigration()
89110
{
90-
JobsExecutor.Execute(s_Jobs);
91-
Reset();
111+
try
112+
{
113+
JobsExecutor.Execute(s_Jobs);
114+
}
115+
finally
116+
{
117+
Reset();
118+
}
92119
}
93120

94-
private void PrintStep(CommandStep step)
121+
private void RemoveRocketModConsoleInput()
95122
{
96-
Logger.Log(string.Empty);
97-
Logger.Log(step.Text);
123+
if (s_RocketModComandWindowsDelegates.Any())
124+
{
125+
return;
126+
}
98127

99-
foreach (var choice in step.Choices)
128+
var commandWindowInputedInvocationList = CommandWindow.onCommandWindowInputted.GetInvocationList();
129+
foreach (var @delegate in commandWindowInputedInvocationList)
100130
{
101-
Logger.Log($" {choice.Name}: {choice.Description}");
131+
if (!IsRocketModDelegate(@delegate))
132+
{
133+
continue;
134+
}
135+
136+
var rocketModDelegate = (CommandWindowInputted)@delegate;
137+
CommandWindow.onCommandWindowInputted -= rocketModDelegate;
138+
s_RocketModComandWindowsDelegates.Add(rocketModDelegate);
102139
}
103140
}
104141

142+
private void RestoreRocketModConsoleInput()
143+
{
144+
foreach (var rocketModDelegate in s_RocketModComandWindowsDelegates)
145+
{
146+
CommandWindow.onCommandWindowInputted += rocketModDelegate;
147+
}
148+
149+
s_RocketModComandWindowsDelegates.Clear();
150+
}
151+
152+
private bool IsRocketModDelegate(Delegate? @delegate)
153+
{
154+
if (@delegate == null)
155+
{
156+
return false;
157+
}
158+
159+
var methodInfo = @delegate.GetMethodInfo();
160+
var assembly = methodInfo?.DeclaringType?.Assembly;
161+
162+
if (assembly == null)
163+
{
164+
return false;
165+
}
166+
167+
var assemblyName = assembly.GetName();
168+
return assemblyName.Name.Equals("Rocket.Unturned")
169+
|| assemblyName.Name.Equals("Rocket.Core");
170+
}
171+
105172
private void Reset()
106173
{
107174
s_CurrentStep = null;

src/Commands/CommandStepChoice.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ public class CommandStepChoice
44
{
55
public string Name { get; set; }
66
public string Description { get; set; }
7-
public string Value { get; set; }
87
}
98
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace OpenMod.Installer.RocketMod.Commands
4+
{
5+
public sealed class InvalidChoiceException : Exception
6+
{
7+
public string Choice { get; }
8+
9+
public InvalidChoiceException(string choice)
10+
{
11+
Choice = choice;
12+
}
13+
}
14+
}

src/Commands/OpenModEconomyStep.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,31 @@ public OpenModEconomyStep(List<IJob> jobs) : base(jobs)
1717
new CommandStepChoice
1818
{
1919
Name = "RocketMod (recommended)",
20-
Value = "OpenMod will use RocketMod Uconomy for economy."
20+
Description = "OpenMod will use RocketMod Uconomy for economy."
2121
},
2222
new CommandStepChoice
2323
{
2424
Name = "OpenMod",
25-
Value = "RocketMod will use OpenMod's economy system. Works with plugins that use Uconomy. Will import existing data from Uconomy."
25+
Description = "RocketMod will use OpenMod's economy system. Works with plugins that use Uconomy. Will import existing data from Uconomy."
2626
}
2727
};
2828

2929
public override void OnChoice(string choice)
3030
{
3131
switch (choice.ToLowerInvariant())
3232
{
33-
case "RocketMod":
34-
break;
35-
case "OpenMod":
33+
case "rm":
34+
case "rocket":
35+
case "rocketmod":
36+
return;
37+
case "om":
38+
case "openmod":
3639
Jobs.Add(new OpenModEconomyInstallJob());
3740
Jobs.Add(new MigrateEconomyJob());
38-
break;
41+
return;
3942
}
4043

41-
throw new InvalidOperationException();
44+
throw new InvalidChoiceException(choice);
4245
}
4346
}
4447
}

src/Commands/OpenModPermissionsStep.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,30 @@ public OpenModPermissionsStep(List<IJob> jobs) : base(jobs)
1717
new CommandStepChoice
1818
{
1919
Name = "RocketMod (recommended)",
20-
Value = "OpenMod will use RocketMod's Permissions.config.xml for permissions."
20+
Description = "OpenMod will use RocketMod's Permissions.config.xml for permissions."
2121
},
2222
new CommandStepChoice
2323
{
2424
Name = "OpenMod",
25-
Value = "RocketMod will use OpenMod's permission system. Will import existing permissions from RocketMod's Permissions.config.xml."
25+
Description = "RocketMod will use OpenMod's permission system. Will import existing permissions from RocketMod's Permissions.config.xml."
2626
}
2727
};
2828

2929
public override void OnChoice(string choice)
3030
{
3131
switch (choice.ToLowerInvariant())
3232
{
33-
case "RocketMod":
34-
break;
35-
case "OpenMod":
33+
case "rm":
34+
case "rocket":
35+
case "rocketmod":
36+
return;
37+
case "om":
38+
case "openmod":
3639
Jobs.Add(new MigratePermissionsJob());
37-
break;
40+
return;
3841
}
3942

40-
throw new InvalidOperationException();
43+
throw new InvalidChoiceException(choice);
4144
}
4245
}
4346
}

src/Helpers/AssemblyHelper.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace OpenMod.Installer.RocketMod.Helpers
6+
{
7+
public static class AssemblyHelper
8+
{
9+
public static Assembly GetAssembly(string name)
10+
{
11+
return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(d => d.GetName().Name.Equals(name))
12+
?? throw new Exception($"Assembly not found: {name}");
13+
}
14+
}
15+
}

src/Helpers/AsyncHelperEx.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace OpenMod.Installer.RocketMod.Helpers
5+
{
6+
public static class AsyncHelperEx
7+
{
8+
public static void RunSync(Func<Task> task)
9+
{
10+
task().GetAwaiter().GetResult();
11+
}
12+
}
13+
}

src/Helpers/Extensions.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Helpers/NuGetHelper.cs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using OpenMod.NuGet;
2-
using System;
1+
using System;
32
using System.IO;
3+
using System.Reflection;
44

55
namespace OpenMod.Installer.RocketMod.Helpers
66
{
77
public static class NuGetHelper
88
{
9-
private static NuGetPackageManager m_NuGetPackageManager;
10-
public static NuGetPackageManager GetNuGetPackageManager()
9+
private static object m_NuGetPackageManager;
10+
public static object GetNuGetPackageManager()
1111
{
1212
if (m_NuGetPackageManager != null)
1313
{
@@ -24,19 +24,23 @@ public static NuGetPackageManager GetNuGetPackageManager()
2424

2525
Environment.SetEnvironmentVariable("NUGET_COMMON_APPLICATION_DATA", packagesPath);
2626

27-
// NuGetPackageManager should auto create directory
28-
m_NuGetPackageManager = new NuGetPackageManager(packagesPath)
29-
{
30-
Logger = new NuGetConsoleLogger()
31-
};
27+
var assembly = AssemblyHelper.GetAssembly("OpenMod.NuGet");
28+
var nugetPackageManagerType = assembly.GetType("OpenMod.NuGet.NuGetPackageManager");
29+
var ignoreDependenciesMethod = nugetPackageManagerType.GetMethod("IgnoreDependencies", BindingFlags.Instance | BindingFlags.Public);
30+
m_NuGetPackageManager = Activator.CreateInstance(nugetPackageManagerType, packagesPath);
3231

33-
m_NuGetPackageManager.IgnoreDependencies(
34-
"Microsoft.NETCore.Platforms",
35-
"Microsoft.Packaging.Tools",
36-
"NETStandard.Library",
37-
"OpenMod.Unturned.Redist",
38-
"OpenMod.UnityEngine.Redist",
39-
"System.IO.FileSystem.Watcher");
32+
ignoreDependenciesMethod.Invoke(m_NuGetPackageManager, new object[]
33+
{
34+
new[]
35+
{
36+
"Microsoft.NETCore.Platforms",
37+
"Microsoft.Packaging.Tools",
38+
"NETStandard.Library",
39+
"OpenMod.Unturned.Redist",
40+
"OpenMod.UnityEngine.Redist",
41+
"System.IO.FileSystem.Watcher"
42+
}
43+
});
4044

4145
return m_NuGetPackageManager;
4246
}

src/Helpers/ReflectionHelper.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Reflection;
3+
4+
namespace OpenMod.Installer.RocketMod.Helpers
5+
{
6+
public static class ReflectionHelper
7+
{
8+
public static object GetPropertyValue(this object o, string propertyName)
9+
{
10+
var property = o.GetType().GetProperty(propertyName, BindingFlags.Static |BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
11+
if (property == null)
12+
{
13+
throw new Exception($"Failed to find {propertyName} in Type {o.GetType()}");
14+
}
15+
16+
return property.GetValue(o);
17+
}
18+
19+
public static void SetPropertyValue(this object o, string propertyName, object value)
20+
{
21+
var property = o.GetType().GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
22+
if (property == null)
23+
{
24+
throw new Exception($"Failed to find {propertyName} in Type {o.GetType()}");
25+
}
26+
27+
property.SetValue(o, value);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)