mirror of
https://github.com/asphyxia-core/core.git
synced 2026-12-04 22:49:12 -06:00
pin profile
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { getAttr } from '../../utils/KBinJSON';
|
||||
import { EamuseModuleContainer } from '../EamuseModuleContainer';
|
||||
import { padStart, get, toSafeInteger, toString } from 'lodash';
|
||||
import { Logger } from '../../utils/Logger';
|
||||
import { SAFEHEX } from '../../utils/Consts';
|
||||
|
||||
export const cardmng = new EamuseModuleContainer();
|
||||
|
||||
@@ -7,8 +9,37 @@ const ProfileCheck: {
|
||||
[key: string]: () => boolean;
|
||||
} = {};
|
||||
|
||||
export function AddProfileCheck(gameCode: string, handler: () => boolean) {
|
||||
if (!ProfileCheck[gameCode]) ProfileCheck[gameCode] = handler;
|
||||
const PROFILE_QUEUE = Array(1024).fill(-1);
|
||||
let PROFILE_QUEUE_INDEX = 0;
|
||||
|
||||
function toRefID(id: number) {
|
||||
return SAFEHEX + padStart(id.toString(16).toUpperCase(), 8, '0');
|
||||
}
|
||||
|
||||
function toQueueIndex(ref: string) {
|
||||
if (ref == null) return -1;
|
||||
const result = parseInt(ref.substr(8), 16);
|
||||
if (isNaN(result)) {
|
||||
return -1;
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
export function getProfileFromRef(ref: string) {
|
||||
const index = toQueueIndex(ref);
|
||||
if (index < 0 || index >= 1024) return null;
|
||||
if (PROFILE_QUEUE[index] < 0) return null;
|
||||
return padStart(PROFILE_QUEUE[index].toString(), 4, '0');
|
||||
}
|
||||
|
||||
export function AddProfileCheck(gameCode: string, handler: () => boolean): boolean {
|
||||
if (!ProfileCheck[gameCode]) {
|
||||
ProfileCheck[gameCode] = handler;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function RemoveProfileCheck(gameCode: string) {
|
||||
@@ -18,14 +49,20 @@ export function RemoveProfileCheck(gameCode: string) {
|
||||
cardmng.add('*', 'cardmng.inquire', async (info, data, send) => {
|
||||
const model: string = info.model;
|
||||
const modelCode = model.split(':')[0] || 'NUL';
|
||||
|
||||
const refid = toRefID(PROFILE_QUEUE_INDEX++);
|
||||
if (PROFILE_QUEUE_INDEX >= 1024) {
|
||||
PROFILE_QUEUE_INDEX = 0;
|
||||
}
|
||||
|
||||
await send.object({
|
||||
'@attr': {
|
||||
binded: (ProfileCheck[modelCode] || (() => false))() ? 1 : 0, // Has profile in game
|
||||
dataid: 'DEADC0DEFEEDBEEF', // Player.dataID
|
||||
dataid: refid, // Player.dataID
|
||||
ecflag: 1, // Is currency card
|
||||
expired: 0, // Is expired, if true will treat as new card
|
||||
newflag: 0,
|
||||
refid: 'DEADC0DEFEEDBEEF', // Player.dataID as well
|
||||
refid: refid, // Player.dataID as well
|
||||
},
|
||||
});
|
||||
|
||||
@@ -33,15 +70,28 @@ cardmng.add('*', 'cardmng.inquire', async (info, data, send) => {
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.getrefid', async (info, data, send) => {
|
||||
await send.object({ '@attr': { dataid: 'DEADC0DEFEEDBEEF', refid: 'DEADC0DEFEEDBEEF' } });
|
||||
const refid = toRefID(PROFILE_QUEUE_INDEX++);
|
||||
if (PROFILE_QUEUE_INDEX >= 1024) {
|
||||
PROFILE_QUEUE_INDEX = 0;
|
||||
}
|
||||
|
||||
await send.object({ '@attr': { dataid: refid, refid: refid } });
|
||||
return;
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.authpass', async (info, data, send) => {
|
||||
const refid = toQueueIndex(get(data, '@attr.refid', null));
|
||||
const pass = toSafeInteger(get(data, '@attr.pass', '0000'));
|
||||
|
||||
if (refid >= 0) {
|
||||
PROFILE_QUEUE[refid] = pass;
|
||||
}
|
||||
Logger.debug(`profile mapped: ${refid} -> ${pass}`);
|
||||
// Right password
|
||||
await send.success();
|
||||
});
|
||||
|
||||
cardmng.add('*', 'cardmng.bindmodel', async (info, data, send) => {
|
||||
await send.object({ '@attr': { dataid: 'DEADC0DEFEEDBEEF' } });
|
||||
const refid = get(data, '@attr.refid', 'DEADC0DEFEEDBEEF');
|
||||
await send.object({ '@attr': { dataid: refid } });
|
||||
});
|
||||
|
||||
@@ -45,8 +45,6 @@ export class EamuseModuleContainer {
|
||||
}
|
||||
|
||||
if (gameCode instanceof EamuseModuleContainer) {
|
||||
// this.modules = { ...this.modules, ...gameCode.modules };
|
||||
// this.fallback = { ...this.fallback, ...gameCode.fallback };
|
||||
this.children.push(gameCode);
|
||||
}
|
||||
}
|
||||
@@ -71,33 +69,52 @@ export class EamuseModuleContainer {
|
||||
delete this.fallback[`${gameCode}`];
|
||||
}
|
||||
|
||||
public async run(
|
||||
public clear() {
|
||||
delete this.modules;
|
||||
delete this.fallback;
|
||||
delete this.children;
|
||||
this.modules = {};
|
||||
this.fallback = {};
|
||||
this.children = [];
|
||||
}
|
||||
|
||||
public run(
|
||||
gameCode: string,
|
||||
moduleName: string,
|
||||
method: string,
|
||||
info: EamuseInfo,
|
||||
data: any,
|
||||
send: EamuseSend
|
||||
): Promise<void> {
|
||||
send: EamuseSend,
|
||||
root: boolean = true
|
||||
): boolean {
|
||||
let handler = this.modules[`${moduleName}.${method}`];
|
||||
if (isNil(handler)) handler = this.modules[`${gameCode}:${moduleName}.${method}`];
|
||||
|
||||
if (isNil(handler)) {
|
||||
if (this.fallback[gameCode]) {
|
||||
return this.fallback[gameCode](info, data, send);
|
||||
this.fallback[gameCode](info, data, send);
|
||||
return true;
|
||||
} else {
|
||||
return send.deny();
|
||||
for (const child of this.children) {
|
||||
if (child.run(gameCode, moduleName, method, info, data, send, false)) return true;
|
||||
}
|
||||
|
||||
if (root) send.deny();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof handler === 'boolean') {
|
||||
if (handler) {
|
||||
return send.success();
|
||||
send.success();
|
||||
return true;
|
||||
} else {
|
||||
return send.deny();
|
||||
send.deny();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
handler(info, data, send);
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,14 +174,14 @@ export class EamuseSend {
|
||||
}
|
||||
}
|
||||
success(options?: EamuseSendOption) {
|
||||
this.object({}, { ...options, status: 0 });
|
||||
return this.object({}, { ...options, status: 0 });
|
||||
}
|
||||
|
||||
deny(options?: EamuseSendOption) {
|
||||
this.object({}, { ...options, status: 1 });
|
||||
return this.object({}, { ...options, status: 1 });
|
||||
}
|
||||
|
||||
status(code: number, options?: EamuseSendOption) {
|
||||
this.object({}, { ...options, status: code });
|
||||
return this.object({}, { ...options, status: code });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +118,7 @@ export function LoadExternalModules() {
|
||||
try {
|
||||
const modules = readdirSync(MODULE_PATH);
|
||||
for (const mod of modules) {
|
||||
const name = path.basename(mod);
|
||||
const modulePath = path.resolve(MODULE_PATH, mod);
|
||||
const moduleExt = path.extname(modulePath);
|
||||
|
||||
@@ -136,9 +137,9 @@ export function LoadExternalModules() {
|
||||
instance = instance.default;
|
||||
}
|
||||
|
||||
loadedModules.push({ name: path.basename(mod), instance });
|
||||
loadedModules.push({ name, instance });
|
||||
} catch (err) {
|
||||
Logger.error(`failed to load`, { module: mod });
|
||||
Logger.error(`failed to load`, { module: name });
|
||||
Logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ export const EamuseRoute = (container: EamuseModuleContainer): RequestHandler =>
|
||||
|
||||
const send = new EamuseSend(body, res);
|
||||
try {
|
||||
await container.run(
|
||||
container.run(
|
||||
gameCode,
|
||||
body.module,
|
||||
body.method,
|
||||
|
||||
@@ -23,20 +23,20 @@ parser.addArgument(['-b', '--bind'], {
|
||||
dest: 'bind',
|
||||
});
|
||||
|
||||
parser.addArgument(['-uip', '--webui-port'], {
|
||||
help: 'Set WebUI port. (default: 8084)',
|
||||
defaultValue: 8084,
|
||||
type: 'int',
|
||||
metavar: 'PORT',
|
||||
dest: 'ui_port',
|
||||
});
|
||||
// parser.addArgument(['-uip', '--webui-port'], {
|
||||
// help: 'Set WebUI port. (default: 8084)',
|
||||
// defaultValue: 8084,
|
||||
// type: 'int',
|
||||
// metavar: 'PORT',
|
||||
// dest: 'ui_port',
|
||||
// });
|
||||
|
||||
parser.addArgument(['-uib', '--webui-bind'], {
|
||||
help: 'WebUI Hostname binding. (default: "localhost")',
|
||||
defaultValue: 'localhost',
|
||||
metavar: 'HOST',
|
||||
dest: 'ui_bind',
|
||||
});
|
||||
// parser.addArgument(['-uib', '--webui-bind'], {
|
||||
// help: 'WebUI Hostname binding. (default: "localhost")',
|
||||
// defaultValue: 'localhost',
|
||||
// metavar: 'HOST',
|
||||
// dest: 'ui_bind',
|
||||
// });
|
||||
|
||||
parser.addArgument(['-m', '--matching-port'], {
|
||||
help: 'Set matching port. (default: 5700)',
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export const VERSION = 'v1.02 beta';
|
||||
export const VERSION = 'v1.03 beta';
|
||||
export const SAFEHEX = 'ADEBEEFA';
|
||||
|
||||
@@ -3,6 +3,8 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
|
||||
import { Logger } from './Logger';
|
||||
import path from 'path';
|
||||
import { ARGS } from './ArgParser';
|
||||
import { padStart } from 'lodash';
|
||||
import { getProfileFromRef } from '../eamuse/Core/CardManager';
|
||||
|
||||
export const EXEC_PATH = (process as any).pkg ? path.dirname(process.argv0) : process.cwd();
|
||||
export const MODULE_PATH = path.join(EXEC_PATH, 'modules');
|
||||
@@ -42,16 +44,16 @@ export function GetCallerModule(): { name: string; single: boolean } {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function ExistsFile(file: string): boolean {
|
||||
try {
|
||||
return existsSync(`${file}.json`);
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// export function ExistsFile(file: string, ref?: string): boolean {
|
||||
// try {
|
||||
// return existsSync(`${file}.json`);
|
||||
// } catch (err) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
export function PrepareSaveDirectory(mod: string): string {
|
||||
const dir = path.join(SAVE_PATH, mod);
|
||||
export function PrepareSaveDirectory(mod: string, profileDir: string = ''): string {
|
||||
const dir = path.join(SAVE_PATH, mod, profileDir);
|
||||
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
@@ -60,6 +62,37 @@ export function PrepareSaveDirectory(mod: string): string {
|
||||
return dir;
|
||||
}
|
||||
|
||||
export function ReadPlayerSave(refid: string, file: string): any {
|
||||
const mod = GetCallerModule();
|
||||
if (!mod) {
|
||||
Logger.error(`unknown module:`);
|
||||
Logger.error(new Error().stack);
|
||||
}
|
||||
|
||||
const profile = getProfileFromRef(refid);
|
||||
if (!profile) {
|
||||
Logger.warn(`invalid refid. aborted reading`, { module: mod.name });
|
||||
return null;
|
||||
}
|
||||
|
||||
const dir = PrepareSaveDirectory(mod.name, profile);
|
||||
const fullFile = path.join(dir, profile, `${file}.json`);
|
||||
try {
|
||||
if (!existsSync(fullFile)) {
|
||||
return null;
|
||||
}
|
||||
const data = JSON.parse(
|
||||
readFileSync(fullFile, {
|
||||
encoding: 'UTF-8',
|
||||
})
|
||||
);
|
||||
return data;
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function ReadSave(file: string): any {
|
||||
const mod = GetCallerModule();
|
||||
if (!mod) {
|
||||
@@ -106,6 +139,33 @@ export function WriteSave(file: string, data: any, formated = false) {
|
||||
}
|
||||
}
|
||||
|
||||
export function WritePlayerSave(refid: string, file: string, data: any, formated = false) {
|
||||
const mod = GetCallerModule();
|
||||
if (!mod) {
|
||||
Logger.error(`unknown module:`);
|
||||
Logger.error(new Error().stack);
|
||||
}
|
||||
|
||||
const profile = getProfileFromRef(refid);
|
||||
if (!profile) {
|
||||
Logger.warn(`invalid refid. aborted saving`, { module: mod.name });
|
||||
return;
|
||||
}
|
||||
|
||||
const dir = PrepareSaveDirectory(mod.name, profile);
|
||||
const fullFile = path.join(dir, profile, `${file}.json`);
|
||||
try {
|
||||
if (formated) {
|
||||
writeFileSync(fullFile, JSON.stringify(data, null, 4));
|
||||
} else {
|
||||
writeFileSync(fullFile, JSON.stringify(data));
|
||||
}
|
||||
Logger.info(`${profile}/${file}.json saved`, { module: mod.name });
|
||||
} catch (err) {
|
||||
Logger.error(`file writing failed: ${err}`, { module: mod.name });
|
||||
}
|
||||
}
|
||||
|
||||
// export function WriteDebugFile(file: string, data: any) {
|
||||
// if (process.env.ASPHYXIA_PRINT_LOG !== 'print') return;
|
||||
// try {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { toSafeInteger, isArrayLike, get, isArray, isEmpty, toString, isNil } fr
|
||||
import json2xml = xml2json.j2xParser;
|
||||
|
||||
import { BinaryLengthType, ReadBuffer, WriteBuffer } from './AutoBuffer';
|
||||
import { SAFEHEX } from './Consts';
|
||||
|
||||
export type KAttrMap = { [key: string]: string };
|
||||
export type KNumberType =
|
||||
@@ -422,6 +423,8 @@ function bufferToString(data: Buffer, encoding: string): string {
|
||||
}
|
||||
const str = codeToString(convert(data, JOBJ_ENCODING, ENC_JP_MAP[encoding]));
|
||||
const result = str.substr(0, str.length - 1);
|
||||
if (result.startsWith(SAFEHEX)) return result;
|
||||
|
||||
if (result.length == 16) {
|
||||
const hex_checker = /^[0-9a-fA-F]+$/;
|
||||
if (hex_checker.test(result)) {
|
||||
|
||||
Reference in New Issue
Block a user