Skip to content

Commit

Permalink
Share frontend & backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Apr 20, 2024
1 parent 691850b commit 703b597
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 341 deletions.
323 changes: 165 additions & 158 deletions assets/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,192 +376,198 @@ function createImage() {
}
}

async function processEntries(entries) {
for await (const entry of entries) {
const filePath = entry.webkitRelativePath || entry.name
if (filePath.includes("/.git/") || filePath.includes("/.svn/")) continue
if (entry.kind == "directory") {
processEntries(entry)
continue
}
if (entry.name.endsWith("/") && entry.content == "") continue
const processFile = async (filePath = "", name = "", loadContentCallback = () => {}) => {
const ext = name.split(".").pop()
if (
ext == "mcmeta" || ext == "json" ||
(!rpMode && (ext == "mcfunction" || ext == "nbt")) ||
(rpMode && (ext == "png" || ext == "icns" || ext == "txt" || ext == "ogg" || ext == "fsh" || ext == "vsh" || ext == "glsl" || ext == "lang" || ext == "properties" || ext == "inc" || ext == "xcf"))
) {
if (filetypes[ext]) filetypes[ext]++
else filetypes[ext] = 1
} else {
if (filetypesOther[(name.includes(".") ? "." : "") + ext]) filetypesOther[(name.includes(".") ? "." : "") + ext]++
else filetypesOther[(name.includes(".") ? "." : "") + ext] = 1
}

const ext = entry.name.split(".").pop()
if (
ext == "mcmeta" || ext == "json" ||
(!rpMode && (ext == "mcfunction" || ext == "nbt")) ||
(rpMode && (ext == "png" || ext == "icns" || ext == "txt" || ext == "ogg" || ext == "fsh" || ext == "vsh" || ext == "glsl" || ext == "lang" || ext == "properties" || ext == "inc" || ext == "xcf"))
) {
if (filetypes[ext]) filetypes[ext]++
else filetypes[ext] = 1
} else {
if (filetypesOther[(entry.name.includes(".") ? "." : "") + ext]) filetypesOther[(entry.name.includes(".") ? "." : "") + ext]++
else filetypesOther[(entry.name.includes(".") ? "." : "") + ext] = 1
}
if (
ext == "mcfunction" || ext == "mcmeta" || (!rpMode && ext == "json" && (filePath.includes("/advancements/") || filePath.includes("/tags/functions/"))) ||
ext == "fsh" || ext == "vsh" || ext == "glsl" || name.endsWith("pack.png")
) {
files++

const processContent = result => {
done++
if (result.trim() == "") return emptyFiles.push(filePath)

if (!rpMode && ext == "mcfunction") {
const fileLocation = /data\/([-a-z0-9_.]+)\/functions\/([-a-z0-9_./]+)\.mcfunction/i.exec(filePath)
if (fileLocation && !dpExclusive.functions.includes(fileLocation[1] + ":" + fileLocation[2])) dpExclusive.functions.push(fileLocation[1] + ":" + fileLocation[2])

for (let line of result.split("\n")) {
line = line.trim()
if (line.startsWith("#")) comments++
if (line == "") empty++
if (line.startsWith("#") || line == "") continue
const splitted = line.split(" ")

let cmd = splitted[0]
if (cmd.startsWith("$")) {
cmd = cmd.slice(1)
if (cmdsBehindMacros[cmd]) cmdsBehindMacros[cmd]++
else cmdsBehindMacros[cmd] = 1
}

if (
ext == "mcfunction" || ext == "mcmeta" || (!rpMode && ext == "json" && (filePath.includes("/advancements/") || filePath.includes("/tags/functions/"))) ||
ext == "fsh" || ext == "vsh" || ext == "glsl" || entry.name.endsWith("pack.png")
) {
files++

const processFile = result => {
done++
if (result.trim() == "") return emptyFiles.push(filePath)

if (!rpMode && ext == "mcfunction") {
const fileLocation = /data\/([-a-z0-9_.]+)\/functions\/([-a-z0-9_./]+)\.mcfunction/i.exec(filePath)
if (fileLocation && !dpExclusive.functions.includes(fileLocation[1] + ":" + fileLocation[2])) dpExclusive.functions.push(fileLocation[1] + ":" + fileLocation[2])

for (let line of result.split("\n")) {
line = line.trim()
if (line.startsWith("#")) comments++
if (line == "") empty++
if (line.startsWith("#") || line == "") continue
const splitted = line.split(" ")

let cmd = splitted[0]
if (cmd.startsWith("$")) {
cmd = cmd.slice(1)
if (cmdsBehindMacros[cmd]) cmdsBehindMacros[cmd]++
else cmdsBehindMacros[cmd] = 1
}
if (commands[cmd]) commands[cmd]++
else commands[cmd] = 1

if (commands[cmd]) commands[cmd]++
else commands[cmd] = 1
if (cmd == "execute") {
const matches = / run ([a-z_:]{2,})/g.exec(line)
if (matches) matches.forEach(match => {
const cmdBehind = match.replace("run ", "").trim()

if (cmdsBehindExecute[cmdBehind]) cmdsBehindExecute[cmdBehind]++
else cmdsBehindExecute[cmdBehind] = 1
if (commands[cmdBehind]) commands[cmdBehind]++
else commands[cmdBehind] = 1

if (cmd == "execute") {
const matches = / run ([a-z_:]{2,})/g.exec(line)
if (matches) matches.forEach(match => {
const cmdBehind = match.replace("run ", "").trim()

if (cmdsBehindExecute[cmdBehind]) cmdsBehindExecute[cmdBehind]++
else cmdsBehindExecute[cmdBehind] = 1
if (commands[cmdBehind]) commands[cmdBehind]++
else commands[cmdBehind] = 1

if (cmdBehind == "return") {
const returnCmd = / run return run ([a-z_:]{2,})/g.exec(line)
if (returnCmd && returnCmd[1]) {
if (cmdsBehindReturn[returnCmd[1]]) cmdsBehindReturn[returnCmd[1]]++
else cmdsBehindReturn[returnCmd[1]] = 1
}
if (cmdBehind == "return") {
const returnCmd = / run return run ([a-z_:]{2,})/g.exec(line)
if (returnCmd && returnCmd[1]) {
if (cmdsBehindReturn[returnCmd[1]]) cmdsBehindReturn[returnCmd[1]]++
else cmdsBehindReturn[returnCmd[1]] = 1
}
})
} else if (cmd == "return") {
const returnCmd = / run return run ([a-z_:]{2,})/g.exec(line)
if (returnCmd && returnCmd[1]) {
if (cmdsBehindReturn[returnCmd[1]]) cmdsBehindReturn[returnCmd[1]]++
else cmdsBehindReturn[returnCmd[1]] = 1
}
})
} else if (cmd == "return") {
const returnCmd = / run return run ([a-z_:]{2,})/g.exec(line)
if (returnCmd && returnCmd[1]) {
if (cmdsBehindReturn[returnCmd[1]]) cmdsBehindReturn[returnCmd[1]]++
else cmdsBehindReturn[returnCmd[1]] = 1
}
if (fileLocation && (cmd == "function" || line.includes(" function ") || line.includes("/function "))) {
const func = /function ((#?[-a-z0-9_.]+):)?([-a-z0-9_./]+)/i.exec(line)
if (func && func[3]) dpExclusive.functionCalls.push({
source: fileLocation[1] + ":" + fileLocation[2],
target: (func[2] || "minecraft") + ":" + func[3]
})
}

if (/scoreboard objectives add \w+ \w+( .+)?$/.test(line)) dpExclusive.scoreboards++

splitted.forEach(arg => {
if (arg.startsWith("@")) {
arg = arg.slice(1)
if (arg.startsWith("a")) dpExclusive.selectors.a++
else if (arg.startsWith("e")) dpExclusive.selectors.e++
else if (arg.startsWith("p")) dpExclusive.selectors.p++
else if (arg.startsWith("r")) dpExclusive.selectors.r++
else if (arg.startsWith("s")) dpExclusive.selectors.s++
}
}
if (fileLocation && (cmd == "function" || line.includes(" function ") || line.includes("/function "))) {
const func = /function ((#?[-a-z0-9_.]+):)?([-a-z0-9_./]+)/i.exec(line)
if (func && func[3]) dpExclusive.functionCalls.push({
source: fileLocation[1] + ":" + fileLocation[2],
target: (func[2] || "minecraft") + ":" + func[3]
})
}
} else if (ext == "mcmeta") {
if (entry.name == "pack.mcmeta") {
try {
packFiles.push(JSON.parse(result))
} catch (e) {
console.warn("Could not parse pack.mcmeta: " + filePath, e)
error++

if (/scoreboard objectives add \w+ \w+( .+)?$/.test(line)) dpExclusive.scoreboards++

splitted.forEach(arg => {
if (arg.startsWith("@")) {
arg = arg.slice(1)
if (arg.startsWith("a")) dpExclusive.selectors.a++
else if (arg.startsWith("e")) dpExclusive.selectors.e++
else if (arg.startsWith("p")) dpExclusive.selectors.p++
else if (arg.startsWith("r")) dpExclusive.selectors.r++
else if (arg.startsWith("s")) dpExclusive.selectors.s++
}
})
}
} else if (ext == "mcmeta") {
if (name == "pack.mcmeta") {
try {
packFiles.push(JSON.parse(result))
} catch (e) {
console.warn("Could not parse pack.mcmeta: " + filePath, e)
error++
}
} else if (entry.name.endsWith("pack.png") && !result.includes(">")) packImages.push(result)
else if (rpMode && (ext == "fsh" || ext == "vsh" || ext == "glsl")) {
for (let line of result.split("\n")) {
line = line.trim()
if (line.startsWith("//") || line.startsWith("/*")) comments++
if (line == "") empty++
if (line.startsWith("//") || line.startsWith("/*") || line == "") continue

const cmd = line.match(/^[a-z_#0-9]+/i)?.[0]
if (cmd && cmd != "{" && cmd != "}") {
if (commands[cmd]) commands[cmd]++
else commands[cmd] = 1
}
}
} else if (name.endsWith("pack.png") && !result.includes(">")) packImages.push(result)
else if (rpMode && (ext == "fsh" || ext == "vsh" || ext == "glsl")) {
for (let line of result.split("\n")) {
line = line.trim()
if (line.startsWith("//") || line.startsWith("/*")) comments++
if (line == "") empty++
if (line.startsWith("//") || line.startsWith("/*") || line == "") continue

const cmd = line.match(/^[a-z_#0-9]+/i)?.[0]
if (cmd && cmd != "{" && cmd != "}") {
if (commands[cmd]) commands[cmd]++
else commands[cmd] = 1
}
} else if (!rpMode && ext == "json") {
if (filePath.includes("/advancements/")) {
const fileLocation = /data\/([-a-z0-9_.]+)\/advancements\/([-a-z0-9_./]+)\.json/i.exec(filePath)

try {
const parsed = JSON.parse(result)
if (parsed.rewards && parsed.rewards.function) dpExclusive.functionCalls.push({
source: "(Advancement) " + fileLocation[1] + ":" + fileLocation[2],
target: parsed.rewards.function.includes(":") ? parsed.rewards.function : "minecraft:" + parsed.rewards.function
})
} catch (e) {
console.warn("Unable to analyze advancement: " + filePath, e)
}
} else if (filePath.includes("/tags/functions/")) {
const fileLocation = /data\/([-a-z0-9_.]+)\/tags\/functions\/([-a-z0-9_./]+)\.json/i.exec(filePath)
if (fileLocation && !dpExclusive.functions.includes("#" + fileLocation[1] + ":" + fileLocation[2])) dpExclusive.functions.push("#" + fileLocation[1] + ":" + fileLocation[2])

try {
const parsed = JSON.parse(result)
if (parsed.values) parsed.values.forEach(func => {
if (typeof func == "object") {
if (func.required === false) return
func = func.id
}
}
} else if (!rpMode && ext == "json") {
if (filePath.includes("/advancements/")) {
const fileLocation = /data\/([-a-z0-9_.]+)\/advancements\/([-a-z0-9_./]+)\.json/i.exec(filePath)

try {
const parsed = JSON.parse(result)
if (parsed.rewards && parsed.rewards.function) dpExclusive.functionCalls.push({
source: "(Advancement) " + fileLocation[1] + ":" + fileLocation[2],
target: parsed.rewards.function.includes(":") ? parsed.rewards.function : "minecraft:" + parsed.rewards.function
})
} catch (e) {
console.warn("Unable to analyze advancement: " + filePath, e)
}
} else if (filePath.includes("/tags/functions/")) {
const fileLocation = /data\/([-a-z0-9_.]+)\/tags\/functions\/([-a-z0-9_./]+)\.json/i.exec(filePath)
if (fileLocation && !dpExclusive.functions.includes("#" + fileLocation[1] + ":" + fileLocation[2])) dpExclusive.functions.push("#" + fileLocation[1] + ":" + fileLocation[2])

try {
const parsed = JSON.parse(result)
if (parsed.values) parsed.values.forEach(func => {
if (typeof func == "object") {
if (func.required === false) return
func = func.id
}

dpExclusive.functionCalls.push({
source: "#" + fileLocation[1] + ":" + fileLocation[2],
target: func.includes(":") ? func : "minecraft:" + func
})
dpExclusive.functionCalls.push({
source: "#" + fileLocation[1] + ":" + fileLocation[2],
target: func.includes(":") ? func : "minecraft:" + func
})
} catch (e) {
console.warn("Unable to analyze function tag: " + filePath, e)
}
})
} catch (e) {
console.warn("Unable to analyze function tag: " + filePath, e)
}
}
}
}

await loadContentCallback(processContent, ext)
}
if (!rpMode && ext == "json") {
Object.keys(dpExclusive.folders).forEach(type => {
if (filePath.includes("/" + type + "/")) dpExclusive.folders[type]++
})
Object.keys(dpExclusive.tags).forEach(type => {
if (filePath.includes("/tags/" + type + "/")) dpExclusive.tags[type]++
})
} else if (rpMode)
Object.keys(rpExclusive).forEach(type => {
if (filePath.includes("/" + type + "/")) rpExclusive[type]++
})
}

async function processEntries(entries) {
for await (const entry of entries) {
const filePath = entry.webkitRelativePath || entry.name
if (filePath.includes("/.git/") || filePath.includes("/.svn/")) continue
if (entry.kind == "directory") {
processEntries(entry)
continue
}
if (entry.name.endsWith("/") && entry.content == "") continue

if ("content" in entry) processFile(entry.content)
processFile(filePath, entry.name, async (processContent, ext) => {
if ("content" in entry) processContent(entry.content)
else {
const reader = new FileReader()
if (ext == "png") reader.readAsDataURL(entry)
else entry.text().then(processFile)
else entry.text().then(processContent)

reader.onload = () => {
processFile(reader.result)
processContent(reader.result)
}
reader.onerror = e => {
console.warn("Could not read file: " + filePath, e)
error++
}
}
}
if (!rpMode && ext == "json") {
Object.keys(dpExclusive.folders).forEach(type => {
if (filePath.includes("/" + type + "/")) dpExclusive.folders[type]++
})
Object.keys(dpExclusive.tags).forEach(type => {
if (filePath.includes("/tags/" + type + "/")) dpExclusive.tags[type]++
})
} else if (rpMode)
Object.keys(rpExclusive).forEach(type => {
if (filePath.includes("/" + type + "/")) rpExclusive[type]++
})
})
}
}

Expand Down Expand Up @@ -722,7 +728,8 @@ async function mainScan(hasData = false) {
(Object.keys(filetypesOther).length > 0 ?
"<details><summary>" +
"<strong>Non-pack file types found:</strong></summary>" +
Object.keys(filetypesOther).sort((a, b) => filetypesOther[b] - filetypesOther[a]).map(type => "<span class='indented'>" + type + ": " + localize(filetypesOther[type]) + "</span><br>").join("") +
Object.keys(filetypesOther).sort((a, b) => filetypesOther[b] - filetypesOther[a])
.map(type => "<span class='indented'>" + type + ": " + localize(filetypesOther[type]) + "</span><br>").join("") +
"</details><br>"
: "") +
(uncalledFunctions.length > 0 ?
Expand Down
5 changes: 3 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ const rules = {
"@stylistic/js/no-trailing-spaces": 2,
"@stylistic/js/max-statements-per-line": 2,
"@stylistic/js/max-len": [2, {
code: 220
code: 210
}],
"@stylistic/js/quote-props": [2, "as-needed"],
"@stylistic/js/quotes": [2, "double", {
Expand Down Expand Up @@ -240,7 +240,8 @@ module.exports = [
languageOptions: {
globals: {
...global,
...globals.node
...globals.node,
processFile: "readonly"
}
},
files: ["vscExtension/**/*.js", "eslint.config.js", "minify.js"],
Expand Down
Loading

0 comments on commit 703b597

Please sign in to comment.