code clean up

This commit is contained in:
Youmu Liu
2025-10-17 10:49:00 +08:00
parent d3e0b5d005
commit 1927f4fdf3
223 changed files with 5675 additions and 6160 deletions
+55 -58
View File
@@ -1,33 +1,30 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="DataBinding.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// -----------------------------------------------------------------------
// file="DataBinding"
// 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:
// 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.
//
// 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 StgSharp.HighPerformance;
using System;
@@ -40,40 +37,40 @@ namespace StgSharp.MVVM.ViewModel
{
#nullable enable
[UnmanagedFunctionPointer( CallingConvention.Cdecl )]
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate ref object BindingDataGetter();
[UnmanagedFunctionPointer( CallingConvention.Cdecl )]
public delegate void BindingDataSetter( ref object value );
[UnmanagedFunctionPointer( CallingConvention.Cdecl )]
public delegate bool MethodLookUpDelegate( string name, out Action callback );
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void BindingDataSetter(ref object value);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool MethodLookUpDelegate(string name, out Action callback);
public abstract partial class ViewModelBase
{
public bool MethodLookup( string methodName, out Action callback )
public bool MethodLookup(string methodName, out Action callback)
{
MethodInfo method = GetType().GetMethod( methodName )!;
if( method == null )
MethodInfo method = GetType().GetMethod(methodName)!;
if (method == null)
{
callback = null!;
return false;
}
try
{
callback = ( Action )Delegate.CreateDelegate( typeof( Action ), this, methodName );
callback = (Action)Delegate.CreateDelegate(typeof(Action), this, methodName);
return true;
}
catch( MissingMethodException )
catch (MissingMethodException)
{
callback = null!;
return false;
}
catch( MethodAccessException )
catch (MethodAccessException)
{
callback = null!;
return false;
}
catch( Exception )
catch (Exception)
{
throw;
}
@@ -81,20 +78,20 @@ namespace StgSharp.MVVM.ViewModel
}
[StructLayout( LayoutKind.Explicit )]
[StructLayout(LayoutKind.Explicit)]
public unsafe struct DataBindingEntry
{
[FieldOffset( 0 )] private IntPtr getterPtr;
[FieldOffset( 8 )] private IntPtr setterPtr;
[FieldOffset( 0 )] private M128 _mask;
[FieldOffset(0)] private IntPtr getterPtr;
[FieldOffset(8)] private IntPtr setterPtr;
[FieldOffset(0)] private M128 _mask;
public DataBindingEntry(
[NotNull] BindingDataGetter gettingCallback,
[NotNull] BindingDataSetter settingCallback )
[NotNull] BindingDataSetter settingCallback)
{
getterPtr = Marshal.GetFunctionPointerForDelegate( gettingCallback );
setterPtr = Marshal.GetFunctionPointerForDelegate( settingCallback );
getterPtr = Marshal.GetFunctionPointerForDelegate(gettingCallback);
setterPtr = Marshal.GetFunctionPointerForDelegate(settingCallback);
}
public static DataBindingEntry NullBinding
@@ -102,22 +99,22 @@ namespace StgSharp.MVVM.ViewModel
get => new DataBindingEntry();
}
public bool Equals( DataBindingEntry other )
public bool Equals(DataBindingEntry other)
{
return ( _mask == other._mask );
return (_mask == other._mask);
}
public override bool Equals( object obj )
public override bool Equals(object obj)
{
if( obj is not DataBindingEntry other ) {
if (obj is not DataBindingEntry other) {
return false;
}
return Equals( other );
return Equals(other);
}
public bool ExactEqual( DataBindingEntry other )
public bool ExactEqual(DataBindingEntry other)
{
return ( getterPtr == other.getterPtr ) && ( setterPtr == other.setterPtr );
return (getterPtr == other.getterPtr) && (setterPtr == other.setterPtr);
}
public override int GetHashCode()
@@ -127,20 +124,20 @@ namespace StgSharp.MVVM.ViewModel
public ref object GetValue()
{
return ref ( ( delegate* unmanaged[Cdecl]<ref object> )getterPtr )();
return ref ((delegate* unmanaged[Cdecl]<ref object>)getterPtr)();
}
public void SetValue( ref object value )
public void SetValue(ref object value)
{
( ( delegate* unmanaged[Cdecl]<ref object, void> )setterPtr )( ref value );
((delegate* unmanaged[Cdecl]<ref object, void>)setterPtr)(ref value);
}
public static bool operator !=( DataBindingEntry left, DataBindingEntry right )
public static bool operator !=(DataBindingEntry left, DataBindingEntry right)
{
return left._mask != right._mask;
}
public static bool operator ==( DataBindingEntry left, DataBindingEntry right )
public static bool operator ==(DataBindingEntry left, DataBindingEntry right)
{
return left._mask == right._mask;
}
+60 -64
View File
@@ -1,33 +1,30 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ShowViewPort.cs"
// Project: StepVisualizer
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// -----------------------------------------------------------------------
// file="ShowViewPort"
// 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:
// 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.
//
// 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 StgSharp.Controls;
using StgSharp.Graphics;
using StgSharp.MVVM;
@@ -56,39 +53,38 @@ namespace StgSharp.MVVM.ViewModel
/// <summary>
/// CustomizeInit this form and render it on screen, and respond with operation
/// </summary>
public unsafe void UseViewModel( string viewEntryName )
public unsafe void UseViewModel(string viewEntryName)
{
CustomizeInitialize();
UseViewInNextFrame( viewEntryName );
TimeSpanAwaitingToken fpsCountingToken = fpsCountProvider.ParticipantSpanAwaiting( 1 );
UseViewInNextFrame(viewEntryName);
TimeSpanAwaitingToken fpsCountingToken = fpsCountProvider.ParticipantSpanAwaiting(1);
fpsCountingToken.TimeSpanRefreshed += CountFps;
fpsCountingToken.MissTimeSpanRefreshed += CountFps;
if( _nextView != _currentView ) {
Interlocked.Exchange( ref _currentView, _nextView );
if (_nextView != _currentView) {
Interlocked.Exchange(ref _currentView, _nextView);
}
Thread renderThread = new Thread(
( token ) => {
try
{
ProcessFrameWithoutEvent();
}
catch( Exception ex )
{
InternalIO.InternalWriteLog(
$"Critical error when rendering:\n" + $"{ex}", LogType.Error );
ShouldClose = true;
throw;
}
} );
Thread renderThread = new Thread((token) => {
try
{
ProcessFrameWithoutEvent();
}
catch (Exception ex)
{
InternalIO.InternalWriteLog(
$"Critical error when rendering:\n" + $"{ex}", LogType.Error);
ShouldClose = true;
throw;
}
});
renderThread.Start();
/**/
using( TimeSpanAwaitingToken eventAwaitingToken = frameTimeProvider.ParticipantSpanAwaiting(
1 ) )
using (TimeSpanAwaitingToken eventAwaitingToken = frameTimeProvider.ParticipantSpanAwaiting(
1))
{
while( _closed )
while (_closed)
{
if( _nextView != _currentView ) {
Interlocked.Exchange( ref _currentView, _nextView );
if (_nextView != _currentView) {
Interlocked.Exchange(ref _currentView, _nextView);
}
InternalIO.glfwPollEvents();
eventAwaitingToken.WaitNextSpan();
@@ -102,24 +98,24 @@ namespace StgSharp.MVVM.ViewModel
frameTimeProvider.StopSpanProviding();
}
protected void UseViewInNextFrame( string name )
protected void UseViewInNextFrame(string name)
{
if( !allView.TryGetValue( name, out ViewBase v ) ) {
if (!allView.TryGetValue(name, out ViewBase v)) {
throw new KeyNotFoundException(
$"{$"Cannot find View named {name} "}{$"in current View Model of {GetType().FullName}"}" );
$"{$"Cannot find View named {name} "}{$"in current View Model of {GetType().FullName}"}");
}
CurrentView = v;
}
private void CountFps( object sender, EventArgs e )
private void CountFps(object sender, EventArgs e)
{
Interlocked.Exchange( ref _frameSpeed, _frameCount );
Interlocked.Exchange( ref _frameCount, 0 );
Interlocked.Exchange(ref _frameSpeed, _frameCount);
Interlocked.Exchange(ref _frameCount, 0);
}
private unsafe void PrivateShow()
{
if( 0 == InternalIO.glfwWindowShouldClose( this.viewPortDisplay.ViewPortHandle ) )
if (0 == InternalIO.glfwWindowShouldClose(this.viewPortDisplay.ViewPortHandle))
{
CurrentView.Use();
RenderStream.PollEvents();
@@ -128,18 +124,18 @@ namespace StgSharp.MVVM.ViewModel
private void ProcessFrameWithoutEvent()
{
using( TimeSpanAwaitingToken renderAwaitingToken = frameTimeProvider.ParticipantSpanAwaiting(
1 ) )
using (TimeSpanAwaitingToken renderAwaitingToken = frameTimeProvider.ParticipantSpanAwaiting(
1))
{
renderAwaitingToken.TimeSpanRefreshed += ( _, _ ) => { };
while( _closed )
renderAwaitingToken.TimeSpanRefreshed += (_, _) => { };
while (_closed)
{
foreach( ViewPort context in CurrentView.Render ) {
foreach (ViewPort context in CurrentView.Render) {
context.FlushSize();
}
CurrentView.Use();
renderAwaitingToken.WaitNextSpan();
Interlocked.Add( ref _frameCount, 1 );
Interlocked.Add(ref _frameCount, 1);
}
}
}
+27 -31
View File
@@ -1,33 +1,30 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// file="ViewManage.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// -----------------------------------------------------------------------
// file="ViewManage"
// 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:
// 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.
//
// 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 StgSharp.Graphics;
using StgSharp.Graphics.OpenGL;
using StgSharp.MVVM;
@@ -44,13 +41,12 @@ namespace StgSharp.MVVM.ViewModel
private Dictionary<string, ViewBase> allView;
public T CreateAndBindView<T>( string name, (int x, int y, int z) unitCubeSize )
where T: ViewBase, new()
public T CreateAndBindView<T>(string name, (int x, int y, int z) unitCubeSize) where T: ViewBase, new()
{
T ret = new T();
ret.SelfName = name;
ret.InternalInitialize( name, unitCubeSize, this );
allView.Add( name, ret );
ret.InternalInitialize(name, unitCubeSize, this);
allView.Add(name, ret);
return ret;
}
+2 -2
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="ViewModel"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. All rights reserved.
// 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