Skip to content

Commit

Permalink
Fix Room Logic for FlickerableLight API (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 authored Dec 27, 2024
1 parent a5ebed9 commit edb6436
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
78 changes: 39 additions & 39 deletions EXILED/Exiled.API/Features/Room.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,46 @@ public void UnlockAll()
/// Factory method to create and add a <see cref="Room"/> component to a Transform.
/// We can add parameters to be set privately here.
/// </summary>
/// <param name="roomGameObject">The Game Object to attach the Room component to.</param>
internal static void CreateComponent(GameObject roomGameObject)
/// <param name="roomIdentifier">The Game Object to attach the Room component to.</param>
internal static void CreateComponent(RoomIdentifier roomIdentifier) => roomIdentifier.gameObject.AddComponent<Room>();

/// <summary>
/// Factory method to complete all element inside a Room.
/// </summary>
internal void InternalCreate()
{
roomGameObject.AddComponent<Room>().InternalCreate();
Identifier = gameObject.GetComponent<RoomIdentifier>();
RoomIdentifierToRoom.Add(Identifier, this);

Zone = FindZone(gameObject);
#if DEBUG
if (Zone is ZoneType.Unspecified)
Log.Error($"[ZONETYPE UNKNOWN] {this} Zone : {Identifier?.Zone}");
#endif
Type = FindType(gameObject);
#if DEBUG
if (Type is RoomType.Unknown)
Log.Error($"[ROOMTYPE UNKNOWN] {this} Name : {gameObject?.name} Shape : {Identifier?.Shape}");
#endif

RoomLightControllers = RoomLightControllersValue.AsReadOnly();

GetComponentsInChildren<BreakableWindow>().ForEach(component =>
{
Window window = new(component, this);
window.Room.WindowsValue.Add(window);
});

if (GetComponentInChildren<global::TeslaGate>() is global::TeslaGate tesla)
{
TeslaGate = new TeslaGate(tesla, this);
}

Windows = WindowsValue.AsReadOnly();
Doors = DoorsValue.AsReadOnly();
NearestRooms = NearestRoomsValue.AsReadOnly();
Speakers = SpeakersValue.AsReadOnly();
Cameras = CamerasValue.AsReadOnly();
}

private static RoomType FindType(GameObject gameObject)
Expand Down Expand Up @@ -500,41 +536,5 @@ private static ZoneType FindZone(GameObject gameObject)
_ => transform.position.y > 900 ? ZoneType.Surface : ZoneType.Unspecified,
};
}

private void InternalCreate()
{
Identifier = gameObject.GetComponent<RoomIdentifier>();
RoomIdentifierToRoom.Add(Identifier, this);

Zone = FindZone(gameObject);
#if DEBUG
if (Zone is ZoneType.Unspecified)
Log.Error($"[ZONETYPE UNKNOWN] {this} Zone : {Identifier?.Zone}");
#endif
Type = FindType(gameObject);
#if DEBUG
if (Type is RoomType.Unknown)
Log.Error($"[ROOMTYPE UNKNOWN] {this} Name : {gameObject?.name} Shape : {Identifier?.Shape}");
#endif

RoomLightControllers = RoomLightControllersValue.AsReadOnly();

GetComponentsInChildren<BreakableWindow>().ForEach(component =>
{
Window window = new(component, this);
window.Room.WindowsValue.Add(window);
});

if (GetComponentInChildren<global::TeslaGate>() is global::TeslaGate tesla)
{
TeslaGate = new TeslaGate(tesla, this);
}

Windows = WindowsValue.AsReadOnly();
Doors = DoorsValue.AsReadOnly();
NearestRooms = NearestRoomsValue.AsReadOnly();
Speakers = SpeakersValue.AsReadOnly();
Cameras = CamerasValue.AsReadOnly();
}
}
}
17 changes: 15 additions & 2 deletions EXILED/Exiled.Events/Patches/Generic/RoomList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
int offset = -3;
int index = newInstructions.FindIndex(i => i.Calls(Method(typeof(RoomIdUtils), nameof(RoomIdUtils.PositionToCoords)))) + offset;

// Room.CreateComponent(gameObject);
// Room.Get(gameObject).InternalCreate();
newInstructions.InsertRange(
index,
new CodeInstruction[]
{
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Component), nameof(Component.gameObject))),
new(OpCodes.Call, Method(typeof(Room), nameof(Room.CreateComponent))),
new(OpCodes.Callvirt, Method(typeof(Room), nameof(Room.FindParentRoom), new System.Type[] { typeof(GameObject) })),
new(OpCodes.Callvirt, Method(typeof(Room), nameof(Room.InternalCreate))),
});

for (int z = 0; z < newInstructions.Count; z++)
Expand All @@ -54,6 +55,18 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
}
}

/// <summary>
/// Patches <see cref="RoomIdentifier.Awake"/>.
/// </summary>
[HarmonyPatch(typeof(RoomIdentifier), nameof(RoomIdentifier.Awake))]
internal class RoomListAdd
{
private static void Postfix(RoomIdentifier __instance)
{
Room.CreateComponent(__instance);
}
}

/// <summary>
/// Patches <see cref="RoomIdentifier.OnDestroy"/>.
/// </summary>
Expand Down

0 comments on commit edb6436

Please sign in to comment.