The errors you're encountering (CS0123) indicate that the delegate signatures for the event handlers OnGameAwake and OnGameShutdown do not match the expected delegate type defined by ModEvents.ModEventHandlerDelegate. This is a common issue when working with event systems in C#, especially in modding frameworks like the one you're using (likely for a game such as 7 Days to Die).
Problem Analysis
The error message suggests that the RegisterHandler method expects a delegate with a specific signature, but the methods you're passing (OnGameAwake and OnGameShutdown) do not match that signature. Based on the code, your methods return bool, but the delegate might not expect a return value (i.e., it might expect void instead of bool), or there might be a mismatch in the parameter types.
The expected delegate type is likely something like this (based on typical modding frameworks):
public delegate void ModEventHandlerDelegate<T>(T data);
If this is the case, your methods should return void instead of bool.
Solution
To fix the issue, you need to ensure that the signatures of OnGameAwake and OnGameShutdown match the expected delegate type. Here’s how you can fix it:
- Change the return type of the event handler methods from
bool to void.
- Update the event registration to use the correct delegate type.
Here’s the corrected code for the ModInit class:
using HarmonyLib;
using UnityEngine;
using System.Collections.Generic;
namespace CustomRadioPlayer
{
public class ModInit : IModApi
{
public void InitMod(Mod _modInstance)
{
Debug.Log("[CustomRadioPlayer] Initializing Custom Radio Player Mod...");
var harmony = new Harmony("com.customradioplayer.patch");
harmony.PatchAll();
GameObject musicObject = new GameObject("CustomRadioPlayer");
UnityEngine.Object.DontDestroyOnLoad(musicObject);
musicObject.AddComponent<RadioManager>();
musicObject.AddComponent<RadioAudioManager>();
musicObject.AddComponent<MusicLoader>();
RadioManager.musicObject = musicObject;
// Register event handlers with the correct delegate signature
ModEvents.GameAwake.RegisterHandler(OnGameAwake);
ModEvents.GameShutdown.RegisterHandler(OnGameShutdown);
if (RadioManager.Config.debugLogging)
Debug.Log("[CustomRadioPlayer] Mod initialized successfully");
}
// Change return type to void to match the expected delegate signature
private static void OnGameAwake(ModEvents.SGameAwakeData _data)
{
if (RadioManager.Config.debugLogging)
Debug.Log("[CustomRadioPlayer] GameAwake: Ensuring mod components are set up");
if (RadioManager.musicObject == null)
{
RadioManager.musicObject = new GameObject("CustomRadioPlayer");
UnityEngine.Object.DontDestroyOnLoad(RadioManager.musicObject);
RadioManager.musicObject.AddComponent<RadioManager>();
RadioManager.musicObject.AddComponent<RadioAudioManager>();
RadioManager.musicObject.AddComponent<MusicLoader>();
}
var commands = new SortedList<string, IConsoleCommand>();
SdtdConsole.Instance.RegisterCommand(commands, "radio", new ConsoleCmdRadio());
SdtdConsole.Instance.RegisterCommand(commands, "rrescan", new ConsoleCmdRescan());
SdtdConsole.Instance.RegisterCommand(commands, "rplaylist", new ConsoleCmdPlaylist());
SdtdConsole.Instance.RegisterCommand(commands, "rinfo", new ConsoleCmdTrackInfo());
SdtdConsole.Instance.RegisterCommand(commands, "rscanplaylists", new ConsoleCmdScanPlaylists());
}
// Change return type to void to match the expected delegate signature
private static void OnGameShutdown(ModEvents.SGameShutdownData _data)
{
if (RadioManager.Config.debugLogging)
Debug.Log("[CustomRadioPlayer] GameShutdown: Cleaning up mod resources");
RadioAudioManager.StopMusic(true);
RadioManager.SetRadioActive(false);
RadioManager.SetActiveRadioPos(null);
foreach (var source in RadioManager.radioSources.Values)
{
if (source != null)
UnityEngine.Object.Destroy(source.gameObject);
}
RadioManager.radioSources.Clear();
RadioManager.PlacedRadios.Clear();
if (RadioManager.musicObject != null)
{
UnityEngine.Object.Destroy(RadioManager.musicObject);
RadioManager.musicObject = null;
}
}
}
// The rest of your code (ConsoleCmdRadio, ConsoleCmdRescan, etc.) remains unchanged
}