Fixed client UI cache

This commit is contained in:
WolverinDEV 2021-04-20 00:43:43 +02:00
parent 4a19f3a827
commit edff7d4014
5 changed files with 89 additions and 76 deletions

View File

@ -123,5 +123,5 @@ function deploy_client() {
#install_npm #install_npm
#compile_scripts #compile_scripts
#compile_native #compile_native
#package_client package_client
deploy_client #deploy_client

View File

@ -183,25 +183,29 @@ async function loadCachedOrRemoteUiPack(channel: string, callbackStatus: (messag
const bundledUi = await shippedClientUi(); const bundledUi = await shippedClientUi();
const clientVersion = await currentClientVersion(); const clientVersion = await currentClientVersion();
console.error("Looking for downloaded up packs on channel %s", channel);
let availableCachedVersions: CachedUIPack[] = localUiCache().cachedPacks.filter(e => { let availableCachedVersions: CachedUIPack[] = localUiCache().cachedPacks.filter(e => {
if(e.status.type !== "valid") { if(e.status.type !== "valid") {
console.error("Not valid");
return false;
}
if(e.packInfo.channel !== channel) {
console.error("%s !== %s", e.packInfo.channel, channel);
/* ui-pack is for another channel */
return false; return false;
} }
if(bundledUi) { if(bundledUi) {
/* remove all cached ui packs which are older than our bundled one */ /* remove all cached ui packs which are older than our bundled one */
if(e.packInfo.timestamp <= bundledUi.downloadTimestamp) { if(e.packInfo.timestamp <= bundledUi.packInfo.timestamp) {
console.error("%d <= %s", e.packInfo.timestamp, bundledUi.packInfo.timestamp);
return false; return false;
} }
} }
if(e.packInfo.channel !== channel) {
/* ui-pack is for another channel */
return false;
}
const requiredVersion = parseVersion(e.packInfo.requiredClientVersion); const requiredVersion = parseVersion(e.packInfo.requiredClientVersion);
return clientVersion.isDevelopmentVersion() || clientVersion.newerThan(requiredVersion) || clientVersion.equals(requiredVersion); return clientVersion.isDevelopmentVersion() || clientVersion.newerThan(requiredVersion, true) || clientVersion.equals(requiredVersion);
}); });
if(processArguments.has_flag(Arguments.UPDATER_UI_NO_CACHE)) { if(processArguments.has_flag(Arguments.UPDATER_UI_NO_CACHE)) {
@ -209,6 +213,7 @@ async function loadCachedOrRemoteUiPack(channel: string, callbackStatus: (messag
availableCachedVersions = []; availableCachedVersions = [];
} }
console.error("Found %d local UI packs (%d not suitable).", availableCachedVersions.length, localUiCache().cachedPacks.length - availableCachedVersions.length);
let remoteVersionDropped = false; let remoteVersionDropped = false;
/* fetch the remote versions */ /* fetch the remote versions */

View File

@ -39,7 +39,7 @@ export class Version {
if(other.patch != this.patch) return false; if(other.patch != this.patch) return false;
if(other.build != this.build) return false; if(other.build != this.build) return false;
return other.timestamp == this.timestamp; return other.timestamp === 0 || this.timestamp === 0 || other.timestamp === this.timestamp;
} }
newerThan(other: Version, compareTimestamps?: boolean) : boolean { newerThan(other: Version, compareTimestamps?: boolean) : boolean {

View File

@ -3,55 +3,52 @@ import * as fs from "fs-extra";
import * as path from "path"; import * as path from "path";
/* We read/write to this file every time again because this file could be used by multiple processes */ /* We read/write to this file every time again because this file could be used by multiple processes */
const data_file: string = path.join((electron.app || electron.remote.app).getPath('userData'), "window-bounds.json"); const configFile: string = path.join((electron.app || electron.remote.app).getPath('userData'), "window-bounds.json");
import BrowserWindow = Electron.BrowserWindow; import BrowserWindow = Electron.BrowserWindow;
import Rectangle = Electron.Rectangle; import Rectangle = Electron.Rectangle;
let changedData: {[key: string]:Rectangle} = {}; let changedData: {[key: string]: Rectangle} = {};
let changedDataSaveTimeout: number; let changedDataSaveTimeout: number;
export async function save_changes() { export async function saveChanges() {
clearTimeout(changedDataSaveTimeout); clearTimeout(changedDataSaveTimeout);
try { try {
const data = (await fs.pathExists(data_file) ? await fs.readJson(data_file) : {}) || {}; const data = (await fs.pathExists(configFile) ? await fs.readJson(configFile) : {}) || {};
Object.assign(data, changedData); Object.assign(data, changedData);
await fs.ensureFile(data_file);
await fs.writeJson(data_file, data);
path_exists = true;
changedData = {}; changedData = {};
await fs.ensureFile(configFile);
await fs.writeJson(configFile, data);
configFileExists = true;
console.log("Window bounds have been successfully saved!");
} catch(error) { } catch(error) {
console.warn("Failed to save window bounds: %o", error); console.warn("Failed to save window bounds: %o", error);
} }
console.log("Window bounds have been successfully saved!");
} }
let path_exists = undefined; let configFileExists: boolean;
export async function get_last_bounds(key: string) : Promise<Rectangle> { export async function loadLastWindowsBounds(key: string) : Promise<Rectangle | undefined> {
try { try {
if(typeof(path_exists) === "undefined" ? !(path_exists = await fs.pathExists(data_file)) : !path_exists) { if(typeof configFileExists !== "boolean") {
throw "skip!"; configFileExists = await fs.pathExists(configFile);
} }
const data = await fs.readJson(data_file) || {}; if(!configFileExists) {
if(data[key]) { return undefined;
}
const data = await fs.readJson(configFile) || {};
if(typeof data[key] === "object") {
return data[key]; return data[key];
} }
} catch(error) { } catch(error) {
if(error !== "skip!") { console.warn("Failed to load window bounds for %s: %o", key, error);
console.warn("Failed to load window bounds for %s: %o", key, error);
}
} }
return { return undefined;
height: undefined,
width: undefined,
x: undefined,
y: undefined
}
} }
export function startTrackWindowBounds(windowId: string, window: BrowserWindow) { export function startTrackWindowBounds(windowId: string, window: BrowserWindow) {
@ -59,57 +56,68 @@ export function startTrackWindowBounds(windowId: string, window: BrowserWindow)
const onWindowBoundsChanged = () => { const onWindowBoundsChanged = () => {
changedData[windowId] = window.getBounds(); changedData[windowId] = window.getBounds();
if(window.isMaximized()) {
changedData[windowId].width = -1;
changedData[windowId].height = -1;
}
clearTimeout(changedDataSaveTimeout); clearTimeout(changedDataSaveTimeout);
changedDataSaveTimeout = setTimeout(save_changes, 1000) as any; changedDataSaveTimeout = setTimeout(saveChanges, 1000) as any;
}; };
for(const event of events) for(const event of events) {
window.on(event as any, onWindowBoundsChanged); window.on(event as any, onWindowBoundsChanged);
}
window.on('closed', () => { window.on('closed', () => {
for(const event of events) for(const event of events) {
window.removeListener(event as any, onWindowBoundsChanged); window.removeListener(event as any, onWindowBoundsChanged);
}
}); });
} }
export async function loadWindowBounds(windowId: string, window: BrowserWindow, bounds?: Rectangle, options?: { applySize?: boolean; applyPosition?: boolean }) { export async function loadWindowBounds(windowId: string, window: BrowserWindow, targetBounds?: Rectangle, options?: { applySize?: boolean; applyPosition?: boolean }) {
const screen = electron.screen || electron.remote.screen; const screen = electron.screen || electron.remote.screen;
if(!bounds) { let maximize = false;
bounds = await get_last_bounds(windowId); targetBounds = Object.assign({}, targetBounds || await loadLastWindowsBounds(windowId));
let originalBounds = window.getBounds();
if(typeof options?.applySize === "boolean" && !options?.applySize) {
targetBounds.width = originalBounds.width;
targetBounds.height = originalBounds.height;
} }
if(!options) { if(typeof options?.applyPosition === "boolean" && !options.applyPosition) {
options = {}; targetBounds.x = originalBounds.x;
targetBounds.y = originalBounds.y;
} }
const original_bounds = window.getBounds(); if(targetBounds.width < 0 || targetBounds.height < 0) {
/* Invalid bounds or may -1 and -1 for max window */
if(typeof(options.applySize) !== "boolean" || options.applySize) { const nearestScreen = screen.getDisplayNearestPoint({ x: targetBounds.x + 30, y: targetBounds.y + 30 });
let height = bounds.height > 0 ? bounds.height : original_bounds.height; if(nearestScreen) {
let width = bounds.width > 0 ? bounds.width : original_bounds.width; maximize = true;
targetBounds = Object.assign({}, nearestScreen.workArea);
if(height != original_bounds.height || width != original_bounds.width) } else {
window.setSize(width, height, true); targetBounds.width = originalBounds.width;
} targetBounds.height = originalBounds.height;
if(typeof(options.applyPosition) !== "boolean" || options.applyPosition) {
let x = typeof(bounds.x) === "number" ? bounds.x : original_bounds.x;
let y = typeof(bounds.y) === "number" ? bounds.y : original_bounds.y;
if(x != original_bounds.x || y != original_bounds.y) {
const display = screen.getDisplayNearestPoint({ x: x, y: y });
if(display) {
const bounds = display.workArea || display.bounds;
let flag_invalid = false;
flag_invalid = flag_invalid || bounds.x > x || (bounds.x + bounds.width) < x;
flag_invalid = flag_invalid || bounds.y > x || (bounds.y + bounds.height) < y;
if(!flag_invalid) {
window.setPosition(x, y, true);
console.log("Updating position for %s", windowId);
}
}
} }
} }
if(originalBounds.x === targetBounds.x &&
originalBounds.y === targetBounds.y &&
originalBounds.width === targetBounds.width &&
originalBounds.height === targetBounds.height) {
/* no changes */
return;
}
/* Test if screen will be fully off display */
/* FIXME: TODO! */
window.setBounds(targetBounds, true);
if(maximize) {
window.maximize();
}
} }

View File

@ -202,13 +202,13 @@ inline bool check_uuid(std::string& uuid, uint32_t check_sum) {
return result == check_sum; return result == check_sum;
} }
static std::string _cached_system_uuid{}; static std::string cached_system_uuid_{};
static uint32_t _cached_system_uuid_cksm = 0; static uint32_t cached_system_uuid_cksm_ = 0;
std::string system_uuid() { std::string system_uuid() {
if(!_cached_system_uuid.empty() && check_uuid(_cached_system_uuid, _cached_system_uuid_cksm)) if(!cached_system_uuid_.empty() && check_uuid(cached_system_uuid_, cached_system_uuid_cksm_))
return _cached_system_uuid; return cached_system_uuid_;
if(!generate_uuid(_cached_system_uuid, _cached_system_uuid_cksm)) if(!generate_uuid(cached_system_uuid_, cached_system_uuid_cksm_))
_cached_system_uuid = ""; cached_system_uuid_ = "";
return _cached_system_uuid; return cached_system_uuid_;
} }