Advertisement
Guest User

Assets.Scripts.Mod (SR2TestMod)

a guest
Jun 25th, 2019
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.33 KB | None | 0 0
  1. namespace Assets.Scripts
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using Assets.Scripts.Ui;
  8.     using ModApi.Common;
  9.     using ModApi.Scenes;
  10.     using ModApi.Ui;
  11.     using ModApi.Ui.Events;
  12.     using UnityEngine;
  13.  
  14.     /// <summary>
  15.     /// A singleton object representing this mod that is instantiated and initialize when the mod is loaded.
  16.     /// </summary>
  17.     public class Mod : ModApi.Mods.GameMod
  18.     {
  19.         /// <summary>
  20.         /// Prevents a default instance of the <see cref="Mod"/> class from being created.
  21.         /// </summary>
  22.         private Mod() : base()
  23.         {
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Gets the singleton instance of the mod object.
  28.         /// </summary>
  29.         /// <value>The singleton instance of the mod object.</value>
  30.         public static Mod Instance { get; } = GetModInstance<Mod>();
  31.  
  32.         /// <summary>
  33.         /// Logs the specified message.
  34.         /// </summary>
  35.         /// <param name="message">The message to log.</param>
  36.         /// <param name="context">The context.</param>
  37.         public void Log(string message, UnityEngine.Object context = null)
  38.         {
  39.             Debug.Log($"{this.ModInfo.Name}: {message}", context);
  40.         }
  41.  
  42.         /// <summary>
  43.         /// Logs the specified error.
  44.         /// </summary>
  45.         /// <param name="error">The error to log.</param>
  46.         /// <param name="context">The context.</param>
  47.         public void LogError(string error, UnityEngine.Object context = null)
  48.         {
  49.             Debug.LogError($"{this.ModInfo.Name}: {error}", context);
  50.         }
  51.  
  52.         /// <summary>
  53.         /// Logs the specified warning.
  54.         /// </summary>
  55.         /// <param name="warning">The warning to log.</param>
  56.         /// <param name="context">The context.</param>
  57.         public void LogWarning(string warning, UnityEngine.Object context = null)
  58.         {
  59.             Debug.LogWarning($"{this.ModInfo.Name}: {warning}", context);
  60.         }
  61.  
  62.         /// <summary>
  63.         /// Called when the mod is initialized.
  64.         /// </summary>
  65.         protected override void OnModInitialized()
  66.         {
  67.             Debug.Log($"Mod Initialized: {this.ModInfo.Name} - {this.ModInfo.Author} - {this.ModInfo.Version} - {this.ModInfo.LastUpdated}");
  68.  
  69.             Game.Instance.SceneManager.SceneLoaded += this.OnSceneLoaded;
  70.  
  71.             var ui = Game.Instance.UserInterface;
  72.             ui.UserInterfaceLoading += this.OnUserInterfaceLoading;
  73.             ui.UserInterfaceLoaded += this.OnUserInterfaceLoaded;
  74.             ui.AddBuildUserInterfaceXmlAction(this.BuildAllUserInterfaceXml);
  75.             ui.AddBuildUserInterfaceXmlAction(UserInterfaceIds.Flight.FlightSceneUI, this.BuildFlightSceneUserInterfaceXml);
  76.             ui.AddBuildUserInterfaceXmlAction(UserInterfaceIds.Flight.StagingPanel, this.BuildFlightSceneStagingPanelUserInterfaceXml);
  77.         }
  78.  
  79.         /// <summary>
  80.         /// Called when every XML based user interface is being built.
  81.         /// </summary>
  82.         /// <param name="request">The build user interface XML request.</param>
  83.         private void BuildAllUserInterfaceXml(BuildUserInterfaceXmlRequest request)
  84.         {
  85.             this.Log($"Build all user interfaces XML callback: {request.UserInterfaceId}");
  86.             this.UpdateUIText(request, "SETTINGS", "*SETTINGS*");
  87.             this.UpdateUIText(request, "#STAGE#", "*STAGE*");
  88.         }
  89.  
  90.         /// <summary>
  91.         /// Called when the flight scene staging panel XML based user interface is being built.
  92.         /// </summary>
  93.         /// <param name="request">The build user interface XML request.</param>
  94.         private void BuildFlightSceneStagingPanelUserInterfaceXml(BuildUserInterfaceXmlRequest request)
  95.         {
  96.             this.Log($"Build flight scene staging panel user interface XML callback: {request.UserInterfaceId}");
  97.             this.UpdateUIText(request, "STAGE", "#STAGE#");
  98.         }
  99.  
  100.         /// <summary>
  101.         /// Called when the flight scene XML based user interface is being built.
  102.         /// </summary>
  103.         /// <param name="request">The build user interface XML request.</param>
  104.         private void BuildFlightSceneUserInterfaceXml(BuildUserInterfaceXmlRequest request)
  105.         {
  106.             this.Log($"Build flight scene user interface XML callback: {request.UserInterfaceId}");
  107.             this.UpdateUIText(request, "RELAUNCH", "*RELAUNCH*");
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Called when the scene is loaded.
  112.         /// </summary>
  113.         /// <param name="sender">The sender.</param>
  114.         /// <param name="e">The <see cref="ModApi.Scenes.Events.SceneEventArgs"/> instance containing the event data.</param>
  115.         private void OnSceneLoaded(object sender, ModApi.Scenes.Events.SceneEventArgs e)
  116.         {
  117.             if (e.Scene == SceneNames.Flight)
  118.             {
  119.                 GameObject.Instantiate(this.ResourceLoader.LoadAsset<GameObject>("Assets/Content/TestSphere.prefab"));
  120.             }
  121.             else if (e.Scene == SceneNames.Menu)
  122.             {
  123.                 var menuButtonScript = Game.Instance.UserInterface.BuildUserInterfaceFromResource<MainMenuModButtonScript>(
  124.                     "SR2TestMod/Xml/MainMenuModButton",
  125.                     (s, c) => s.OnLayoutRebuilt(c.XmlLayout),
  126.                     Game.Instance.UserInterface.Transform.Find("GameMenu"));
  127.                 menuButtonScript.transform.SetAsFirstSibling();
  128.             }
  129.         }
  130.  
  131.         /// <summary>
  132.         /// Called when a user interface is loaded.
  133.         /// </summary>
  134.         /// <param name="sender">The sender.</param>
  135.         /// <param name="e">The <see cref="UserInterfaceLoadedEventArgs"/> instance containing the event data.</param>
  136.         private void OnUserInterfaceLoaded(object sender, UserInterfaceLoadedEventArgs e)
  137.         {
  138.             this.Log($"User Interface Loaded: {e.UserInterfaceId}");
  139.         }
  140.  
  141.         /// <summary>
  142.         /// Called when a user interface is loading.
  143.         /// </summary>
  144.         /// <param name="sender">The sender.</param>
  145.         /// <param name="e">The <see cref="ModApi.Ui.Events.UserInterfaceLoadingEventArgs"/> instance containing the event data.</param>
  146.         private void OnUserInterfaceLoading(object sender, ModApi.Ui.Events.UserInterfaceLoadingEventArgs e)
  147.         {
  148.             this.Log($"User Interface Loading: {e.UserInterfaceId}");
  149.             this.UpdateUIText(e.BuildXmlRequest, "END FLIGHT", "*END FLIGHT*");
  150.         }
  151.  
  152.         /// <summary>
  153.         /// Updates a UI text element, replacing one text string with another.
  154.         /// </summary>
  155.         /// <param name="buildXmlRequest">The build XML request.</param>
  156.         /// <param name="originalText">The original text.</param>
  157.         /// <param name="newText">The new text.</param>
  158.         private void UpdateUIText(BuildUserInterfaceXmlRequest buildXmlRequest, string originalText, string newText)
  159.         {
  160.             foreach (var element in buildXmlRequest.XmlDocument.Root.Descendants(XmlLayoutConstants.XmlNamespace + "TextMeshPro"))
  161.             {
  162.                 var attribute = element.Attribute("text");
  163.                 if (attribute != null && attribute.Value == originalText)
  164.                 {
  165.                     attribute.SetValue(newText);
  166.                 }
  167.             }
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement