summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Tss/TssSDKManager.cs
blob: 74e80743b4821a43ce6dc88300340f204ffc50df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using XUtliPoolLib;

public class TssSDKManager : MonoBehaviour, ITssSdk
{

    public static TssSDKManager sington = null;

    public readonly uint gameId = 2601;
    
    public new readonly string tag = "TssSDKManager=>";

    /// <summary>
    /// 网络同步2秒同步一次
    /// </summary>
    public readonly uint sync = 2;

    private bool isLogin = false;

    void Awake()
    {
        sington = this;
    }

    /// <summary>
    /// 初始化
    /// </summary>
    void Start()
    {
#if TSS
        Debug.Log(tag + " Tss init");
        TssSdk.TssSdkInit(gameId);
#endif

        m_last = Time.time;

       
    }

    void OnDestroy()
    {
        sington = null;
    }


    float m_last = 0;
    void Update()
    {
        if (isLogin)
        {
            if (Time.time - m_last > sync)
            {
                m_last = Time.time;
                StartSendDataToSvr();
            }
        }
    }


    /// <summary>
    /// 当成功登录
    /// </summary>
    /// <param name="platf">1是internal 2是盛大 3是QQ 4是微信 max是其他</param>
    /// <param name="openId"></param>
    /// <param name="worldId">游戏分区</param>
    /// <param name="roleId">角色id</param>
    public void OnLogin(int platf, string openId, uint worldId, string roleId)
    {
#if TSS
        isLogin = true;
        Debug.Log(tag + " plat: " + platf + "openid:" + openId + " worldid: " + worldId + " roleid: " + roleId);
        if (platf == 3)
        {
            TssSdk.TssSdkSetUserInfoEx(TssSdk.EENTRYID.ENTRY_ID_QQ, openId, "1105309683", worldId, roleId);
        }
        else if (platf == 4)
        {
            TssSdk.TssSdkSetUserInfoEx(TssSdk.EENTRYID.ENTRY_ID_WX, openId, "wxfdab5af74990787a", worldId, roleId);
        }
        else
        {
            TssSdk.TssSdkSetUserInfoEx(TssSdk.EENTRYID.ENTRY_ID_OTHERS, openId, "guest100023", worldId, roleId);
        }
#endif
       
        //byte[] bytes = new byte[] { 0x12, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23 };
        //Hotfix.PrintBytes("start:" ,bytes);
        //ITssSdkSend itss = XInterfaceMgr.singleton.GetInterface<ITssSdkSend>(XCommon.singleton.XHash("ITssSdkSend"));
        //if (itss != null) itss.SendDataToServer(bytes, (uint)(bytes.Length));
    }

    /// <summary>
    /// 游戏从前台后台切换
    /// </summary>
    /// <param name="pause"></param>
    void OnApplicationPause(bool pause)
    {
#if TSS
        Debug.Log(tag + " puase: " + pause);
        if (pause)
        {
            TssSdk.TssSdkSetGameStatus(TssSdk.EGAMESTATUS.GAME_STATUS_BACKEND);
        }
        else
        {
            TssSdk.TssSdkSetGameStatus(TssSdk.EGAMESTATUS.GAME_STATUS_FRONTEND);
        }
#endif
    }


    /// <summary>
    /// 上报数据
    /// </summary>
    public void StartSendDataToSvr()
    {
#if TSS
        IntPtr addr = TssSdk.tss_get_report_data();
        if (addr != IntPtr.Zero)
        {
            TssSdk.AntiDataInfo info = new TssSdk.AntiDataInfo();
            if (TssSdk.Is64bit())
            {
                short anti_data_len = Marshal.ReadInt16(addr, 0);
                Int64 anti_data = Marshal.ReadInt64(addr, 2);
                info.anti_data_len = (ushort)anti_data_len;
                info.anti_data = new IntPtr(anti_data);
            }
            else if (TssSdk.Is32bit())
            {
                short anti_data_len = Marshal.ReadInt16(addr, 0);
                Int64 anti_data = Marshal.ReadInt32(addr, 2);
                info.anti_data_len = (ushort)anti_data_len;
                info.anti_data = new IntPtr(anti_data);
            }
            else
            {
                Debug.LogError(tag+" TSSSDK NO INT TYPE");
            }
            if (SendDataToSvr(info))
            {
                TssSdk.tss_del_report_data(addr);
            }
        }
        else
        {
           // Debug.Log(tag + "addr is nil!");
        }
#endif
    }


    private bool SendDataToSvr(TssSdk.AntiDataInfo info)
    {
        byte[] data = new byte[info.anti_data_len];
        Marshal.Copy(info.anti_data, data, 0, info.anti_data_len);
        return DoSendDataToSvr(data, info.anti_data_len);
    }


    private bool DoSendDataToSvr(byte[] data, uint length)
    {
#if TSS
        //send data to server
        Hotfix.PrintBytes("Send "+tag, data);
        ITssSdkSend itss = XInterfaceMgr.singleton.GetInterface<ITssSdkSend>(XCommon.singleton.XHash("ITssSdkSend"));
        if (itss != null) itss.SendDataToServer(data, length);
#endif
        return true;
    }


    /// <summary>
    /// 由服务器调用
    /// </summary>
    public void OnRcvWhichNeedToSendClientSdk(byte[] data, uint length)
    {
//        Hotfix.PrintBytes("rcv:", data);
#if TSS
        Hotfix.PrintBytes("RCV "+tag, data);
        TssSdk.TssSdkRcvAntiData(data, (ushort)length);
#endif
    }


}