-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMain.cpp
More file actions
862 lines (767 loc) · 25.5 KB
/
Copy pathMain.cpp
File metadata and controls
862 lines (767 loc) · 25.5 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#include <windows.h>
#include "tp_stub.h"
#include "ncbind.hpp"
#include <string>
#include <cstdio>
#include "SteamAchievements.h"
#include <steam_api.h>
// x64 縺ョ繧、繝ウ繝昴�繝医Λ繧、繝悶Λ繝ェ縺ッ steam_api64.lib縲�Make 蛛エ縺ァ繧よ欠螳壹@縺ヲ縺�k縺後
// 蜊倅ス薙�繝ュ繧ク繧ァ繧ッ繝医〒繝薙Ν繝峨☆繧句エ蜷医�縺溘a縺ォ縺薙%縺ァ繧ょ粋繧上○縺ヲ縺翫¥縲
#ifdef _MSC_VER
# ifndef STEAMAPI_LIB
# if defined(_M_AMD64) || defined(_M_X64)
# define STEAMAPI_LIB "steam_api64.lib"
# else
# define STEAMAPI_LIB "steam_api.lib"
# endif
# endif
# pragma comment(lib,STEAMAPI_LIB)
#endif
//繝ゥ繧、繝悶Λ繝ェ莠呈鋤縺後Ο繧ケ繝医@縺ヲ繧九h縺�↑縺ョ縺ァ莉ョ蟇セ遲悶�讖溯�逧�↓辟。蜉ケ縺九←縺�°縺ッ荳肴�
// k_iClientVideoCallbacks 縺ッ譁ー縺励> SDK 縺ァ縺ッ蜈ャ髢九�繝�ム縺九i豸医∴縺ヲ縺�k縺溘a縲
// 蛟、 (4600) 繧堤峩謗・鄂ョ縺上ゆク。譁ケ縺ョ蝙九′ SDK 蛛エ縺ァ螳夂セゥ貂医∩縺ェ繧峨%縺ョ荳蠑上�荳崎ヲ√
// SDK 1.60 以降は isteamvideo.h が BroadcastUpload*_t を定義済み。
// #ifndef は型名には効かない (マクロではない) ため、isteamvideo.h の
// インタフェース版数マクロの有無で「SDK 側にあるか」を判定する。
#ifndef STEAMVIDEO_INTERFACE_VERSION
#if !defined(BroadcastUploadStart_t) || !defined(BroadcastUploadStop_t)
namespace { enum { __k_iSteamVideoCallbacks = 4600 }; }
#endif
#ifndef BroadcastUploadStart_t
STEAM_CALLBACK_BEGIN(BroadcastUploadStart_t, __k_iSteamVideoCallbacks + 4)
STEAM_CALLBACK_END(0)
#endif
#ifndef BroadcastUploadStop_t
STEAM_CALLBACK_BEGIN(BroadcastUploadStop_t, __k_iSteamVideoCallbacks + 5)
STEAM_CALLBACK_MEMBER(0, EBroadcastUploadResult, m_eResult)
STEAM_CALLBACK_END(1)
#endif
#endif // STEAMVIDEO_INTERFACE_VERSION
// ttstr繧旦TF8譁�ュ怜�縺ク螟画鋤
std::string
convertTtstrToUtf8String(ttstr &buf)
{
int len = TVPWideCharToUtf8String(buf.c_str(), NULL);
char *dat = new char[len];
len = TVPWideCharToUtf8String(buf.c_str(), dat);
std::string result(dat, len);
delete[] dat;
return result;
}
// std::string繧稚tstr縺ォ螟画鋤
ttstr
convertUtf8StringToTtstr(const char *buf, size_t length=-1)
{
if (length < 0) {
length = strlen(buf);
}
tjs_uint len = TVPUtf8ToWideCharString(buf, NULL);
tjs_char *dat = new tjs_char[len];
len = TVPUtf8ToWideCharString(buf, dat);
ttstr result(dat, len);
delete[] dat;
return result;
}
ttstr
convertUtf8StringToTtstr(const std::string &buf)
{
return convertUtf8StringToTtstr(buf.c_str(), buf.length());
}
extern void initStorage();
extern void doneStorage();
class SteamScreenshotCallback;
class SteamBroadcastCallback;
class LayerImageToRGB;
/**
* Steam諠�ア蝓コ遉弱け繝ゥ繧ケ
*/
class Steam : public tTVPContinuousEventCallbackIntf {
public:
// 蛻晄悄蛹 (驕�サカ蛻晄悄蛹悶°繧牙他縺ー繧後k縺ョ縺ァ莠碁㍾蜻シ縺ウ蜃コ縺励↓閠舌∴繧九%縺ィ)
static void registerSteam() {
if (!instance) {
instance = new Steam();
instance->init();
initStorage();
}
}
// 隗」髯、
static void unregisterSteam() {
if (instance) {
doneStorage();
delete instance;
instance = 0;
}
}
// -------------------------------------------------------------------
// 驕�サカ蛻晄悄蛹
//
// SteamAPI_Init() 縺ッ Steam 繧ッ繝ゥ繧、繧「繝ウ繝医′蜍輔>縺ヲ縺�↑縺�→螟ア謨励@縲(nit() 縺
// 萓句、悶r謚輔£繧九ゅ�繝ゥ繧ー繧、繝ウ隱ュ縺ソ霎シ縺ソ譎ゅ↓蛻晄悄蛹悶@縺ヲ縺励∪縺�→縲ヾteam 辟。縺励〒縺ッ
// 襍キ蜍輔☆繧峨〒縺阪↑縺�ゅ◎縺薙〒逋サ骭イ譎ゅ�譛ャ迚ゥ縺ョ Steam 繧ッ繝ゥ繧ケ繧貞キョ縺玲崛縺医◆
// 繝励Ο繧ュ繧キ縺縺代r鄂ョ縺阪∵怙蛻昴↓ Steam.* 縺悟盾辣ァ縺輔l縺滓凾轤ケ縺ァ蛻晄悄蛹悶☆繧九
//
// RestartAppIfNecessary 縺縺代�縲郡team 邨檎罰縺ァ襍キ蜍輔@逶エ縺吶∋縺阪°縲阪�蛻、螳壹↑縺ョ縺ァ縲
// 蛻晄悄蛹悶r襍キ縺薙&縺壹↓邏騾壹@縺吶k (縺薙l繧貞他縺カ譎らせ縺ァ縺ッ縺セ縺蛻晄悄蛹悶〒縺阪↑縺)縲
// -------------------------------------------------------------------
class SteamDelayRegister : public tTJSDispatch {
tTJSVariantClosure closure;
bool terminate;
bool PassThroughCheck(const tjs_char * membername, tjs_uint32 *hint) {
// 蛻晄悄蛹悶r襍キ縺薙&縺ェ縺�Γ繝ウ繝 (繝。繧ス繝�ラ蜷阪r螟峨∴縺溘i縺薙%繧ょ粋繧上○繧)
if (membername && ttstr(membername) == TJS_W("RestartAppIfNecessary")) return true;
if (membername && !terminate) {
tTJSVariant v;
if (TJS_SUCCEEDED(closure.PropGet(0, membername, hint, &v, 0))) {
Steam::registerSteam(); // 驕�サカ蛻晄悄蛹門ョ溯。
SetClosure(TJS_W("Steam"), closure);
this->Release();
terminate = true;
}
}
return false;
}
static bool GetClosure(const tjs_char * membername, tTJSVariantClosure &closure) {
bool r = false;
iTJSDispatch2 * global = TVPGetScriptDispatch();
if (global) {
tTJSVariant v;
if (TJS_SUCCEEDED(global->PropGet(0, membername, 0, &v, global))) {
closure = v.AsObjectClosure();
r = true;
}
global->Release();
}
return r;
}
static bool SetClosure(const tjs_char * membername, const tTJSVariantClosure &closure) {
bool r = false;
iTJSDispatch2 * global = TVPGetScriptDispatch();
if (global) {
tTJSVariant v(closure.Object, closure.ObjThis);
r = TJS_SUCCEEDED(global->PropSet(TJS_MEMBERENSURE|TJS_IGNOREPROP, membername, 0, &v, global));
global->Release();
}
return r;
}
public:
SteamDelayRegister() : closure(NULL,NULL), terminate(false) {
GetClosure(TJS_W("Steam"), closure);
}
~SteamDelayRegister() {
closure.Release();
}
void SetSelf() {
SetClosure(TJS_W("Steam"), tTJSVariantClosure(this, this));
}
// FuncCall 縺ィ PropGet 縺縺大キョ縺玲崛縺医l縺ー蜈・蜿」繧呈款縺輔∴繧峨l繧
tjs_error TJS_INTF_METHOD FuncCall(tjs_uint32 flag, const tjs_char * membername, tjs_uint32 *hint, tTJSVariant *result, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *objthis) {
PassThroughCheck(membername, hint);
return closure.FuncCall(flag, membername, hint, result, numparams, param, objthis);
}
tjs_error TJS_INTF_METHOD PropGet(tjs_uint32 flag, const tjs_char * membername, tjs_uint32 *hint, tTJSVariant *result, iTJSDispatch2 *objthis) {
PassThroughCheck(membername, hint);
return closure.PropGet(flag, membername, hint, result, objthis);
}
};
static void setupSteam() {
SteamDelayRegister *delay = new SteamDelayRegister(); // Steam.* 蜿ら�縺ァ閾ェ蜍慕エ譽�&繧後k
delay->SetSelf();
}
// Steam 繧ッ繝ゥ繧、繧「繝ウ繝育オ檎罰縺ァ襍キ蜍輔@逶エ縺吝ソ�ヲ√′縺ゅk縺 (蛻晄悄蛹門燕縺ォ蜻シ縺ケ繧)
static bool RestartAppIfNecessary(tTVInteger appid) {
return SteamAPI_RestartAppIfNecessary(static_cast<uint32>(appid));
}
// 繧ウ繝ウ繧ケ繝医Λ繧ッ繧ソ逕ィ(蟶ク縺ォ萓句、)
static tjs_error Factory(Steam **obj, tjs_int numparams, tTJSVariant **param, iTJSDispatch2 *objthis) {
TVPThrowExceptionMessage(TJSGetMessageMapMessage(TJS_W("TVPCannotCreateInstance")).c_str());
return TJS_E_FAIL;
}
// 繧ウ繝ウ繧ケ繝医Λ繧ッ繧ソ
Steam() : inited(false), achieve(0), screenshot(0), broadcast(0) {}
// 繝�せ繝医Λ繧ッ繧ソ
virtual ~Steam();
// 蛻晄悄蛹
void init() {
if ((inited = SteamAPI_Init())) {
ISteamUtils *utils = SteamUtils();
if (utils) {
utils->SetOverlayNotificationPosition(k_EPositionTopLeft);
}
achieve = new SteamAchievements();
TVPAddContinuousEventHook(this);
} else {
TVPThrowExceptionMessage(TJS_W("Steam must be running to play this game (SteamAPI_Init() failed)"));
}
}
// ---------------------------------------------------------
// 螳溽クセ
// ---------------------------------------------------------
static bool requestInitialize() {
if (instance && instance->achieve) {
return instance->achieve->requestInitialize();
}
return false;
}
static bool getInitialized() {
if (instance && instance->achieve) {
return instance->achieve->getInitialized();
}
return false;
}
static int getAchievementsCount() {
if (instance && instance->achieve) {
return instance->achieve->getAchievementsCount();
}
return 0;
}
static tTJSVariant getAchievement(tTJSVariant n) {
if (instance && instance->achieve) {
return instance->achieve->getAchievement(n);
}
return tTJSVariant();
}
static bool setAchievement(tTJSVariant n) {
if (instance && instance->achieve) {
return instance->achieve->setAchievement(n);
}
return false;
}
static bool clearAchievement(tTJSVariant n) {
if (instance && instance->achieve) {
return instance->achieve->clearAchievement(n);
}
return false;
}
// ---------------------------------------------------------
// 諠�ア
// ---------------------------------------------------------
// 迴セ蝨ィ縺ョ險隱槭r蜿門セ
static ttstr getLanguage() {
ttstr ret;
ISteamApps *app = SteamApps();
if (app) {
std::string lang = app->GetCurrentGameLanguage();
ret = convertUtf8StringToTtstr(lang);
}
return ret;
}
// ---------------------------------------------------------
// 繝ヲ繝シ繧カ諠�ア
//
// 繧サ繝シ繝悶ョ繝シ繧ソ縺ョ鄂ョ縺榊エ謇繧偵Θ繝シ繧カ縺斐→縺ォ蛻�¢繧狗畑騾斐r諠ウ螳壹@縺ヲ縺�k縲
// Steam 閾ェ霄ォ縺ョ userdata 繝輔か繝ォ繝縺ッ 32bit 縺ョ繧「繧ォ繧ヲ繝ウ繝 ID 縺ァ蛻�°繧後k縺ョ縺ァ縲
// 縺昴l縺ォ蜷医o縺帙k縺ェ繧 accountID 繧剃スソ縺�
// ---------------------------------------------------------
// SteamID (64bit) 繧呈枚蟄怜�縺ァ霑斐☆縲 譛ェ繝ュ繧ー繧、繝ウ遲峨〒蜿悶l縺ェ縺�→縺阪�遨コ譁�ュ怜�縲
// TJS 縺ョ謨エ謨ー縺ッ 64bit 隨ヲ蜿キ莉倥″縺ェ縺ョ縺ァ迴セ螳溘� SteamID 縺ッ謨ー蛟、縺ァ繧り。ィ縺帙k縺後
// 繝輔か繝ォ繝蜷阪↑縺ゥ縺ォ菴ソ縺�畑騾斐r閠�∴縺ヲ譁�ュ怜�縺ァ霑斐☆縲
static ttstr getSteamID() {
ISteamUser *user = SteamUser();
if (!user) return ttstr();
uint64 id = user->GetSteamID().ConvertToUint64();
if (id == 0) return ttstr();
char buf[32];
snprintf(buf, sizeof buf, "%llu", (unsigned long long)id);
return convertUtf8StringToTtstr(buf);
}
// 繧「繧ォ繧ヲ繝ウ繝 ID (32bit)縲 Steam 縺ョ userdata 繝輔か繝ォ繝蜷阪→蜷後§蛟、縲
// 蜿悶l縺ェ縺�→縺阪� 0縲
static tTVInteger getAccountID() {
ISteamUser *user = SteamUser();
if (!user) return 0;
return static_cast<tTVInteger>(user->GetSteamID().GetAccountID());
}
// Steam 縺ォ繝ュ繧ー繧、繝ウ貂医∩縺
static bool getLoggedOn() {
ISteamUser *user = SteamUser();
return user ? user->BLoggedOn() : false;
}
// 陦ィ遉コ蜷 (繝壹Ν繧ス繝雁錐)縲 蜿悶l縺ェ縺�→縺阪�遨コ譁�ュ怜�縲
static ttstr getPersonaName() {
ISteamFriends *friends = SteamFriends();
if (!friends) return ttstr();
const char *name = friends->GetPersonaName();
return name ? convertUtf8StringToTtstr(name) : ttstr();
}
// 蜍穂ス應クュ縺ョ AppID縲 蜿悶l縺ェ縺�→縺阪� 0縲
static tTVInteger getAppID() {
ISteamUtils *utils = SteamUtils();
return utils ? static_cast<tTVInteger>(utils->GetAppID()) : 0;
}
// ---------------------------------------------------------
// 繧ッ繝ゥ繧ヲ繝
// ---------------------------------------------------------
static bool getCloudEnabled() {
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
return storage->IsCloudEnabledForApp();
}
return false;
}
static void setCloudEnabled(bool enabled) {
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
storage->SetCloudEnabledForApp(enabled);
}
}
static tTJSVariant getCloudQuota() {
tTJSVariant ret;
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
uint64 total;
uint64 available;
if (storage->GetQuota(&total, &available)) {
iTJSDispatch2 *dict = TJSCreateDictionaryObject();
if (dict) {
ncbPropAccessor obj(dict);
obj.SetValue(TJS_W("total"), static_cast<tTVInteger>(total));
obj.SetValue(TJS_W("available"), static_cast<tTVInteger>(available));
ret = tTJSVariant(dict, dict);
dict->Release();
}
}
}
return ret;
}
static int getCloudFileCount() {
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
return storage->GetFileCount();
}
return 0;
}
static tTJSVariant getCloudFileInfo(int n) {
tTJSVariant ret;
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
int32 size;
std::string name = storage->GetFileNameAndSize(n, &size);
int64 time = storage->GetFileTimestamp(name.c_str());
iTJSDispatch2 *dict = TJSCreateDictionaryObject();
if (dict) {
ncbPropAccessor obj(dict);
obj.SetValue(TJS_W("filename"), convertUtf8StringToTtstr(name));
obj.SetValue(TJS_W("size"), size);
obj.SetValue(TJS_W("time"), time);
ret = tTJSVariant(dict, dict);
dict->Release();
}
}
return ret;
}
static bool deleteCloudFile(ttstr name) {
bool ret = false;
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
ttstr n = name;
n.ToLowerCase();
std::string filename = convertTtstrToUtf8String(n);
ret = storage->FileDelete(filename.c_str());
}
return ret;
}
static bool copyCloudFile(ttstr src, ttstr dest) {
bool ret = false;
ISteamRemoteStorage *storage = SteamRemoteStorage();
if (storage) {
ttstr n = src;
n.ToLowerCase();
std::string srcfile = convertTtstrToUtf8String(n);
n = dest;
n.ToLowerCase();
std::string destfile = convertTtstrToUtf8String(n);
if (storage->FileExists(srcfile.c_str())) {
int size = storage->GetFileSize(srcfile.c_str());
unsigned char *buffer = new unsigned char[size];
if (buffer) {
if (storage->FileRead(srcfile.c_str(), buffer, size) == size) {
if (storage->FileWrite(destfile.c_str(), buffer, size)) {
ret = true;
}
}
delete[] buffer;
}
}
}
return ret;
}
// ---------------------------------------------------------
// 繧ケ繧ッ繝ェ繝シ繝ウ繧キ繝ァ繝�ヨ蛻カ蠕。
// ---------------------------------------------------------
/**
* 繧ケ繧ッ繝ェ繝シ繝ウ繧キ繝ァ繝�ヨ蜃ヲ逅�r繧「繝励Μ蛛エ縺九i襍キ蜍輔&縺帙k
*/
static void triggerScreenshot() {
ISteamScreenshots *shots = SteamScreenshots();
if (shots) {
shots->TriggerScreenshot();
}
}
/**
* @param callback 繧偵ヵ繝�け縺吶k繧ウ繝シ繝ォ繝舌ャ繧ッ髢「謨ー(void縺ェ繧蛾幕謾セ)
* @return 逋サ骭イ繝サ隗」髯、縺ォ謌仙粥縺励◆繧液rue
*/
static bool hookScreenshots(tTJSVariant callback) {
if (instance) {
return instance->_hookScreenshots(callback);
}
return false;
}
/**
* 繧ケ繧ッ繝ェ繝シ繝ウ繧キ繝ァ繝�ヨ縺ョ逋サ骭イ
* @param layer 逕サ蜒
* @param location 蝣エ謇譁�ュ怜�
*/
static void writeScreenshot(iTJSDispatch2 *layer, ttstr location) {
if (instance) {
instance->_writeScreenshot(layer, location);
}
}
// ---------------------------------------------------------
// 繝悶Ο繝シ繝峨く繝」繧ケ繝亥宛蠕。
// ---------------------------------------------------------
static bool isBroadcasting() {
int numViewers = 0;
ISteamVideo *video = SteamVideo();
return video && video->IsBroadcasting(&numViewers);
}
static bool hookBroadcasting(tTJSVariant callback) {
if (instance) {
return instance->_hookBroadcasting(callback);
}
return false;
}
// ---------------------------------------------------------
// DLC諠�ア
// ---------------------------------------------------------
// only use this member if you need to check ownership of another game related to yours, a demo for example
static bool isIsSubscribedApp(uint32 appID) {
bool ret = false;
ISteamApps *app = SteamApps();
if (app) {
ret = app->BIsSubscribedApp( (AppId_t)appID );
}
return ret;
}
// Takes AppID of DLC and checks if the user owns the DLC & if the DLC is installed
static bool isDlcInstalled(uint32 appID) {
bool ret = false;
ISteamApps *app = SteamApps();
if (app) {
ret = app->BIsDlcInstalled( (AppId_t)appID );
}
return ret;
}
/**
* @return DLC縺ョ蛟区焚繧定ソ斐☆
*/
static int getDLCCount() {
int ret = 0;
ISteamApps *app = SteamApps();
if (app) {
ret = app->GetDLCCount();
}
return ret;
}
/**
* @param no DLC縺ョ逡ェ蜿キ
* @rerurn DLC諠�ア
appID
aAvailable
chName
*
*/
static tTJSVariant getDLCData(int no) {
tTJSVariant ret;
ISteamApps *app = SteamApps();
if (app) {
AppId_t appId;
bool available;
char chName[4096];
if (app->BGetDLCDataByIndex(no, &appId, &available, chName, sizeof chName-1)) {
ttstr name = convertUtf8StringToTtstr(chName);
iTJSDispatch2 *dict = TJSCreateDictionaryObject();
if (dict) {
ncbPropAccessor obj(dict);
obj.SetValue(TJS_W("appId"), appId);
obj.SetValue(TJS_W("available"), available);
obj.SetValue(TJS_W("chName"), name);
ret = tTJSVariant(dict, dict);
dict->Release();
}
}
}
return ret;
}
public:
virtual void TJS_INTF_METHOD OnContinuousCallback(tjs_uint64 tick) {
SteamAPI_RunCallbacks();
}
protected:
static Steam *instance;
bool _hookScreenshots(tTJSVariant callback);
bool _hookBroadcasting(tTJSVariant callback);
void _writeScreenshot(iTJSDispatch2 *layer, ttstr location);
bool inited;
SteamAchievements *achieve;
SteamScreenshotCallback *screenshot;
SteamBroadcastCallback *broadcast;
};
/**
* SteamCallback繧ッ繝ゥ繧ケ
*/
class SteamCallback : public tTJSVariantClosure {
public:
SteamCallback() : tTJSVariantClosure(NULL, NULL) {}
virtual ~SteamCallback() { Release(); }
bool setCallback(tTJSVariant const &var) {
if (var.Type() == tvtObject) {
tTJSVariantClosure closure(var.AsObjectClosureNoAddRef());
Object = closure.Object;
ObjThis = closure.ObjThis;
AddRef();
} else {
Object = NULL;
ObjThis = NULL;
}
return Object != NULL;
}
bool invokeCallback(tjs_uint numparams , tTJSVariant * * params) const {
tTJSVariant result;
return Object && Try_iTJSDispatch2_FuncCall(Object, 0, NULL, NULL, &result, numparams, params, ObjThis) == TJS_S_OK;
}
private:
tTJSVariantClosure callback;
};
/**
* SteamScreenshotCallback繧ッ繝ゥ繧ケ
*/
class SteamScreenshotCallback : public SteamCallback {
public:
SteamScreenshotCallback() : SteamCallback()
,m_CallbackScreenshotRequested(this, &SteamScreenshotCallback::OnScreenShotRequested)
// ,m_CallbackScreenshotReady(this, &SteamScreenshotCallback::OnScreenShotReady)
{
}
STEAM_CALLBACK(SteamScreenshotCallback, OnScreenShotRequested, ScreenshotRequested_t, m_CallbackScreenshotRequested);
// STEAM_CALLBACK(SteamScreenshotCallback, OnScreenShotReady, ScreenshotReady_t, m_CallbackScreenshotReady);
};
void
SteamScreenshotCallback::OnScreenShotRequested(ScreenshotRequested_t *pCallback)
{
// 諱舌i縺輯teamAPI_RunCallbacks() 縺ョ繧ソ繧、繝溘Φ繧ー縺ァ蜻シ縺ー繧後k縺ィ諤昴o繧後k
invokeCallback(0, NULL);
// FuncCall 縺ァ縺ェ縺上※ TVPPostEvent 縺ェ繧薙◆繧峨�縺後h縺九▲縺溘°繧
//::TVPPostEvent(source, target, TJS_W("onSteamScreenShotRequested"), 0, TVP_EPT_EXCLUSIVE, numargs, args);
}
/*
// 繧ケ繧ッ繝ェ繝シ繝ウ繧キ繝ァ繝�ヨ繧呈聴縺」縺溷セ後↓蜻シ縺ー繧後k�滂シ医ち繧ー莉倥¢繧�ocation繧定ィュ螳壹☆繧句エ蜷医↑縺ゥ�
void
SteamScreenshotCallback::OnScreenShotReady(ScreenshotReady_t *pCallback)
{
ISteamScreenshots *shots = SteamScreenshots();
if (shots) {
ScreenshotHandle hScreenshot = pCallback->m_hLocal;
//shorts->SetLocation(hScreenshot, pchLocation);
//shorts->TagPublishedFile(hScreenshot, unPublishedFileID);
}
}
*/
/**
* SteamBroadcastCallback繧ッ繝ゥ繧ケ
*/
class SteamBroadcastCallback : public SteamCallback {
public:
SteamBroadcastCallback() : SteamCallback()
,m_CallbackBroadcastStart(this, &SteamBroadcastCallback::OnBroadcastStart)
,m_CallbackBroadcastStop(this, &SteamBroadcastCallback::OnBroadcastStop)
{
}
STEAM_CALLBACK(SteamBroadcastCallback, OnBroadcastStart, BroadcastUploadStart_t, m_CallbackBroadcastStart);
STEAM_CALLBACK(SteamBroadcastCallback, OnBroadcastStop, BroadcastUploadStop_t, m_CallbackBroadcastStop);
};
void
SteamBroadcastCallback::OnBroadcastStart(BroadcastUploadStart_t *pCallback)
{
tTJSVariant state(1), *args = &state;
invokeCallback(1, &args);
}
void
SteamBroadcastCallback::OnBroadcastStop(BroadcastUploadStop_t *pCallback)
{
tTJSVariant state(0), *args = &state;
invokeCallback(1, &args);
}
class LayerImageToRGB {
public:
/**/ LayerImageToRGB() : buffer(0), size(0), width(0), height(0) {}
/**/~LayerImageToRGB() { term(); }
void init(iTJSDispatch2 *lay) {
term();
if (!lay) return;
const BYTE *ptr;
long pitch;
if (!GetLayerSize(lay, width, height) ||
!GetLayerImage(lay, ptr, pitch)) return;
size = width * height * 3;
buffer = new BYTE[size];
BYTE *w = buffer;
for (size_t y = 0; y < height; y++) {
const BYTE* r = ptr + pitch * y;
for (size_t x = 0; x < width; x++, r+=4) {
*w++ = r[2];
*w++ = r[1];
*w++ = r[0];
}
}
}
void term() {
if (buffer) delete[] buffer;
buffer = 0;
size = width = height = 0;
}
inline void *getRGB() const { return buffer; }
inline uint32 getSize() const { return size; }
inline int getWidth() const { return width; }
inline int getHeight() const { return height; }
private:
BYTE *buffer;
size_t size, width, height;
static iTJSDispatch2 *LayerClass;
static bool GetLayerSize(iTJSDispatch2 *lay, size_t &w, size_t &h) {
static ttstr hasImage (TJS_W("hasImage"));
static ttstr imageWidth (TJS_W("imageWidth"));
static ttstr imageHeight(TJS_W("imageHeight"));
tTVInteger lw, lh;
if (!LayerPropGet(lay, hasImage) ||
(lw = LayerPropGet(lay, imageWidth )) <= 0 ||
(lh = LayerPropGet(lay, imageHeight)) <= 0) return false;
w = (size_t)lw;
h = (size_t)lh;
return true;
}
static bool GetLayerImage(iTJSDispatch2 *lay, const BYTE* &ptr, long &pitch) {
static ttstr mainImageBufferPitch(TJS_W("mainImageBufferPitch"));
static ttstr mainImageBuffer(TJS_W("mainImageBuffer"));
tTVInteger lpitch, lptr;
if ((lpitch = LayerPropGet(lay, mainImageBufferPitch)) == 0 ||
(lptr = LayerPropGet(lay, mainImageBuffer)) == 0) return false;
pitch = (long)lpitch;
ptr = reinterpret_cast<const BYTE*>(lptr);
return true;
}
static tTVInteger LayerPropGet(iTJSDispatch2 *lay, ttstr &prop, tTVInteger defval = 0) {
if (!LayerClass) {
tTJSVariant var;
TVPExecuteExpression(TJS_W("Layer"), &var);
LayerClass = var.AsObjectNoAddRef();
}
tTJSVariant val;
return (TJS_FAILED(LayerClass->PropGet(0, prop.c_str(), prop.GetHint(), &val, lay))) ? defval : val.AsInteger();
}
};
iTJSDispatch2 *LayerImageToRGB::LayerClass = 0;
// ---------------------------------------------------------
// 螳溯」
Steam::~Steam() {
if (achieve) {
delete achieve;
achieve = 0;
}
if (screenshot) {
delete screenshot;
screenshot = 0;
}
if (broadcast) {
delete broadcast;
broadcast = 0;
}
if (inited){
TVPRemoveContinuousEventHook(this);
SteamAPI_Shutdown();
}
}
bool Steam::_hookScreenshots(tTJSVariant callback) {
ISteamScreenshots *shots = SteamScreenshots();
if (shots) {
if(!screenshot) {
screenshot = new SteamScreenshotCallback();
}
if (screenshot) {
bool bHook = screenshot->setCallback(callback);
shots->HookScreenshots(bHook);
return true;
}
}
return false;
}
bool Steam::_hookBroadcasting(tTJSVariant callback) {
if(!broadcast) {
broadcast = new SteamBroadcastCallback();
}
if (broadcast) {
broadcast->setCallback(callback);
return true;
}
return false;
}
void Steam::_writeScreenshot(iTJSDispatch2 *layer, ttstr location) {
if (!layer) return;
ISteamScreenshots *shots = SteamScreenshots();
if (shots) {
LayerImageToRGB *image = new LayerImageToRGB();
try {
image->init(layer);
ScreenshotHandle handle = shots->WriteScreenshot(image->getRGB(), image->getSize(), image->getWidth(), image->getHeight());
if (!location.IsEmpty()) {
std::string text = convertTtstrToUtf8String(location);
shots->SetLocation(handle, text.c_str());
}
} catch (...) {
delete image;
throw;
}
delete image;
}
}
Steam *Steam::instance = 0;
NCB_REGISTER_CLASS(Steam) {
Factory(&Class::Factory);
NCB_METHOD(getLanguage);
// Steam 譛ェ蛻晄悄蛹悶〒繧ょ他縺ケ繧 (驕�サカ蛻晄悄蛹悶r襍キ縺薙&縺ェ縺)縲
// 窶サ繝。繝ウ繝仙錐繧貞、峨∴縺溘i SteamDelayRegister::PassThroughCheck 繧ら峩縺吶%縺ィ
NCB_METHOD(RestartAppIfNecessary);
// 繝ヲ繝シ繧カ諠�ア (繧サ繝シ繝悶ョ繝シ繧ソ縺ョ鄂ョ縺榊エ謇繧偵Θ繝シ繧カ縺斐→縺ォ蛻�¢繧狗畑騾)
NCB_PROPERTY_RO(steamID, getSteamID);
NCB_PROPERTY_RO(accountID, getAccountID);
NCB_PROPERTY_RO(loggedOn, getLoggedOn);
NCB_PROPERTY_RO(personaName, getPersonaName);
NCB_PROPERTY_RO(appID, getAppID);
NCB_METHOD(requestInitialize);
NCB_PROPERTY_RO(initialized, getInitialized);
NCB_PROPERTY_RO(achievementsCount, getAchievementsCount);
NCB_METHOD(getAchievement);
NCB_METHOD(setAchievement);
NCB_METHOD(clearAchievement);
NCB_PROPERTY(cloudEnabled, getCloudEnabled, setCloudEnabled);
NCB_METHOD(getCloudQuota);
NCB_PROPERTY_RO(cloudFileCount, getCloudFileCount);
NCB_METHOD(getCloudFileInfo);
NCB_METHOD(deleteCloudFile);
NCB_METHOD(copyCloudFile);
NCB_METHOD(triggerScreenshot);
NCB_METHOD(hookScreenshots);
NCB_METHOD(writeScreenshot);
NCB_METHOD(isBroadcasting);
NCB_METHOD(hookBroadcasting);
NCB_METHOD(isIsSubscribedApp);
NCB_METHOD(isDlcInstalled);
NCB_METHOD(getDLCCount);
NCB_METHOD(getDLCData);
}
// 隱ュ縺ソ霎シ縺ソ譎ゅ↓ SteamAPI_Init() 縺セ縺ァ貂医∪縺帙※縺励∪縺�→縲ヾteam 繧ッ繝ゥ繧、繧「繝ウ繝医′
// 蜍輔>縺ヲ縺�↑縺�腸蠅�〒縺ッ繝励Λ繧ー繧、繝ウ繧貞�繧後◆譎らせ縺ァ襍キ蜍輔〒縺阪↑縺上↑繧九
// 逋サ骭イ逶エ蠕後�繝励Ο繧ュ繧キ縺縺醍スョ縺阪∵怙蛻昴↓ Steam.* 縺悟盾辣ァ縺輔l縺滓凾轤ケ縺ァ蛻晄悄蛹悶☆繧九
NCB_REGISTER_CALLBACK(PostRegist, Steam::setupSteam, 0, 0_setupSteam);
NCB_REGISTER_CALLBACK(PostRegist, 0, Steam::unregisterSteam, 0_unregisterSteam);