Remove dependency injection and hosting dependencies

This commit is contained in:
Alessandro Proto 2023-07-24 13:01:10 +02:00
parent d8d6e76729
commit b85adb960b
24 changed files with 64 additions and 196 deletions

View file

@ -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) { }
}

View file

@ -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<IConfiguration>();
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>("EngineMode", DefaultParameters.EngineMode));
SetEngineMode(Configuration.GetValue<EngineMode>("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);
}

View file

@ -40,11 +40,13 @@
<PackageReference Include="DiscordRichPresence" Version="1.1.3.18" />
<PackageReference Include="FontStashSharp.MonoGame" Version="1.2.8" />
<PackageReference Include="KeraLua" Version="1.3.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="MonoGame.Extended.Graphics" Version="3.8.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
<PackageReference Include="MonoGame.Content.Builder.Task" Version="3.8.1.303" />
<PackageReference Include="System.ComponentModel.Composition" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<EditorConfigFiles Remove="C:\Users\Alex\source\repos\Capy64\Capy64\Capy64\.editorconfig" />

View file

@ -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<IComponent> NativePlugins { get; }
IList<IComponent> 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<EventArgs> Exiting;
void Run();
void Exit();
// Integrations
DiscordIntegration Discord { get; }
}

View file

@ -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);

View file

@ -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);
}
}

View file

@ -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<IGame>(game);
services.AddHostedService<Worker>();
})
.Build();
await host.RunAsync();
game.Run();

View file

@ -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;

View file

@ -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;
}

View file

@ -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())

View file

@ -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;
}

View file

@ -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<WebSocketClient.Client> 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)

View file

@ -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;
}

View file

@ -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;

View file

@ -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<uint, Timer> timers = new();
public TimerLib(IGame game)
public TimerLib(Capy64 game)
{
_game = game;

View file

@ -25,8 +25,8 @@ public class ObjectManager : IComponent
{
private static readonly ConcurrentDictionary<nint, object> _objects = new();
private static IGame _game;
public ObjectManager(IGame game)
private static Capy64 _game;
public ObjectManager(Capy64 game)
{
_game = game;
_game.EventEmitter.OnClose += OnClose;

View file

@ -88,6 +88,8 @@ public class FileHandle : IComponent
new(),
};
public FileHandle(Capy64 _) { }
public void LuaInit(Lua L)
{
CreateMeta(L);

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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;
}

View file

@ -73,6 +73,8 @@ public class WebSocketClient : IComponent
new(),
};
public WebSocketClient(Capy64 _) { }
public void LuaInit(Lua L)
{
CreateMeta(L);

View file

@ -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;

View file

@ -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()
{
}
}

View file

@ -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;
}