Skip to content

Commit d4e3f58

Browse files
committed
Moved code to namespace, added ValueStopwatch to wrap it, changed to return ticks
1 parent 3ba7247 commit d4e3f58

8 files changed

Lines changed: 56 additions & 23 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
/Runtime/Plugins/*/*.pdb
1111
/cmake-build-debug
1212
/cmake-build-release
13-
*.meta
13+
*.meta
14+
/out/*

NativeTime.Plugin/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include_directories(PluginAPI)
22

3-
add_library(NativeTimeoNative SHARED
3+
add_library(NativeTimeNative SHARED
44
PluginAPI/IUnityEventQueue.h
55
PluginAPI/IUnityGraphics.h
66
PluginAPI/IUnityGraphicsD3D11.h
@@ -14,7 +14,7 @@ add_library(NativeTimeoNative SHARED
1414
framework.h
1515
NativeTime.cpp
1616
NativeTime.h
17-
DirectX11.cpp
17+
Windows.cpp
1818
Osx.cpp
1919
)
2020

@@ -24,10 +24,6 @@ else()
2424
target_precompile_headers(NativeTimeNative PRIVATE pch.h)
2525
endif()
2626

27-
if (WIN32)
28-
target_sources(NativeTimeNative PRIVATE dllmain.cpp)
29-
endif()
30-
3127
if (APPLE)
3228
set(CMAKE_C_FLAGS "-x objective-c")
3329
add_compile_options("-dynamiclib")

NativeTime.Plugin/NativeTime.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,3 @@ extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API
2121
UnityPluginUnload()
2222
{
2323
}
24-

NativeTime.Plugin/Windows.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern "C" int64_t UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetTimestamp()
1919

2020
LARGE_INTEGER time;
2121
QueryPerformanceCounter(&time);
22-
time.QuadPart *= 1000000;
22+
time.QuadPart *= 10000000;
2323
time.QuadPart /= s_frequency.QuadPart;
2424
return time.QuadPart;
2525
}

NativeTime.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,28 @@ EndProject
2020
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Plugins", "Plugins", "{AAA36D2A-6A46-4997-91F3-58947873E9B2}"
2121
EndProject
2222
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "x86", "x86", "{21CE29B1-C45D-42CE-8DDB-1901E2F576FF}"
23+
ProjectSection(SolutionItems) = preProject
24+
Runtime\Plugins\x86\NativeTimeNative.dll = Runtime\Plugins\x86\NativeTimeNative.dll
25+
Runtime\Plugins\x86\NativeTimeNative.dll.meta = Runtime\Plugins\x86\NativeTimeNative.dll.meta
26+
Runtime\Plugins\x86\NativeTimeNative.exp = Runtime\Plugins\x86\NativeTimeNative.exp
27+
Runtime\Plugins\x86\NativeTimeNative.exp.meta = Runtime\Plugins\x86\NativeTimeNative.exp.meta
28+
Runtime\Plugins\x86\NativeTimeNative.lib = Runtime\Plugins\x86\NativeTimeNative.lib
29+
Runtime\Plugins\x86\NativeTimeNative.lib.meta = Runtime\Plugins\x86\NativeTimeNative.lib.meta
30+
Runtime\Plugins\x86\NativeTimeNative.pdb = Runtime\Plugins\x86\NativeTimeNative.pdb
31+
Runtime\Plugins\x86\NativeTimeNative.pdb.meta = Runtime\Plugins\x86\NativeTimeNative.pdb.meta
32+
EndProjectSection
2333
EndProject
2434
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "x86_64", "x86_64", "{AFF56142-A429-4228-AC3C-B0C5D4635869}"
35+
ProjectSection(SolutionItems) = preProject
36+
Runtime\Plugins\x86_64\NativeTimeNative.dll = Runtime\Plugins\x86_64\NativeTimeNative.dll
37+
Runtime\Plugins\x86_64\NativeTimeNative.dll.meta = Runtime\Plugins\x86_64\NativeTimeNative.dll.meta
38+
Runtime\Plugins\x86_64\NativeTimeNative.exp = Runtime\Plugins\x86_64\NativeTimeNative.exp
39+
Runtime\Plugins\x86_64\NativeTimeNative.exp.meta = Runtime\Plugins\x86_64\NativeTimeNative.exp.meta
40+
Runtime\Plugins\x86_64\NativeTimeNative.lib = Runtime\Plugins\x86_64\NativeTimeNative.lib
41+
Runtime\Plugins\x86_64\NativeTimeNative.lib.meta = Runtime\Plugins\x86_64\NativeTimeNative.lib.meta
42+
Runtime\Plugins\x86_64\NativeTimeNative.pdb = Runtime\Plugins\x86_64\NativeTimeNative.pdb
43+
Runtime\Plugins\x86_64\NativeTimeNative.pdb.meta = Runtime\Plugins\x86_64\NativeTimeNative.pdb.meta
44+
EndProjectSection
2545
EndProject
2646
Global
2747
GlobalSection(SolutionConfigurationPlatforms) = preSolution

Runtime/NativeTime.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
using System;
22
using System.Runtime.InteropServices;
33

4-
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
5-
internal class NativeTimeInternal
4+
namespace StrangeLoopGames.NativeTime
65
{
7-
[DllImport("NativeTimeNative")]
8-
public static extern long GetTimestamp();
9-
}
6+
internal class NativeTimeInternal
7+
{
8+
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX || UNITY_STANDALONE_LINUX || UNITY_EDITOR_LINUX
9+
[DllImport("NativeTimeNative")]
10+
public static extern long GetTimestamp();
1011
#else
11-
internal class NativeTimeInternal
12-
{
13-
public static long GetTimestamp() { return 0; }
14-
}
12+
/// <summary> Fallback for unsupported platform </summary>
13+
[BurstCompatible]
14+
public static long GetTimestamp() { return 0; }
1515
#endif
16+
}
1617

17-
public class NativeTime
18-
{
19-
/// <summary> Returns the number of microseconds since system startup. </summary>
20-
public static long GetTimestamp() => NativeTimeInternal.GetTimestamp();
21-
}
18+
/// <summary>A burst compatible Stopwatch. Similar to <see cref="Stopwatch"/>.</summary>
19+
[BurstCompatible]
20+
public struct ValueStopwatch
21+
{
22+
long start;
23+
24+
/// <summary> Returns the number of ticks since system startup. </summary>
25+
public static long GetTimestamp() => NativeTimeInternal.GetTimestamp();
26+
27+
public TimeSpan ElapsedTicks()
28+
{
29+
long elapsed = GetTimestamp() - this.start;
30+
return new TimeSpan(elapsed);
31+
}
32+
33+
public static ValueStopwatch StartNew()
34+
{
35+
return new ValueStopwatch { start = GetTimestamp() };
36+
}
37+
}
38+
}
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)