1- using System ;
1+ using System ;
22using System . Collections . Generic ;
33using System . Linq ;
44using GS2Engine . Enums ;
55using GS2Engine . GS2 . Script ;
66using GS2Engine . Models ;
77
8- namespace GS2Engine . Extensions
8+ namespace GS2Engine . Extensions ;
9+
10+ public static class StackEntryExtensions
911{
10- public static class StackEntryExtensions
11- {
12- public static StackEntry ToStackEntry ( this object ? stackObject , bool isVariable = false ) =>
13- new ( isVariable ? StackEntryType . Variable : GetStackEntryType ( stackObject ) , FixStackValue ( stackObject ) ) ;
12+ public static StackEntry ToStackEntry ( this object ? stackObject , bool isVariable = false ) =>
13+ new ( isVariable ? StackEntryType . Variable : GetStackEntryType ( stackObject ) , FixStackValue ( stackObject ) ) ;
1414
15- private static object ? FixStackValue ( object ? stackObject )
15+ private static object ? FixStackValue ( object ? stackObject )
16+ {
17+ return stackObject switch
1618 {
17- return stackObject switch
18- {
19- string => ( TString ) stackObject . ToString ( ) ,
20- TString => stackObject ,
21- int i => ( double ) i ,
22- double d => d ,
23- float f => ( double ) f ,
24- decimal o => ( double ) o ,
25- bool b => b ,
26- _ => stackObject ,
27- } ;
28- }
19+ string => ( TString ) stackObject . ToString ( ) ,
20+ TString => stackObject ,
21+ int i => ( double ) i ,
22+ double d => d ,
23+ float f => ( double ) f ,
24+ decimal o => ( double ) o ,
25+ bool b => b ,
26+ _ => stackObject ,
27+ } ;
28+ }
2929
30- private static StackEntryType GetStackEntryType ( object ? stackObject )
30+ private static StackEntryType GetStackEntryType ( object ? stackObject )
31+ {
32+ switch ( Type . GetTypeCode ( stackObject ? . GetType ( ) ) )
3133 {
32- switch ( Type . GetTypeCode ( stackObject ? . GetType ( ) ) )
34+ case TypeCode . Boolean :
35+ return StackEntryType . Boolean ;
36+ case TypeCode . Byte :
37+ case TypeCode . Char :
38+ case TypeCode . Decimal :
39+ case TypeCode . Double :
40+ case TypeCode . Int16 :
41+ case TypeCode . Int32 :
42+ case TypeCode . Int64 :
43+ case TypeCode . UInt16 :
44+ case TypeCode . UInt32 :
45+ case TypeCode . UInt64 :
46+ case TypeCode . SByte :
47+ return StackEntryType . Number ;
48+ case TypeCode . String :
49+ case TypeCode . DateTime :
50+ return StackEntryType . String ;
51+ default :
3352 {
34- case TypeCode . Boolean :
35- return StackEntryType . Boolean ;
36- case TypeCode . Byte :
37- case TypeCode . Char :
38- case TypeCode . Decimal :
39- case TypeCode . Double :
40- case TypeCode . Int16 :
41- case TypeCode . Int32 :
42- case TypeCode . Int64 :
43- case TypeCode . UInt16 :
44- case TypeCode . UInt32 :
45- case TypeCode . UInt64 :
46- case TypeCode . SByte :
47- return StackEntryType . Number ;
48- case TypeCode . String :
49- case TypeCode . DateTime :
53+ var stackType = stackObject ? . GetType ( ) ;
54+ if ( stackType == typeof ( TString ) )
5055 return StackEntryType . String ;
51- default :
52- {
53- Type ? stackType = stackObject ? . GetType ( ) ;
54- if ( stackType == typeof ( TString ) )
55- return StackEntryType . String ;
5656
57- if ( stackType == typeof ( Script . Command ) )
58- return StackEntryType . Function ;
57+ if ( stackType == typeof ( Script . Command ) )
58+ return StackEntryType . Function ;
59+
60+ if ( stackType == typeof ( Script ) )
61+ return StackEntryType . Script ;
5962
60- if ( stackType != null && stackType . GetInterfaces ( )
61- . Any ( x => x . Name . Equals ( "IGuiControl" , StringComparison . CurrentCultureIgnoreCase ) ) )
62- return StackEntryType . Array ;
63+ if ( stackType != null && stackType . GetInterfaces ( )
64+ . Any ( x => x . Name . Equals ( "IGuiControl" , StringComparison . CurrentCultureIgnoreCase ) ) )
65+ return StackEntryType . Array ;
6366
64- if ( stackType == typeof ( VariableCollection ) )
65- return StackEntryType . Array ;
67+ if ( stackType == typeof ( VariableCollection ) )
68+ return StackEntryType . Array ;
6669
67- if ( stackObject is float )
68- return StackEntryType . Number ;
70+ if ( stackObject is float )
71+ return StackEntryType . Number ;
6972
70- if ( stackType is { IsGenericType : true } &&
71- stackType . GetGenericTypeDefinition ( ) . IsAssignableFrom ( typeof ( List < > ) ) )
72- return StackEntryType . Array ;
73+ if ( stackType is { IsGenericType : true } &&
74+ stackType . GetGenericTypeDefinition ( ) . IsAssignableFrom ( typeof ( List < > ) ) )
75+ return StackEntryType . Array ;
7376
74- throw new ArgumentOutOfRangeException ( ) ;
75- }
77+ throw new ArgumentOutOfRangeException ( ) ;
7678 }
7779 }
80+ }
7881
79- public static IStackEntry ToStackEntry ( this IEnumerable < string > stackObject ) =>
80- new StackEntry ( StackEntryType . Array , stackObject . ToList ( ) ) ;
82+ public static IStackEntry ToStackEntry ( this IEnumerable < string > stackObject ) =>
83+ new StackEntry ( StackEntryType . Array , stackObject . ToList ( ) ) ;
8184
82- public static IStackEntry ToStackEntry ( this IEnumerable < int > stackObject ) =>
83- new StackEntry ( StackEntryType . Array , stackObject . ToList ( ) ) ;
84- }
85+ public static IStackEntry ToStackEntry ( this IEnumerable < int > stackObject ) =>
86+ new StackEntry ( StackEntryType . Array , stackObject . ToList ( ) ) ;
8587}
0 commit comments