JD2022-TU1/main/tools/apps/ConductorDebugger/Source/Plugin/ConductorDebuggerPlugin.cs

112 lines
No EOL
3.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using ConductorDebugger.Source.Brick;
using TeaBox;
using TeaBox.Collections;
using TeaBox.ObjectModel;
using ConductorDebugger.Model;
using ConductorDebugger.View;
using JD.Application.Plugin;
using JD.Application.Plugin.World;
using JD.Application.View.Actions;
using JD.Interfaces.Remoting;
using JD.Remoting.Gui;
using TeaBox.Actions;
using JD.Remoting;
namespace ConductorDebugger.Plugin
{
/// <summary>
/// Responsible of communication with the engine.
/// </summary>
public class ConductorDebuggerPlugin : BasePlugin
{
public static readonly ConductorDebuggerPlugin Instance = new ConductorDebuggerPlugin();
private ConductorDocumentView _currentView;
private readonly EngineManager _engineManager = null;
private readonly Conductor _player;
private readonly World _world;
private readonly BrickCollection _bricks = new BrickCollection();
public ConductorDebuggerPlugin()
: base("ConductorDebuggerPlugin")
{
_player = new Conductor(this);
_player.UseAbsoluteTime = true;
_world = new World(this);
_world.WaveChanged += world_WaveChanged;
_engineManager = new EngineManager(this);
EngineConnectActionHandler connectAction = new EngineConnectActionHandler();
connectAction.EngineManager = _engineManager;
((SimpleAction)Application.Main.Actions["ConductorDebugger.EngineConnect"]).DefaultHandler = connectAction;
RegisterHandler(CommandNames.SendConductor, HandleSendConductor);
}
protected override void OnConnectedChanged(IPlugin e)
{
base.OnConnectedChanged(e);
if (IsConnected)
{
SendGetWave();
}
}
/// <summary>
/// Open the new trk, close the old one when the current wave in the engine changes.
/// </summary>
void world_WaveChanged(object sender, System.EventArgs e)
{
if (_world.CurrentWave != null && _currentView == null)
{
using (var documentHandle = Application.Main.Open.FromFileAndReturn(_world.CurrentWave.FileName, DocumentOpeningOptions.Background))
{
_currentView = new ConductorDocumentView((ConductorDocument)documentHandle.Document, _world.CurrentWave, _player, _bricks);
Application.Main.Views.Add(_currentView);
}
}
}
#region Command Handle
private void HandleSendConductor(IBlob blob)
{
var bricks = ParseBricks(blob).ToArray();
if (!_bricks.SequenceEqual(bricks))
{
using (_bricks.BeginScopedUpdate())
{
_bricks.Clear();
_bricks.AddRange(bricks);
}
}
}
private static IEnumerable<BrickModel> ParseBricks(IBlob blob)
{
uint nbMusicScore = blob.ExtractUInt();
for (uint i = 0; i < nbMusicScore; i++)
{
//Extract MusicScore
uint musicScoreType = blob.ExtractUInt();
uint nbBlocks = blob.ExtractUInt();
for (uint j = 0; j < nbBlocks; j++)
{
//Extract Block
uint blockId = blob.ExtractUInt();
uint nbBricks = blob.ExtractUInt();
for (uint k = 0; k < nbBricks; k++)
{
BrickModel brick = new BrickModel(blob);
yield return brick;
}
}
}
}
#endregion
}
}