コード例 #1
0
ファイル: HITVM.cs プロジェクト: yuripourre-forks/FreeSO
        public HITSound PlaySoundEvent(string evt)
        {
            if (DISABLE_SOUND)
            {
                return(null);
            }
            evt = evt.ToLowerInvariant();
            if (evt.StartsWith("nc_"))
            {
                NightclubMode = true;
            }
            HITThread InterruptBlocker = null; //the thread we have to wait for to finish before we begin.

            if (ActiveEvents.ContainsKey(evt))
            {
                var aevt = ActiveEvents[evt];
                if (aevt.Dead)
                {
                    ActiveEvents.Remove(evt);            //if the last event is dead, remove and make a new one
                }
                else
                {
                    if ((aevt as HITThread)?.InterruptBlocker != null)
                    {
                        //we can stop this thread - steal its waiter
                        (aevt as HITThread).Dead = true;
                        InterruptBlocker         = (aevt as HITThread).InterruptBlocker;
                    }
                    else if ((aevt as HITThread)?.Interruptable == true)
                    {
                        InterruptBlocker = (aevt as HITThread);
                    }
                    else
                    {
                        return(aevt); //an event of this type is already alive - here, take it.
                    }
                }
            }

            var content = FSO.Content.Content.Get();
            var evts    = content.Audio.Events;

            if (evts != null && evts.ContainsKey(evt))
            {
                var evtent = evts[evt];

                //objects call the wrong event for piano playing
                //there is literally no file or evidence that this is not hard code mapped to PlayPiano in TSO, so it's hardcoded here.
                //the track and HSM associated with the piano_play event, however, are correct. it's just the subroutine that is renamed.
                if (evt.Equals("piano_play", StringComparison.InvariantCultureIgnoreCase))
                {
                    evt = "playpiano";
                    if (ActiveEvents.ContainsKey(evt))
                    {
                        if (ActiveEvents[evt].Dead)
                        {
                            ActiveEvents.Remove(evt);                         //if the last event is dead, remove and make a new one
                        }
                        else
                        {
                            return(ActiveEvents[evt]); //an event of this type is already alive - here, take it.
                        }
                    }
                }

                uint TrackID           = 0;
                uint SubroutinePointer = 0;

                if (content.TS1)
                {
                    TrackID = evtent.TrackID;
                    var track = content.Audio.GetTrack(TrackID, 0, evtent.ResGroup);
                    if (track != null && track.SubroutineID != 0)
                    {
                        SubroutinePointer = track.SubroutineID;
                    }
                }
                else
                {
                    if (evtent.ResGroup.hsm != null)
                    {
                        var c = evtent.ResGroup.hsm.Constants;
                        if (c.ContainsKey(evt))
                        {
                            SubroutinePointer = (uint)c[evt];
                        }
                        var trackIdName = "guid_tkd_" + evt;
                        if (c.ContainsKey(trackIdName))
                        {
                            TrackID = (uint)c[trackIdName];
                        }
                        else
                        {
                            TrackID = evtent.TrackID;
                        }
                    }
                    else
                    { //no hsm, fallback to eent and event track ids (tsov2)
                        var entPoints = evtent.ResGroup.hit.EntryPointByTrackID;
                        TrackID = evtent.TrackID;
                        if (entPoints != null && entPoints.ContainsKey(evtent.TrackID))
                        {
                            SubroutinePointer = entPoints[evtent.TrackID];
                        }
                    }
                }

                if (evtent.EventType == HITEvents.kTurnOnTV)
                {
                    var thread = new HITTVOn(evtent.TrackID, this);
                    thread.VolGroup = HITVolumeGroup.FX;
                    Sounds.Add(thread);
                    ActiveEvents.Add(evt, thread);
                    return(thread);
                }
                else if (evtent.EventType == HITEvents.kSetMusicMode)
                {
                    if (evtent.TrackID == 0)
                    {
                        if (evtent.Name == "bkground_buy1")
                        {
                            evtent.TrackID = 1;
                        }
                        else if (evtent.Name == "bkground_build")
                        {
                            evtent.TrackID = 2;
                        }
                    }
                    var thread = new HITTVOn(evtent.TrackID, this, true);
                    thread.VolGroup = HITVolumeGroup.MUSIC;
                    ActiveEvents.Add(evt, thread);
                    if (NextMusic != null)
                    {
                        NextMusic.Kill();
                    }
                    if (MusicEvent != null)
                    {
                        MusicEvent.Fade();
                    }
                    NextMusic = thread;
                    return(thread);
                }
                else if (SubroutinePointer != 0)
                {
                    var thread = new HITThread(evtent.ResGroup, this);
                    thread.PC          = SubroutinePointer;
                    thread.LoopPointer = (int)thread.PC;
                    if (TrackID != 0)
                    {
                        thread.SetTrack(TrackID, evtent.TrackID);
                    }
                    Sounds.Add(thread);
                    ActiveEvents[evt] = thread;
                    if (InterruptBlocker != null)
                    {
                        InterruptBlocker.Interrupt(thread);
                        if (!InterruptBlocker.Name.StartsWith("nc_"))
                        {
                            InterruptBlocker.KillVocals();
                        }
                    }
                    thread.Name = evt;
                    return(thread);
                }
                else if (TrackID != 0 && content.Audio.GetTrack(TrackID, 0, evtent.ResGroup) != null)
                {
                    var thread = new HITThread(TrackID, this, evtent.ResGroup);
                    Sounds.Add(thread);
                    ActiveEvents[evt] = thread;
                    if (InterruptBlocker != null)
                    {
                        InterruptBlocker.Interrupt(thread);
                        if (!InterruptBlocker.Name.StartsWith("nc_"))
                        {
                            InterruptBlocker.KillVocals();
                        }
                    }
                    thread.Name = evt;
                    return(thread);
                }
            }

            return(null);
        }