Bugfix not using binary directory path for assets

This commit is contained in:
Alessandro Proto 2023-07-24 14:16:41 +02:00
parent b85adb960b
commit 2b8eb54a8b
3 changed files with 33 additions and 24 deletions

View file

@ -29,6 +29,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using static Capy64.Utils; using static Capy64.Utils;
@ -56,20 +57,23 @@ public class Capy64 : Game
public const int FreeTickrate = 60; 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 { public static string AppDataPath
get { {
get
{
string baseDir = string baseDir =
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
? Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData,
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create) Environment.SpecialFolderOption.Create) :
: RuntimeInformation.IsOSPlatform(OSPlatform.Linux) RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ?
? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData,
Environment.SpecialFolderOption.Create) Environment.SpecialFolderOption.Create) :
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
? Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData, Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData,
Environment.SpecialFolderOption.Create) Environment.SpecialFolderOption.Create) :
:
"./"; "./";
return Path.Combine(baseDir, "Capy64"); return Path.Combine(baseDir, "Capy64");
@ -94,7 +98,8 @@ public class Capy64 : Game
public Color BorderColor { get; set; } = Color.Black; public Color BorderColor { get; set; } = Color.Black;
public Borders Borders = new() { public Borders Borders = new()
{
Top = 0, Top = 0,
Bottom = 0, Bottom = 0,
Left = 0, Left = 0,
@ -181,7 +186,8 @@ public class Capy64 : Game
private void OnWindowSizeChange(object sender, EventArgs e) private void OnWindowSizeChange(object sender, EventArgs e)
{ {
if (EngineMode == EngineMode.Classic) { if (EngineMode == EngineMode.Classic)
{
UpdateSize(true); UpdateSize(true);
return; return;
} }
@ -214,7 +220,8 @@ public class Capy64 : Game
private void ResetBorder() private void ResetBorder()
{ {
var size = (int)(Scale * DefaultParameters.BorderMultiplier); var size = (int)(Scale * DefaultParameters.BorderMultiplier);
Borders = new Borders { Borders = new Borders
{
Top = size, Top = size,
Bottom = size, Bottom = size,
Left = size, Left = size,
@ -233,10 +240,10 @@ public class Capy64 : Game
} }
if (!File.Exists(settingsPath)) 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); configBuilder.AddJsonFile(settingsPath, false);
Configuration = configBuilder.Build(); Configuration = configBuilder.Build();
@ -297,7 +304,8 @@ public class Capy64 : Game
// Register user input // Register user input
_inputManager.Update(IsActive); _inputManager.Update(IsActive);
EventEmitter.RaiseTick(new() { EventEmitter.RaiseTick(new()
{
GameTime = gameTime, GameTime = gameTime,
TotalTicks = _totalTicks, TotalTicks = _totalTicks,
IsActiveTick = (int)_totalTicks % everyTick == 0, 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, SpriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale,
SpriteEffects.None, 0); SpriteEffects.None, 0);
EventEmitter.RaiseOverlay(new() { EventEmitter.RaiseOverlay(new()
{
GameTime = gameTime, GameTime = gameTime,
TotalTicks = _totalTicks, TotalTicks = _totalTicks,
}); });

View file

@ -54,7 +54,7 @@ public class Drawing : IDisposable
public Drawing() public Drawing()
{ {
_fontSystem = new FontSystem(); _fontSystem = new FontSystem();
_fontSystem.AddFont(File.ReadAllBytes(@"Assets/font.ttf")); _fontSystem.AddFont(File.ReadAllBytes(Path.Combine(Capy64.AssetsPath, "font.ttf")));
} }
public void Begin() public void Begin()

View file

@ -124,7 +124,7 @@ internal class RuntimeManager : IComponent
luaState.Thread.PushCFunction(L_Exit); luaState.Thread.PushCFunction(L_Exit);
luaState.Thread.SetGlobal("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) if (status != LuaStatus.OK)
{ {
throw new LuaException(luaState.Thread.ToString(-1)); throw new LuaException(luaState.Thread.ToString(-1));
@ -164,7 +164,7 @@ internal class RuntimeManager : IComponent
private void LoadFirmware() 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); var errored = luaState.Thread.DoString(firmwareContent);
if(errored) if(errored)
{ {
@ -194,7 +194,7 @@ internal class RuntimeManager : IComponent
var installedFilePath = Path.Combine(Capy64.AppDataPath, ".installed"); var installedFilePath = Path.Combine(Capy64.AppDataPath, ".installed");
if (!File.Exists(installedFilePath) || force) 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(); File.Create(installedFilePath).Dispose();
} }
} }