Skip to content

Commit

Permalink
added http server for loading modules in v5
Browse files Browse the repository at this point in the history
  • Loading branch information
rrgeorge committed Aug 25, 2024
1 parent 0e1f755 commit 9c1a8b1
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 25 deletions.
10 changes: 4 additions & 6 deletions ddb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,6 @@ class DDB {
tdSvc.use(turndownGfm.gfm)
let entry = {}
entry.name = cls.name
entry.ddbraw = cls
let sources = []
for (let source of cls.sources) {
let sourceName = this.ruledata.sources.find(s=>s.id===source.sourceId)?.description
Expand Down Expand Up @@ -2990,18 +2989,17 @@ class DDB {
value: 0
})

fs.copyFile(path.join(app.getPath("cache"),app.getName(),"modcache",`${book.name.toLowerCase()}.zip`),filename,()=>{
prog.value = 15
this.convertModule(moduleId,bookkey,filename,prog)
})
fs.copyFileSync(path.join(app.getPath("cache"),app.getName(),"modcache",`${book.name.toLowerCase()}.zip`),filename)
prog.value = 15
await this.convertModule(moduleId,bookkey,filename,prog)
return
}
}
const bookkey = await this.postRequest(keyurl,kparams)
.catch(e=>{throw new Error(`Could not get book key: ${e}`)})
const bookurl = await this.postRequest(url,params)
.catch(e=>{throw new Error(`Could not get book url: ${e}`)})
download(win,bookurl.data,{
await download(win,bookurl.data,{
saveAs: false,
filename: path.basename(filename),
directory: path.dirname(filename),
Expand Down
67 changes: 67 additions & 0 deletions http.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const { app } = require('electron')
const express = require('express')
const { networkInterfaces } = require('os')
const path = require('path')
class http {
get ipaddr() {
return this.app.locals.ip
}
get port() {
return this.app.locals.port
}

constructor(filename,code,name) {
this.app = express()
this.app.locals.filename = filename
this.app.locals.code = code
this.app.locals.name = name

this.app.get('/:filename', function(req,res) {
if (req.params.filename != path.basename(req.app.locals.filename)) {
res.sendStatus(404)
} else {
res.setHeader('Content-Disposition',`attachment; filename=${path.basename(req.app.locals.filename)}`)
.sendFile(filename,()=>{})
}
})

this.app.get('/', function(req,res) {
res.contentType('application/json')
.send(JSON.stringify(
{
"id": code,
"name": name,
"type": "module",
"version": app.getVersion(),
"description": `EncounterLog Export of ${name}`,
"download": `http://${req.app.locals.ip}:${req.app.locals.port}/${path.basename(req.app.locals.filename)}`
}
)
)
})

this.server = new Promise((resolve,reject)=>{
try{
let server = this.app.listen(0,'::',()=>{
console.log("Server started.")
const nets = networkInterfaces()
for (const net of Object.keys(nets)) {
for(const ip of nets[net]) {
if (ip.family == 'IPv4' && !ip.internal) {
this.app.locals.ip = ip.address
break;
}
}
if (this.app.locals.ip) break;
}
console.log(server.address())
this.app.locals.port = server.address()?.port
resolve(server)
})
} catch(e) {
reject(e)
}
})
}
}
module.exports = http;
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 51 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const DDB = require('./ddb')
const fs = require('fs')
const { autoUpdater } = require('electron-updater')
const { convert } = require('html-to-text')
const { networkInterfaces } = require('os')
const { isObject } = require('util')
const http = require('./http')

var _ws = null
var _eWs = null
Expand Down Expand Up @@ -162,6 +165,14 @@ const preferences = new ElectronPreferences({
{ 'label': 'In "Maps" group', 'value': 'group' },
],
},
{
'label': "After conversion is complete, EncounterLog can launch a web server for E+ to download the manifest and data from",
'key': 'launchserver',
'type': 'checkbox',
'options': [
{ 'label': 'Launch Server After Conversion', 'value': true },
]
},
],
},
{
Expand Down Expand Up @@ -267,8 +278,8 @@ app.on('ready', () => {
})
const splash = new BrowserWindow({
show: false,
width: 512,
height: 512,
width: 256,
height: 256,
transparent: true,
frame: false,
alwaysOnTop: true
Expand Down Expand Up @@ -389,17 +400,16 @@ app.on('ready', () => {
)
win.once('ready-to-show', () => {
let splashInt = setInterval(()=>{
if (!splash?.isVisible())
clearInterval(splashInt)
const op = splash.getOpacity()
if (op > 0) {
splash.setOpacity(op - .02)
} else {
clearInterval(splashInt)
splash.close()
}
if (!splash?.isVisible())
clearInterval(splashInt)
const op = splash.getOpacity()
if (op > 0) {
splash.setOpacity(op - .02)
} else {
clearInterval(splashInt)
splash.close()
}
},5)

win.show()
})

Expand Down Expand Up @@ -715,7 +725,21 @@ function populateCompendiumMenu(force=false) {
defaultPath: `${book.bookCode.toLowerCase()}-v5.compendium`,
}).then((save) => {
if (save.filePath)
ddb.getV5Compendium(book.id,save.filePath)
ddb.getV5Compendium(book.id,save.filePath).then(()=>{
if (preferences.value('export.launchserver').includes(true)) {
let httpServer = new http(save.filePath,'all','Complete Compendium')
httpServer.server.then((s)=>{
dialog.showMessageBox(_win,{
title: 'Server Running',
message: `The web server is running. Set the manifest to http://${httpServer.ipaddr}:${httpServer.port}. It will shutdown after you close this dialog.`,
type: "info"
}).then((r)=>{
s.close()
})
})
}
})

}
)
}
Expand Down Expand Up @@ -887,7 +911,20 @@ function populateCompendiumMenu(force=false) {
defaultPath: `v5.compendium`,
}).then((save) => {
if (save.filePath)
ddb.getV5Compendium(null,save.filePath)
ddb.getV5Compendium(null,save.filePath).then(()=>{
if (preferences.value('export.launchserver').includes(true)) {
let httpServer = new http(save.filePath,'all','Complete Compendium')
httpServer.server.then((s)=>{
dialog.showMessageBox(_win,{
title: 'Server Running',
message: `The web server is running. Set the manifest to http://${httpServer.ipaddr}:${httpServer.port}. It will shutdown after you close this dialog.`,
type: "info"
}).then((r)=>{
s.close()
})
})
}
})
}
)
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
"electron-preferences": "^2.8.2",
"electron-progressbar": "^2.0.1",
"electron-updater": "6.3.3",
"express": "^4.19.2",
"fuse.js": "^6.5.3",
"he": "^1.2.0",
"html-to-text": "^8.0.0",
Expand Down
55 changes: 55 additions & 0 deletions splash.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>EncounterLog</title>
<style type="text/css">
img { max-height: 100%; max-width: 100%; }
.spinner {
position: fixed;
bottom: 15px;
left: 0px;
width: 100%;
text-align: center;
z-index: 999;
}
.loader {
width: 48px;
height: 48px;
border-radius: 50%;
display: inline-block;
border-top: 4px solid #FFF;
border-right: 4px solid transparent;
box-sizing: border-box;
animation: rotation 1s linear infinite;
}
.loader::after {
content: '';
box-sizing: border-box;
position: absolute;
left: 0;
top: 0;
width: 48px;
height: 48px;
border-radius: 50%;
border-left: 4px solid #FF0000;
border-bottom: 4px solid transparent;
animation: rotation 0.5s linear infinite reverse;
}
@keyframes rotation {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
</head>
<body>
<div>
<img src="icon.png">
</div>
<div class="spinner">
<span class="loader"></span>
</div>
</body>
Loading

0 comments on commit 9c1a8b1

Please sign in to comment.