use modular initing system rather than calling Init() method.

This commit is contained in:
Youmu Liu
2025-11-11 18:22:28 +08:00
parent 678a846e54
commit 44bf78b937
65 changed files with 1328 additions and 1814 deletions
+2 -1
View File
@@ -44,7 +44,6 @@ namespace StgSharp
private static byte[] _currentHash = [];
private static bool _inited;
private static int _mainThreadID = -1;
public static byte[] CurrentAssemblyHash
{
@@ -73,6 +72,8 @@ namespace StgSharp
internal set => _mainThreadID = value;
}
private static int _mainThreadID { get; set; } = -1;
/// <summary>
/// CustomizeInit an instance of OpenGL program, This method should be called before any
/// other World api.
@@ -0,0 +1,84 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="IStaticModule"
// 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.Collections;
using System.Collections.Generic;
namespace StgSharp
{
public interface IStaticModule
{
string ModuleName { get; }
void InitializeModule();
}
public class ModuleToInitializeCollection : IEnumerable<IStaticModule>, IStaticModule
{
private readonly List<IStaticModule> _modules = [];
internal ModuleToInitializeCollection(World.Initializer i)
{
_modules.Add(i);
}
public IEnumerator<IStaticModule> GetEnumerator()
{
return ((IEnumerable<IStaticModule>)_modules).GetEnumerator();
}
public void InitializeModule()
{
foreach (IStaticModule module in _modules) {
module.InitializeModule();
}
}
public ModuleToInitializeCollection UseModule<T>() where T: IStaticModule, new()
{
_modules.Add(new T());
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_modules).GetEnumerator();
}
void IStaticModule.InitializeModule()
{
throw new NotImplementedException();
}
string IStaticModule.ModuleName => "Module Initializer";
}
}
+8 -14
View File
@@ -25,38 +25,32 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp
{
public static partial class World
{
private static unsafe IntPtr[] monitorsArray = [];
public static unsafe IntPtr[] MonitorsHandleArray
{
get
{
if (monitorsArray == null)
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);
IntPtr* arrayHandle = GraphicFramework.glfwGetMonitors(&count);
MonitorsArray = new IntPtr[count];
Marshal.Copy((IntPtr)arrayHandle, MonitorsArray, 0, count);
Marshal.FreeHGlobal((IntPtr)arrayHandle);
}
return monitorsArray;
return MonitorsArray;
}
}
public static nint[] MonitorsArray { get; set; } = [];
public static IntPtr DefaultMonitor => MonitorsHandleArray[0];
}
+37 -19
View File
@@ -65,26 +65,9 @@ namespace StgSharp
_ => 8,
};
public static unsafe void Init()
public static ModuleToInitializeCollection Initialize()
{
mainTimeProvider = new StgSharpTime();
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);
MainThreadID = Environment.CurrentManagedThreadId;
Dialogue.LoadDialogue();
if (Dialogue.NeedShowWhenStartup) {
Dialogue.CreateDialogueProcessIfNotExist();
}
NativeIntrinsic.InitIntrinsicContext();
return new ModuleToInitializeCollection(new Initializer());
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -201,5 +184,40 @@ namespace StgSharp
}
}
internal static unsafe void InternalInitialize()
{
mainTimeProvider = new StgSharpTime();
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);
MainThreadID = System.Environment.CurrentManagedThreadId;
Dialogue.LoadDialogue();
if (Dialogue.NeedShowWhenStartup) {
Dialogue.CreateDialogueProcessIfNotExist();
}
NativeIntrinsic.InitIntrinsicContext();
}
internal class Initializer : IStaticModule
{
public string ModuleName => "StgSharp";
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void InitializeModule()
{
World.InternalInitialize();
}
}
}
}