From 2b8eb54a8b3151cf92c65684ccb01f65937a12fd Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Mon, 24 Jul 2023 14:16:41 +0200 Subject: [PATCH] Bugfix not using binary directory path for assets --- Capy64/Capy64.cs | 49 +++++++++++++++++++------------- Capy64/Core/Drawing.cs | 2 +- Capy64/Runtime/RuntimeManager.cs | 6 ++-- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Capy64/Capy64.cs b/Capy64/Capy64.cs index 4afc6e3..67b1f8e 100644 --- a/Capy64/Capy64.cs +++ b/Capy64/Capy64.cs @@ -29,6 +29,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; using static Capy64.Utils; @@ -56,21 +57,24 @@ public class Capy64 : Game public const int FreeTickrate = 60; } + public static readonly string AssemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + public static readonly string AssetsPath = Path.Combine(AssemblyPath, "Assets"); - public static string AppDataPath { - get { + public static string AppDataPath + { + get + { string baseDir = - RuntimeInformation.IsOSPlatform(OSPlatform.Windows) - ? - Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create) - : RuntimeInformation.IsOSPlatform(OSPlatform.Linux) - ? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, - Environment.SpecialFolderOption.Create) - : RuntimeInformation.IsOSPlatform(OSPlatform.OSX) - ? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, - Environment.SpecialFolderOption.Create) - : - "./"; + RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, + Environment.SpecialFolderOption.Create) : + RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, + Environment.SpecialFolderOption.Create) : + RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? + Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, + Environment.SpecialFolderOption.Create) : + "./"; return Path.Combine(baseDir, "Capy64"); } @@ -94,7 +98,8 @@ public class Capy64 : Game public Color BorderColor { get; set; } = Color.Black; - public Borders Borders = new() { + public Borders Borders = new() + { Top = 0, Bottom = 0, Left = 0, @@ -181,7 +186,8 @@ public class Capy64 : Game private void OnWindowSizeChange(object sender, EventArgs e) { - if (EngineMode == EngineMode.Classic) { + if (EngineMode == EngineMode.Classic) + { UpdateSize(true); return; } @@ -214,7 +220,8 @@ public class Capy64 : Game private void ResetBorder() { var size = (int)(Scale * DefaultParameters.BorderMultiplier); - Borders = new Borders { + Borders = new Borders + { Top = size, Bottom = size, Left = size, @@ -233,10 +240,10 @@ public class Capy64 : Game } if (!File.Exists(settingsPath)) { - File.Copy("Assets/default.json", settingsPath); + File.Copy(Path.Combine(AssetsPath, "default.json"), settingsPath); } - configBuilder.AddJsonFile("Assets/default.json", false); + configBuilder.AddJsonFile(Path.Combine(AssetsPath, "default.json"), false); configBuilder.AddJsonFile(settingsPath, false); Configuration = configBuilder.Build(); @@ -297,7 +304,8 @@ public class Capy64 : Game // Register user input _inputManager.Update(IsActive); - EventEmitter.RaiseTick(new() { + EventEmitter.RaiseTick(new() + { GameTime = gameTime, TotalTicks = _totalTicks, IsActiveTick = (int)_totalTicks % everyTick == 0, @@ -322,7 +330,8 @@ public class Capy64 : Game SpriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0); - EventEmitter.RaiseOverlay(new() { + EventEmitter.RaiseOverlay(new() + { GameTime = gameTime, TotalTicks = _totalTicks, }); diff --git a/Capy64/Core/Drawing.cs b/Capy64/Core/Drawing.cs index 436cb42..0f16450 100644 --- a/Capy64/Core/Drawing.cs +++ b/Capy64/Core/Drawing.cs @@ -54,7 +54,7 @@ public class Drawing : IDisposable public Drawing() { _fontSystem = new FontSystem(); - _fontSystem.AddFont(File.ReadAllBytes(@"Assets/font.ttf")); + _fontSystem.AddFont(File.ReadAllBytes(Path.Combine(Capy64.AssetsPath, "font.ttf"))); } public void Begin() diff --git a/Capy64/Runtime/RuntimeManager.cs b/Capy64/Runtime/RuntimeManager.cs index c3fe91c..dae9e34 100644 --- a/Capy64/Runtime/RuntimeManager.cs +++ b/Capy64/Runtime/RuntimeManager.cs @@ -124,7 +124,7 @@ internal class RuntimeManager : IComponent luaState.Thread.PushCFunction(L_Exit); luaState.Thread.SetGlobal("exit"); - var status = luaState.Thread.LoadFile("Assets/Lua/bios.lua"); + var status = luaState.Thread.LoadFile(Path.Combine(Capy64.AssetsPath, "Lua/bios.lua")); if (status != LuaStatus.OK) { throw new LuaException(luaState.Thread.ToString(-1)); @@ -164,7 +164,7 @@ internal class RuntimeManager : IComponent private void LoadFirmware() { - var firmwareContent = File.ReadAllText("Assets/Lua/firmware.lua"); + var firmwareContent = File.ReadAllText(Path.Combine(Capy64.AssetsPath, "Lua/firmware.lua")); var errored = luaState.Thread.DoString(firmwareContent); if(errored) { @@ -194,7 +194,7 @@ internal class RuntimeManager : IComponent var installedFilePath = Path.Combine(Capy64.AppDataPath, ".installed"); if (!File.Exists(installedFilePath) || force) { - FileSystemLib.CopyDirectory("Assets/Lua/CapyOS", FileSystemLib.DataPath, true, true); + FileSystemLib.CopyDirectory(Path.Combine(Capy64.AssetsPath, "Lua/CapyOS"), FileSystemLib.DataPath, true, true); File.Create(installedFilePath).Dispose(); } }