This commit is contained in:
Robert Janetzko 2022-05-10 07:51:04 +00:00
parent 375eab8e22
commit 52bffc322d
17 changed files with 75 additions and 20 deletions

View File

@ -29,6 +29,14 @@ var rootCmd = &cobra.Command{
Use: "legendsbrowser", Use: "legendsbrowser",
Short: "A Legends Browser for Dwarf Fortress", Short: "A Legends Browser for Dwarf Fortress",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
fmt.Println(`
__ __ ____
/ / ___ ____ ___ ____ ____/ /____ / __ )_________ _ __________ _____
/ / / _ \/ __ \/ _ \/ __ \/ __ / ___/ / __ / ___/ __ \ | /| / / ___/ _ \/ ___/
/ /___/ __/ /_/ / __/ / / / /_/ (__ ) / /_/ / / / /_/ / |/ |/ (__ ) __/ /
/_____/\___/\__, /\___/_/ /_/\__,_/____/ /_____/_/ \____/|__/|__/____/\___/_/
/____/ ` + "\n ")
if *p { if *p {
defer profile.Start(profile.ProfilePath(".")).Stop() defer profile.Start(profile.ProfilePath(".")).Stop()
go func() { go func() {

View File

@ -1,8 +1,10 @@
package model package model
import ( import (
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -17,15 +19,20 @@ type EntityLeader struct {
} }
func (w *DfWorld) LoadHistory() { func (w *DfWorld) LoadHistory() {
w.LoadDimensions() fmt.Println("")
path := strings.ReplaceAll(w.FilePath, "-legends.xml", "-world_history.txt") path := strings.ReplaceAll(w.FilePath, "-legends.xml", "-world_history.txt")
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println(err) if errors.Is(err, os.ErrNotExist) {
fmt.Println("no world history found")
} else {
fmt.Println(err)
}
return return
} }
fmt.Println("found world history", path)
leaderRegEx := regexp.MustCompile(` \[\*\] (.+?) \(.*?Reign Began: (-?\d+)\)`) leaderRegEx := regexp.MustCompile(` \[\*\] (.+?) \(.*?Reign Began: (-?\d+)\)`)
results := regexp.MustCompile(`\n([^ ].*?), [^\n]+(?:\n [^\n]+)*`).FindAllStringSubmatch(util.ConvertCp473(data), -1) results := regexp.MustCompile(`\n([^ ].*?), [^\n]+(?:\n [^\n]+)*`).FindAllStringSubmatch(util.ConvertCp473(data), -1)
for _, result := range results { for _, result := range results {

View File

@ -18,6 +18,8 @@ import (
func (w *DfWorld) LoadMap() { func (w *DfWorld) LoadMap() {
w.LoadDimensions() w.LoadDimensions()
fmt.Println("")
path := "" path := ""
files, err := filepath.Glob(strings.ReplaceAll(w.FilePath, "-legends.xml", "-world_map.*")) files, err := filepath.Glob(strings.ReplaceAll(w.FilePath, "-legends.xml", "-world_map.*"))
if err == nil && len(files) > 0 { if err == nil && len(files) > 0 {
@ -29,6 +31,7 @@ func (w *DfWorld) LoadMap() {
} }
if path == "" { if path == "" {
fmt.Println("no world map found")
return return
} }
@ -38,13 +41,13 @@ func (w *DfWorld) LoadMap() {
return return
} }
fmt.Println("Found Map", path) fmt.Println("found world map", path)
img, format, err := image.Decode(mapImage) img, format, err := image.Decode(mapImage)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
fmt.Println("loaded img", format) fmt.Println("loaded world map imgage as", format)
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err = png.Encode(buf, img) err = png.Encode(buf, img)
if err != nil { if err != nil {
@ -56,6 +59,8 @@ func (w *DfWorld) LoadMap() {
} }
func (w *DfWorld) LoadDimensions() { func (w *DfWorld) LoadDimensions() {
fmt.Println("")
files, err := filepath.Glob(filepath.Join(filepath.Dir(w.FilePath), "*-world_gen_param.txt")) files, err := filepath.Glob(filepath.Join(filepath.Dir(w.FilePath), "*-world_gen_param.txt"))
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -70,18 +75,21 @@ func (w *DfWorld) LoadDimensions() {
} }
} }
if path == "" { if path == "" {
fmt.Println("no worldgen params found")
return return
} }
fmt.Println("Found Worldgen", path) fmt.Println("found worldgen params", path)
content, err := ioutil.ReadFile(path) content, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println(err)
return return
} }
r := regexp.MustCompile(`\[DIM:(\d+):(\d+)\]`) r := regexp.MustCompile(`\[DIM:(\d+):(\d+)\]`)
result := r.FindAllStringSubmatch(string(content), 1) result := r.FindAllStringSubmatch(string(content), 1)
if result == nil { if result == nil {
fmt.Println("no world dimensions found")
return return
} }
w.Width, _ = strconv.Atoi(result[0][2]) w.Width, _ = strconv.Atoi(result[0][2])

View File

@ -28,7 +28,7 @@ func NewLegendsDecoder(file string) (*xml.Decoder, *os.File, *pb.ProgressBar, er
fmt.Println(err) fmt.Println(err)
} }
fmt.Println("Successfully Opened", file) fmt.Println("Loading:", file)
converter := util.NewConvertReader(xmlFile) converter := util.NewConvertReader(xmlFile)
barReader := bar.NewProxyReader(converter) barReader := bar.NewProxyReader(converter)
@ -50,7 +50,7 @@ func NewLegendsParser(file string) (*util.XMLParser, *os.File, *pb.ProgressBar,
fmt.Println(err) fmt.Println(err)
} }
fmt.Println("Successfully Opened", file) fmt.Println("\nLoading:", file)
barReader := bar.NewProxyReader(xmlFile) barReader := bar.NewProxyReader(xmlFile)
d := util.NewXMLParser(bufio.NewReader(barReader)) d := util.NewXMLParser(bufio.NewReader(barReader))
@ -100,10 +100,14 @@ BaseLoop:
bar.Finish() bar.Finish()
plus := false plus := false
file = strings.Replace(file, "-legends.xml", "-legends_plus.xml", 1)
if _, err := os.Stat(file); err == nil {
plus = true
} else {
fmt.Println("\nno legends_plus.xml found")
}
if plus { if plus {
file = strings.Replace(file, "-legends.xml", "-legends_plus.xml", 1)
p, xmlFile, bar, err = NewLegendsParser(file) p, xmlFile, bar, err = NewLegendsParser(file)
if lp != nil { if lp != nil {
lp.Message = "Loading " + file lp.Message = "Loading " + file

View File

@ -2,7 +2,6 @@ package server
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -31,9 +30,7 @@ func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path) data, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
fmt.Println("OPEN", err)
if os.IsNotExist(err) { if os.IsNotExist(err) {
fmt.Println("EX", err)
home, err := os.UserHomeDir() home, err := os.UserHomeDir()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -1,7 +1,6 @@
package server package server
import ( import (
"fmt"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -28,13 +27,9 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// prepend the path with the path to the static directory // prepend the path with the path to the static directory
path = h.staticPath + path path = h.staticPath + path
fmt.Println(path)
_, err := h.staticFS.Open(path) _, err := h.staticFS.Open(path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
fmt.Println("ERR")
// file does not exist, serve index.html // file does not exist, serve index.html
fmt.Println(path)
file, err := h.staticFS.Open(h.staticPath + "/" + h.indexPath) file, err := h.staticFS.Open(h.staticPath + "/" + h.indexPath)
if err != nil { if err != nil {
h.server.notFound(w) h.server.notFound(w)

View File

@ -4,11 +4,14 @@ import (
"fmt" "fmt"
"os/exec" "os/exec"
"runtime" "runtime"
"strings"
) )
func OpenBrowser(url string) { func OpenBrowser(url string) {
var err error var err error
fmt.Println()
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
err = exec.Command("xdg-open", url).Start() err = exec.Command("xdg-open", url).Start()
@ -21,7 +24,13 @@ func OpenBrowser(url string) {
} }
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
fmt.Println("navigate to " + url + " in your browser")
} }
s := "If your web browser doesn't open automatically, navigate to " + url + " manually"
t := strings.Repeat("=", len(s))
fmt.Println()
fmt.Println(t)
fmt.Println(s)
fmt.Println(t)
} }

View File

@ -154,6 +154,8 @@
{{ $active = ""}}{{- end}} {{ $active = ""}}{{- end}}
</div> </div>
</div> </div>
{{ if world.MapReady }}
{{- if gt (len .Sites) 0 }} {{- if gt (len .Sites) 0 }}
<div class="page-map"> <div class="page-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
@ -164,6 +166,7 @@
{{- end }} {{- end }}
</div> </div>
{{- end }} {{- end }}
{{- end }}
</div> </div>

View File

@ -16,6 +16,7 @@
{{- end }} {{- end }}
</ul> </ul>
{{ if world.MapReady }}
<div id="map" style="height: 400px"></div> <div id="map" style="height: 400px"></div>
{{initMap}} {{initMap}}
{{- range $race, $civs := .Civilizations }} {{- range $race, $civs := .Civilizations }}
@ -25,5 +26,6 @@
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }}
{{- end }} {{- end }}

View File

@ -3,12 +3,14 @@
{{define "title"}}{{ title .Name }}{{end}} {{define "title"}}{{ title .Name }}{{end}}
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div class="object-map"> <div class="object-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
</div> </div>
{{initMap}} {{initMap}}
{{ addLandmass .Id }} {{ addLandmass .Id }}
<script>map.addLayer(landmassesLayer);</script> <script>map.addLayer(landmassesLayer);</script>
{{- end }}
<h1>{{ title .Name }}</h1> <h1>{{ title .Name }}</h1>
<p>landmass</p> <p>landmass</p>

View File

@ -24,18 +24,19 @@
<body> <body>
<nav class="navbar navbar-expand-lg navbar-light bg-light mb-4"> <nav class="navbar navbar-expand-lg navbar-light bg-light mb-4">
<div class="container"> <div class="container">
<a class="navbar-brand" href="#">Legends Browser</a> <a class="navbar-brand" href="#" style="font-weight: 500;">Legends Browser</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span> <span class="navbar-toggler-icon"></span>
</button> </button>
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
{{ if world }}
<ul class="navbar-nav me-auto mb-2 mb-lg-0"> <ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="./">Civilizations</a> <a class="nav-link" href="./">Civilizations</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="./worldmap">World Map</a> <a class="nav-link{{if not world.MapReady}} disabled{{end}}" href="./worldmap">World Map</a>
</li> </li>
<li class="nav-item dropdown"> <li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown"
@ -77,6 +78,7 @@
onSelectItem: ({ label, value }) => window.location = "." + value onSelectItem: ({ label, value }) => window.location = "." + value
}); });
</script> </script>
{{ end }}
</div> </div>
</div> </div>
</nav> </nav>

View File

@ -3,11 +3,14 @@
{{define "title"}}{{ title .Name }}{{end}} {{define "title"}}{{ title .Name }}{{end}}
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div class="object-map"> <div class="object-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
</div> </div>
{{initMap}} {{initMap}}
{{ addMountain .Id false }} {{ addMountain .Id false }}
{{- end }}
<h3>{{ title .Name }}</h3> <h3>{{ title .Name }}</h3>
<p>{{ if .IsVolcano }}volcano{{else}}mountain{{end}}</p> <p>{{ if .IsVolcano }}volcano{{else}}mountain{{end}}</p>

View File

@ -3,12 +3,15 @@
{{define "title"}}{{ title .Name }}{{end}} {{define "title"}}{{ title .Name }}{{end}}
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div class="object-map"> <div class="object-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
</div> </div>
{{initMap}} {{initMap}}
{{ addRegion .Id }} {{ addRegion .Id }}
<script>map.addLayer(regionsLayer);</script> <script>map.addLayer(regionsLayer);</script>
{{- end }}
<h3>{{ title .Name }}</h3> <h3>{{ title .Name }}</h3>
<p>{{ .Type }}</p> <p>{{ .Type }}</p>

View File

@ -63,12 +63,14 @@
</div> </div>
</div> </div>
</div> </div>
{{ if world.MapReady }}
<div class="page-map"> <div class="page-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
<!-- <img class="site-map" src="$suburi/sitemap/$site.id" width="300" /> --> <!-- <img class="site-map" src="$suburi/sitemap/$site.id" width="300" /> -->
{{initMap}} {{initMap}}
{{ addSite .Id false }} {{ addSite .Id false }}
</div> </div>
{{- end }}
</div> </div>
<h5 class="mt-3">Events</h5> <h5 class="mt-3">Events</h5>

View File

@ -3,11 +3,14 @@
{{define "title"}}{{ title .Name }}{{end}} {{define "title"}}{{ title .Name }}{{end}}
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div class="object-map"> <div class="object-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
</div> </div>
{{initMap}} {{initMap}}
{{ addSite .SiteId false }} {{ addSite .SiteId false }}
{{- end }}
<h3>{{ title .Name }}</h3> <h3>{{ title .Name }}</h3>
{{ .Type_ }} in {{ site .SiteId }} {{ .Type_ }} in {{ site .SiteId }}

View File

@ -4,6 +4,7 @@
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div id="map" style="width: 100%; height: 1000px"></div> <div id="map" style="width: 100%; height: 1000px"></div>
{{initMap}} {{initMap}}
<script>L.control.layers(null, overlayMaps).addTo(map);</script> <script>L.control.layers(null, overlayMaps).addTo(map);</script>
@ -30,5 +31,8 @@
{{- range $id, $r := .Rivers }} {{- range $id, $r := .Rivers }}
{{ addRiver $id }} {{ addRiver $id }}
{{- end }} {{- end }}
{{ else }}
No map data available
{{- end }}
{{- end }} {{- end }}

View File

@ -3,11 +3,14 @@
{{define "title"}}{{ title .Name }}{{end}} {{define "title"}}{{ title .Name }}{{end}}
{{define "content"}} {{define "content"}}
{{ if world.MapReady }}
<div class="object-map"> <div class="object-map">
<div id="map" style="width: 300px; height: 300px"></div> <div id="map" style="width: 300px; height: 300px"></div>
</div> </div>
{{initMap}} {{initMap}}
{{ addWorldConstruction .Id }} {{ addWorldConstruction .Id }}
{{- end }}
<h3>{{ title .Name }}</h3> <h3>{{ title .Name }}</h3>
<p>{{ .Type }}</p> <p>{{ .Type }}</p>