Skip to content

Commit

Permalink
Added sources
Browse files Browse the repository at this point in the history
  • Loading branch information
RimuruDev committed May 26, 2024
1 parent 59f7a99 commit e17451d
Show file tree
Hide file tree
Showing 12 changed files with 693 additions and 0 deletions.
62 changes: 62 additions & 0 deletions IndieInject/Common/MonoSingleton.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

using UnityEngine;

namespace IndieInjector
{
[HelpURL("https://github.com/RimuruDev/IndieInject")]
public class MonoSingleton<TComponent> : MonoBehaviour where TComponent : Component
{
protected static TComponent instance;

public static bool HasInstance => instance != null;

public static TComponent TryGetInstance() => HasInstance ? instance : null;

public static TComponent Current => instance;

public static TComponent Self => instance;

public static TComponent Instance
{
get
{
if (instance != null)
return instance;

instance = FindFirstObjectByType<TComponent>();

if (instance != null)
return instance;

var obj = new GameObject
{
name = $"[ === {typeof(TComponent).Name} === ]"
};

instance = obj.AddComponent<TComponent>();

return instance;
}
}

protected virtual void Awake() =>
InitializeSingleton();

protected virtual void InitializeSingleton()
{
if (!Application.isPlaying)
return;

instance = this as TComponent;
}
}
}
16 changes: 16 additions & 0 deletions IndieInject/Core/Abstractions/IDependencyProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

namespace IndieInjector
{
public interface IDependencyProvider
{
}
}
21 changes: 21 additions & 0 deletions IndieInject/Core/Attributes/InjectAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

using System;
using UnityEngine;

namespace IndieInjector
{
[HelpURL("https://github.com/RimuruDev/IndieInject")]
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property)]
public sealed class InjectAttribute : Attribute
{
}
}
21 changes: 21 additions & 0 deletions IndieInject/Core/Attributes/ProvideAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

using System;
using UnityEngine;

namespace IndieInjector
{
[HelpURL("https://github.com/RimuruDev/IndieInject")]
[AttributeUsage(AttributeTargets.Method)]
public sealed class ProvideAttribute : Attribute
{
}
}
80 changes: 80 additions & 0 deletions IndieInject/Core/Extensions/IndieInjectorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

using UnityEngine;

namespace IndieInjector
{
[HelpURL("https://github.com/RimuruDev/IndieInject")]
public static class IndieInjectorExtensions
{
public static GameObject InstantiateWithDependencies(GameObject prefab, Vector3 position, Quaternion rotation)
{
var instance = Object.Instantiate(prefab, position, rotation);
IndieInjector.Instance.Inject(instance, InjectionType.All);
return instance;
}

public static GameObject InstantiateWithFields(GameObject prefab, Vector3 position, Quaternion rotation)
{
var instance = Object.Instantiate(prefab, position, rotation);
IndieInjector.Instance.Inject(instance, InjectionType.Fields);
return instance;
}

public static GameObject InstantiateWithProperties(GameObject prefab, Vector3 position, Quaternion rotation)
{
var instance = Object.Instantiate(prefab, position, rotation);
IndieInjector.Instance.Inject(instance, InjectionType.Properties);
return instance;
}

public static GameObject InstantiateWithMethods(GameObject prefab, Vector3 position, Quaternion rotation)
{
var instance = Object.Instantiate(prefab, position, rotation);
IndieInjector.Instance.Inject(instance, InjectionType.Methods);
return instance;
}

public static GameObject InstantiateWithDependencies(GameObject prefab, Transform parent)
{
var instance = Object.Instantiate(prefab, parent);
IndieInjector.Instance.Inject(instance, InjectionType.All);
return instance;
}

public static GameObject InstantiateWithFields(GameObject prefab, Transform parent)
{
var instance = Object.Instantiate(prefab, parent);
IndieInjector.Instance.Inject(instance, InjectionType.Fields);
return instance;
}

public static GameObject InstantiateWithProperties(GameObject prefab, Transform parent)
{
var instance = Object.Instantiate(prefab, parent);
IndieInjector.Instance.Inject(instance, InjectionType.Properties);
return instance;
}

public static GameObject InstantiateWithMethods(GameObject prefab, Transform parent)
{
var instance = Object.Instantiate(prefab, parent);
IndieInjector.Instance.Inject(instance, InjectionType.Methods);
return instance;
}

public static void DestroyWithDependencies(GameObject gameObject, float timeToDestroy = 0f)
{
IndieInjector.Instance.RemoveDependencies(gameObject);
Object.Destroy(gameObject, timeToDestroy);
}
}
}
65 changes: 65 additions & 0 deletions IndieInject/Core/Extensions/IndieObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

// TODO: Add Generic variant's

using UnityEngine;

namespace IndieInjector
{
[HelpURL("https://github.com/RimuruDev/IndieInject")]
public static class IndieObject
{
public static GameObject InstantiateWithDependencies(GameObject prefab, Vector3 position, Quaternion rotation)
{
return IndieInjectorExtensions.InstantiateWithDependencies(prefab, position, rotation);
}

public static GameObject InstantiateWithFields(GameObject prefab, Vector3 position, Quaternion rotation)
{
return IndieInjectorExtensions.InstantiateWithFields(prefab, position, rotation);
}

public static GameObject InstantiateWithProperties(GameObject prefab, Vector3 position, Quaternion rotation)
{
return IndieInjectorExtensions.InstantiateWithProperties(prefab, position, rotation);
}

public static GameObject InstantiateWithMethods(GameObject prefab, Vector3 position, Quaternion rotation)
{
return IndieInjectorExtensions.InstantiateWithMethods(prefab, position, rotation);
}

public static GameObject InstantiateWithDependencies(GameObject prefab, Transform parent)
{
return IndieInjectorExtensions.InstantiateWithDependencies(prefab, parent);
}

public static GameObject InstantiateWithFields(GameObject prefab, Transform parent)
{
return IndieInjectorExtensions.InstantiateWithFields(prefab, parent);
}

public static GameObject InstantiateWithProperties(GameObject prefab, Transform parent)
{
return IndieInjectorExtensions.InstantiateWithProperties(prefab, parent);
}

public static GameObject InstantiateWithMethods(GameObject prefab, Transform parent)
{
return IndieInjectorExtensions.InstantiateWithMethods(prefab, parent);
}

public static void DestroyWithDependencies(GameObject gameObject, float timeToDestroy = 0f)
{
IndieInjectorExtensions.DestroyWithDependencies(gameObject, timeToDestroy);
}
}
}
23 changes: 23 additions & 0 deletions IndieInject/Core/Flags/InjectionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// **************************************************************** //
//
// Copyright (c) RimuruDev. All rights reserved.
// Contact me:
// - Gmail: [email protected]
// - LinkedIn: https://www.linkedin.com/in/rimuru/
// - GitHub: https://github.com/RimuruDev
//
// **************************************************************** //

using System;

namespace IndieInjector
{
[Flags]
public enum InjectionType
{
Fields = 1,
Properties = 2,
Methods = 4,
All = Fields | Properties | Methods
}
}
Loading

0 comments on commit e17451d

Please sign in to comment.