diff --git a/Capy64/API/IComponent.cs b/Capy64/API/IComponent.cs index ab21111..2e3bd80 100644 --- a/Capy64/API/IComponent.cs +++ b/Capy64/API/IComponent.cs @@ -14,13 +14,10 @@ // limitations under the License. using KeraLua; -using Microsoft.Extensions.DependencyInjection; namespace Capy64.API; public interface IComponent { - void ConfigureServices(IServiceCollection services) { } void LuaInit(Lua L) { } - } diff --git a/Capy64/Capy64.cs b/Capy64/Capy64.cs index cebcbe9..4afc6e3 100644 --- a/Capy64/Capy64.cs +++ b/Capy64/Capy64.cs @@ -21,7 +21,6 @@ using Capy64.Integrations; using Capy64.PluginManager; using Capy64.Runtime; using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -41,7 +40,7 @@ public enum EngineMode Free } -public class Capy64 : Game, IGame +public class Capy64 : Game { public const string Version = "1.1.0-beta"; @@ -91,6 +90,7 @@ public class Capy64 : Game, IGame public Eventing.EventEmitter EventEmitter { get; private set; } public DiscordIntegration Discord { get; set; } public int TickRate => tickrate; + public IConfiguration Configuration { get; private set; } public Color BorderColor { get; set; } = Color.Black; @@ -224,11 +224,26 @@ public class Capy64 : Game, IGame protected override void Initialize() { - var configuration = _serviceProvider.GetService(); + var configBuilder = new ConfigurationBuilder(); + + var settingsPath = Path.Combine(AppDataPath, "settings.json"); + if (!Directory.Exists(AppDataPath)) + { + Directory.CreateDirectory(AppDataPath); + } + if (!File.Exists(settingsPath)) + { + File.Copy("Assets/default.json", settingsPath); + } + + configBuilder.AddJsonFile("Assets/default.json", false); + configBuilder.AddJsonFile(settingsPath, false); + + Configuration = configBuilder.Build(); Window.Title = "Capy64 " + Version; - Scale = configuration.GetValue("Window:Scale", DefaultParameters.Scale); + Scale = Configuration.GetValue("Window:Scale", DefaultParameters.Scale); ResetBorder(); UpdateSize(); @@ -238,7 +253,7 @@ public class Capy64 : Game, IGame InactiveSleepTime = new TimeSpan(0); - SetEngineMode(configuration.GetValue("EngineMode", DefaultParameters.EngineMode)); + SetEngineMode(Configuration.GetValue("EngineMode", DefaultParameters.EngineMode)); Audio = new Audio(); @@ -261,7 +276,8 @@ public class Capy64 : Game, IGame foreach (var type in types) { - var instance = (IComponent)ActivatorUtilities.CreateInstance(_serviceProvider, type)!; + var instance = (IComponent)Activator.CreateInstance(type, this); + //var instance = (IComponent)ActivatorUtilities.CreateInstance(_serviceProvider, type)!; plugins.Add(instance); } diff --git a/Capy64/Capy64.csproj b/Capy64/Capy64.csproj index c74b6a1..edd60b6 100644 --- a/Capy64/Capy64.csproj +++ b/Capy64/Capy64.csproj @@ -40,11 +40,13 @@ - + + + + - diff --git a/Capy64/IGame.cs b/Capy64/IGame.cs deleted file mode 100644 index c7c878b..0000000 --- a/Capy64/IGame.cs +++ /dev/null @@ -1,50 +0,0 @@ -// This file is part of Capy64 - https://github.com/Ale32bit/Capy64 -// Copyright 2023 Alessandro "AlexDevs" Proto -// -// Licensed under the Apache License, Version 2.0 (the "License"). -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Capy64.API; -using Capy64.Core; -using Capy64.Integrations; -using Capy64.Runtime; -using Microsoft.Xna.Framework; -using System; -using System.Collections.Generic; - -namespace Capy64; - -public interface IGame -{ - Capy64 Game { get; } - EngineMode EngineMode { get; } - IList NativePlugins { get; } - IList Plugins { get; } - GameWindow Window { get; } - Drawing Drawing { get; } - Audio Audio { get; } - LuaState LuaRuntime { get; set; } - Eventing.EventEmitter EventEmitter { get; } - void ConfigureServices(IServiceProvider serviceProvider); - - int Width { get; set; } - int Height { get; set; } - float Scale { get; set; } - void UpdateSize(bool resize = true); - - event EventHandler Exiting; - void Run(); - void Exit(); - - // Integrations - DiscordIntegration Discord { get; } -} diff --git a/Capy64/Integrations/DiscordIntegration.cs b/Capy64/Integrations/DiscordIntegration.cs index 16db4bc..0377653 100644 --- a/Capy64/Integrations/DiscordIntegration.cs +++ b/Capy64/Integrations/DiscordIntegration.cs @@ -28,9 +28,9 @@ public class DiscordIntegration : IComponent public readonly bool Enabled; private readonly IConfiguration _configuration; - public DiscordIntegration(IConfiguration configuration) + public DiscordIntegration(Capy64 game) { - _configuration = configuration; + _configuration = game.Configuration; var discordConfig = _configuration.GetSection("Integrations:Discord"); Enabled = discordConfig.GetValue("Enable", false); diff --git a/Capy64/PluginManager/PluginLoader.cs b/Capy64/PluginManager/PluginLoader.cs index dc2268d..5bdcd2c 100644 --- a/Capy64/PluginManager/PluginLoader.cs +++ b/Capy64/PluginManager/PluginLoader.cs @@ -14,7 +14,6 @@ // limitations under the License. using Capy64.API; -using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.IO; @@ -47,7 +46,7 @@ internal class PluginLoader { if (typeof(IComponent).IsAssignableFrom(type)) { - IComponent result = ActivatorUtilities.CreateInstance(provider, type) as IComponent; + IComponent result = Activator.CreateInstance(type, Capy64.Instance) as IComponent; plugins.Add(result); } } diff --git a/Capy64/Program.cs b/Capy64/Program.cs index 6b78a4f..1278760 100644 --- a/Capy64/Program.cs +++ b/Capy64/Program.cs @@ -13,34 +13,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -using Capy64; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using System.IO; - using var game = new Capy64.Capy64(); -using IHost host = Host.CreateDefaultBuilder(args) - .ConfigureAppConfiguration((context, c) => - { - var settingsPath = Path.Combine(Capy64.Capy64.AppDataPath, "settings.json"); - if (!Directory.Exists(Capy64.Capy64.AppDataPath)) - { - Directory.CreateDirectory(Capy64.Capy64.AppDataPath); - } - if (!File.Exists(settingsPath)) - { - File.Copy("Assets/default.json", settingsPath); - } - c.AddJsonFile("Assets/default.json", false); - c.AddJsonFile(settingsPath, false); - }) - .ConfigureServices((hostContext, services) => - { - services.AddSingleton(game); - services.AddHostedService(); - }) - .Build(); - -await host.RunAsync(); \ No newline at end of file +game.Run(); diff --git a/Capy64/Runtime/Libraries/AudioLib.cs b/Capy64/Runtime/Libraries/AudioLib.cs index 89d5406..f68c2d6 100644 --- a/Capy64/Runtime/Libraries/AudioLib.cs +++ b/Capy64/Runtime/Libraries/AudioLib.cs @@ -25,8 +25,8 @@ public class AudioLib : IComponent { private const int queueLimit = 8; - private static IGame _game; - public AudioLib(IGame game) + private static Capy64 _game; + public AudioLib(Capy64 game) { _game = game; _game.EventEmitter.OnClose += OnClose; diff --git a/Capy64/Runtime/Libraries/EventLib.cs b/Capy64/Runtime/Libraries/EventLib.cs index ea05245..bec39f4 100644 --- a/Capy64/Runtime/Libraries/EventLib.cs +++ b/Capy64/Runtime/Libraries/EventLib.cs @@ -29,8 +29,8 @@ public class EventLib : IComponent private static bool FrozenTaskAwaiter = false; - private static IGame _game; - public EventLib(IGame game) + private static Capy64 _game; + public EventLib(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Libraries/FileSystemLib.cs b/Capy64/Runtime/Libraries/FileSystemLib.cs index af93c48..398248c 100644 --- a/Capy64/Runtime/Libraries/FileSystemLib.cs +++ b/Capy64/Runtime/Libraries/FileSystemLib.cs @@ -111,6 +111,8 @@ public class FileSystemLib : IComponent new(), // NULL }; + public FileSystemLib(Capy64 _) { } + public void LuaInit(Lua state) { // Add "fs" library to lua, not global (uses require()) diff --git a/Capy64/Runtime/Libraries/GPULib.cs b/Capy64/Runtime/Libraries/GPULib.cs index 7b7e6f7..c2a1297 100644 --- a/Capy64/Runtime/Libraries/GPULib.cs +++ b/Capy64/Runtime/Libraries/GPULib.cs @@ -28,8 +28,8 @@ namespace Capy64.Runtime.Libraries; public class GPULib : IComponent { - private static IGame _game; - public GPULib(IGame game) + private static Capy64 _game; + public GPULib(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Libraries/HTTPLib.cs b/Capy64/Runtime/Libraries/HTTPLib.cs index 6179a7f..aef6325 100644 --- a/Capy64/Runtime/Libraries/HTTPLib.cs +++ b/Capy64/Runtime/Libraries/HTTPLib.cs @@ -30,7 +30,7 @@ namespace Capy64.Runtime.Libraries; #nullable enable public class HTTPLib : IComponent { - private static IGame _game = null!; + private static Capy64 _game = null!; private static HttpClient _httpClient = null!; private static long _requestId; public static readonly HashSet WebSocketConnections = new(); @@ -57,13 +57,13 @@ public class HTTPLib : IComponent }, new(), }; - public HTTPLib(IGame game, IConfiguration configuration) + public HTTPLib(Capy64 game) { _game = game; _requestId = 0; _httpClient = new(); _httpClient.DefaultRequestHeaders.Add("User-Agent", UserAgent); - _configuration = configuration; + _configuration = game.Configuration; } public void LuaInit(Lua L) diff --git a/Capy64/Runtime/Libraries/MachineLib.cs b/Capy64/Runtime/Libraries/MachineLib.cs index 3eb55d5..185e29a 100644 --- a/Capy64/Runtime/Libraries/MachineLib.cs +++ b/Capy64/Runtime/Libraries/MachineLib.cs @@ -24,8 +24,8 @@ namespace Capy64.Runtime.Libraries; public class MachineLib : IComponent { - private static IGame _game; - public MachineLib(IGame game) + private static Capy64 _game; + public MachineLib(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Libraries/TermLib.cs b/Capy64/Runtime/Libraries/TermLib.cs index f753bc4..6336b0e 100644 --- a/Capy64/Runtime/Libraries/TermLib.cs +++ b/Capy64/Runtime/Libraries/TermLib.cs @@ -49,11 +49,11 @@ internal class TermLib : IComponent public static Color BackgroundColor { get; set; } private static Char?[] CharGrid; - private static IGame _game; + private static Capy64 _game; private static bool cursorState = false; private static bool enableCursor = true; private static Texture2D cursorTexture; - public TermLib(IGame game) + public TermLib(Capy64 game) { _game = game; diff --git a/Capy64/Runtime/Libraries/TimerLib.cs b/Capy64/Runtime/Libraries/TimerLib.cs index e15b2f9..1b4f989 100644 --- a/Capy64/Runtime/Libraries/TimerLib.cs +++ b/Capy64/Runtime/Libraries/TimerLib.cs @@ -50,11 +50,11 @@ class TimerLib : IComponent new(), }; - private static IGame _game; + private static Capy64 _game; private static uint _timerId = 0; private static readonly ConcurrentDictionary timers = new(); - public TimerLib(IGame game) + public TimerLib(Capy64 game) { _game = game; diff --git a/Capy64/Runtime/ObjectManager.cs b/Capy64/Runtime/ObjectManager.cs index 5f4e011..5b524d9 100644 --- a/Capy64/Runtime/ObjectManager.cs +++ b/Capy64/Runtime/ObjectManager.cs @@ -25,8 +25,8 @@ public class ObjectManager : IComponent { private static readonly ConcurrentDictionary _objects = new(); - private static IGame _game; - public ObjectManager(IGame game) + private static Capy64 _game; + public ObjectManager(Capy64 game) { _game = game; _game.EventEmitter.OnClose += OnClose; diff --git a/Capy64/Runtime/Objects/FileHandle.cs b/Capy64/Runtime/Objects/FileHandle.cs index 615ba66..82b7c50 100644 --- a/Capy64/Runtime/Objects/FileHandle.cs +++ b/Capy64/Runtime/Objects/FileHandle.cs @@ -88,6 +88,8 @@ public class FileHandle : IComponent new(), }; + public FileHandle(Capy64 _) { } + public void LuaInit(Lua L) { CreateMeta(L); diff --git a/Capy64/Runtime/Objects/GPUBufferMeta.cs b/Capy64/Runtime/Objects/GPUBufferMeta.cs index a8aa7f0..eb983d9 100644 --- a/Capy64/Runtime/Objects/GPUBufferMeta.cs +++ b/Capy64/Runtime/Objects/GPUBufferMeta.cs @@ -66,8 +66,8 @@ public class GPUBufferMeta : IComponent new(), }; - private static IGame _game; - public GPUBufferMeta(IGame game) + private static Capy64 _game; + public GPUBufferMeta(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Objects/Socket.cs b/Capy64/Runtime/Objects/Socket.cs index 63745e1..0a7eab6 100644 --- a/Capy64/Runtime/Objects/Socket.cs +++ b/Capy64/Runtime/Objects/Socket.cs @@ -29,8 +29,8 @@ public class Socket : IDisposable public class SocketLib : IComponent { - private static IGame _game = null!; - public SocketLib(IGame game) + private static Capy64 _game = null!; + public SocketLib(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Objects/Task.cs b/Capy64/Runtime/Objects/Task.cs index fd8e9ed..da7343e 100644 --- a/Capy64/Runtime/Objects/Task.cs +++ b/Capy64/Runtime/Objects/Task.cs @@ -21,8 +21,8 @@ namespace Capy64.Runtime.Objects; public class TaskMeta : IComponent { - private static IGame _game; - public TaskMeta(IGame game) + private static Capy64 _game; + public TaskMeta(Capy64 game) { _game = game; } diff --git a/Capy64/Runtime/Objects/WebSocketClient.cs b/Capy64/Runtime/Objects/WebSocketClient.cs index 55379c0..5152652 100644 --- a/Capy64/Runtime/Objects/WebSocketClient.cs +++ b/Capy64/Runtime/Objects/WebSocketClient.cs @@ -73,6 +73,8 @@ public class WebSocketClient : IComponent new(), }; + public WebSocketClient(Capy64 _) { } + public void LuaInit(Lua L) { CreateMeta(L); diff --git a/Capy64/Runtime/RuntimeManager.cs b/Capy64/Runtime/RuntimeManager.cs index a8327e9..c3fe91c 100644 --- a/Capy64/Runtime/RuntimeManager.cs +++ b/Capy64/Runtime/RuntimeManager.cs @@ -32,8 +32,8 @@ internal class RuntimeManager : IComponent private static bool close = false; private static bool inPanic = false; - private static IGame _game; - public RuntimeManager(IGame game) + private static Capy64 _game; + public RuntimeManager(Capy64 game) { _game = game; diff --git a/Capy64/Worker.cs b/Capy64/Worker.cs deleted file mode 100644 index 5600877..0000000 --- a/Capy64/Worker.cs +++ /dev/null @@ -1,73 +0,0 @@ -// This file is part of Capy64 - https://github.com/Ale32bit/Capy64 -// Copyright 2023 Alessandro "AlexDevs" Proto -// -// Licensed under the Apache License, Version 2.0 (the "License"). -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using Microsoft.Extensions.Hosting; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Capy64; - -public class Worker : IHostedService -{ - private readonly IGame _game; - private readonly IHostApplicationLifetime _appLifetime; - private readonly IServiceProvider _serviceProvider; - - public Worker(IGame game, IHostApplicationLifetime appLifetime, IServiceProvider serviceProvider) - { - _game = game; - _appLifetime = appLifetime; - _serviceProvider = serviceProvider; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - _appLifetime.ApplicationStarted.Register(OnStarted); - _appLifetime.ApplicationStopping.Register(OnStopping); - _appLifetime.ApplicationStopped.Register(OnStopped); - - _game.Exiting += OnGameExiting; - - _game.ConfigureServices(_serviceProvider); - - return Task.CompletedTask; - } - - private void OnGameExiting(object sender, EventArgs e) - { - StopAsync(new CancellationToken()); - } - - public Task StopAsync(CancellationToken cancellationToken) - { - _appLifetime.StopApplication(); - - return Task.CompletedTask; - } - - private void OnStarted() - { - _game.Run(); - } - - private void OnStopping() - { - } - - private void OnStopped() - { - } -} \ No newline at end of file diff --git a/ExamplePlugin/MyPlugin.cs b/ExamplePlugin/MyPlugin.cs index d5e4b28..9f43d1e 100644 --- a/ExamplePlugin/MyPlugin.cs +++ b/ExamplePlugin/MyPlugin.cs @@ -1,13 +1,12 @@ -using Capy64; -using Capy64.API; +using Capy64.API; using KeraLua; namespace ExamplePlugin; public class MyPlugin : IComponent { - private static IGame _game; - public MyPlugin(IGame game) + private static Capy64.Capy64 _game; + public MyPlugin(Capy64.Capy64 game) { _game = game; }