Skip to content

Commit 8a64fea

Browse files
Added autodetect logic beta
1 parent 4bfe96d commit 8a64fea

File tree

1 file changed

+105
-43
lines changed

1 file changed

+105
-43
lines changed

src/index.ts

Lines changed: 105 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,53 @@ export class Framework {
6969

7070
if (this.config.AutoDetect) {
7171
console.log(
72-
"WARNING: AutoDetect is currently not supported in this build. Please set AutoDetect to false in framework.json and select a framework manually."
72+
"WARNING: AutoDetect is currently experimantal in this build. There might be bugs. Please report them in the GitHub repository (https://github.com/Garmingo/framework-js-server)."
7373
);
74+
75+
/* -- Check for ESX Legacy -- */
76+
try {
77+
if (exports.es_extended.getSharedObject()) {
78+
this.config.Framework = "ESX Legacy";
79+
console.log("ESX Legacy detected. Initializing...");
80+
}
81+
} catch (e) {}
82+
/* -- Check for ESX Legacy -- */
83+
84+
/* -- Check for QBCore -- */
85+
if (this.config.Framework === "None") {
86+
try {
87+
if (exports["qb-core"]["GetCoreObject"]()) {
88+
this.config.Framework = "QBCore";
89+
console.log("QBCore detected. Initializing...");
90+
}
91+
} catch (e) {}
92+
}
93+
/* -- Check for QBCore -- */
94+
95+
/* -- Check for Custom Framework -- */
96+
if (this.config.Framework === "None") {
97+
try {
98+
if (exports[this.config.ExportResource].GetPlayerJobName()) {
99+
this.config.Framework = "Custom";
100+
console.log("Custom framework detected. Initializing...");
101+
}
102+
} catch (e) {}
103+
}
104+
/* -- Check for Custom Framework -- */
105+
106+
/* -- Check for ESX Infinity -- */
107+
if (this.config.Framework === "None") {
108+
try {
109+
emit(this.config.ESXEvent, (obj: any) => {
110+
this.framework = obj;
111+
});
112+
if (this.framework) {
113+
this.config.Framework = "ESX Infinity";
114+
console.log("ESX Infinity detected. Initializing...");
115+
}
116+
} catch (e) {}
117+
}
118+
/* -- Check for ESX Infinity -- */
74119
}
75120

76121
if (this.config.Framework === "None") {
@@ -203,17 +248,16 @@ export class Framework {
203248
public addPlayerWalletMoney(player: number, amount: number): void {
204249
switch (this.config.Framework) {
205250
case "ESX Legacy":
206-
this.framework
207-
.GetPlayerFromId(player)
208-
.addMoney(amount);
251+
this.framework.GetPlayerFromId(player).addMoney(amount);
209252
break;
210253
case "ESX Infinity":
211-
this.framework
212-
.GetPlayerFromId(player)
213-
.addMoney(amount);
254+
this.framework.GetPlayerFromId(player).addMoney(amount);
214255
break;
215256
case "QBCore":
216-
this.framework.Functions.GetPlayer(player).Functions.AddMoney("cash", amount);
257+
this.framework.Functions.GetPlayer(player).Functions.AddMoney(
258+
"cash",
259+
amount
260+
);
217261
break;
218262
case "Custom":
219263
exports[this.config.ExportResource].AddPlayerWalletMoney(
@@ -235,17 +279,16 @@ export class Framework {
235279
public removePlayerWalletMoney(player: number, amount: number): void {
236280
switch (this.config.Framework) {
237281
case "ESX Legacy":
238-
this.framework
239-
.GetPlayerFromId(player)
240-
.removeMoney(amount);
282+
this.framework.GetPlayerFromId(player).removeMoney(amount);
241283
break;
242284
case "ESX Infinity":
243-
this.framework
244-
.GetPlayerFromId(player)
245-
.removeMoney(amount);
285+
this.framework.GetPlayerFromId(player).removeMoney(amount);
246286
break;
247287
case "QBCore":
248-
this.framework.Functions.GetPlayer(player).Functions.RemoveMoney("cash", amount);
288+
this.framework.Functions.GetPlayer(player).Functions.RemoveMoney(
289+
"cash",
290+
amount
291+
);
249292
break;
250293
case "Custom":
251294
exports[this.config.ExportResource].RemovePlayerWalletMoney(
@@ -264,20 +307,23 @@ export class Framework {
264307
* @param account Name of the account (e.g. bank)
265308
* @param amount Amount to add
266309
*/
267-
public addPlayerAccountMoney(player: number, account: string, amount: number): void {
310+
public addPlayerAccountMoney(
311+
player: number,
312+
account: string,
313+
amount: number
314+
): void {
268315
switch (this.config.Framework) {
269316
case "ESX Legacy":
270-
this.framework
271-
.GetPlayerFromId(player)
272-
.addAccountMoney(account, amount);
317+
this.framework.GetPlayerFromId(player).addAccountMoney(account, amount);
273318
break;
274319
case "ESX Infinity":
275-
this.framework
276-
.GetPlayerFromId(player)
277-
.addAccountMoney(account, amount);
320+
this.framework.GetPlayerFromId(player).addAccountMoney(account, amount);
278321
break;
279322
case "QBCore":
280-
this.framework.Functions.GetPlayer(player).Functions.AddMoney(account, amount);
323+
this.framework.Functions.GetPlayer(player).Functions.AddMoney(
324+
account,
325+
amount
326+
);
281327
break;
282328
case "Custom":
283329
exports[this.config.ExportResource].AddPlayerAccountMoney(
@@ -297,7 +343,11 @@ export class Framework {
297343
* @param account Name of the account (e.g. bank)
298344
* @param amount Amount to remove
299345
*/
300-
public removePlayerAccountMoney(player: number, account: string, amount: number): void {
346+
public removePlayerAccountMoney(
347+
player: number,
348+
account: string,
349+
amount: number
350+
): void {
301351
switch (this.config.Framework) {
302352
case "ESX Legacy":
303353
this.framework
@@ -310,7 +360,10 @@ export class Framework {
310360
.removeAccountMoney(account, amount);
311361
break;
312362
case "QBCore":
313-
this.framework.Functions.GetPlayer(player).Functions.RemoveMoney(account, amount);
363+
this.framework.Functions.GetPlayer(player).Functions.RemoveMoney(
364+
account,
365+
amount
366+
);
314367
break;
315368
case "Custom":
316369
exports[this.config.ExportResource].RemovePlayerAccountMoney(
@@ -331,24 +384,27 @@ export class Framework {
331384
* @param item Name of the item to add
332385
* @param amount Amount of the item to add
333386
*/
334-
public addPlayerInventoryItem(player: number, item: string, amount: number): void {
387+
public addPlayerInventoryItem(
388+
player: number,
389+
item: string,
390+
amount: number
391+
): void {
335392
if (amount <= 0) {
336393
return;
337394
}
338395

339396
switch (this.config.Framework) {
340397
case "ESX Legacy":
341-
this.framework
342-
.GetPlayerFromId(player)
343-
.addInventoryItem(item, amount);
398+
this.framework.GetPlayerFromId(player).addInventoryItem(item, amount);
344399
break;
345400
case "ESX Infinity":
346-
this.framework
347-
.GetPlayerFromId(player)
348-
.addInventoryItem(item, amount);
401+
this.framework.GetPlayerFromId(player).addInventoryItem(item, amount);
349402
break;
350403
case "QBCore":
351-
this.framework.Functions.GetPlayer(player).Functions.AddItem(item, amount);
404+
this.framework.Functions.GetPlayer(player).Functions.AddItem(
405+
item,
406+
amount
407+
);
352408
break;
353409
case "Custom":
354410
exports[this.config.ExportResource].AddPlayerInventoryItem(
@@ -371,7 +427,11 @@ export class Framework {
371427
* @param item Name of the item to remove
372428
* @param amount Amount of the item to remove
373429
*/
374-
public removePlayerInventoryItem(player: number, item: string, amount: number): void {
430+
public removePlayerInventoryItem(
431+
player: number,
432+
item: string,
433+
amount: number
434+
): void {
375435
if (amount <= 0) {
376436
return;
377437
}
@@ -388,7 +448,10 @@ export class Framework {
388448
.removeInventoryItem(item, amount);
389449
break;
390450
case "QBCore":
391-
this.framework.Functions.GetPlayer(player).Functions.RemoveItem(item, amount);
451+
this.framework.Functions.GetPlayer(player).Functions.RemoveItem(
452+
item,
453+
amount
454+
);
392455
break;
393456
case "Custom":
394457
exports[this.config.ExportResource].RemovePlayerInventoryItem(
@@ -415,15 +478,15 @@ export class Framework {
415478
public getPlayerInventoryItemCount(player: number, item: string): number {
416479
switch (this.config.Framework) {
417480
case "ESX Legacy":
418-
return this.framework
419-
.GetPlayerFromId(player)
420-
.getInventoryItem(item).count;
481+
return this.framework.GetPlayerFromId(player).getInventoryItem(item)
482+
.count;
421483
case "ESX Infinity":
422-
return this.framework
423-
.GetPlayerFromId(player)
424-
.getInventoryItem(item).count;
484+
return this.framework.GetPlayerFromId(player).getInventoryItem(item)
485+
.count;
425486
case "QBCore":
426-
return this.framework.Functions.GetPlayer(player).Functions.GetItemByName(item).amount;
487+
return this.framework.Functions.GetPlayer(
488+
player
489+
).Functions.GetItemByName(item).amount;
427490
case "Custom":
428491
return exports[this.config.ExportResource].GetPlayerInventoryItemCount(
429492
player,
@@ -433,5 +496,4 @@ export class Framework {
433496
return 0;
434497
}
435498
}
436-
437499
}

0 commit comments

Comments
 (0)