From 057a030c2075485a8476c0c6d7306189f1e7cf60 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Tue, 21 Jul 2020 02:36:11 +0200 Subject: [PATCH] Update music block and attempt to fix test --- GamecraftModdingAPI/Block.cs | 15 ++- GamecraftModdingAPI/Blocks/BlockTests.cs | 17 +++- GamecraftModdingAPI/Blocks/MusicBlock.cs | 123 +++++++++++------------ 3 files changed, 81 insertions(+), 74 deletions(-) diff --git a/GamecraftModdingAPI/Block.cs b/GamecraftModdingAPI/Block.cs index 5f30a64..2f9a901 100644 --- a/GamecraftModdingAPI/Block.cs +++ b/GamecraftModdingAPI/Block.cs @@ -29,7 +29,7 @@ namespace GamecraftModdingAPI protected static readonly SignalEngine SignalEngine = new SignalEngine(); protected static readonly BlockEventsEngine BlockEventsEngine = new BlockEventsEngine(); protected static readonly ScalingEngine ScalingEngine = new ScalingEngine(); - + protected internal static readonly BlockEngine BlockEngine = new BlockEngine(); /// @@ -123,6 +123,7 @@ namespace GamecraftModdingAPI { {typeof(ConsoleBlock), new[] {CommonExclusiveGroups.BUILD_CONSOLE_BLOCK_GROUP}}, {typeof(Motor), new[] {CommonExclusiveGroups.BUILD_MOTOR_BLOCK_GROUP}}, + {typeof(MusicBlock), new[] {CommonExclusiveGroups.BUILD_MUSIC_BLOCK_GROUP}}, {typeof(Piston), new[] {CommonExclusiveGroups.BUILD_PISTON_BLOCK_GROUP}}, {typeof(Servo), new[] {CommonExclusiveGroups.BUILD_SERVO_BLOCK_GROUP}}, { @@ -198,9 +199,15 @@ namespace GamecraftModdingAPI public Block(EGID id) { Id = id; - if (typeToGroup.TryGetValue(GetType(), out var groups) && groups.All(gr => gr != id.groupID)) - throw new BlockTypeException("The block has the wrong group! The type is " + GetType() + - " while the group is " + id.groupID); + var type = GetType(); + if (typeToGroup.TryGetValue(type, out var groups)) + { + if (groups.All(gr => gr != id.groupID)) + throw new BlockTypeException("The block has the wrong group! The type is " + GetType() + + " while the group is " + id.groupID); + } + else if (type != typeof(Block)) + Logging.LogWarning($"Unknown block type! Add {type} to the dictionary."); } /// diff --git a/GamecraftModdingAPI/Blocks/BlockTests.cs b/GamecraftModdingAPI/Blocks/BlockTests.cs index 85328d2..6186a1d 100644 --- a/GamecraftModdingAPI/Blocks/BlockTests.cs +++ b/GamecraftModdingAPI/Blocks/BlockTests.cs @@ -74,8 +74,8 @@ namespace GamecraftModdingAPI.Blocks if (!Assert.CloseTo(b.MaximumForce, 750f, $"Servo.MaximumForce {b.MaximumForce} does not equal default value, possibly because it failed silently.", "Servo.MaximumForce is close enough to default.")) return; } - [APITestCase(TestType.EditMode)] - public static void TestMusicBlock() + [APITestCase(TestType.Game)] + public static void TestMusicBlock1() { Block newBlock = Block.PlaceNew(BlockIDs.MusicBlock, Unity.Mathematics.float3.zero + 2); MusicBlock b = null; // Note: the assignment operation is a lambda, which slightly confuses the compiler @@ -83,8 +83,19 @@ namespace GamecraftModdingAPI.Blocks if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return; if (!Assert.CloseTo(b.Volume, 100f, $"MusicBlock.Volume {b.Volume} does not equal default value, possibly because it failed silently.", "MusicBlock.Volume is close enough to default.")) return; if (!Assert.Equal(b.TrackIndex, 0, $"MusicBlock.TrackIndex {b.TrackIndex} does not equal default value, possibly because it failed silently.", "MusicBlock.TrackIndex is equal to default.")) return; + _musicBlock = b; + } + + private static MusicBlock _musicBlock; + + [APITestCase(TestType.EditMode)] + public static void TestMusicBlock2() + { + //Block newBlock = Block.GetLastPlacedBlock(); + var b = _musicBlock; + if (!Assert.NotNull(b, "Block.Specialize() returned null, possibly because it failed silently.", "Specialized MusicBlock is not null.")) return; b.IsPlaying = true; // play sfx - if (!Assert.Equal(b.IsPlaying, true, $"MusicBlock.IsPlaying {b.IsPlaying} does not equal default value, possibly because it failed silently.", "MusicBlock.IsPlaying is set properly.")) return; + if (!Assert.Equal(b.IsPlaying, true, $"MusicBlock.IsPlaying {b.IsPlaying} does not equal true, possibly because it failed silently.", "MusicBlock.IsPlaying is set properly.")) return; if (!Assert.Equal(b.ChannelType, ChannelType.Character, $"MusicBlock.ChannelType {b.ChannelType} does not equal default value, possibly because it failed silently.", "MusicBlock.ChannelType is equal to default.")) return; //Assert.Log(b.Track.ToString()); if (!Assert.Equal(b.Track.ToString(), new Guid("3237ff8f-f5f2-4f84-8144-496ca280f8c0").ToString(), $"MusicBlock.Track {b.Track} does not equal default value, possibly because it failed silently.", "MusicBlock.Track is equal to default.")) return; diff --git a/GamecraftModdingAPI/Blocks/MusicBlock.cs b/GamecraftModdingAPI/Blocks/MusicBlock.cs index a7f862e..2128a45 100644 --- a/GamecraftModdingAPI/Blocks/MusicBlock.cs +++ b/GamecraftModdingAPI/Blocks/MusicBlock.cs @@ -3,58 +3,38 @@ using System; using FMOD.Studio; using FMODUnity; using Gamecraft.Wires; +using RobocraftX.Common; using RobocraftX.Blocks; using Svelto.ECS; using Unity.Mathematics; using GamecraftModdingAPI; +using GamecraftModdingAPI.Tests; using GamecraftModdingAPI.Utility; namespace GamecraftModdingAPI.Blocks { public class MusicBlock : Block { - public static MusicBlock PlaceNew(float3 position, - float3 rotation = default, BlockColors color = BlockColors.Default, byte darkness = 0, - int uscale = 1, float3 scale = default, Player player = null) - { - if (PlacementEngine.IsInGame && GameState.IsBuildMode()) - { - EGID id = PlacementEngine.PlaceBlock(BlockIDs.MusicBlock, color, darkness, - position, uscale, scale, player, rotation); - return new MusicBlock(id); - } - - return null; - } - public MusicBlock(EGID id) : base(id) { - if (!BlockEngine.GetBlockInfoExists(this.Id)) - { - throw new BlockTypeException($"Block is not a {this.GetType().Name} block"); - } } - public MusicBlock(uint id) : base(id) + public MusicBlock(uint id) : base(new EGID(id, CommonExclusiveGroups.BUILD_MUSIC_BLOCK_GROUP)) { - if (!BlockEngine.GetBlockInfoExists(this.Id)) - { - throw new BlockTypeException($"Block is not a {this.GetType().Name} block"); - } } public byte TrackIndex { get { - return BlockEngine.GetBlockInfo(Id).trackIndx; + return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct st) => st.trackIndx); } set { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - msdes.trackIndx = value; + BlockEngine.SetBlockInfo(this, + (ref MusicBlockDataEntityStruct msdes, byte val) => msdes.trackIndx = val, value); } } @@ -62,22 +42,24 @@ namespace GamecraftModdingAPI.Blocks { get { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - return msdes.fmod2DEventPaths.Get(msdes.trackIndx); + return BlockEngine.GetBlockInfo(this, + (MusicBlockDataEntityStruct msdes) => msdes.fmod2DEventPaths.Get(msdes.trackIndx)); } set { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - for (byte i = 0; i < msdes.fmod2DEventPaths.Count(); i++) + BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, Guid val) => { - Guid track = msdes.fmod2DEventPaths.Get(i); - if (track == value) + for (byte i = 0; i < msdes.fmod2DEventPaths.Count(); i++) { - msdes.trackIndx = i; - break; + Guid track = msdes.fmod2DEventPaths.Get(i); + if (track == val) + { + msdes.trackIndx = i; + break; + } } - } + }, value); } } @@ -85,13 +67,15 @@ namespace GamecraftModdingAPI.Blocks { get { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count()]; - for (byte i = 0; i < tracks.Length; i++) + return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => { - tracks[i] = msdes.fmod2DEventPaths.Get(i); - } - return tracks; + Guid[] tracks = new Guid[msdes.fmod2DEventPaths.Count()]; + for (byte i = 0; i < tracks.Length; i++) + { + tracks[i] = msdes.fmod2DEventPaths.Get(i); + } + return tracks; + }); } } @@ -99,13 +83,13 @@ namespace GamecraftModdingAPI.Blocks { get { - return BlockEngine.GetBlockInfo(Id).tweakableVolume; + return BlockEngine.GetBlockInfo(this, (MusicBlockDataEntityStruct msdes) => msdes.tweakableVolume); } set { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - msdes.tweakableVolume = value; + BlockEngine.SetBlockInfo(this, + (ref MusicBlockDataEntityStruct msdes, float val) => msdes.tweakableVolume = val, value); } } @@ -113,13 +97,15 @@ namespace GamecraftModdingAPI.Blocks { get { - return (ChannelType)BlockEngine.GetBlockInfo(Id).channelType; + Assert.Log("Block exists: " + Exists); + return BlockEngine.GetBlockInfo(this, + (MusicBlockDataEntityStruct msdes) => (ChannelType) msdes.channelType); } - + set { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - msdes.channelType = (byte)value; + BlockEngine.SetBlockInfo(this, + (ref MusicBlockDataEntityStruct msdes, ChannelType val) => msdes.channelType = (byte) val, value); } } @@ -127,30 +113,33 @@ namespace GamecraftModdingAPI.Blocks { get { - return BlockEngine.GetBlockInfo(Id).isPlaying; + return BlockEngine.GetBlockInfo(this, + (MusicBlockDataEntityStruct msdes) => msdes.isPlaying); } set { - ref MusicBlockDataEntityStruct msdes = ref BlockEngine.GetBlockInfo(Id); - if (msdes.isPlaying == value) return; - if (value) + BlockEngine.SetBlockInfo(this, (ref MusicBlockDataEntityStruct msdes, bool val) => { - // start playing - EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get(msdes.trackIndx)); - inst.setVolume(msdes.tweakableVolume / 100f); - inst.start(); - msdes.eventHandle = inst.handle; - } - else - { - // stop playing - EventInstance inst = default(EventInstance); - inst.handle = msdes.eventHandle; - inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); - inst.release(); - } - msdes.isPlaying = value; + if (msdes.isPlaying == val) return; + if (val) + { + // start playing + EventInstance inst = RuntimeManager.CreateInstance(msdes.fmod2DEventPaths.Get(msdes.trackIndx)); + inst.setVolume(msdes.tweakableVolume / 100f); + inst.start(); + msdes.eventHandle = inst.handle; + } + else + { + // stop playing + EventInstance inst = default(EventInstance); + inst.handle = msdes.eventHandle; + inst.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT); + inst.release(); + } + msdes.isPlaying = val; + }, value); } } }