From 428a58cf4a751921ccaf3d5a9533ad6fe669b4be Mon Sep 17 00:00:00 2001 From: Cheesenibbles123 <56054349+cheesenibbles123@users.noreply.github.com> Date: Wed, 31 Mar 2021 03:48:57 +0100 Subject: [PATCH] Patch to fix delay --- FlagReplacement/Mainmod.cs | 120 +++++++++++++++++++++++++------------ 1 file changed, 83 insertions(+), 37 deletions(-) diff --git a/FlagReplacement/Mainmod.cs b/FlagReplacement/Mainmod.cs index d1d07ac..be54fd4 100644 --- a/FlagReplacement/Mainmod.cs +++ b/FlagReplacement/Mainmod.cs @@ -18,6 +18,13 @@ public class Mainmod : MonoBehaviour static Dictionary flags = new Dictionary(); + static Dictionary ships = new Dictionary(); + + static bool hasNotSetNavy = true; + static bool hasNotSetPirate = true; + static Texture navyFlag; + static Texture pirateFlag; + public static Mainmod Instance; void Awake() @@ -50,6 +57,11 @@ void initFiles() streamWriter.Close(); } + if (!File.Exists(Application.dataPath + texturesFilePath)) + { + Directory.CreateDirectory(Application.dataPath + texturesFilePath); + } + initFlags(); } @@ -108,62 +120,96 @@ static void Postfix(ShipConstruction __instance, string shipType, int team, ïç } } - private IEnumerator doStuff(int team) + static void resetFlag(Renderer renderer, bool isNavy) { - Transform shipTransform = GameMode.Instance.teamParents[team]; - Renderer[] renderers = shipTransform.GetComponentsInChildren(true); - - Thread childThread = new Thread(() => threadedFlags(renderers, team)); - childThread.Start(); - yield return null; + if (isNavy) + { + renderer.material.mainTexture = navyFlag; + } + else + { + renderer.material.mainTexture = pirateFlag; + } } - static void threadedFlags(Renderer[] renderers, int teamNum) + static void loopOver(Renderer[] renderers, Texture flag, int team) { - try + foreach (Renderer renderer in renderers) { - float startTime = Time.time; - while (1 != 2) + try { - if (startTime <= Time.time - 2) + if (renderer.name == "teamflag") { - if (GameMode.Instance.teamCaptains[teamNum].steamID != 0) + debugLog($"Faction flag: -{renderer.material.mainTexture.name}-"); + + if (ships.TryGetValue(team, out bool isNavy)) + { + if (flag != null && flag.name != "NOFLAG") + { + renderer.material.mainTexture = flag; + } + else + { + resetFlag(renderer, isNavy); + } + } + else { - string steamID = GameMode.Instance.teamCaptains[teamNum].steamID.ToString(); - if (flags.TryGetValue(steamID, out Texture2D flag)) + if (renderer.material.mainTexture.name == "flag_navy") { - foreach (Renderer renderer in renderers) + ships.Add(team, true); + if (hasNotSetNavy) { - try - { - if (renderer.name == "teamflag") - { - debugLog($"Faction flag: -{renderer.material.mainTexture.name}-"); - if (flag.name != "NOFLAG") - { - renderer.material.mainTexture = flag; - } - } - } - catch (Exception e) - { - // Just ignore - } + navyFlag = renderer.material.mainTexture; + hasNotSetNavy = false; } - break; } else { - break; + ships.Add(team, false); + if (hasNotSetPirate) + { + pirateFlag = renderer.material.mainTexture; + hasNotSetPirate = false; + } + } + + if (flag != null && flag.name != "NOFLAG") + { + renderer.material.mainTexture = flag; + } + else + { + resetFlag(renderer, isNavy); } } + } } - }catch (Exception e) + catch (Exception e) + { + // Just ignore + } + } + } + + private IEnumerator doStuff(int team) + { + yield return new WaitForSeconds(4f); + + if (GameMode.Instance.teamCaptains[team].steamID != 0) { - debugLog("ERROR"); - debugLog(e.Message); - debugLog("ERROR"); + Transform shipTransform = GameMode.Instance.teamParents[team]; + Renderer[] renderers = shipTransform.GetComponentsInChildren(true); + string steamID = GameMode.Instance.teamCaptains[team].steamID.ToString(); + if (flags.TryGetValue(steamID, out Texture2D flag)) + { + loopOver(renderers, flag, team); + } + else + { + loopOver(renderers, null, team); + } } } }