Skip to content

Commit

Permalink
Feature: Support NCMS mod to use IConfigurable and IReloadable
Browse files Browse the repository at this point in the history
  • Loading branch information
inmny committed Jan 27, 2024
1 parent 17ba24c commit d9abdd8
Showing 1 changed file with 40 additions and 37 deletions.
77 changes: 40 additions & 37 deletions ui/ModListWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ public override void Setup(IMod mod)
TipButton icon_tip_button = icon.GetComponent<TipButton>();

icon.sprite = sprite;
configure_button.gameObject.SetActive(mod is IConfigurable);
var configurable = mod.GetGameObject()?.GetComponent<IConfigurable>();
configure_button.gameObject.SetActive(configurable != null);

icon.GetComponent<Button>().onClick.RemoveAllListeners();
configure_button.onClick.RemoveAllListeners();
Expand Down Expand Up @@ -352,50 +353,52 @@ public override void Setup(IMod mod)
configure_button.onClick.AddListener(() =>
{
// It can be sure that if mod is IConfigurable, then mod is loaded actually.
if (mod is IConfigurable configurable)
{
ModConfigureWindow.ShowWindow(configurable.GetConfig());
}
ModConfigureWindow.ShowWindow(configurable?.GetConfig());
});
website_button.onClick.AddListener(() => { Application.OpenURL(mod.GetUrl()); });

if (Config.isEditor && mod is IReloadable reloadable)
if (!Config.isEditor)
{
Button reload_button = transform.Find("Reload").GetComponent<Button>();
reload_button.gameObject.SetActive(true);
reload_button.onClick.RemoveAllListeners();
reload_button.onClick.AddListener(() =>
{
if (!ModReloadUtils.Prepare(reloadable, mod_declare))
{
LogService.LogWarning($"Failed to prepare mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.CompileNew())
{
LogService.LogWarning($"Failed to compile new mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.PatchHotfixMethods())
{
LogService.LogWarning(
$"Failed to patch hotfix methods of mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.Reload())
{
LogService.LogWarning($"Failed to reload mod {mod_declare.Name}.");
return;
}
});
transform.Find("Reload").gameObject.SetActive(false);
return;
}
else

var reloadable = mod.GetGameObject()?.GetComponent<IReloadable>();
if (reloadable == null)
{
transform.Find("Reload").gameObject.SetActive(false);
return;
}

var reload_button = transform.Find("Reload").GetComponent<Button>();
reload_button.gameObject.SetActive(true);
reload_button.onClick.RemoveAllListeners();
reload_button.onClick.AddListener(() =>
{
if (!ModReloadUtils.Prepare(reloadable, mod_declare))
{
LogService.LogWarning($"Failed to prepare mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.CompileNew())
{
LogService.LogWarning($"Failed to compile new mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.PatchHotfixMethods())
{
LogService.LogWarning(
$"Failed to patch hotfix methods of mod {mod_declare.Name} for reloading.");
return;
}

if (!ModReloadUtils.Reload())
{
LogService.LogWarning($"Failed to reload mod {mod_declare.Name}.");
}
});
}
}
}

0 comments on commit d9abdd8

Please sign in to comment.