Skip to content

Commit

Permalink
use @tybys/cgen
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jan 12, 2021
1 parent c8aabec commit 2186e32
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 17 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
/test
/wasm
/examples
/src/util/wz.js
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ package-lock.json
/test/**/*.mp3
/Sound
/src/util/wz.js
/wasm/.cgenbuild
/wasm/CMakeLists.txt
11 changes: 6 additions & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ shift
done

cd ./wasm
cmakeoutdir="./cmake_build"
cmakeoutdir="./.cgenbuild"
mkdir -p $cmakeoutdir
cd $cmakeoutdir
cmake -DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" -DCMAKE_BUILD_TYPE="$type" -G "Unix Makefiles" ..
cmake --build .
cd ..
# cd $cmakeoutdir
# cmake -DCMAKE_TOOLCHAIN_FILE="$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake" -DCMAKE_BUILD_TYPE="$type" -G "Unix Makefiles" ..
# cmake --build .
# cd ..
if [ "$type" == "Debug" ]; then npx cgen rebuild -e --debug; else npx cgen rebuild -e; fi

exename="wz"

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tybys/wz",
"version": "1.1.1",
"version": "1.1.2",
"description": "MapleStory wz reader.",
"typings": "./lib/cjs-modern/index.d.ts",
"module": "./lib/esm-modern/index.js",
Expand Down Expand Up @@ -37,6 +37,7 @@
"access": "public"
},
"devDependencies": {
"@tybys/cgen": "^0.3.2",
"@tybys/tsgo": "^0.8.0",
"@types/crypto-js": "^4.0.1",
"@types/node": "^12.12.36",
Expand Down
5 changes: 2 additions & 3 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { init as initWasm } from './util/wz'

/**
* @public
*/
export async function init (): Promise<void> {
if (typeof window !== 'undefined') {
await initWasm()
const wzWasm = await import('./util/wz')
await wzWasm.default()
} else {
await Promise.resolve()
}
Expand Down
4 changes: 2 additions & 2 deletions src/properties/WzPngProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { ErrorLevel, ErrorLogger } from '../util/ErrorLogger'
import { NotImplementedError } from '../util/NotImplementedError'
import { _Buffer, zlib } from '../util/node'
import { Canvas } from '../util/Canvas'
import { init } from '../util/wz'

/**
* @public
Expand Down Expand Up @@ -486,7 +485,8 @@ function inflate (data: Uint8Array, len: number): Promise<Uint8Array> {
}

async function inflateWasm (data: Uint8Array, len: number): Promise<Uint8Array> {
const mod = await init()
const wzWasm = await import('../util/wz')
const mod = await wzWasm.default()
const buf = mod.inflate(data, len)
return buf
}
Expand Down
4 changes: 3 additions & 1 deletion src/util/wz.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export declare namespace mod {
export function aesEnc (data: Uint8Array, key: Uint8Array): Uint8Array
}

export declare function init (): Promise<typeof mod>
declare function init (): Promise<typeof mod>

export default init
File renamed without changes.
50 changes: 50 additions & 0 deletions wasm/cgen.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { defineFunctionConfig } = require('@tybys/cgen')

module.exports = defineFunctionConfig(function (_options, { isDebug }) {

const debugFlags = [
'-sDISABLE_EXCEPTION_CATCHING=0',
'-sSAFE_HEAP=1'
]

const commonFlags = [
'--bind',
'-sALLOW_MEMORY_GROWTH=1',
// '-sDISABLE_EXCEPTION_CATCHING=0',
...(isDebug ? debugFlags : [])
]

return {
project: 'wz',
dependencies: {
'./deps/zlib': {}
},
targets: [
{
name: 'wz',
type: 'exe',
sources: [
'./src/**/*.cpp',
'./src/**/*.c'
],
defines: [
'AES256=1',
'ECB=1'
],
wrapScript: '',
compileOptions: [
...commonFlags
],
linkOptions: [
...commonFlags
],
includePaths: [
'./deps/zlib'
],
libs: [
"zlibstatic"
]
}
]
}
})
15 changes: 10 additions & 5 deletions wasm/cmakebuild.bat
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ if "%mode%" == "" set mode=Release

echo %mode%

set cmakeoutdir=cmake_build
set cmakeoutdir=.cgenbuild

if not exist %cmakeoutdir% mkdir %cmakeoutdir%
cd %cmakeoutdir%
cmake -DCMAKE_TOOLCHAIN_FILE=%EMSDK%\upstream\emscripten\cmake\Modules\Platform\Emscripten.cmake -DCMAKE_BUILD_TYPE=%mode% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=make ..
cmake --build .
cd ..
if "%mode%" == "Debug" (
call npx.cmd cgen rebuild -e --debug
) else (
call npx.cmd cgen rebuild -e
)
@REM cd %cmakeoutdir%
@REM cmake -DCMAKE_TOOLCHAIN_FILE=%EMSDK%\upstream\emscripten\cmake\Modules\Platform\Emscripten.cmake -DCMAKE_BUILD_TYPE=%mode% -G "MinGW Makefiles" -DCMAKE_MAKE_PROGRAM=make ..
@REM cmake --build .
@REM cd ..

set exename=wz

Expand Down

0 comments on commit 2186e32

Please sign in to comment.