Skip to content

Commit

Permalink
feat: Remove Runner and make some minor changes to code
Browse files Browse the repository at this point in the history
  • Loading branch information
adrenak committed Nov 21, 2020
1 parent 1c4e824 commit 7d9d71b
Show file tree
Hide file tree
Showing 54 changed files with 185 additions and 535 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions Assets/Adrenak.Unex/Runtime/UnexInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using UnityEngine;

namespace Adrenak.Unex {
public class UnexInitializer : MonoBehaviour {
static UnexInitializer instance;

void Awake() {
if (instance == null)
instance = GetComponent<UnexInitializer>();
Initialize();
}

public static void Initialize() {
Dispatcher.Init();
Runnable.Init();
}
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace Adrenak.Unex {
public class MonoSingleton<T> : MonoBehaviour where T : Component {
private static T _instance;
private static T instance;
public static T Instance {
get {
if (_instance == null) {
if (instance == null) {
var objs = FindObjectsOfType(typeof(T)) as T[];
if (objs.Length > 0)
_instance = objs[0];
instance = objs[0];
if (objs.Length > 1)
Debug.LogWarning("There is more than one " + typeof(T).Name + " in the scene.");
if (_instance == null) {
if (instance == null) {
GameObject obj = new GameObject();
_instance = obj.AddComponent<T>();
instance = obj.AddComponent<T>();
}
}
return _instance;
return instance;
}
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,169 +1,169 @@
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Uncomment to enable debugging of the Runnable class.
//#define ENABLE_RUNNABLE_DEBUGGING

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Adrenak.Unex {
/// <summary>
/// Helper class for running co-routines without having to inherit from MonoBehavior.
/// </summary>
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

// Uncomment to enable debugging of the Runnable class.
//#define ENABLE_RUNNABLE_DEBUGGING

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace Adrenak.Unex {
/// <summary>
/// Helper class for running co-routines without having to inherit from MonoBehavior.
/// </summary>
public class Runnable : MonoBehaviour {

static Runnable instance;
#region Public Properties
/// <summary>
/// Returns the Runnable instance.
/// </summary>
#region Public Properties
/// <summary>
/// Returns the Runnable instance.
/// </summary>
public static Runnable Instance {
get {
if (instance == null)
instance = GameObject.FindObjectOfType<Runnable>();
instance = FindObjectOfType<Runnable>();
if (instance == null)
Init();
return instance;
}
}

}

public static void Init(){
var go = new GameObject("Runnable");
go.hideFlags = HideFlags.HideAndDontSave;
instance = go.AddComponent<Runnable>();
}
#endregion

#region Public Interface
/// <summary>
/// Start a co-routine function.
/// </summary>
/// <param name="routine">The IEnumerator returns by the co-routine function the user is invoking.</param>
/// <returns>Returns a ID that can be passed into Stop() to halt the co-routine.</returns>
public static int Run(IEnumerator routine) {
Routine r = new Routine(routine);
return r.ID;
}

/// <summary>
/// Stops a active co-routine.
/// </summary>
/// <param name="ID">THe ID of the co-routine to stop.</param>
public static void Stop(int ID) {
Routine r = null;
if (Instance._routines.TryGetValue(ID, out r))
r.Stop = true;
}

/// <summary>
/// Check if a routine is still running.
/// </summary>
/// <param name="id">The ID returned by Run().</param>
/// <returns>Returns true if the routine is still active.</returns>
static public bool IsRunning(int id) {
return Instance._routines.ContainsKey(id);
}

#if UNITY_EDITOR
private static bool _editorRunnable = false;

/// <summary>
/// This function enables the Runnable in edit mode.
/// </summary>
public static void EnableRunnableInEditor() {
if (!_editorRunnable) {
_editorRunnable = true;
UnityEditor.EditorApplication.update += UpdateRunnable;
}
}
static void UpdateRunnable() {
if (!Application.isPlaying)
Instance.UpdateRoutines();
}

#endif
#endregion

#region Private Types
/// <summary>
/// This class handles a running co-routine.
/// </summary>
private class Routine : IEnumerator {
#region Public Properties
public int ID { get; private set; }
public bool Stop { get; set; }
#endregion

#region Private Data
private bool _moveNext = false;
private IEnumerator _enumerator = null;
#endregion

public Routine(IEnumerator a_enumerator) {
_enumerator = a_enumerator;
Runnable.Instance.StartCoroutine(this);
Stop = false;
ID = Runnable.Instance._nextRoutineId++;

Runnable.Instance._routines[ID] = this;
#if ENABLE_RUNNABLE_DEBUGGING
Log.Debug("Runnable.Routine()", "Coroutine {0} started.", ID );
#endif
}

#region IEnumerator Interface
public object Current { get { return _enumerator.Current; } }
public bool MoveNext() {
_moveNext = _enumerator.MoveNext();
if (_moveNext && Stop)
_moveNext = false;

if (!_moveNext) {
Runnable.Instance._routines.Remove(ID); // remove from the mapping
}

return _moveNext;
}
public void Reset() { _enumerator.Reset(); }
#endregion
}
#endregion

#region Private Data
private Dictionary<int, Routine> _routines = new Dictionary<int, Routine>();
private int _nextRoutineId = 1;
#endregion

/// <summary>
/// THis can be called by the user to force all co-routines to get a time slice, this is usually
/// invoked from an EditorApplication.Update callback so we can use runnable in Editor mode.
/// </summary>
public void UpdateRoutines() {
if (_routines.Count > 0) {
// we are not in play mode, so we must manually update our co-routines ourselves
List<Routine> routines = new List<Routine>();
foreach (var kp in _routines)
routines.Add(kp.Value);

foreach (var r in routines)
r.MoveNext();
}
}
}
}
}
#endregion

#region Public Interface
/// <summary>
/// Start a co-routine function.
/// </summary>
/// <param name="routine">The IEnumerator returns by the co-routine function the user is invoking.</param>
/// <returns>Returns a ID that can be passed into Stop() to halt the co-routine.</returns>
public static int Run(IEnumerator routine) {
Routine r = new Routine(routine);
return r.ID;
}

/// <summary>
/// Stops a active co-routine.
/// </summary>
/// <param name="ID">THe ID of the co-routine to stop.</param>
public static void Stop(int ID) {
Routine r = null;
if (Instance._routines.TryGetValue(ID, out r))
r.Stop = true;
}

/// <summary>
/// Check if a routine is still running.
/// </summary>
/// <param name="id">The ID returned by Run().</param>
/// <returns>Returns true if the routine is still active.</returns>
static public bool IsRunning(int id) {
return Instance._routines.ContainsKey(id);
}

#if UNITY_EDITOR
private static bool _editorRunnable = false;

/// <summary>
/// This function enables the Runnable in edit mode.
/// </summary>
public static void EnableRunnableInEditor() {
if (!_editorRunnable) {
_editorRunnable = true;
UnityEditor.EditorApplication.update += UpdateRunnable;
}
}
static void UpdateRunnable() {
if (!Application.isPlaying)
Instance.UpdateRoutines();
}

#endif
#endregion

#region Private Types
/// <summary>
/// This class handles a running co-routine.
/// </summary>
private class Routine : IEnumerator {
#region Public Properties
public int ID { get; private set; }
public bool Stop { get; set; }
#endregion

#region Private Data
private bool _moveNext = false;
private IEnumerator _enumerator = null;
#endregion

public Routine(IEnumerator a_enumerator) {
_enumerator = a_enumerator;
Instance.StartCoroutine(this);
Stop = false;
ID = Instance._nextRoutineId++;

Instance._routines[ID] = this;
#if ENABLE_RUNNABLE_DEBUGGING
Log.Debug("Runnable.Routine()", "Coroutine {0} started.", ID );
#endif
}

#region IEnumerator Interface
public object Current { get { return _enumerator.Current; } }
public bool MoveNext() {
_moveNext = _enumerator.MoveNext();
if (_moveNext && Stop)
_moveNext = false;

if (!_moveNext) {
Runnable.Instance._routines.Remove(ID); // remove from the mapping
}

return _moveNext;
}
public void Reset() { _enumerator.Reset(); }
#endregion
}
#endregion

#region Private Data
private Dictionary<int, Routine> _routines = new Dictionary<int, Routine>();
private int _nextRoutineId = 1;
#endregion

/// <summary>
/// THis can be called by the user to force all co-routines to get a time slice, this is usually
/// invoked from an EditorApplication.Update callback so we can use runnable in Editor mode.
/// </summary>
public void UpdateRoutines() {
if (_routines.Count > 0) {
// we are not in play mode, so we must manually update our co-routines ourselves
List<Routine> routines = new List<Routine>();
foreach (var kp in _routines)
routines.Add(kp.Value);

foreach (var r in routines)
r.MoveNext();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public async Task<Dictionary<T, K>> Run() {
while (taskMap.Count != 0) {
var done = await Task.WhenAny(taskMap.Values.ToList());
var index = taskMap.Values.ToList().IndexOf(done);
var genre = taskMap.Keys.ToList()[index];
var key = taskMap.Keys.ToList()[index];

resultMap.Add(genre, done.Result);
resultMap.Add(key, done.Result);
SingleTaskCompleted?.Invoke(done.Result, index);
taskMap.Remove(genre);
taskMap.Remove(key);
}

return resultMap;
Expand Down
File renamed without changes.
9 changes: 0 additions & 9 deletions Assets/Adrenak.Unex/Samples/Runner.meta

This file was deleted.

Binary file removed Assets/Adrenak.Unex/Samples/Runner/Runner.unity
Binary file not shown.
Loading

0 comments on commit 7d9d71b

Please sign in to comment.