Skip to content

Commit

Permalink
library
Browse files Browse the repository at this point in the history
  • Loading branch information
izayoijiichan committed Jan 1, 2020
1 parent fd75a63 commit 2f50287
Show file tree
Hide file tree
Showing 77 changed files with 2,540 additions and 0 deletions.
8 changes: 8 additions & 0 deletions DepthFirstScheduler.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions DepthFirstScheduler/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 ousttrue

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.
8 changes: 8 additions & 0 deletions DepthFirstScheduler/LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions DepthFirstScheduler/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# DepthFirstScheduler(深さ優先スケジューラー)
Asynchronous task scheduler for Unity-5.6 or later

これは、Unity5.6でTaskが無いことを補完するためのライブラリです。
木構造にタスクを組み立てて深さ優先で消化します。

* タスクの実行スケジューラー(Unityメインスレッドやスレッドプール)を指定できる

# 使い方

```cs
var schedulable = new Schedulable<Unit>();

schedulable
.AddTask(Scheduler.ThreadPool, () => // 子供のタスクを追加する
{
return glTF_VRM_Material.Parse(ctx.Json);
})
.ContinueWith(Scheduler.MainThread, gltfMaterials => // 兄弟のタスクを追加する
{
ctx.MaterialImporter = new VRMMaterialImporter(ctx, gltfMaterials);
})
.Subscribe(Scheduler.MainThread, onLoaded, onError);
;
```

# Schedulable<T>
T型の結果を返すタスク。

## AddTask(IScheduler scheduler, Func<T> firstTask)

子供のタスクを追加する。

ToDo: 一つ目の子供に引数を渡す手段が無い

## ContinueWith

## ContinueWithCoroutine

## OnExecute

動的にタスクを追加するためのHook。

中で、

```
parent.AddTask
```

することで実行時に子タスクを追加できる。

## Subscribe
タスクの実行を開始する。
実行結果を得る。


# Scheduler
## StepScheduler
Unity
## CurrentThreadScheduler
即時
## ThreadPoolScheduler
スレッド
## ThreadScheduler
スレッド

8 changes: 8 additions & 0 deletions DepthFirstScheduler/README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions DepthFirstScheduler/Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions DepthFirstScheduler/Runtime/DepthFirstScheduler.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "DepthFirstScheduler"
}
7 changes: 7 additions & 0 deletions DepthFirstScheduler/Runtime/DepthFirstScheduler.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

192 changes: 192 additions & 0 deletions DepthFirstScheduler/Runtime/Functor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System;
using System.Collections;
using System.Collections.Generic;

namespace DepthFirstScheduler
{
public enum ExecutionStatus
{
Unknown,
Done,
Continue, // coroutine or schedulable
Error,
}

public interface IFunctor<T>
{
T GetResult();
Exception GetError();
ExecutionStatus Execute();
}

#region Functor
public class Functor<T> : IFunctor<T>
{
T m_result;
public T GetResult()
{
return m_result;
}

Exception m_error;
public Exception GetError()
{
return m_error;
}

Action m_pred;
public Functor(Func<T> func)
{
m_pred = () => m_result = func();
}

public ExecutionStatus Execute()
{
try
{
m_pred();
return ExecutionStatus.Done;
}
catch (Exception ex)
{
m_error = ex;
return ExecutionStatus.Error;
}
}
}

public static class Functor
{
/// <summary>
/// 引数の型を隠蔽した実行器を生成する
/// </summary>
/// <typeparam name="S">引数の型</typeparam>
/// <typeparam name="T">結果の型</typeparam>
/// <param name="arg"></param>
/// <param name="pred"></param>
/// <returns></returns>
public static Functor<T> Create<S, T>(Func<S> arg, Func<S, T> pred)
{
return new Functor<T>(() => pred(arg()));
}
}
#endregion

#region CoroutineFunctor
public class CoroutineFunctor<T> : IFunctor<T>
{
T m_result;
public T GetResult()
{
return m_result;
}

Exception m_error;
public Exception GetError()
{
return m_error;
}

Func<T> m_arg;
Func<T, IEnumerator> m_starter;
Stack<IEnumerator> m_it;
public CoroutineFunctor(Func<T> arg, Func<T, IEnumerator> starter)
{
m_arg = arg;
m_starter = starter;
}

public ExecutionStatus Execute()
{
if (m_it == null)
{
m_result = m_arg();
m_it = new Stack<IEnumerator>();
m_it.Push(m_starter(m_result));
}

try
{
if (m_it.Count!=0)
{
if (m_it.Peek().MoveNext())
{
var nested = m_it.Peek().Current as IEnumerator;
if (nested!=null)
{
m_it.Push(nested);
}
}
else
{
m_it.Pop();
}
return ExecutionStatus.Continue;
}
else
{
return ExecutionStatus.Done;
}

}
catch(Exception ex)
{
m_error = ex;
return ExecutionStatus.Error;
}
}
}

public static class CoroutineFunctor
{
public static CoroutineFunctor<T> Create<T>(Func<T> arg, Func<T, IEnumerator> starter)
{
return new CoroutineFunctor<T>(arg, starter);
}
}
#endregion

/*
public class SchedulableFunctor<T> : IFunctor<T>
{
Schedulable<T> m_schedulable;
Func<Schedulable<T>> m_starter;
TaskChain m_chain;
public SchedulableFunctor(Func<Schedulable<T>> starter)
{
m_starter = starter;
}
public ExecutionStatus Execute()
{
if (m_chain == null)
{
m_schedulable = m_starter();
m_chain = TaskChain.Schedule(m_schedulable, ex => m_error = ex);
}
return m_chain.Next();
}
Exception m_error;
public Exception GetError()
{
return m_error;
}
public T GetResult()
{
return m_schedulable.Func.GetResult();
}
}
public static class SchedulableFunctor
{
public static SchedulableFunctor<T> Create<T>(Func<Schedulable<T>> starter)
{
return new SchedulableFunctor<T>(starter);
}
}
*/
}
12 changes: 12 additions & 0 deletions DepthFirstScheduler/Runtime/Functor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions DepthFirstScheduler/Runtime/IEnumeratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;


namespace
DepthFirstScheduler
{
public static class IEnumeratorExtensions
{
[Obsolete("Use CoroutineToEnd")]
public static void CoroutinetoEnd(this IEnumerator coroutine)
{
CoroutineToEnd(coroutine);
}

public static void CoroutineToEnd(this IEnumerator coroutine)
{
var stack = new Stack<IEnumerator>();
stack.Push(coroutine);
while (stack.Count > 0)
{
if (stack.Peek().MoveNext())
{
var nested = stack.Peek().Current as IEnumerator;
if (nested != null)
{
stack.Push(nested);
}
}
else
{
stack.Pop();
}
}
}
}
}
11 changes: 11 additions & 0 deletions DepthFirstScheduler/Runtime/IEnumeratorExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2f50287

Please sign in to comment.