-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMain.cpp
More file actions
688 lines (606 loc) · 18.2 KB
/
Main.cpp
File metadata and controls
688 lines (606 loc) · 18.2 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
#include <windows.h>
#include "tp_stub.h"
#include "ncbind.hpp"
#include <string>
#include "SteamAchievements.h"
#include <steam_api.h>
#pragma comment(lib,"steam_api.lib")
//ライブラリ互換がロストしてるようなので仮対策・機能的に無効かどうかは不明
#ifndef BroadcastUploadStart_t
STEAM_CALLBACK_BEGIN(BroadcastUploadStart_t, k_iClientVideoCallbacks + 4)
STEAM_CALLBACK_END(0)
#endif
#ifndef BroadcastUploadStop_t
STEAM_CALLBACK_BEGIN(BroadcastUploadStop_t, k_iClientVideoCallbacks + 5)
STEAM_CALLBACK_MEMBER(0, EBroadcastUploadResult, m_eResult)
STEAM_CALLBACK_END(1)
#endif
// ttstrをUTF8文字列へ変換
std::string
convertTtstrToUtf8String(ttstr &buf)
{
int maxlen = buf.length() * 6 + 1;
char *dat = new char[maxlen];
int len = TVPWideCharToUtf8String(buf.c_str(), dat);
std::string result(dat, len);
delete[] dat;
return result;
}
// std::stringをttstrに変換
ttstr
convertUtf8StringToTtstr(const char *buf, size_t length=-1)
{
if (length < 0) {
length = strlen(buf);
}
tjs_uint maxlen = (tjs_uint)length * 2 + 1;
tjs_char *dat = new tjs_char[maxlen];
tjs_uint 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:
// 初期化
static void registerSteam() {
instance = new Steam();
instance->init();
initStorage();
}
// 解除
static void unregisterSteam() {
doneStorage();
delete instance;
}
// コンストラクタ用(常に例外)
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;
}
// ---------------------------------------------------------
// 情報
// ---------------------------------------------------------
// 現在の言語を取得
static ttstr getLanguage() {
ttstr ret;
ISteamApps *app = SteamApps();
if (app) {
std::string lang = app->GetCurrentGameLanguage();
ret = convertUtf8StringToTtstr(lang);
}
return ret;
}
// ---------------------------------------------------------
// クラウド
// ---------------------------------------------------------
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;
}
// ---------------------------------------------------------
// スクリーンショット制御
// ---------------------------------------------------------
/**
* スクリーンショット処理をアプリ側から起動させる
*/
static void triggerScreenshot() {
ISteamScreenshots *shots = SteamScreenshots();
if (shots) {
shots->TriggerScreenshot();
}
}
/**
* @param callback をフックするコールバック関数(voidなら開放)
* @return 登録・解除に成功したらtrue
*/
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)
{
// 恐らくSteamAPI_RunCallbacks() のタイミングで呼ばれると思われる
invokeCallback(0, NULL);
// FuncCall でなくて TVPPostEvent なんたらのがよかったかも
//::TVPPostEvent(source, target, TJS_W("onSteamScreenShotRequested"), 0, TVP_EPT_EXCLUSIVE, numargs, args);
}
/*
// スクリーンショットを撮った後に呼ばれる?(タグ付けやlocationを設定する場合など)
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);
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);
}
NCB_REGISTER_CALLBACK(PreRegist, Steam::registerSteam, 0, registerSteam_0);
NCB_REGISTER_CALLBACK(PostRegist, 0, Steam::unregisterSteam, 0_unregisterSteam);