rbxcompiler/internal/rbxbuilder/util.go

119 lines
3.0 KiB
Go
Raw Normal View History

2020-09-29 20:58:00 +00:00
package rbxbuilder
import (
2021-08-11 00:16:40 +00:00
"git.itzana.me/itzaname/rbxfile"
2020-09-29 20:58:00 +00:00
"github.com/google/uuid"
"io/ioutil"
"os"
"strings"
)
var seperator = string(os.PathSeparator)
func isScript(instance *rbxfile.Instance) bool {
var scriptTypes = []string{"LocalScript", "ModuleScript", "Script"}
for i := 0; i < len(scriptTypes); i++ {
if instance.ClassName == scriptTypes[i] {
return true
}
}
return false
}
func isFolder(instance *rbxfile.Instance) bool {
return instance.ClassName == "Folder"
}
func getSource(instance *rbxfile.Instance) string {
if source, ok := instance.Properties["Source"]; ok {
return source.String()
}
return "BUILD ERROR: FAILED TO LOAD SOURCE"
}
func getPath(instance *rbxfile.Instance) []string {
if instance == nil {
return []string{}
}
return append(getPath(instance.Parent()), instance.Name())
}
func getPathString(instance *rbxfile.Instance) string {
return seperator + strings.Join(getPath(instance), seperator)
}
func getScriptFilePath(instance *rbxfile.Instance) []string {
path := getPath(instance)
if len(instance.Children) > 0 {
path = append(path, instance.Name())
}
if len(path) > 0 {
path[len(path)-1] += ".lua"
}
return path
}
func getScriptFilePathString(instance *rbxfile.Instance) string {
return seperator + strings.Join(getScriptFilePath(instance), seperator)
}
func instanceFromScriptPath(root *rbxfile.Root, path string) *rbxfile.Instance {
paths := strings.Split(strings.TrimPrefix(strings.TrimSuffix(path, ".lua"), seperator), seperator)
if len(path) == 0 {
return nil
}
for i := 0; i < len(root.Instances); i++ {
instance := root.Instances[i]
if instance.Name() != paths[0] {
continue
}
for j := 0; j < len(paths); j++ {
if instance.Name() == paths[j] {
if len(paths)-1 == j {
return instance
}
}
for _, child := range instance.Children {
if child.Name() == paths[j] {
if len(paths)-1 == j {
return child
}
instance = child
break
}
}
}
}
return nil
}
func loadScript(name string, class string, path string) *rbxfile.Instance {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil
}
script := rbxfile.NewInstance(class, nil)
script.Properties["AttributesSerialize"] = rbxfile.NewValue(rbxfile.TypeBinaryString)
script.Properties["LinkedSource"] = rbxfile.NewValue(rbxfile.TypeContent)
script.Properties["Name"] = rbxfile.ValueString(name)
script.Properties["ScriptGuid"] = rbxfile.ValueString("{" + uuid.New().String() + "}")
script.Properties["Source"] = rbxfile.ValueProtectedString(data)
script.Properties["Tags"] = rbxfile.ValueBinaryString("")
return script
}
func generateFolder(name string) *rbxfile.Instance {
script := rbxfile.NewInstance("Folder", nil)
script.Properties["AttributesSerialize"] = rbxfile.NewValue(rbxfile.TypeBinaryString)
script.Properties["Name"] = rbxfile.ValueString(name)
script.Properties["SourceAssetId"] = rbxfile.ValueInt64(-1)
script.Properties["Tags"] = rbxfile.ValueBinaryString("")
return script
}