Updates and Features

This commit is contained in:
Raum0x2A
2021-05-19 21:23:39 +00:00
parent a370e0f33d
commit 60de0ebb46
10 changed files with 191 additions and 101 deletions

View File

@@ -8,6 +8,8 @@ NMSlib is a GoLang package for dealing with converting Galactic coordinates to P
NMSlib also translates in game languages (Korvax, Gek Vy'Keen and Atlas) to english, and create portal banners. NMSlib also translates in game languages (Korvax, Gek Vy'Keen and Atlas) to english, and create portal banners.
[GoDoc](https://pkg.go.dev/gitlab.com/Raum0x2A/nmslib#section-documentation)
## Installation ## Installation
To install this module use `go get` To install this module use `go get`

View File

@@ -1,3 +1,8 @@
/*
Xainesworld Video: https://www.youtube.com/watch?v=xmZbkTahw4w
Fandom Wiki: https://nomanssky.fandom.com/wiki/Portal_address
*/
package nmslib package nmslib
import ( import (
@@ -7,18 +12,11 @@ import (
"strings" "strings"
) )
/*
Xainesworld Video: https://www.youtube.com/watch?v=xmZbkTahw4w
Fandom Wiki: https://nomanssky.fandom.com/wiki/Portal_address
*/
/* /*
P2gc - Portal code to galactic coordinates P2gc - Portal code to galactic coordinates
Requires 1 var and returns 1 var string and an error Requires 1 var and returns 1 var string and an error
var p string var p string: Portal Glyph hex string 12 chars in len (ex. 006afa556c30)
Portal Glyph hex string 12 chars in len (ex. 006afa556c30) Returns var string: Galactic address (ex. 042F:0079:0D55:006A)
Return var string
Galactic address (ex. 042F:0079:0D55:006A)
*/ */
func P2gc(p string) (gc string, err error) { func P2gc(p string) (gc string, err error) {
if len(p) == 12 { // Test if length of provided string is 12 chars long if len(p) == 12 { // Test if length of provided string is 12 chars long
@@ -68,12 +66,9 @@ func P2gc(p string) (gc string, err error) {
/* /*
Gc2p - Galactic coordinates to portal code Gc2p - Galactic coordinates to portal code
Requires 1 string and 1 int returns a string and error Requires 1 string and 1 int returns a string and error
var gc string var gc string: Galactic address (ex. 042F:0079:0D55:006A)
Galactic address (ex. 042F:0079:0D55:006A) var p int: Planet ID [1-6]
var p int Returns portalcode string: Portal Glyph hex string 12 chars in len (ex. 006afa556c30)
Planet ID [1-6]
Return portalcode string
Portal Glyph hex string 12 chars in len (ex. 006afa556c30)
*/ */
func Gc2p(gc string, p int) (portalcode string, err error) { func Gc2p(gc string, p int) (portalcode string, err error) {
// split and store string // split and store string
@@ -114,6 +109,10 @@ func Gc2p(gc string, p int) (portalcode string, err error) {
} }
// Apply shifts to Handle the shape/boundaries of the galaxy // Apply shifts to Handle the shape/boundaries of the galaxy
/*Note:
[P][SSS][YY][ZZZ][XXX] len == 12
* SSS == SSI
*/
hexCoords[2] = hexCoords[2] + 0x81 // Y ->> shift hexCoords[2] = hexCoords[2] + 0x81 // Y ->> shift
hexCoords[3] = hexCoords[3] + 0x801 // Z ->> shift hexCoords[3] = hexCoords[3] + 0x801 // Z ->> shift
hexCoords[4] = hexCoords[4] + 0x801 // X ->> shift hexCoords[4] = hexCoords[4] + 0x801 // X ->> shift
@@ -125,6 +124,7 @@ func Gc2p(gc string, p int) (portalcode string, err error) {
} }
// Assemble padded values as a string // Assemble padded values as a string
portalcode = fmt.Sprintf("%00X%03X", hexCoords[0], hexCoords[1]) portalcode = fmt.Sprintf("%00X%03X", hexCoords[0], hexCoords[1])
for n := 2; n < len(hexCoords); n++ { for n := 2; n < len(hexCoords); n++ {
if n == 2 { if n == 2 {

View File

@@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"image" "image"
"io/ioutil" "io/ioutil"
"os"
"strconv" "strconv"
"github.com/fogleman/gg" "github.com/fogleman/gg"
@@ -13,10 +12,8 @@ import (
/*CreateBanner - Output PNG of Portal Glyphs /*CreateBanner - Output PNG of Portal Glyphs
Requires 3 vars and returns 1 err, Outputs a PNG file Requires 3 vars and returns 1 err, Outputs a PNG file
var portalex string var portalex string: Portal Glyph hex string
Portal Glyph hex string var savename string: Output name of PNG file
var savename string
Output name of PNG file
var opt int var opt int
00 sets horizontal banner in light mode [ 00 || 0 ] 00 sets horizontal banner in light mode [ 00 || 0 ]
01 sets horizontal banner in dark mode [ 01 || 1 ] 01 sets horizontal banner in dark mode [ 01 || 1 ]
@@ -31,14 +28,6 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) {
var mode string var mode string
var imgArray [12]image.Image var imgArray [12]image.Image
// Setup temp dir
tempdir, err := ioutil.TempDir("", "nmslib-")
if err != nil {
return errors.New("can not create temp directory")
}
// don't forget to clean up afterwords
defer os.RemoveAll(tempdir)
//Set light or dark mode glyphs //Set light or dark mode glyphs
if opt == 00 || opt == 10 { if opt == 00 || opt == 10 {
mode = "light" mode = "light"
@@ -71,14 +60,7 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) {
if err != nil { if err != nil {
return errors.New("can not load images from assets: " + glyphImg[k]) return errors.New("can not load images from assets: " + glyphImg[k])
} }
_, err2 := os.Stat(tempdir + "/assets/glyphs/" + mode + "/") err = ioutil.WriteFile(NmsTemp+"/"+glyphImg[k], []byte(data), 0644)
if os.IsNotExist(err2) {
errDir := os.MkdirAll(tempdir+"/assets/glyphs/"+mode, 0755)
if errDir != nil {
return errors.New("can not create temp directory")
}
}
err = ioutil.WriteFile(tempdir+"/"+glyphImg[k], []byte(data), 0644)
if err != nil { if err != nil {
return errors.New("can not write file to temp directory") return errors.New("can not write file to temp directory")
} }
@@ -86,7 +68,7 @@ func CreateBanner(portalhex string, savename string, opt int) (err error) {
//Load/open images needed //Load/open images needed
for iter := 0; iter < 12; iter++ { for iter := 0; iter < 12; iter++ {
imgArray[iter], err = gg.LoadPNG(tempdir + "/" + glyphImg[iter]) imgArray[iter], err = gg.LoadPNG(NmsTemp + "/" + glyphImg[iter])
if err != nil { if err != nil {
return errors.New("can not read glyph " + glyphImg[iter]) return errors.New("can not read glyph " + glyphImg[iter])
} }

71
init.go Normal file
View File

@@ -0,0 +1,71 @@
package nmslib
import (
"errors"
"io/ioutil"
"os"
)
/*NmsTemp - Global string variable
Location of instance temporary directory
Dir Structure:
- NmsTemp
-
*/
var NmsTemp string
/*Initialize temporary directory
create a temp dir and save location to `NmsTemp` string
*/
func init() {
// create temp dir
temploc, err := ioutil.TempDir("", "nmslib-")
if err != nil {
panic(err)
}
NmsTemp = temploc
//Create directory structure
err = os.Mkdir(NmsTemp+"/assets", 0755)
if err != nil {
panic(errors.New("failed to create temporary folder"))
}
err = os.Mkdir(NmsTemp+"/assets/lang", 0755)
if err != nil {
panic(errors.New("failed to create temporary folder"))
}
err = os.Mkdir(NmsTemp+"/assets/glyphs", 0755)
if err != nil {
panic(errors.New("failed to create temporary folder"))
}
err = os.Mkdir(NmsTemp+"/assets/glyphs/dark", 0755)
if err != nil {
panic(errors.New("failed to create temporary folder"))
}
err = os.Mkdir(NmsTemp+"/assets/glyphs/light", 0755)
if err != nil {
panic(errors.New("failed to create temporary folder"))
}
//Write language files to temp directory
fileloc := [4]string{"atlas-lang.csv", "gek-lang.csv", "korvax-lang.csv", "vykeen-lang.csv"}
for x := 0; x < 4; x++ {
data, err := Asset("assets/lang/" + fileloc[x])
if err != nil {
panic(errors.New("can not load: " + fileloc[x]))
}
err = ioutil.WriteFile(NmsTemp+"/"+fileloc[x], []byte(data), 0644)
if err != nil {
panic(errors.New("can not write " + fileloc[x] + " to temp dir"))
}
}
}
/*CleanUp removes temporary directory an it's contents
this is intended to be called in a defer statement in func main
takes no input
*/
func CleanUp() {
os.RemoveAll(NmsTemp)
}

11
init_test.go Normal file
View File

@@ -0,0 +1,11 @@
package nmslib
import (
"fmt"
"testing"
)
func TestInit(t *testing.T) {
fmt.Printf("\nTesting init:\n")
fmt.Printf("Temporary directory: %s\n\n", NmsTemp)
}

87
lang.go
View File

@@ -3,13 +3,29 @@ package nmslib
import ( import (
"encoding/csv" "encoding/csv"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"strings" "strings"
) )
func readCSV(lagcsv string) [][]string {
fileloc := NmsTemp + "/" + lagcsv
csvFile, err := os.Open(fileloc)
if err != nil {
fmt.Println(err)
}
defer csvFile.Close() // close language file when finished
// read csv file from memory
csvlines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
fmt.Println(err)
}
return csvlines
}
/* /*
Translate words Create struct of Word string
*/ */
type Translate struct { type Translate struct {
Word string Word string
@@ -27,7 +43,7 @@ type trans0 struct {
Kor2Eng translate korvax word to english word Kor2Eng translate korvax word to english word
*/ */
func (t Translate) Kor2Eng() (returnstring string) { func (t Translate) Kor2Eng() (returnstring string) {
csvlines := openCSV("korvax-lang.csv") csvlines := readCSV("korvax-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toEng(t, csvlines)) returnstring = (toEng(t, csvlines))
} }
@@ -38,7 +54,7 @@ func (t Translate) Kor2Eng() (returnstring string) {
Eng2Kor translate english word to korvax word Eng2Kor translate english word to korvax word
*/ */
func (t Translate) Eng2Kor() (returnstring string) { func (t Translate) Eng2Kor() (returnstring string) {
csvlines := openCSV("korvax-lang.csv") csvlines := readCSV("korvax-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toNMS(t, csvlines)) returnstring = (toNMS(t, csvlines))
} }
@@ -49,7 +65,7 @@ func (t Translate) Eng2Kor() (returnstring string) {
Gek2Eng translate Gek word to english word Gek2Eng translate Gek word to english word
*/ */
func (t Translate) Gek2Eng() (returnstring string) { func (t Translate) Gek2Eng() (returnstring string) {
csvlines := openCSV("gek-lang.csv") csvlines := readCSV("gek-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toEng(t, csvlines)) returnstring = (toEng(t, csvlines))
} }
@@ -60,7 +76,7 @@ func (t Translate) Gek2Eng() (returnstring string) {
Eng2Gek translate english word to Gek word Eng2Gek translate english word to Gek word
*/ */
func (t Translate) Eng2Gek() (returnstring string) { func (t Translate) Eng2Gek() (returnstring string) {
csvlines := openCSV("gek-lang.csv") csvlines := readCSV("gek-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toNMS(t, csvlines)) returnstring = (toNMS(t, csvlines))
} }
@@ -68,10 +84,10 @@ func (t Translate) Eng2Gek() (returnstring string) {
} }
/* /*
Vyk2Eng translate vykeen word to english word Vyk2Eng translate Vy'keen word to english word
*/ */
func (t Translate) Vyk2Eng() (returnstring string) { func (t Translate) Vyk2Eng() (returnstring string) {
csvlines := openCSV("vykeen-lang.csv") csvlines := readCSV("vykeen-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toEng(t, csvlines)) returnstring = (toEng(t, csvlines))
} }
@@ -82,7 +98,7 @@ func (t Translate) Vyk2Eng() (returnstring string) {
Eng2Vyk translate english word to vykeen word Eng2Vyk translate english word to vykeen word
*/ */
func (t Translate) Eng2Vyk() (returnstring string) { func (t Translate) Eng2Vyk() (returnstring string) {
csvlines := openCSV("vykeen-lang.csv") csvlines := readCSV("vykeen-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toNMS(t, csvlines)) returnstring = (toNMS(t, csvlines))
} }
@@ -93,7 +109,7 @@ func (t Translate) Eng2Vyk() (returnstring string) {
Atl2Eng translate Atlas word to english word Atl2Eng translate Atlas word to english word
*/ */
func (t Translate) Atl2Eng() (returnstring string) { func (t Translate) Atl2Eng() (returnstring string) {
csvlines := openCSV("atlas-lang.csv") csvlines := readCSV("atlas-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toEng(t, csvlines)) returnstring = (toEng(t, csvlines))
} }
@@ -104,7 +120,7 @@ func (t Translate) Atl2Eng() (returnstring string) {
Eng2Atl translate english word to Atlas word Eng2Atl translate english word to Atlas word
*/ */
func (t Translate) Eng2Atl() (returnstring string) { func (t Translate) Eng2Atl() (returnstring string) {
csvlines := openCSV("atlas-lang.csv") csvlines := readCSV("atlas-lang.csv")
for range csvlines { for range csvlines {
returnstring = (toNMS(t, csvlines)) returnstring = (toNMS(t, csvlines))
} }
@@ -159,52 +175,3 @@ func toNMS(t Translate, csvlines [][]string) string {
} }
return returnstring return returnstring
} }
/*
func openCSV(lagcsv string) [][]string {
csvFile, err := os.Open("./assets/lang/" + lagcsv)
if err != nil {
fmt.Println(err)
}
defer csvFile.Close() // close language file when finished
// read csv file from memory
csvlines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
fmt.Println(err)
}
return csvlines
*/
func openCSV(lagcsv string) [][]string {
// create temp dir
tempdir, err := ioutil.TempDir("", "nmslib-")
if err != nil {
panic(err)
}
defer os.RemoveAll(tempdir) // Clean up temp files
// extract language file from resources.go
data, err := Asset("assets/lang/" + lagcsv)
if err != nil {
panic(err)
}
// wirte extracted data to temp dir
err = ioutil.WriteFile(tempdir+"/"+lagcsv, []byte(data), 0644)
if err != nil {
fmt.Println(err)
}
csvFile, err := os.Open(tempdir + "/" + lagcsv)
if err != nil {
fmt.Println(err)
}
defer csvFile.Close() // close language file when finished
// read csv file from memory
csvlines, err := csv.NewReader(csvFile).ReadAll()
if err != nil {
fmt.Println(err)
}
return csvlines
}

View File

@@ -23,8 +23,8 @@ func TestGalactic2portal(t *testing.T) {
fmt.Println(err) fmt.Println(err)
} }
if want != "21F2F8EDB94D" { if want != "21F2F8EDB94D" {
t.Errorf("\nTesting Gc2p(\"014C:0077:06DA:01F2\"): got %q, want: 21F2F8EDB94D.\n", want) t.Errorf("Testing Gc2p(\"014C:0077:06DA:01F2\"): got %q, want: 21F2F8EDB94D.\n", want)
} else { } else {
fmt.Printf("\nTesting Gc2p(\"014C:0077:06DA:01F2\"): got: %q, want: 21F2F8EDB94D.\n", want) fmt.Printf("Testing Gc2p(\"014C:0077:06DA:01F2\"): got: %q, want: 21F2F8EDB94D.\n", want)
} }
} }

View File

@@ -9,3 +9,13 @@ func TestRndPortal(t *testing.T) {
fmt.Printf("\nRandom Portal Code: ") fmt.Printf("\nRandom Portal Code: ")
fmt.Println(RndPortal()) fmt.Println(RndPortal())
} }
func TestRndAtlas(t *testing.T) {
fmt.Printf("semi-Random Atlas Code: ")
fmt.Println(RndAtlas())
}
func TestRndBH(t *testing.T) {
fmt.Printf("semi-Random black hole Code: ")
fmt.Println(RndBH())
}

View File

@@ -32,4 +32,6 @@ func TestTranslate(t *testing.T) {
fmt.Printf("Testing Translate{\"Alinichel\"}.Kor2Eng(): got %q, want: %q.\n", want.Kor2Eng(), "Emergency") fmt.Printf("Testing Translate{\"Alinichel\"}.Kor2Eng(): got %q, want: %q.\n", want.Kor2Eng(), "Emergency")
} }
fmt.Printf("\nTesting complete.\nStatus: ") fmt.Printf("\nTesting complete.\nStatus: ")
CleanUp()
} }

49
rpcg.go
View File

@@ -25,8 +25,7 @@ import (
/* /*
RndPortal - Random Portalcode generator RndPortal - Random Portalcode generator
Requires no input required Requires no input required and returns a string
Returns a string
*/ */
func RndPortal() (final string) { func RndPortal() (final string) {
@@ -45,3 +44,49 @@ func RndPortal() (final string) {
return return
} }
/*RndAtlas genorates a semi-random atlas code
Requires no input and returns a string
This is not a true random atlas genorator. Insted this will create a Planet id of 1, Solar System Index of 0x07A and random Y, Z, X
SSI 0x07A always has an Atlas Interface.
REF: https://nomanssky.fandom.com/wiki/Portal_address#Solar_System_Index https://nomanssky.fandom.com/wiki/Star_system#Classification
*/
func RndAtlas() (final string) {
rand.Seed(time.Now().UnixNano())
min := 0x1
ymax := 0xFF
xzmax := 0xFFF
x := rand.Intn(xzmax-min+1) + min
z := rand.Intn(xzmax-min+1) + min
y := rand.Intn(ymax-min+1) + min
final = fmt.Sprintf("%00X%03X%02X%03X%03X", 1, 0x07A, y, z, x)
return
}
/*RndBH genorates a semi-random atlas code
Requires no input required and returns a string
This is not a true random atlas genorator. Insted this will create a Planet id of 1, Solar System Index of 0x079 and random Y, Z, X
SSI 0x079 always has a black hole.
REF: https://nomanssky.fandom.com/wiki/Portal_address#Solar_System_Index https://nomanssky.fandom.com/wiki/Star_system#Classification
*/
func RndBH() (final string) {
rand.Seed(time.Now().UnixNano())
min := 0x1
ymax := 0xFF
xzmax := 0xFFF
x := rand.Intn(xzmax-min+1) + min
z := rand.Intn(xzmax-min+1) + min
y := rand.Intn(ymax-min+1) + min
final = fmt.Sprintf("%00X%03X%02X%03X%03X", 1, 0x079, y, z, x)
return
}