Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for mods to interact with other mods' preloads #147

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Assembly-CSharp/IMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public interface IMod : ILogger
/// <returns>List of tuples containg scene names and the respective actions.</returns>
(string, Func<IEnumerator>)[] PreloadSceneHooks();

/// <summary>
/// This function will be invoked on each gameObject preloaded through the <see cref="GetPreloadNames"/> system.
/// </summary>
/// <param name="go">The preloaded gameObject.</param>
/// <param name="sceneName">The scene the gameObject was preloaded from.</param>
/// <param name="goName">The path to the preloaded gameObject.</param>
void InvokeOnGameObjectPreloaded(GameObject go, string sceneName, string goName);


/// <summary>
/// Called after preloading of all mods.
/// </summary>
Expand Down
10 changes: 9 additions & 1 deletion Assembly-CSharp/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private string GetGlobalSettingsPath()
Log("Overriding Global Settings path with Mod directory");
return globalSettingsOverride;
}

return Path.Combine(Application.persistentDataPath, globalSettingsFileName);
}

Expand Down Expand Up @@ -148,6 +148,14 @@ public string GetName()
/// <returns>List of tuples containg scene names and the respective actions.</returns>
public virtual (string, Func<IEnumerator>)[] PreloadSceneHooks() => Array.Empty<(string, Func<IEnumerator>)>();

/// <summary>
/// This function will be invoked on each gameObject preloaded through the <see cref="GetPreloadNames"/> system.
/// </summary>
/// <param name="go">The preloaded gameObject.</param>
/// <param name="sceneName">The scene the gameObject was preloaded from.</param>
/// <param name="goName">The path to the preloaded gameObject.</param>
public virtual void InvokeOnGameObjectPreloaded(GameObject go, string sceneName, string goName) { }

/// <inheritdoc />
/// <summary>
/// Called after preloading of all mods.
Expand Down
19 changes: 19 additions & 0 deletions Assembly-CSharp/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ Assembly Resolve(object sender, ResolveEventArgs args)
yield return pld.Preload(toPreload, preloadedObjects, sceneHooks);
}

foreach ((string sceneName, Dictionary<string, GameObject> goMap) in preloadedObjects.Values.SelectMany(x => x))
{
foreach ((string goName, GameObject go) in goMap)
{
OnPreloadedObject(go, sceneName, goName);
}
}

foreach (ModInstance mod in orderedMods)
{
if (mod.Error is not null)
Expand Down Expand Up @@ -466,6 +474,17 @@ internal static void UnloadMod(ModInstance mod, bool updateModText = true)
if (updateModText) UpdateModText();
}

public static void OnPreloadedObject(GameObject go, string sceneName, string goName)
{
foreach (ModInstance modInstance in ModInstances)
{
if (modInstance.Error is not null)
continue;

modInstance.Mod.InvokeOnGameObjectPreloaded(go, sceneName, goName);
}
}

// Essentially the state of a loaded **mod**. The assembly has nothing to do directly with mods.
public class ModInstance
{
Expand Down