Skip to content

Commit

Permalink
automatic RP loading
Browse files Browse the repository at this point in the history
  • Loading branch information
solvedDev committed May 10, 2019
1 parent 996896c commit 1251169
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/renderer/components/sidebar/Content.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
explorer_type="resource_pack"
:base_path="RP_BASE_PATH"
:load_plugins="false"
:force_project_algorithm="findRP"
:show_toolbar="false"
/>
<content-plugins v-else-if="menu_type === 'extensions'"/>
<content-documentation v-else-if="menu_type === 'documentation'"/>
Expand All @@ -28,6 +30,7 @@
import ContentCustom from "./content/Custom";
import ContentNotImplemented from "./content/NotImplemented";
import { BASE_PATH, RP_BASE_PATH } from "../../scripts/constants";
import findRP from "../../scripts/utilities/FindRP";
export default {
name: "sidebar-content",
Expand All @@ -45,7 +48,8 @@
data() {
return {
BASE_PATH,
RP_BASE_PATH
RP_BASE_PATH,
findRP
}
}
}
Expand Down
48 changes: 42 additions & 6 deletions src/renderer/components/sidebar/content/Explorer.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<v-container>
<v-toolbar flat height="30px">
<v-toolbar v-if="show_toolbar" flat height="30px">
<v-tooltip bottom class="first">
<v-btn icon flat @click.stop="refresh" slot="activator" small>
<v-icon small>refresh</v-icon>
Expand Down Expand Up @@ -45,6 +45,7 @@
</v-tooltip>
</v-toolbar>
<v-select
v-if="force_project_algorithm === undefined"
ref="project_select"
:items="project_items"
:value="selected"
Expand All @@ -54,9 +55,32 @@
:loading="loading"
:disabled="items.length <= 1"
@input="getDirectory"
></v-select>
/>
<v-subheader
v-else
>{{ selected }}</v-subheader>
<v-divider></v-divider>
<file-displayer :files="directory" :project="selected" :base_path="base_path" class="file-displayer"></file-displayer>
<file-displayer
v-if="selected !== undefined && selected !== '/@NO-RP@/' && selected !== '/@NO-DEPENDENCY@/'"
:files="directory"
:project="selected"
:base_path="base_path"
class="file-displayer"
/>
<v-progress-linear v-else-if="selected === undefined" indeterminate/>
<div
v-else-if="selected === '/@NO-DEPENDENCY@/'"
style="padding: 4px;"

>
It doesn't look like your current behavior pack has a corresponding resource pack registered inside its manifest file.
</div>
<div
v-else
style="padding: 4px;"
>
The resource pack which belongs to this behavior pack does not exist.
</div>
<v-divider></v-divider>
</v-container>
</template>
Expand All @@ -80,7 +104,12 @@
props: {
load_plugins: Boolean,
base_path: String,
explorer_type: String
explorer_type: String,
force_project_algorithm: Function,
show_toolbar: {
default: true,
type: Boolean
}
},
data() {
return {
Expand All @@ -90,7 +119,7 @@
project_select_size: window.innerWidth / 7.5
};
},
mounted() {
async mounted() {
this.$root.$on("refreshExplorer", () => {
this.refresh();
});
Expand All @@ -105,7 +134,13 @@
}
});
this.getProjects({ event_name: "initialProjectLoad", func () {} });
if(this.force_project_algorithm) {
this.selected = undefined;
this.selected = await this.force_project_algorithm();
this.getDirectory(this.selected);
} else {
this.getProjects({ event_name: "initialProjectLoad", func () {} });
}
window.addEventListener("resize", this.onResize);
},
Expand Down Expand Up @@ -187,6 +222,7 @@
});
},
getDirectory(dir=this.selected, force_reload=false) {
if(dir === undefined || dir === "/@NO-RP@/" || dir === '/@NO-DEPENDENCY@/') return;
if(dir !== this.selected) {
this.$set(this, "selected", dir);
TabSystem.select(0);
Expand Down
54 changes: 54 additions & 0 deletions src/renderer/scripts/utilities/FindRP.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import TabSystem from "../TabSystem";
import fs from "fs";
import { RP_BASE_PATH, BASE_PATH } from "../constants";

let last_selected;
let last_result;
export default async function findRP() {
let selected = TabSystem.project;
if(selected === last_selected) return last_result;

last_selected = selected;
let manifest;
let uuid;
try {
manifest = JSON.parse(await readFile(`${BASE_PATH}${selected}\\manifest.json`));
uuid = manifest.dependencies[0].uuid;
} catch(e) {
last_result = "/@NO-DEPENDENCY@/";
return "/@NO-DEPENDENCY@/";
}

let rps = await readDirectory(RP_BASE_PATH);
let promises = [];
rps.forEach(rp => promises.push(readFile(`${RP_BASE_PATH}${rp}\\manifest.json`)));
promises = await Promise.all(promises).then(data => data.map(e => JSON.parse(e)));

for(let i = 0; i < promises.length; i++) {
if(promises[i].header.uuid === uuid) {
last_result = rps[i];
return rps[i];
}

}

last_result = "/@NO-RP@/";
return "/@NO-RP@/";
}

function readFile(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, "utf8", (err, data) => {
if(err) reject(err);
resolve(data);
});
});
}
function readDirectory(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, (err, data) => {
if(err) reject(err);
resolve(data);
});
});
}

0 comments on commit 1251169

Please sign in to comment.