Skip to content

Commit

Permalink
version 1.3.0, ToolbarControl support
Browse files Browse the repository at this point in the history
  • Loading branch information
radistmorse committed Jan 24, 2018
1 parent 75fe327 commit 9beec16
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
61 changes: 47 additions & 14 deletions AdjustableModPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*/

using System;
using System.Linq;
using UnityEngine;
using KSP.UI.Screens;
using System.Collections.Generic;
Expand Down Expand Up @@ -67,6 +68,52 @@ private class ModDescriptor {

static internal AdjustableModPanel Instance { get; private set; } = null;

#region ToolbarControl

private bool ToolbarControllerAvailable = true;
private Type ToolbarControllerType = null;
private System.Reflection.MethodInfo ToolbarControllerMethod = null;

public bool IsButtonToolbarController (ApplicationLauncherButton button, out string name, out string id) {
if (!ToolbarControllerAvailable) {
name = null;
id = null;
return false;
}
if (ToolbarControllerType == null) {
ToolbarControllerType = AssemblyLoader.loadedAssemblies
.Select (a => a.assembly.GetExportedTypes ())
.SelectMany (t => t)
.FirstOrDefault (t => t.FullName == "ToolbarControl_NS.ToolbarControl");
if (ToolbarControllerType == null) {
Debug.Log ("[Adjustable Mod Panel] INFO: ToolbarControl mod not found");
ToolbarControllerAvailable = false;
name = null;
id = null;
return false;
}
}
if (ToolbarControllerMethod == null) {
ToolbarControllerMethod = ToolbarControllerType.GetMethod ("IsStockButtonManaged",
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static);
if (ToolbarControllerMethod == null) {
Debug.Log ("[Adjustable Mod Panel] INFO: ToolbarControl mod found, but the method is absent");
ToolbarControllerAvailable = false;
name = null;
id = null;
return false;
}
Debug.Log ("[Adjustable Mod Panel] INFO: Found ToolbarControl mod, will check mods from now on");
}
System.Object[] parameters = new System.Object[]{button, null, null};
bool rez = (bool)ToolbarControllerMethod.Invoke (null, parameters);
name = (string)parameters[1];
id = (string)parameters[2];
return rez;
}

#endregion

#region Main window

private class ToggleParameters : MonoBehaviour {
Expand Down Expand Up @@ -582,20 +629,6 @@ internal void RecordMod (string module, string method, ApplicationLauncher.AppSc
requiredScenes = alwaysOn,
modIcon = texture
});
/*if (!modList.ContainsKey (descriptor)) {
Debug.Log ("[Adjustable Mod Panel] Recorded a new mod: " + descriptor.Split ('+')[0]);
modList[descriptor] = new KeyValuePair<ApplicationLauncher.AppScenes, Texture2D> (visibleInScenes, texture);
} else {
// update the value in case the visibility was expanded by the mod
modList[descriptor] = new KeyValuePair<ApplicationLauncher.AppScenes, Texture2D> ((visibleInScenes | modList[descriptor].Key), texture);
}
if (!modMatrix.ContainsKey (descriptor))
modMatrix[descriptor] = visibleInScenes;
if (!modMatrixAlwaysOn.ContainsKey (descriptor)) {
if (alwaysOn != ApplicationLauncher.AppScenes.NEVER)
Debug.Log ("[Adjustable Mod Panel] Mod " + descriptor.Split('+')[0] + " requests to be unswitchable in scenes: " + alwaysOn);
modMatrixAlwaysOn[descriptor] = alwaysOn;
}*/
}

internal void EndRecordingMods () {
Expand Down
2 changes: 1 addition & 1 deletion AdjustableModPanel.version
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"VERSION": {
"MAJOR": 1,
"MINOR": 2,
"MINOR": 3,
"PATCH": 0,
"BUILD": 0
},
Expand Down
13 changes: 13 additions & 0 deletions ModPanelComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public void Update () {
if (module.EndsWith (".dll"))
module = module.Substring (0, module.Length - 4);

if (AdjustableModPanel.Instance.IsButtonToolbarController (button, out string name, out string id)) {
// button created by ToolbarControl needs special treatment
module = name;
method = id;
}

uint textureHash = 0;
unchecked {
foreach (var byt in DuplicateTexture (texture).GetRawTextureData ()) {
Expand Down Expand Up @@ -121,6 +127,13 @@ public void Update () {
var module = func.Method.Module.Name;
if (module.EndsWith (".dll"))
module = module.Substring (0, module.Length - 4);

if (AdjustableModPanel.Instance.IsButtonToolbarController (button, out string name, out string id)) {
// button created by ToolbarControl needs special treatment
module = name;
method = id;
}

button.VisibleInScenes = AdjustableModPanel.Instance.GetModScenes (module, method, button.VisibleInScenes, cashes[button]);
// normally, this should be enough. But for some reason some mode buttons remain active,
//even when they should not. So we change the button state explicitly.
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]

0 comments on commit 9beec16

Please sign in to comment.