Add option to generate C source

This commit is contained in:
Milan Nikolic 2017-11-27 15:08:20 +01:00
parent 31a41399ad
commit 4101a15c47
2 changed files with 58 additions and 0 deletions

View file

@ -18,6 +18,8 @@ Usage of ./rrem:
Generate C header (.h file)
-key string
Encryption key
-source
Generate C source (.c file)
```
### Example

View file

@ -53,6 +53,7 @@ func main() {
enc := flag.Int("enc", rres.CryptoNone, "Encryption type, 0=NONE, 1=XOR, 2=AES, 3=3DES, 4=Blowfish, 5=XTEA")
key := flag.String("key", "", "Encryption key")
header := flag.Bool("header", false, "Generate C header (.h file)")
source := flag.Bool("source", false, "Generate C source (.c file)")
bin := flag.Bool("bin", false, "Generate Go bindata (.go file)")
flag.Parse()
@ -114,6 +115,17 @@ func main() {
defer headerFile.Close()
}
var sourceFile *os.File
if *source {
sourceFile, err = os.Create(fmt.Sprintf("%s.c", *base))
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
defer sourceFile.Close()
}
var fileHeader rres.FileHeader
// "rRES" identifier
@ -390,6 +402,50 @@ func main() {
}
}
// Generate C source
if *source {
rresFile.Seek(0, 0)
d, err := ioutil.ReadAll(rresFile)
if err != nil {
fmt.Printf("%v\n", err)
}
_, err = sourceFile.Write([]byte("// This file has been automatically generated by rREM - raylib Resource Embedder\n\n"))
if err != nil {
fmt.Printf("%v\n", err)
}
_, err = sourceFile.Write([]byte(fmt.Sprintf("const unsigned char data[%d] = {\n ", len(d))))
if err != nil {
fmt.Printf("%v\n", err)
}
blCounter := 0 // break line counter
for i := 0; i < len(d)-1; i++ {
blCounter++
_, err = sourceFile.Write([]byte(fmt.Sprintf("0x%.2x, ", d[i])))
if err != nil {
fmt.Printf("%v\n", err)
}
if blCounter >= 24 {
_, err = sourceFile.Write([]byte("\n "))
if err != nil {
fmt.Printf("%v\n", err)
}
blCounter = 0
}
}
_, err = sourceFile.Write([]byte(fmt.Sprintf("0x%.2x };\n", d[len(d)-1])))
if err != nil {
fmt.Printf("%v\n", err)
}
}
// Generate bindata
if *bin {
cfg := bindata.NewConfig()