Fix multiple CEF bug and add player verification

This commit is contained in:
Micah Allen 2018-11-05 10:06:37 -05:00
parent 24d9313c99
commit 6bd89ddeea

View File

@ -10,9 +10,7 @@ const PROCEDURE_EXISTS = '__rpc:exists';
const glob = environment === "cef" ? window : global; const glob = environment === "cef" ? window : global;
const init = !glob[PROCESS_EVENT]; if(!glob[PROCESS_EVENT]){
if(init){
glob.__rpcListeners = {}; glob.__rpcListeners = {};
glob.__rpcPending = {}; glob.__rpcPending = {};
@ -21,7 +19,6 @@ if(init){
if(environment === "server") rawData = args[1]; if(environment === "server") rawData = args[1];
const data = util.parseData(rawData); const data = util.parseData(rawData);
if(data.req){ // someone is trying to remotely call a procedure if(data.req){ // someone is trying to remotely call a procedure
const info = { const info = {
id: data.id, id: data.id,
@ -31,7 +28,8 @@ if(init){
const promise = callProcedure(data.name, data.args, info); const promise = callProcedure(data.name, data.args, info);
const part = { const part = {
ret: 1, ret: 1,
id: data.id id: data.id,
env: environment
}; };
switch(environment){ switch(environment){
case "server": { case "server": {
@ -66,12 +64,12 @@ if(init){
passEventToBrowsers({ passEventToBrowsers({
...part, ...part,
res res
}); }, true);
}).catch(err => { }).catch(err => {
passEventToBrowsers({ passEventToBrowsers({
...part, ...part,
err err
}); }, true);
}); });
} }
break; break;
@ -92,6 +90,7 @@ if(init){
} }
}else if(data.ret){ // a previously called remote procedure has returned }else if(data.ret){ // a previously called remote procedure has returned
const info = glob.__rpcPending[data.id]; const info = glob.__rpcPending[data.id];
if(environment === "server" && info.player !== args[0]) return;
if(info){ if(info){
if(data.err) info.reject(data.err); if(data.err) info.reject(data.err);
else info.resolve(data.res); else info.resolve(data.res);
@ -123,13 +122,13 @@ if(init){
let passEventToBrowser, passEventToBrowsers; let passEventToBrowser, passEventToBrowsers;
if(environment === "client"){ if(environment === "client"){
passEventToBrowser = (browser, data) => { passEventToBrowser = (browser, data, ignore) => {
const raw = util.stringifyData(data); const raw = util.stringifyData(data);
browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}"}'); }`); browser.execute(`var process = window["${PROCESS_EVENT}"]; if(process){ process('${raw}'); }else{ ${ignore ? '' : `mp.trigger("${PROCESS_EVENT}", '{"ret":1,"id":"${data.id}","err":"${ERR_NOT_FOUND}","env":"cef"}');`} }`);
}; };
passEventToBrowsers = (data) => { passEventToBrowsers = (data, ignore) => {
mp.browsers.forEach(browser => passEventToBrowser(browser, data)); mp.browsers.forEach(browser => passEventToBrowser(browser, data, ignore));
}; };
} }
@ -243,7 +242,8 @@ export function callClient(player, name, args){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
glob.__rpcPending[id] = { glob.__rpcPending[id] = {
resolve, resolve,
reject reject,
player
}; };
player.call(PROCESS_EVENT, [util.stringifyData({ player.call(PROCESS_EVENT, [util.stringifyData({
req: 1, req: 1,
@ -289,7 +289,7 @@ function _callBrowser(id, browser, name, args, extraData){
env: environment, env: environment,
args, args,
...extraData ...extraData
}); }, false);
}); });
} }
async function _callBrowsers(player, name, args, extraData){ async function _callBrowsers(player, name, args, extraData){