Skip to content

Commit

Permalink
adding features and make the code prettier
Browse files Browse the repository at this point in the history
- we now have flags
- we now can just simply get the converted UUID without any file altering involved
- we now have a readme, kind of
  • Loading branch information
noxifoxi committed Apr 29, 2020
1 parent 3cedbef commit 26a79bb
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 37 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
# minecraft-UUID-coverter
Written in hatred. Converts Minecraft UUID into the 1.16 int-array format.
Converts Minecraft UUID into the 1.16 int-array format.

I will update this in the future to make it actually usable for more situations
## Usage:
```cmd
uuidc
```
Scans the current directory looking for uuids in files (don't do this if you don't know what you are doing)
**Notice to me**: Stop making this the default behaviour, this is very bad for people who don't rtfm...

```cmd
uuidc <UUID>
```
Convert the UUID and prints the converted int array

```cmd
uuidc -uuid=<UUID>
```
Same as above but you have to write more

```cmd
uuidc <-flags>
```
Flags:
- `-uuid` - look above
- `-help` - no
- `-file=<file>` - specify a file to change
- `-dir=<directory>` - specify a directory to scan
- `-ext=<extension>` - only include files with that extension (include the .)
- `-r` - scan directory recursive
- `-simulate` - don't do anything, just shows you what would be done
114 changes: 79 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
Expand All @@ -12,59 +13,106 @@ import (
"strings"
)

// UUIDRegex is the regex to find UUIDs
const UUIDRegex = `([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})`

// TrUUIDRegex is the simple regex to find trimmed UUIDs
const TrUUIDRegex = `([0-9a-f]{32})`

func main() {
// current directory or the first argument
root := "."
if len(os.Args) > 1 {
root = os.Args[1]
// yey flags
uuidP := flag.String("uuid", "", "UUID to convert")
helpP := flag.Bool("help", false, "help?")
fileP := flag.String("file", "", "Specify a file to change")
dirP := flag.String("dir", ".", "Directory to scan for files")
extP := flag.String("ext", "", "Only scan these extensions")
recursiveP := flag.Bool("r", false, "Scan directory recursive")
simulateP := flag.Bool("simulate", false, "Don't change files, only simulate the process")
flag.Parse()

if *helpP {
println("this unholy place has no help")
return
}

// just walk through all files and alter them
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
// do have to deal with arguments?
if len(os.Args) > 1 {
var r *regexp.Regexp

var uuid *string = &os.Args[1]
if *uuidP != "" {
uuid = uuidP
}

if filepath.Ext(path) == ".json" {
alterFile(path)
if matched, _ := regexp.MatchString(UUIDRegex, *uuid); matched {
r = regexp.MustCompile(UUIDRegex) // UUID
} else if matched, _ := regexp.MatchString(TrUUIDRegex, *uuid); matched {
r = regexp.MustCompile(TrUUIDRegex) // Trimmed UUID
}

if r != nil {
// return the converted UUID
intArray, _ := convertUUID(r.FindStringSubmatch(*uuid)[0])
fmt.Println(stringifyArray(intArray))
return
}
}

if *fileP != "" {
alterFile(*fileP, *simulateP)
} else {
// just walk through all files and alter them
err := filepath.Walk(*dirP, func(path string, info os.FileInfo, err error) error {
if info.Name() == ".git" || (!*recursiveP && path != "." && info.IsDir()) {
return filepath.SkipDir
}

if info.IsDir() || filepath.Ext(path) == ".exe" {
return nil
}

if *extP == "" || filepath.Ext(path) == *extP {
alterFile(path, *simulateP)
}
return nil
})
if err != nil {
panic(err)
}
return nil
})
if err != nil {
panic(err)
}
}

func alterFile(file string) {
re := regexp.MustCompile(`SkullOwner:\{Id:\\"([0-9a-f-]{36})\\"`)
func alterFile(file string, simulate bool) {
re := regexp.MustCompile(UUIDRegex)

// don't pass a wrong formatted file
json, err := ioutil.ReadFile(file)
rFile, err := ioutil.ReadFile(file)

if err != nil {
log.Fatal(err)
return
}

// skip file if no match was found
if len(re.FindSubmatch(json)) < 2 {
if len(re.FindSubmatch(rFile)) < 2 {
fmt.Println(file + " skipped.")
return
}

intArray, err := convertUUID(string(re.FindSubmatch(json)[1]))
if err != nil {
panic(err)
}

// replace old UUID with int_array format
json = re.ReplaceAll(json, []byte("SkullOwner:{Id:"+parseUUIDArray(intArray)))
// replace UUID with int_array format
rFile = re.ReplaceAllFunc(rFile, func(match []byte) []byte {
intArray, _ := convertUUID(string(re.FindSubmatch(match)[1]))
return []byte(stringifyArray(intArray))
})

fmt.Print(file + " updated.")
fmt.Println(file + " updated.")

// your original file is now rip
ioutil.WriteFile(file, json, 0666)
if !simulate {
// your original file is now rip
ioutil.WriteFile(file, rFile, 0666)
}
}

func parseUUIDArray(uuid [4]int32) string {
func stringifyArray(uuid [4]int32) string {
parsed := "[I;"

for i, n := range uuid {
Expand Down Expand Up @@ -92,11 +140,7 @@ func convertUUID(uuid string) ([4]int32, error) {
slice := uuid[i*8 : i*8+8]

// convert hex to int64 because stupid
n, err := strconv.ParseInt(slice, 16, 64)
if err != nil {
// fuck you
return uuidIntArray, err
}
n, _ := strconv.ParseInt(slice, 16, 64)

// convert int64 to int32 because stupid
uuidIntArray[i] = int32(n)
Expand Down

0 comments on commit 26a79bb

Please sign in to comment.