Skip to content

Commit

Permalink
add 'overwrite' option for new mod sets
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorVanGogh committed Sep 30, 2017
1 parent 9c36c77 commit f80e573
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 34 deletions.
Binary file modified Assemblies/ModSwitch.dll
Binary file not shown.
Empty file added CHANGELOG.md
Empty file.
6 changes: 6 additions & 0 deletions Languages/English/Keyed/Keyed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<ModSwitch.Tip.Undo>Undo</ModSwitch.Tip.Undo>
<ModSwitch.Tip.Apply>Apply mod set</ModSwitch.Tip.Apply>

<ModSwitch.CreateNew>Create new</ModSwitch.CreateNew>
<ModSwitch.OverwritExisting>Overwrite existing...</ModSwitch.OverwritExisting>
<ModSwitch.OverwritExisting.Confirm><![CDATA[Really overwrite {0}?
<i>(You can suppress this dialog by holding SHIFT when you choose this option.)</i>]]></ModSwitch.OverwritExisting.Confirm>

<ModSwitch.Import>Import</ModSwitch.Import>

<ModSwitch.ModSet.Mods>{0} items</ModSwitch.ModSet.Mods>
Expand Down
12 changes: 12 additions & 0 deletions Source/ModSwitch/LanguageKeys.Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public static class @keyed {
/// </summary>
public const string @ModSwitch_Tip_Apply = "ModSwitch.Tip.Apply";
/// <summary>
/// Get's the translation key for '<em>Create new</em>'
/// </summary>
public const string @ModSwitch_CreateNew = "ModSwitch.CreateNew";
/// <summary>
/// Get's the translation key for '<em>Overwrite existing...</em>'
/// </summary>
public const string @ModSwitch_OverwritExisting = "ModSwitch.OverwritExisting";
/// <summary>
/// Get's the translation key for '<em>Really overwrite {0}?<br /><br />&lt;i&gt;(You can suppress this dialog by holding SHIFT when you choose this option.)&lt;/i&gt;</em>'
/// </summary>
public const string @ModSwitch_OverwritExisting_Confirm = "ModSwitch.OverwritExisting.Confirm";
/// <summary>
/// Get's the translation key for '<em>Import</em>'
/// </summary>
public const string @ModSwitch_Import = "ModSwitch.Import";
Expand Down
14 changes: 8 additions & 6 deletions Source/ModSwitch/Model/ModSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ class ModSet : IExposable {
public string Name = String.Empty;

static ModSet() {
var tModsConfig = typeof(ModsConfig);
var tModsConfig = typeof(Verse.ModsConfig);
var tModsConfigData = AccessTools.Inner(tModsConfig, @"ModsConfigData");
fiModsConfigData_activeMods = AccessTools.Field(tModsConfigData, @"activeMods");
fiModsConfigData_buildNumber = AccessTools.Field(tModsConfigData, @"buildNumber");
fiModsConfig_data = AccessTools.Field(tModsConfig, @"data");

rgxSteamModId = new Regex(@"^\d+$", RegexOptions.Singleline | RegexOptions.Compiled);

Util.Log($"ModSet cctor: {tModsConfig != null}, {tModsConfigData != null}, {fiModsConfigData_activeMods != null}, {fiModsConfigData_buildNumber != null}, {fiModsConfig_data != null}");
}

public ModSet(Settings owner) {
Expand Down Expand Up @@ -179,14 +181,14 @@ public void Delete() {
}


public static ModSet FromCurrent(string name, Settings owner) {
public static ModSet FromCurrent(string name, Settings owner) {
object modsConfigData = fiModsConfig_data.GetValue(null);

return new ModSet(owner) {
Name = name,
BuildNumber = (int) fiModsConfigData_buildNumber.GetValue(modsConfigData),
Mods = new List<string>((IEnumerable<string>) fiModsConfigData_activeMods.GetValue(modsConfigData))
};
Name = name,
BuildNumber = (int)fiModsConfigData_buildNumber.GetValue(modsConfigData),
Mods = new List<string>((IEnumerable<string>)fiModsConfigData_activeMods.GetValue(modsConfigData))
};
}
}
}
2 changes: 1 addition & 1 deletion Source/ModSwitch/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
[assembly: System.Reflection.AssemblyCopyright("Copyright © DoctorVanGogh 2017")]
[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assembly: System.Runtime.InteropServices.Guid("ab2e8e89-3d1d-4e36-a0ae-489b6ebc84a6")]
[assembly: System.Reflection.AssemblyVersion("1.2.0.66")]
[assembly: System.Reflection.AssemblyVersion("1.2.0.81")]


2 changes: 1 addition & 1 deletion Source/ModSwitch/Properties/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.0.66
1.2.0.81
58 changes: 48 additions & 10 deletions Source/ModSwitch/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,16 +203,49 @@ public void DoModsConfigWindowContents(Rect target) {
ms.Apply();
})).ToList()));
var rctNew = new Rect(target.x + 30f + 8f, target.y, 30f, 30f);
if (ExtraWidgets.ButtonImage(rctNew, Assets.Extract, false, TipCreateNew, rctNew.ContractedBy(4)))
Find.WindowStack.Add(
new Dialog_SetText(
s => {
Sets.Add(ModSet.FromCurrent(s, this));
Mod.WriteSettings();
},
LanguageKeys.keyed.ModSwitch_Create_DefaultName.Translate()
));
var rctUndo = new Rect(target.x + 2* (30f + 8f) , target.y, 30f, 30f);
if (ExtraWidgets.ButtonImage(rctNew, Assets.Extract, false, TipCreateNew, rctNew.ContractedBy(4))) {
Find.WindowStack.Add(new FloatMenu(new List<FloatMenuOption> {
new FloatMenuOption(
LanguageKeys.keyed.ModSwitch_CreateNew.Translate(),
() => Find.WindowStack.Add(
new Dialog_SetText(
s => {
Sets.Add(ModSet.FromCurrent(s, this));
Mod.WriteSettings();
},
LanguageKeys.keyed.ModSwitch_Create_DefaultName.Translate()
))),
new FloatMenuOption(
LanguageKeys.keyed.ModSwitch_OverwritExisting.Translate(),
() => Find.WindowStack.Add(
new FloatMenu(Sets.Select(
ms => new FloatMenuOption(
ms.Name,
() => {
if (Input.GetKey(KeyCode.LeftShift) ||
Input.GetKey(KeyCode.RightShift)
) {
OverwriteMod(ms);
}
else {
Find.WindowStack.Add(
Dialog_MessageBox.CreateConfirmation(
LanguageKeys
.keyed.ModSwitch_OverwritExisting_Confirm
.Translate(ms.Name),
() => OverwriteMod(ms),
true,
LanguageKeys.keyed.ModSwitch_Confirmation_Title
.Translate()
));
}
})

).ToList()))
)
}));
}
var rctUndo = new Rect(target.x + 2 * (30f + 8f), target.y, 30f, 30f);
if (_undo != null)
if (ExtraWidgets.ButtonImage(rctUndo, Assets.Undo, false, TipUndo, rctUndo.ContractedBy(4))) {
_undo.Apply();
Expand All @@ -225,7 +258,12 @@ public void DoModsConfigWindowContents(Rect target) {
AccessTools.Field(typeof(Dialog_ModSettings), @"selMod").SetValue(settings, Mod);
Find.WindowStack.Add(settings);
}
}

private void OverwriteMod(ModSet ms) {
var idx = Sets.IndexOf(ms);
Sets[idx] = ModSet.FromCurrent(ms.Name, this);
Mod.WriteSettings();
}
}
}
30 changes: 15 additions & 15 deletions Source/ModSwitch/[Patches]/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
}

public static Rect AllocateAndDrawSearchboxRect(Rect r) {
const float offset = ModsConfig.Search.buttonSize + 2 * ModsConfig.Search.buttonsInset;
const float offset = ModsConfigUI.Search.buttonSize + 2 * ModsConfigUI.Search.buttonsInset;

ModsConfig.Search.DoSearchBlock(
new Rect(r.x + ModsConfig.Search.buttonsInset, r.y + ModsConfig.Search.buttonsInset, r.width - 2 * ModsConfig.Search.buttonsInset, ModsConfig.Search.buttonSize),
ModsConfigUI.Search.DoSearchBlock(
new Rect(r.x + ModsConfigUI.Search.buttonsInset, r.y + ModsConfigUI.Search.buttonsInset, r.width - 2 * ModsConfigUI.Search.buttonsInset, ModsConfigUI.Search.buttonSize),
LanguageKeys.keyed.ModSwitch_Search_Watermark.Translate());

return new Rect(r.x, r.y + offset, r.width, r.height - offset);
Expand Down Expand Up @@ -72,7 +72,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
Util.Warning("Could not find Page_ModsConfig.PreOpen transpiler anchor - not injecting code.");
return instructions;
}
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfig.Helpers), nameof(ModsConfig.Helpers.ForceSteamWorkshopRequery));
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfigUI.Helpers), nameof(ModsConfigUI.Helpers.ForceSteamWorkshopRequery));
return instructions;
}

Expand All @@ -84,8 +84,8 @@ public class Page_ModsConfig_DoModRow {
[HarmonyPatch(typeof(Page_ModsConfig), "DoModRow", new Type[] { typeof(Listing_Standard), typeof(ModMetaData), typeof(int), typeof(int) })]
public class SupressNonMatchingFilteredRows {
public static bool Prefix(ModMetaData mod) {
if (ModsConfig.Search.searchTerm != String.Empty)
return mod.Name.IndexOf(ModsConfig.Search.searchTerm, StringComparison.CurrentCultureIgnoreCase) != -1;
if (ModsConfigUI.Search.searchTerm != String.Empty)
return mod.Name.IndexOf(ModsConfigUI.Search.searchTerm, StringComparison.CurrentCultureIgnoreCase) != -1;
return true;
}
}
Expand All @@ -97,7 +97,7 @@ public class InjectRightClickMenu {
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilGen) {
var instr = new List<CodeInstruction>(instructions);

int idxCheckboxLabeledSelectable = instr.FirstIndexOf(ci => ci.opcode == OpCodes.Call && ci.operand == ModsConfig.miCheckboxLabeledSelectable);
int idxCheckboxLabeledSelectable = instr.FirstIndexOf(ci => ci.opcode == OpCodes.Call && ci.operand == ModsConfigUI.miCheckboxLabeledSelectable);
if (idxCheckboxLabeledSelectable == -1) {
Util.Warning("Could not find anchor for ModRow transpiler - not modifying code");
return instr;
Expand Down Expand Up @@ -143,7 +143,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
// insert <code>else { GUI.contentColor = color; }</code>
instr.InsertRange(idxBlockEnd, new[] {
new CodeInstruction(OpCodes.Ldloc, localColor) {labels = new List<Label> {lblNoClick}},
new CodeInstruction(OpCodes.Call, ModsConfig.miGuiSetContentColor),
new CodeInstruction(OpCodes.Call, ModsConfigUI.miGuiSetContentColor),
});

// setup <code>else { ... }</code> branch label
Expand All @@ -152,12 +152,12 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
// insert <code>GUI.contentColor = color; if (Input.GetMouseButtonUp(1)) { DoContextMenu(mod); }</code>
instr.InsertRange(idxCheckboxLabeledSelectable + 2, new[] {
new CodeInstruction(OpCodes.Ldloc, localColor),
new CodeInstruction(OpCodes.Call, ModsConfig.miGuiSetContentColor),
new CodeInstruction(OpCodes.Call, ModsConfigUI.miGuiSetContentColor),
new CodeInstruction(OpCodes.Ldc_I4_1),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(Input), nameof(Input.GetMouseButtonUp))),
new CodeInstruction(OpCodes.Brfalse, lblExistingClickCode),
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfig), nameof(ModsConfig.DoContextMenu))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfigUI), nameof(ModsConfigUI.DoContextMenu))),
new CodeInstruction(OpCodes.Br, lblBlockEnd),
});

Expand All @@ -167,7 +167,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
// insert <code>Color color = Page_ModsConfig_DoModRow.SetGUIColorMod(mod);</code>
instr.InsertRange(idxCheckboxLabeledSelectable - 4, new[] {
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfig.Helpers), nameof(ModsConfig.Helpers.SetGUIColorMod))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfigUI.Helpers), nameof(ModsConfigUI.Helpers.SetGUIColorMod))),
new CodeInstruction(OpCodes.Stloc, localColor),
});

Expand All @@ -194,10 +194,10 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
*
* with
*
* ModsConfig.DrawContentSource(rect1, rowCAnonStorey428.mod.Source, clickAction, mod);
* ModsConfigUI.DrawContentSource(rect1, rowCAnonStorey428.mod.Source, clickAction, mod);
*
*/
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfig), nameof(ModsConfig.DrawContentSource));
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfigUI), nameof(ModsConfigUI.DrawContentSource));
instructions.Insert(idxAnchor, new CodeInstruction(OpCodes.Ldarg_2));

return instructions;
Expand Down Expand Up @@ -233,7 +233,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
*
* if (workshopItem == null)
* {
* ModsConfig.UpdateSteamTS(pfid, num2);
* ModsConfigUI.UpdateSteamTS(pfid, num2);
* workshopItem = new WorkshopItem_Mod();
* }
*
Expand All @@ -244,7 +244,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
new [] {
new CodeInstruction(OpCodes.Ldarg_0),
new CodeInstruction(OpCodes.Ldloc_2),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfig.Helpers), nameof(ModsConfig.Helpers.UpdateSteamTS))),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfigUI.Helpers), nameof(ModsConfigUI.Helpers.UpdateSteamTS))),
}
);

Expand Down
2 changes: 1 addition & 1 deletion Source/ModSwitch/[UI]/ModsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
using Verse.Steam;

namespace DoctorVanGogh.ModSwitch {
public static class ModsConfig {
public static class ModsConfigUI {
public static MethodInfo miCheckboxLabeledSelectable = AccessTools.Method(typeof(Widgets), nameof(Widgets.CheckboxLabeledSelectable));
public static MethodInfo miGuiSetContentColor = AccessTools.Property(typeof(GUI), nameof(GUI.color)).GetSetMethod(true);
private static readonly MethodInfo miGetModWithIdentifier = AccessTools.Method(typeof(ModLister), "GetModWithIdentifier");
Expand Down

0 comments on commit f80e573

Please sign in to comment.