Skip to content

Commit 01469c0

Browse files
Save invokers in resource instead of statically and create public usable ChannelTickSchedulerFactory
1 parent 4e894ca commit 01469c0

File tree

7 files changed

+56
-33
lines changed

7 files changed

+56
-33
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Threading;
2+
3+
namespace AltV.Net.Async
4+
{
5+
public class ChannelTickSchedulerFactory : ITickSchedulerFactory
6+
{
7+
public TickScheduler Create(Thread mainThread)
8+
{
9+
return new ChannelTickScheduler(mainThread);
10+
}
11+
}
12+
}

api/AltV.Net/Native/AltV.Invoker.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ internal class Invoker : IDisposable
88
{
99
public static Invoker Create(MValue.Function function)
1010
{
11-
return new Invoker(AltNative.MValueCreate.Invoker_Create(function));
11+
return new Invoker(
12+
AltNative.MValueCreate.Invoker_Create(Alt.Module.CSharpNativeResource.NativePointer, function));
1213
}
1314

1415
internal IntPtr NativePointer { get; }
@@ -20,7 +21,7 @@ private Invoker(IntPtr nativePointer)
2021

2122
public void Destroy()
2223
{
23-
AltNative.MValueCreate.Invoker_Destroy(NativePointer);
24+
AltNative.MValueCreate.Invoker_Destroy(Alt.Module.CSharpNativeResource.NativePointer, NativePointer);
2425
}
2526

2627
public void Dispose()

api/AltV.Net/Native/AltV.MValue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ internal static partial class AltNative
1212
internal static class MValueCreate
1313
{
1414
[DllImport(DllName, CallingConvention = NativeCallingConvention)]
15-
internal static extern IntPtr Invoker_Create(MValue.Function function);
15+
internal static extern IntPtr Invoker_Create(IntPtr csharpResourcePointer, MValue.Function function);
1616

1717
[DllImport(DllName, CallingConvention = NativeCallingConvention)]
18-
internal static extern void Invoker_Destroy(IntPtr invokerPointer);
18+
internal static extern void Invoker_Destroy(IntPtr csharpResourcePointer, IntPtr invokerPointer);
1919

2020
[DllImport(DllName, CallingConvention = NativeCallingConvention)]
2121
internal static extern void MValue_CreateNil(ref MValue mValue);

runtime/include/CSharpResource.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@
6262
#pragma clang diagnostic pop
6363
#endif
6464

65+
typedef void (* MValueFunctionCallback)(alt::MValueList*, alt::MValue*);
66+
67+
class CustomInvoker : public alt::MValueFunction::Invoker {
68+
public:
69+
MValueFunctionCallback mValueFunctionCallback;
70+
71+
explicit CustomInvoker(MValueFunctionCallback mValueFunctionCallback) {
72+
this->mValueFunctionCallback = mValueFunctionCallback;
73+
}
74+
75+
alt::MValue Invoke(alt::MValueList args) override {
76+
//auto list = args.Get<alt::MValue::List>();
77+
alt::MValue result;
78+
mValueFunctionCallback(&args, &result);
79+
return result;
80+
}
81+
};
82+
6583
typedef void (* MainDelegate_t)(alt::IServer* server, alt::IResource* resource, const char* resourceName,
6684
const char* entryPoint);
6785
typedef void (* TickDelegate_t)();
@@ -194,6 +212,8 @@ class CSharpResource : public alt::IResource {
194212

195213
void* runtimeHost;
196214
unsigned int domainId;
215+
216+
alt::Array<CustomInvoker*>* invokers;
197217
};
198218

199219
EXPORT void CSharpResource_SetExport(CSharpResource* resource, const char* key, const alt::MValue &val);

runtime/src/CSharpResource.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ auto resourcesCache = new alt::Array<CSharpResource*>;
77
CSharpResource::CSharpResource(alt::IServer* server, CoreClr* coreClr, alt::IResource::CreationInfo* info)
88
: alt::IResource(info) {
99
this->server = server;
10+
this->invokers = new alt::Array<CustomInvoker*>();
1011
resourcesCache->Push(this);
1112

1213
auto isDll = true;
@@ -207,8 +208,9 @@ bool CSharpResource::Start() {
207208

208209
bool CSharpResource::Stop() {
209210
alt::IResource::Stop();
210-
for (int i = 0; i < invokers.GetSize(); i++) {
211-
delete invokers[i];
211+
this->server->LogInfo("resource stopping");
212+
for (int i = 0, length = invokers->GetSize(); i < length; i++) {
213+
delete invokers->operator[](i);
212214
}
213215
if (OnStopDelegate == nullptr) return false;
214216
OnStopDelegate();

runtime/src/altv-c-api/mvalue.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
#include "mvalue.h"
22

3-
alt::Array<CustomInvoker*> invokers;
4-
5-
CustomInvoker* Invoker_Create(MValueFunctionCallback val) {
3+
CustomInvoker* Invoker_Create(CSharpResource* resource, MValueFunctionCallback val) {
64
auto invoker = new CustomInvoker(val);
7-
invokers.Push(invoker);
5+
resource->invokers->Push(invoker);
86
return invoker;
97
}
108

11-
void Invoker_Destroy(CustomInvoker* val) {
9+
void Invoker_Destroy(CSharpResource* resource, CustomInvoker* val) {
10+
auto newInvokers = new alt::Array<CustomInvoker*>();
11+
for (int i = 0, length = resource->invokers->GetSize(); i < length; i++) {
12+
auto invoker = resource->invokers->operator[](i);
13+
if (invoker != val) {
14+
newInvokers->Push(invoker);
15+
}
16+
}
1217
delete val;
18+
delete resource->invokers;
19+
resource->invokers = newInvokers;
1320
}
1421

1522
void MValue_CreateNil(alt::MValue &mValue) {

runtime/src/altv-c-api/mvalue.h

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,18 @@
77
#endif
88

99
#include <altv-cpp-api/SDK.h>
10+
#include <CSharpResource.h>
1011

1112
#ifdef __clang__
1213
#pragma clang diagnostic pop
1314
#endif
1415

15-
typedef void (* MValueFunctionCallback)(alt::MValueList*, alt::MValue*);
16-
17-
class CustomInvoker : public alt::MValueFunction::Invoker {
18-
public:
19-
MValueFunctionCallback mValueFunctionCallback;
20-
21-
explicit CustomInvoker(MValueFunctionCallback mValueFunctionCallback) {
22-
this->mValueFunctionCallback = mValueFunctionCallback;
23-
}
24-
25-
alt::MValue Invoke(alt::MValueList args) override {
26-
//auto list = args.Get<alt::MValue::List>();
27-
alt::MValue result;
28-
mValueFunctionCallback(&args, &result);
29-
return result;
30-
}
31-
};
32-
33-
extern alt::Array<CustomInvoker*> invokers;
34-
3516
#ifdef __cplusplus
3617
extern "C"
3718
{
3819
#endif
39-
EXPORT CustomInvoker* Invoker_Create(MValueFunctionCallback val);
40-
EXPORT void Invoker_Destroy(CustomInvoker* val);
20+
EXPORT CustomInvoker* Invoker_Create(CSharpResource* resource, MValueFunctionCallback val);
21+
EXPORT void Invoker_Destroy(CSharpResource* resource, CustomInvoker* val);
4122

4223
EXPORT void MValue_CreateNil(alt::MValue &mValue);
4324
EXPORT void MValue_CreateBool(bool val, alt::MValue &mValue);

0 commit comments

Comments
 (0)