Skip to content

Commit

Permalink
Merge pull request #122 from cangzhang/change/latest-version
Browse files Browse the repository at this point in the history
  • Loading branch information
cangzhang authored Nov 30, 2021
2 parents 9708399 + 1b3d936 commit 263ac7a
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 35 deletions.
1 change: 1 addition & 0 deletions app/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export const appConfig = new Store({
},
sourceList: DefaultSourceList,
lolDirHasCJKChar: false,
alwaysRequestLatestVersion: false,
},
});
12 changes: 7 additions & 5 deletions app/utils/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ export const saveToFile = async (
const appendGameToDir = appConfig.get(`appendGameToDir`);
const hasCjkChar = appConfig.get(`lolDirHasCJKChar`);

const file = `${appendGameToDir ? `${desDir}/Game` : desDir}/Config/Champions/${
data.champion
}/Recommended/${data.fileName}.json`;
const tencentFile = `${desDir}/Game/Config/Champions/${data.champion}/Recommended/${data.fileName}.json`;
const riotFile = `${desDir}/Config/Champions/${data.champion}/Recommended/${data.fileName}.json`;
const content = stripProps ? _pick(data, ItemSetProps) : data;
await fse.outputFile(file, JSON.stringify(content, null, 4));
await Promise.all([
fse.outputFile(tencentFile, JSON.stringify(content, null, 4)),
fse.outputFile(riotFile, JSON.stringify(content, null, 4)),
]);

if (appendGameToDir && hasCjkChar) {
const cnFile = utf8ToGb18030(file);
const cnFile = utf8ToGb18030(tencentFile);
await fse.outputFile(cnFile, JSON.stringify(content, null, 4));
}

Expand Down
71 changes: 58 additions & 13 deletions app/utils/lcu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,49 @@ interface IEventBus {
listeners: IBusListener[];
}

const getAuthFromPs = async (): Promise<ILcuAuth | null> => {
try {
const stdout = await execCmd(
`Get-CimInstance Win32_Process -Filter "name = 'LeagueClientUx.exe'" | Select CommandLine | ConvertTo-Json`,
true,
);
const cmdLine = (JSON.parse(stdout) ?? {}).CommandLine ?? ``;
const port = cmdLine.split('--app-port=')[1]?.split('"')[0] ?? ``;
const token = cmdLine.split('--remoting-auth-token=')[1]?.split('"')[0] ?? ``;
const urlWithAuth = `https://riot:${token}@127.0.0.1:${port}`;

return {
port,
token,
urlWithAuth,
};
} catch (err) {
console.error(`[ps] `, err);
return null;
}
};

const getAuthFromCmd = async (): Promise<ILcuAuth | null> => {
try {
const cmdLine = await execCmd(
`wmic PROCESS WHERE name='LeagueClientUx.exe' GET commandline`,
false,
);
const port = cmdLine.split('--app-port=')[1]?.split('"')[0] ?? ``;
const token = cmdLine.split('--remoting-auth-token=')[1]?.split('"')[0] ?? ``;
const urlWithAuth = `https://riot:${token}@127.0.0.1:${port}`;

return {
port,
token,
urlWithAuth,
};
} catch (err) {
console.error(`[cmd] `, err);
return null;
}
};

export class LcuWatcher {
private evBus: IEventBus | null = null;
private request!: Got;
Expand All @@ -96,10 +139,16 @@ export class LcuWatcher {
}

public startAuthTask = () => {
clearInterval(this.getAuthTask!);
clearTimeout(this.getAuthTask!);

this.getAuthTask = setInterval(() => {
this.getAuthFromCmd();
this.getAuthTask = setTimeout(async () => {
try {
await this.getAuthFromCmd();
} catch (e) {
console.error(`[watcher] [getAuthTask]`, e);
} finally {
this.startAuthTask();
}
}, 2000);
};

Expand All @@ -119,23 +168,19 @@ export class LcuWatcher {

public getAuthFromCmd = async () => {
try {
const stdout = await execCmd(
`Get-CimInstance Win32_Process -Filter "name = 'LeagueClientUx.exe'" | Select CommandLine | ConvertTo-Json`,
true,
);
const cmdLine = (JSON.parse(stdout) ?? {}).CommandLine ?? ``;
const appPort = cmdLine.split('--app-port=')[1]?.split('"')[0] ?? ``;
const remotingAuthToken = cmdLine.split('--remoting-auth-token=')[1]?.split('"')[0] ?? ``;
let lcuURL = `https://riot:${remotingAuthToken}@127.0.0.1:${appPort}`;
const cmdRet = await Promise.all([getAuthFromPs(), getAuthFromCmd()]);
const { port: appPort, token: remotingAuthToken, urlWithAuth: lcuURL } =
cmdRet.filter(Boolean)[0] ?? {};

if (appPort && remotingAuthToken) {
if (lcuURL !== this.lcuURL) {
this.lcuURL = lcuURL;
this.lcuURL = lcuURL ?? ``;
console.info(this.lcuURL);
}

clearInterval(this.getAuthTask!);
clearTimeout(this.getAuthTask!);
clearInterval(this.checkLcuStatusTask!);

this.request = got.extend({
prefixUrl: this.lcuURL,
});
Expand Down
16 changes: 11 additions & 5 deletions src/modules/home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,17 @@ export default function Home({ onDirChange = _noop }: IProps) {
LolQQ.getLolVersion().then((v) => {
dispatch(updateDataSourceVersion(sourceList[0].label, v));
}),
...instances.current.map((i) =>
i.getPkgInfo().then(({ sourceVersion }) => {
dispatch(updateDataSourceVersion(i.pkgName, sourceVersion));
}),
),
...instances.current.map((i) => {
if (window.bridge.appConfig.get(`alwaysRequestLatestVersion`)) {
return i.getPkgInfo().then(({ sourceVersion }) => {
dispatch(updateDataSourceVersion(i.pkgName, sourceVersion));
});
}

return i.getPkgInfoFromJsdelivr().then((ver) => {
dispatch(updateDataSourceVersion(i.pkgName, ver));
});
}),
]),
[dispatch, sourceList],
);
Expand Down
9 changes: 4 additions & 5 deletions src/modules/home/useSourceList.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useEffect, useRef, useState } from 'react';
import http from 'src/service/http';
import { SourceQQ, ISourceItem, DefaultSourceList } from 'src/share/constants/sources';
import { NPM_MIRROR, CDN_PREFIX } from 'src/service/data-source/cdn-service';

const CHECK_INTV = 5 * 60 * 1000;
const VersionUrl = `https://registry.npm.taobao.org/@champ-r/source-list`;
const DevVersionUrl = `https://registry.npm.taobao.org/@champ-r/source-list.dev`;
const VersionUrl = `${NPM_MIRROR}/@champ-r/source-list`;
const DevVersionUrl = `${NPM_MIRROR}/@champ-r/source-list.dev`;
const getLatestList = (version: string, isDev: boolean) =>
`https://cdn.jsdelivr.net/npm/@champ-r/source-list${
isDev ? '.dev' : ''
}@${version}/index.json?_${Date.now()}`;
`${CDN_PREFIX}/source-list${isDev ? '.dev' : ''}@${version}/index.json?_${Date.now()}`;

const ENABLED_TEST_CHANNEL = Boolean(process.env.IS_DEV || process.env.ENABLED_TEST_CHANNEL);

Expand Down
8 changes: 5 additions & 3 deletions src/modules/import/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ export default function Import() {
dispatch(updateFetchingSource(selectedSources));

if (!keepOld) {
await window.bridge.file.removeFolderContent(`${lolDir}/Game/Config/Champions`).then(() => {
toaster.positive(t(`removed outdated items`), {});
});
await Promise.all([
window.bridge.file.removeFolderContent(`${lolDir}/Game/Config/Champions`),
window.bridge.file.removeFolderContent(`${lolDir}/Config/Champions`),
]);
toaster.positive(t(`removed outdated items`), {});
}

const { itemMap } = store;
Expand Down
16 changes: 14 additions & 2 deletions src/service/data-source/cdn-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
IChampionInfo,
} from '@interfaces/commonTypes';

const CDN_PREFIX = `https://cdn.jsdelivr.net/npm/@champ-r`;
const T_NPM_PREFIX = `https://registry.npm.taobao.org/@champ-r`;
export const CDN_PREFIX = `https://cdn.jsdelivr.net/npm/@champ-r`;
export const T_NPM_PREFIX = `https://registry.npmmirror.com/@champ-r`;
// export const NPM_MIRROR = `https://registry.npm.taobao.org`
export const NPM_MIRROR = `https://registry.npmmirror.com`;

const Stages = {
FETCH_CHAMPION_LIST: `FETCH_CHAMPION_LIST`,
Expand Down Expand Up @@ -50,6 +52,16 @@ export default class CdnService extends SourceProto {

public getPkgInfo = () => SourceProto.getPkgInfo(this.tNpmUrl, this.cdnUrl);

public getPkgInfoFromJsdelivr = async () => {
try {
const info: any = await http.get(`${this.cdnUrl}@latest/package.json`);
return info.sourceVersion;
} catch (err) {
console.error(err);
return Promise.resolve(`latest`);
}
};

public getChampionList = async () => {
try {
const version = await fetchLatestVersionFromCdn(this.tNpmUrl);
Expand Down
4 changes: 2 additions & 2 deletions src/service/data-source/source-proto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ICachedReq<T> {
subscribers: { resolve: (r: T) => void; reject: (reason?: any) => void }[];
}

const fetchLatestVersionFromCdn_ = () => {
const fetchLatestVersionFromMirror = () => {
let requestIdx = 0;
const versionReq: { [key: string]: ICachedReq<string> } = {};

Expand Down Expand Up @@ -50,7 +50,7 @@ const fetchLatestVersionFromCdn_ = () => {
};
};

export const fetchLatestVersionFromCdn = fetchLatestVersionFromCdn_();
export const fetchLatestVersionFromCdn = fetchLatestVersionFromMirror();

export default class SourceProto {
public cancelHandlers: { [key: string]: IVoidFunc } = {};
Expand Down

0 comments on commit 263ac7a

Please sign in to comment.