Skip to content

Commit 41cce21

Browse files
committed
Preparing for release with more logging and more safety checks
1 parent 9660263 commit 41cce21

File tree

8 files changed

+50
-37
lines changed

8 files changed

+50
-37
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ VSCode-MC-Shader is a [Visual Studio Code](https://code.visualstudio.com/) exten
1212

1313
## Features
1414

15-
- Linting (unpolished)
15+
- Linting
1616
- Syntax highlighting (by extension dependency)
17-
- Support for `#includes` directives
17+
- Support for `#include` directives
1818
- Auto-complete prompts (incomplete)
1919

2020
## Planned
@@ -37,7 +37,6 @@ Got a feature request? Chuck it into an Issue!
3737
| Option Name | Data Type | Description | Default Value |
3838
| ----------- | --------- | ----------- | ------------- |
3939
| `mcglsl.glslangValidatorPath` | string | The path to the glslangValidator executable. | In your `PATH`.|
40-
| `mcglsl.lintOnType` | bool | Whether or not to lint while typing. Can decrease performance. | `false` |
4140
| `mcglsl.shaderpacksPath` | string | The path to the shaderpacks folder in your Minecraft installation folder. | None |
4241

4342
## Contributing

package.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "vscode-mc-shader",
3-
"displayName": "vscode-mc-shader",
3+
"displayName": "Minecraft GLSL Shader",
44
"description": "A Visual Studio Code extension for linting/etc Minecraft GLSL Shaders",
5-
"version": "0.0.1",
6-
"publisher": "Noah Santschi-Cooney (Strum355)",
5+
"version": "0.9.0",
6+
"publisher": "Strum355",
77
"author": "Noah Santschi-Cooney (Strum355)",
88
"license": "MIT",
99
"repository": {
@@ -50,11 +50,6 @@
5050
"default": "glslangValidator",
5151
"description": "The path to the glslangValidator executable. Default value assumes its in your PATH."
5252
},
53-
"mcglsl.lintOnType": {
54-
"type": "boolean",
55-
"default": false,
56-
"description": "Whether or not to lint while typing. May decrease performance."
57-
},
5853
"mcglsl.shaderpacksPath": {
5954
"type": "string",
6055
"default": "",

server/src/config.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { connection, documents, onEvent } from './server'
22
import fetch from 'node-fetch'
33
import { platform } from 'os'
4-
import { createWriteStream, chmodSync, createReadStream, unlinkSync } from 'fs'
4+
import { createWriteStream, chmodSync, createReadStream, unlinkSync, read } from 'fs'
55
import * as unzip from 'unzip'
66
import { postError } from './utils'
77
import { execSync } from 'child_process'
8+
import { serverLog } from './logging'
9+
import { dirname } from 'path'
810

911
const url = {
1012
'win32': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-windows-x64-Release.zip',
1113
'linux': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-linux-Release.zip',
1214
'darwin': 'https://github.com/KhronosGroup/glslang/releases/download/master-tot/glslang-master-osx-Release.zip'
1315
}
1416

17+
export let glslangReady = false
18+
1519
export interface Config {
1620
readonly shaderpacksPath: string
1721
readonly glslangPath: string
@@ -21,17 +25,28 @@ export let conf: Config = {shaderpacksPath: '', glslangPath: ''}
2125

2226
export const onConfigChange = async (change) => {
2327
const temp = change.settings.mcglsl as Config
28+
if (temp.shaderpacksPath === conf.shaderpacksPath && temp.glslangPath === conf.glslangPath) return
2429
conf = {shaderpacksPath: temp['shaderpacksPath'].replace(/\\/g, '/'), glslangPath: temp['glslangValidatorPath'].replace(/\\/g, '/')}
30+
serverLog.debug(() => 'new config: ' + JSON.stringify(temp))
31+
serverLog.debug(() => 'old config: ' + JSON.stringify(conf))
32+
33+
if (conf.shaderpacksPath === '' || conf.shaderpacksPath.replace(dirname(conf.shaderpacksPath), '') !== '/shaderpacks') {
34+
serverLog.error(() => 'shaderpack path not set or doesn\'t end in \'shaderpacks\'', null)
35+
connection.window.showErrorMessage('mcglsl.shaderpacksPath is not set or doesn\'t end in \'shaderpacks\'. Please set it in your settings.')
36+
return
37+
}
2538

2639
try {
2740
if (!execSync(conf.glslangPath).toString().startsWith('Usage')) {
2841
documents.all().forEach(onEvent)
42+
glslangReady = true
2943
} else {
3044
promptDownloadGlslang()
3145
}
3246
} catch (e) {
3347
if ((e.stdout.toString() as string).startsWith('Usage')) {
3448
documents.all().forEach(onEvent)
49+
glslangReady = true
3550
} else {
3651
promptDownloadGlslang()
3752
}
@@ -47,11 +62,6 @@ async function promptDownloadGlslang() {
4762

4863
if (!chosen || chosen.title !== 'Download') return
4964

50-
if (conf.shaderpacksPath === '') {
51-
connection.window.showErrorMessage('Please set mcglsl.shaderpacksPath as this is where glslangValidator will be saved to.')
52-
return
53-
}
54-
5565
downloadGlslang()
5666
}
5767

@@ -76,7 +86,8 @@ async function downloadGlslang() {
7686
chmodSync(conf.shaderpacksPath + '/glslangValidator', 0o775)
7787
unlinkSync(conf.shaderpacksPath + '/glslangValidator.zip')
7888
connection.sendNotification('update-config', conf.shaderpacksPath + '/glslangValidator')
79-
connection.window.showInformationMessage('glslangValidator has been downloaded to ' + conf.shaderpacksPath + '/glslangValidator')
89+
connection.window.showInformationMessage('glslangValidator has been downloaded to ' + conf.shaderpacksPath + '/glslangValidator. Your config should be updated automatically.')
90+
glslangReady = true
8091
})
8192
})
8293
} catch (e) {

server/src/graph.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// can you imagine that some people out there would import a whole library just for this?
2+
export type Pair<T, S> = {
3+
first: T,
4+
second: S
5+
}
6+
17
type Node = {
28
parents: Map<string, Pair<number, Node>>
39
children: Map<string, Node>
@@ -10,28 +16,26 @@ export class Graph {
1016
return this.nodes.has(uri) ? this.nodes.get(uri).parents.size > 0 : false
1117
}
1218

19+
public get(uri: string): Node {
20+
if (!this.nodes.has(uri)) this.nodes.set(uri, {parents: new Map(), children: new Map()})
21+
return this.nodes.get(uri)
22+
}
23+
1324
public setParent(uri: string, parent: string, lineNum: number) {
1425
const par: Node = this.nodes.has(parent) ? this.nodes.get(parent) : {parents: new Map(), children: new Map()}
1526
if (this.nodes.has(uri)) {
1627
const node = this.nodes.get(uri)
1728
node.parents.set(parent, {first: lineNum, second: par})
1829
par.children.set(uri, node)
1930
} else {
20-
const node: Node = {parents: new Map([par].map(p => [parent, {first: lineNum, second: p}]) as [string, Pair<number, Node>][]), children: new Map()}
31+
const node: Node = {
32+
parents: new Map([par].map(p => [parent, {first: lineNum, second: p}]) as [string, Pair<number, Node>][]),
33+
children: new Map()
34+
}
35+
2136
par.children.set(uri, node)
2237
this.nodes.set(uri, node)
2338
}
2439
this.nodes.set(parent, par)
2540
}
26-
27-
public get(uri: string): Node {
28-
if (!this.nodes.has(uri)) this.nodes.set(uri, {parents: new Map(), children: new Map()})
29-
return this.nodes.get(uri)
30-
}
31-
}
32-
33-
// can you imagine that some people out there would import a whole library just for this?
34-
export type Pair<T, S> = {
35-
first: T,
36-
second: S
3741
}

server/src/linter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function processIncludes(lines: string[], incStack: string[], allIncludes: Set<I
136136
}
137137
}
138138

139-
export function getIncludes(uri: string, lines: string[]) {
139+
function getIncludes(uri: string, lines: string[]) {
140140
// the numbers start at -1 because we increment them as soon as we enter the loop so that we
141141
// dont have to put an incrememnt at each return
142142
const lineInfo: LinesProcessingInfo = {
@@ -312,7 +312,7 @@ function propogateDiagnostic(error: ErrorMatch, diagnostics: Map<string, Diagnos
312312
})
313313
}
314314

315-
export const replaceWords = (msg: string) => Array.from(tokens.entries()).reduce((acc, [key, value]) => acc.replace(key, value), msg)
315+
const replaceWords = (msg: string) => Array.from(tokens.entries()).reduce((acc, [key, value]) => acc.replace(key, value), msg)
316316

317317
const errorType = (error: string) => error === 'ERROR' ? DiagnosticSeverity.Error : DiagnosticSeverity.Warning
318318

@@ -333,7 +333,7 @@ function calcRange(lineNum: number, uri: string): Range {
333333
return Range.create(lineNum, startOfLine, lineNum, endOfLine)
334334
}
335335

336-
export function absPath(currFile: string, includeFile: string): string {
336+
function absPath(currFile: string, includeFile: string): string {
337337
if (!currFile.startsWith(conf.shaderpacksPath) || conf.shaderpacksPath === '') {
338338
connection.window.showErrorMessage(`Shaderpacks path may not be correct. Current file is in '${currFile}' but the path is set to '${conf.shaderpacksPath}'`)
339339
return ''

server/src/logging.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ import { CategoryServiceFactory, CategoryConfiguration, LogLevel, Category } fro
33
CategoryServiceFactory.setDefaultConfiguration(new CategoryConfiguration(LogLevel.Debug))
44

55
export const linterLog = new Category('linter')
6-
export const completionLog = new Category('completion')
6+
export const completionLog = new Category('completion')
7+
export const serverLog = new Category('server')

server/src/server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import * as vsclang from 'vscode-languageserver'
22
import * as vsclangproto from 'vscode-languageserver-protocol'
33
import { completions } from './completionProvider'
44
import { preprocess, ext, includeGraph } from './linter'
5-
import { extname } from 'path'
5+
import { extname, dirname } from 'path'
66

77
const reVersion = /#version [\d]{3}/
88

99
export let connection: vsclang.IConnection
1010
connection = vsclang.createConnection(new vsclang.IPCMessageReader(process), new vsclang.IPCMessageWriter(process))
1111

12-
import { onConfigChange } from './config'
12+
import { onConfigChange, conf, glslangReady } from './config'
1313
import { formatURI, postError, getDocumentContents } from './utils'
1414

1515
export const documents = new vsclang.TextDocuments()
@@ -39,6 +39,8 @@ documents.onDidClose((event) => connection.sendDiagnostics({uri: event.document.
3939
//documents.onDidChangeContent(onEvent)
4040

4141
export function onEvent(document: vsclangproto.TextDocument) {
42+
if (conf.shaderpacksPath.replace(dirname(conf.shaderpacksPath), '') !== '/shaderpacks' || !glslangReady) return
43+
4244
const uri = formatURI(document.uri)
4345
if (includeGraph.get(uri).parents.size > 0) {
4446
lintBubbleDown(uri, document)

server/src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { connection, documents } from './server'
22
import { readFileSync } from 'fs'
33
import { conf } from './config'
4+
import { serverLog } from './logging'
45

56
export function postError(e: Error) {
67
connection.window.showErrorMessage(e.message)
7-
console.log(e)
8+
serverLog.error(e.message, new Error())
89
}
910

1011
export const formatURI = (uri: string) => uri.replace(/^file:\/\//, '').replace(/^(?:\/)c%3A/, 'C:').replace(/\\/g, '/')

0 commit comments

Comments
 (0)