Skip to content

Commit

Permalink
feat(assets): add dependency loader (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenira committed May 3, 2024
1 parent 82a590a commit c26bbec
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 5 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"conventionalCommits.scopes": [
"nuget",
"readme"
"readme",
"behaviour"
]
}
}
7 changes: 6 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ cd plugin
dotnet build
cd ..

# Cleanup
echo "Cleaning up..."
rm -rf "$GAME_PATH/BepInEx/plugins/$MOD_DLL"
mkdir "$GAME_PATH/BepInEx/plugins/$MOD_DLL"

# Copy the mod dll to the mods folder
echo "Copying the mod dll to the mods folder..."
cp ./plugin/bin/Debug/netstandard2.1/$MOD_DLL.* "$GAME_PATH/BepInEx/plugins/"
cp ./plugin/bin/Debug/netstandard2.1/$MOD_DLL.* "$GAME_PATH/BepInEx/plugins/$MOD_DLL/"
27 changes: 27 additions & 0 deletions plugin/src/assets/DependencyLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Reflection;
using PiUtils.Util;

namespace PiUtils.Assets;

public class DependencyLoader
{
private static PluginLogger Logger = PluginLogger.GetLogger<DependencyLoader>();

public static void LoadDirectory(string path)
{
string[] files = Directory.GetFiles(path, "*.dll");

foreach (string file in files)
{
try
{
Assembly.LoadFile(file);
}
catch (System.Exception e)
{
Logger.LogError($"Failed to load assembly {file}", e);
}
}
}
}
37 changes: 37 additions & 0 deletions plugin/src/patch/AtLeastOnePatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Linq;

namespace PiUtils.Patches;

/// <summary>
/// A patch that applies a list of patches in order.
/// Once at least one of the patches is applied successfully, this patch is considered applied.
///
/// Each patch is applied at least once, even if another patch in the list has already been applied successfully.
/// </summary>
public class AtLeastOnePatch : IPatch
{
private bool applied = false;
private IPatch[] patches;

public AtLeastOnePatch(IPatch[] patches)
{
this.patches = patches;
}

public bool Apply()
{
foreach (var patch in patches)
{
patch.Apply();
}

applied = patches.Select(p => p.IsApplied()).Any(applied => applied);

return applied;
}

public bool IsApplied()
{
return applied;
}
}
4 changes: 2 additions & 2 deletions plugin/src/util/BitState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public bool hasState(long newState)
return (state & newState) != 0;
}

public bool hasAnyState(long newState)
public bool isState(long newState)
{
return (state & newState) != 0;
return state == newState;
}
}
5 changes: 5 additions & 0 deletions plugin/src/util/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public void LogError(string message)
logger.LogError($"<{Time.frameCount}> {message}");
}

public void LogError(string message, Exception e)
{
LogError(message + "\n" + e.ToString());
}

public void LogTrace(string v)
{
if (!ModConfig.TraceLogEnabled())
Expand Down
14 changes: 14 additions & 0 deletions plugin/src/util/TransformUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using UnityEngine;

namespace PiUtils.Util;

public static class TransformUtils
{
public static void DestroyAllChildren(this Transform transform)
{
for (int i = transform.childCount - 1; i >= 0; i--)
{
GameObject.Destroy(transform.GetChild(i).gameObject);
}
}
}

0 comments on commit c26bbec

Please sign in to comment.