using UnityEngine;
using System.Collections.Generic;
using System.Collections;
using UnityEngine.UI;
using System.Linq;
using ModTool;
///
/// Example mod manager. This menu displays all mods and lets you enable/disable them.
///
public class ModMenu : MonoBehaviour
{
///
/// The content panel where the menu items will be parented
///
public Transform menuContentPanel;
///
/// The prefab for the mod menu item
///
public ModItem modItemPrefab;
///
/// Button that will start loading enabled mods
///
public Button loadButton;
///
/// Dictionary linking mod menu items with mods
///
private Dictionary modItems;
///
/// Are the enabled mods loaded?
///
private bool isLoaded;
void Start()
{
ModManager.AddSearchDirectory(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)+@"\My Games\Firstborn\Mods");
modItems = new Dictionary();
//Subscribe to ModManager events for keeping track of found mods
ModManager.ModFound += OnModFound;
ModManager.ModRemoved += OnModRemoved;
//Subscribe to ModManager events to keep track of loaded mods
ModManager.ModLoaded += OnModLoaded;
ModManager.ModUnloaded += OnModUnloaded;
//Refresh and look mods in mod search directories
ModManager.Refresh();
//Start refreshing ModManager to look for changes every 2 seconds
StartCoroutine(AutoRefresh(2));
Application.runInBackground = true;
}
private void OnModFound(Mod mod)
{
ModItem modItem = Instantiate(modItemPrefab, menuContentPanel);
modItem.Initialize(mod);
modItem.SetToggleInteractable(!isLoaded);
modItems.Add(mod, modItem);
}
private void OnModRemoved(Mod mod)
{
ModItem modItem;
if(modItems.TryGetValue(mod, out modItem))
{
modItems.Remove(mod);
Destroy(modItem.gameObject);
}
}
private void SetTogglesInteractable(bool interactable)
{
foreach (ModItem item in modItems.Values)
{
item.SetToggleInteractable(interactable);
}
}
///
/// Toggle load or unload all enabled mods.
///
public void LoadButton()
{
if (isLoaded)
{
Unload();
}
else
{
Load();
}
}
IEnumerator AutoRefresh(float seconds)
{
while(true)
{
ModManager.Refresh();
yield return new WaitForSeconds(seconds);
}
}
private void Load()
{
//load mods
foreach (Mod mod in modItems.Keys)
{
if(mod.isEnabled)
mod.Load();
}
SetTogglesInteractable(false);
loadButton.GetComponentInChildren().text = "U N L O A D";
isLoaded = true;
}
private void Unload()
{
//unload all mods - this will unload their scenes and destroy any associated objects
foreach (Mod mod in modItems.Keys)
{
mod.Unload();
}
SetTogglesInteractable(true);
loadButton.GetComponentInChildren().text = "L O A D";
isLoaded = false;
}
private void OnModLoaded(Mod mod)
{
Debug.Log("Loaded Mod: " + mod.name);
//load first scene (if present) when a mod is loaded
ModScene scene = mod.scenes.FirstOrDefault();
if (scene != null)
scene.Load();
}
private void OnModUnloaded(Mod mod)
{
Debug.Log("Unloaded Mod: " + mod.name);
}
}