diff --git a/src/GHost.ts b/src/GHost.ts index 3dd92f2..b590b3c 100644 --- a/src/GHost.ts +++ b/src/GHost.ts @@ -12,7 +12,7 @@ import {GhostBNetSIDHandler} from "./ghost/bnet/GhostBNetSIDHandler"; import {GhostBNetSIDReceiver} from "./ghost/bnet/GhostBNetSIDReceiver"; import {Config} from "./Config"; import {SHA1} from "./SHA1"; -import {MPQ} from './libStorm'; +import {MPQ} from './storm'; const {debug, info, error} = createLoggerFor('GHost'); diff --git a/src/Storm.ts b/src/Storm.ts deleted file mode 100644 index 0a720ee..0000000 --- a/src/Storm.ts +++ /dev/null @@ -1,5 +0,0 @@ -// import MPQ = require('./libStorm'); - -export class Storm { - -} diff --git a/src/BNCSUtil.ts b/src/bncsutil/BNCSUtil.ts similarity index 90% rename from src/BNCSUtil.ts rename to src/bncsutil/BNCSUtil.ts index 06d8f50..afebf0b 100644 --- a/src/BNCSUtil.ts +++ b/src/bncsutil/BNCSUtil.ts @@ -1,11 +1,10 @@ -// libbncsutil import * as ref from 'ref-napi'; import * as bp from 'bufferpack'; import * as os from 'os'; -import * as lib from './libbncsutil/libbncsutil'; -import {ByteArray} from './Bytes'; -import {createLoggerFor} from "./Logger"; +import {bncsutil} from './libbncsutil'; +import {ByteArray} from '../Bytes'; +import {createLoggerFor} from "../Logger"; const {debug, info, error} = createLoggerFor('BNCSUtil'); @@ -30,7 +29,7 @@ export class BNCSUtil { static PLATFORM_OSX = 3; static extractMPQNumber(mpqName): string { - return lib.libbncsutil.extractMPQNumber(mpqName); + return bncsutil.extractMPQNumber(mpqName); } static getPlatform(): number { @@ -52,7 +51,7 @@ export class BNCSUtil { static getVersion(): string { let verChar = ref.alloc('string'); - lib.libbncsutil.bncsutil_getVersionString(verChar); + bncsutil.bncsutil_getVersionString(verChar); return verChar.toString(); } @@ -72,7 +71,7 @@ export class BNCSUtil { //MEXP(int) getExeInfo(const char* file_name, char* exe_info, size_t exe_info_size, uint32_t* version, int platform) - let length = lib.libbncsutil.getExeInfo( + let length = bncsutil.getExeInfo( fileName, exeInfo, exeInfo.length, @@ -106,7 +105,7 @@ export class BNCSUtil { //console.log('checkRevisionFlat', arguments); - lib.libbncsutil.checkRevisionFlat( + bncsutil.checkRevisionFlat( valueString, file1, file2, @@ -139,7 +138,7 @@ export class BNCSUtil { let product = ref.alloc('uint32'); let hashBuffer = ref.alloc('string'); - lib.libbncsutil.kd_quick( + bncsutil.kd_quick( CDKey, clientToken, serverToken, @@ -171,7 +170,7 @@ export class BNCSUtil { * MEXP(nls_t*) nls_init_l(const char* username, unsigned long username_length, const char* password, unsigned long password_length) */ - return lib.libbncsutil.nls_init_l(username, username.length, password, password.length) + return bncsutil.nls_init_l(username, username.length, password, password.length) } /** @@ -180,7 +179,7 @@ export class BNCSUtil { static nls_get_A(nls_t): Buffer { let buffer = Buffer.alloc(32); - lib.libbncsutil.nls_get_A(nls_t, buffer); + bncsutil.nls_get_A(nls_t, buffer); return buffer; } @@ -193,7 +192,7 @@ export class BNCSUtil { //MEXP(void) nls_get_M1(nls_t* nls, char* out, const char* B, const char* salt); let buffer = Buffer.alloc(20); - lib.libbncsutil.nls_get_M1(nls_t, buffer, B, salt); + bncsutil.nls_get_M1(nls_t, buffer, B, salt); return buffer; } @@ -204,7 +203,7 @@ export class BNCSUtil { static hashPassword(password): Buffer { let buffer = Buffer.alloc(20); - lib.libbncsutil.hashPassword(password, buffer); + bncsutil.hashPassword(password, buffer); return buffer } diff --git a/src/libbncsutil/ffi-generate.sh b/src/bncsutil/ffi-generate.sh similarity index 100% rename from src/libbncsutil/ffi-generate.sh rename to src/bncsutil/ffi-generate.sh diff --git a/src/libbncsutil/gmp.dylib b/src/bncsutil/gmp.dylib similarity index 100% rename from src/libbncsutil/gmp.dylib rename to src/bncsutil/gmp.dylib diff --git a/src/libbncsutil/libbncsutil.dylib b/src/bncsutil/libbncsutil.dylib similarity index 100% rename from src/libbncsutil/libbncsutil.dylib rename to src/bncsutil/libbncsutil.dylib diff --git a/src/libbncsutil/libbncsutil.so b/src/bncsutil/libbncsutil.so similarity index 100% rename from src/libbncsutil/libbncsutil.so rename to src/bncsutil/libbncsutil.so diff --git a/src/libbncsutil/libbncsutil.js b/src/bncsutil/libbncsutil.ts similarity index 73% rename from src/libbncsutil/libbncsutil.js rename to src/bncsutil/libbncsutil.ts index ee84c0e..7f44140 100644 --- a/src/libbncsutil/libbncsutil.js +++ b/src/bncsutil/libbncsutil.ts @@ -1,10 +1,11 @@ +import { resolveLibraryPath } from "../util"; + const FFI = require('ffi-napi'), - ref = require('ref-napi'), - path = require('path'); + ref = require('ref-napi'); const voidPtr = ref.refType(ref.types.void); -exports.CONSTANTS = { +const CONSTANTS = { '': { P_ALL: 0, P_PID: 1, @@ -15,47 +16,12 @@ exports.CONSTANTS = { }, }; -// var siginfo_t = exports.siginfo_t = voidPtr; -// var siginfo_tPtr = exports.siginfo_tPtr = ref.refType(siginfo_t); -// var div_t = exports.div_t = Struct({ -// quot: ref.types.int32, -// rem: ref.types.int32, -// }); -// var div_tPtr = exports.div_tPtr = ref.refType(div_t); -// var ldiv_t = exports.ldiv_t = Struct({ -// quot: ref.types.long, -// rem: ref.types.long, -// }); -// var ldiv_tPtr = exports.ldiv_tPtr = ref.refType(ldiv_t); -// var lldiv_t = exports.lldiv_t = Struct({ -// quot: ref.types.longlong, -// rem: ref.types.longlong, -// }); -// var lldiv_tPtr = exports.lldiv_tPtr = ref.refType(lldiv_t); -// var wchar_t = exports.wchar_t = Struct({ -// __darwin_wchar_t: ref.types.int32, -// }); -// var wchar_tPtr = exports.wchar_tPtr = ref.refType(wchar_t); const uint32_t = exports.uint32_t = voidPtr; const uint32_tPtr = exports.uint32_tPtr = ref.refType(uint32_t); const nls_t = exports.nls_t = voidPtr; const nls_tPtr = exports.nls_tPtr = ref.refType(nls_t); -const platform = process.platform; -const cwd = process.cwd(); -let libPath = null; - -if (platform === 'win32'){ - libPath = '/libbncsutil.dll'; -}else if(platform === 'linux'){ - libPath = '/libbncsutil.so'; -}else if(platform === 'darwin'){ - libPath = '/libbncsutil.dylib'; -}else{ - throw new Error('unsupported plateform for mathlibLoc'); -} - -exports.libbncsutil = new FFI.Library(path.resolve(cwd + libPath), { +const bncsutil = new FFI.Library(resolveLibraryPath('bncsutil'), { extractMPQNumber: [ref.types.int32, [ ref.types.CString, ]], @@ -86,11 +52,6 @@ exports.libbncsutil = new FFI.Library(path.resolve(cwd + libPath), { ref.types.ulong, ref.types.CString, ]], - // bsha1_hash: [ref.types.void, [ - // ref.types.CString, - // ref.types.uint32, - // ref.types.CString, - // ]], doubleHashPassword: [ref.types.void, [ ref.types.CString, ref.types.uint32, @@ -232,3 +193,4 @@ exports.libbncsutil = new FFI.Library(path.resolve(cwd + libPath), { ]], }); +export { bncsutil } diff --git a/src/bnet/AuthInfo.ts b/src/bnet/AuthInfo.ts index 3f0e285..93df1a2 100644 --- a/src/bnet/AuthInfo.ts +++ b/src/bnet/AuthInfo.ts @@ -1,6 +1,6 @@ import * as assert from "assert"; import {ByteExtractString, ByteExtractUInt32, BytesExtract, ValidateLength} from "../Bytes"; -import {BNCSUtil} from "../BNCSUtil"; +import {BNCSUtil} from "../bncsutil/BNCSUtil"; import {createLoggerFor, hex} from '../Logger'; import {BNetConnection} from "./BNetConnection"; diff --git a/src/bnet/AuthState.ts b/src/bnet/AuthState.ts index 3a4fddd..249d069 100644 --- a/src/bnet/AuthState.ts +++ b/src/bnet/AuthState.ts @@ -3,7 +3,7 @@ import * as assert from "assert"; import {BNetConnection} from "./BNetConnection"; import {createLoggerFor, hex} from '../Logger'; import {BNetKR} from "./BNetKR"; -import {BNCSUtil} from "../BNCSUtil"; +import {BNCSUtil} from "../bncsutil/BNCSUtil"; const {debug, info, error} = createLoggerFor('AuthState'); diff --git a/src/game/Map.ts b/src/game/Map.ts index 550216d..251c367 100644 --- a/src/game/Map.ts +++ b/src/game/Map.ts @@ -7,7 +7,7 @@ import {createLoggerFor} from '../Logger'; import {Config} from "../Config"; import {GHost} from "../GHost"; import {arch} from "os"; -import {MPQ} from "../libStorm"; +import {MPQ} from "../storm"; const {debug, info, error} = createLoggerFor('Map'); diff --git a/src/ghost/bnet/GhostBNetSIDHandler.ts b/src/ghost/bnet/GhostBNetSIDHandler.ts index 5ee4005..9037493 100644 --- a/src/ghost/bnet/GhostBNetSIDHandler.ts +++ b/src/ghost/bnet/GhostBNetSIDHandler.ts @@ -9,7 +9,7 @@ import {IBNetSIDHandler} from "../../bnet/IBNetSIDHandler"; import {BNetConnection} from "../../bnet/BNetConnection"; import {IBNetProtocol} from "../../bnet/IBNetProtocol"; import {createLoggerFor} from "../../Logger"; -import {BNCSUtil} from '../../BNCSUtil'; +import {BNCSUtil} from '../../bncsutil/BNCSUtil'; import {IBNetConnection} from "../../bnet/IBNetConnection"; const {debug, info, error} = createLoggerFor('BNet_SID_Handler'); diff --git a/src/libStorm/chain.ts b/src/storm/chain.ts similarity index 100% rename from src/libStorm/chain.ts rename to src/storm/chain.ts diff --git a/src/libStorm/ffi-generate.sh b/src/storm/ffi-generate.sh similarity index 100% rename from src/libStorm/ffi-generate.sh rename to src/storm/ffi-generate.sh diff --git a/src/libStorm/file.ts b/src/storm/file.ts similarity index 100% rename from src/libStorm/file.ts rename to src/storm/file.ts diff --git a/src/libStorm/files.ts b/src/storm/files.ts similarity index 100% rename from src/libStorm/files.ts rename to src/storm/files.ts diff --git a/src/libStorm/index.ts b/src/storm/index.ts similarity index 100% rename from src/libStorm/index.ts rename to src/storm/index.ts diff --git a/src/libStorm/libStorm.dylib b/src/storm/libStorm.dylib similarity index 100% rename from src/libStorm/libStorm.dylib rename to src/storm/libStorm.dylib diff --git a/src/libStorm/storm-lib.ts b/src/storm/storm-lib.ts similarity index 78% rename from src/libStorm/storm-lib.ts rename to src/storm/storm-lib.ts index d9ff9fe..441986b 100755 --- a/src/libStorm/storm-lib.ts +++ b/src/storm/storm-lib.ts @@ -1,8 +1,8 @@ -const fs = require('fs'); +import {resolveLibraryPath} from "../util"; + const ffi = require('ffi-napi'); const ref = require('ref-napi'); const Struct = require('ref-struct-napi'); -const path = require('path'); const { bool, int32, uint32 } = ref.types; const string = ref.types.CString; @@ -39,29 +39,7 @@ const HANDLEPtr = ref.refType(HANDLE); const LPDWORD = voidPtr; -const platform = process.platform; -const cwd = process.cwd(); -let libPath = null; - -if (platform === 'win32'){ - libPath = '/storm.dll'; -}else if(platform === 'linux'){ - libPath = '/libstorm.so'; -}else if(platform === 'darwin'){ - libPath = '/libstorm.dylib'; -}else{ - throw new Error('unsupported plateform for mathlibLoc'); -} - -let libName = path.resolve(cwd + libPath); - -if (!fs.existsSync(libName)) { - console.error(libName + ' not found, fallback to libstorm'); - - libName = 'libstorm'; -} - -const StormLib = new ffi.Library(libName, { +const StormLib = new ffi.Library(resolveLibraryPath('storm'), { SFileGetLocale: [uint32, []], SFileSetLocale: [uint32, [ diff --git a/src/util.ts b/src/util.ts index 96e7e94..b0824cf 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,3 +1,5 @@ +import * as fs from 'fs'; +import * as path from 'path'; const startTime = Date.now(); @@ -52,3 +54,29 @@ export function ipToBuffer(ip:string) { export function isNameValid(name) { return name.length && name.length <= 15 } + +export function resolveLibraryPath(name: string) { + const platform = process.platform; + const cwd = process.cwd(); + let libPath = null; + + if (platform === 'win32') { + libPath = `${name}.dll`; + } else if (platform === 'linux') { + libPath = `lib${name}.so`; + } else if (platform === 'darwin') { + libPath = `lib${name}.dylib`; + } else { + throw new Error(`unsupported plateform for ${name}`); + } + + let libName = path.resolve(cwd, libPath); + + if (!fs.existsSync(libName)) { + console.error(`${libName} not found, fallback to libstorm`); + + return `lib${name}`; + } + + return libName; +}