feat: add modlist export to Arma 3 Launcher HTML format
- RenderModlistHTML: generates compatible preset HTML from a modlist - GET /api/modlists/:id/export: returns downloadable .html file - Export button on ModlistEditor page - Skips local mods (no workshop ID), includes only Steam workshop mods
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/url"
|
||||
"path"
|
||||
@@ -11,6 +13,42 @@ import (
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
const modlistHTMLTmpl = `<?xml version="1.0" encoding="utf-8"?>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="arma:Type" content="list"/>
|
||||
<meta name="generator" content="Arma 3 Web Server"/>
|
||||
<title>Arma 3</title>
|
||||
<style>
|
||||
body{margin:0;padding:0;color:#fff;background:#000;font:95%/1.3 Roboto,Segoe UI,Tahoma,Arial,Helvetica,sans-serif}
|
||||
td{padding:3px 30px 3px 0}
|
||||
h1{padding:20px 20px 0;color:#fff;font-weight:200;font-family:segoe ui;font-size:3em;margin:0}
|
||||
.before-list{padding:5px 20px 10px}
|
||||
.mod-list{background:#222;padding:20px}
|
||||
.footer{padding:20px;color:gray}
|
||||
.from-steam{color:#449EBD}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Arma 3 Mods</h1>
|
||||
<p class="before-list">
|
||||
<em>To import this preset, drag this file onto the Launcher window. Or click the MODS tab, then PRESET in the top right, then IMPORT at the bottom, and finally select this file.</em>
|
||||
</p>
|
||||
<div class="mod-list">
|
||||
<table>{{range .Mods}}
|
||||
<tr data-type="ModContainer">
|
||||
<td data-type="DisplayName">{{.Name}}</td>
|
||||
<td><span class="from-steam">Steam</span></td>
|
||||
<td><a href="https://steamcommunity.com/sharedfiles/filedetails/?id={{.ID}}" data-type="Link">https://steamcommunity.com/sharedfiles/filedetails/?id={{.ID}}</a></td>
|
||||
</tr>{{end}}
|
||||
</table>
|
||||
</div>
|
||||
<div class="footer">
|
||||
<span>Created by Arma 3 Web Server.</span>
|
||||
</div>
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
func ParseModlistHTML(r io.Reader) (string, []models.ModEntry, error) {
|
||||
doc, err := html.Parse(r)
|
||||
if err != nil {
|
||||
@@ -114,3 +152,32 @@ func FileNameToModlistName(filename string) string {
|
||||
name = strings.ReplaceAll(name, "-", " ")
|
||||
return name
|
||||
}
|
||||
|
||||
type renderMod struct {
|
||||
Name string
|
||||
ID string
|
||||
}
|
||||
|
||||
func RenderModlistHTML(name string, mods []models.ModEntry) (string, error) {
|
||||
tmpl, err := template.New("modlist").Parse(modlistHTMLTmpl)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse template: %w", err)
|
||||
}
|
||||
|
||||
var renderMods []renderMod
|
||||
for _, m := range mods {
|
||||
if m.ID == "" {
|
||||
continue
|
||||
}
|
||||
renderMods = append(renderMods, renderMod{
|
||||
Name: m.Name,
|
||||
ID: m.ID,
|
||||
})
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tmpl.Execute(&buf, struct{ Mods []renderMod }{renderMods}); err != nil {
|
||||
return "", fmt.Errorf("execute template: %w", err)
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user