Skip to content

Commit

Permalink
Merge pull request #107 from ethanmoffat/linuxdev
Browse files Browse the repository at this point in the history
* Support development in Linux desktop environment.
* Use portable replacement library ImageSharp in place of System.Drawing.
* Make tests runnable on Linux
* Add EndlessClient.Linux.sln for building projects on Linux (BatchPub/PacketDecoder are windows-only and not intended to be portable)
  • Loading branch information
ethanmoffat committed Feb 11, 2022
2 parents 5844315 + 0eac61f commit 720aa58
Show file tree
Hide file tree
Showing 27 changed files with 328 additions and 152 deletions.
46 changes: 46 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "EndlessClient - Linux",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-client-linux",
"program": "${workspaceFolder}/bin/Debug/client/net6.0/EndlessClient",
"args": [],
"cwd": "${workspaceFolder}/bin/Debug/client/net6.0",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "EOBot - Linux",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-eobot-linux",
"program": "${workspaceFolder}/bin/Debug/utils/EOBot/net6.0/EOBot",
"args": [],
"cwd": "${workspaceFolder}/bin/Debug/utils/EOBot/net6.0",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Launch - Windows",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build-client-windows",
"program": "${workspaceFolder}/bin/Debug/client/net6.0-windows/EndlessClient.exe",
"args": [],
"cwd": "${workspaceFolder}/bin/Debug/client/net6.0-windows",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
41 changes: 41 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build-client-linux",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/EndlessClient/EndlessClient.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-eobot-linux",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/EOBot/EOBot.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "build-client-windows",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/EndlessClient/EndlessClient.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
]
}
2 changes: 1 addition & 1 deletion EOBot/EOBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net6.0</TargetFramework>
<OutputType>Exe</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>..\bin\$(Configuration)\Utils\EOBot\</OutputPath>
<OutputPath>..\bin\$(Configuration)\utils\EOBot\</OutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EOLib.Config\EOLib.Config.csproj" />
Expand Down
16 changes: 4 additions & 12 deletions EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace EOBot
static class Program
{
private static BotFramework f;
private static Win32.ConsoleCtrlDelegate consoleControlHandler;

[AutoMappedType]
class NpcWalkNotifier : INPCActionNotifier
Expand Down Expand Up @@ -167,12 +166,7 @@ static async Task<int> Main(string[] args)
"EOLib.Logger"
};

// this needs to be a delegate because it is getting garbage collected
consoleControlHandler = new Win32.ConsoleCtrlDelegate(HandleCtrl);
if (!Win32.SetConsoleCtrlHandler(consoleControlHandler, true))
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Warning, "Unable to set console control handler! CTRL+C will not terminate cleanly.", ConsoleColor.DarkYellow);
}
Console.CancelKeyPress += HandleCtrlC;

ArgumentsParser parsedArgs = new ArgumentsParser(args);

Expand Down Expand Up @@ -230,14 +224,12 @@ static async Task<int> Main(string[] args)
return 0;
}

static bool HandleCtrl(Win32.CtrlTypes type)
static void HandleCtrlC(object sender, ConsoleCancelEventArgs e)
{
var name = Enum.GetName(type.GetType(), type);
var name = Enum.GetName(e.SpecialKey.GetType(), e.SpecialKey);
ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, $"Exiting due to {name} event from system");

if (f != null)
f.TerminateBots();
return true;
f?.TerminateBots();
}

static void ShowError(ArgumentsParser args)
Expand Down
21 changes: 0 additions & 21 deletions EOBot/Win32.cs

This file was deleted.

9 changes: 3 additions & 6 deletions EOLib.Graphics.Test/EOLib.Graphics.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<TargetFramework>net6.0</TargetFramework>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<OutputPath>..\bin\$(Configuration)\test\</OutputPath>
<NoWarn>CA1416</NoWarn> <!-- TODO: Remove when using cross-platform graphics library instead of System.Drawing -->
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration.Contains(`Debug`))'">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,7 +16,8 @@
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.0.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
</ItemGroup>
</Project>
12 changes: 0 additions & 12 deletions EOLib.Graphics.Test/GraphicsDeviceTestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.ComponentModel.Design;
using System.Diagnostics.CodeAnalysis;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

Expand All @@ -10,25 +9,15 @@ namespace EOLib.Graphics.Test
[ExcludeFromCodeCoverage]
internal sealed class GraphicsDeviceTestHelper : IDisposable
{
private readonly Form _form;
private readonly Game _game;
//private readonly ServiceContainer _serviceContainer;

public GraphicsDeviceManager GraphicsDeviceManager { get; }

public GraphicsDeviceTestHelper()
{
_form = new Form();
_game = new Game();
GraphicsDeviceManager = new GraphicsDeviceManager(_game);
_game.RunOneFrame();

//var gds = GraphicsDeviceServiceTestHelper.AddRef(_form.Handle, _form.ClientSize.Width, _form.ClientSize.Height);

//_serviceContainer = new ServiceContainer();
//_serviceContainer.AddService(typeof(IGraphicsDeviceService), gds);

//GraphicsDeviceManager.ApplyChanges();
}

public void Dispose()
Expand All @@ -48,7 +37,6 @@ private void Dispose(bool disposing)
{
GraphicsDeviceManager.Dispose();
_game.Dispose();
_form.Dispose();
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions EOLib.Graphics.Test/NativeGraphicsLoaderTest.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using NUnit.Framework;
using Moq;
using PELoaderLib;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Formats.Bmp;

namespace EOLib.Graphics.Test
{
Expand Down Expand Up @@ -60,11 +61,11 @@ public void WhenLoadGFX_EmptyDataArray_ThrowsException()
private byte[] CreateDataArrayForBitmap()
{
byte[] array;
using (var retBmp = new Bitmap(10, 10))
using (var retBmp = new Image<Rgb24>(10, 10))
{
using (var ms = new MemoryStream())
{
retBmp.Save(ms, ImageFormat.Bmp);
retBmp.Save(ms, new BmpEncoder());
array = ms.ToArray();
}
}
Expand Down
21 changes: 11 additions & 10 deletions EOLib.Graphics.Test/NativeGraphicsManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using NUnit.Framework;
using Microsoft.Xna.Framework.Graphics;
using Moq;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Drawing.Processing;

namespace EOLib.Graphics.Test
{
Expand Down Expand Up @@ -161,7 +163,7 @@ public void WhenLoadTexture_MaleHat_Transparent_SetsSpecialColorToTransparent()
Texture2D resultTexture;
using (var bmp = LoadGFXReturnsBitmap(GFXTypes.MaleHat, requestedResource))
{
FillBitmapWithColor(bmp, Color.FromArgb(0xff, 0x08, 0x00, 0x00));
FillBitmapWithColor(bmp, Color.FromRgba(0x08, 0x00, 0x00, 0xff));
resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.MaleHat, requestedResource, true);
}

Expand All @@ -179,7 +181,7 @@ public void WhenLoadTexture_FemaleHat_Transparent_SetsSpecialColorToTransparent(
Texture2D resultTexture;
using (var bmp = LoadGFXReturnsBitmap(GFXTypes.FemaleHat, requestedResource))
{
FillBitmapWithColor(bmp, Color.FromArgb(0xff, 0x08, 0x00, 0x00));
FillBitmapWithColor(bmp, Color.FromRgba(0x08, 0x00, 0x00, 0xff));
resultTexture = _nativeGraphicsManager.TextureFromResource(GFXTypes.FemaleHat, requestedResource, true);
}

Expand All @@ -201,9 +203,9 @@ public void WhenLoadTexture_RaceCondition_DisposesExistingCachedTextureAndReturn
Assert.IsFalse(resultTexture.IsDisposed);
}

private Bitmap LoadGFXReturnsBitmap(GFXTypes whichFile, int requestedResource, Action loadCallback = null)
private IImage LoadGFXReturnsBitmap(GFXTypes whichFile, int requestedResource, Action loadCallback = null)
{
var bitmapToReturn = new Bitmap(10, 10, PixelFormat.Format24bppRgb);
var bitmapToReturn = new Image<Rgba32>(10, 10);

var graphicsLoaderMock = Mock.Get(_graphicsLoader);
graphicsLoaderMock.Setup(x => x.LoadGFX(whichFile, requestedResource))
Expand All @@ -213,11 +215,10 @@ private Bitmap LoadGFXReturnsBitmap(GFXTypes whichFile, int requestedResource, A
return bitmapToReturn;
}

private static void FillBitmapWithColor(Bitmap bitmap, Color color)
private static void FillBitmapWithColor(IImage image, Color color)
{
for(int row = 0; row < bitmap.Height; ++row)
for (int col = 0; col < bitmap.Width; ++col)
bitmap.SetPixel(col, row, color);
var colorBrush = new SolidBrush(color);
((Image)image).Mutate(x => x.Clear(colorBrush));
}

private void GetTextureAgain(GFXTypes whichFile, int requestedResource)
Expand Down
2 changes: 1 addition & 1 deletion EOLib.Graphics.Test/PEFileCollectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace EOLib.Graphics.Test
[TestFixture, ExcludeFromCodeCoverage]
public class PEFileCollectionTest
{
private const string ExpectedDirectory = "GFX";
private const string ExpectedDirectory = "gfx";

private IPEFileCollection _collection;

Expand Down
4 changes: 2 additions & 2 deletions EOLib.Graphics/EOLib.Graphics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<OutputPath>..\bin\$(Configuration)\lib\</OutputPath>
Expand All @@ -16,6 +15,7 @@
<PackageReference Include="AutomaticTypeMapper" Version="1.4.1" />
<PackageReference Include="PELoaderLib" Version="1.1.1" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="2.0.0" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions EOLib.Graphics/INativeGraphicsLoader.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Drawing;
using SixLabors.ImageSharp;

namespace EOLib.Graphics
{
public interface INativeGraphicsLoader
{
Bitmap LoadGFX(GFXTypes file, int resourceValue);
IImage LoadGFX(GFXTypes file, int resourceValue);
}
}
16 changes: 4 additions & 12 deletions EOLib.Graphics/NativeGraphicsLoader.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using AutomaticTypeMapper;
using AutomaticTypeMapper;
using SixLabors.ImageSharp;

namespace EOLib.Graphics
{
Expand All @@ -16,19 +13,14 @@ public NativeGraphicsLoader(IPEFileCollection modules)
_modules = modules;
}

public Bitmap LoadGFX(GFXTypes file, int resourceValue)
public IImage LoadGFX(GFXTypes file, int resourceValue)
{
var fileBytes = _modules[file].GetEmbeddedBitmapResourceByID(resourceValue + 100);

if (fileBytes.Length == 0)
throw new GFXLoadException(resourceValue, file);

var ms = new MemoryStream(fileBytes);

if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new NotImplementedException("TODO: use cross-platform image library instead of System.Drawing");

return (Bitmap)Image.FromStream(ms);
return Image.Load(fileBytes);
}
}
}

0 comments on commit 720aa58

Please sign in to comment.