Skip to content

Commit

Permalink
Add s2ts version to compiled code for debuging
Browse files Browse the repository at this point in the history
  • Loading branch information
Peterclark1996 committed Aug 1, 2024
1 parent 08486b7 commit 3604d37
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/create-s2ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-s2ts",
"version": "0.2.1",
"version": "0.2.2",
"description": "A tool to scaffold a s2ts project",
"main": "dist/index.js",
"bin": {
Expand Down
4 changes: 2 additions & 2 deletions packages/s2ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "s2ts",
"version": "0.2.1",
"version": "0.2.2",
"description": "A tool to automatically compile counter-strike TS files (.vts files)",
"main": "dist/index.js",
"bin": {
Expand All @@ -22,7 +22,7 @@
"compiler"
],
"scripts": {
"build": "tsc --project tsconfig.build.json",
"build": "tsc --project tsconfig.build.json && ts-node ./src/postBuild.ts",
"start": "ts-node --transpile-only --project tsconfig.json src/cli.ts start",
"test": "jest",
"publish-npm": "npm run build && npm publish"
Expand Down
12 changes: 8 additions & 4 deletions packages/s2ts/src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ModuleKind, ScriptTarget, transpileModule } from "typescript"

const version: number = 8
import { s2tsVersion } from "."

export const compileVtsFile = (data: string): Buffer => {
const transpiledData = transpileTypeScript(data)
return compileToVtsc(transpiledData)
const transpiledDataWithVersion = addS2tsVersion(transpiledData)
return compileToVtsc(transpiledDataWithVersion)
}

const transpileTypeScript = (source: string): string => {
Expand All @@ -20,6 +20,10 @@ const transpileTypeScript = (source: string): string => {
return result.outputText
}

const addS2tsVersion = (data: string): string => {
return `// s2ts v${s2tsVersion}\n${data}`
}

const compileToVtsc = (data: string): Buffer => {
const dataSize = Buffer.byteLength(data, "utf-8")
const newData: number[] = []
Expand All @@ -28,7 +32,7 @@ const compileToVtsc = (data: string): Buffer => {

newData.push(...intToBytes(fileSize))
newData.push(...intToBytes(131084)) // unknown constant
newData.push(...intToBytes(version))
newData.push(...intToBytes(8)) // version
newData.push(...intToBytes(3)) // unknown constant
newData.push(...Array.from(Buffer.from("RED2", "ascii")))
newData.push(...intToBytes(0)) // offset
Expand Down
2 changes: 2 additions & 0 deletions packages/s2ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import path from "path"
import { compileVtsFile } from "./compile"

export const s2tsVersion = process.env.npm_package_version

const sourcePathPart = "/content/csgo_addons"
const targetPathPart = "/game/csgo_addons"

Expand Down
12 changes: 12 additions & 0 deletions packages/s2ts/src/postBuild.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { existsSync, readFileSync, unlinkSync, writeFileSync } from "fs"
import path from "path"

const indexFilePath = path.join(process.cwd(), "dist/index.js")
const fileContent = readFileSync(indexFilePath, "utf8")
const fileContentWithVersion = fileContent.replace("process.env.npm_package_version", `"${process.env.npm_package_version}"`)
writeFileSync(indexFilePath, fileContentWithVersion)

const fileToRemovePath = path.join(process.cwd(), "dist/postBuild.js")
if (existsSync(fileToRemovePath)) {
unlinkSync(fileToRemovePath)
}
9 changes: 9 additions & 0 deletions packages/s2ts/tests/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,12 @@ test("I can compile a vts typescript file and get a correctly formed vts_c javas

expect(actual).toBe(expected)
})

test("I can compile a vts file and see the s2ts version in the header of the vts_c file", async () => {
const sourcePath = path.join(__dirname, "/resource/test.vts")
const data = readFileSync(sourcePath).toString("utf-8")

const actual = compileVtsFile(data).toString("utf-8")

expect(actual).toContain(`// s2ts v${process.env.npm_package_version}`)
})
Binary file modified packages/s2ts/tests/resource/test.vts_c
Binary file not shown.
Binary file modified packages/s2ts/tests/resource/test_withTypes.vts_c
Binary file not shown.

0 comments on commit 3604d37

Please sign in to comment.