Skip to content

Commit

Permalink
Add Language support
Browse files Browse the repository at this point in the history
  • Loading branch information
DestroyedClone authored and NotTsunami committed Mar 7, 2022
1 parent 04fe5aa commit dbf0d1f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
11 changes: 11 additions & 0 deletions Language/en/tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"strings":
{
"FALL_DAMAGE_PREFIX_DEATH" : "<color=#FFFFFF>Killed By:</color> <color=#964B00>Fall Damage</color>",
"GENERIC_PREFIX_DEATH" : "<color=#FFFFFF>Killed By:</color> <color=#FFFF80>{0}</color> <color=#FFFFFF>({1} damage)</color>",
"SDC_PLAYER_DEATH_FALL_DAMAGE": "<color=#00FF80>{0}</color> died to fall damage.",
"SDC_PLAYER_DEATH_FRIENDLY_CRIT": "<color=#FF0000>CRITICAL HIT!</color> <color=#00FF80>{0}</color> killed by <color=#FF8000>{1}</color> ({2} damage taken).",
"SDC_PLAYER_DEATH_FRIENDLY": "<color=#00FF80>{0}</color> killed by <color=#FF8000>{1}</color> ({2} damage taken).",
"SDC_PLAYER_DEATH": "<color=#00FF80>{0}</color> killed by <color=#FF8000>{1}</color> ({2} damage taken)."
}
}
63 changes: 43 additions & 20 deletions ShowDeathCause/ShowDeathCause.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BepInEx;
using RoR2;
using System.Collections.Generic;
using Zio;
using Zio.FileSystems;

namespace ShowDeathCause
{
Expand All @@ -8,69 +11,89 @@ public class ShowDeathCause : BaseUnityPlugin
{
// These members are added to avoid trying to later access a GameObject that doesn't exist
private static DamageReport _damageReport;
private static string _friendlyAttacker;
private static string _damageTaken;
private static string _attacker;

// FileSystem for ZIO
public static FileSystem FileSystem { get; private set; }

public void Awake()
{
// This adds in support for multiple languages
// R2API offers LanguageAPI but we want to remain compatible with vanilla, thus use ZIO
PhysicalFileSystem physicalFileSystem = new PhysicalFileSystem();
var assemblyDir = System.IO.Path.GetDirectoryName(Info.Location);
FileSystem = new SubFileSystem(physicalFileSystem, physicalFileSystem.ConvertPathFromInternal(assemblyDir));

if (FileSystem.DirectoryExists("/Language/"))
{
Language.collectLanguageRootFolders += delegate (List<DirectoryEntry> list)
{
list.Add(FileSystem.GetDirectoryEntry("/Language/"));
};
}

// Subscribe to the pre-existing event, we were being a bad boy and hooking onto the GlobalEventManager before
GlobalEventManager.onCharacterDeathGlobal += (damageReport) =>
{
// This should never happen, but protect against it just in case
if (damageReport == null) return;
string victimUserName = damageReport.victimMaster.playerCharacterMasterController.networkUser.userName;
string reportToken;
NetworkUser networkUser = damageReport.attackerMaster.playerCharacterMasterController.networkUser;
if (!networkUser) return;
_damageReport = damageReport;
_damageTaken = $"{damageReport.damageInfo.damage:F2}";
string reportToken;
if (damageReport.isFallDamage)
{
// Fall damage is fatal when HP <=1 or when Artifact of Frailty is active
reportToken = $"<color=#00FF80>{victimUserName}</color> died to fall damage.";
reportToken = "SDC_PLAYER_DEATH_FALL_DAMAGE";
}
else if (damageReport.isFriendlyFire)
{
// Friendly fire is possible through the Artifact of Chaos or other mods
// Compatibility with other mods is untested, but shouldn't break
_friendlyAttacker = damageReport.attackerMaster.playerCharacterMasterController.networkUser
.userName;
reportToken = damageReport.damageInfo.crit
? $"<color=#FF0000>CRITICAL HIT!</color> <color=#00FF80>{victimUserName}</color> killed by <color=#FF8000>{_friendlyAttacker}</color> ({damageReport.damageInfo.damage:F2} damage taken)."
: $"<color=#00FF80>{victimUserName}</color> killed by <color=#FF8000>{_friendlyAttacker}</color> ({damageReport.damageInfo.damage:F2} damage taken).";
_attacker = networkUser.userName;
reportToken = damageReport.damageInfo.crit ? $"SDC_PLAYER_DEATH_FRIENDLY_CRIT" : $"SDC_PLAYER_DEATH_FRIENDLY";
}
else
{
// Standard code path, GetBestBodyName replaces the need for a check against damageReport.attackerBody
_attacker = Util.GetBestBodyName(damageReport.attackerBody.gameObject);
reportToken = $"<color=#00FF80>{victimUserName}</color> killed by <color=#FF8000>{_attacker}</color> ({damageReport.damageInfo.damage:F2} damage taken).";
reportToken = "SDC_PLAYER_DEATH";
}
Chat.SendBroadcastChat(new Chat.SimpleChatMessage {baseToken = reportToken});
Chat.SendBroadcastChat(new Chat.SimpleChatMessage {
baseToken = reportToken,
paramTokens = new[] { networkUser.userName, _attacker, _damageTaken }
});
};

// This upgrades the game end panel to show damage numbers and be more concise
On.RoR2.UI.GameEndReportPanelController.SetPlayerInfo += (orig, self, playerInfo) =>
{
orig(self, playerInfo);
// Do nothing if the damage report is unset
// This should never happen, but leave original text in the case the report is null
if (_damageReport == null) return;
// Override the string for killerBodyLabel ("Killed By: <killer>" on the end game panel)
// Override the string for killerBodyLabel ("Killed By: <killer>" on the end game report panel)
string labelToken;
if (_damageReport.isFallDamage)
{
labelToken = "<color=#FFFFFF>Killed By:</color> <color=#964B00>Fall Damage</color>";
labelToken = "FALL_DAMAGE_PREFIX_DEATH";
}
else if (_damageReport.isFriendlyFire)
{
labelToken = $"<color=#FFFFFF>Killed By:</color> <color=#FFFF80>{_friendlyAttacker}</color> <color=#FFFFFF>({_damageReport.damageInfo.damage:F2} damage)</color>";
labelToken = $"GENERIC_PREFIX_DEATH";
}
else
{
labelToken = $"<color=#FFFFFF>Killed By:</color> <color=#FFFF80>{_attacker}</color> <color=#FFFFFF>({_damageReport.damageInfo.damage:F2} damage)</color>";
labelToken = $"GENERIC_PREFIX_DEATH";
}
self.killerBodyLabel.text = labelToken;
self.killerBodyLabel.text = Language.GetStringFormatted(labelToken, _attacker, _damageTaken);
};
}
}
Expand Down
3 changes: 3 additions & 0 deletions ShowDeathCause/ShowDeathCause.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<Reference Include="UnityEngine.Networking">
<HintPath>./libs/UnityEngine.Networking.dll</HintPath>
</Reference>
<Reference Include="Zio">
<HintPath>libs\Zio.dll</HintPath>
</Reference>
</ItemGroup>

</Project>
Binary file added ShowDeathCause/libs/Zio.dll
Binary file not shown.

0 comments on commit dbf0d1f

Please sign in to comment.