Better module loading.
This commit is contained in:
@@ -1,91 +1,81 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SwissTable"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SwissTable"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Intrinsics.Arm;
|
||||
using System.Runtime.Intrinsics.X86;
|
||||
|
||||
namespace StgSharp.Collections
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public unsafe partial class SwissTable
|
||||
{
|
||||
|
||||
private const int BucketCount = 1 << EntryMaskWidth;
|
||||
private const int BucketSize = 256;
|
||||
private const int EntryMaskWidth = 10; // 1024 entries total, 256 per bucket
|
||||
public sealed unsafe partial class SwissTable
|
||||
{
|
||||
|
||||
private const int BucketCount = 1 << EntryMaskWidth;
|
||||
private const int BucketSize = 256;
|
||||
private const int EntryMaskWidth = 10; // 1024 entries total, 256 per bucket
|
||||
public const int MaxCapacity = 1024 * BucketSize; // 256 entries per bucket, 1024 buckets
|
||||
|
||||
private readonly Entry* _entries;
|
||||
|
||||
private SwissTable(
|
||||
Entry* buffer
|
||||
)
|
||||
{
|
||||
_entries = buffer;
|
||||
for (int i = 0; i < BucketCount; i++)
|
||||
{
|
||||
// Set all control bytes to 0xFF (empty)
|
||||
Unsafe.InitBlock(&_entries[i].Bucket.Control, 0xFF, BucketSize);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAddOrSet(
|
||||
nint key,
|
||||
nint value
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool TryAddOrSet(
|
||||
nint key,
|
||||
ref nint value,
|
||||
out bool isNewKey
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
return IBucketScanner.Current.TrySet(key, hash, bucket, value, out isNewKey);
|
||||
}
|
||||
|
||||
public bool TryGet(
|
||||
nint key,
|
||||
out nint value
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
private readonly Entry* _entries;
|
||||
|
||||
private SwissTable(Entry* buffer)
|
||||
{
|
||||
_entries = buffer;
|
||||
for (int i = 0; i < BucketCount; i++)
|
||||
{
|
||||
// Set all control bytes to 0xFF (empty)
|
||||
Unsafe.InitBlock(&_entries[i].Bucket.Control, 0xFF, BucketSize);
|
||||
}
|
||||
}
|
||||
|
||||
public static SwissTable Create()
|
||||
{
|
||||
Entry* buffer = (Entry*)NativeMemory.AlignedAlloc((nuint)Marshal.SizeOf<Entry>(), 16);
|
||||
return new SwissTable(buffer);
|
||||
}
|
||||
|
||||
public bool TryAddOrSet(nint key, nint value, out bool isNewKey)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
return IBucketScanner.Current.TrySet(key, hash, bucket, value, out isNewKey);
|
||||
}
|
||||
|
||||
public bool TryGet(nint key, out nint value)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
return IBucketScanner.Current.TryFind(key, hash, bucket, out value);
|
||||
/*
|
||||
* hash = CRC32 of key
|
||||
@@ -93,103 +83,89 @@ namespace StgSharp.Collections
|
||||
* ctrl = 8-15 of hash
|
||||
* index = rest of hash
|
||||
* find value of hash
|
||||
*/
|
||||
}
|
||||
|
||||
public bool TryRemove(
|
||||
nint key,
|
||||
out nint value
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
return IBucketScanner.Current.TryRemove(key, hash, bucket, out value);
|
||||
}
|
||||
|
||||
[InlineArray(BucketCount)]
|
||||
private struct Entry
|
||||
{
|
||||
|
||||
public Bucket Bucket;
|
||||
|
||||
}
|
||||
|
||||
private unsafe struct Bucket
|
||||
{
|
||||
|
||||
public BucketControl Control;
|
||||
public BucketKeyStorage Keys;
|
||||
public BucketValueStorage Values;
|
||||
|
||||
}
|
||||
|
||||
private interface IBucketScanner
|
||||
{
|
||||
|
||||
static IBucketScanner Current { get; } = Create();
|
||||
|
||||
bool TryFind(
|
||||
nint key,
|
||||
uint Hash,
|
||||
Bucket* bucket,
|
||||
out nint value
|
||||
);
|
||||
|
||||
unsafe bool TryRemove(
|
||||
nint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
out nint value
|
||||
);
|
||||
|
||||
bool TrySet(
|
||||
nint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
nint value,
|
||||
out bool isNewKey
|
||||
);
|
||||
|
||||
private static IBucketScanner Create()
|
||||
{
|
||||
if (X86Base.IsSupported)
|
||||
{
|
||||
return new BucketScannerX86();
|
||||
} else if (ArmBase.IsSupported)
|
||||
{
|
||||
throw new NotSupportedException("ARM architecture is currently not supported for bucket scanning. Please use an x86 platform.");
|
||||
} else
|
||||
{
|
||||
throw new NotSupportedException("No suitable bucket scanner implementation found for the current platform.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketControl
|
||||
{
|
||||
|
||||
public byte Control;
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketKeyStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketValueStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public bool TryRemove(nint key, out nint value)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(0xffff9961, (ulong)key);
|
||||
short entry = (short)(hash & ((1 << 11) - 1));
|
||||
Bucket* bucket = &_entries[entry].Bucket;
|
||||
return IBucketScanner.Current.TryRemove(key, hash, bucket, out value);
|
||||
}
|
||||
|
||||
~SwissTable()
|
||||
{
|
||||
NativeMemory.AlignedFree(_entries);
|
||||
}
|
||||
|
||||
[InlineArray(BucketCount)]
|
||||
private struct Entry
|
||||
{
|
||||
|
||||
public Bucket Bucket;
|
||||
|
||||
}
|
||||
|
||||
private unsafe struct Bucket
|
||||
{
|
||||
|
||||
public BucketControl Control;
|
||||
public BucketKeyStorage Keys;
|
||||
public BucketValueStorage Values;
|
||||
|
||||
}
|
||||
|
||||
private interface IBucketScanner
|
||||
{
|
||||
|
||||
static IBucketScanner Current { get; } = Create();
|
||||
|
||||
bool TryFind(nint key, uint Hash, Bucket* bucket, out nint value);
|
||||
|
||||
unsafe bool TryRemove(nint key, uint hash, Bucket* bucket, out nint value);
|
||||
|
||||
bool TrySet(nint key, uint hash, Bucket* bucket, nint value, out bool isNewKey);
|
||||
|
||||
private static IBucketScanner Create()
|
||||
{
|
||||
if (X86Base.IsSupported)
|
||||
{
|
||||
return new BucketScannerX86();
|
||||
} else if (ArmBase.IsSupported)
|
||||
{
|
||||
throw new NotSupportedException("ARM architecture is currently not supported for bucket scanning. Please use an x86 platform.");
|
||||
} else
|
||||
{
|
||||
throw new NotSupportedException("No suitable bucket scanner implementation found for the current platform.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketControl
|
||||
{
|
||||
|
||||
public byte Control;
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketKeyStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
|
||||
}
|
||||
|
||||
[InlineArray(BucketSize)]
|
||||
private struct BucketValueStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,30 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="GlobalSetting"
|
||||
// file="GlobalSetting.cs"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the “Software”), to deal in the Software without restriction,
|
||||
// including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software,
|
||||
// and to permit persons to whom the Software is furnished to do so,
|
||||
// subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and
|
||||
// this permission notice shall be included in all copies
|
||||
// or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED “AS IS”,
|
||||
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH
|
||||
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
@@ -74,21 +77,6 @@ namespace StgSharp
|
||||
|
||||
private static int _mainThreadID { get; set; } = -1;
|
||||
|
||||
/// <summary>
|
||||
/// CustomizeInit an instance of OpenGL program, This method should be called before any
|
||||
/// other World api.
|
||||
/// </summary>
|
||||
public static void InitGL(int majorVersion, int minorVersion)
|
||||
{
|
||||
if (API == default) {
|
||||
API = GraphicAPI.GL;
|
||||
}
|
||||
if (API != GraphicAPI.GL) {
|
||||
return;
|
||||
}
|
||||
GraphicFramework.InternalInitGL(majorVersion, minorVersion);
|
||||
}
|
||||
|
||||
public static class GlobalSetting
|
||||
{
|
||||
|
||||
@@ -99,7 +87,7 @@ namespace StgSharp
|
||||
get => vsyncActivated;
|
||||
set
|
||||
{
|
||||
GraphicFramework.glfwSwapInterval(value ? 1 : 0);
|
||||
// GraphicFramework.glfwSwapInterval(value ? 1 : 0);
|
||||
vsyncActivated = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="IStaticModule"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -31,56 +31,254 @@ using System.Collections.Generic;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a statically activated module that can be registered and initialized as part of
|
||||
/// the application startup sequence, and gracefully torn down on shutdown.
|
||||
/// </summary>
|
||||
public interface IStaticModule
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Gets the display name of this module, used for logging and diagnostics.
|
||||
/// </summary>
|
||||
string ModuleName { get; }
|
||||
|
||||
void InitializeModule();
|
||||
/// <summary>
|
||||
/// Performs all initialization work for this module using the provided profile.
|
||||
/// </summary>
|
||||
/// <param name="profile">
|
||||
/// The configuration profile supplied at registration time. Use <see
|
||||
/// cref="IModuleInitializeProfile.Empty" /> if no configuration is required.
|
||||
/// </param>
|
||||
void InitializeModule(IModuleInitializeProfile profile);
|
||||
|
||||
/// <summary>
|
||||
/// Releases all resources acquired during <see cref="InitializeModule" />. Implementations should
|
||||
/// be idempotent: calling this method more than once must not throw.
|
||||
/// </summary>
|
||||
void UninitializeModule();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marker interface for module initialization configuration. Implementations are plain data
|
||||
/// carriers; no static factory is required.
|
||||
/// </summary>
|
||||
public interface IModuleInitializeProfile
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// A shared, allocation-free empty profile for modules that require no configuration.
|
||||
/// </summary>
|
||||
static readonly IModuleInitializeProfile Empty = EmptyModuleInitializeProfile.Instance;
|
||||
|
||||
private sealed class EmptyModuleInitializeProfile : IModuleInitializeProfile
|
||||
{
|
||||
|
||||
public static readonly EmptyModuleInitializeProfile Instance = new();
|
||||
|
||||
private EmptyModuleInitializeProfile() { }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pairs an <see cref="IStaticModule" /> with its <see cref="IModuleInitializeProfile" />,
|
||||
/// ensuring the two are never stored or iterated independently.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Using a <see langword="readonly struct" /> avoids the boxing overhead that would occur if
|
||||
/// the pair were stored as two parallel lists of reference types.
|
||||
/// </remarks>
|
||||
public readonly struct ModuleDescriptor
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new <see cref="ModuleDescriptor" /> using <see
|
||||
/// cref="IModuleInitializeProfile.Empty" /> as the profile.
|
||||
/// </summary>
|
||||
public ModuleDescriptor(IStaticModule module) : this(module, IModuleInitializeProfile.Empty) { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new <see cref="ModuleDescriptor" /> with an explicit profile.
|
||||
/// </summary>
|
||||
public ModuleDescriptor(IStaticModule module, IModuleInitializeProfile profile)
|
||||
{
|
||||
Module = module;
|
||||
Profile = profile;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the configuration profile associated with the module.
|
||||
/// </summary>
|
||||
public IModuleInitializeProfile Profile { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the module to be initialized.
|
||||
/// </summary>
|
||||
public IStaticModule Module { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Calls <see cref="IStaticModule.InitializeModule" /> with the stored profile.
|
||||
/// </summary>
|
||||
public void Initialize()
|
||||
{
|
||||
Module.InitializeModule(Profile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calls <see cref="IStaticModule.UninitializeModule" /> on the stored module.
|
||||
/// </summary>
|
||||
public void Uninitialize()
|
||||
{
|
||||
Module.UninitializeModule();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An ordered collection of modules to be initialized during application startup and
|
||||
/// uninitialized on shutdown. Supports three registration strategies so that the <c>new()</c>
|
||||
/// generic constraint is never a hard requirement.
|
||||
/// </summary>
|
||||
public sealed class ModuleToInitializeCollection : IEnumerable<IStaticModule>, IStaticModule
|
||||
{
|
||||
|
||||
private readonly List<IStaticModule> _modules = [];
|
||||
private readonly List<ModuleDescriptor> _descriptors = [];
|
||||
|
||||
internal ModuleToInitializeCollection(
|
||||
World.Initializer i
|
||||
)
|
||||
internal ModuleToInitializeCollection(World.Initializer i)
|
||||
{
|
||||
_modules.Add(i);
|
||||
_descriptors.Add(new ModuleDescriptor(i));
|
||||
}
|
||||
|
||||
// ── IEnumerable<IStaticModule> ───────────────────────────────────────────
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IStaticModule> GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable<IStaticModule>)_modules).GetEnumerator();
|
||||
}
|
||||
|
||||
public void InitializeModule()
|
||||
{
|
||||
foreach (IStaticModule module in _modules) {
|
||||
module.InitializeModule();
|
||||
foreach (ModuleDescriptor d in _descriptors) {
|
||||
yield return d.Module;
|
||||
}
|
||||
}
|
||||
|
||||
public ModuleToInitializeCollection UseModule<T>() where T : IStaticModule, new()
|
||||
// ── Initialization / Uninitialization ────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Initializes all registered modules in the order they were registered. Each module
|
||||
/// receives the profile that was supplied at registration time.
|
||||
/// </summary>
|
||||
public void InitializeModule()
|
||||
{
|
||||
_modules.Add(new T());
|
||||
for (int i = 0; i < _descriptors.Count; i++) {
|
||||
_descriptors[i].Initialize();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uninitializes all registered modules in <b> reverse </b> registration order, mirroring
|
||||
/// the typical LIFO teardown convention (e.g., GLFW must be unloaded after all subsystems
|
||||
/// that depend on it).
|
||||
/// </summary>
|
||||
public void UninitializeModule()
|
||||
{
|
||||
for (int i = _descriptors.Count - 1; i >= 0; i--) {
|
||||
_descriptors[i].Uninitialize();
|
||||
}
|
||||
}
|
||||
|
||||
// ── Registration API ─────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Registers a module of type <typeparamref name="T" /> using its default constructor.
|
||||
/// This is the most concise overload for simple modules that expose a public
|
||||
/// parameterless constructor.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">
|
||||
/// A concrete module type that implements <see cref="IStaticModule" /> and exposes a
|
||||
/// public parameterless constructor.
|
||||
/// </typeparam>
|
||||
/// <param name="profile">
|
||||
/// Optional initialization profile. Defaults to <see
|
||||
/// cref="IModuleInitializeProfile.Empty" /> when <see langword="null" />.
|
||||
/// </param>
|
||||
public ModuleToInitializeCollection UseModule<T>(IModuleInitializeProfile? profile = null)
|
||||
where T: IStaticModule, new()
|
||||
{
|
||||
return UseModule(static() => new T(), profile);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a module produced by a factory delegate. This overload imposes no generic
|
||||
/// constraints and supports any construction strategy, including dependency injection and
|
||||
/// parameterized constructors.
|
||||
/// </summary>
|
||||
/// <param name="factory">
|
||||
/// A delegate that constructs and returns the module instance. The delegate is invoked
|
||||
/// once, immediately at registration time. If <paramref name="factory" /> is <see
|
||||
/// langword="null" /> or returns <see langword="null" />, the call is silently ignored
|
||||
/// and the collection is returned unchanged.
|
||||
/// </param>
|
||||
/// <param name="profile">
|
||||
/// Optional initialization profile. Defaults to <see
|
||||
/// cref="IModuleInitializeProfile.Empty" /> when <see langword="null" />.
|
||||
/// </param>
|
||||
public ModuleToInitializeCollection UseModule(
|
||||
Func<IStaticModule> factory,
|
||||
IModuleInitializeProfile? profile = null)
|
||||
{
|
||||
if (factory is null) {
|
||||
return this;
|
||||
}
|
||||
IStaticModule? mod = factory();
|
||||
if (mod is null) {
|
||||
return this;
|
||||
}
|
||||
_descriptors.Add(new ModuleDescriptor(
|
||||
mod,
|
||||
profile ?? IModuleInitializeProfile.Empty));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a pre-existing module instance. Use this overload when the module is
|
||||
/// provided externally, such as from a DI container, an object pool, or a singleton.
|
||||
/// </summary>
|
||||
/// <param name="module">
|
||||
/// The module instance to register.
|
||||
/// </param>
|
||||
/// <param name="profile">
|
||||
/// Optional initialization profile. Defaults to <see
|
||||
/// cref="IModuleInitializeProfile.Empty" /> when <see langword="null" />.
|
||||
/// </param>
|
||||
public ModuleToInitializeCollection UseModule(IStaticModule module, IModuleInitializeProfile? profile = null)
|
||||
{
|
||||
_descriptors.Add(new ModuleDescriptor(
|
||||
module,
|
||||
profile ?? IModuleInitializeProfile.Empty));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable)_modules).GetEnumerator();
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
void IStaticModule.InitializeModule()
|
||||
// ── IStaticModule explicit implementation ────────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// Explicit <see cref="IStaticModule" /> implementation. Delegates to <see
|
||||
/// cref="InitializeModule()" />, ignoring the supplied profile because each contained module uses
|
||||
/// its own registered profile.
|
||||
/// </summary>
|
||||
void IStaticModule.InitializeModule(IModuleInitializeProfile profile)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
InitializeModule();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
string IStaticModule.ModuleName => "Module Initializer";
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="Monitors"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp
|
||||
{
|
||||
public static partial class World
|
||||
{
|
||||
|
||||
public static unsafe IntPtr[] MonitorsHandleArray
|
||||
{
|
||||
get
|
||||
{
|
||||
if (MonitorsArray == null)
|
||||
{
|
||||
int count = 0;
|
||||
IntPtr* arrayHandle = GraphicFramework.glfwGetMonitors(&count);
|
||||
MonitorsArray = new IntPtr[count];
|
||||
Marshal.Copy((IntPtr)arrayHandle, MonitorsArray, 0, count);
|
||||
Marshal.FreeHGlobal((IntPtr)arrayHandle);
|
||||
}
|
||||
return MonitorsArray;
|
||||
}
|
||||
}
|
||||
|
||||
public static nint[] MonitorsArray { get; set; } = [];
|
||||
|
||||
public static IntPtr DefaultMonitor => MonitorsHandleArray[0];
|
||||
|
||||
}
|
||||
}
|
||||
@@ -58,29 +58,6 @@ namespace StgSharp
|
||||
private static uint markCount = 0;
|
||||
internal static GraphicAPI API = default;
|
||||
|
||||
public static unsafe int DefaultSIMDAlignment
|
||||
{
|
||||
get
|
||||
{
|
||||
SIMDID id = NativeIntrinsic.IntrinsicMask;
|
||||
switch (id.MaskByte[0] & 0b_00001111)
|
||||
{
|
||||
case 1:
|
||||
return id.MaskByte[1] switch
|
||||
{
|
||||
1 => 16,
|
||||
2 => 32,
|
||||
3 => 64,
|
||||
4 => 64,
|
||||
5 => 64,
|
||||
_ => 8,
|
||||
};
|
||||
default:
|
||||
return 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ModuleToInitializeCollection Initialize()
|
||||
{
|
||||
return new ModuleToInitializeCollection(new Initializer());
|
||||
@@ -122,7 +99,8 @@ namespace StgSharp
|
||||
public static void Terminate(int exitCode)
|
||||
{
|
||||
MainTimeProvider.StopProvidingTime();
|
||||
GraphicFramework.glfwTerminate();
|
||||
|
||||
// GraphicFramework.glfwTerminate();
|
||||
Environment.Exit(exitCode);
|
||||
}
|
||||
|
||||
@@ -204,10 +182,6 @@ namespace StgSharp
|
||||
{
|
||||
// Initialize time provider and start providing time
|
||||
MainTimeProvider.StartProvidingTime();
|
||||
GraphicFramework.LoadGlfw();
|
||||
if (GraphicFramework.glfwInit() == 0) {
|
||||
throw new Exception("Failed to init system graphic environment.");
|
||||
}
|
||||
DefaultLog.InternalAppendLog("\n\n\n");
|
||||
DefaultLog.InternalWriteLog(
|
||||
$"Program {Assembly.GetEntryAssembly()!.FullName} on {Environment.MachineName} Started.", LogType.Info);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//-----------------------------------------------------------------------
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="PipelineScheduler"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -25,8 +25,6 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.Stg;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
Reference in New Issue
Block a user