Skip to content

Commit

Permalink
fix live song crash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Jan 21, 2018
1 parent dbc80e3 commit 73f3ed4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 63 deletions.
151 changes: 95 additions & 56 deletions pack.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const unzip = require('unzip')
const rcedit = require('rcedit')
const request = require('request')
const { slog, log, ilog, elog } = require('cmd-rainbow')
const { slog, log, ilog, elog, wlog } = require('cmd-rainbow')
const fs = require('fs')
const path = require('path')
const packageJson = require('./package.json')
Expand All @@ -20,36 +20,77 @@ const APP_PATH = path.join(DIST_PATH, 'resources', 'app')
const IGNORE_REGEXP = new RegExp(`node_modules|data|release|download|dist|src|screenshot|${path.join('public/img/card').replace(/\\/g, '\\\\')}|${path.join('public/asset/sound/live').replace(/\\/g, '\\\\')}|.gitignore|README.md|webpack|.eslintrc.json|config.json|manifest.json|package-lock.json|pack.js|.git`)

if (!fs.existsSync(ELECTRON_RELEASE_DIR)) fs.mkdirSync(ELECTRON_RELEASE_DIR)
downloadElectronRelease()
.then(() => {
if (fs.existsSync(DIST_PATH)) {
wlog(`[WARNING ${t()}] ${DIST_PATH} exists.`)
return remove(DIST_PATH)
}
return []
})
.then(() => unzipElectron())
.then(() => new Promise((resolve, reject) => {
fs.unlinkSync(path.join(DIST_PATH, 'resources', 'default_app.asar'))
fs.rename(path.join(DIST_PATH, 'electron.exe'), path.join(DIST_PATH, `./${packageJson.name}.exe`), err => {
if (err) reject(err)
else resolve()
})
}))
.then(() => changeExeInfo(path.join(DIST_PATH, `${packageJson.name}.exe`), {
icon: path.join(__dirname, './src/res/icon/mishiro.ico'),
'file-version': packageJson.version,
'product-version': packageJson.version,
'version-string': {
'Block Header': '080404b0',
FileDescription: packageJson.description,
InternalName: packageJson.name,
OriginalFilename: packageJson.name + '.exe',
ProductName: packageJson.name,
CompanyName: packageJson.author.name,
LegalCopyright: 'Copyright (C) 2017 Toyobayashi'
}
}))
.then(() => {
ilog(`[INFO ${t()}] EXE file changed.`)
return copy(__dirname, APP_PATH, IGNORE_REGEXP)
})
.then(pathArr => {
ilog(`[INFO ${t()}] Pack done.`)
})
.catch(err => {
elog(`[ERROR ${t()}] ${err}`)
})

function downloadElectronRelease () {
const PROGRESS_LENGTH = 30
if (!fs.existsSync(ELECTRON_RELEASE_PATH)) {
return new Promise((resolve, reject) => {
let cur = 0
let contentLength = 0
const FILE_SIZE = fs.existsSync(ELECTRON_RELEASE_PATH + '.tmp') ? fs.readFileSync(ELECTRON_RELEASE_PATH + '.tmp').length : 0
const options = FILE_SIZE ? { url: ELECTRON_URL, headers: { 'Range': 'bytes=' + FILE_SIZE + '-' } } : { url: ELECTRON_URL }
let options = FILE_SIZE ? { url: ELECTRON_URL, headers: { 'Range': 'bytes=' + FILE_SIZE + '-' } } : { url: ELECTRON_URL }
let req = request(options)
req.on('response', response => {
if (response.statusCode !== 200 && response.statusCode !== 304 && response.statusCode !== 206) {
log(response.statusCode)
req.abort()
reject(new Error('Request failed.'))
} else {
contentLength = Number(response.headers['content-length'])
ilog(`[INFO ${t()}] Downloading ${ELECTRON_NAME}.zip`)
const CONTENT_LENGTH = Number(response.headers['content-length'])
let ws = fs.createWriteStream(ELECTRON_RELEASE_PATH + '.tmp', { flags: 'a+' })
req.on('data', chunk => {
cur += chunk.length
let percent = (FILE_SIZE + cur) / (contentLength + FILE_SIZE)
let l = Math.floor(PROGRESS_LENGTH * percent)
let progress = `[${repeat('=', l - 1)}>${repeat(' ', PROGRESS_LENGTH - l)}] `
slog(`Downloading ${ELECTRON_NAME}.zip\n` + progress + (100 * percent).toFixed(2) + '%')
const PERCENT = (FILE_SIZE + cur) / (CONTENT_LENGTH + FILE_SIZE)
const COMPLETED_LENGTH = Math.floor(PROGRESS_LENGTH * PERCENT)
const PROGRESS_BAR = `[${repeat('=', COMPLETED_LENGTH - 1)}>${repeat(' ', PROGRESS_LENGTH - COMPLETED_LENGTH)}] `
slog(PROGRESS_BAR + (100 * PERCENT).toFixed(2) + '%')
})
req.on('error', err => {
reject(err)
})
ws.on('close', () => {
fs.renameSync(ELECTRON_RELEASE_PATH + '.tmp', ELECTRON_RELEASE_PATH)
ilog('\ndownload complete.')
ilog(`\n[INFO ${t()}] Download completed.`)
resolve()
})
req.pipe(ws)
Expand Down Expand Up @@ -78,7 +119,7 @@ function changeExeInfo (exePath, option) {
return new Promise((resolve, reject) => {
rcedit(exePath, option, err => {
if (err) reject(err)
else resolve('EXE file changed.')
else resolve()
})
})
}
Expand All @@ -100,73 +141,71 @@ function copyFolder (oldPath, newPath, ignore) {
let task = []
const files = fs.readdirSync(oldPath)
if (!fs.existsSync(newPath)) fs.mkdirSync(newPath)
// if (ignore) files = files.filter(name => !ignore.test(name))
for (let i = 0; i < files.length; i++) {
let oldFile = path.join(oldPath, files[i])
let newFile = path.join(newPath, files[i])
if (ignore) {
if (!ignore.test(oldFile)) {
let stats = fs.statSync(oldFile)
if (stats.isFile()) {
task.push(copyFile(oldFile, newFile))
} else {
task.push(copyFolder(oldFile, newFile, ignore))
}
task.push(fs.statSync(oldFile).isFile() ? copyFile(oldFile, newFile) : copyFolder(oldFile, newFile, ignore))
}
} else {
let stats = fs.statSync(oldFile)
if (stats.isFile()) {
task.push(copyFile(oldFile, newFile))
} else {
task.push(copyFolder(oldFile, newFile, ignore))
}
task.push(fs.statSync(oldFile).isFile() ? copyFile(oldFile, newFile) : copyFolder(oldFile, newFile))
}
}
return Promise.all(task)
return task.length ? Promise.all(task) : Promise.resolve([])
}

function removeFile (filePath) {
return new Promise((resolve, reject) => {
fs.unlink(filePath, err => {
if (err) reject(err)
else resolve(filePath)
})
})
}

function removeFolder (folderPath) {
let task = []
const files = fs.readdirSync(folderPath)
if (files.length) {
for (let i = 0; i < files.length; i++) {
let file = path.join(folderPath, files[i])
task.push(fs.statSync(file).isFile() ? removeFile(file) : removeFolder(file))
}
return Promise.all(task).then((arr) => {
fs.rmdirSync(folderPath)
return arr
})
} else {
fs.rmdirSync(folderPath)
return Promise.resolve([])
}
}

function copy (src, tar, ignore) {
return new Promise((resolve, reject) => {
fs.stat(src, (err, stats) => {
if (err) reject(err)
else {
if (stats.isFile()) copyFile(src, tar).then((pathArr) => resolve(pathArr))
else copyFolder(src, tar, ignore).then((pathArr) => resolve(pathArr))
if (stats.isFile()) resolve(copyFile(src, tar))
else resolve(copyFolder(src, tar, ignore))
}
})
})
}

downloadElectronRelease()
.then(() => unzipElectron())
.then(() => new Promise((resolve, reject) => {
fs.unlinkSync(path.join(DIST_PATH, 'resources', 'default_app.asar'))
fs.rename(path.join(DIST_PATH, 'electron.exe'), path.join(DIST_PATH, `./${packageJson.name}.exe`), err => {
function remove (src) {
return new Promise((resolve, reject) => {
fs.stat(src, (err, stats) => {
if (err) reject(err)
else resolve(path.join(DIST_PATH, `${packageJson.name}.exe`))
else {
if (stats.isFile()) resolve(removeFile(src))
else resolve(removeFolder(src))
}
})
}))
.then(exePath => changeExeInfo(exePath, {
icon: path.join(__dirname, './src/res/icon/mishiro.ico'),
'file-version': packageJson.version,
'product-version': packageJson.version,
'version-string': {
'Block Header': '080404b0',
FileDescription: packageJson.description,
InternalName: packageJson.name,
OriginalFilename: packageJson.name + '.exe',
ProductName: packageJson.name,
CompanyName: packageJson.author.name,
LegalCopyright: 'Copyright (C) 2017 Toyobayashi'
}
}))
.then(msg => {
ilog(msg)
return copy(__dirname, APP_PATH, IGNORE_REGEXP)
})
.then(pathArr => {
ilog(`Pack done.`)
})
.catch(err => {
elog(err)
})
}

function t () {
return new Date().toLocaleString()
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mishiro",
"productName": "mishiro",
"companyName": "Toyo",
"version": "1.1.3",
"version": "1.1.4",
"description": "mishiro",
"main": "main.min.js",
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/js/icpMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { configurer } from './config.js'
let config = configurer.getConfig()
let fix = {}
if (!config.latestResVer) {
fix.latestResVer = 10034500
fix.latestResVer = 10034600
}
if (config.language !== 'zh' && config.language !== 'ja') {
fix.language = 'zh'
Expand Down Expand Up @@ -116,12 +116,12 @@ ipcMain.on('readMaster', (event, masterFile) => {
} else {
if (arr.length > 2) {
if (arr[2] === 'another') {
fileName = arr[1] + '_' + arr[2] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n/g, '') + '.mp3'
fileName = arr[1] + '_' + arr[2] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n|\\|\/|<|>|\*|\?|:|"|\|/g, '') + '.mp3'
} else {
fileName = arr[1] + '_' + arr[2] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n/g, '') + '(' + charaData.filter(row => row.chara_id == arr[2])[0].name + ').mp3'
fileName = arr[1] + '_' + arr[2] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n|\\|\/|<|>|\*|\?|:|"|\|/g, '') + '(' + charaData.filter(row => row.chara_id == arr[2])[0].name + ').mp3'
}
} else {
fileName = arr[1] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n/g, '') + '.mp3'
fileName = arr[1] + '-' + musicData.filter(row => row.id == arr[1])[0].name.replace(/\\n|\\|\/|<|>|\*|\?|:|"|\|/g, '') + '.mp3'
}
}
liveManifest[i].fileName = fileName
Expand Down Expand Up @@ -242,8 +242,11 @@ ipcMain.on('acb', (event, acbPath, url = '') => {
exec(`${getPath()}\\bin\\CGSSAudio.exe ${acbPath}`, (err) => {
if (!err) {
if (url) {
if (url.split('/')[url.split('/').length - 2] === 'live') {
fs.renameSync(getPath(`./public/asset/sound/live/${name}.mp3`), getPath(`./public/asset/sound/live/${url.split('/')[url.split('/').length - 1]}`))
let urlArr = url.split('/')
if (urlArr[urlArr.length - 2] === 'live') {
let fileName = urlArr[urlArr.length - 1]
urlArr[urlArr.length - 1] = fileName
fs.renameSync(getPath(`./public/asset/sound/live/${name}.mp3`), getPath(`./public/asset/sound/live/${fileName}`))
event.sender.send('acb', acbPath, url)
} else {
event.sender.send('acb', acbPath, url)
Expand Down

0 comments on commit 73f3ed4

Please sign in to comment.