summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/Tss/TssSdk.cs
blob: 3bd5489e97249c369c108b39911c0ccbc4fc771e (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
using UnityEngine;
using System.Collections;
using System;
using System.Reflection;
using System.Runtime.InteropServices;

public sealed class BugtraceAgent2  {
	#if UNITY_4_6
	private static Application.LogCallback s_oldLogCallback;
	public static Application.LogCallback GetCurrentLogCallback(){
        Type t = typeof(Application);
        // Instance Field
        BindingFlags flag = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
        // Static Field
        flag = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;
        FieldInfo fieldInfo = t.GetField ("s_LogCallback", flag);
        if (fieldInfo != null && fieldInfo.IsPrivate && fieldInfo.IsStatic) {
            object callback = fieldInfo.GetValue (null);
            if (callback != null){
                return (Application.LogCallback)callback;
            }
        }
        return null;
	}
	#endif

	private static bool _isInitialized = false;
	public static void EnableExceptionHandler(){
		if (_isInitialized){
			return;
		}
		RegisterExceptionHandler();
		_isInitialized = true;
	}

	public static void DisableExceptionHandler(){
		if (!_isInitialized){
			return;
		}
		UnregisterExceptionHandler();
		_isInitialized = false;
	}

	private static void RegisterExceptionHandler(){
		#if UNITY_4_6 
		AppDomain.CurrentDomain.UnhandledException += UncaughtExceptionHandler;
		s_oldLogCallback = GetCurrentLogCallback ();
		Application.RegisterLogCallback (LogCallbackHandler);
		#endif

		#if (UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3)
		Application.logMessageReceived += LogCallbackHandler;
		#endif

	}

	private static void UnregisterExceptionHandler(){
		#if UNITY_4_6
		AppDomain.CurrentDomain.UnhandledException -= UncaughtExceptionHandler;
		Application.RegisterLogCallback (s_oldLogCallback);
		#endif

		#if (UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3)
		Application.logMessageReceived -= LogCallbackHandler;
		#endif
	}

	private static void LogCallbackHandler(string condition, string stack, LogType type){
		if (type == LogType.Exception) {
			//FileLog.Instance.Log ("<LogCallbackHandler> reason:" + condition);
			//FileLog.Instance.Log ("<LogCallbackHandler> stack:" + stack);
			HandleException (condition, stack);
		}
		#if UNITY_4_6
		if (s_oldLogCallback != null) {
            s_oldLogCallback(condition, stack, type);
		}
		#endif
	}

	private static void UncaughtExceptionHandler(object sender, System.UnhandledExceptionEventArgs args){
		Exception e = (Exception)args.ExceptionObject;
		if (e != null){
			HandleException (e.Message, e.StackTrace);
		}

	}

	#if (UNITY_EDITOR || UNITY_STANDALONE)
	private static void HandleException(string reason, string stack){

	}

	#elif UNITY_ANDROID
    private static void HandleException(string reason, string stack){
        string cmd = "crash-reportcsharpexception|";
        cmd += "cause:" + reason;
        cmd += "stack:" + stack;
        //FileLog.Instance.Log("Android HandleException");
        try{
            AndroidJavaClass agent  = new AndroidJavaClass("com.tencent.tp.TssJavaMethod");
            if (agent != null){
                agent.CallStatic("sendCmd", cmd);
            }
        }catch{
            //FileLog.Instance.Log("Android HandleException catch");
        }
	}

	#elif (UNITY_IOS || UNITY_IPHONE)
	[DllImport("__Internal")]
	private static extern void ReportCSharpException (string reason, string stack);
	private static void HandleException(string reason, string stack){
        ReportCSharpException(reason, stack);
	}

	#endif
} // end of class BugtraceAgent2

public static class TssSdk{

	public enum ESERAILIZETAG{
		TAG_INT = 0x00,
		TAG_TYPE = 0x01,
		TAG_GAME_ID = 0x02,
		TAG_GAME_STATUS = 0x03,
		TAG_ENTRY_ID = 0x04,
		TAG_WORLD_ID = 0x05,
		TAG_STR = 0x40,
		TAG_APPID = 0x41,
		TAG_OPENID = 0x42,
		TAG_ROLEID = 0x43
	}

	public enum ESERIALIZETYPE{
		TYPE_INIT = 0x01,
		TYPE_SETUSERINFO = 0x02,
		TYPE_SETGAMESTATUS = 0x03
	}
	public enum EUINTYPE
	{
		UIN_TYPE_INT = 1, // integer format
		UIN_TYPE_STR = 2  // string format
	}
	
	public enum EAPPIDTYPE
	{
		APP_ID_TYPE_INT = 1, // integer format
		APP_ID_TYPE_STR = 2  // string format
	}
	
	public enum EENTRYID
	{
        ENTRY_ID_QQ         = 1,       // QQ
		ENTRY_ID_QZONE      = 1,	   // QQ
		ENTRY_ID_MM			= 2,	   // WeChat
        ENTRY_ID_WX         = 2,       // 微信
        ENTRT_ID_FACEBOOK   = 3,       // facebook
        ENTRY_ID_TWITTER    = 4,       // twitter
        ENTRY_ID_LINE       = 5,       // line
        ENTRY_ID_WHATSAPP   = 6,       // whatsapp
        ENTRY_ID_OTHERS     = 99,      // 其他平台
	}
	
	public enum EGAMESTATUS
	{
		GAME_STATUS_FRONTEND = 1,  // running in front-end
		GAME_STATUS_BACKEND = 2    // running in back-end
	}
	
	public enum AntiEncryptResult
	{
		ANTI_ENCRYPT_OK = 0,
		ANTI_NOT_NEED_ENCRYPT = 1,
	}
	
	public enum AntiDecryptResult
	{
		ANTI_DECRYPT_OK = 0,
		ANTI_DECRYPT_FAIL = 1,
	}
	
	// sdk anti-data info
	[StructLayout(LayoutKind.Sequential)]
	public class AntiDataInfo
	{
		//[FieldOffset(0)]
		public ushort anti_data_len;
		//[FieldOffset(2)]
		public IntPtr anti_data;
	};
	
	[StructLayout(LayoutKind.Explicit, Size = 20)]
	public class EncryptPkgInfo
	{
		[FieldOffset(0)]
		public int cmd_id_;				/* [in] game pkg cmd */
		[FieldOffset(4)]
		public IntPtr game_pkg_;		/* [in] game pkg */
		[FieldOffset(8)]
		public uint game_pkg_len_;		/* [in] the length of game data packets, maximum length less than 65,000 */
		[FieldOffset(12)]
		public IntPtr encrpty_data_;	/* [in/out] assembling encrypted game data package into anti data, memory allocated by the caller, 64k at the maximum */
		[FieldOffset(16)]
		public uint encrypt_data_len_;	/* [in/out] length of anti_data when input, actual length of anti_data when output */
	}
	
	[StructLayout(LayoutKind.Explicit, Size = 16)]
	public class DecryptPkgInfo
	{
		[FieldOffset(0)]
		public IntPtr encrypt_data_;		/* [in] anti data received by game client */
		[FieldOffset(4)]
		public uint encrypt_data_len;       /* [in] length of anti data received by game client */
		[FieldOffset(8)]
		public IntPtr game_pkg_;            /* [out] buffer used to store the decrypted game package, space allocated by the caller */
		[FieldOffset(12)]
		public uint game_pkg_len_;          /* [out] input is size of game_pkg_, output is the actual length of decrypted game package */
	}
	
	public static Boolean Is64bit()
	{
		return IntPtr.Size == 8;
	}
	
	public static Boolean Is32bit()
	{
		return IntPtr.Size == 4;
	}
    class OutputUnityBuffer{
            private byte[] data;
            private uint offset;
            private uint count;
            public OutputUnityBuffer(uint length){
                    this.data = new byte[length];
                    this.offset = 0;
                    this.count = length;
            }

            public void write(byte b){
                    if (offset < count) {
                            this.data [offset] = b;
                            this.offset++;
                    }
            }

            public byte[] toByteArray(){
                    return data;
            }

            public uint getLength(){
                    return this.offset;
            }
    }
    class SerializeUnity{
            public static void putLength(OutputUnityBuffer data, uint length){
                    data.write ((byte)(length >> 24));
                    data.write ((byte)(length >> 16));
                    data.write ((byte)(length >> 8));
                    data.write ((byte)(length));
            }

            public static void putInteger(OutputUnityBuffer data, uint value){
                    data.write ((byte)(value >> 24));
                    data.write ((byte)(value >> 16));
                    data.write ((byte)(value >> 8));
                    data.write ((byte)(value));
            }

            public static void putByteArray(OutputUnityBuffer data, byte[] value){
                    int len = value.Length;
                    for (int i = 0; i < len; i++) {
                            data.write (value [i]);
                    }
                    data.write (0);
            }

            public static void setInitInfo(uint gameId){
                    OutputUnityBuffer data = new OutputUnityBuffer (1 + 4 + 1 + 1 + 4 + 4);
                    data.write((byte)ESERAILIZETAG.TAG_TYPE);
                    putLength (data, 1);
                    data.write ((byte)ESERIALIZETYPE.TYPE_INIT);

                    data.write ((byte)ESERAILIZETAG.TAG_GAME_ID);
                    putLength (data, 4);
                    putInteger (data, gameId);

                    tss_unity_str (data.toByteArray (), data.getLength());

            }

            public static void setGameStatus(EGAMESTATUS gameStatus){
                    OutputUnityBuffer data = new OutputUnityBuffer (1 + 4 + 1 + 1 + 4 + 4);
                    data.write((byte)ESERAILIZETAG.TAG_TYPE);
                    putLength (data, 1);
                    data.write ((byte)ESERIALIZETYPE.TYPE_SETGAMESTATUS);

                    data.write ((byte)ESERAILIZETAG.TAG_GAME_STATUS);
                    putLength (data, 4);
                    putInteger (data, (uint)gameStatus);

                    tss_unity_str (data.toByteArray (), data.getLength());
            }

            public static void setUserInfoEx(EENTRYID entryId,
                    string uin,
                    string appId,
                    uint worldId,
                    string roleId){

                    byte[] valOpenId = System.Text.Encoding.ASCII.GetBytes (uin);
                    byte[] valAppId = System.Text.Encoding.ASCII.GetBytes (appId);
                    byte[] valRoleId = System.Text.Encoding.ASCII.GetBytes (roleId);
                    uint length = 0;
                    OutputUnityBuffer data = new OutputUnityBuffer (6*1 + 6*4 + 1 + 4 + 4 + (uint)valOpenId.Length + 1 + (uint)valAppId.Length + 1 + (uint)valRoleId.Length + 1);
                    data.write((byte)ESERAILIZETAG.TAG_TYPE);
                    putLength (data, 1);
                    data.write ((byte)ESERIALIZETYPE.TYPE_SETUSERINFO);

                    data.write ((byte)ESERAILIZETAG.TAG_ENTRY_ID);
                    putLength (data, 4);
                    putInteger (data, (uint)entryId);

                    data.write ((byte)ESERAILIZETAG.TAG_OPENID);
                    length = (uint)valOpenId.Length + 1;
                    //Debug.Log ("openid length:" + length);
                    putLength (data, length);
                    putByteArray (data, valOpenId);

                    data.write ((byte)ESERAILIZETAG.TAG_APPID);
                    length = (uint)valAppId.Length + 1;
                    //Debug.Log ("appid length:" + length);
                    putLength (data, length);
                    putByteArray (data, valAppId);

                    data.write ((byte)ESERAILIZETAG.TAG_WORLD_ID);
                    putLength (data, 4);
                    putInteger (data, worldId);

                    data.write ((byte)ESERAILIZETAG.TAG_ROLEID);
                    length = (uint)valRoleId.Length + 1;
                    //Debug.Log ("roleid length:" + length);
                    putLength (data, length);
                    putByteArray (data, valRoleId);

                    //Debug.Log ("data length:" + data.getLength());
                    tss_unity_str (data.toByteArray (), data.getLength());
            }
    }

    private static bool isEnable(string name)
    {
#if (UNITY_IOS || UNITY_IPHONE)
        byte[] keyName = System.Text.Encoding.ASCII.GetBytes (name);
        int ret = tss_unity_is_enable(keyName, keyName.Length);
        return (ret != 0);
#endif

#if UNITY_ANDROID
        try
        {
            AndroidJavaClass agent = new AndroidJavaClass("com.tencent.tp.TssJavaMethod");
            if (agent != null)
            {
                string cmd = "is_enabled2:" + name;
                // FileLog.Instance.Log("[tsssdk]cmd:" + cmd);
                int ret = agent.CallStatic<int>("sendCmdEx", cmd);
                return (ret != 0);
            }
        }
        catch(Exception)
        {
            // Debug.LogException(e);
            // FileLog.Instance.Log("isEnable exception catch");
        }
#endif
        return false;
    }
	
	/// <summary>
	/// Tsses the sdk init.
	/// </summary>
	/// <param name='gameId'>
	/// game id provided by sdk provider
	/// </param>
	public static void TssSdkInit(uint gameId)
	{
		SerializeUnity.setInitInfo (gameId);
		tss_enable_get_report_data();
		tss_log_str(TssSdkVersion.GetSdkVersion());
		tss_log_str(TssSdtVersion.GetSdtVersion());
        if (isEnable("bugtrace"))
        {
            // Debug.Log("Enable Exception Handler");
            BugtraceAgent2.EnableExceptionHandler();
        }
	}
	/// <summary>
	/// Tsses the sdk set game status.
	/// </summary>
	/// <param name='gameStatus'>
	/// back-end or front-end
	/// </param>
	public static void TssSdkSetGameStatus(EGAMESTATUS gameStatus)
	{
		SerializeUnity.setGameStatus(gameStatus);
	}
	
	public static void TssSdkSetUserInfo(EENTRYID entryId,
	                                     string uin,
	                                     string appId)
	{
		TssSdkSetUserInfoEx(entryId, uin, appId, 0, "0");
	}
	
	public static void TssSdkSetUserInfoEx(EENTRYID entryId,
	                                       string uin,
	                                       string appId,
	                                       uint worldId,
	                                       string roleId
	                                       )
	{
				
		if (roleId == null) {
			roleId = "0";
		}
		SerializeUnity.setUserInfoEx (entryId, uin, appId, worldId, roleId);

	}
	

	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	private static extern void tss_log_str(string sdk_version);
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	private static extern void tss_sdk_rcv_anti_data(IntPtr info);
	public static void TssSdkRcvAntiData(byte[] data, ushort length)
	{
		IntPtr pv = Marshal.AllocHGlobal (2 + IntPtr.Size);
		if (pv != IntPtr.Zero) 
		{
			Marshal.WriteInt16 (pv,0,(short)length);
			//Marshal.WriteIntPtr (pv,2,(IntPtr)data);
			IntPtr p = Marshal.AllocHGlobal(data.Length);
			if (p != IntPtr.Zero)
			{
				Marshal.Copy (data,0,p, data.Length);
				Marshal.WriteIntPtr (pv,2,p);
				tss_sdk_rcv_anti_data (pv);
				Marshal.FreeHGlobal(p);
			}
			
			Marshal.FreeHGlobal(pv);
		}
	}
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	private static extern AntiEncryptResult tss_sdk_encryptpacket(EncryptPkgInfo info);
	public static AntiEncryptResult TssSdkEncrypt(/*[in]*/int cmd_id, /*[in]*/byte[] src, /*[in]*/uint src_len,
	                                              /*[out]*/ref byte[] tar, /*[out]*/ref uint tar_len) 
	{
		AntiEncryptResult ret = AntiEncryptResult.ANTI_NOT_NEED_ENCRYPT;
		GCHandle src_handle = GCHandle.Alloc(src, GCHandleType.Pinned);
		GCHandle tar_handle = GCHandle.Alloc(tar, GCHandleType.Pinned);
		if (src_handle.IsAllocated && tar_handle.IsAllocated) 
		{
			EncryptPkgInfo info = new EncryptPkgInfo();
			info.cmd_id_ = cmd_id;
			info.game_pkg_ = src_handle.AddrOfPinnedObject();
			info.game_pkg_len_ = src_len;
			info.encrpty_data_ = tar_handle.AddrOfPinnedObject();
			info.encrypt_data_len_ = tar_len;
			ret = tss_sdk_encryptpacket(info);
			tar_len = info.encrypt_data_len_;
		}
		if (src_handle.IsAllocated) src_handle.Free();
		if (tar_handle.IsAllocated) tar_handle.Free();
		return ret;
	}
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	private static extern AntiDecryptResult tss_sdk_decryptpacket(DecryptPkgInfo info);
	public static AntiDecryptResult TssSdkDecrypt(/*[in]*/byte[] src, /*[in]*/uint src_len,
	                                              /*[out]*/ref byte[] tar, /*[out]*/ref uint tar_len) 
	{
		AntiDecryptResult ret = AntiDecryptResult.ANTI_DECRYPT_FAIL;
		GCHandle src_handle = GCHandle.Alloc(src, GCHandleType.Pinned);
		GCHandle tar_handle = GCHandle.Alloc(tar, GCHandleType.Pinned);
		if (src_handle.IsAllocated && tar_handle.IsAllocated) 
		{
			DecryptPkgInfo info = new DecryptPkgInfo();
			info.encrypt_data_ = src_handle.AddrOfPinnedObject();
			info.encrypt_data_len = src_len;
			info.game_pkg_ = tar_handle.AddrOfPinnedObject();
			info.game_pkg_len_ = tar_len;
			ret = tss_sdk_decryptpacket(info);
			tar_len = info.game_pkg_len_;
		}
		if (src_handle.IsAllocated) src_handle.Free();
		if (tar_handle.IsAllocated) tar_handle.Free();
		return ret;
	}
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	private static extern void tss_enable_get_report_data();
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	public static extern IntPtr tss_get_report_data();
	
	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	public static extern void tss_del_report_data(IntPtr info);

	#if UNITY_IOS
	[DllImport("__Internal")]
	#else
	[DllImport("tersafe")]
	#endif
	public static extern void tss_unity_str(byte[] data, UInt32 len);

#if (UNITY_IOS || UNITY_IPHONE)
    [DllImport("__Internal")]
    public static extern int tss_unity_is_enable(byte[] data, int len);
#endif


}

class TssSdkVersion 
{
	private const string  cs_sdk_version = "C# SDK ver: 2.6.1(2016/09/30)";
	public static string GetSdkVersion()
	{
		return cs_sdk_version;	
	}
}