Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix Room Logic for FlickerableLight API #350

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading