Skip to content

Commit 96d328d

Browse files
committed
replace css class name with the ones that are exported
1 parent c34aae9 commit 96d328d

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed

generate_readable_css.mjs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import { readFile, writeFile, readdir as readDir, rm, mkdir } from "node:fs/promises";
2+
import { parse, latestEcmaVersion } from "espree";
3+
import { traverse, Syntax } from "estraverse";
4+
import { resolve as pathResolve, basename, dirname, join } from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
7+
const CLASS_SELECTOR_REGEX = /(\.[a-zA-Z0-9_-]+)/g;
8+
9+
const __dirname = dirname(fileURLToPath(import.meta.url));
10+
11+
12+
async function ParseJsFile(file, classNames) {
13+
//TODO remove /c. once it's not needed
14+
if (file.endsWith("licenses.js") || file.includes("/c.")) {
15+
return;
16+
}
17+
18+
try {
19+
const code = await readFile(file);
20+
const ast = parse(code, {
21+
ecmaVersion: latestEcmaVersion,
22+
sourceType: "module",
23+
loc: true,
24+
});
25+
26+
traverse(ast, {
27+
enter: (node) => {
28+
if (
29+
node.type === Syntax.AssignmentExpression &&
30+
node.left.type === Syntax.MemberExpression &&
31+
node.left.property.type === Syntax.Identifier &&
32+
node.left.property.name === "exports" &&
33+
node.right.type === Syntax.ObjectExpression &&
34+
node.right.properties.constructor === Array
35+
) {
36+
for (const array_node of node.right.properties) {
37+
if (array_node.value.type !== Syntax.Literal || isProbablyNumeric(String(array_node.value.value))) {
38+
continue;
39+
}
40+
const value = array_node.key.name ?? array_node.key.value;
41+
if (!Object.hasOwn(classNames, array_node.value.value)) {
42+
classNames[array_node.value.value] = value;
43+
} else {
44+
if (!classNames[array_node.value.value]?.includes(value)) {
45+
// this makes invalid css but we don't have to care
46+
classNames[array_node.value.value] = `${classNames[array_node.value.value]}|${value}`;
47+
}
48+
}
49+
}
50+
}
51+
},
52+
});
53+
} catch (e) {
54+
console.error(`Unable to parse "${file}":`, e);
55+
}
56+
}
57+
58+
async function FixCssFile(path, classNames) {
59+
try {
60+
const css = (await readFile(path)).toString();
61+
let output = css;
62+
const matches = [...new Set(css.match(CLASS_SELECTOR_REGEX))];
63+
if (matches) {
64+
for (const match of matches) {
65+
if (Object.hasOwn(classNames, match.slice(1))) {
66+
output = output.replaceAll(match, `.${classNames[match.slice(1)]}`);
67+
}
68+
}
69+
}
70+
const outputPath = path.includes("/ClientExtracted/") ? join(dirname(path), "/c.", basename(path)) : join(__dirname, "/c/", path.replace(__dirname, ""));
71+
if (output !== css) {
72+
await mkdir(dirname(outputPath), { recursive: true });
73+
await writeFile(outputPath, output);
74+
}
75+
} catch (e) {
76+
console.error(`Unable to fix "${path}":`, e);
77+
}
78+
}
79+
80+
async function* GetRecursiveFiles(dir, ext) {
81+
const dirents = await readDir(dir, { withFileTypes: true });
82+
for (const dirent of dirents) {
83+
const res = pathResolve(dir, dirent.name);
84+
if (dirent.isDirectory()) {
85+
yield* GetRecursiveFiles(res, ext);
86+
} else if (dirent.isFile() && dirent.name.endsWith(ext)) {
87+
yield res;
88+
}
89+
}
90+
}
91+
92+
function isProbablyNumeric(string, nbrRecursiceSlice) {
93+
if (!Number.isNaN(Number(string))) {
94+
return true;
95+
}
96+
let testStr = string;
97+
for (let i = 0; i < 2 && testStr.length > 0; i++) {
98+
testStr = testStr.slice(0, -1);
99+
if (!Number.isNaN(Number(testStr))) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
106+
async function cleanPreviousRun(cssPath) {
107+
try {
108+
const rmPath = join(__dirname, "/c/", cssPath, "/**.css");
109+
await rm(rmPath, { force: true });
110+
} catch (e) {
111+
if (e.code !== 'ENOENT') {
112+
console.error(`Unable to clean "${cssPath}":`, e);
113+
}
114+
}
115+
}
116+
117+
let paths;
118+
if (process.argv.length > 2) {
119+
paths = [process.argv[2]];
120+
}
121+
else {
122+
paths = [
123+
"./Scripts/WebUI/",
124+
"./ClientExtracted/",
125+
"./help.steampowered.com/",
126+
"./partner.steamgames.com/",
127+
"./steamcommunity.com/",
128+
"./store.steampowered.com/",
129+
"./www.dota2.com/",
130+
"./www.underlords.com/",
131+
"./www.counter-strike.net/",
132+
];
133+
}
134+
135+
136+
for (const path of paths) {
137+
const classNames = {};
138+
139+
cleanPreviousRun(path);
140+
141+
for await (const file of GetRecursiveFiles(path, ".js")) {
142+
await ParseJsFile(file, classNames);
143+
}
144+
145+
for await (const file of GetRecursiveFiles(path, ".css")) {
146+
await FixCssFile(file, classNames);
147+
}
148+
}

update.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SteamTracker
3030
private bool $ExtractClientArchives = false;
3131
private bool $SyncProtobufs = false;
3232
private bool $DumpJavascriptFiles = false;
33+
private bool $FixCssFiles = false;
3334
private bool $UpdateManifestUrls = false;
3435
private bool $UpdateSSRUrls = false;
3536

@@ -181,6 +182,7 @@ public function __construct( string $Option )
181182
{
182183
$this->Log( '{lightblue}Extracting client archives' );
183184
$this->DumpJavascriptFiles = true;
185+
$this->FixCssFiles = true;
184186
$this->SyncProtobufs = true;
185187

186188
system( 'bash extract_client.sh' );
@@ -196,6 +198,13 @@ public function __construct( string $Option )
196198
system( 'node dump_javascript_urls.mjs' );
197199
}
198200

201+
if( $this->FixCssFiles )
202+
{
203+
$this->Log( '{lightblue}Fixing CSS files' );
204+
205+
system( 'node generate_readable_css.mjs' );
206+
}
207+
199208
if( $this->SyncProtobufs )
200209
{
201210
$this->Log( '{lightblue}Syncing protobufs' );
@@ -604,6 +613,11 @@ private function HandleResponse( string $File, string $Data, string $URL ) : boo
604613

605614
system( 'npm run prettier ' . escapeshellarg( $File ) );
606615

616+
if( str_ends_with( $File, '.css' ) || str_ends_with( $File, '.js' ) )
617+
{
618+
$this->FixCssFiles = true;
619+
}
620+
607621
return true;
608622
}
609623

0 commit comments

Comments
 (0)