-
Notifications
You must be signed in to change notification settings - Fork 15
perf: Optimize RocketSoundEnhancement.LateUpdate by patching AudioSource ctor
#42
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| using HarmonyLib; | ||
| using UnityEngine; | ||
| using System; | ||
|
|
||
| namespace RocketSoundEnhancement.Patches | ||
| { | ||
| [HarmonyPatch(typeof(AudioSource), MethodType.Constructor, argumentTypes: new Type[0])] | ||
| internal static class AudioSource_Ctor_Patch | ||
| { | ||
| static void Postfix(AudioSource __instance) | ||
| { | ||
| RocketSoundEnhancement.Instance?.newAudioSources?.Add(__instance); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,13 +58,16 @@ public static AudioMixer Mixer | |
| private float lastInteriorCutoffFreq; | ||
|
|
||
| private HashSet<int> managedSources = new HashSet<int>(); | ||
| internal readonly List<AudioSource> newAudioSources = new List<AudioSource>(); | ||
|
|
||
| private bool gamePaused; | ||
|
|
||
| private void Awake() | ||
| { | ||
| if (instance != null) { Destroy(instance); } | ||
|
|
||
| // Get all the currently existing audio sources. | ||
| newAudioSources.AddRange((AudioSource[])FindObjectsOfType(typeof(AudioSource))); | ||
| instance = this; | ||
| } | ||
|
|
||
|
|
@@ -162,27 +165,42 @@ public float WindModulation() | |
| return Mathf.Lerp(1, windModulation, Mathf.Clamp01((float)FlightGlobals.ActiveVessel.atmDensity)); | ||
| } | ||
|
|
||
| bool GetNextAudioSource(out AudioSource source) | ||
| { | ||
| if (newAudioSources.Count == 0) | ||
| { | ||
| source = null; | ||
| return false; | ||
| } | ||
|
|
||
| // This makes sure things still work even if a new audio source ends | ||
| // up being created while we are running. | ||
| source = newAudioSources[newAudioSources.Count - 1]; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe should check if the source is destroyed here and only return non-destroyed ones? Oh never mind I thought you might be spreading them out over several frames. |
||
| newAudioSources.RemoveAt(newAudioSources.Count - 1); | ||
| return true; | ||
| } | ||
|
|
||
| public void LateUpdate() | ||
| { | ||
| if (gamePaused || !HighLogic.LoadedSceneIsFlight) return; | ||
|
|
||
| if (!Settings.EnableAudioEffects || Mixer == null) | ||
| { | ||
| // TODO: some kind of latch so this only runs once | ||
| foreach (var sourceObj in FindObjectsOfType(typeof(AudioSource))) | ||
| while (GetNextAudioSource(out var source)) | ||
| { | ||
| AudioSource source = (AudioSource)sourceObj; | ||
| if (source != null) source.outputAudioMixerGroup = null; | ||
| if (source != null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a faster null check, see KSPCF |
||
| source.outputAudioMixerGroup = null; | ||
| } | ||
|
|
||
| managedSources.Clear(); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| // NOTE: the generic (strongly typed) version of FindObjectsOfType is slower! | ||
| foreach (var sourceObj in FindObjectsOfType(typeof(AudioSource))) | ||
| while (GetNextAudioSource(out var source)) | ||
| { | ||
| AudioSource source = (AudioSource)sourceObj; | ||
| if (source == null) | ||
| continue; | ||
|
|
||
| int instanceID = source.GetInstanceID(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be use-ckan: true and set the properties appropriately in the csproj