-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47552e9
commit aee324c
Showing
13 changed files
with
354 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Scellecs.Morpeh | ||
{ | ||
public interface IRequestData | ||
{ | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#if UNITY_EDITOR | ||
#define MORPEH_DEBUG | ||
#endif | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using JetBrains.Annotations; | ||
using Scellecs.Morpeh.Collections; | ||
using Unity.IL2CPP.CompilerServices; | ||
using UnityEngine; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
[Il2CppSetOption(Option.NullChecks, false)] | ||
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] | ||
[Il2CppSetOption(Option.DivideByZeroChecks, false)] | ||
public class Request<TData> : RequestBase where TData : struct, IRequestData | ||
{ | ||
internal readonly FastList<TData> changes = new FastList<TData>(); | ||
|
||
#if MORPEH_DEBUG | ||
internal int lastConsumeFrame; | ||
#endif | ||
internal int lastConsumedIndex; | ||
|
||
[PublicAPI] | ||
public void Publish(in TData request, bool allowNextFrame = false) | ||
{ | ||
#if MORPEH_DEBUG | ||
if (!allowNextFrame && lastConsumeFrame == Time.frameCount) | ||
{ | ||
MLogger.LogError( | ||
"The request was already consumed in the current frame. " + | ||
"Reorder systems or set allowNextFrame parameter"); | ||
} | ||
#endif | ||
|
||
changes.Add(request); | ||
} | ||
|
||
[PublicAPI] | ||
public Consumer Consume() | ||
{ | ||
#if MORPEH_DEBUG | ||
lastConsumeFrame = Time.frameCount; | ||
#endif | ||
|
||
if (lastConsumedIndex > 0) | ||
{ | ||
Cleanup(); | ||
} | ||
|
||
Consumer consumer; | ||
consumer.request = this; | ||
return consumer; | ||
} | ||
|
||
internal void Cleanup() | ||
{ | ||
if (lastConsumedIndex >= changes.length) | ||
{ | ||
changes.Clear(); | ||
lastConsumedIndex = 0; | ||
return; | ||
} | ||
|
||
Array.Copy(changes.data, lastConsumedIndex, changes.data, 0, changes.length - lastConsumedIndex); | ||
|
||
changes.length -= lastConsumedIndex; | ||
changes.lastSwappedIndex = -1; | ||
|
||
for (var i = 0; i < lastConsumedIndex; i++) | ||
{ | ||
changes.data[i + changes.length] = default; | ||
} | ||
|
||
lastConsumedIndex = 0; | ||
} | ||
|
||
public struct Consumer | ||
{ | ||
public Request<TData> request; | ||
|
||
public Enumerator GetEnumerator() | ||
{ | ||
Enumerator e; | ||
e.request = request; | ||
e.current = default; | ||
return e; | ||
} | ||
} | ||
|
||
public struct Enumerator | ||
{ | ||
public Request<TData> request; | ||
public TData current; | ||
|
||
public bool MoveNext() | ||
{ | ||
if (request.lastConsumedIndex >= request.changes.length) | ||
{ | ||
return false; | ||
} | ||
|
||
current = request.changes.data[request.lastConsumedIndex++]; | ||
return true; | ||
} | ||
|
||
public TData Current | ||
{ | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get => this.current; | ||
} | ||
} | ||
} | ||
|
||
public abstract class RequestBase | ||
{ | ||
internal RequestRegistry registry; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
internal class RequestRegistry | ||
{ | ||
internal readonly Dictionary<Type, RequestBase> RegisteredRequests = new Dictionary<Type, RequestBase>(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
|
||
namespace Scellecs.Morpeh | ||
{ | ||
public static class RequestWorldExtensions | ||
{ | ||
[PublicAPI] | ||
public static Request<TData> GetRequest<TData>(this World world) | ||
where TData : struct, IRequestData | ||
{ | ||
var type = typeof(TData); | ||
var registry = world.CodeWriterRequestsRegistry; | ||
|
||
if (registry.RegisteredRequests.TryGetValue(type, out var registeredRequest)) | ||
{ | ||
return (Request<TData>) registeredRequest; | ||
} | ||
|
||
registeredRequest = new Request<TData>(); | ||
registeredRequest.registry = registry; | ||
|
||
registry.RegisteredRequests.Add(type, registeredRequest); | ||
|
||
return (Request<TData>) registeredRequest; | ||
} | ||
|
||
[PublicAPI] | ||
public static RequestBase GetReflectionRequest(this World world, Type type) | ||
{ | ||
var registry = world.CodeWriterRequestsRegistry; | ||
|
||
if (registry.RegisteredRequests.TryGetValue(type, out var registeredRequest)) | ||
{ | ||
return registeredRequest; | ||
} | ||
|
||
var constructedType = typeof(Request<>).MakeGenericType(type); | ||
registeredRequest = (RequestBase) Activator.CreateInstance(constructedType, true); | ||
registeredRequest.registry = registry; | ||
|
||
registry.RegisteredRequests.Add(type, registeredRequest); | ||
|
||
return registeredRequest; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.