Skip to content

Commit

Permalink
Merge pull request #43 from zawedcvg/fix-role-changes
Browse files Browse the repository at this point in the history
Closes issue #11 Adds role changes to the server logs
  • Loading branch information
data-bomb authored Jun 16, 2024
2 parents d00e3d3 + 298c738 commit ebe711e
Showing 1 changed file with 31 additions and 60 deletions.
91 changes: 31 additions & 60 deletions Si_Logging/Si_Logging.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ You should have received a copy of the GNU General Public License
using System.IO;
using System.Text;

[assembly: MelonInfo(typeof(HL_Logging), "Half-Life Logger", "1.2.8", "databomb&zawedcvg", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(HL_Logging), "Half-Life Logger", "1.2.9", "databomb&zawedcvg", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand Down Expand Up @@ -170,9 +170,11 @@ public override void OnInitializeMelon()
}
}

#if NET6_0
public override void OnLateInitializeMelon()
{
//subscribing to the event
Event_Roles.OnRoleChanged += OnRoleChanged;
#if NET6_0
bool QListLoaded = RegisteredMelons.Any(m => m.Info.Name == "QList");
if (!QListLoaded)
{
Expand All @@ -183,11 +185,11 @@ public override void OnLateInitializeMelon()

QList.OptionTypes.BoolOption logDamage = new(Pref_Log_Damage, Pref_Log_Damage.Value);
QList.OptionTypes.BoolOption logAllKills = new(Pref_Log_Kills_Include_AI_vs_Player, Pref_Log_Kills_Include_AI_vs_Player.Value);

QList.Options.AddOption(logDamage);

QList.Options.AddOption(logAllKills);
#endif
}
#endif

// 003. Change Map
public override void OnSceneWasLoaded(int buildIndex, string sceneName)
Expand Down Expand Up @@ -441,69 +443,38 @@ public static void Postfix(MP_Strategy __instance, Player __0, Team __1, Team __
}

// 055. Role Selection - Commander Change
#if NET6_0
[HarmonyPatch(typeof(MP_Strategy), nameof(MP_Strategy.SetCommander))]
#else
[HarmonyPatch(typeof(MP_Strategy), "SetCommander")]
#endif
private static class ApplyPatch_MP_Strategy_SetCommander
public void OnRoleChanged(object? sender, OnRoleChangedArgs args)
{
public static void Postfix(MP_Strategy __instance, Team __0, Player __1)
try
{
try
{
if (__0 == null || lastCommander == null)
{
return;
}

#pragma warning disable CS8602, CS8604 // Dereference of a possibly null reference.
if (__1 != null)
{
// is it the same as what we already captured?
if (__1 == lastCommander[__0.Index])
{
return;
}

// a change occurred and this player was promoted
int commanderUserID = Math.Abs(__1.GetInstanceID());
string LogLine = "\"" + __1.PlayerName + "<" + commanderUserID + "><" + GetPlayerID(__1) + "><" + __0.TeamShortName + ">\" changed role to \"Commander\"";
PrintLogLine(LogLine);
if (args == null) return;

// check if another player was demoted
if (lastCommander[__0.Index] != null)
{
// this player is no longer commander
int prevCommanderUserID = Math.Abs(lastCommander[__0.Index].GetInstanceID());
LogLine = "\"" + lastCommander[__0.Index].PlayerName + "<" + prevCommanderUserID + "><" + GetPlayerID(lastCommander[__0.Index]) + "><" + __0.TeamShortName + ">\" changed role to \"Infantry\"";
PrintLogLine(LogLine);
}

lastCommander[__0.Index] = __1;
}
else
{
// is it the same as what we already captured?
if (lastCommander[__0.Index] == null)
{
return;
}

// a change occurred and this player is no longer commander
int prevCommanderUserID = Math.Abs(lastCommander[__0.Index].GetInstanceID());
string LogLine = "\"" + lastCommander[__0.Index].PlayerName + "<" + prevCommanderUserID + "><" + GetPlayerID(lastCommander[__0.Index]) + "><" + __0.TeamShortName + ">\" changed role to \"Infantry\"";
PrintLogLine(LogLine);
Player player = args.Player;

if (player == null || player.Team == null) return;
int userID = Math.Abs(player.GetInstanceID());
string role;

lastCommander[__0.Index] = null;
}
#pragma warning restore CS8602, CS8604 // Dereference of a possibly null reference.
if (args.Role == MP_Strategy.ETeamRole.COMMANDER)
{
role = "Commander";
}
catch (Exception error)
else if (args.Role == MP_Strategy.ETeamRole.INFANTRY)
{
role = "Infantry";
} else
{
HelperMethods.PrintError(error, "Failed to run MP_Strategy::SetCommander");
role = "None";
}

string LogLine = "\"" + player.PlayerName + "<" + userID + "><" + GetPlayerID(player) + "><" + player.Team.TeamShortName + ">\" changed role to \"" + role + "\"";
PrintLogLine(LogLine);
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run OnRoleChanged");
}

}

// 056. Change Name
Expand Down Expand Up @@ -965,4 +936,4 @@ public static void Postfix(Silica.UI.Chat __instance, Player __0, string __1, bo
// 069. Weapon Pickup
// None for now. Re-evaluate with updates
}
}
}

0 comments on commit ebe711e

Please sign in to comment.