forked from KaBooMa/S1API
-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat(casino): expose game state and lifecycle events #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| using System.ComponentModel; | ||
| using System.Reflection; | ||
| using S1API.Casino; | ||
|
|
||
| #if IL2CPPMELON | ||
| using S1Casino = Il2CppScheduleOne.Casino; | ||
| #elif MONOMELON | ||
| using S1Casino = ScheduleOne.Casino; | ||
| #endif | ||
|
|
||
| namespace S1API.Tests.Casino; | ||
|
|
||
| public sealed class CasinoApiContractTests | ||
| { | ||
| [Fact] | ||
| public void NativeLifecyclePatchPointsExistInTargetRuntime() | ||
| { | ||
| Assert.NotNull(typeof(S1Casino.BlackjackGameController).GetMethod( | ||
| "set_CurrentStage", | ||
| BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); | ||
| Assert.NotNull(typeof(S1Casino.RTBGameController).GetMethod( | ||
| "set_CurrentStage", | ||
| BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); | ||
| MethodBase? slotStartMethod = global::S1API.Internal.Patches.CasinoGamePatches | ||
| .FindSlotStartLogicMethod(typeof(S1Casino.SlotMachine)); | ||
| Assert.NotNull(slotStartMethod); | ||
| Assert.StartsWith("RpcLogic___StartSpin_", slotStartMethod!.Name); | ||
| Assert.NotNull(typeof(S1Casino.SlotMachine).GetMethod( | ||
| "DisplayOutcome", | ||
| BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ManagedEnumsPreserveNativeWireValues() | ||
| { | ||
| Assert.Equal((int)S1Casino.BlackjackGameController.EStage.WaitingForPlayers, (int)BlackjackStage.WaitingForPlayers); | ||
| Assert.Equal((int)S1Casino.BlackjackGameController.EStage.Ending, (int)BlackjackStage.Ending); | ||
| Assert.Equal((int)S1Casino.RTBGameController.EStage.RedOrBlack, (int)RideTheBusStage.RedOrBlack); | ||
| Assert.Equal((int)S1Casino.RTBGameController.EStage.Suit, (int)RideTheBusStage.Suit); | ||
| Assert.Equal((int)S1Casino.PlayingCard.ECardSuit.Clubs, (int)CasinoCardSuit.Clubs); | ||
| Assert.Equal((int)S1Casino.PlayingCard.ECardValue.King, (int)CasinoCardValue.King); | ||
| Assert.Equal((int)S1Casino.SlotMachine.ESymbol.Seven, (int)SlotSymbol.Seven); | ||
| Assert.Equal((int)S1Casino.SlotMachine.EOutcome.NoWin, (int)SlotOutcome.NoWin); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(typeof(CasinoPlayerSnapshot))] | ||
| [InlineData(typeof(SlotSpinSnapshot))] | ||
| public void SnapshotReferenceTypesExposeNoPublicSetters(Type snapshotType) | ||
| { | ||
| Assert.All( | ||
| snapshotType.GetProperties(BindingFlags.Public | BindingFlags.Instance), | ||
| property => Assert.Null(property.SetMethod)); | ||
| AssertPublicInstanceFieldsAreReadonly(snapshotType); | ||
| Assert.Empty(snapshotType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void CasinoWrappersCannotBePubliclyConstructedOrMutated() | ||
| { | ||
| Type[] wrapperTypes = | ||
| { | ||
| typeof(BlackjackGame), | ||
| typeof(RideTheBusGame), | ||
| typeof(SlotMachine) | ||
| }; | ||
|
|
||
| foreach (Type wrapperType in wrapperTypes) | ||
| { | ||
| Assert.Empty(wrapperType.GetConstructors(BindingFlags.Public | BindingFlags.Instance)); | ||
| Assert.All( | ||
| wrapperType.GetProperties(BindingFlags.Public | BindingFlags.Instance), | ||
| property => Assert.Null(property.SetMethod)); | ||
| AssertPublicInstanceFieldsAreReadonly(wrapperType); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void RegistrySurfaceIsReadOnlyDiscoveryAndQueriesOnly() | ||
| { | ||
| MethodInfo[] publicMethods = typeof(CasinoGameRegistry) | ||
| .GetMethods(BindingFlags.Public | BindingFlags.Static) | ||
| .Where(method => !method.IsSpecialName) | ||
| .ToArray(); | ||
|
|
||
| Assert.NotEmpty(publicMethods); | ||
| Assert.All(publicMethods, method => | ||
| Assert.True( | ||
| method.Name.StartsWith("Get", StringComparison.Ordinal) || | ||
| method.Name.StartsWith("Find", StringComparison.Ordinal), | ||
| $"Unexpected registry method: {method.Name}")); | ||
| Assert.DoesNotContain(publicMethods, method => method.ReturnType == typeof(void)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LegacyNativeSlotLookupRemainsAsAnObsoleteCompatibilityShim() | ||
| { | ||
| MethodInfo method = typeof(SlotMachineHelper).GetMethod( | ||
| nameof(SlotMachineHelper.FindNearestSlotMachine), | ||
| BindingFlags.Public | BindingFlags.Static, | ||
| binder: null, | ||
| types: new[] { typeof(UnityEngine.Vector3), typeof(float) }, | ||
| modifiers: null)!; | ||
|
|
||
| Assert.NotNull(method); | ||
| ObsoleteAttribute obsolete = Assert.IsType<ObsoleteAttribute>( | ||
| method.GetCustomAttribute<ObsoleteAttribute>()); | ||
| Assert.False(obsolete.IsError); | ||
| EditorBrowsableAttribute editorBrowsable = Assert.IsType<EditorBrowsableAttribute>( | ||
| method.GetCustomAttribute<EditorBrowsableAttribute>()); | ||
| Assert.Equal(EditorBrowsableState.Never, editorBrowsable.State); | ||
| Assert.Equal(typeof(S1Casino.SlotMachine), method.ReturnType); | ||
|
ifBars marked this conversation as resolved.
|
||
|
|
||
| MethodInfo managedMethod = typeof(CasinoGameRegistry).GetMethod( | ||
| nameof(CasinoGameRegistry.FindNearestSlotMachine), | ||
| BindingFlags.Public | BindingFlags.Static, | ||
| binder: null, | ||
| types: new[] { typeof(UnityEngine.Vector3), typeof(float) }, | ||
| modifiers: null)!; | ||
|
|
||
| Assert.NotNull(managedMethod); | ||
| Assert.Equal(typeof(SlotMachine), managedMethod.ReturnType); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void PublicCasinoApiDoesNotExposeNativeCasinoTypes() | ||
| { | ||
| Type[] publicCasinoTypes = | ||
| { | ||
| typeof(CasinoGameRegistry), | ||
| typeof(CasinoGameTable), | ||
| typeof(BlackjackGame), | ||
| typeof(RideTheBusGame), | ||
| typeof(SlotMachine), | ||
| typeof(CasinoPlayerSnapshot), | ||
| typeof(CasinoCardSnapshot), | ||
| typeof(SlotSpinSnapshot) | ||
| }; | ||
|
|
||
| foreach (Type type in publicCasinoTypes) | ||
| { | ||
| IEnumerable<Type> exposedTypes = type | ||
| .GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static) | ||
| .SelectMany(GetExposedTypes); | ||
|
|
||
| Assert.DoesNotContain(exposedTypes, exposed => | ||
| exposed.Namespace?.Contains("ScheduleOne.Casino", StringComparison.Ordinal) == true); | ||
|
ifBars marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<Type> GetExposedTypes(MemberInfo member) | ||
| { | ||
| IEnumerable<Type> declaredTypes; | ||
| switch (member) | ||
| { | ||
| case PropertyInfo property: | ||
| declaredTypes = new[] { property.PropertyType }; | ||
| break; | ||
| case FieldInfo field: | ||
| declaredTypes = new[] { field.FieldType }; | ||
| break; | ||
| case EventInfo eventInfo when eventInfo.EventHandlerType != null: | ||
| declaredTypes = new[] { eventInfo.EventHandlerType }; | ||
| break; | ||
| case MethodInfo method: | ||
| declaredTypes = new[] { method.ReturnType } | ||
| .Concat(method.GetParameters().Select(parameter => parameter.ParameterType)); | ||
| break; | ||
| case ConstructorInfo constructor: | ||
| declaredTypes = constructor.GetParameters().Select(parameter => parameter.ParameterType); | ||
| break; | ||
| default: | ||
| return Array.Empty<Type>(); | ||
| } | ||
|
|
||
| return declaredTypes.SelectMany(ExpandCompositeType); | ||
| } | ||
|
|
||
| private static IEnumerable<Type> ExpandCompositeType(Type root) | ||
| { | ||
| var pending = new Stack<Type>(); | ||
| var visited = new HashSet<Type>(); | ||
| pending.Push(root); | ||
|
|
||
| while (pending.Count > 0) | ||
| { | ||
| Type current = pending.Pop(); | ||
| if (!visited.Add(current)) | ||
| continue; | ||
|
|
||
| yield return current; | ||
|
|
||
| if (current.HasElementType && current.GetElementType() is Type elementType) | ||
| pending.Push(elementType); | ||
|
|
||
| foreach (Type argument in current.GetGenericArguments()) | ||
| pending.Push(argument); | ||
|
|
||
| if (current.BaseType != null) | ||
| pending.Push(current.BaseType); | ||
|
|
||
| foreach (Type implementedInterface in current.GetInterfaces()) | ||
| pending.Push(implementedInterface); | ||
| } | ||
| } | ||
|
|
||
| private static void AssertPublicInstanceFieldsAreReadonly(Type type) | ||
| { | ||
| Assert.All( | ||
| type.GetFields(BindingFlags.Public | BindingFlags.Instance), | ||
| field => Assert.True(field.IsInitOnly, $"{type.Name}.{field.Name} must be readonly.")); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.