Skip to content

Commit

Permalink
Merge pull request #6 from Timberborn-Modding-Central/rename
Browse files Browse the repository at this point in the history
Renames for consistency of plural and clarity
  • Loading branch information
hawkfalcon authored Nov 19, 2021
2 parents d5de5bd + b4c3701 commit 59d5dac
Show file tree
Hide file tree
Showing 32 changed files with 109 additions and 172 deletions.
82 changes: 13 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,18 @@
# TimberAPI
Upcoming API to enable easier Timberborn modding
Unofficial API to enable easier Timberborn modding

*Currently has the following features:*
1. Bind your code to Timberborn to allow Dependency Injection. Supports InGame, MainMenu, and MapEditor
2. Listen to any event. Extend the Listener class and use `[OnEvent]`
3. Add labels to localization
### **Currently supported features:**
1. Bind your code easily with depencency injection
2. Event listening of any event to get triggered on in game actions
3. A comprehensive UI component builder
4. Asset injection, provide your own icons or even unity bundles
5. Labels and localization support

Wiki (with general Timberborn modding info too!
[Wiki Link](https://github.com/Timberborn-Modding-Central/TimberAPI/wiki)
## More Information can be found on the wiki
**[Timberborn Modding Wiki](https://github.com/Timberborn-Modding-Central/TimberAPI/wiki)**

*To use this API, add this BepInEx annotation:*
`[BepInDependency("com.timberapi.timberapi")]`
## TimberAPI Documentation
**[Documentation for TimberAPI](https://github.com/Timberborn-Modding-Central/TimberAPI/wiki/TimberAPI-Feature-Guide)**

## Dependency Injection:
1. Create a configurator and bind your class:
```
public class ExampleConfigurator : IConfigurator {
public void Configure(IContainerDefinition containerDefinition) {
containerDefinition.Bind<ExampleListener>().AsSingleton();
}
}
```
2. Register it with the game
```
TimberAPI.Dependencies.AddConfigurator(new ExampleConfigurator());
```

## Event Listening
1. Extend the TimberAPI Listener class, which will automatically hook the eventbus
2. Listen to an event with `[OnEvent`
```
public class ExampleListener : Listener {
[OnEvent]
public void OnDroughtStarted(DroughtStartedEvent droughtStartedEvent) {...}
```
3. Register your class with the game through a configurator (see Dependency Injection)

## Localization Labels
1. Add a label
```
TimberAPI.Localization.AddLabel("ExampleMod.ToolGroups.ExampleToolGroup", "Example Label");
```

## UIBuilder
1. Inject UIBuilder into a configurated class
2. Build any visual element with the timberborn components
```c#
public UIBuilderFragmentExample(IUIBuilder builder)
{
builder.CreateComponentBuilder()
.AddButton("First Button", name: "firstButton")
...
.Build();
}
```

## Asset loader
1. Register asset to the game (default asset file location `assets`)
2. Use the `IAssetLoader` to use the loaded assets, `IAssetLoader` will automatically be injected into binded classes
```c#
TimberAPI.AssetLoaderSystem.AddSceneAssets("TimberAPIExample", IAssetLoaderSystem.EntryPoint.InGame);
```
Custom asset root location:
```c#
TimberAPI.AssetLoaderSystem.AddSceneAssets("TimberAPIExample", IAssetLoaderSystem.EntryPoint.InGame, new []{ "assets", "ingame" });
```
```c#
private readonly IAssetLoader _assetLoader;
_assetLoader.Load<Sprite>("prefix/subfolder/filename/itemname")
```


See [ExamplePlugin](https://github.com/Timberborn-Modding-Central/TimberAPI/blob/main/TimberAPIExample/TimberApiExamplePlugin.cs) for more examples
## Provided Sample Plugin with API examples
**[Example Plugin](https://github.com/Timberborn-Modding-Central/TimberAPI/blob/main/TimberAPIExample/TimberApiExamplePlugin.cs)**
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace TimberbornAPI.AssetLoader
{
public class AssetLoaderSystem : IAssetLoaderSystem
public class AssetRegistry : IAssetRegistry
{
internal static SceneEntryPoint ActiveScene = SceneEntryPoint.Global;

Expand Down
6 changes: 3 additions & 3 deletions TimberAPI/AssetLoader/AssetSystem/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public T Load<T>(string prefix, string[] path, string fileName, string name) whe
{
try
{
return AssetLoaderSystem.PluginRepository.FindByPrefix(prefix).AssetRepository
return AssetRegistry.PluginRepository.FindByPrefix(prefix).AssetRepository
.FindByPathAndFileName(path, fileName).AssetBundle.LoadAsset<T>(name);
}
catch (PrefixNotFoundException e)
{
Console.WriteLine($"Given prefix ({e.Prefix}) was not found. did you load it in the correct scene: {AssetLoaderSystem.ActiveScene}");
Console.WriteLine($"Given prefix ({e.Prefix}) was not found. did you load it in the correct scene: {AssetRegistry.ActiveScene}");
Console.WriteLine(e.StackTrace);
throw;
}
Expand Down Expand Up @@ -108,7 +108,7 @@ public T[] LoadAll<T>(string prefix, string[] path, string fileName) where T : U
{
try
{
return AssetLoaderSystem.PluginRepository.FindByPrefix(prefix).AssetRepository
return AssetRegistry.PluginRepository.FindByPrefix(prefix).AssetRepository
.FindByPathAndFileName(path, fileName).AssetBundle.LoadAllAssets<T>();
}
catch (PrefixNotFoundException e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace TimberbornAPI.AssetLoader
{
public interface IAssetLoaderSystem
public interface IAssetRegistry
{
/// <summary>
/// Adds assets to a scene that are placed in the assetLocation
Expand Down
8 changes: 4 additions & 4 deletions TimberAPI/AssetLoader/Patches/SceneLoaderPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public static class ProjectConfiguratorPatch
{
private static void Postfix()
{
TimberAPI.AssetLoaderSystem.LoadSceneAssets(SceneEntryPoint.Global);
TimberAPI.AssetLoaderSystem.LoadSceneAssets(SceneEntryPoint.MainMenu);
TimberAPI.AssetRegistry.LoadSceneAssets(SceneEntryPoint.Global);
TimberAPI.AssetRegistry.LoadSceneAssets(SceneEntryPoint.MainMenu);
}
}

Expand Down Expand Up @@ -73,8 +73,8 @@ private static class LoadHelper
*/
internal static void LoadSceneAssets(SceneEntryPoint scene)
{
TimberAPI.AssetLoaderSystem.UnloadSceneAssets(AssetLoaderSystem.ActiveScene);
TimberAPI.AssetLoaderSystem.LoadSceneAssets(scene);
TimberAPI.AssetRegistry.UnloadSceneAssets(AssetRegistry.ActiveScene);
TimberAPI.AssetRegistry.LoadSceneAssets(scene);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion TimberAPI/AssetLoader/PluginSystem/PluginRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public PluginRepository()
/// <exception cref="PrefixNotFoundException"></exception>
public Plugin FindByPrefix(string prefix)
{
Plugin modPlugin = _plugins.FirstOrDefault(plugin => plugin.Prefix == prefix && (plugin.LoadingScene == AssetLoaderSystem.ActiveScene
Plugin modPlugin = _plugins.FirstOrDefault(plugin => plugin.Prefix == prefix && (plugin.LoadingScene == AssetRegistry.ActiveScene
|| plugin.LoadingScene == SceneEntryPoint.Global));

if (modPlugin == null)
Expand Down
16 changes: 0 additions & 16 deletions TimberAPI/Dependency/IDependencies.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
using TimberbornAPI.Common;
using UnityEngine;

namespace TimberbornAPI.Dependency
namespace TimberbornAPI.DependencySystem
{
[HarmonyPatch]
public class Dependencies : IDependencies
public class DependencyRegistry : IDependencyRegistry
{
private static Dictionary<SceneEntryPoint, List<IConfigurator>> configuratorsByEntryPoint = new();

/**
* Install a Configurator into a scene to allow dependency injection
* The class must implement IConfigurator and can use Bind<>() to inject dependencies
*/
/// <summary>
/// Install a Configurator into a scene to allow dependency injection
/// The class must implement IConfigurator and can use Bind<>() to inject dependencies
/// </summary>
/// <param name="configurator">The configurator class to inject, which does the binding</param>
/// <param name="entryPoint">Scene to bind to, defaults to InGame</param>
public void AddConfigurator(IConfigurator configurator, SceneEntryPoint entryPoint = SceneEntryPoint.InGame)
{
if (entryPoint == SceneEntryPoint.Global)
Expand All @@ -35,39 +37,27 @@ public void AddConfigurator(IConfigurator configurator, SceneEntryPoint entryPoi
}
}

/**
* Inject configurators into MasterScene (InGame)
*/
[HarmonyPostfix]
[HarmonyPatch(typeof(MasterSceneConfigurator), "Configure")]
static void InjectMasterScene(IContainerDefinition containerDefinition)
static void InjectIntoMasterScene(IContainerDefinition containerDefinition)
{
InstallAll(containerDefinition, SceneEntryPoint.InGame);
}

/**
* Inject configurators into MainMenuScene
*/
[HarmonyPostfix]
[HarmonyPatch(typeof(MainMenuSceneConfigurator), "Configure")]
static void InjectMainMenuScene(IContainerDefinition containerDefinition)
static void InjectIntoMainMenuScene(IContainerDefinition containerDefinition)
{
InstallAll(containerDefinition, SceneEntryPoint.MainMenu);
}

/**
* Inject configurators into MapEditorScene
*/
[HarmonyPostfix]
[HarmonyPatch(typeof(MapEditorSceneConfigurator), "Configure")]
static void InjectMapEditorScene(IContainerDefinition containerDefinition)
static void InjectIntoMapEditorScene(IContainerDefinition containerDefinition)
{
InstallAll(containerDefinition, SceneEntryPoint.MapEditor);
}

/**
* Install all the configurators for the given entryPoint
*/
private static void InstallAll(IContainerDefinition containerDefinition, SceneEntryPoint entryPoint)
{
List<IConfigurator> configurators =
Expand Down
17 changes: 17 additions & 0 deletions TimberAPI/DependencyRegistry/IDependencyRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Bindito.Core;
using TimberbornAPI.Common;

namespace TimberbornAPI.DependencySystem
{
public interface IDependencyRegistry
{
/// <summary>
/// Install a Configurator into a scene to allow dependency injection
/// The class must implement IConfigurator and can use Bind<>() to inject dependencies
/// </summary>
/// <param name="configurator">The configurator class to inject, which does the binding</param>
/// <param name="entryPoint">Scene to bind to, defaults to InGame</param>
void AddConfigurator(IConfigurator configurator, SceneEntryPoint entryPoint = SceneEntryPoint.InGame);

}
}
29 changes: 29 additions & 0 deletions TimberAPI/Event/EventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Bindito.Core;
using Timberborn.SingletonSystem;

namespace TimberbornAPI.EventSystem
{
/// <summary>
/// Extend Event Listener class to automatically register event listening
/// Listen to any event with [OnEvent]
/// </summary>
/// <remarks>
/// You must also bind that class in a Configurator and add it with
/// TimberAPI.Dependency.AddConfigurator()
/// </remarks>
public abstract class EventListener : ILoadableSingleton
{
private EventBus _eventBus;

[Inject]
public void InjectDependencies(EventBus eventBus)
{
_eventBus = eventBus;
}

public void Load()
{
_eventBus.Register(this);
}
}
}
28 changes: 0 additions & 28 deletions TimberAPI/Event/Listener.cs

This file was deleted.

3 changes: 1 addition & 2 deletions TimberAPI/Localization/ILocalization.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Collections.Generic;

namespace TimberbornAPI.Localizations
namespace TimberbornAPI.LocalizationSystem
{
public interface ILocalization
{

/// <summary>
/// Add a label into the current localization
/// For use in DisplayNameLocKey, such as in ToolGroup
Expand Down
21 changes: 11 additions & 10 deletions TimberAPI/Localization/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@
using HarmonyLib;
using Timberborn.Common;

namespace TimberbornAPI.Localizations
namespace TimberbornAPI.LocalizationSystem
{
[HarmonyPatch]
public class Localization : ILocalization
{
private static Dictionary<string, string> labelsToInject = new();

/**
* Add a label into the current localization
*
* Example: CreativeMode.ToolGroups.MapEditor : "Map editor tools"
* For use in DisplayNameLocKey, such as in ToolGroup
*/
/// <summary>
/// Add a label into the current localization
/// For use in DisplayNameLocKey, such as in ToolGroup
/// </summary>
/// <param name="key">The key to reference the label with later</param>
/// <param name="value">The localized label</param>
public void AddLabel(string key, string value)
{
labelsToInject.Add(key, value);
}

/**
* Add multiple labels into the current localization
*/
/// <summary>
/// Add multiple labels into the current localization
/// </summary>
/// <param name="labels">Multiple key:value labels to add</param>
public void AddLabels(Dictionary<string, string> labels)
{
labelsToInject.AddRange(labels);
Expand Down
2 changes: 1 addition & 1 deletion TimberAPI/Localization/LocalizationRecord.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using LINQtoCSV;

namespace TimberbornAPI.Localizations
namespace TimberbornAPI.LocalizationSystem
{
/// <summary>
/// Timberborn code Timberborn.Localization.LocalizationRecord
Expand Down
2 changes: 1 addition & 1 deletion TimberAPI/Localization/LocalizationRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using TimberbornAPI.Internal;
using UnityEngine;

namespace TimberbornAPI.Localizations
namespace TimberbornAPI.LocalizationSystem
{
internal static class LocalizationRepository
{
Expand Down
Loading

0 comments on commit 59d5dac

Please sign in to comment.