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

Commit ef115f0

Browse files
committed
Refactoring
1 parent 4d1584b commit ef115f0

18 files changed

Lines changed: 191 additions & 161 deletions

.github/workflows/deployment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup .NET Core
1919
uses: actions/setup-dotnet@v1
2020
with:
21-
dotnet-version: 3.1.101
21+
dotnet-version: 5.0.100
2222
- name: Install dependencies
2323
run: dotnet restore src/OpenMod.Installer.RocketMod.csproj
2424
- name: Update version

src/Commands/CommandOpenMod.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using Rocket.API;
1+
using OpenMod.Installer.RocketMod.Jobs;
2+
using Rocket.API;
23
using Rocket.Core.Logging;
4+
using SDG.Unturned;
35
using System;
46
using System.Collections.Generic;
57
using System.Linq;
68
using System.Reflection;
7-
using OpenMod.Installer.RocketMod.Jobs;
8-
using SDG.Unturned;
99

1010
namespace OpenMod.Installer.RocketMod.Commands
1111
{
@@ -45,6 +45,7 @@ public void Execute(IRocketPlayer caller, string[] command)
4545

4646
s_Jobs.Add(new OpenModModuleInstallJob());
4747
s_Jobs.Add(new OpenModAssemblyLoadJob());
48+
s_Jobs.Add(new OpenModUnturnedInstallJob());
4849
s_Jobs.Add(new PermissionsExInstallJob());
4950

5051
s_CurrentStep = GetNextStep();
@@ -120,7 +121,7 @@ private void PerformMigration()
120121

121122
private void RemoveRocketModConsoleInput()
122123
{
123-
if (s_RocketModComandWindowsDelegates.Any())
124+
if (s_RocketModComandWindowsDelegates.Count > 0)
124125
{
125126
return;
126127
}
@@ -149,7 +150,7 @@ private void RestoreRocketModConsoleInput()
149150
s_RocketModComandWindowsDelegates.Clear();
150151
}
151152

152-
private bool IsRocketModDelegate(Delegate? @delegate)
153+
private bool IsRocketModDelegate(Delegate @delegate)
153154
{
154155
if (@delegate == null)
155156
{
@@ -181,18 +182,14 @@ private CommandStep GetNextStep()
181182
{
182183
// null -> Permission -> Economy -> null
183184

184-
switch (s_CurrentStep)
185+
return s_CurrentStep switch
185186
{
186-
case null:
187-
return new OpenModPermissionsStep(s_Jobs);
188-
case OpenModPermissionsStep _:
189-
return new OpenModEconomyStep(s_Jobs);
190-
case OpenModEconomyStep _:
191-
return null;
192-
}
193-
194-
// can not happen
195-
throw new InvalidOperationException("Invalid state");
187+
null => new OpenModPermissionsStep(s_Jobs),
188+
OpenModPermissionsStep _ => new OpenModEconomyStep(s_Jobs),
189+
OpenModEconomyStep _ => null,
190+
// can not happen
191+
_ => throw new InvalidOperationException("Invalid state"),
192+
};
196193
}
197194

198195
public AllowedCaller AllowedCaller { get; } = AllowedCaller.Console;

src/Commands/CommandStep.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using System.Collections.Generic;
2-
using OpenMod.Installer.RocketMod.Jobs;
1+
using OpenMod.Installer.RocketMod.Jobs;
2+
using System.Collections.Generic;
33

44
namespace OpenMod.Installer.RocketMod.Commands
55
{

src/Commands/OpenModEconomyStep.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System;
1+
using OpenMod.Installer.RocketMod.Jobs;
22
using System.Collections.Generic;
3-
using OpenMod.Installer.RocketMod.Jobs;
43

54
namespace OpenMod.Installer.RocketMod.Commands
65
{

src/Commands/OpenModPermissionsStep.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System;
1+
using OpenMod.Installer.RocketMod.Jobs;
22
using System.Collections.Generic;
3-
using OpenMod.Installer.RocketMod.Jobs;
43

54
namespace OpenMod.Installer.RocketMod.Commands
65
{

src/Helpers/AssemblyHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class AssemblyHelper
88
{
99
public static Assembly GetAssembly(string name)
1010
{
11-
return AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(d => d.GetName().Name.Equals(name))
11+
return Array.Find(AppDomain.CurrentDomain.GetAssemblies(), d => d.GetName().Name.Equals(name))
1212
?? throw new Exception($"Assembly not found: {name}");
1313
}
1414
}

src/Helpers/AsyncHelperEx.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using Nito.AsyncEx;
2+
using System;
23
using System.Threading.Tasks;
34

45
namespace OpenMod.Installer.RocketMod.Helpers
@@ -7,7 +8,7 @@ public static class AsyncHelperEx
78
{
89
public static void RunSync(Func<Task> task)
910
{
10-
task().GetAwaiter().GetResult();
11+
AsyncContext.Run(task);
1112
}
1213
}
1314
}

src/Helpers/NuGetHelper.cs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
using System;
1+
using OpenMod.Installer.RocketMod.Jobs;
2+
using OpenMod.NuGet;
3+
using System;
24
using System.IO;
3-
using System.Reflection;
45

56
namespace OpenMod.Installer.RocketMod.Helpers
67
{
78
public static class NuGetHelper
89
{
9-
private static object m_NuGetPackageManager;
10-
public static object GetNuGetPackageManager()
10+
private static NuGetPackageManager s_NuGetPackageManager;
11+
12+
public static NuGetPackageManager GetNuGetPackageManager()
1113
{
12-
if (m_NuGetPackageManager != null)
14+
if (s_NuGetPackageManager != null)
1315
{
14-
return m_NuGetPackageManager;
16+
return s_NuGetPackageManager;
1517
}
1618

1719
var workingDirectory = OpenModInstallerPlugin.Instance.OpenModManager.WorkingDirectory;
@@ -24,25 +26,20 @@ public static object GetNuGetPackageManager()
2426

2527
Environment.SetEnvironmentVariable("NUGET_COMMON_APPLICATION_DATA", packagesPath);
2628

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);
31-
32-
ignoreDependenciesMethod.Invoke(m_NuGetPackageManager, new object[]
29+
s_NuGetPackageManager = new NuGetPackageManager(packagesPath)
3330
{
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-
});
44-
45-
return m_NuGetPackageManager;
31+
Logger = new NuGetConsoleLogger()
32+
};
33+
34+
s_NuGetPackageManager.IgnoreDependencies(
35+
"Microsoft.NETCore.Platforms",
36+
"Microsoft.Packaging.Tools",
37+
"NETStandard.Library",
38+
/*"OpenMod.Unturned.Redist",
39+
"OpenMod.UnityEngine.Redist",*/ // todo
40+
"System.IO.FileSystem.Watcher");
41+
42+
return s_NuGetPackageManager;
4643
}
4744
}
4845
}

src/Helpers/ReflectionHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ public static class ReflectionHelper
77
{
88
public static object GetPropertyValue(this object o, string propertyName)
99
{
10-
var property = o.GetType().GetProperty(propertyName, BindingFlags.Static |BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
10+
var property = o.GetType().GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
1111
if (property == null)
1212
{
1313
throw new Exception($"Failed to find {propertyName} in Type {o.GetType()}");
1414
}
15-
15+
1616
return property.GetValue(o);
1717
}
1818

src/Jobs/JobsExecutor.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System;
1+
using Rocket.Core.Logging;
2+
using System;
23
using System.Collections.Generic;
3-
using System.Linq;
4-
using Rocket.Core.Logging;
54

65
namespace OpenMod.Installer.RocketMod.Jobs
76
{
@@ -13,7 +12,7 @@ public static bool Execute(List<IJob> jobsToExecute)
1312

1413
for (var i = 0; i < jobsToExecute.Count; i++)
1514
{
16-
var job = jobsToExecute.ElementAt(i);
15+
var job = jobsToExecute[i];
1716

1817
var jobName = job.GetType().Name;
1918

@@ -45,7 +44,7 @@ private static void Revert(IReadOnlyList<IJob> jobs)
4544
for (var i = (jobs.Count - 1); i >= 0; i--)
4645
{
4746
var job = jobs[i];
48-
if (!(job is IReversibleJob reversibleJob))
47+
if (job is not IReversibleJob reversibleJob)
4948
{
5049
continue;
5150
}

0 commit comments

Comments
 (0)