the actual thing

This commit is contained in:
Ellpeck 2019-06-22 17:24:50 +02:00
parent 7c6294bf97
commit bd525cb85a
13 changed files with 386 additions and 1 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
.idea
bin
obj
packages
*.user

22
Coroutine.sln Normal file
View file

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Coroutine", "Coroutine\Coroutine.csproj", "{1657964D-2503-426A-8514-D020660BEE4D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{8BE6B559-927D-47A6-8253-D7D809D337AF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1657964D-2503-426A-8514-D020660BEE4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1657964D-2503-426A-8514-D020660BEE4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1657964D-2503-426A-8514-D020660BEE4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1657964D-2503-426A-8514-D020660BEE4D}.Release|Any CPU.Build.0 = Release|Any CPU
{8BE6B559-927D-47A6-8253-D7D809D337AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BE6B559-927D-47A6-8253-D7D809D337AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BE6B559-927D-47A6-8253-D7D809D337AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BE6B559-927D-47A6-8253-D7D809D337AF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

36
Coroutine/Coroutine.cs Normal file
View file

@ -0,0 +1,36 @@
using System.Collections.Generic;
namespace Coroutine {
internal class Coroutine {
private readonly IEnumerator<Wait> enumerator;
public Coroutine(IEnumerator<Wait> enumerator) {
this.enumerator = enumerator;
this.enumerator.MoveNext();
}
public bool Tick(double deltaSeconds) {
var curr = this.enumerator.Current;
if (curr != null && curr.Tick(deltaSeconds)) {
if (!this.enumerator.MoveNext())
return true;
}
return false;
}
public bool OnEvent(Event evt) {
var curr = this.enumerator.Current;
if (curr != null && curr.OnEvent(evt)) {
if (!this.enumerator.MoveNext())
return true;
}
return false;
}
public WaitType GetCurrentType() {
return this.enumerator.Current.GetWaitType();
}
}
}

View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1657964D-2503-426A-8514-D020660BEE4D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Coroutine</RootNamespace>
<AssemblyName>Coroutine</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Coroutine.cs" />
<Compile Include="CoroutineHandler.cs" />
<Compile Include="Event.cs" />
<Compile Include="Wait.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="WaitEvent.cs" />
<Compile Include="WaitSeconds.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View file

@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace Coroutine {
public static class CoroutineHandler {
private static readonly List<Coroutine> TickingCoroutines = new List<Coroutine>();
private static readonly List<Coroutine> EventCoroutines = new List<Coroutine>();
public static void Start(IEnumerator<Wait> coroutine) {
var inst = new Coroutine(coroutine);
var type = inst.GetCurrentType();
if (type == WaitType.Tick)
TickingCoroutines.Add(inst);
else if (type == WaitType.Event)
EventCoroutines.Add(inst);
}
public static void Tick(double deltaSeconds) {
for (var i = TickingCoroutines.Count - 1; i >= 0; i--) {
var coroutine = TickingCoroutines[i];
if (coroutine.Tick(deltaSeconds)) {
TickingCoroutines.RemoveAt(i);
} else if (coroutine.GetCurrentType() != WaitType.Tick) {
TickingCoroutines.RemoveAt(i);
EventCoroutines.Add(coroutine);
}
}
}
public static void RaiseEvent(Event evt) {
for (var i = EventCoroutines.Count - 1; i >= 0; i--) {
var coroutine = EventCoroutines[i];
if (coroutine.OnEvent(evt)) {
EventCoroutines.RemoveAt(i);
} else if (coroutine.GetCurrentType() != WaitType.Event) {
EventCoroutines.RemoveAt(i);
TickingCoroutines.Add(coroutine);
}
}
}
}
}

7
Coroutine/Event.cs Normal file
View file

@ -0,0 +1,7 @@
namespace Coroutine {
public class Event {
}
}

View file

@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Coroutine")]
[assembly: AssemblyDescription("A simple implementation of Unity's Coroutines to be used for any C# project")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Ellpeck")]
[assembly: AssemblyProduct("Coroutine")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1657964D-2503-426A-8514-D020660BEE4D")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

24
Coroutine/Wait.cs Normal file
View file

@ -0,0 +1,24 @@
using System;
namespace Coroutine {
public abstract class Wait {
public abstract WaitType GetWaitType();
public virtual bool Tick(double deltaSeconds) {
throw new NotSupportedException();
}
public virtual bool OnEvent(Event evt) {
throw new NotSupportedException();
}
}
public enum WaitType {
Tick,
Event
}
}

19
Coroutine/WaitEvent.cs Normal file
View file

@ -0,0 +1,19 @@
namespace Coroutine {
public class WaitEvent : Wait {
private readonly Event evt;
public WaitEvent(Event evt) {
this.evt = evt;
}
public override WaitType GetWaitType() {
return WaitType.Event;
}
public override bool OnEvent(Event evt) {
return evt == this.evt;
}
}
}

20
Coroutine/WaitSeconds.cs Normal file
View file

@ -0,0 +1,20 @@
namespace Coroutine {
public class WaitSeconds : Wait {
private double seconds;
public WaitSeconds(double seconds) {
this.seconds = seconds;
}
public override WaitType GetWaitType() {
return WaitType.Tick;
}
public override bool Tick(double deltaSeconds) {
this.seconds -= deltaSeconds;
return this.seconds <= 0;
}
}
}

View file

@ -1,2 +1,2 @@
# Coroutines
# Coroutine
A simple implementation of Unity's Coroutines to be used for any C# project

57
Test/Example.cs Normal file
View file

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Threading;
using Coroutine;
namespace Test {
internal static class Example {
private static readonly Event TestEvent = new Event();
public static void Main() {
CoroutineHandler.Start(WaitSeconds());
CoroutineHandler.Start(RaiseTestEvent());
CoroutineHandler.Start(WaitForTestEvent());
CoroutineHandler.Start(PrintEvery5Seconds());
var lastTime = DateTime.Now;
while (true) {
var currTime = DateTime.Now;
CoroutineHandler.Tick((currTime - lastTime).TotalSeconds);
lastTime = currTime;
Thread.Sleep(1);
}
}
private static IEnumerator<Wait> WaitSeconds() {
Console.WriteLine("First thing " + DateTime.Now);
yield return new WaitSeconds(1);
Console.WriteLine("After 1 second " + DateTime.Now);
yield return new WaitSeconds(5);
Console.WriteLine("After 5 seconds " + DateTime.Now);
yield return new WaitSeconds(10);
Console.WriteLine("After 10 seconds " + DateTime.Now);
}
private static IEnumerator<Wait> RaiseTestEvent() {
yield return new WaitSeconds(10);
Console.WriteLine("Raising test event");
CoroutineHandler.RaiseEvent(TestEvent);
}
private static IEnumerator<Wait> WaitForTestEvent() {
yield return new WaitSeconds(5);
Console.WriteLine("Waited 5 seconds before waiting for the event");
yield return new WaitEvent(TestEvent);
Console.WriteLine("Test event received");
}
private static IEnumerator<Wait> PrintEvery5Seconds() {
while (true) {
yield return new WaitSeconds(10);
Console.WriteLine("The time is " + DateTime.Now);
}
}
}
}

58
Test/Test.csproj Normal file
View file

@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8BE6B559-927D-47A6-8253-D7D809D337AF}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Test</RootNamespace>
<AssemblyName>Test</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Example.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Coroutine\Coroutine.csproj">
<Project>{1657964d-2503-426a-8514-d020660bee4d}</Project>
<Name>Coroutine</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>