Skip to content

Commit e348617

Browse files
authored
Merge branch 'unity-client' into resolve-conflicts
2 parents e38302a + a9fd7d0 commit e348617

21 files changed

+1613
-0
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ Please move to the [unity-client branch](https://github.com/youngmonkeys/ezyfox-
1313

1414
[https://youngmonkeys.org/ezyfox-csharp-client-sdk/](https://youngmonkeys.org/ezyfox-csharp-client-sdk/)
1515

16+
# How to use?
17+
* Since our project depends on `Newtonsoft Json` unity package, you first need to add it to your Unity project in either one of the following ways:
18+
- Select `Window` >> `Package Manager` >> :heavy_plus_sign: icon >> `Add package from git URL` >> Add `com.unity.nuget.newtonsoft-json` to URL field >> Add
19+
- Edit the `manifest.json` file in the `Packages` folder of your Unity project and add "com.unity.nuget.newtonsoft-json": "3.0.2"
20+
* Clone/copy this repository into your unity project
21+
* Your Unity project can now start using scripts from this repository
22+
1623
# Code Example
1724

1825
**1. Import**
@@ -95,6 +102,12 @@ while(true)
95102
}
96103
```
97104

105+
# Backward compatibility
106+
- **v1.1.4-unity**: all classes extending MonoBehaviour have been made abstract. To use these classes in
107+
a Unity project, it is necessary to generate concrete classes that inherit from these abstract classes and
108+
subsequently attach them to the desired Unity components. E.g., `EzyDefaultController`, `EzyEventProcessor`,
109+
`EzyUnityLoggerFactory`, `EzySocketConfigHolderVariable`, and `EzySocketConfigVariable`.
110+
98111
# Used By
99112

100113
1. [Defi Warrior](https://gamefi.org/games/defi-warrior)

jsproxy/proxy.jslib

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
var EzyFoxServerClientPlugin = {
2+
$ezy: {
3+
client: null,
4+
setup: null,
5+
setupApp: null,
6+
handshakeHandler: null,
7+
accessAppHandler: null,
8+
zoneName: null,
9+
appName: null,
10+
eventHandlerCallback: null,
11+
dataHandlerCallback: null,
12+
toUTF8: function (s) {
13+
var sLength = lengthBytesUTF8(s) + 1;
14+
var sPtr = _malloc(sLength);
15+
stringToUTF8(s, sPtr, sLength);
16+
return sPtr;
17+
},
18+
map: {
19+
'init': function (clientName, jsonConfig, callback) {
20+
var config = JSON.parse(jsonConfig);
21+
config.getClientName = function () {
22+
return this.clientName;
23+
}
24+
var clients = EzyClients.getInstance();
25+
ezy.client = clients.newClient(config);
26+
ezy.setup = ezy.client.setup;
27+
28+
for (var ezyEventType in EzyEventType) {
29+
if (EzyEventType.hasOwnProperty(ezyEventType)) {
30+
var eventHandler = {"ezyEventType": ezyEventType, "clientName": clientName};
31+
eventHandler.handle = function (event) {
32+
var jsonData = event ? JSON.stringify(event) : "{}";
33+
EzyLogger.console(this.clientName);
34+
EzyLogger.console(this.ezyEventType);
35+
EzyLogger.console(jsonData);
36+
dynCall_viii(
37+
ezy.eventHandlerCallback,
38+
ezy.toUTF8(this.clientName),
39+
ezy.toUTF8(this.ezyEventType),
40+
ezy.toUTF8(jsonData)
41+
);
42+
}
43+
ezy.setup.addEventHandler(ezyEventType, eventHandler);
44+
}
45+
}
46+
47+
for (var commandId in EzyCommands) {
48+
if (EzyCommands.hasOwnProperty(commandId)) {
49+
var dataHandler = {"cmd": EzyCommands[commandId], "clientName": clientName};
50+
dataHandler.handle = function (data) {
51+
var jsonData = data ? JSON.stringify(data) : "{}";
52+
EzyLogger.console(this.clientName);
53+
EzyLogger.console(this.cmd);
54+
EzyLogger.console(jsonData);
55+
dynCall_viii(
56+
ezy.dataHandlerCallback,
57+
ezy.toUTF8(this.clientName),
58+
this.cmd.id,
59+
ezy.toUTF8(jsonData)
60+
);
61+
}
62+
ezy.setup.addDataHandler(EzyCommands[commandId], dataHandler);
63+
}
64+
}
65+
66+
dynCall_vii(callback, ezy.toUTF8(clientName), ezy.toUTF8(jsonConfig));
67+
},
68+
'connect': function (clientName, jsonData, callback) {
69+
var data = JSON.parse(jsonData);
70+
ezy.client.connect(data.url);
71+
},
72+
'reconnect': function (clientName, callback) {
73+
EzyLogger.console('reconnect: clientName = ' + clientName);
74+
ezy.client.reconnect();
75+
},
76+
'disconnect': function (clientName, jsonData, callback) {
77+
EzyLogger.console('disconnect: clientName = ' + clientName + ', jsonData = ' + jsonData);
78+
var data = JSON.parse(jsonData);
79+
ezy.client.disconnect(data.reason);
80+
},
81+
'send': function (clientName, jsonData, callback) {
82+
var data = JSON.parse(jsonData);
83+
var cmd = EzyCommands[data.cmdId];
84+
var sendData = data.data;
85+
EzyClients.getInstance()
86+
.getClient(clientName)
87+
.send(cmd, sendData);
88+
},
89+
'startPing': function (clientName, callback) {
90+
EzyLogger.console('start ping: clientName = ' + clientName);
91+
ezy.client.pingSchedule.start();
92+
},
93+
'stopPing': function (clientName, callback) {
94+
EzyLogger.console('start ping: clientName = ' + clientName);
95+
ezy.client.pingSchedule.stop();
96+
},
97+
}
98+
},
99+
100+
setEventHandlerCallback: function (callback) {
101+
ezy.eventHandlerCallback = callback;
102+
},
103+
104+
setDataHandlerCallback: function (callback) {
105+
ezy.dataHandlerCallback = callback;
106+
},
107+
108+
setDebug: function (value) {
109+
EzyLogger.debug = value;
110+
},
111+
112+
run4: function (clientName, functionName, jsonData, callback) {
113+
var clientNameString = UTF8ToString(clientName);
114+
var functionNameString = UTF8ToString(functionName);
115+
var jsonDataString = UTF8ToString(jsonData);
116+
EzyLogger.console(
117+
'run4(clientName=' + clientNameString + ', functionName=' +
118+
functionNameString + ', jsonData=' + jsonDataString + ')'
119+
);
120+
ezy.map[functionNameString](clientNameString, jsonDataString, callback);
121+
},
122+
123+
run3: function (clientName, functionName, callback) {
124+
var clientNameString = UTF8ToString(clientName);
125+
var functionNameString = UTF8ToString(functionName);
126+
EzyLogger.console('run3(clientName=' + clientNameString + ', functionName=' + functionNameString + ')');
127+
ezy.map[functionNameString](clientNameString, callback);
128+
}
129+
};
130+
131+
autoAddDeps(EzyFoxServerClientPlugin, '$ezy');
132+
mergeInto(LibraryManager.library, EzyFoxServerClientPlugin);

unity/EzyAbstractController.cs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using com.tvd12.ezyfoxserver.client.binding;
4+
using com.tvd12.ezyfoxserver.client.config;
5+
using com.tvd12.ezyfoxserver.client.evt;
6+
using com.tvd12.ezyfoxserver.client.logger;
7+
using com.tvd12.ezyfoxserver.client.support;
8+
using UnityEngine;
9+
using Object = System.Object;
10+
11+
namespace com.tvd12.ezyfoxserver.client.unity
12+
{
13+
public abstract class EzyAbstractController : MonoBehaviour
14+
{
15+
protected EzySocketProxy socketProxy;
16+
protected EzyAppProxy appProxy;
17+
18+
private readonly List<Object> socketHandlers = new List<Object>();
19+
private readonly List<Tuple<String, Object>> appHandlers =
20+
new List<Tuple<string, Object>>();
21+
22+
protected static readonly EzyLogger LOGGER = EzyUnityLoggerFactory
23+
.getLogger<EzyAbstractController>();
24+
25+
protected virtual EzyBinding CreateBinding()
26+
{
27+
return null;
28+
}
29+
30+
protected void OnEnable()
31+
{
32+
var socketConfig = GetSocketConfig();
33+
var socketProxyManager = EzySocketProxyManager.getInstance();
34+
if (!socketProxyManager.hasInited())
35+
{
36+
var binding = CreateBinding();
37+
if (binding != null)
38+
{
39+
socketProxyManager.init(binding);
40+
}
41+
else
42+
{
43+
socketProxyManager.init();
44+
}
45+
}
46+
socketProxy = socketProxyManager.getSocketProxy(
47+
socketConfig.ZoneName
48+
);
49+
if (socketProxy.getClient() == null)
50+
{
51+
var config = EzyClientConfig.builder()
52+
.clientName(socketConfig.ZoneName)
53+
.zoneName(socketConfig.ZoneName)
54+
.enableSSL(socketConfig.EnableSSL)
55+
.build();
56+
EzyClientFactory
57+
.getInstance()
58+
.getOrCreateClient(
59+
config,
60+
socketConfig.UdpUsage
61+
);
62+
}
63+
appProxy = socketProxy.getAppProxy(
64+
socketConfig.AppName,
65+
true
66+
);
67+
}
68+
69+
protected abstract EzySocketConfig GetSocketConfig();
70+
71+
protected void OnLoginSuccess<T>(EzySocketProxyDataHandler<T> handler)
72+
{
73+
socketHandlers.Add(
74+
socketProxy.onLoginSuccess(handler)
75+
);
76+
}
77+
78+
protected void OnLoginError<T>(EzySocketProxyDataHandler<T> handler)
79+
{
80+
socketHandlers.Add(
81+
socketProxy.onLoginError(handler)
82+
);
83+
}
84+
85+
protected void OnUdpHandshake<T>(EzySocketProxyDataHandler<T> handler)
86+
{
87+
socketHandlers.Add(
88+
socketProxy.onUdpHandshake(handler)
89+
);
90+
}
91+
92+
protected void OnAppAccessed<T>(EzyAppProxyDataHandler<T> handler)
93+
{
94+
socketHandlers.Add(
95+
socketProxy.onAppAccessed(handler)
96+
);
97+
}
98+
99+
protected void OnDisconnected(EzySocketProxyEventHandler<EzyDisconnectionEvent> handler)
100+
{
101+
socketHandlers.Add(
102+
socketProxy.onDisconnected(handler)
103+
);
104+
}
105+
106+
protected void OnReconnecting(EzySocketProxyEventHandler<EzyDisconnectionEvent> handler)
107+
{
108+
socketHandlers.Add(
109+
socketProxy.onReconnecting(handler)
110+
);
111+
}
112+
113+
protected void OnPingLost(EzySocketProxyEventHandler<EzyLostPingEvent> handler)
114+
{
115+
socketHandlers.Add(
116+
socketProxy.onPingLost(handler)
117+
);
118+
}
119+
120+
protected void OnTryConnect(EzySocketProxyEventHandler<EzyTryConnectEvent> handler)
121+
{
122+
socketHandlers.Add(
123+
socketProxy.onTryConnect(handler)
124+
);
125+
}
126+
127+
protected void AddHandler<T>(String cmd, EzyAppProxyDataHandler<T> handler)
128+
{
129+
appHandlers.Add(
130+
new Tuple<String, Object>(cmd, appProxy.on(cmd, handler))
131+
);
132+
}
133+
134+
protected void Disconnect() {
135+
socketProxy.disconnect();
136+
}
137+
138+
protected virtual void OnDestroy()
139+
{
140+
UnbindSocketHandlers();
141+
UnbindAppHandlers();
142+
}
143+
144+
protected virtual void UnbindSocketHandlers()
145+
{
146+
foreach (var socketProxyHandler in socketHandlers)
147+
{
148+
socketProxy.unbind(socketProxyHandler);
149+
}
150+
}
151+
152+
protected virtual void UnbindAppHandlers()
153+
{
154+
foreach (Tuple<String, Object> tuple in appHandlers)
155+
{
156+
appProxy.unbind(tuple.Item1, tuple.Item2);
157+
}
158+
}
159+
}
160+
}

unity/EzyAbstractEventProcessor.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using UnityEngine;
2+
3+
namespace com.tvd12.ezyfoxserver.client.unity
4+
{
5+
public abstract class EzyAbstractEventProcessor : MonoBehaviour
6+
{
7+
private static EzyAbstractEventProcessor INSTANCE;
8+
9+
private void Awake()
10+
{
11+
// If go back to current scene, don't make duplication
12+
if (INSTANCE != null)
13+
{
14+
Destroy(gameObject);
15+
}
16+
else
17+
{
18+
INSTANCE = this;
19+
DontDestroyOnLoad(gameObject);
20+
}
21+
}
22+
23+
void Update()
24+
{
25+
// Main thread pulls data from socket
26+
#if UNITY_WEBGL && !UNITY_EDITOR
27+
#else
28+
EzyClients.getInstance()
29+
.getClient(GetZoneName())
30+
.processEvents();
31+
#endif
32+
}
33+
34+
protected abstract string GetZoneName();
35+
}
36+
}

0 commit comments

Comments
 (0)