提交Unity 联机Pro

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-17 14:27:18 +08:00
parent f00193b000
commit 894100ae37
7448 changed files with 854473 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2a1ee7e98b494ccfadfd9ee103715117
timeCreated: 1723691610

View File

@@ -0,0 +1,42 @@
using System.Net;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGClient : JNTCPClient
{
private IPEndPoint _point;
public void SetPoint(IPEndPoint point)
{
_point = point;
}
protected override async UniTask<IPEndPoint> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
//连接
await base.OnInit();
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.AddState(info);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de07e6b56137456fb6464195f9e1941d
timeCreated: 1722426114

View File

@@ -0,0 +1,30 @@
using System.Threading.Tasks;
using Plugins.JNGame.Network.Group;
namespace AppGame.Systems
{
public class JNGClientGroup : JNClientGroup<JNGClient>
{
//玩家Id
private int clientId;
public int ClientID => clientId;
public override async Task OnInit()
{
clientId = (await App.GAPI.NSyncTileClientId).data;
await base.OnInit();
}
public override void AddClient(JNGClient client)
{
base.AddClient(client);
//向服务器发送玩家Id
client.Send((int)GActionEnum.BindClientID,new RClientIDMessage()
{
ClientId = clientId
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b74afb48ac594bbd8db4d8323f8c2b7f
timeCreated: 1723451509

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using JNGame.Network;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGServer : JNTCPServer
{
private int _index = 1;
private bool isInit = false;
//客户端绑定的Id
private Dictionary<int, int> ids = new();
public override async Task OnInit()
{
if (isInit) return;
isInit = true;
//监听服务端事件
AddListener((int)NActionEnum.NSyncFrameInput,OnNSyncFrameInput);
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
//连接
await base.OnInit();
}
public override void OnClose()
{
isInit = false;
base.OnClose();
}
protected override async UniTask<int> GetPort()
{
return (await App.GAPI.NSyncTilePort).data;
}
/// <summary>
/// 绑定客户端Id
/// </summary>
/// <param name="obj"></param>
/// <exception cref="NotImplementedException"></exception>
private void OnBindClientID(JNServerParam args)
{
var message = RClientIDMessage.Parser.ParseFrom(args.Message);
ids[args.Client] = message.ClientId;
}
/// <summary>
/// 接收帧数入
/// </summary>
/// <param name="args"></param>
private void OnNSyncFrameInput(JNServerParam args)
{
var inputs = JNFrameInputs.Parser.ParseFrom(args.Message);
var frame = new JNFrameInfo();
frame.Index = 0;
foreach (var input in inputs.Inputs)
{
frame.Messages.Add(input);
}
App.Game.AddInput(frame);
}
/// <summary>
/// 接收瓦片输入
/// </summary>
/// <param name="args"></param>
private void OnNSyncTileInput(JNServerParam args)
{
var inputs = JNStateTileInputs.Parser.ParseFrom(args.Message);
//只有绑定过ID 的客户端才可以执行操作
inputs.Message.Inputs.ForEach(child =>
{
child.ClientId = args.Client;
});
App.Game.AddTileInput(inputs);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4d29d51922a6446490e06c87636d2590
timeCreated: 1722426103

View File

@@ -0,0 +1,43 @@
using System.Net;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGTileClient : JNTCPClient
{
private IPEndPoint _point;
public void SetPoint(IPEndPoint point)
{
_point = point;
}
protected override async UniTask<IPEndPoint> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
//连接
await base.OnInit();
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.AddState(info);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 892255e4a0a4489784ba78fef9492936
timeCreated: 1723691588

View File

@@ -0,0 +1,135 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AppGame.Sync;
using JNGame.Sync.Frame;
using JNGame.Sync.State;
using JNGame.Sync.System.Data;
using JNGame.Sync.System.View;
using Plugins.JNGame.System;
namespace AppGame.Systems
{
public class JNGGame : SystemBase
{
/// <summary>
/// 同步组
/// </summary>
private List<JNSyncDefaultService> syncs = new();
private JNSyncDefaultService client;
private List<JNSStateServerService> servers = new();
public bool IsStartGame => syncs.Count > 0 && syncs[0].IsStartGame;
public override async Task OnInit()
{
}
/// <summary>
/// 运行一个同步类
/// </summary>
public T Start<T>() where T : JNSyncDefaultService,new()
{
syncs.Add(new T());
syncs[^1].Initialize();
if (syncs[^1] is JNSStateServerService server)
{
servers.Add(server);
}
else
{
client = syncs[^1];
}
return syncs[^1] as T;
}
/// <summary>
/// 更新周期
/// </summary>
public void Update()
{
foreach (var sync in syncs)
{
sync.Execute();
}
}
/// <summary>
/// 获取第一个客户端的输入类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetInput<T>() where T : JNInputBase, new()
{
return GetClient().GetSystem<JNInputSystem>().Input<T>();
}
/// <summary>
/// 获取第一个客户端
/// </summary>
/// <returns></returns>
public JNSyncDefaultService GetClient()
{
return client;
}
public T GetClient<T>() where T : JNSyncDefaultService
{
return (T)client;
}
/// <summary>
/// 接收输入数据
/// </summary>
/// <param name="frame"></param>
public void AddInput(JNFrameInfo frame)
{
foreach (var sync in syncs)
{
(sync as JNGFrameSystem)?.AddFrame(frame);
(sync as JNGStateServerSystem)?.AddInput(frame);
}
}
public void AddTileInput(JNStateTileInputs frame)
{
foreach (var sync in syncs)
{
if (sync is JNGTileServerSystem system)
{
if (system.TID == frame.TId || frame.TId == 0)
{
system.AddInput(frame);
}
}
}
}
/// <summary>
/// 接收状态数据
/// </summary>
/// <param name="frame"></param>
public void AddState(JNStateItemData frame)
{
var message = new Dictionary<long, byte[]>();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
foreach (var sync in syncs)
{
sync.GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bf6b9c1fec19438fa9b636bc2e7e4df1
timeCreated: 1722495304

View File

@@ -0,0 +1,12 @@

using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
public class JNGSocket : JNSocket
{
protected override async UniTask<string> GetUrl()
{
await UniTask.NextFrame();
return "ws://127.0.0.1:8080/websocket";
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa09b61b047f4c98bd0b5e3dc2fad6aa
timeCreated: 1721384427