Skip to content

Commit

Permalink
Add -ignore, reset checker to 0.25, compile for .25
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanKell committed Oct 10, 2014
1 parent 7f8477a commit d280283
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 64 deletions.
167 changes: 119 additions & 48 deletions Checker.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
/**
* Copyright (fundCfg) 2014, Majiir
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/**
* Copyright (c) 2014, Majiir
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
Expand All @@ -30,45 +31,56 @@
using UnityEngine;

/*-----------------------------------------*\
| SUBSTITUTE YOUR MOD'S NAMESPACE HERE. |
| SUBSTITUTE YOUR MOD'S NAMESPACE HERE. |
\*-----------------------------------------*/
namespace TweakScale
{

/**
* This utility displays destination warning with destination list of mods that determine themselves
* to be incompatible with the current running version of Kerbal Space Program.
*
* See this forum thread for details:
* http://forum.kerbalspaceprogram.com/threads/65395-Voluntarily-Locking-Plugins-to-destination-Particular-KSP-Version
*/

[KSPAddon(KSPAddon.Startup.MainMenu, true)]
* This utility displays a warning with a list of mods that determine themselves
* to be incompatible with the current running version of Kerbal Space Program.
*
* See this forum thread for details:
* http://forum.kerbalspaceprogram.com/threads/65395-Voluntarily-Locking-Plugins-to-a-Particular-KSP-Version
*/

[KSPAddon(KSPAddon.Startup.Instantly, true)]
internal class CompatibilityChecker : MonoBehaviour
{
public static bool IsCompatible()
{
/*-----------------------------------------------*\
| BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
| BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
\*-----------------------------------------------*/

// TODO: Implement your own compatibility check.
//
// If you want to disable some behavior when incompatible, other parts of the plugin
// should query this method:
//
// if (!CompatibilityChecker.IsCompatible()) {
// ...disable some features...
// }
// if (!CompatibilityChecker.IsAllCompatible()) {
// ...disable some features...
// }
//
// Even if you don't lock down functionality, you should return true if your users
// can expect destination future update to be available.
// can expect a future update to be available.
//
return Versioning.version_major == 0 && Versioning.version_minor == 25 && Versioning.Revision == 0;

if (Versioning.version_minor != 24)
{
return false;
}
/*-----------------------------------------------*\
| IMPLEMENTERS SHOULD NOT EDIT BEYOND THIS POINT! |
\*-----------------------------------------------*/
}

public static bool IsUnityCompatible()
{
/*-----------------------------------------------*\
| BEGIN IMPLEMENTATION-SPECIFIC EDITS HERE. |
\*-----------------------------------------------*/

// TODO: Implement your own Unity compatibility check.
//
// Not going to care about the fact that KSP .25 OSX uses a different Unity...
return true;

/*-----------------------------------------------*\
Expand All @@ -77,7 +89,7 @@ public static bool IsCompatible()
}

// Version of the compatibility checker itself.
private static int _version = 2;
private static int _version = 4;

public void Start()
{
Expand Down Expand Up @@ -113,23 +125,82 @@ public void Start()
}
catch (Exception e)
{
// If destination mod throws an exception from IsCompatible, it's not compatible.
// If a mod throws an exception from IsCompatible, it's not compatible.
Debug.LogWarning(String.Format("[CompatibilityChecker] Exception while invoking IsCompatible() from '{0}':\n\n{1}", m.DeclaringType.Assembly.GetName().Name, e));
return true;
}
})
.Select(m => m.DeclaringType.Assembly.GetName().Name)
.ToArray();

// A mod is incompatible with Unity if its compatibility checker has an IsUnityCompatible method which returns false.
String[] incompatibleUnity =
fields
.Select(f => f.DeclaringType.GetMethod("IsUnityCompatible", Type.EmptyTypes))
.Where(m => m != null) // Mods without IsUnityCompatible() are assumed to be compatible.
.Where(m => m.IsStatic)
.Where(m => m.ReturnType == typeof(bool))
.Where(m =>
{
try
{
return !(bool)m.Invoke(null, new object[0]);
}
catch (Exception e)
{
// If a mod throws an exception from IsUnityCompatible, it's not compatible.
Debug.LogWarning(String.Format("[CompatibilityChecker] Exception while invoking IsUnityCompatible() from '{0}':\n\n{1}", m.DeclaringType.Assembly.GetName().Name, e));
return true;
}
})
.Select(m => m.DeclaringType.Assembly.GetName().Name)
.ToArray();

Array.Sort(incompatible);
Array.Sort(incompatibleUnity);

String message = String.Empty;

if (IsWin64())
{
message += "WARNING: You are using 64-bit KSP on Windows. This version of KSP is known to cause crashes. It's highly recommended that you use either 32-bit KSP on Windows or switch to Linux.";
}

if ((incompatible.Length > 0) || (incompatibleUnity.Length > 0))
{
message += ((message == String.Empty) ? "Some" : "\n\nAdditionally, some") + " installed mods may be incompatible with this version of Kerbal Space Program. Features may be broken or disabled. Please check for updates to the listed mods.";

if (incompatible.Length > 0)
{
Debug.LogWarning("[CompatibilityChecker] Incompatible mods detected: " + String.Join(", ", incompatible));
message += String.Format("\n\nThese mods are incompatible with KSP {0}.{1}.{2}:\n\n", Versioning.version_major, Versioning.version_minor, Versioning.Revision);
message += String.Join("\n", incompatible);
}

if (incompatibleUnity.Length > 0)
{
Debug.LogWarning("[CompatibilityChecker] Incompatible mods (Unity) detected: " + String.Join(", ", incompatibleUnity));
message += String.Format("\n\nThese mods are incompatible with Unity {0}:\n\n", Application.unityVersion);
message += String.Join("\n", incompatibleUnity);
}
}

if (incompatible.Length > 0)
if ((incompatible.Length > 0) || (incompatibleUnity.Length > 0) || IsWin64())
{
Debug.LogWarning("[CompatibilityChecker] Incompatible mods detected: " + String.Join(", ", incompatible));
PopupDialog.SpawnPopupDialog("Incompatible Mods Detected", "Some installed mods are incompatible with this version of Kerbal Space Program. Some features may be broken or disabled. Please check for updates to the following mods:\n\n" + String.Join("\n", incompatible), "OK", false, HighLogic.Skin);
PopupDialog.SpawnPopupDialog("Incompatible Mods Detected", message, "OK", true, HighLogic.Skin);
}
}

public static bool IsWin64()
{
return (IntPtr.Size == 8) && (Environment.OSVersion.Platform == PlatformID.Win32NT);
}

public static bool IsAllCompatible()
{
return IsCompatible() && IsUnityCompatible() && !IsWin64();
}

private static IEnumerable<Type> getAllTypes()
{
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
Expand All @@ -151,4 +222,4 @@ private static IEnumerable<Type> getAllTypes()
}
}
}
}
}
1 change: 1 addition & 0 deletions Gamedata/TweakScale/ScaleExponents.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ TWEAKSCALEEXPONENTS
{
!amount = 3
maxAmount = 3
-ignore = ModuleFuelTanks
}

attachNodes
Expand Down
Binary file modified Gamedata/TweakScale/plugins/KSPAPIExtensions.dll
Binary file not shown.
Binary file modified Gamedata/TweakScale/plugins/Scale.dll
Binary file not shown.
Binary file added Gamedata/TweakScale/plugins/Scale_Editor.dll
Binary file not shown.
Binary file modified Gamedata/TweakScale/plugins/Scale_Redist.dll
Binary file not shown.
59 changes: 43 additions & 16 deletions ScaleExponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class ScaleExponents
private string _id;
private string _name;
private Dictionary<string, Tuple<string, bool>> _exponents;
private List<string> _ignores;
private Dictionary<string, ScaleExponents> _children;

private static Dictionary<string, ScaleExponents> globalList;
Expand Down Expand Up @@ -66,6 +67,7 @@ private ScaleExponents(ScaleExponents source)
{
_exponents = new Dictionary<string, Tuple<string, bool>>();
_children = new Dictionary<string, ScaleExponents>();
_ignores = new List<string>();
}
else
{
Expand All @@ -74,6 +76,7 @@ private ScaleExponents(ScaleExponents source)
._children
.Select(a => new KeyValuePair<string, ScaleExponents>(a.Key, a.Value.Clone()))
.ToDictionary(a => a.Key, a => a.Value);
_ignores = new List<string>(source._ignores);
}
}

Expand All @@ -97,6 +100,7 @@ private ScaleExponents(ConfigNode node, ScaleExponents source = null)

_exponents = new Dictionary<string, Tuple<string, bool>>();
_children = new Dictionary<string, ScaleExponents>();
_ignores = new List<string>();

foreach (var value in node.values.OfType<ConfigNode.Value>().Where(a=>a.name != "name"))
{
Expand All @@ -106,7 +110,10 @@ private ScaleExponents(ConfigNode node, ScaleExponents source = null)
}
else
{
_exponents[value.name] = Tuple.Create(value.value, false);
if(value.name.Equals("-ignore"))
_ignores.Add(value.value);
else
_exponents[value.name] = Tuple.Create(value.value, false);
}
}

Expand Down Expand Up @@ -140,6 +147,13 @@ public static ScaleExponents Merge(ScaleExponents destination, ScaleExponents so
destination._exponents[value.Key] = value.Value;
}
}
foreach (string value in source._ignores)
{
if (!destination._ignores.Contains(value))
{
destination._ignores.Add(value);
}
}
foreach (var child in source._children)
{
if (destination._children.ContainsKey(child.Key))
Expand Down Expand Up @@ -222,7 +236,8 @@ public double Rescale(double currentValue, double baseValue, string name, Scalin
/// <param name="obj">The object to rescale.</param>
/// <param name="baseObj">The corresponding object in the prefab.</param>
/// <param name="factor">The new scale.</param>
public void UpdateFields(object obj, object baseObj, ScalingFactor factor)
/// <param name="part">The part the object is on.</param>
public void UpdateFields(object obj, object baseObj, ScalingFactor factor, Part part = null)
{
if (obj is PartModule && obj.GetType().Name != _id)
{
Expand All @@ -232,7 +247,7 @@ public void UpdateFields(object obj, object baseObj, ScalingFactor factor)

if (obj is IEnumerable)
{
UpdateEnumerable((IEnumerable)obj, (IEnumerable)baseObj, factor);
UpdateEnumerable((IEnumerable)obj, (IEnumerable)baseObj, factor, part);
return;
}

Expand All @@ -248,15 +263,26 @@ public void UpdateFields(object obj, object baseObj, ScalingFactor factor)
{
continue;
}
if (baseObjTmp == null)
{
// No prefab from which to grab values. Use relative scaling.
value.Value = Rescale(value.Value, value.Value, fieldName, factor, relative: true);
}
else
bool doScale = true;
if ((object)part != null)
foreach (string v in _ignores)
if (part.Modules.Contains(v))
{
doScale = false;
break;
}
if (doScale)
{
var baseValue = FieldChanger<double>.CreateFromName(baseObj, fieldName);
value.Value = Rescale(value.Value, baseValue.Value, fieldName, factor);
if (baseObjTmp == null)
{
// No prefab from which to grab values. Use relative scaling.
value.Value = Rescale(value.Value, value.Value, fieldName, factor, relative: true);
}
else
{
var baseValue = FieldChanger<double>.CreateFromName(baseObj, fieldName);
value.Value = Rescale(value.Value, baseValue.Value, fieldName, factor);
}
}
}

Expand All @@ -267,7 +293,7 @@ public void UpdateFields(object obj, object baseObj, ScalingFactor factor)
if (childObjField != null)
{
var baseChildObjField = FieldChanger<object>.CreateFromName(baseObj, childName);
_child.Value.UpdateFields(childObjField.Value, baseChildObjField.Value, factor);
_child.Value.UpdateFields(childObjField.Value, baseChildObjField.Value, factor, part);
}
}
}
Expand All @@ -278,7 +304,8 @@ public void UpdateFields(object obj, object baseObj, ScalingFactor factor)
/// <param name="obj">The list whose items we want to update.</param>
/// <param name="baseObj">The corresponding list in the prefab.</param>
/// <param name="factor">The scaling factor.</param>
private void UpdateEnumerable(IEnumerable obj, IEnumerable baseObj, ScalingFactor factor)
/// <param name="part">The part the object is on.</param>
private void UpdateEnumerable(IEnumerable obj, IEnumerable baseObj, ScalingFactor factor, Part part = null)
{
IEnumerable other = baseObj;
if (baseObj == null || obj.StupidCount() != baseObj.StupidCount())
Expand All @@ -299,22 +326,22 @@ private void UpdateEnumerable(IEnumerable obj, IEnumerable baseObj, ScalingFacto
}
}
}
UpdateFields(item.Item1, item.Item2, factor);
UpdateFields(item.Item1, item.Item2, factor, part);
}
}

public static void UpdateObject(Part part, Part basePart, Dictionary<string, ScaleExponents> exps, ScalingFactor factor)
{
if (exps.ContainsKey("Part"))
{
exps["Part"].UpdateFields(part, basePart, factor);
exps["Part"].UpdateFields(part, basePart, factor, part);
}

var modulesAndExponents = part.Modules.Cast<PartModule>().Zip(basePart.Modules.Cast<PartModule>()).Join(exps, modules => modules.Item1.moduleName, exponents => exponents.Key, (modules, exponent) => Tuple.Create(modules, exponent.Value)).ToArray();

foreach (var modExp in modulesAndExponents)
{
modExp.Item2.UpdateFields(modExp.Item1.Item1, modExp.Item1.Item2, factor);
modExp.Item2.UpdateFields(modExp.Item1.Item1, modExp.Item1.Item2, factor, part);
}
}

Expand Down

0 comments on commit d280283

Please sign in to comment.