Skip to content

Commit

Permalink
Add a button to clear the browsers (tile) cache and reload them
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Aug 6, 2020
1 parent 2a14718 commit 62c8306
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
24 changes: 19 additions & 5 deletions BlueMapCore/src/main/webroot/js/libs/BlueMap.js
Expand Up @@ -57,13 +57,14 @@ import SKY_VERTEX_SHADER from './shaders/SkyVertexShader.js';
import SKY_FRAGMENT_SHADER from './shaders/SkyFragmentShader.js';

import { stringToImage, pathFromCoords } from './utils.js';
import {getCookie, setCookie} from "./utils";
import {cachePreventionNr, getCookie, setCookie} from "./utils";

export default class BlueMap {
constructor(element, dataRoot) {
this.element = $('<div class="bluemap-container"></div>').appendTo(element)[0];
this.dataRoot = dataRoot;
this.locationHash = '';
this.cacheSuffix = '';

this.hiresViewDistance = 160;
this.lowresViewDistance = 3200;
Expand Down Expand Up @@ -112,6 +113,17 @@ export default class BlueMap {
});
}

reloadMap() {
if (this.hiresTileManager !== undefined){
this.hiresTileManager.removeAllTiles();
this.hiresTileManager.update();
}
if (this.lowresTileManager !== undefined){
this.lowresTileManager.removeAllTiles();
this.lowresTileManager.update();
}
}

changeMap(map, loadTiles = true) {
if (this.debugInfo) console.debug("changing map: ", map);

Expand Down Expand Up @@ -310,7 +322,7 @@ export default class BlueMap {

async loadSettings() {
return new Promise(resolve => {
this.fileLoader.load(this.dataRoot + 'settings.json', settings => {
this.fileLoader.load(this.dataRoot + 'settings.json?' + cachePreventionNr(), settings => {
try {
this.settings = JSON.parse(settings);
this.maps = [];
Expand Down Expand Up @@ -379,6 +391,7 @@ export default class BlueMap {
this.hiresViewDistance = this.loadUserSetting("hiresViewDistance", this.hiresViewDistance);
this.lowresViewDistance = this.loadUserSetting("lowresViewDistance", this.lowresViewDistance);
this.controls.settings.zoom.max = this.loadUserSetting("maxZoomDistance", this.controls.settings.zoom.max);
this.cacheSuffix = this.loadUserSetting("cacheSuffix", this.cacheSuffix);
this.debugInfo = this.loadUserSetting("debugInfo", this.debugInfo);
}

Expand All @@ -393,6 +406,7 @@ export default class BlueMap {
this.saveUserSetting("hiresViewDistance", this.hiresViewDistance);
this.saveUserSetting("lowresViewDistance", this.lowresViewDistance);
this.saveUserSetting("maxZoomDistance", this.controls.settings.zoom.max);
this.saveUserSetting("cacheSuffix", this.cacheSuffix);
this.saveUserSetting("debugInfo", this.debugInfo);
}

Expand Down Expand Up @@ -427,7 +441,7 @@ export default class BlueMap {

async loadHiresMaterial() {
return new Promise(resolve => {
this.fileLoader.load(this.dataRoot + 'textures.json', textures => {
this.fileLoader.load(this.dataRoot + 'textures.json?' + cachePreventionNr(), textures => {
textures = JSON.parse(textures);
let materials = [];
for (let i = 0; i < textures['textures'].length; i++) {
Expand Down Expand Up @@ -501,7 +515,7 @@ export default class BlueMap {
async loadHiresTile(tileX, tileZ) {
let path = this.dataRoot + this.map + '/hires/';
path += pathFromCoords(tileX, tileZ);
path += '.json';
path += '.json?' + this.cacheSuffix;

return new Promise((resolve, reject) => {
this.bufferGeometryLoader.load(path, geometry => {
Expand All @@ -522,7 +536,7 @@ export default class BlueMap {
async loadLowresTile(tileX, tileZ) {
let path = this.dataRoot + this.map + '/lowres/';
path += pathFromCoords(tileX, tileZ);
path += '.json';
path += '.json?' + this.cacheSuffix;

return new Promise((reslove, reject) => {
this.bufferGeometryLoader.load(path, geometry => {
Expand Down
8 changes: 8 additions & 0 deletions BlueMapCore/src/main/webroot/js/libs/ui/UI.js
Expand Up @@ -41,6 +41,8 @@ import NIGHT from '../../../assets/night.svg';
import HudInfo from "../hud/HudInfo";
import MarkerManager from "../hud/MarkerManager";

import {cachePreventionNr} from "../utils";

export default class UI {

constructor(blueMap) {
Expand Down Expand Up @@ -101,6 +103,11 @@ export default class UI {
this.blueMap.debugInfo = button.isSelected();
});

let clearCache = new Button("clear tile cache", button => {
this.blueMap.cacheSuffix = cachePreventionNr();
this.blueMap.reloadMap();
});

//toolbar
this.toolbar.addElement(menuButton);
this.toolbar.addElement(mapSelect);
Expand Down Expand Up @@ -128,6 +135,7 @@ export default class UI {
this.menu.addElement(lowresSlider);
this.menu.addElement(extendedZoom);
this.menu.addElement(new Separator());
this.menu.addElement(clearCache);
this.menu.addElement(debugInfo);
this.menu.update();
}
Expand Down

0 comments on commit 62c8306

Please sign in to comment.