public void HandleMessage(IServerMessageBase msg) { if (!(msg.Data is FlagBaseMsgData msgData)) { return; } switch (msgData.FlagMessageType) { case FlagMessageType.ListResponse: { var data = (FlagListResponseMsgData)msgData; for (var i = 0; i < data.FlagCount; i++) { var extendedFlagInfo = new ExtendedFlagInfo(data.FlagFiles[i]); System.ServerFlags.TryAdd(extendedFlagInfo.FlagName, extendedFlagInfo); } MainSystem.NetworkState = ClientState.FlagsSynced; } break; case FlagMessageType.FlagData: { var data = (FlagDataMsgData)msgData; var extendedFlagInfo = new ExtendedFlagInfo(data.Flag); System.ServerFlags.AddOrUpdate(extendedFlagInfo.FlagName, extendedFlagInfo, (key, existingVal) => extendedFlagInfo); } break; } }
/// <summary> /// Here we handle an unloaded flag and we load it into the game /// </summary> private void HandleFlag(ExtendedFlagInfo flagInfo) { //We have a flag with the same name! if (FlagExists(flagInfo.FlagName)) { return; } var flagTexture = new Texture2D(4, 4); if (flagTexture.LoadImage(flagInfo.FlagData)) { //Flags have names like: Squad/Flags/default flagTexture.name = flagInfo.FlagName; var textureInfo = new GameDatabase.TextureInfo(null, flagTexture, true, true, false) { name = flagInfo.FlagName }; GameDatabase.Instance.databaseTexture.Add(textureInfo); LunaLog.Log($"[LMP]: Loaded flag {flagTexture.name}"); } else { LunaLog.LogError($"[LMP]: Failed to load flag {flagInfo.FlagName}"); } }
/// <summary> /// Here we handle an unloaded flag and we load it into the game /// </summary> private static void HandleFlag(ExtendedFlagInfo flagInfo) { if (HighLogic.CurrentGame.flagURL.Contains("LunaMultiPlayer/Flags/")) { var currentFlagName = HighLogic.CurrentGame.flagURL.Substring("LunaMultiPlayer/Flags/".Length); //If the flag name is the same as ours just skip it as otherwise we would overwrite ours if (currentFlagName == flagInfo.FlagName) { return; } } var flagTexture = new Texture2D(4, 4); if (flagTexture.LoadImage(flagInfo.FlagData)) { //Flags have names like: Squad/Flags/default or LunaMultiplayer/Flags/coolflag flagTexture.name = "LunaMultiPlayer/Flags/" + Path.GetFileNameWithoutExtension(flagInfo.FlagName); File.WriteAllBytes(flagInfo.FlagPath, flagInfo.FlagData); var textureInfo = new GameDatabase.TextureInfo(null, flagTexture, false, true, false) { name = flagTexture.name }; var textureExists = GameDatabase.Instance.databaseTexture.Any(t => t.name == textureInfo.name); if (!textureExists) { GameDatabase.Instance.databaseTexture.Add(textureInfo); } else { GameDatabase.Instance.ReplaceTexture(textureInfo.name, textureInfo); } LunaLog.Log($"[LMP]: Loaded {flagTexture.name}"); } else { LunaLog.LogError($"[LMP]: Failed to load flag {flagInfo.FlagName}"); } }