Commit 929de022 authored by hex's avatar hex

Changed card addition from double-click to long-press (good news for those with tenosynovitis)

parent 5cf5390a
Pipeline #37618 failed
using System; using System;
using System.Net.Sockets; using System.Collections.Generic;
using YGOSharp.Network.Enums;
using UnityEngine;
using System.IO; using System.IO;
using System.Threading; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Collections.Generic; using System.Threading;
using UnityEngine;
using YGOSharp.Network.Enums;
using YGOSharp.OCGWrapper.Enums; using YGOSharp.OCGWrapper.Enums;
public static class TcpHelper public static class TcpHelper
...@@ -17,7 +17,13 @@ public static class TcpHelper ...@@ -17,7 +17,13 @@ public static class TcpHelper
static bool canjoin = true; static bool canjoin = true;
static bool roomListChecking = false; static bool roomListChecking = false;
public static void join(string ipString, string name, string portString, string pswString, string version) public static void join(
string ipString,
string name,
string portString,
string pswString,
string version
)
{ {
if (canjoin) if (canjoin)
{ {
...@@ -26,7 +32,11 @@ public static class TcpHelper ...@@ -26,7 +32,11 @@ public static class TcpHelper
canjoin = false; canjoin = false;
try try
{ {
tcpClient = new TcpClientWithTimeout(ipString, int.Parse(portString), 3000).Connect(); tcpClient = new TcpClientWithTimeout(
ipString,
int.Parse(portString),
3000
).Connect();
networkStream = tcpClient.GetStream(); networkStream = tcpClient.GetStream();
Thread t = new Thread(receiver); Thread t = new Thread(receiver);
t.Start(); t.Start();
...@@ -59,7 +69,9 @@ public static class TcpHelper ...@@ -59,7 +69,9 @@ public static class TcpHelper
{ {
try try
{ {
while (tcpClient != null && networkStream != null && tcpClient.Connected && Program.Running) while (
tcpClient != null && networkStream != null && tcpClient.Connected && Program.Running
)
{ {
byte[] data = SocketMaster.ReadPacket(networkStream); byte[] data = SocketMaster.ReadPacket(networkStream);
addDateJumoLine(data); addDateJumoLine(data);
...@@ -72,7 +84,6 @@ public static class TcpHelper ...@@ -72,7 +84,6 @@ public static class TcpHelper
onDisConnected = true; onDisConnected = true;
Program.DEBUGLOG("onDisConnected 3"); Program.DEBUGLOG("onDisConnected 3");
} }
} }
public static void addDateJumoLine(byte[] data) public static void addDateJumoLine(byte[] data)
...@@ -95,7 +106,7 @@ public static class TcpHelper ...@@ -95,7 +106,7 @@ public static class TcpHelper
public static void preFrameFunction() public static void preFrameFunction()
{ {
if (datas.Count>0) if (datas.Count > 0)
{ {
if (Monitor.TryEnter(datas)) if (Monitor.TryEnter(datas))
{ {
...@@ -198,23 +209,34 @@ public static class TcpHelper ...@@ -198,23 +209,34 @@ public static class TcpHelper
{ {
onDisConnected = false; onDisConnected = false;
Program.I().ocgcore.setDefaultReturnServant(); Program.I().ocgcore.setDefaultReturnServant();
if (TcpHelper.tcpClient != null)
try
{ {
if (TcpHelper.tcpClient.Connected) if (tcpClient != null)
{ {
tcpClient.Client.Shutdown(0); if (tcpClient.Connected)
{
tcpClient.Client.Shutdown(SocketShutdown.Both);
}
tcpClient.Close(); tcpClient.Close();
} }
} }
catch (Exception e)
{
Debug.LogWarning("Socket cleanup failed: " + e.Message);
}
tcpClient = null; tcpClient = null;
if (Program.I().ocgcore.isShowed == false) if (Program.I().ocgcore.isShowed == false)
{ {
if (Program.I().menu.isShowed == false) if (Program.I().menu.isShowed == false)
{ {
if (Program.I().mycard.isMatching) { if (Program.I().mycard.isMatching)
{
Program.I().shiftToServant(Program.I().mycard); Program.I().shiftToServant(Program.I().mycard);
} else { }
else
{
Program.I().shiftToServant(Program.I().selectServer); Program.I().shiftToServant(Program.I().selectServer);
} }
} }
...@@ -225,10 +247,13 @@ public static class TcpHelper ...@@ -225,10 +247,13 @@ public static class TcpHelper
} }
else else
{ {
Program.I().cardDescription.RMSshow_none(InterString.Get("对方离开游戏,您现在可以截图。")); Program
.I()
.cardDescription.RMSshow_none(
InterString.Get("对方离开游戏,您现在可以截图。")
);
Program.I().ocgcore.forceMSquit(); Program.I().ocgcore.forceMSquit();
} }
} }
} }
...@@ -269,7 +294,6 @@ public static class TcpHelper ...@@ -269,7 +294,6 @@ public static class TcpHelper
public static void CtosMessage_Response(byte[] response) public static void CtosMessage_Response(byte[] response)
{ {
Package message = new Package(); Package message = new Package();
message.Fuction = (int)CtosMessage.Response; message.Fuction = (int)CtosMessage.Response;
message.Data.writer.Write(response); message.Data.writer.Write(response);
...@@ -277,6 +301,7 @@ public static class TcpHelper ...@@ -277,6 +301,7 @@ public static class TcpHelper
} }
public static YGOSharp.Deck deck; public static YGOSharp.Deck deck;
public static void CtosMessage_UpdateDeck(YGOSharp.Deck deckFor) public static void CtosMessage_UpdateDeck(YGOSharp.Deck deckFor)
{ {
if (deckFor.Main.Count == 0) if (deckFor.Main.Count == 0)
...@@ -335,12 +360,11 @@ public static class TcpHelper ...@@ -335,12 +360,11 @@ public static class TcpHelper
Send(message); Send(message);
} }
public static void CtosMessage_CreateGame() public static void CtosMessage_CreateGame() { }
{
}
public static List<string> deckStrings = new List<string>(); public static List<string> deckStrings = new List<string>();
public static void CtosMessage_JoinGame(string psw,string version)
public static void CtosMessage_JoinGame(string psw, string version)
{ {
deckStrings.Clear(); deckStrings.Clear();
Package message = new Package(); Package message = new Package();
...@@ -541,6 +565,7 @@ public class Package ...@@ -541,6 +565,7 @@ public class Package
{ {
public int Fuction = 0; public int Fuction = 0;
public BinaryMaster Data = null; public BinaryMaster Data = null;
public Package() public Package()
{ {
Fuction = (int)CtosMessage.Response; Fuction = (int)CtosMessage.Response;
...@@ -553,6 +578,7 @@ public class BinaryMaster ...@@ -553,6 +578,7 @@ public class BinaryMaster
MemoryStream memstream = null; MemoryStream memstream = null;
public BinaryReader reader = null; public BinaryReader reader = null;
public BinaryWriter writer = null; public BinaryWriter writer = null;
public BinaryMaster(byte[] raw = null) public BinaryMaster(byte[] raw = null)
{ {
if (raw == null) if (raw == null)
...@@ -566,21 +592,25 @@ public class BinaryMaster ...@@ -566,21 +592,25 @@ public class BinaryMaster
reader = new BinaryReader(memstream); reader = new BinaryReader(memstream);
writer = new BinaryWriter(memstream); writer = new BinaryWriter(memstream);
} }
public void set(byte[] raw) public void set(byte[] raw)
{ {
memstream = new MemoryStream(raw); memstream = new MemoryStream(raw);
reader = new BinaryReader(memstream); reader = new BinaryReader(memstream);
writer = new BinaryWriter(memstream); writer = new BinaryWriter(memstream);
} }
public byte[] get() public byte[] get()
{ {
byte[] bytes = memstream.ToArray(); byte[] bytes = memstream.ToArray();
return bytes; return bytes;
} }
public int getLength() public int getLength()
{ {
return (int)memstream.Length; return (int)memstream.Length;
} }
public override string ToString() public override string ToString()
{ {
string return_value = ""; string return_value = "";
...@@ -588,11 +618,11 @@ public class BinaryMaster ...@@ -588,11 +618,11 @@ public class BinaryMaster
for (int i = 0; i < bytes.Length; i++) for (int i = 0; i < bytes.Length; i++)
{ {
return_value += ((int)bytes[i]).ToString(); return_value += ((int)bytes[i]).ToString();
if (i < bytes.Length - 1) return_value += ","; if (i < bytes.Length - 1)
return_value += ",";
} }
return return_value; return return_value;
} }
} }
public static class BinaryExtensions public static class BinaryExtensions
...@@ -617,7 +647,6 @@ public static class BinaryExtensions ...@@ -617,7 +647,6 @@ public static class BinaryExtensions
{ {
UnityEngine.Debug.Log(e); UnityEngine.Debug.Log(e);
} }
} }
public static string ReadUnicode(this BinaryReader reader, int len) public static string ReadUnicode(this BinaryReader reader, int len)
...@@ -661,7 +690,7 @@ public static class BinaryExtensions ...@@ -661,7 +690,7 @@ public static class BinaryExtensions
return a; return a;
} }
public static void readCardData(this BinaryReader r, gameCard cardTemp=null) public static void readCardData(this BinaryReader r, gameCard cardTemp = null)
{ {
gameCard cardToRefresh = cardTemp; gameCard cardToRefresh = cardTemp;
int flag = r.ReadInt32(); int flag = r.ReadInt32();
...@@ -670,13 +699,13 @@ public static class BinaryExtensions ...@@ -670,13 +699,13 @@ public static class BinaryExtensions
if ((flag & (int)Query.Code) != 0) if ((flag & (int)Query.Code) != 0)
{ {
code= r.ReadInt32(); code = r.ReadInt32();
} }
if ((flag & (int)Query.Position) != 0) if ((flag & (int)Query.Position) != 0)
{ {
gps = r.ReadGPS(); gps = r.ReadGPS();
cardToRefresh = null; cardToRefresh = null;
cardToRefresh = Program.I().ocgcore.GCS_cardGet(gps,false); cardToRefresh = Program.I().ocgcore.GCS_cardGet(gps, false);
} }
if (cardToRefresh == null) if (cardToRefresh == null)
...@@ -699,7 +728,6 @@ public static class BinaryExtensions ...@@ -699,7 +728,6 @@ public static class BinaryExtensions
cardToRefresh.p = gps; cardToRefresh.p = gps;
} }
if (data.Id > 0) if (data.Id > 0)
{ {
if ((cardToRefresh.p.location & (UInt32)CardLocation.Hand) > 0) if ((cardToRefresh.p.location & (UInt32)CardLocation.Hand) > 0)
...@@ -795,7 +823,11 @@ public static class BinaryExtensions ...@@ -795,7 +823,11 @@ public static class BinaryExtensions
l3 = r.ReadInt32(); //link value l3 = r.ReadInt32(); //link value
data.LinkMarker = r.ReadInt32(); data.LinkMarker = r.ReadInt32();
} }
if (((flag & (int)Query.Level) != 0) || ((flag & (int)Query.Rank) != 0) || ((flag & (int)Query.Link) != 0)) if (
((flag & (int)Query.Level) != 0)
|| ((flag & (int)Query.Rank) != 0)
|| ((flag & (int)Query.Link) != 0)
)
{ {
if (l1 > l2) if (l1 > l2)
{ {
...@@ -805,7 +837,7 @@ public static class BinaryExtensions ...@@ -805,7 +837,7 @@ public static class BinaryExtensions
{ {
data.Level = l2; data.Level = l2;
} }
if(l3 > data.Level) if (l3 > data.Level)
data.Level = l3; data.Level = l3;
} }
...@@ -842,7 +874,6 @@ public class SocketMaster ...@@ -842,7 +874,6 @@ public class SocketMaster
var buf = ReadFull(stream, plen); var buf = ReadFull(stream, plen);
return buf; return buf;
} }
} }
public class TcpClientWithTimeout public class TcpClientWithTimeout
...@@ -860,6 +891,7 @@ public class TcpClientWithTimeout ...@@ -860,6 +891,7 @@ public class TcpClientWithTimeout
_port = port; _port = port;
_timeout_milliseconds = timeout_milliseconds; _timeout_milliseconds = timeout_milliseconds;
} }
public TcpClient Connect() public TcpClient Connect()
{ {
// kick off the thread that tries to connect // kick off the thread that tries to connect
...@@ -891,13 +923,17 @@ public class TcpClientWithTimeout ...@@ -891,13 +923,17 @@ public class TcpClientWithTimeout
{ {
// 同样地抛出错误 // 同样地抛出错误
thread.Abort(); thread.Abort();
string message = string.Format("TcpClient connection to {0}:{1} timed out", string message = string.Format(
_hostname, _port); "TcpClient connection to {0}:{1} timed out",
_hostname,
_port
);
TcpHelper.onDisConnected = true; TcpHelper.onDisConnected = true;
Program.DEBUGLOG("onDisConnected 8"); Program.DEBUGLOG("onDisConnected 8");
throw new TimeoutException(message); throw new TimeoutException(message);
} }
} }
protected void BeginConnect() protected void BeginConnect()
{ {
try try
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment