Skip to content

Commit

Permalink
plugin changes add harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
200Tigersbloxed committed Oct 9, 2024
1 parent 025a3d0 commit c32a85e
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Hypernex.CCK.Unity/Hypernex.CCK.Unity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
</ItemGroup>
<ItemGroup>
<Content Include="Libs\HypernexSharp.dll" />
<Content Include="Libs\Lib.Harmony\LICENSE" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Libs\Lib.Harmony\0Harmony.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
Binary file added Hypernex.CCK.Unity/Libs/Lib.Harmony/0Harmony.dll
Binary file not shown.
21 changes: 21 additions & 0 deletions Hypernex.CCK.Unity/Libs/Lib.Harmony/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Andreas Pardeike

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
26 changes: 26 additions & 0 deletions Hypernex.CCK.Unity/PluginLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public class PluginLoader : MonoBehaviour
private static readonly List<HypernexPlugin> _loadedPlugins = new List<HypernexPlugin>();
public static List<HypernexPlugin> LoadedPlugins => new List<HypernexPlugin>(_loadedPlugins);

private static readonly string[] Libs = {"Hypernex.CCK.Unity.Libs.Lib.Harmony.0Harmony.dll"};

public static int LoadAllPlugins(string path)
{
LoadEmbeddedLibs();
int pluginsLoaded = 0;
if (!Directory.Exists(path))
{
Expand All @@ -32,6 +35,7 @@ public static int LoadAllPlugins(string path)
HypernexPlugin hypernexPlugin = (HypernexPlugin) Activator.CreateInstance(pluginType);
if (hypernexPlugin == null)
throw new Exception("Failed to create instance from HypernexPlugin!");
typeof(HypernexPlugin).GetProperty("Logger")?.SetValue(hypernexPlugin, new UnityPluginLogger(hypernexPlugin.PluginName));
Logger.CurrentLogger.Log($"Loaded Plugin {hypernexPlugin.PluginName} by {hypernexPlugin.PluginCreator} ({hypernexPlugin.PluginVersion})");
_loadedPlugins.Add(hypernexPlugin);
pluginsLoaded++;
Expand All @@ -46,6 +50,28 @@ public static int LoadAllPlugins(string path)
return pluginsLoaded;
}

private static void LoadEmbeddedLibs()
{
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string lib in Libs)
{
using (Stream stream = assembly.GetManifestResourceStream(lib))
{
if (stream == null)
{
Logger.CurrentLogger.Error("Cannot find dll from reference " + lib);
continue;
}
using (MemoryStream memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
byte[] data = memoryStream.ToArray();
AppDomain.CurrentDomain.Load(data);
}
}
}
}

private void Start()
{
foreach (HypernexPlugin hypernexPlugin in LoadedPlugins)
Expand Down
47 changes: 47 additions & 0 deletions Hypernex.CCK.Unity/UnityLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,51 @@ public override void Critical(Exception e)
OnCritical.Invoke(e);
}
}

public class UnityPluginLogger : Logger
{
public static Action<object> OnDebug = o => { };
public static Action<object> OnLog = o => { };
public static Action<object> OnWarn = o => { };
public static Action<object> OnError = o => { };
public static Action<Exception> OnCritical = o => { };

private string identifier;

public UnityPluginLogger(string identifier) => this.identifier = identifier;

public override void Debug(object o)
{
o = $"[{identifier}] {o}";
UnityEngine.Debug.Log(o);
OnDebug.Invoke(o);
}

public override void Log(object o)
{
o = $"[{identifier}] {o}";
UnityEngine.Debug.Log(o);
OnLog.Invoke(o);
}

public override void Warn(object o)
{
o = $"[{identifier}] {o}";
UnityEngine.Debug.LogWarning(o);
OnWarn.Invoke(o);
}

public override void Error(object o)
{
o = $"[{identifier}] {o}";
UnityEngine.Debug.LogError(o);
OnError.Invoke(o);
}

public override void Critical(Exception e)
{
UnityEngine.Debug.LogException(e);
OnCritical.Invoke(e);
}
}
}
2 changes: 2 additions & 0 deletions Hypernex.CCK/HypernexPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public abstract class HypernexPlugin
public abstract string PluginCreator { get; }
public abstract string PluginVersion { get; }

public Logger Logger { get; private set; }

public virtual void OnPluginLoaded(){}
public virtual void Start(){}
public virtual void FixedUpdate(){}
Expand Down

0 comments on commit c32a85e

Please sign in to comment.