Skip to content
This repository has been archived by the owner on Nov 20, 2021. It is now read-only.

Commit

Permalink
light id rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeroluna committed Mar 15, 2021
1 parent 5481cdb commit a8fc978
Show file tree
Hide file tree
Showing 29 changed files with 317 additions and 35 deletions.
24 changes: 24 additions & 0 deletions Chroma/Chroma.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="AnimationHelper.cs" />
<Compile Include="ChromaPatch.cs" />
<Compile Include="ChromaRingsRotationEffect.cs" />
<Compile Include="LightIDTableManager.cs" />
<Compile Include="Colorizer\BombColorizer.cs" />
<Compile Include="Colorizer\SaberColorizer.cs" />
<Compile Include="Colorizer\NoteColorizer.cs" />
Expand Down Expand Up @@ -92,6 +93,24 @@
<None Include="Directory.Build.props" Condition="Exists('Directory.Build.props')" />
<None Include="Directory.Build.targets" Condition="Exists('Directory.Build.targets')" />
<None Include="Chroma.csproj.user" Condition="Exists('Chroma.csproj.user')" />
<EmbeddedResource Include="LightIDTables\BigMirrorEnvironment.json" />
<EmbeddedResource Include="LightIDTables\BTSEnvironment.json" />
<EmbeddedResource Include="LightIDTables\CrabRaveEnvironment.json" />
<EmbeddedResource Include="LightIDTables\DefaultEnvironment.json" />
<EmbeddedResource Include="LightIDTables\DragonsEnvironment.json" />
<EmbeddedResource Include="LightIDTables\FitBeatEnvironment.json" />
<EmbeddedResource Include="LightIDTables\GlassDesertEnvironment.json" />
<EmbeddedResource Include="LightIDTables\GreenDayEnvironment.json" />
<EmbeddedResource Include="LightIDTables\GreenDayGrenadeEnvironment.json" />
<EmbeddedResource Include="LightIDTables\KDAEnvironment.json" />
<EmbeddedResource Include="LightIDTables\LinkinParkEnvironment.json" />
<EmbeddedResource Include="LightIDTables\MonstercatEnvironment.json" />
<EmbeddedResource Include="LightIDTables\NiceEnvironment.json" />
<EmbeddedResource Include="LightIDTables\OriginsEnvironment.json" />
<EmbeddedResource Include="LightIDTables\PanicEnvironment.json" />
<EmbeddedResource Include="LightIDTables\RocketEnvironment.json" />
<EmbeddedResource Include="LightIDTables\TimbalandEnvironment.json" />
<EmbeddedResource Include="LightIDTables\TriangleEnvironment.json" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\StyleCop.Analyzers.1.1.118\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
Expand Down Expand Up @@ -160,6 +179,11 @@
<Private>False</Private>
<SpecificVersion>False</SpecificVersion>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(BeatSaberDir)\Libs\Newtonsoft.Json.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="NoodleExtensions, Version=1.3.8.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>$(BeatSaberDir)\Plugins\NoodleExtensions.dll</HintPath>
Expand Down
4 changes: 2 additions & 2 deletions Chroma/Colorizer/LightColorizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ internal static void SetLastValue(this MonoBehaviour lse, int value)
LSEColorManager.GetLSEColorManager(lse)?.SetLastValue(value);
}

internal static ILightWithId[] GetLights(this LightSwitchEventEffect lse)
internal static List<ILightWithId> GetLights(this LightSwitchEventEffect lse)
{
return LSEColorManager.GetLSEColorManager(lse)?.Lights.ToArray();
return LSEColorManager.GetLSEColorManager(lse)?.Lights;
}

internal static ILightWithId[][] GetLightsPropagationGrouped(this LightSwitchEventEffect lse)
Expand Down
34 changes: 25 additions & 9 deletions Chroma/HarmonyPatches/LightSwitchEventEffect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace Chroma.HarmonyPatches
{
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Chroma.Colorizer;
using HarmonyLib;
using UnityEngine;
Expand All @@ -25,11 +27,30 @@ private static IEnumerator WaitThenStart(LightSwitchEventEffect instance, Beatma
[ChromaPatch("SetColor")]
internal static class LightSwitchEventEffectSetColor
{
private static bool Prefix(Color color)
private static bool Prefix(LightSwitchEventEffect __instance, BeatmapEventType ____event, Color color)
{
if (LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.OverrideLightWithIdActivation != null)
if (LightColorManager.LightIDOverride != null)
{
ILightWithId[] lights = LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.OverrideLightWithIdActivation;
List<ILightWithId> lights = __instance.GetLights();
int type = (int)____event;
IEnumerable<int> newIds = LightColorManager.LightIDOverride.Select(n => LightIDTableManager.GetActiveTableValue(type, n) ?? n);
foreach (int id in newIds)
{
if (lights[id].isRegistered)
{
lights[id].ColorWasSet(color);
}
}

LightColorManager.LightIDOverride = null;

return false;
}

// Legacy Prop Id stuff
if (LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.LegacyLightOverride != null)
{
ILightWithId[] lights = LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.LegacyLightOverride;
for (int i = 0; i < lights.Length; i++)
{
lights[i].ColorWasSet(color);
Expand All @@ -46,7 +67,7 @@ private static bool Prefix(Color color)
[ChromaPatch("HandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger")]
internal static class LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger
{
internal static ILightWithId[] OverrideLightWithIdActivation { get; set; }
internal static ILightWithId[] LegacyLightOverride { get; set; }

// 0 = off
// 1 = blue on, 5 = red on
Expand All @@ -59,10 +80,5 @@ private static void Prefix(LightSwitchEventEffect __instance, BeatmapEventData b
LightColorManager.ColorLightSwitch(__instance, beatmapEventData);
}
}

private static void Postfix()
{
OverrideLightWithIdActivation = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Chroma.HarmonyPatches
{
using System;
using System.Collections.Generic;
using HarmonyLib;

[HarmonyPatch(
Expand All @@ -9,6 +10,11 @@
[HarmonyPatch("Init")]
internal static class MissionLevelScenesTransitionSetupDataSOInit
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return SceneTransitionHelper.Transpiler(instructions);
}

private static void Postfix(IDifficultyBeatmap difficultyBeatmap)
{
SceneTransitionHelper.Patch(difficultyBeatmap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
[HarmonyPatch("Init")]
internal static class MultiplayerLevelScenesTransitionSetupDataSOInit
{
private static void Postfix(IDifficultyBeatmap difficultyBeatmap)
private static void Postfix(IDifficultyBeatmap difficultyBeatmap, EnvironmentInfoSO ____multiplayerEnvironmentInfo)
{
LightIDTableManager.SetEnvironment(____multiplayerEnvironmentInfo.serializedName);

SceneTransitionHelper.Patch(difficultyBeatmap);
}
}
Expand Down
34 changes: 34 additions & 0 deletions Chroma/HarmonyPatches/ScenesTransition/SceneTransitionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@
{
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using Chroma.Settings;
using CustomJSONData;
using CustomJSONData.CustomBeatmap;
using HarmonyLib;

internal static class SceneTransitionHelper
{
private static readonly MethodInfo _setEnvironmentTable = SymbolExtensions.GetMethodInfo(() => SetEnvironmentTable(null));

internal static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> instructionList = instructions.ToList();
bool foundReturn = false;
for (int i = 0; i < instructionList.Count; i++)
{
if (!foundReturn &&
instructionList[i].opcode == OpCodes.Ret)
{
foundReturn = true;

instructionList.Insert(i, new CodeInstruction(OpCodes.Ldloc_0));
instructionList.Insert(i + 1, new CodeInstruction(OpCodes.Call, _setEnvironmentTable));
}
}

if (!foundReturn)
{
ChromaLogger.Log("Failed to find ret!", IPA.Logging.Logger.Level.Error);
}

return instructionList.AsEnumerable();
}

internal static void Patch(IDifficultyBeatmap difficultyBeatmap)
{
if (difficultyBeatmap.beatmapData is CustomBeatmapData customBeatmapData)
Expand All @@ -28,6 +57,11 @@ internal static void Patch(IDifficultyBeatmap difficultyBeatmap, ref OverrideEnv
}
}

private static void SetEnvironmentTable(EnvironmentInfoSO environmentInfo)
{
LightIDTableManager.SetEnvironment(environmentInfo.serializedName);
}

private static bool BasicPatch(CustomBeatmapData customBeatmapData)
{
IEnumerable<string> requirements = ((List<object>)Trees.at(customBeatmapData.beatmapCustomData, "_requirements"))?.Cast<string>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Chroma.HarmonyPatches
{
using System;
using System.Collections.Generic;
using HarmonyLib;

[HarmonyPatch(
Expand All @@ -9,6 +10,11 @@
[HarmonyPatch("Init")]
internal static class StandardLevelScenesTransitionSetupDataSOInit
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
return SceneTransitionHelper.Transpiler(instructions);
}

private static void Prefix(IDifficultyBeatmap difficultyBeatmap, ref OverrideEnvironmentSettings overrideEnvironmentSettings)
{
SceneTransitionHelper.Patch(difficultyBeatmap, ref overrideEnvironmentSettings);
Expand Down
30 changes: 9 additions & 21 deletions Chroma/LightColorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

internal static class LightColorManager
{
internal static List<int> LightIDOverride { get; set; }

internal static void ColorLightSwitch(MonoBehaviour monobehaviour, BeatmapEventData beatmapEventData)
{
monobehaviour.SetLastValue(beatmapEventData.value);
Expand All @@ -28,35 +30,21 @@ internal static void ColorLightSwitch(MonoBehaviour monobehaviour, BeatmapEventD
object lightID = Trees.at(dynData, LIGHTID);
if (lightID != null)
{
ILightWithId[] lights = lightSwitchEventEffect.GetLights();
int lightCount = lights.Length;
switch (lightID)
{
case List<object> lightIDobjects:
int[] lightIDArray = lightIDobjects.Select(n => System.Convert.ToInt32(n)).ToArray();
List<ILightWithId> overrideLights = new List<ILightWithId>();
for (int i = 0; i < lightIDArray.Length; i++)
{
if (lightCount > lightIDArray[i])
{
overrideLights.Add(lights[lightIDArray[i]]);
}
}

SetOverrideLightWithIds(overrideLights.ToArray());
LightIDOverride = lightIDobjects.Select(n => System.Convert.ToInt32(n)).ToList();

break;

case long lightIDint:
if (lightCount > lightIDint)
{
SetOverrideLightWithIds(lights[lightIDint]);
}
LightIDOverride = new List<int> { (int)lightIDint };

break;
}
}

// propID is now DEPRECATED!!!!!!!!
object propID = Trees.at(dynData, PROPAGATIONID);
if (propID != null)
{
Expand All @@ -75,14 +63,14 @@ internal static void ColorLightSwitch(MonoBehaviour monobehaviour, BeatmapEventD
}
}

SetOverrideLightWithIds(overrideLights.ToArray());
SetLegacyPropIdOverride(overrideLights.ToArray());

break;

case long propIDlong:
if (lightCount > propIDlong)
{
SetOverrideLightWithIds(lights[propIDlong]);
SetLegacyPropIdOverride(lights[propIDlong]);
}

break;
Expand Down Expand Up @@ -114,9 +102,9 @@ internal static void ColorLightSwitch(MonoBehaviour monobehaviour, BeatmapEventD
}
}

private static void SetOverrideLightWithIds(params ILightWithId[] lights)
private static void SetLegacyPropIdOverride(params ILightWithId[] lights)
{
HarmonyPatches.LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.OverrideLightWithIdActivation = lights;
HarmonyPatches.LightSwitchEventEffectHandleBeatmapObjectCallbackControllerBeatmapEventDidTrigger.LegacyLightOverride = lights;
}
}
}
70 changes: 70 additions & 0 deletions Chroma/LightIDTableManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
namespace Chroma
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json;

internal static class LightIDTableManager
{
private static readonly Dictionary<string, Dictionary<int, int>[]> _lightIDTable = new Dictionary<string, Dictionary<int, int>[]>();

private static Dictionary<int, int>[] _activeTable;

internal static int? GetActiveTableValue(int type, int id)
{
if (_activeTable != null)
{
if (_activeTable[type].TryGetValue(id, out int newId))
{
return newId;
}
else
{
ChromaLogger.Log($"Unable to find value for type [{type}] and id [{id}].", IPA.Logging.Logger.Level.Warning);
}
}

return null;
}

internal static void SetEnvironment(string environmentName)
{
if (_lightIDTable.TryGetValue(environmentName, out Dictionary<int, int>[] activeTable))
{
_activeTable = activeTable;
}
else
{
_activeTable = null;
ChromaLogger.Log($"Table not found for: {environmentName}", IPA.Logging.Logger.Level.Warning);
}
}

internal static void InitTable()
{
string tableNamespace = "Chroma.LightIDTables.";
Assembly assembly = Assembly.GetExecutingAssembly();
IEnumerable<string> tableNames = assembly.GetManifestResourceNames().Where(n => n.StartsWith(tableNamespace));
foreach (string tableName in tableNames)
{
using (JsonReader reader = new JsonTextReader(new StreamReader(assembly.GetManifestResourceStream(tableName))))
{
Dictionary<int, int>[] typeTable = new Dictionary<int, int>[5];

JsonSerializer serializer = new JsonSerializer();
Dictionary<string, Dictionary<string, int>> rawDict = serializer.Deserialize<Dictionary<string, Dictionary<string, int>>>(reader);

foreach (KeyValuePair<string, Dictionary<string, int>> typePair in rawDict)
{
typeTable[int.Parse(typePair.Key)] = typePair.Value.ToDictionary(n => int.Parse(n.Key), n => n.Value);
}

string tableNameWithoutExtension = Path.GetFileNameWithoutExtension(tableName.Remove(tableName.IndexOf(tableNamespace), tableNamespace.Length));
_lightIDTable.Add(tableNameWithoutExtension, typeTable);
}
}
}
}
}
1 change: 1 addition & 0 deletions Chroma/LightIDTables/BTSEnvironment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"0":{"1":4,"2":1,"3":3},"1":{"1":14,"2":8,"3":0,"4":12,"5":16,"6":6,"7":3,"8":9,"9":17,"10":5,"11":2,"12":10,"13":15,"14":4,"15":1,"16":11,"17":24,"18":22,"19":23,"20":21},"2":{"1":8,"2":12,"3":10,"4":17,"5":4,"6":25,"7":18,"8":1,"9":21,"10":14,"11":6,"12":27,"13":16,"14":3,"15":24,"16":15,"17":7,"18":28,"19":20,"20":2,"21":23,"22":13,"23":5,"24":26,"25":19,"26":0,"27":22},"3":{"1":0,"2":20,"3":10,"4":19,"5":5,"6":21,"7":16,"8":8,"9":24,"10":12,"11":2,"12":27,"13":18,"14":6,"15":22,"16":13,"17":1,"18":26,"19":17,"20":7,"21":23,"22":11,"23":3,"24":28,"25":15,"26":9,"27":25},"4":{"1":7,"2":8,"3":10,"4":9,"5":12}}
1 change: 1 addition & 0 deletions Chroma/LightIDTables/BigMirrorEnvironment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"0":{"1":7,"2":9,"3":6,"4":3,"5":2,"6":0,"7":5,"8":1,"9":8,"10":4},"1":{"1":3,"2":4,"3":5,"4":6,"5":7,"6":8,"7":9,"8":10,"9":11,"10":12,"11":13,"12":14,"13":15,"14":16,"15":17,"16":18,"17":19,"18":20,"19":21,"20":22,"21":23,"22":24,"23":25,"24":26,"25":27,"26":28,"27":29,"28":30,"29":31,"30":32,"31":33,"32":34,"33":35,"34":36,"35":37,"36":38,"37":39,"38":40,"39":41,"40":42,"41":43,"42":44,"43":45,"44":46,"45":47,"46":48,"47":49,"48":50,"49":51,"50":52,"51":53,"52":54,"53":55,"54":56,"55":57,"56":58,"57":59,"58":60,"59":61,"60":62},"2":{"1":4,"2":2,"3":1,"4":3,"5":6},"3":{"1":3,"2":2,"3":1,"4":4,"5":6},"4":{"1":19,"2":17,"3":10,"4":14,"5":13,"6":15,"7":11,"8":16,"9":18,"10":12,"11":4,"12":2,"13":5,"14":7,"15":8,"16":9}}
1 change: 1 addition & 0 deletions Chroma/LightIDTables/CrabRaveEnvironment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"0":{"1":3,"2":5,"3":11,"4":9,"5":12,"6":8,"7":10,"8":4},"1":{"1":3,"2":11,"3":8,"4":10,"5":6,"6":4,"7":9},"2":{"1":3,"2":1,"3":2,"4":4,"5":5},"3":{"1":5,"2":4,"3":2,"4":1,"5":3},"4":{"1":16,"2":18,"3":10,"4":9,"5":1,"6":11,"7":7,"8":0,"9":13,"10":19,"11":20,"12":17,"13":6,"14":4}}

0 comments on commit a8fc978

Please sign in to comment.