-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from blowfishpro/TechRestrictions
Tech restrictions
- Loading branch information
Showing
13 changed files
with
372 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace B9PartSwitch | ||
{ | ||
public class BestSubtypeDeterminator | ||
{ | ||
public PartSubtype FindBestSubtype(IEnumerable<PartSubtype> subtypes, IEnumerable<string> resourceNamesOnPart) | ||
{ | ||
// No managed resources, so no way to guess subtype | ||
if (!subtypes.Any(s => s.HasTank)) return subtypes.MaxBy(subtype => subtype.defaultSubtypePriority); | ||
|
||
// Now use resources | ||
// This finds all the managed resources that currently exist on teh part | ||
IEnumerable<string> managedResourceNames = subtypes.SelectMany(s => s.ResourceNames).Distinct(); | ||
string[] managedResourcesOnPart = managedResourceNames.Intersect(resourceNamesOnPart).ToArray(); | ||
|
||
// If any of the part's current resources are managed, look for a subtype which has all of the managed resources (and all of its resources exist) | ||
// Otherwise, look for a structural subtype (no resources) | ||
if (managedResourcesOnPart.Any()) | ||
{ | ||
PartSubtype bestSubtype = null; | ||
|
||
foreach (PartSubtype subtype in subtypes) | ||
{ | ||
if (!subtype.HasTank) continue; | ||
if (!subtype.ResourceNames.SameElementsAs(managedResourcesOnPart)) continue; | ||
if (bestSubtype.IsNotNull() && subtype.defaultSubtypePriority <= bestSubtype.defaultSubtypePriority) continue; | ||
bestSubtype = subtype; | ||
} | ||
|
||
if (bestSubtype.IsNotNull()) return bestSubtype; | ||
} | ||
else | ||
{ | ||
PartSubtype bestSubtype = null; | ||
|
||
foreach (PartSubtype subtype in subtypes) | ||
{ | ||
if (subtype.HasTank) continue; | ||
if (bestSubtype.IsNotNull() && subtype.defaultSubtypePriority <= bestSubtype.defaultSubtypePriority) continue; | ||
bestSubtype = subtype; | ||
} | ||
|
||
if (bestSubtype.IsNotNull()) return bestSubtype; | ||
} | ||
|
||
// No useful way to determine correct subtype, just pick first | ||
return subtypes.MaxBy(subtype => subtype.defaultSubtypePriority); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
|
||
namespace B9PartSwitch | ||
{ | ||
public class LockedSubtypeWarningHandler | ||
{ | ||
private const int MAX_MESSAGE_COUNT = 10; | ||
private static PopupDialog dialog; | ||
private static List<string> allMessages = new List<string>(); | ||
|
||
public static void WarnSubtypeLocked(string message) | ||
{ | ||
try | ||
{ | ||
UpsertDialog(message); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.LogError("Exception while trying to create locked subtype warning dialog"); | ||
Debug.LogException(ex); | ||
Application.Quit(); | ||
} | ||
} | ||
|
||
private static void UpsertDialog(string message) | ||
{ | ||
if (string.IsNullOrEmpty(message) || allMessages.Contains(message)) return; | ||
|
||
if (allMessages.Count < MAX_MESSAGE_COUNT) | ||
{ | ||
allMessages.Add(message); | ||
} | ||
else if (allMessages.Count == MAX_MESSAGE_COUNT) | ||
{ | ||
Debug.LogError("[LockedSubtypeWarningHandler] Not displaying locked subtype warning because too many warnings have already been added:"); | ||
Debug.LogError(message); | ||
allMessages.Add("(too many locked subtype messages to display)"); | ||
} | ||
else | ||
{ | ||
Debug.LogError("[LockedSubtypeWarningHandler] Not displaying locked subtype warning because too many warnings have already been added:"); | ||
Debug.LogError(message); | ||
return; | ||
} | ||
|
||
if (dialog != null) dialog.Dismiss(); | ||
|
||
dialog = PopupDialog.SpawnPopupDialog(new Vector2(0.5f, 0.5f), | ||
new Vector2(0.5f, 0.5f), | ||
new MultiOptionDialog( | ||
"B9PartSwitchLockedSubtypeWarning", | ||
$"Some parts have locked subtypes selected. They have been replaced with the highest priority unlocked subtype\n\n{string.Join("\n\n", allMessages.ToArray())}", | ||
"B9PartSwitch - Locked Subtypes", | ||
HighLogic.UISkin, | ||
new Rect(0.5f, 0.5f, 500f, 60f), | ||
new DialogGUIFlexibleSpace(), | ||
new DialogGUIHorizontalLayout( | ||
new DialogGUIFlexibleSpace(), | ||
new DialogGUIButton("OK", delegate () { }, 140.0f, 30.0f, true), | ||
new DialogGUIFlexibleSpace() | ||
) | ||
), | ||
true, | ||
HighLogic.UISkin); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.