Skip to content

Commit 95b8dba

Browse files
committed
feat(game): hijack screenshot-basic exports
1 parent 7b05fb9 commit 95b8dba

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

game/client/bootstrap.ts

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { request } from 'http';
12
import { netEventController } from './event';
23
import { CaptureRequest, RequestScreenshotUploadCB, ScreenshotCreatedBody } from './types';
3-
import { uuidv4 } from './utils';
4-
5-
RegisterNuiCallbackType('screenshot_created');
4+
import { exportHandler, uuidv4 } from './utils';
65

76
const clientCaptureMap = new Map<string, RequestScreenshotUploadCB>();
87

8+
RegisterNuiCallbackType('screenshot_created');
9+
910
onNet('screencapture:captureScreen', (token: string, options: object, dataType: string) => {
1011
SendNUIMessage({
1112
...options,
@@ -38,6 +39,45 @@ on('__cfx_nui:screenshot_created', (body: ScreenshotCreatedBody, cb: (arg: any)
3839
}
3940
});
4041

42+
async function requestScreenshotUpload(
43+
url: string,
44+
formField: string,
45+
optionsOrCB: CaptureRequest | RequestScreenshotUploadCB,
46+
callback: RequestScreenshotUploadCB,
47+
) {
48+
// forgive me
49+
const isOptions = typeof optionsOrCB === 'object' && optionsOrCB !== null;
50+
const realOptions = isOptions
51+
? (optionsOrCB as CaptureRequest)
52+
: ({ headers: {}, encoding: 'webp' } as CaptureRequest);
53+
const realCallback = isOptions
54+
? (callback as RequestScreenshotUploadCB)
55+
: (optionsOrCB as RequestScreenshotUploadCB);
56+
57+
const correlationId = uuidv4();
58+
clientCaptureMap.set(correlationId, realCallback);
59+
60+
const token = await netEventController<string>('screencapture:INTERNAL_requestUploadToken', {
61+
...realOptions,
62+
formField,
63+
url,
64+
correlationId,
65+
});
66+
67+
if (!token) {
68+
return console.error('Failed to get upload token');
69+
}
70+
71+
return createImageCaptureMessage({
72+
...realOptions,
73+
formField,
74+
url,
75+
uploadToken: token,
76+
dataType: 'blob',
77+
});
78+
}
79+
80+
exportHandler('requestScreenshotUpload', requestScreenshotUpload);
4181
global.exports(
4282
'requestScreenshotUpload',
4383
async (
@@ -46,41 +86,11 @@ global.exports(
4686
optionsOrCB: CaptureRequest | RequestScreenshotUploadCB,
4787
callback: RequestScreenshotUploadCB,
4888
) => {
49-
// forgive me
50-
const isOptions = typeof optionsOrCB === 'object' && optionsOrCB !== null;
51-
const realOptions = isOptions
52-
? (optionsOrCB as CaptureRequest)
53-
: ({ headers: {}, encoding: 'webp' } as CaptureRequest);
54-
const realCallback = isOptions
55-
? (callback as RequestScreenshotUploadCB)
56-
: (optionsOrCB as RequestScreenshotUploadCB);
57-
58-
const correlationId = uuidv4();
59-
clientCaptureMap.set(correlationId, realCallback);
60-
61-
const token = await netEventController<string>('screencapture:INTERNAL_requestUploadToken', {
62-
...realOptions,
63-
formField,
64-
url,
65-
correlationId,
66-
});
67-
68-
if (!token) {
69-
return console.error('Failed to get upload token');
70-
}
71-
72-
return createImageCaptureMessage({
73-
...realOptions,
74-
formField,
75-
url,
76-
uploadToken: token,
77-
dataType: 'blob',
78-
});
79-
},
89+
return await requestScreenshotUpload(url, formField, optionsOrCB, callback);
90+
}
8091
);
8192

82-
// returns base64 data URI
83-
global.exports('requestScreenshot', (options: CaptureRequest, callback: RequestScreenshotUploadCB) => {
93+
function requestScreenshot(options: CaptureRequest, callback: RequestScreenshotUploadCB) {
8494
const correlationId = uuidv4();
8595

8696
const realOptions = (callback !== undefined) ? options : {
@@ -100,6 +110,11 @@ global.exports('requestScreenshot', (options: CaptureRequest, callback: RequestS
100110
callbackUrl: `http://${GetCurrentResourceName()}/screenshot_created`,
101111
correlationId,
102112
});
113+
}
114+
115+
exportHandler('requestScreenshot', requestScreenshot);
116+
global.exports('requestScreenshot', (options: CaptureRequest, callback: RequestScreenshotUploadCB) => {
117+
return requestScreenshot(options, callback);
103118
});
104119

105120
function createImageCaptureMessage(options: CaptureRequest) {

game/client/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ export const uuidv4 = (): string => {
2020
}
2121
}
2222
return uuid;
23-
};
23+
};
24+
25+
26+
// https://github.com/overextended/ox_target/blob/main/client/compat/qtarget.lua#L1
27+
export function exportHandler(exportName: string, func: (...args: any[]) => any): void {
28+
AddEventHandler(`__cfx_export_screenshot-basic_${exportName}`, (setCB: (cb: (...args: any[]) => any) => void) => {
29+
setCB(func);
30+
});
31+
}

0 commit comments

Comments
 (0)