From ee2c51e648cd613394624eab4b83541ad8d0facc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 24 Apr 2020 00:24:39 +0200 Subject: [PATCH] added the option to create an app bundle for mac --- GameBundle/GameBundle.csproj | 2 +- GameBundle/Options.cs | 5 +++++ GameBundle/Program.cs | 33 ++++++++++++++++++++++++++++++++- README.md | 2 +- Test/Bundle.bat | 2 +- Test/GameImpl.cs | 16 ++++++++++++++++ 6 files changed, 56 insertions(+), 4 deletions(-) diff --git a/GameBundle/GameBundle.csproj b/GameBundle/GameBundle.csproj index 24e19c0..5271f12 100644 --- a/GameBundle/GameBundle.csproj +++ b/GameBundle/GameBundle.csproj @@ -13,7 +13,7 @@ https://raw.githubusercontent.com/Ellpeck/GameBundle/master/Logo.png true gamebundle - 1.1.0 + 1.1.1 diff --git a/GameBundle/Options.cs b/GameBundle/Options.cs index 0c4b30c..abed456 100644 --- a/GameBundle/Options.cs +++ b/GameBundle/Options.cs @@ -19,6 +19,11 @@ namespace GameBundle { [Option('m', "mac", HelpText = "Bundle for mac")] public bool BuildMac { get; set; } + [Option('b', "mac-bundle", HelpText = "Create an app bundle for mac")] + public bool MacBundle { get; set; } + [Option("mac-bundle-resources", Default = new[] {"Content", "*.icns"}, HelpText = "When creating an app bundle for mac, things that should go into the Resources folder rather than the MacOS folder")] + public IEnumerable MacBundleResources { get; set; } + [Option('e', "exclude", HelpText = "Files that should not be moved to the library folder")] public IEnumerable ExcludedFiles { get; set; } [Option("32-bit", HelpText = "Publish for 32 bit instead of 64 bit. Note that this is only possible on Windows")] diff --git a/GameBundle/Program.cs b/GameBundle/Program.cs index 81c7057..9983376 100644 --- a/GameBundle/Program.cs +++ b/GameBundle/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; @@ -37,7 +38,10 @@ namespace GameBundle { } if (options.BuildMac) { Console.WriteLine("Bundling for mac"); - Publish(options, proj, $"{bundleDir}/mac", "osx-x64"); + var dir = $"{bundleDir}/mac"; + Publish(options, proj, dir, "osx-x64"); + if (options.MacBundle) + CreateMacBundle(options, new DirectoryInfo(dir), proj.FullName); } Console.WriteLine("Done"); @@ -82,5 +86,32 @@ namespace GameBundle { return null; } + private static void CreateMacBundle(Options options, DirectoryInfo dir, string proj) { + var app = dir.CreateSubdirectory($"{Path.GetFileNameWithoutExtension(proj)}.app"); + var contents = app.CreateSubdirectory("Contents"); + var resources = contents.CreateSubdirectory("Resources"); + var macOs = contents.CreateSubdirectory("MacOS"); + var resRegex = GlobRegex(options.MacBundleResources); + foreach (var file in dir.GetFiles()) { + var destDir = resRegex.IsMatch(file.Name) ? resources : macOs; + if (file.Name.EndsWith("plist")) + destDir = app; + file.MoveTo(Path.Combine(destDir.FullName, file.Name), true); + } + foreach (var sub in dir.GetDirectories()) { + if (sub.Name == app.Name) + continue; + var destDir = resRegex.IsMatch(sub.Name) ? resources : macOs; + var dest = new DirectoryInfo(Path.Combine(destDir.FullName, sub.Name)); + if (dest.Exists) + dest.Delete(true); + sub.MoveTo(dest.FullName); + } + } + + private static Regex GlobRegex(IEnumerable strings) { + return new Regex('(' + string.Join("|", strings.Select(s => s.Replace(".", "[.]").Replace("*", ".*").Replace("?", "."))) + ')'); + } + } } \ No newline at end of file diff --git a/README.md b/README.md index ccd839a..da53f4b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,6 @@ gamebundle --help # Future features In the future, GameBundle plans to include the following features: -- An option to create a `.app` folder for Mac +- ~~An option to create a `.app` folder for Mac~~ Implemented in `1.1.1` - An option to create an installer for Windows - An option to create a flatpak (or similar) installer for Linux \ No newline at end of file diff --git a/Test/Bundle.bat b/Test/Bundle.bat index c5a0c02..9f7072c 100644 --- a/Test/Bundle.bat +++ b/Test/Bundle.bat @@ -1 +1 @@ -"../GameBundle/bin/Debug/netcoreapp3.1/GameBundle.exe" -wlm -s Test.csproj -o bin/Bundled -v \ No newline at end of file +"../GameBundle/bin/Debug/netcoreapp3.1/GameBundle.exe" -wlm -s Test.csproj -o bin/Bundled -v -b \ No newline at end of file diff --git a/Test/GameImpl.cs b/Test/GameImpl.cs index 0e9f419..09f102d 100644 --- a/Test/GameImpl.cs +++ b/Test/GameImpl.cs @@ -1,13 +1,29 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using MLEM.Startup; namespace Test { public class GameImpl : MlemGame { public static GameImpl Instance { get; private set; } + private Texture2D texture; public GameImpl() { Instance = this; } + protected override void LoadContent() { + base.LoadContent(); + this.texture = LoadContent("Textures/Test"); + } + + protected override void DoDraw(GameTime gameTime) { + this.GraphicsDevice.Clear(Color.Black); + base.DoDraw(gameTime); + this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: Matrix.CreateScale(10)); + this.SpriteBatch.Draw(this.texture, Vector2.Zero, Color.White); + this.SpriteBatch.End(); + } + } } \ No newline at end of file