mirror of
https://github.com/cwinfo/yggdrasil-go.git
synced 2024-11-10 07:30:27 +00:00
Merge pull request #129 from neilalexander/utf16
Convert config from UTF-16 if a BOM is found
This commit is contained in:
commit
5fc4dddf83
18
yggdrasil.go
18
yggdrasil.go
@ -1,7 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "encoding/json"
|
import "bytes"
|
||||||
import "encoding/hex"
|
import "encoding/hex"
|
||||||
|
import "encoding/json"
|
||||||
import "flag"
|
import "flag"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "io/ioutil"
|
import "io/ioutil"
|
||||||
@ -13,6 +14,8 @@ import "regexp"
|
|||||||
import "math/rand"
|
import "math/rand"
|
||||||
import "log"
|
import "log"
|
||||||
|
|
||||||
|
import "golang.org/x/text/encoding/unicode"
|
||||||
|
|
||||||
import "yggdrasil"
|
import "yggdrasil"
|
||||||
import "yggdrasil/config"
|
import "yggdrasil/config"
|
||||||
|
|
||||||
@ -107,6 +110,19 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
// If there's a byte order mark - which Windows 10 is now incredibly fond of
|
||||||
|
// throwing everywhere when it's converting things into UTF-16 for the hell
|
||||||
|
// of it - remove it and decode back down into UTF-8. This is necessary
|
||||||
|
// because hjson doesn't know what to do with UTF-16 and will panic
|
||||||
|
if bytes.Compare(config[0:2], []byte{0xFF, 0xFE}) == 0 ||
|
||||||
|
bytes.Compare(config[0:2], []byte{0xFF, 0xFF}) == 0 {
|
||||||
|
utf := unicode.UTF16(unicode.BigEndian, unicode.UseBOM)
|
||||||
|
decoder := utf.NewDecoder()
|
||||||
|
config, err = decoder.Bytes(config)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
// Generate a new configuration - this gives us a set of sane defaults -
|
// Generate a new configuration - this gives us a set of sane defaults -
|
||||||
// then parse the configuration we loaded above on top of it. The effect
|
// then parse the configuration we loaded above on top of it. The effect
|
||||||
// of this is that any configuration item that is missing from the provided
|
// of this is that any configuration item that is missing from the provided
|
||||||
|
Loading…
Reference in New Issue
Block a user