Skip to content

Commit 813cf04

Browse files
committed
Path Normalization
Normalize path inside of models to be posix/unix-like forward slashes - Fixes #53
1 parent 8345d3a commit 813cf04

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/util/minecraft/resourcepack.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as path from 'path'
33
import { CustomError } from '../customError'
44
import { tl } from '../intl'
5+
import { normalizePath } from '../misc'
56
import { format } from '../replace'
67

78
export function getTexturePath(texture: any) {
@@ -32,8 +33,11 @@ export function getTexturePath(texture: any) {
3233
const textureIndex = relative.indexOf('textures') // Locate 'texture' in the path
3334
if (textureIndex > -1) {
3435
relative.splice(textureIndex, 1) // Remove 'texture' from the path
35-
console.log('Texture Path', `${namespace}:${path.join(...relative)}`)
36-
return `${namespace}:${path.join(...relative)}` // Generate texture path
36+
const textureReference = `${namespace}:${normalizePath(
37+
path.join(...relative)
38+
)}` // Generate texture path
39+
console.log('Texture Reference:', textureReference)
40+
return textureReference
3741
}
3842
}
3943
}
@@ -69,8 +73,11 @@ export function getModelPath(modelPath: string, modelName: string) {
6973
const modelsIndex = relative.indexOf('models') // Locate 'texture' in the path
7074
if (modelsIndex > -1) {
7175
relative.splice(modelsIndex, 1) // Remove 'texture' from the path
72-
console.log('Model Path', `${namespace}:${path.join(...relative)}`)
73-
return `${namespace}:${path.join(...relative)}` // Generate texture path
76+
const modelReference = `${namespace}:${normalizePath(
77+
path.join(...relative)
78+
)}` // Generate texture path
79+
console.log('Model Reference:', modelReference)
80+
return modelReference
7481
}
7582
}
7683
}
@@ -83,7 +90,7 @@ export function getModelPath(modelPath: string, modelName: string) {
8390
lines: format(
8491
tl('animatedJava.popup.error.unableToGenerateModelPath.body'),
8592
{
86-
modelName
93+
modelName,
8794
}
8895
)
8996
.split('\n')

src/util/misc.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ export function size(item: any) {
3232
export function wrapNumber(num: number, min: number, max: number): number {
3333
return ((((num - min) % (max - min)) + (max - min)) % (max - min)) + min
3434
}
35+
36+
export function normalizePath(path: string, stripTrailing?: boolean) {
37+
if (path === '\\' || path === '/') return '/'
38+
39+
var len = path.length
40+
if (len <= 1) return path
41+
42+
// ensure that win32 namespaces has two leading slashes, so that the path is
43+
// handled properly by the win32 version of path.parse() after being normalized
44+
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces
45+
var prefix = ''
46+
if (len > 4 && path[3] === '\\') {
47+
var ch = path[2]
48+
if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') {
49+
path = path.slice(2)
50+
prefix = '//'
51+
}
52+
}
53+
54+
var segs = path.split(/[/\\]+/)
55+
if (stripTrailing !== false && segs[segs.length - 1] === '') {
56+
segs.pop()
57+
}
58+
return prefix + segs.join('/')
59+
}

0 commit comments

Comments
 (0)