Skip to content

Commit f80e573

Browse files
committed
add 'overwrite' option for new mod sets
1 parent 9c36c77 commit f80e573

File tree

10 files changed

+92
-34
lines changed

10 files changed

+92
-34
lines changed

Assemblies/ModSwitch.dll

1.5 KB
Binary file not shown.

CHANGELOG.md

Whitespace-only changes.

Languages/English/Keyed/Keyed.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
<ModSwitch.Tip.Undo>Undo</ModSwitch.Tip.Undo>
1010
<ModSwitch.Tip.Apply>Apply mod set</ModSwitch.Tip.Apply>
1111

12+
<ModSwitch.CreateNew>Create new</ModSwitch.CreateNew>
13+
<ModSwitch.OverwritExisting>Overwrite existing...</ModSwitch.OverwritExisting>
14+
<ModSwitch.OverwritExisting.Confirm><![CDATA[Really overwrite {0}?
15+
16+
<i>(You can suppress this dialog by holding SHIFT when you choose this option.)</i>]]></ModSwitch.OverwritExisting.Confirm>
17+
1218
<ModSwitch.Import>Import</ModSwitch.Import>
1319

1420
<ModSwitch.ModSet.Mods>{0} items</ModSwitch.ModSet.Mods>

Source/ModSwitch/LanguageKeys.Generated.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ public static class @keyed {
4545
/// </summary>
4646
public const string @ModSwitch_Tip_Apply = "ModSwitch.Tip.Apply";
4747
/// <summary>
48+
/// Get's the translation key for '<em>Create new</em>'
49+
/// </summary>
50+
public const string @ModSwitch_CreateNew = "ModSwitch.CreateNew";
51+
/// <summary>
52+
/// Get's the translation key for '<em>Overwrite existing...</em>'
53+
/// </summary>
54+
public const string @ModSwitch_OverwritExisting = "ModSwitch.OverwritExisting";
55+
/// <summary>
56+
/// 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>'
57+
/// </summary>
58+
public const string @ModSwitch_OverwritExisting_Confirm = "ModSwitch.OverwritExisting.Confirm";
59+
/// <summary>
4860
/// Get's the translation key for '<em>Import</em>'
4961
/// </summary>
5062
public const string @ModSwitch_Import = "ModSwitch.Import";

Source/ModSwitch/Model/ModSet.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@ class ModSet : IExposable {
3333
public string Name = String.Empty;
3434

3535
static ModSet() {
36-
var tModsConfig = typeof(ModsConfig);
36+
var tModsConfig = typeof(Verse.ModsConfig);
3737
var tModsConfigData = AccessTools.Inner(tModsConfig, @"ModsConfigData");
3838
fiModsConfigData_activeMods = AccessTools.Field(tModsConfigData, @"activeMods");
3939
fiModsConfigData_buildNumber = AccessTools.Field(tModsConfigData, @"buildNumber");
4040
fiModsConfig_data = AccessTools.Field(tModsConfig, @"data");
4141

4242
rgxSteamModId = new Regex(@"^\d+$", RegexOptions.Singleline | RegexOptions.Compiled);
43+
44+
Util.Log($"ModSet cctor: {tModsConfig != null}, {tModsConfigData != null}, {fiModsConfigData_activeMods != null}, {fiModsConfigData_buildNumber != null}, {fiModsConfig_data != null}");
4345
}
4446

4547
public ModSet(Settings owner) {
@@ -179,14 +181,14 @@ public void Delete() {
179181
}
180182

181183

182-
public static ModSet FromCurrent(string name, Settings owner) {
184+
public static ModSet FromCurrent(string name, Settings owner) {
183185
object modsConfigData = fiModsConfig_data.GetValue(null);
184186

185187
return new ModSet(owner) {
186-
Name = name,
187-
BuildNumber = (int) fiModsConfigData_buildNumber.GetValue(modsConfigData),
188-
Mods = new List<string>((IEnumerable<string>) fiModsConfigData_activeMods.GetValue(modsConfigData))
189-
};
188+
Name = name,
189+
BuildNumber = (int)fiModsConfigData_buildNumber.GetValue(modsConfigData),
190+
Mods = new List<string>((IEnumerable<string>)fiModsConfigData_activeMods.GetValue(modsConfigData))
191+
};
190192
}
191193
}
192194
}

Source/ModSwitch/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
[assembly: System.Reflection.AssemblyCopyright("Copyright © DoctorVanGogh 2017")]
1313
[assembly: System.Runtime.InteropServices.ComVisible(false)]
1414
[assembly: System.Runtime.InteropServices.Guid("ab2e8e89-3d1d-4e36-a0ae-489b6ebc84a6")]
15-
[assembly: System.Reflection.AssemblyVersion("1.2.0.66")]
15+
[assembly: System.Reflection.AssemblyVersion("1.2.0.81")]
1616

1717

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2.0.66
1+
1.2.0.81

Source/ModSwitch/Settings.cs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,49 @@ public void DoModsConfigWindowContents(Rect target) {
203203
ms.Apply();
204204
})).ToList()));
205205
var rctNew = new Rect(target.x + 30f + 8f, target.y, 30f, 30f);
206-
if (ExtraWidgets.ButtonImage(rctNew, Assets.Extract, false, TipCreateNew, rctNew.ContractedBy(4)))
207-
Find.WindowStack.Add(
208-
new Dialog_SetText(
209-
s => {
210-
Sets.Add(ModSet.FromCurrent(s, this));
211-
Mod.WriteSettings();
212-
},
213-
LanguageKeys.keyed.ModSwitch_Create_DefaultName.Translate()
214-
));
215-
var rctUndo = new Rect(target.x + 2* (30f + 8f) , target.y, 30f, 30f);
206+
if (ExtraWidgets.ButtonImage(rctNew, Assets.Extract, false, TipCreateNew, rctNew.ContractedBy(4))) {
207+
Find.WindowStack.Add(new FloatMenu(new List<FloatMenuOption> {
208+
new FloatMenuOption(
209+
LanguageKeys.keyed.ModSwitch_CreateNew.Translate(),
210+
() => Find.WindowStack.Add(
211+
new Dialog_SetText(
212+
s => {
213+
Sets.Add(ModSet.FromCurrent(s, this));
214+
Mod.WriteSettings();
215+
},
216+
LanguageKeys.keyed.ModSwitch_Create_DefaultName.Translate()
217+
))),
218+
new FloatMenuOption(
219+
LanguageKeys.keyed.ModSwitch_OverwritExisting.Translate(),
220+
() => Find.WindowStack.Add(
221+
new FloatMenu(Sets.Select(
222+
ms => new FloatMenuOption(
223+
ms.Name,
224+
() => {
225+
if (Input.GetKey(KeyCode.LeftShift) ||
226+
Input.GetKey(KeyCode.RightShift)
227+
) {
228+
OverwriteMod(ms);
229+
}
230+
else {
231+
Find.WindowStack.Add(
232+
Dialog_MessageBox.CreateConfirmation(
233+
LanguageKeys
234+
.keyed.ModSwitch_OverwritExisting_Confirm
235+
.Translate(ms.Name),
236+
() => OverwriteMod(ms),
237+
true,
238+
LanguageKeys.keyed.ModSwitch_Confirmation_Title
239+
.Translate()
240+
));
241+
}
242+
})
243+
244+
).ToList()))
245+
)
246+
}));
247+
}
248+
var rctUndo = new Rect(target.x + 2 * (30f + 8f), target.y, 30f, 30f);
216249
if (_undo != null)
217250
if (ExtraWidgets.ButtonImage(rctUndo, Assets.Undo, false, TipUndo, rctUndo.ContractedBy(4))) {
218251
_undo.Apply();
@@ -225,7 +258,12 @@ public void DoModsConfigWindowContents(Rect target) {
225258
AccessTools.Field(typeof(Dialog_ModSettings), @"selMod").SetValue(settings, Mod);
226259
Find.WindowStack.Add(settings);
227260
}
261+
}
228262

263+
private void OverwriteMod(ModSet ms) {
264+
var idx = Sets.IndexOf(ms);
265+
Sets[idx] = ModSet.FromCurrent(ms.Name, this);
266+
Mod.WriteSettings();
229267
}
230268
}
231269
}

Source/ModSwitch/[Patches]/Patches.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
4141
}
4242

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

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

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

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

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

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

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

@@ -194,10 +194,10 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
194194
*
195195
* with
196196
*
197-
* ModsConfig.DrawContentSource(rect1, rowCAnonStorey428.mod.Source, clickAction, mod);
197+
* ModsConfigUI.DrawContentSource(rect1, rowCAnonStorey428.mod.Source, clickAction, mod);
198198
*
199199
*/
200-
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfig), nameof(ModsConfig.DrawContentSource));
200+
instructions[idxAnchor].operand = AccessTools.Method(typeof(ModsConfigUI), nameof(ModsConfigUI.DrawContentSource));
201201
instructions.Insert(idxAnchor, new CodeInstruction(OpCodes.Ldarg_2));
202202

203203
return instructions;
@@ -233,7 +233,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
233233
*
234234
* if (workshopItem == null)
235235
* {
236-
* ModsConfig.UpdateSteamTS(pfid, num2);
236+
* ModsConfigUI.UpdateSteamTS(pfid, num2);
237237
* workshopItem = new WorkshopItem_Mod();
238238
* }
239239
*
@@ -244,7 +244,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
244244
new [] {
245245
new CodeInstruction(OpCodes.Ldarg_0),
246246
new CodeInstruction(OpCodes.Ldloc_2),
247-
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfig.Helpers), nameof(ModsConfig.Helpers.UpdateSteamTS))),
247+
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(ModsConfigUI.Helpers), nameof(ModsConfigUI.Helpers.UpdateSteamTS))),
248248
}
249249
);
250250

Source/ModSwitch/[UI]/ModsConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Verse.Steam;
1414

1515
namespace DoctorVanGogh.ModSwitch {
16-
public static class ModsConfig {
16+
public static class ModsConfigUI {
1717
public static MethodInfo miCheckboxLabeledSelectable = AccessTools.Method(typeof(Widgets), nameof(Widgets.CheckboxLabeledSelectable));
1818
public static MethodInfo miGuiSetContentColor = AccessTools.Property(typeof(GUI), nameof(GUI.color)).GetSetMethod(true);
1919
private static readonly MethodInfo miGetModWithIdentifier = AccessTools.Method(typeof(ModLister), "GetModWithIdentifier");

0 commit comments

Comments
 (0)