-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.go
266 lines (214 loc) · 5.64 KB
/
package.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package unitypackage
import (
"crypto/md5"
_ "embed"
"encoding/hex"
"errors"
"gopkg.in/yaml.v3"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)
// metaFile the asset metafile
type metaFile struct {
Guid string `yaml:"guid"` // a unique identifier for the asset
Path string `yaml:"-"` // the asset path
MetaPath string `yaml:"-"` // the asset metafile path
}
const DefaultUnityRootPath = "Assets"
// getAssetsRootPath get Assets path from unpackage path
func getAssetsRootPath(path string) string {
return filepath.Join(path, DefaultUnityRootPath)
}
// preProcessFilesInPath preprocesses the assets at the given assets root directory.
func preprocessFilesInPath(assetsRoot, relPath string) error {
assetPath := filepath.Join(assetsRoot, relPath)
err := filepath.Walk(assetPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if path == assetPath {
return processFile(assetsRoot, relPath)
}
if info.IsDir() {
return nil
}
childRelativePath, err := filepath.Rel(assetsRoot, path)
if err != nil {
return err
}
if strings.HasSuffix(assetsRoot, path) {
return nil
}
return processFile(assetsRoot, childRelativePath)
})
return err
}
func processFile(assetsRoot, relativeFilePath string) error {
fullFilePath := filepath.Join(assetsRoot, relativeFilePath)
fullMetaFilePath := getAssetMetaPath(fullFilePath)
_, err := os.Stat(fullMetaFilePath)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
if errors.Is(err, os.ErrNotExist) {
return generateMetafile(fullMetaFilePath, relativeFilePath)
}
return nil
}
func getAssetMetaPath(filePath string) string {
if strings.HasSuffix(filePath, ".meta") {
return filePath
}
return filePath + ".meta"
}
//go:embed metafile_template.yaml
var metafileTemplate []byte
// // generateMetafile generates a metafile for the given file.
func generateMetafile(fullMetaFilePath, relativeFilePath string) error {
metafile := make([]byte, len(metafileTemplate))
copy(metafile, metafileTemplate)
contents := string(metafile)
contents = strings.ReplaceAll(contents, "{guid}", getDeterministicGuid(relativeFilePath))
return os.WriteFile(fullMetaFilePath, []byte(contents), 0755)
}
func getDeterministicGuid(relativeFilePath string) string {
hash := md5.Sum([]byte(relativeFilePath))
return hex.EncodeToString(hash[:])
}
func GeneratePackage(assetsRoot, outputPath string) error {
assetsRoot = getAssetsRootPath(assetsRoot)
err := preprocessFilesInPath(assetsRoot, ".")
if err != nil {
return err
}
outputPath = filepath.Clean(outputPath)
if err := os.MkdirAll(filepath.Dir(outputPath), 0755); err != nil {
return err
}
assets, err := collectAssetsInPath(assetsRoot)
if err != nil {
return err
}
tempDir, err := os.MkdirTemp("", "temp")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
localBaseName := filepath.Base(assetsRoot)
for _, asset := range assets {
if strings.HasSuffix(asset.Path, ".meta") {
continue
}
assetDir := filepath.Join(tempDir, asset.Guid)
assetPath, metaPath, pathNamePath := prepareAssetPaths(assetDir)
pathNameLocal := strings.ReplaceAll(asset.Path, assetsRoot, "")
pathNameLocal = strings.ReplaceAll(pathNameLocal, ".meta", "")
pathNameLocal = strings.TrimPrefix(pathNameLocal, "/")
pathNameLocal = filepath.Join(localBaseName, pathNameLocal)
pathNameLocal = strings.ReplaceAll(pathNameLocal, "\\", "/")
if isDir(assetDir) {
err = os.RemoveAll(assetDir)
if err != nil {
return err
}
}
err = os.MkdirAll(assetDir, 0777)
if err != nil {
return err
}
if !isDir(asset.Path) {
if err = copyFile(asset.Path, assetPath); err != nil {
return err
}
}
if err = copyFile(asset.MetaPath, metaPath); err != nil {
return err
}
if err = os.WriteFile(pathNamePath, []byte(pathNameLocal), 0777); err != nil {
return err
}
}
if err = tarGz(outputPath, tempDir); err != nil {
return err
}
return nil
}
// prepareAssetPaths prepares file paths for the asset's content.
func prepareAssetPaths(assetDir string) (assetPath, metaPath, pathNamePath string) {
assetPath = filepath.Join(assetDir, "asset")
metaPath = filepath.Join(assetDir, "asset.meta")
pathNamePath = filepath.Join(assetDir, "pathname")
return
}
func isDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
func collectAssetsInPath(assetPath string) ([]metaFile, error) {
assets := make([]metaFile, 0)
err := filepath.Walk(assetPath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
assetRef, err := getAssetReference(path)
if err == nil && assetRef != nil {
assets = append(assets, *assetRef)
}
}
return nil
})
if err != nil {
return nil, err
}
return assets, nil
}
func getAssetReference(filePath string) (*metaFile, error) {
metaFilePath := getAssetMetaPath(filePath)
info, err := os.Stat(metaFilePath)
if err != nil {
return nil, err
}
if info.IsDir() {
return nil, nil
} else {
content, err := os.ReadFile(metaFilePath)
if err != nil {
return nil, err
}
res := metaFile{
Path: filePath,
MetaPath: metaFilePath,
}
err = yaml.Unmarshal(content, &res)
if err != nil {
return nil, err
}
return &res, nil
}
}
// copyFile the src file to dst. Any existing file will be overwritten and will not
// copy file attributes.
func copyFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}