Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
fix copying to clipboard shenanigans
Browse files Browse the repository at this point in the history
add copy and paste module list to antijank
  • Loading branch information
Tyler-IN committed Apr 25, 2020
1 parent d2cc250 commit f61e692
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/CommunityPatch/CommunityPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
<PackageReference Include="TextCopy" Version="3.0.2" />
<PackageReference Include="Tomlyn" Version="0.1.2" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/CommunityPatch/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,13 @@ public static void GenerateReport() {
try {
ms.Position = 0;
using var sr = new StreamReader(ms, Encoding.UTF8);
Input.SetClipboardText(sr.ReadToEnd());
TextCopy.Clipboard.SetText(sr.ReadToEnd());
ShowMessage("Diagnostics also copied to system clipboard.");
}
catch {
ShowMessage($"Writing report to system clipboard failed!");
try {
Input.SetClipboardText(fullPath);
TextCopy.Clipboard.SetText(fullPath);
ShowMessage("Writing diagnostic file path to clipboard instead.");
}
catch (Exception ex) {
Expand Down
1 change: 1 addition & 0 deletions tools/Antijank/Antijank.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<PackageReference Include="ModuleInit.Fody" Version="2.1.0" />
<PackageReference Include="Sigil" Version="5.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.7.1" />
<PackageReference Include="TextCopy" Version="3.0.2" />
</ItemGroup>

<ItemGroup>
Expand Down
110 changes: 79 additions & 31 deletions tools/Antijank/LoaderPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static unsafe bool GraphicsFormUpdateInputPatch(
return false;
}

private static bool waitForSortKeysReset = false;
private static bool waitForKeysReset = false;

public static void LauncherUICtorPostfix(LauncherUI __instance, UserDataManager userDataManager, UIContext context, Action onClose, Action onMinimize) {
LauncherUI = __instance;
Expand All @@ -159,35 +159,76 @@ void KeyWatcher(float tick) {
}, null);

var ctrl = input.IsControlDown();
var s = input.IsKeyDown(InputKey.S);

if (waitForSortKeysReset) {
if (!ctrl || !s)
waitForSortKeysReset = false;
if (waitForKeysReset) {
if (!ctrl)
waitForKeysReset = false;
return;
}

var wantsToSort = ctrl && s;
if (!wantsToSort)
if (!ctrl)
return;

waitForSortKeysReset = true;
Console.WriteLine("Sorting modules list.");
ref var launcherVm = ref _launcherVmAccessor(__instance);
var isMultiplayer = launcherVm.IsMultiplayer;
if (input.IsKeyDown(InputKey.S)) {
waitForKeysReset = true;
Console.WriteLine("Sorting modules list.");
ref var launcherVm = ref _launcherVmAccessor(__instance);

var dict = ModuleList.ToDictionary(mi => mi.Id);
var dict = ModuleList.ToDictionary(mi => mi.Id);

var list = ModuleList
.OrderTopologicallyBy(mi => mi.DependedModuleIds.Select(id => dict[id]))
.ThenBy(mi => mi.IsNative() ? 0 : 1)
.ThenBy(mi => mi.Alias)
.ToList();
var list = ModuleList
.OrderTopologicallyBy(mi => mi.DependedModuleIds.Select(id => dict[id]))
.ThenBy(mi => mi.IsNative() ? 0 : 1)
.ThenBy(mi => mi.Alias)
.ToList();

IdentitySort(launcherVm.ModsData.Modules, list);
IdentitySort(launcherVm.ModsData.Modules, list);

_gauntletMovieAccessor(__instance)
.RefreshDataSource(launcherVm);
_gauntletMovieAccessor(__instance)
.RefreshDataSource(launcherVm);
return;
}

if (input.IsKeyDown(InputKey.C)) {
Console.WriteLine("Copying modules list to clipboard.");
var modsList = string.Join("\n",
ModuleList
.Where(m => m.IsSelected)
.Select(m => m.Id));
TextCopy.Clipboard.SetText(modsList);
waitForKeysReset = true;
return;
}

if (input.IsKeyDown(InputKey.V)) {
Console.WriteLine("Pasting modules list from clipboard.");
ref var launcherVm = ref _launcherVmAccessor(__instance);
var inputText = TextCopy.Clipboard.GetText() ?? "";
var ids = inputText.Split(new[] {
", ", ",",
"; ", ";",
"\r\n",
"\r", "\n"
}, StringSplitOptions.RemoveEmptyEntries);
var idSet = new HashSet<string>(ids);
IdentitySort(launcherVm.ModsData.Modules, ids);
foreach (var mod in launcherVm.ModsData.Modules) {
var info = mod.Info;
if (info.IsNative() || info.IsOfficial)
continue;

var id = info.Id;
if (idSet.Contains(id)) {
mod.IsSelected = true;
}
else {
mod.IsSelected = false;
}
}

FixSequence(launcherVm.ModsData.Modules);
waitForKeysReset = true;
}
}

context.EventManager.AddLateUpdateAction(context.Root, KeyWatcher, 5);
Expand Down Expand Up @@ -271,6 +312,16 @@ private static void IdentitySort(MBBindingList<LauncherModuleVM> list, IReadOnly
=> sorted.FindIndex(x => x == a.Info)
.CompareTo(sorted.FindIndex(x => x == b.Info))));

private static void IdentitySort(MBBindingList<LauncherModuleVM> list, IReadOnlyList<string> sorted)
=> list.Sort(Comparer<LauncherModuleVM>.Create((a, b)
=> {
var indexA = sorted.FindIndex(x => x == a.Info.Id);
if (indexA == -1) indexA = int.MaxValue;
var indexB = sorted.FindIndex(x => x == b.Info.Id);
if (indexB == -1) indexB = int.MaxValue;
return indexA.CompareTo(indexB);
}));

public static IReadOnlyList<ModuleInfo> ModuleList { get; private set; }

private static void UnblockModule(ModuleInfo moduleInfo) {
Expand Down Expand Up @@ -318,22 +369,19 @@ private static void ChangeLoadingOrderOf(LauncherModsVM mods, LauncherModuleVM t

private static void ChangeIsSelectedOf(LauncherModsVM mods, UserData userData, bool isMultiplayer, LauncherModuleVM targetModule) {
if (targetModule.IsSelected) {
using (var enumerator = mods.Modules.GetEnumerator()) {
while (enumerator.MoveNext()) {
var launcherModuleVm = enumerator.Current;
if (launcherModuleVm == null)
continue;

launcherModuleVm.IsSelected |= targetModule.Info.DependedModuleIds.Contains(launcherModuleVm.Info.Id);
}
foreach (var module in mods.Modules) {
if (module == null)
continue;

return;
module.IsSelected |= targetModule.Info.DependedModuleIds
.Contains(module.Info.Id);
}

return;
}

foreach (var launcherModuleVm2 in mods.Modules) {
foreach (var launcherModuleVm2 in mods.Modules)
launcherModuleVm2.IsSelected &= !launcherModuleVm2.Info.DependedModuleIds.Contains(targetModule.Info.Id);
}
}

}
Expand Down

0 comments on commit f61e692

Please sign in to comment.