Skip to content

Commit

Permalink
Merge pull request #128 from iamtakingiteasy/master
Browse files Browse the repository at this point in the history
Customizable useragent (workaround for #127)
  • Loading branch information
iamapig120 authored Dec 4, 2023
2 parents 6494d6f + 6695620 commit 8b215b9
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 28 deletions.
2 changes: 2 additions & 0 deletions i18n/en-US/manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"useHttpServer": "User http server(avoid certificate error)",
"serverToPlay": "Server to play (0 is China server, 1 is Japan server, 2 is America server)",
"proxyUrl": "Proxy server address (leave it empty if you don't want to use it)",
"userAgent": "User-Agent",
"vanillaMode": "Vanilla mode (no extensions)",
"userLibPath": "'User Library' Path",
"localVersion": "Version",
"isManagerHide": "Re-open the launcher when the game quit",
Expand Down
2 changes: 2 additions & 0 deletions i18n/ja-JP/manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"useHttpServer": "http サービスを使用する(証明書エラーがない)",
"serverToPlay": "ゲームサーバー(0は中国サーバー、1は日本サーバー、2は米国サーバー)",
"proxyUrl": "プロキシサーバーアドレス(プロキシを使用しない場合は空白のままにします)",
"userAgent": "ユーザーエージェント",
"vanillaMode": "バニラモード(エクステンションなし)",
"userLibPath": "ユーザライブラリディレクトリ",
"localVersion": "バージョン",
"isManagerHide": "ゲーム終了後、管理画面に戻る",
Expand Down
2 changes: 2 additions & 0 deletions i18n/ko/manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"useHttpServer": "\uc0ac\uc6a9\uc790 HTTP \uc11c\ubc84(\uc778\uc99d\uc11c \uc624\ub958 \ud68c\ud53c)",
"serverToPlay": "\ud50c\ub808\uc774\ud560 \uc11c\ubc84 (0 : \uc911\uad6d \uc11c\ubc84, 1 : \uc77c\ubcf8 \uc11c\ubc84, 2 : \ubbf8\uad6d \uc11c\ubc84)",
"proxyUrl": "\ud504\ub85d\uc2dc \uc11c\ubc84 \uc8fc\uc18c (\uc0ac\uc6a9\ud558\uc9c0 \uc54a\uc73c\ub824\uba74 \ube48\uce78\uc73c\ub85c \ub458 \uac83)",
"userAgent": "\uc0ac\uc6a9\uc790\u0020\uc5d0\uc774\uc804\ud2b8",
"vanillaMode": "Vanilla mode (no extensions)",
"userLibPath": "'\uc0ac\uc6a9\uc790 \ub77c\uc774\ube0c\ub7ec\ub9ac' \uacbd\ub85c",
"localVersion": "\ubc84\uc804",
"isManagerHide": "\uac8c\uc784 \uc885\ub8cc \ud6c4 \ub7f0\ucc98 \uc7ac\uc2e4\ud589",
Expand Down
2 changes: 2 additions & 0 deletions i18n/zh-CN/manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"useHttpServer": "使用 http 服务器(不存在证书错误)",
"serverToPlay": "游戏服务器(0为国服,1为日服,2为美服)",
"proxyUrl": "代理服务器地址(留空为不使用代理)",
"userAgent": "User-Agent",
"vanillaMode": "纯净模式(停用所有扩展)",
"userLibPath": "用户库目录",
"localVersion": "版本",
"isManagerHide": "退出游戏后回到管理器界面",
Expand Down
2 changes: 2 additions & 0 deletions i18n/zh-TW/manager.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"useHttpServer": "\u4f7f\u7528 http \u670d\u52d9\u5668\uff08\u4e0d\u5b58\u5728\u8b49\u66f8\u932f\u8aa4\uff09",
"serverToPlay": "\u904a\u6232\u4f3a\u670d\u5668\uff080\u70ba\u570b\u4f3a\uff0c1\u70ba\u65e5\u4f3a\uff0c2\u70ba\u7f8e\u4f3a\uff09",
"proxyUrl": "\u4ee3\u7406\u4f3a\u670d\u5668\u5730\u5740\uff08\u7559\u7a7a\u70ba\u4e0d\u4f7f\u7528\u4ee3\u7406\uff09",
"userAgent": "User-Agent",
"vanillaMode": "\u7d14\u6de8\u6a21\u5f0f\uff08\u505c\u7528\u6240\u6709\u64f4\u5c55\uff09",
"userLibPath": "\u8cc7\u6599\u593e\u76ee\u9304",
"localVersion": "\u7248\u672c",
"isManagerHide": "\u9000\u51fa\u904a\u6232\u5f8c\uff0c\u8fd4\u56de\u6a94\u6848\u7e3d\u7ba1",
Expand Down
5 changes: 3 additions & 2 deletions src/bin/main/mainLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ function getLocalUrlWithParams(url: string) {
function redirectGameWindow(url: string, gameWindow: Electron.WebviewTag) {
const localUrl = getLocalUrlWithParams(url)
console.log('[Majsoul Plus] Redirect Target:' + localUrl)

gameWindow.loadURL(localUrl)
}

Expand Down Expand Up @@ -139,8 +140,6 @@ mainWindow.addEventListener('dom-ready', () => {
} else {
scaleWindow(100)
}

mainWindow.useragent = navigator.userAgent
})

ipcRenderer.on(
Expand All @@ -152,6 +151,8 @@ ipcRenderer.on(
if (partition) {
mainWindow.partition = partition
}

mainWindow.useragent = navigator.userAgent
mainWindow.src = url
mainWindowBox.style.width = '100vw'
mainWindowBox.style.height = '100vh'
Expand Down
6 changes: 4 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const defaultWindowConfig: MajsoulPlus.UserWindowConfig = {
renderingMultiple: 100,
isKioskModeOn: false,
isNoBorder: false,
isManagerHide: false
isManagerHide: false,
userAgent: Global.HttpGetUserAgent
}

const defaultUpdateConfig: MajsoulPlus.UserUpdateConfig = {
Expand All @@ -29,7 +30,8 @@ const defaultChromiumConfig: MajsoulPlus.UserChromiumConfig = {

const defaultUserDataConfig: MajsoulPlus.UserDataConfig = {
useHttpServer: false,
serverToPlay: 0
serverToPlay: 0,
vanillaMode: false
}

const defaultConfig: MajsoulPlus.UserConfig = {
Expand Down
27 changes: 20 additions & 7 deletions src/extension/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,26 @@ export default class MajsoulPlusExtensionManager extends BaseManager {
}
}

const processRegion = (data) => UserConfigs.userData.serverToPlay === 0
? Buffer.from(
data
.toString('utf-8')
.replace(/\.\.\/region\/region\.txt/g, 'region.txt')
)
: data;

router.get('/:version/code.js', async (ctx, next) => {
if (UserConfigs.userData.vanillaMode) {
const code = (
await getRemoteOrCachedFile(ctx.request.originalUrl, false, data =>
processRegion(data)
)
).data.toString('utf-8')
ctx.res.setHeader('Content-Type', 'application/javascript')
ctx.body = code;
return;
}

if (this.codejs !== '') {
ctx.res.statusCode = 200
ctx.body = this.codejs
Expand Down Expand Up @@ -265,13 +284,7 @@ function addScript(url) {
const url = ctx.request.originalUrl.replace(/^\/majsoul_plus/, '')
const code = (
await getRemoteOrCachedFile(url, false, data =>
UserConfigs.userData.serverToPlay === 0
? Buffer.from(
data
.toString('utf-8')
.replace(/\.\.\/region\/region\.txt/g, 'region.txt')
)
: data
processRegion(data)
)
).data.toString('utf-8')
ctx.res.setHeader('Content-Type', 'application/javascript')
Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,13 @@ app.on('ready', () => {

// 资源管理器通知启动游戏
ipcMain.on('start-game', () => {
// 加载服务器路由规则
LoadServer()
if (!UserConfigs.userData.vanillaMode) {
// 加载服务器路由规则
LoadServer()

// 启动服务器
ListenServer(Global.ServerPort)
// 启动服务器
ListenServer(Global.ServerPort)
}

if (!process.env.SERVER_ONLY) {
GameWindows.newWindow()
Expand Down
2 changes: 2 additions & 0 deletions src/majsoul_plus.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ declare namespace MajsoulPlus {
isKioskModeOn: boolean
isNoBorder: boolean
isManagerHide: boolean
userAgent: string
}

export interface UserUpdateConfig {
Expand All @@ -28,6 +29,7 @@ declare namespace MajsoulPlus {
export interface UserDataConfig {
useHttpServer: boolean
serverToPlay: number
vanillaMode: boolean
}

export interface UserConfig {
Expand Down
2 changes: 1 addition & 1 deletion src/manager/pages/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Setting {
index
}),
string: () => {
if (item === 'proxyUrl') {
if (item === 'proxyUrl' || item === 'userAgent') {
this.renderTextSectionItem({
settingInner,
section,
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export async function getRemoteSource(
const remoteUrl = getRemoteUrl(url)
const resp = await fetch(remoteUrl, {
headers: {
'User-Agent': Global.HttpGetUserAgent
'User-Agent': UserConfigs.window.userAgent
}
})

Expand Down Expand Up @@ -261,7 +261,7 @@ export async function getRemoteOrCachedFile(
export async function fetchAnySite(url: string, encoding: BufferEncoding = 'binary') {
const resp = await fetch(url, {
headers: {
'User-Agent': Global.HttpGetUserAgent
'User-Agent': UserConfigs.window.userAgent
}
})
return (await resp.buffer()).toString(encoding)
Expand Down
28 changes: 18 additions & 10 deletions src/windows/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ListenServer,
LoadServer
} from '../server'
import { getRemoteUrl } from '../utils';
import { ToolManager } from '../tool/tool'
import { AudioPlayer, initPlayer, shutoffPlayer } from './audioPlayer'
import { ManagerWindow } from './manager'
Expand Down Expand Up @@ -103,15 +104,21 @@ export function newGameWindow(id: number) {
window.setMenu(getGameWindowMenu(id))

window.webContents.on('dom-ready', () => {
// 加载本地服务器地址
const http = UserConfigs.userData.useHttpServer
const port = (UserConfigs.userData.useHttpServer
? (httpServer.address() as AddressInfo)
: (httpsServer.address() as AddressInfo)
).port
const url = `http${
UserConfigs.userData.useHttpServer ? '' : 's'
}://localhost:${port}/`
let url, port, http

if (UserConfigs.userData.vanillaMode) {
url = getRemoteUrl('/')
} else {
// 加载本地服务器地址
http = UserConfigs.userData.useHttpServer
port = (UserConfigs.userData.useHttpServer
? (httpServer.address() as AddressInfo)
: (httpsServer.address() as AddressInfo)
).port
url = `http${
UserConfigs.userData.useHttpServer ? '' : 's'
}://localhost:${port}/`
}

window.webContents.send(
'load-url',
Expand Down Expand Up @@ -160,8 +167,9 @@ export function newGameWindow(id: number) {
window.show()
})


// 设置 GameWindow 的 userAgent
window.webContents.userAgent = Global.HttpGetUserAgent
window.webContents.userAgent = UserConfigs.window.userAgent

// 载入本地启动器
window.loadURL('file://' + path.join(__dirname, '../bin/main/index.html'))
Expand Down

0 comments on commit 8b215b9

Please sign in to comment.