Skip to content
Permalink
Browse files

Introduce Rocket.NuGet for shared NuGet code.

  • Loading branch information
Trojaner committed Mar 4, 2019
1 parent 0bf8a10 commit 45d24f3a3574a136601457b4f0cc25b4c0656106
@@ -2,21 +2,20 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<AssemblyName>Rocket.API</AssemblyName>
<Description>RocketMod .NET Game Server Plugin Framework Core definitions</Description>
<Description>RocketMod .NET Game Server Plugin Framework Core definitions</Description>
<RootNamespace>Rocket.API</RootNamespace>
<OutputType>Library</OutputType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.0.0</Version> <!-- Set by CI -->
<PackageVersion>0.0.0.0</PackageVersion> <!-- Set by CI -->
<AssemblyVersion>0.0.0.0</AssemblyVersion> <!-- Set by CI -->
<FileVersion>0.0.0.0</FileVersion> <!-- Set by CI -->
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net461' ">
<!-- Stuff which isn't compatible with NET35 -->
<PackageReference Include="System.Numerics.Vectors">
<PackageVersion>0.0.0.0</PackageVersion> <!-- Set by CI -->
<AssemblyVersion>0.0.0.0</AssemblyVersion> <!-- Set by CI -->
<FileVersion>0.0.0.0</FileVersion> <!-- Set by CI -->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Numerics.Vectors">
<Version>4.5.0</Version>
</PackageReference>
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\props\SharedNuGet.props" />
</Project>
@@ -10,6 +10,7 @@
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Rocket.NuGet;

namespace Rocket.Core.Commands.RocketCommands
{
@@ -15,8 +15,8 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Rocket.NuGet;

//todo: find and install dependencies
namespace Rocket.Core.Plugins.NuGet
{
public class NuGetPluginLoader : ClrPluginLoader
@@ -89,7 +89,9 @@ public override async Task InitAsync()
Logger.LogDebug($"[{GetType().Name}] Initializing NuGet.");

CreateConfiguration();
nugetInstaller = new NuGetInstaller(logger, PackagesDirectory);

var adapter = new NuGetLoggerAdapter(logger);
nugetInstaller = new NuGetInstaller(adapter, PackagesDirectory);

PluginManagerInitEvent pluginManagerInitEvent =
new PluginManagerInitEvent(this, EventExecutionTargetContext.Sync);
@@ -194,33 +196,32 @@ protected virtual async Task<NuGetInstallResult> InstallOrUpdateAsync(string pac
return new NuGetInstallResult(NuGetInstallCode.PackageNotFound);

var enabledRepos = Repositories.Where(d => d.IsEnabled).ToList();
var repoUrls = enabledRepos.Select(c => c.Url).ToList();

Repository repo;
string repo;
if (repoName == null)
{
repo = await nugetInstaller.FindRepositoryForPackageAsync(enabledRepos, packageName, version, isPreRelease);
repo = await nugetInstaller.FindRepositoryForPackageAsync(repoUrls, packageName, version, isPreRelease);
if (repo == null)
{
return new NuGetInstallResult(NuGetInstallCode.PackageNotFound);
}
}
else
{
repo = enabledRepos.FirstOrDefault(c => c.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase));
}

if (repo == null)
{
return new NuGetInstallResult(NuGetInstallCode.PackageNotFound);
repo = enabledRepos.FirstOrDefault(c => c.Name.Equals(repoName, StringComparison.OrdinalIgnoreCase))?.Url;
if (repo == null)
{
return new NuGetInstallResult(NuGetInstallCode.RepositoryNotFound);
}
}

if (isUpdate
|| Packages.NugetPackages.Any(c => c.Id.Equals(packageName, StringComparison.OrdinalIgnoreCase))
|| PackageExists(packageName))
await UninstallAsync(packageName);

if (!repo.IsEnabled)
{
return new NuGetInstallResult(NuGetInstallCode.RepositoryNotFound);
}

var packages = (await nugetInstaller.QueryPackagesAsync(enabledRepos, packageName, version, isPreRelease))
var packages = (await nugetInstaller.QueryPackagesAsync(repoUrls, packageName, version, isPreRelease))
.ToList();

if (packages.Count == 0)
@@ -312,7 +313,7 @@ public virtual async Task<bool> LoadPluginFromNugetAsync(string pluginName)

protected virtual async Task<bool> LoadPluginFromNugetPackageAsync(string packagePath)
{
var assemblies = await LoadAssembliesFromNugetPackageAsync(packagePath);
var assemblies = await nugetInstaller.LoadAssembliesFromNugetPackageAsync(packagePath);
bool success = false;
foreach (var asm in assemblies)
{
@@ -362,58 +363,10 @@ protected override async Task<IEnumerable<Assembly>> LoadAssembliesAsync()
if (!File.Exists(nugetPackageFile))
continue;

assemblies.AddRange(await LoadAssembliesFromNugetPackageAsync(nugetPackageFile));
assemblies.AddRange(await nugetInstaller.LoadAssembliesFromNugetPackageAsync(nugetPackageFile));
}

return assemblies;
}

public virtual async Task<IEnumerable<Assembly>> LoadAssembliesFromNugetPackageAsync(string nugetPackageFile)
{
List<Assembly> allAssemblies = new List<Assembly>();
using (Stream fs = new FileStream(nugetPackageFile, FileMode.Open))
{
ZipFile zf = new ZipFile(fs);
List<ZipEntry> assemblies = new List<ZipEntry>();

#if NET461
foreach (ZipEntry entry in zf)
if (entry.Name.ToLower().StartsWith("lib/net461") && entry.Name.EndsWith(".dll"))
assemblies.Add(entry);

#if NETSTANDARD2_0
if (assemblies.Count < 1) // if no NET461 assemblies were found; continue to check.
#endif
#elif NETSTANDARD2_0 || NET461
foreach (ZipEntry entry in zf)
if (entry.Name.ToLower().StartsWith("lib/netstandard2.0") && entry.Name.EndsWith(".dll"))
assemblies.Add(entry);

#endif

if (assemblies.Count == 0)
{
logger.LogWarning("No assemblies found in: " + nugetPackageFile);
return allAssemblies;
}

foreach (var ze in assemblies)
{
Stream inputStream = zf.GetInputStream(ze);
MemoryStream ms = new MemoryStream();

await inputStream.CopyToAsync(ms);
var asm = Assembly.Load(ms.ToArray());

ms.Close();
inputStream.Close();
zf.Close();

allAssemblies.Add(asm);
}
}

return allAssemblies;
}
}
}
@@ -5,12 +5,16 @@
<Description>RocketMod .NET Game Server Plugin Framework Core implementations</Description>
<RootNamespace>Rocket.Core</RootNamespace>
<OutputType>Library</OutputType>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<Version>0.0.0.0</Version> <!-- Set by CI -->
<PackageVersion>0.0.0.0</PackageVersion> <!-- Set by CI -->
<AssemblyVersion>0.0.0.0</AssemblyVersion> <!-- Set by CI -->
<FileVersion>0.0.0.0</FileVersion> <!-- Set by CI -->
</PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<!-- Set by CI -->
<Version>0.0.0.0</Version>
<!-- Set by CI -->
<PackageVersion>0.0.0.0</PackageVersion>
<!-- Set by CI -->
<AssemblyVersion>0.0.0.0</AssemblyVersion>
<!-- Set by CI -->
<FileVersion>0.0.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NuGet.Commands" Version="4.9.3" />
@@ -24,20 +28,17 @@
<PackageReference Include="NuGet.Resolver" Version="4.9.3" />
<PackageReference Include="SharpFileSystem" Version="1.0.0.37" />
<PackageReference Include="Unity" Version="5.9.6" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'net461' ">
<!-- Stuff which isn't compatible with NET35 -->
<PackageReference Include="System.Numerics.Vectors">
<PackageReference Include="System.Numerics.Vectors">
<Version>4.5.0</Version>
</PackageReference>
<PackageReference Include="RestSharp" Version="106.6.9" />
<PackageReference Include="SharpZipLib" Version="1.1.0" />
<PackageReference Include="RestSharp" Version="106.6.9" />
<PackageReference Include="morelinq">
<Version>3.1.0</Version>
</PackageReference>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Rocket.API\Rocket.API.csproj" />
<ProjectReference Include="..\Rocket.NuGet\Rocket.NuGet.csproj" />
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\props\SharedNuGet.props" />
@@ -1,4 +1,4 @@
namespace Rocket.Core.Plugins.NuGet
namespace Rocket.NuGet
{
public class NuGetInstallResult
{

0 comments on commit 45d24f3

Please sign in to comment.
You can’t perform that action at this time.