Skip to content

Commit

Permalink
Fixed crash when trying to log something if the console UI was not ye…
Browse files Browse the repository at this point in the history
…t loaded, also fixed crash when getting/setting modded settings values, it now gets the owner mod by checking what mod the calling method belongs to. Also (hopefully) fixed the bug where the mod settings window would occasionally lock the players cursor on screen, unable to do anything.
  • Loading branch information
Goorakh committed Jul 31, 2021
1 parent 9a0d1fd commit 1de820a
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 188 deletions.
2 changes: 1 addition & 1 deletion Mod Bot/Internal/CloseModOptionsWindowOnEscapeKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
_owner.ForceCloseWindow();
_owner.CloseWindow();
Destroy(this);
}
}
Expand Down
14 changes: 14 additions & 0 deletions Mod Bot/Internal/InternalUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Reflection;
using UnityEngine;
using ModLibrary;

namespace InternalModBot
{
Expand Down Expand Up @@ -67,6 +68,19 @@ public static string GetCallerModPath(int methodsAbove=1)
return GetModFolderRootFromAssemblyPath(type.Assembly.Location);
}

public static Mod GetCallerModInstance(int methodsAbove = 1)
{
StackFrame frame = new StackFrame(1 + methodsAbove);
MethodBase method = frame.GetMethod();
Type type = method.DeclaringType;

LoadedModInfo loadedModInfo = ModsManager.Instance.GetLoadedModInstanceForAssembly(type.Assembly);
if (loadedModInfo != null)
return loadedModInfo.ModReference;

return null;
}

/// <summary>
/// Gets the full path from a path relative to the clone drone folder
/// </summary>
Expand Down
50 changes: 0 additions & 50 deletions Mod Bot/Internal/LoadedMod.cs

This file was deleted.

16 changes: 16 additions & 0 deletions Mod Bot/Internal/ModBotHarmonyInjectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,22 @@ public static Vector3 Character_GetPositionToDeflectArrowsAt_Postfix(Vector3 __r

return __result;
}

public static bool EscMenu_Hide_Prefix()
{
if (GameUIRoot.Instance.GetPrivateField<bool>("_isEscMenuDisabled"))
return false;

return true;
}

public static bool EscMenu_Show_Prefix()
{
if (GameUIRoot.Instance.GetPrivateField<bool>("_isEscMenuDisabled"))
return false;

return true;
}
}
}
}
7 changes: 6 additions & 1 deletion Mod Bot/Internal/OptionsSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@ internal static void SaveToFile()

internal static void SetSetting(Mod owner, string providedSaveID, object value, bool writeToFile)
{
string saveID = getSaveIDForSetting(owner, providedSaveID);
if (string.IsNullOrEmpty(providedSaveID))
throw new ArgumentException("The ID of the setting cannot be null or empty", nameof(providedSaveID));

string saveID = getSaveIDForSetting(owner, providedSaveID);
_savedSettingsDictionary[saveID] = value;

if (writeToFile)
Expand All @@ -138,6 +140,9 @@ internal static void SetSetting(Mod owner, string providedSaveID, object value,

internal static object LoadSetting(Mod owner, string providedSaveID)
{
if (string.IsNullOrEmpty(providedSaveID))
throw new ArgumentException("The ID of the setting cannot be null or empty", nameof(providedSaveID));

string saveID = getSaveIDForSetting(owner, providedSaveID);

if (_savedSettingsDictionary.TryGetValue(saveID, out object value))
Expand Down
14 changes: 10 additions & 4 deletions Mod Bot/Internal/UI/ConsoleUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal class ConsoleUI : MonoBehaviour

Queue<Text> _lines = new Queue<Text>();

bool _isInitialized = false;

/// <summary>
/// Initialized the <see cref="ConsoleUI"/>
/// </summary>
Expand All @@ -45,6 +47,8 @@ public void Init(Animator animator, GameObject content, GameObject innerHolder,
_consoleTextElementPrefab = InternalAssetBundleReferences.ModBot.GetObject("ConsoleTextElement");

_input.onEndEdit.AddListener(OnEndEdit);

_isInitialized = true;
}

private void OnEndEdit(string arg0)
Expand All @@ -55,16 +59,17 @@ private void OnEndEdit(string arg0)

// If the edit ended because we clicked away, don't do anything extra
if (!Input.GetKeyDown(KeyCode.Return))
{
return;
}

RunCommand(_input.text);
_input.text = "";
}

void Update()
{
if (!_isInitialized)
return;

if (Input.GetKeyDown(ModBotInputManager.GetKeyCode(ModBotInputType.OpenConsole)))
Flip();
}
Expand Down Expand Up @@ -93,6 +98,9 @@ public void Log(string whatToLog)

void log(string whatToLog, string prefix = "", string postfix = "")
{
if (!_isInitialized)
return;

Text spawnedText = Instantiate(_consoleTextElementPrefab, _content.transform).GetComponent<Text>();
_lines.Enqueue(spawnedText);

Expand Down Expand Up @@ -179,7 +187,6 @@ void log(string whatToLog, string prefix = "", string postfix = "")
{
_scroll.ScrollToBottom();
}, -1f); // Run this next frame

}

/// <summary>
Expand All @@ -190,7 +197,6 @@ void log(string whatToLog, string prefix = "", string postfix = "")
public void Log(string whatToLog, Color color)
{
string colorText = ColorUtility.ToHtmlStringRGB(color);

log(whatToLog, "<color=#" + colorText + ">", "</color>");

Console.WriteLine(whatToLog);
Expand Down
1 change: 0 additions & 1 deletion Mod Bot/Mod Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<Compile Include="Internal\LevelEditor\ModBotCustomLevelEditorManager.cs" />
<Compile Include="Internal\LevelEditor\Scriptable.cs" />
<Compile Include="Internal\LineInfo.cs" />
<Compile Include="Internal\LoadedMod.cs" />
<Compile Include="Internal\ModBotHarmonyInjectionManager.cs" />
<Compile Include="Internal\ModBotInputManager.cs" />
<Compile Include="Internal\ModBotLocalizationManager.cs" />
Expand Down
5 changes: 3 additions & 2 deletions Mod Bot/ModHandling/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine;
using ModLibrary;
using InternalModBot;
using System.Reflection;

namespace ModLibrary
{
Expand All @@ -11,7 +12,6 @@ namespace ModLibrary
/// </summary>
public abstract class Mod
{

/// <summary>
/// The modinfo that goes with this Mod, this contains data about the mod name, version ect.
/// </summary>
Expand All @@ -21,9 +21,10 @@ public abstract class Mod
/// <summary>
/// Returns an ID you should use when harmony patching in this mod, this is to help mod-bot clean up patches made by this mod.
/// </summary>
//public string HarmonyID => "com.Mod-Bot.Mod." + GetUniqueID();
protected internal string HarmonyID => "com.Mod-Bot.Mod." + ModInfo.UniqueID;

internal Assembly SourceAssembly;

/// <summary>
/// Called in <see cref="Character.Start"/> if the <see cref="Character"/> is of type <see cref="FirstPersonMover"/>
/// </summary>
Expand Down
75 changes: 12 additions & 63 deletions Mod Bot/ModHandling/ModDownloading/ModDownloadInfoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class ModDownloadInfoItem : MonoBehaviour
Button _downloadButton;
Button _loadButton;

string _modDownloadUrl;

ModInfo _underlyingModInfo;

/// <summary>
Expand All @@ -45,8 +43,6 @@ public void Init(ModInfo holder)
_downloadButton = moddedObject.GetObject<Button>(4);
_loadButton = moddedObject.GetObject<Button>(5);

_modDownloadUrl = "modbot.org/api?operation=downloadMod&id=" + holder.UniqueID;

_downloadButton.onClick.AddListener(onDownloadButtonClicked);
_loadButton.onClick.AddListener(onBrowseButtonClicked);

Expand Down Expand Up @@ -78,30 +74,31 @@ void onDownloadButtonClicked()
LocalizationManager.Instance.GetTranslatedString("mod_download_confirm_no"), null,
LocalizationManager.Instance.GetTranslatedString("mod_download_confirm_yes"), delegate
{
StartCoroutine(downloadModFileAndLoadAsync(_modDownloadUrl));
StartCoroutine(downloadModFileAndLoadAsync());
});
}

void onBrowseButtonClicked()
{
Process.Start("https://modbot.org/modPreview.html?modID=" + _underlyingModInfo.UniqueID);
/*
new Generic2ButtonDialogue(ModBotLocalizationManager.FormatLocalizedStringFromID("mod_load_confirm_message", _nameDisplay.text),
LocalizationManager.Instance.GetTranslatedString("mod_load_confirm_no"), null,
LocalizationManager.Instance.GetTranslatedString("mod_load_confirm_yes"), delegate
{
StartCoroutine(downloadModBytesAndLoadAsync(_modDownloadUrl));
});
*/
}

IEnumerator downloadModFileAndLoadAsync(string url)
IEnumerator downloadModFileAndLoadAsync()
{
// If mod is already loaded, just cancel the download instead of throwing an exception
if (ModsManager.Instance.GetLoadedModWithID(_underlyingModInfo.UniqueID) != null)
yield break;

string folderName = _underlyingModInfo.DisplayName;
foreach (char invalidCharacter in Path.GetInvalidFileNameChars())
{
throw new Exception("That mod is already installed");
folderName = folderName.Replace(invalidCharacter, '_');
}

string targetDirectory = ModsManager.Instance.ModFolderPath + folderName;
if (Directory.Exists(targetDirectory))
yield break;

UnityWebRequest webRequest = UnityWebRequest.Get("https://modbot.org/api?operation=downloadMod&id=" + _underlyingModInfo.UniqueID);

yield return webRequest.SendWebRequest();
Expand All @@ -111,61 +108,14 @@ IEnumerator downloadModFileAndLoadAsync(string url)
string tempFile = Path.GetTempFileName();
File.WriteAllBytes(tempFile, data);

string folderName = _underlyingModInfo.DisplayName;
foreach (char invalidCharacter in Path.GetInvalidFileNameChars())
{
folderName = folderName.Replace(invalidCharacter, '_');
}

string targetDirectory = ModsManager.Instance.ModFolderPath + folderName;
if (Directory.Exists(targetDirectory))
{
throw new Exception("That mod is already installed");
}
Directory.CreateDirectory(targetDirectory);
FastZip fastZip = new FastZip();
fastZip.ExtractZip(tempFile, targetDirectory, null);


ModsManager.Instance.ReloadMods();

File.Delete(tempFile);
/*
UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if(webRequest.isHttpError || webRequest.isNetworkError)
yield break;
string[] subUrls = url.Split('/');
string fileName = subUrls[subUrls.Length-1];
string path = AssetLoader.GetModsFolderDirectory() + fileName;
File.WriteAllBytes(path, webRequest.downloadHandler.data);
ModsManager.Instance.ReloadMods();
ModsPanelManager.Instance.ReloadModItems();
*/
}

IEnumerator downloadModBytesAndLoadAsync(string url)
{
yield return 0;

// Old pre 2.0 code code
/*
UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if(webRequest.isHttpError || webRequest.isNetworkError)
yield break;
ModsManager.Instance.LoadMod(webRequest.downloadHandler.data, false, out string error);
ModsPanelManager.Instance.ReloadModItems();
*/
}

}

/// <summary>
Expand All @@ -179,5 +129,4 @@ public struct ModsHolder
[JsonProperty(PropertyName = "ModInfos")]
public ModInfo[] Mods;
}

}
Loading

0 comments on commit 1de820a

Please sign in to comment.