using System.Reflection;
using HutongGames.PlayMaker;
using HutongGames.PlayMaker.Actions;
using MSCLoader;
using UnityEngine;
namespace MSCO
{
/// Hilfsfunktionen rund um Hauptmenü und das Laden der Spielszene.
public static class MenuHelper
{
public const string MenuScene = "MainMenu";
public const string GameScene = "GAME";
public static bool InMenu { get { return Application.loadedLevelName == MenuScene; } }
public static bool InGame { get { return Application.loadedLevelName == GameScene; } }
static readonly MethodInfo switchState =
typeof(Fsm).GetMethod("SwitchState", BindingFlags.NonPublic | BindingFlags.Instance);
///
/// Sucht im Hauptmenü die PlayMaker-FSM des CONTINUE-Buttons und den Zustand,
/// der per LoadLevel die Szene "GAME" lädt.
///
public static bool FindContinueLoadState(out PlayMakerFSM fsm, out FsmState state)
{
fsm = null;
state = null;
foreach (Object o in Object.FindObjectsOfType(typeof(PlayMakerFSM)))
{
var candidate = (PlayMakerFSM)o;
if (!HierarchyContains(candidate.transform, "continue")) continue;
foreach (FsmState s in candidate.FsmStates)
{
if (!s.ActionsLoaded) s.LoadActions();
if (s.Actions == null) continue;
foreach (FsmStateAction action in s.Actions)
{
var load = action as LoadLevel;
if (load != null && load.levelName != null && load.levelName.Value == GameScene)
{
fsm = candidate;
state = s;
return true;
}
}
}
}
return false;
}
/// Startet das Spiel wie ein Klick auf CONTINUE.
public static void StartGame()
{
PlayMakerFSM fsm;
FsmState state;
if (switchState != null && FindContinueLoadState(out fsm, out state))
{
ModConsole.Print("[MSCO] Loading via CONTINUE (" + fsm.name + " / " + state.Name + ")");
switchState.Invoke(fsm.Fsm, new object[] { state });
}
else
{
ModConsole.Warning("[MSCO] CONTINUE button not found, loading GAME directly");
Application.LoadLevel(GameScene);
}
}
static bool HierarchyContains(Transform t, string text)
{
for (; t != null; t = t.parent)
if (t.name.ToLower().Contains(text)) return true;
return false;
}
}
}