Skip to content

Commit

Permalink
v2.5.2: Fix #10: Fix patching of uncached files
Browse files Browse the repository at this point in the history
Fix #9: Improve debug messages
  • Loading branch information
Tibowl committed May 30, 2020
1 parent 5a40210 commit 20fa0c1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kccacheproxy",
"productName": "KCCacheProxy",
"version": "2.5.1",
"version": "2.5.2",
"description": "KanColle caching proxy",
"main": "src/electron/index.js",
"scripts": {
Expand Down
46 changes: 29 additions & 17 deletions src/proxy/cacher.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,29 @@ function loadCached() {
Logger.log(`Loading cached from ${CACHE_INFORMATION}.`)

try {
if (existsSync(CACHE_INFORMATION))
if (existsSync(CACHE_INFORMATION)) {
cached = JSON.parse(readFileSync(CACHE_INFORMATION))

return Logger.send("stats", getCacheStats())
return Logger.send("stats", getCacheStats())
}
} catch (error) {
Logger.error("Failed to load cached.json")
}

try {
if (existsSync(CACHE_INFORMATION + ".bak"))
if (existsSync(CACHE_INFORMATION + ".bak")) {
cached = JSON.parse(readFileSync(CACHE_INFORMATION + ".bak"))

return Logger.send("stats", getCacheStats())
return Logger.send("stats", getCacheStats())
}
} catch (error) {
Logger.error("Failed to load cached.json.bak")
}

if (cached == undefined)
if (cached == undefined) {
cached = {}
Logger.log("No valid file found, using empty cache")
} else {
Logger.log("No valid file found, not reloading cache")
}

return Logger.send("stats", getCacheStats())
}
Expand Down Expand Up @@ -186,6 +190,12 @@ async function cache(cacheFile, file, url, version, lastmodified, headers = {})
}
Logger.addStatAndSend("fetched")

const newCached = {
"version": version,
"lastmodified": data.headers.get("last-modified"),
"length": (+data.headers.get("content-length")) || contents.length,
"cache": data.headers.get("cache-control")
}
const queueSave = async () => {
await ensureDir(dirname(cacheFile))

Expand All @@ -196,12 +206,7 @@ async function cache(cacheFile, file, url, version, lastmodified, headers = {})
await remove(cacheFile)
await move(cacheFile + ".tmp", cacheFile)

cached[file] = {
"version": version,
"lastmodified": data.headers.get("last-modified"),
"length": (+data.headers.get("content-length")) || contents.length,
"cache": data.headers.get("cache-control")
}
cached[file] = newCached
queueCacheSave()

Logger.log("Saved", url)
Expand All @@ -210,7 +215,13 @@ async function cache(cacheFile, file, url, version, lastmodified, headers = {})

if (cached[file])
cached[file].length = (+data.headers.get("content-length")) || contents.length
queueSave()

// Metadata is written after writing file to prevent incorrectly loading wrong file (before written to disk)
// Modder requires some of these
if (getConfig().enableModder)
await queueSave()
else
queueSave()

return rep
}
Expand Down Expand Up @@ -443,10 +454,11 @@ async function saveCached() {
return Logger.log(`Cache is empty, not saved to ${CACHE_INFORMATION}`)

await ensureDir(getCacheLocation())
if (await exists(CACHE_INFORMATION) && await exists(CACHE_INFORMATION + ".bak"))
await remove(CACHE_INFORMATION + ".bak")
if (await exists(CACHE_INFORMATION))
if (await exists(CACHE_INFORMATION)) {
if (await exists(CACHE_INFORMATION + ".bak"))
await remove(CACHE_INFORMATION + ".bak")
await move(CACHE_INFORMATION, CACHE_INFORMATION + ".bak")
}
await writeFile(CACHE_INFORMATION, str)

Logger.send("stats", getCacheStats())
Expand Down
39 changes: 27 additions & 12 deletions src/proxy/mod/patchedcache.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
const { join, dirname } = require("path")
const { exists, readFile, remove, rename, ensureDir, writeFile, move } = require("fs-extra")
const { exists, readFile, remove, ensureDir, writeFile, move } = require("fs-extra")

const Logger = require("./../ipc")
const { getCacheLocation } = require("./../config")

let cached = undefined
async function loadCached() {
const CACHE_INFORMATION = join(getCacheLocation(), "mod-cache.json")
Logger.log(`Loading modded cached from ${CACHE_INFORMATION}.`)
Logger.log(`Loading modded cached from ${CACHE_INFORMATION}`)

try {
if (await exists(CACHE_INFORMATION)) {
cached = JSON.parse(await readFile(CACHE_INFORMATION))
return
}
} catch (error) {
Logger.error("Failed to load mod-cached.json")
}

if (await exists(CACHE_INFORMATION + ".bak")) {
if (await exists(CACHE_INFORMATION))
await remove(CACHE_INFORMATION)
await rename(CACHE_INFORMATION + ".bak", CACHE_INFORMATION)
try {
if (await exists(CACHE_INFORMATION + ".bak")){
cached = JSON.parse(await readFile(CACHE_INFORMATION + ".bak"))
return
}
} catch (error) {
Logger.error("Failed to load mod-cached.json.bak")
}

if (await exists(CACHE_INFORMATION))
cached = JSON.parse(await readFile(CACHE_INFORMATION))
else
if (cached == undefined) {
cached = {}
Logger.log("No valid file found, using empty mod-cache")
} else {
Logger.log("No valid file found, not reloading mod-cache")
}
}

async function checkCached(file, patchHash, lastmodified) {
Expand Down Expand Up @@ -91,11 +105,12 @@ async function saveCached() {
return Logger.log(`Cache is empty, not saved to ${CACHE_INFORMATION}`)

await ensureDir(getCacheLocation())
if (await exists(CACHE_INFORMATION))
if (await exists(CACHE_INFORMATION)) {
if (await exists(CACHE_INFORMATION + ".bak"))
await remove(CACHE_INFORMATION + ".bak")
await move(CACHE_INFORMATION, CACHE_INFORMATION + ".bak")
}
await writeFile(CACHE_INFORMATION, str)
if (await exists(CACHE_INFORMATION + ".bak"))
await remove(CACHE_INFORMATION + ".bak")

Logger.log(`Saved mod cache to ${CACHE_INFORMATION}.`)
}
Expand Down

0 comments on commit 20fa0c1

Please sign in to comment.