Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Biotronic committed Nov 17, 2014
1 parent 2d7d32e commit 0a5a30c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
22 changes: 11 additions & 11 deletions ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ public static IEnumerable<T> Repeat<T>(this T a)
/// <summary>
/// Creates destination copy of destination dictionary.
/// </summary>
/// <typeparam name="K">The key type.</typeparam>
/// <typeparam name="V">The exponentValue type.</typeparam>
/// <typeparam name="TK">The key type.</typeparam>
/// <typeparam name="TV">The exponentValue type.</typeparam>
/// <param name="source">The dictionary to copy.</param>
/// <returns>A copy of <paramref name="source"/>.</returns>
public static Dictionary<TK, TV> Clone<TK, TV>(this Dictionary<TK, TV> source)
Expand Down Expand Up @@ -190,18 +190,18 @@ public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source)
public static class ConvertEx
{
/// <summary>
/// Returns an object of tyep <typeparamref name="T"/> and whose exponentValue is equivalent to <paramref name="value"/>.
/// Returns an object of tyep <typeparamref name="TTo"/> and whose exponentValue is equivalent to <paramref name="value"/>.
/// </summary>
/// <typeparam name="T">The type of object to return.</typeparam>
/// <typeparam name="TU">The type of the object to convert.</typeparam>
/// <typeparam name="TTo">The type of object to return.</typeparam>
/// <typeparam name="TFrom">The type of the object to convert.</typeparam>
/// <param name="value">The object to convert.</param>
/// <returns>An object whose type is <typeparamref name="T"/> and whose exponentValue is equivalent to <paramref name="value"/>. -or- A null reference (Nothing in Visual Basic), if <paramref name="value"/> is null and <typeparamref name="T"/> is not destination exponentValue type.</returns>
/// <exception cref="System.InvalidCastException">This conversion is not supported. -or-<paramref name="value"/> is null and <typeparamref name="T"/> is destination exponentValue type.</exception>
/// <exception cref="System.FormatException"><paramref name="value"/> is not in destination format recognized by <typeparamref name="T"/>.</exception>
/// <exception cref="System.OverflowException"><paramref name="value"/> represents destination number that is out of the range of <typeparamref name="T"/>.</exception>
public static T ChangeType<T, TU>(TU value) where TU : IConvertible
/// <returns>An object whose type is <typeparamref name="TTo"/> and whose exponentValue is equivalent to <paramref name="value"/>. -or- A null reference (Nothing in Visual Basic), if <paramref name="value"/> is null and <typeparamref name="TTo"/> is not destination exponentValue type.</returns>
/// <exception cref="System.InvalidCastException">This conversion is not supported. -or-<paramref name="value"/> is null and <typeparamref name="TTo"/> is destination exponentValue type.</exception>
/// <exception cref="System.FormatException"><paramref name="value"/> is not in destination format recognized by <typeparamref name="TTo"/>.</exception>
/// <exception cref="System.OverflowException"><paramref name="value"/> represents destination number that is out of the range of <typeparamref name="TTo"/>.</exception>
public static TTo ChangeType<TTo, TFrom>(TFrom value) where TFrom : IConvertible
{
return (T)Convert.ChangeType(value, typeof(T));
return (TTo)Convert.ChangeType(value, typeof(TTo));
}

/// <summary>
Expand Down
32 changes: 14 additions & 18 deletions Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TweakScaleRegister : RescalableRegistratorAddon
{
override public void OnStart()
{
var genericRescalable = Tools.getAllTypes()
var genericRescalable = Tools.GetAllTypes()
.Where(IsGenericRescalable)
.ToArray();

Expand All @@ -54,7 +54,7 @@ private static void RegisterGenericRescalable(Type resc, Type arg)
var c = resc.GetConstructor(new[] { arg });
if (c == null)
return;
Func<PartModule, IRescalable> creator = pm => (IRescalable)c.Invoke(new[] { pm });
Func<PartModule, IRescalable> creator = pm => (IRescalable)c.Invoke(new object[] { pm });

TweakScaleUpdater.RegisterUpdater(arg, creator);
}
Expand All @@ -70,7 +70,7 @@ private static bool IsGenericRescalable(Type t)
static class TweakScaleUpdater
{
// Every kind of updater is registered here, and the correct kind of updater is created for each PartModule.
static readonly Dictionary<Type, Func<PartModule, IRescalable>> ctors = new Dictionary<Type, Func<PartModule, IRescalable>>();
static readonly Dictionary<Type, Func<PartModule, IRescalable>> Ctors = new Dictionary<Type, Func<PartModule, IRescalable>>();

/// <summary>
/// Registers an updater for partmodules of type <paramref name="pm"/>.
Expand All @@ -79,35 +79,31 @@ static class TweakScaleUpdater
/// <param name="creator">A function that creates an updater for this PartModule type.</param>
static public void RegisterUpdater(Type pm, Func<PartModule, IRescalable> creator)
{
ctors[pm] = creator;
Ctors[pm] = creator;
}

// Creates an updater for each modules attached to destination part.
public static IEnumerable<IRescalable> CreateUpdaters(Part part)
{
foreach (var mod in part.Modules.Cast<PartModule>())
var myUpdaters = part
.Modules.Cast<PartModule>()
.Select(CreateUpdater)
.Where(updater => updater != null);
foreach (var updater in myUpdaters)
{
var updater = createUpdater(mod);
if (updater != null)
{
yield return updater;
}
yield return updater;
}
yield return new TSGenericUpdater(part);
yield return new EmitterUpdater(part);
}

private static IRescalable createUpdater(PartModule module)
private static IRescalable CreateUpdater(PartModule module)
{
if (module is IRescalable)
{
return module as IRescalable;
}
if (ctors.ContainsKey(module.GetType()))
{
return ctors[module.GetType()](module);
}
return null;
return Ctors.ContainsKey(module.GetType()) ? Ctors[module.GetType()](module) : null;
}
}

Expand All @@ -130,7 +126,7 @@ public TSGenericUpdater(Part part)

public void OnRescale(ScalingFactor factor)
{
ScaleExponents.UpdateObject(_part, _basePart, _ts.Config.exponents, factor);
ScaleExponents.UpdateObject(_part, _basePart, _ts.Config.Exponents, factor);
}
}

Expand Down Expand Up @@ -184,7 +180,7 @@ private void UpdateParticleEmitter(KSPParticleEmitter pe)
{
return;
}
var factor = _ts.scalingFactor;
var factor = _ts.ScalingFactor;

if (!_scales.ContainsKey(pe))
{
Expand Down

0 comments on commit 0a5a30c

Please sign in to comment.