Skip to content

Commit

Permalink
Merge branch 'netstandard' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Oct 3, 2021
2 parents 45b0b3c + e0ffdd7 commit 431c29b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 65 deletions.
10 changes: 5 additions & 5 deletions CmlLib/CmlLib.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net462;net5.0</TargetFrameworks>
<TargetFrameworks>net462;netstandard2.0</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Version>3.3.2</Version>
Expand Down Expand Up @@ -31,16 +31,16 @@ Support all version, forge, optifine
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Fody" Version="6.5.1">
<PackageReference Include="Fody" Version="6.5.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="LZMA-SDK" Version="19.0.0" />
<PackageReference Include="MethodTimer.Fody" Version="3.2.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SharpZipLib" Version="1.2.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<None Include="../icon.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>

Expand Down
22 changes: 9 additions & 13 deletions CmlLib/Core/Launcher/MNative.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CmlLib.Core.Version;
using System;
using System.Diagnostics;
using CmlLib.Core.Version;
using CmlLib.Utils;
using System.IO;

Expand All @@ -25,22 +27,16 @@ public string ExtractNatives()

foreach (var item in version.Libraries)
{
try
// do not ignore exception
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
{
if (item.IsRequire && item.IsNative && !string.IsNullOrEmpty(item.Path))
string zPath = Path.Combine(gamePath.Library, item.Path);
if (File.Exists(zPath))
{
string zPath = Path.Combine(gamePath.Library, item.Path);
if (File.Exists(zPath))
{
var z = new SharpZip(zPath);
z.Unzip(path);
}
var z = new SharpZip(zPath);
z.Unzip(path);
}
}
catch
{
// ignore invalid native library file
}
}

return path;
Expand Down
18 changes: 9 additions & 9 deletions CmlLib/Core/MRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,8 @@ static MRule()

private static string getOSName()
{
// Environment.OSVersion.Platform does not work in NET Core
#if NETCOREAPP
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSX;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else
return Linux;
#elif NETFRAMEWORK
// RuntimeInformation does not work in .NET Framework
#if NETFRAMEWORK
var osType = Environment.OSVersion.Platform;

if (osType == PlatformID.MacOSX)
Expand All @@ -42,6 +35,13 @@ private static string getOSName()
return Linux;
else
return Windows;
#else
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSX;
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return Windows;
else
return Linux;
#endif
}

Expand Down
36 changes: 9 additions & 27 deletions CmlLib/Utils/IOUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,52 +175,34 @@ public static StreamWriter AsyncStreamWriter(string path, Encoding encoding, boo
return new StreamWriter(stream, encoding);
}

// In .NET Framework 4.6.2, There is no File.ReadFileTextAsync. so I copied it from .NET Core source code
// In .NET Standard 2.0, There is no File.ReadFileTextAsync. so I copied it from .NET Core source code
public static async Task<string> ReadFileAsync(string path)
{
using var reader = AsyncStreamReader(path, Encoding.UTF8);
var content = await reader.ReadToEndAsync()
.ConfigureAwait(false); // **MUST be awaited in this scope**
await disposeStreamAsync(reader.BaseStream).ConfigureAwait(false);
await reader.BaseStream.FlushAsync().ConfigureAwait(false);
return content;
}
// In .NET Framework 4.6.2, There is no File.WriteFileTextAsync. so I copied it from .NET Core source code

// In .NET Standard 2.0, There is no File.WriteFileTextAsync. so I copied it from .NET Core source code
public static async Task WriteFileAsync(string path, string content)
{
// UTF8 with BOM might not be recognized by minecraft. not tested
var encoder = new UTF8Encoding(false);
var writer = AsyncStreamWriter(path, encoder, false);
using var writer = AsyncStreamWriter(path, encoder, false);
await writer.WriteAsync(content).ConfigureAwait(false); // **MUST be awaited in this scope**

#if NETFRAMEWORK
writer.Dispose();
#elif NETCOREAPP
await writer.DisposeAsync().ConfigureAwait(false);
#endif
await writer.FlushAsync().ConfigureAwait(false);
}

public static async Task CopyFileAsync(string sourceFile, string destinationFile)
{
var sourceStream = AsyncReadStream(sourceFile);
var destinationStream = AsyncWriteStream(destinationFile, false);
using var sourceStream = AsyncReadStream(sourceFile);
using var destinationStream = AsyncWriteStream(destinationFile, false);

await sourceStream.CopyToAsync(destinationStream).ConfigureAwait(false);

await destinationStream.FlushAsync().ConfigureAwait(false);

await disposeStreamAsync(sourceStream).ConfigureAwait(false);
await disposeStreamAsync(destinationStream).ConfigureAwait(false);
}

private static Task disposeStreamAsync(Stream stream)
{
// .NET Framework does not support DisposeAsync
#if NETFRAMEWORK
stream.Dispose();
return Task.CompletedTask;
#elif NETCOREAPP
return stream.DisposeAsync().AsTask();
#endif
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion CmlLibCoreSample/CmlLibCoreSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion CmlLibWinFormSample/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
6 changes: 5 additions & 1 deletion CmlLibWinFormSample/CmlLibWinFormSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<OutputType>WinExe</OutputType>
<RootNamespace>CmlLibWinFormSample</RootNamespace>
<AssemblyName>CmlLibWinFormSample</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -32,6 +33,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
13 changes: 5 additions & 8 deletions release.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
if ($args.Length -gt 0)
{
$ver=$args[0]
if ($args.Length -gt 0) {
$ver = $args[0]
}
else
{
else {
$ver = Read-Host 'version'
}

Expand All @@ -14,9 +12,8 @@ mkdir .\release\SampleCoreLauncher$ver
mkdir .\release\SampleWinformLauncher$ver

Copy-Item .\CmlLib\bin\Release\* -Destination .\release\CmlLib.Core.$ver -Recurse
Get-ChildItem .\release\CmlLib.Core.$ver -Recurse -File | Where {($_.Extension -ne ".dll")} | Remove-Item
Copy-Item -Path .\CmlLibCoreSample\bin\Release\netcoreapp3.1\* -Destination .\release\SampleCoreLauncher$ver
Copy-Item .\CmlLibWinFormSample\bin\Release\* -Destination .\release\SampleWinformLauncher$ver -Include *.exe,*.dll,*.pdb,*.config
Get-ChildItem .\release\CmlLib.Core.$ver -Recurse -File | Where-Object { ($_.Extension -ne ".dll") } | Remove-Item
Copy-Item .\CmlLibWinFormSample\bin\Release\* -Destination .\release\SampleWinformLauncher$ver -Include *.exe, *.dll, *.pdb, *.config

Compress-Archive -Path .\release\CmlLib.Core.$ver -DestinationPath .\release\CmlLib.Core.$ver.zip
Compress-Archive -Path .\release\SampleCoreLauncher$ver -DestinationPath .\release\SampleCoreLauncher$ver.zip
Expand Down

0 comments on commit 431c29b

Please sign in to comment.