Skip to content

Commit

Permalink
Adapt to new MonkeyLoader updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Banane9 committed Apr 17, 2024
1 parent 34dd37f commit 669b194
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 55 deletions.
94 changes: 49 additions & 45 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
name: CI

on: [push]
# Trigger the action on push to master
on:
push:
branches:
- master

env:
ResonitePath: "${{ github.workspace }}/Resonite"
# Sets permissions of the GITHUB_TOKEN to allow reading packages and writing to GH pages
permissions:
actions: read
pages: write
id-token: write
packages: read

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
group: "ci"
cancel-in-progress: false

jobs:
ci:
publish-docs:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Get Resonite Infos
run: |
{
echo "APP_INFO<<EOFEOF"
curl https://api.steamcmd.net/v1/info/2519830
echo ""
echo "EOFEOF"
} >> "$GITHUB_ENV"
- name: Get Resonite from Cache
id: cache-resonite
uses: actions/cache@v3
with:
path: "${{ env.ResonitePath }}"
key: "${{ fromJson( env.APP_INFO ).data['2519830'].depots.branches.public.buildid }}"
- name: Download Steam
if: steps.cache-resonite.outputs.cache-hit != 'true'
uses: CyberAndrii/setup-steamcmd@b786e0da44db3d817e66fa3910a9560cb28c9323
- name: Download Resonite
if: steps.cache-resonite.outputs.cache-hit != 'true'
run: |
steamcmd '+@sSteamCmdForcePlatformType windows' '+force_install_dir "${{ env.ResonitePath }}"' '+login "${{ secrets.STEAM_USER }}" "${{ secrets.STEAM_TOKEN }}"' '+app_license_request 2519830' '+app_update 2519830 validate' '+quit'
#The following line makes the cache much much smaller:
rm -r '${{ env.ResonitePath }}/RuntimeData/PreCache'
- name: Setup dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: '6.x'
- uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build
- name: Test
run: dotnet test
# Setup environment
- name: Checkout
uses: actions/checkout@v3
- name: Dotnet Setup
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
source-url: https://nuget.pkg.github.com/MonkeyModdingTroop/index.json
env:
NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# Cache NuGet packages
- uses: actions/cache@v3
with:
path: ~/.nuget/packages
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj') }}
restore-keys: |
${{ runner.os }}-nuget
# Build and test projects
- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build

- name: Test
run: dotnet test
3 changes: 2 additions & 1 deletion FlexibleContactsSort/ExtraContactColoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ namespace FlexibleContactsSort
[HarmonyPatch(typeof(LegacyUIStyle), nameof(LegacyUIStyle.GetStatusColor))]
internal sealed class ExtraContactColoring : ResoniteMonkey<ExtraContactColoring>
{
public override bool CanBeDisabled => true;
public override string Name { get; } = "ExtraContactColoring";

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => Enumerable.Empty<IFeaturePatch>();

private static void Postfix(Contact contact, ContactData status, bool text, ref colorX __result)
{
if (contact.ContactStatus == ContactStatus.Accepted && !contact.IsAccepted)
if (Enabled && contact.ContactStatus == ContactStatus.Accepted && !contact.IsAccepted)
__result = text ? RadiantUI_Constants.Hero.YELLOW : RadiantUI_Constants.MidLight.YELLOW;
}
}
Expand Down
10 changes: 9 additions & 1 deletion FlexibleContactsSort/FlexibleContactSorting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal sealed class FlexibleContactSorting : ConfiguredResoniteMonkey<Flexible

private static readonly ConditionalWeakTable<ContactItem, ReadMessageTracker> _contactReadMessageTrackers = new();

public override bool CanBeDisabled => true;

internal static int Compare((ContactData?, bool) contactSortInfo1, (ContactData?, bool) contactSortInfo2)
{
var contact1 = contactSortInfo1.Item1?.Contact;
Expand Down Expand Up @@ -139,9 +141,15 @@ private static void OnCommonUpdatePostfix(ContactsDialog __instance, bool __stat
[HarmonyPatch(nameof(ContactsDialog.OnCommonUpdate))]
private static void OnCommonUpdatePrefix(ContactsDialog __instance, out bool __state)
{
if (!Enabled)
{
__state = false;
return;
}

// steal the sortList bool's value, and force it to false from Resonite's perspective
__state = __instance.sortList;
__instance.sortList = false;
__instance.sortList &= !Enabled;
}

[HarmonyPostfix]
Expand Down
8 changes: 4 additions & 4 deletions FlexibleContactsSort/FlexibleContactsSort.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageId>FlexibleContactSorting</PackageId>
<Title>Flexible Contact Sorting</Title>
<Authors>Banane9</Authors>
<Version>0.3.0-beta</Version>
<Version>0.4.0-beta</Version>
<Description>This MonkeyLoader mod for Resonite allows sorting contacts flexibly and to your liking, including pinning your favorites to the top.</Description>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>LGPL-3.0-or-later</PackageLicenseExpression>
Expand Down Expand Up @@ -42,14 +42,14 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonkeyLoader" Version="0.11.0-beta" />
<PackageReference Include="MonkeyLoader.GamePacks.Resonite" Version="0.10.1-beta" />
<PackageReference Include="MonkeyLoader" Version="0.15.0-beta" />
<PackageReference Include="MonkeyLoader.GamePacks.Resonite" Version="0.13.0-beta" />
<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Resonite.Elements.Core" Version="1.0.2" />
<PackageReference Include="Resonite.FrooxEngine" Version="2024.3.3.1178" />
<PackageReference Include="Resonite.FrooxEngine" Version="2024.4.17.1407" />
<PackageReference Include="Resonite.SkyFrost.Base" Version="1.0.2" />
<PackageReference Include="System.Text.Json" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
Expand Down
116 changes: 112 additions & 4 deletions FlexibleContactsSort/LagFreeContactsLoading.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Elements.Core;
using Elements.Assets;
using FrooxEngine;
using HarmonyLib;
using MonkeyLoader.Patching;
Expand All @@ -20,6 +20,7 @@ internal sealed class LagFreeContactsLoading : ResoniteMonkey<LagFreeContactsLoa
{
private const int ContactsPerUpdate = 8;

public override bool CanBeDisabled => true;
internal static bool AllowSorting { get; private set; } = true;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => Enumerable.Empty<IFeaturePatch>();
Expand All @@ -43,22 +44,39 @@ private static void AddContactItems(ContactsDialog contactsDialog)
var segments = contactsSortInfo.Length / ContactsPerUpdate;
for (var segment = 0; segment < segments; ++segment)
{
for (var i = 0; i <= ContactsPerUpdate; ++i)
contactsDialog.UpdateContactItem(contactsSortInfo[ContactsPerUpdate * segment + i].contactData);
for (var i = 0; i < ContactsPerUpdate; ++i)
contactsDialog.UpdateContactItem(contactsSortInfo[(ContactsPerUpdate * segment) + i].contactData);

await default(NextUpdate);
}

for (var i = segments * ContactsPerUpdate; i < contactsSortInfo.Length; ++i)
contactsDialog.UpdateContactItem(contactsSortInfo[i].contactData);

await default(NextUpdate);

AllowSorting = true;
});

private static void AddSearchResult(ContactsDialog contactsDialog, SkyFrost.Base.User user)
{
if (contactsDialog._contactItems.ContainsKey(user.Id)
|| contactsDialog._searchResultItems.Any(item => item.Contact.ContactUserId == user.Id)
|| user.Id == contactsDialog.Cloud.CurrentUserID)
return;

var contactItem = contactsDialog.AddContactItem();
contactItem.Update(user);
contactsDialog._searchResultItems.Add(contactItem);
}

[HarmonyTranspiler]
[HarmonyPatch("OnAttach")]
[HarmonyPatch(nameof(ContactsDialog.OnAttach))]
private static IEnumerable<CodeInstruction> OnAttachTranspiler(IEnumerable<CodeInstruction> instructionsEnumerable)
{
if (!Enabled)
return instructionsEnumerable;

var foreachContactDataMethod = AccessTools.Method(typeof(ContactManager), nameof(ContactManager.ForeachContactData));
var addContactItemsMethod = AccessTools.Method(typeof(LagFreeContactsLoading), nameof(AddContactItems));

Expand All @@ -71,5 +89,95 @@ private static IEnumerable<CodeInstruction> OnAttachTranspiler(IEnumerable<CodeI

return instructions;
}

private static bool RemoveItem(ContactItem contact, string? searchTerm, bool clear)
{
if (clear || !(contact.Username.StartsWith(searchTerm)
|| contact.AlternateNames.Any(name => name.StartsWith(searchTerm, StringComparison.InvariantCulture))))
{
contact.Slot.Destroy();
return true;
}

return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(ContactsDialog.SearchTextChanged))]
private static bool SearchTextChangedPrefix(ContactsDialog __instance, TextEditor editor)
{
if (!Enabled)
return true;

__instance.StartTask(async () =>
{
var searchTerm = editor.TargetString?.Trim().ToLower();
var clear = string.IsNullOrWhiteSpace(searchTerm);

var segments = __instance._searchResultItems.Count / ContactsPerUpdate;

for (var i = __instance._searchResultItems.Count - 1; i >= segments * ContactsPerUpdate; --i)
{
if (RemoveItem(__instance._searchResultItems[i], searchTerm, clear))
__instance._searchResultItems.RemoveAt(i);
}

for (var segment = segments - 1; segment >= 0; --segment)
{
await default(NextUpdate);

for (var i = ContactsPerUpdate - 1; i >= 0; --i)
{
var x = (ContactsPerUpdate * segment) + i;

if (RemoveItem(__instance._searchResultItems[x], searchTerm, clear))
__instance._searchResultItems.RemoveAt(x);
}
}

await default(NextUpdate);

foreach (var contactItem in __instance._contactItems)
{
contactItem.Value.Slot.ActiveSelf = clear
|| contactItem.Value.Username.StartsWith(searchTerm, StringComparison.InvariantCultureIgnoreCase)
|| contactItem.Value.AlternateNames.Any(name => name.StartsWith(searchTerm, StringComparison.InvariantCulture));
}
});

__instance.globalSearchTimer = 0.5;

return false;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(ContactsDialog.SetSearchResults))]
private static bool SetSearchResultsPrefix(ContactsDialog __instance, List<SkyFrost.Base.User> users)
{
if (!Enabled)
return true;

__instance.StartTask(async () =>
{
var segments = users.Count / ContactsPerUpdate;

for (var segment = 0; segment < segments; ++segment)
{
for (var i = 0; i < ContactsPerUpdate; ++i)
AddSearchResult(__instance, users[(ContactsPerUpdate * segment) + i]);

await default(NextUpdate);
}

for (var i = segments * ContactsPerUpdate; i < users.Count; ++i)
AddSearchResult(__instance, users[i]);

await default(NextUpdate);

__instance.sortList = true;
});

return false;
}
}
}
5 changes: 5 additions & 0 deletions FlexibleContactsSort/SessionUserCapacity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ namespace FlexibleContactsSort
[HarmonyPatch(typeof(SessionItem), nameof(SessionItem.Update))]
internal sealed class SessionUserCapacity : ConfiguredResoniteMonkey<SessionUserCapacity, SessionCapacityConfig>
{
public override bool CanBeDisabled => true;

protected override IEnumerable<IFeaturePatch> GetFeaturePatches() => Enumerable.Empty<IFeaturePatch>();

private static void Postfix(SessionItem __instance, SessionInfo session)
{
if (!Enabled)
return;

var format = "{0} ({1})";

if (ConfigSection.ShowUserCapacityInSessionList)
Expand Down

0 comments on commit 669b194

Please sign in to comment.