1225
|
1 type
|
|
2 ResourceBundlingType = enum
|
|
3 Dir # Directories
|
|
4 Zip # Zip files
|
|
5 Exe # Embeded in executable
|
|
6
|
|
7 const
|
|
8 thebundletype = parseEnum[ResourceBundlingType](PACKAGETYPE.toLowerAscii().capitalizeAscii())
|
|
9 ASCII_CHARSET = PrintableChars.toSeq.toRunes
|
|
10 DEFAULT_PACKAGE = "default"
|
|
11
|
|
12 # resource loading
|
|
13
|
|
14 func normalizeDir(dir: string): string =
|
|
15 result = dir
|
|
16 if result.startsWith("./"):
|
|
17 result = result[2 .. ^1]
|
|
18 if result.startsWith("/"):
|
|
19 result = result[1 .. ^1]
|
|
20 result = dir.replace('\\', '/')
|
|
21 if not result.endsWith("/") and result != "":
|
|
22 result = result & "/"
|
|
23
|
|
24 when thebundletype == Dir:
|
|
25
|
|
26 proc resourceRoot(): string =
|
|
27 getAppDir().absolutePath().joinPath(RESOURCEROOT)
|
|
28 proc packageRoot(package: string): string =
|
|
29 resourceRoot().joinPath(package)
|
|
30
|
|
31 proc loadResource_intern(path: string, package: string): Stream =
|
|
32 let realpath = package.packageRoot().joinPath(path)
|
|
33 if not realpath.fileExists():
|
|
34 raise newException(Exception, &"Resource {path} not found (checked {realpath})")
|
|
35 newFileStream(realpath, fmRead)
|
|
36
|
|
37 proc modList_intern(): seq[string] =
|
|
38 for kind, file in walkDir(resourceRoot(), relative = true):
|
|
39 if kind == pcDir:
|
|
40 result.add file
|
|
41
|
|
42 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
|
|
43 for file in walkDirRec(package.packageRoot().joinPath(dir), relative = true):
|
|
44 yield file
|
|
45
|
|
46 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
|
|
47 for i in walkDir(package.packageRoot().joinPath(dir), relative = true):
|
|
48 yield i
|
|
49
|
|
50 elif thebundletype == Zip:
|
|
51
|
|
52 import ./thirdparty/zippy/zippy/ziparchives
|
|
53
|
|
54 proc resourceRoot(): string =
|
|
55 absolutePath(getAppDir()).joinPath(RESOURCEROOT)
|
|
56 proc packageRoot(package: string): string =
|
|
57 resourceRoot().joinPath(package)
|
|
58
|
|
59 proc loadResource_intern(path: string, package: string): Stream =
|
|
60 let archive = openZipArchive(package.packageRoot() & ".zip")
|
|
61 try:
|
|
62 result = newStringStream(archive.extractFile(path))
|
|
63 except ZippyError:
|
|
64 raise newException(Exception, &"Resource {path} not found")
|
|
65 archive.close()
|
|
66
|
|
67 proc modList_intern(): seq[string] =
|
|
68 for kind, file in walkDir(resourceRoot(), relative = true):
|
|
69 if kind == pcFile and file.endsWith(".zip"):
|
|
70 result.add file[0 ..< ^4]
|
|
71
|
|
72 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
|
|
73 let archive = openZipArchive(package.packageRoot() & ".zip")
|
|
74 let normDir = dir.normalizeDir()
|
|
75 for i in archive.walkFiles:
|
|
76 if i.startsWith(normDir):
|
|
77 yield i
|
|
78 archive.close()
|
|
79
|
|
80 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
|
|
81 let archive = openZipArchive(package.packageRoot() & ".zip")
|
|
82 let normDir = dir.normalizeDir()
|
|
83 var yielded: HashSet[string]
|
|
84
|
|
85 for i in archive.walkFiles:
|
|
86 if i.startsWith(normDir):
|
|
87 let components = i[normDir.len .. ^1].split('/', maxsplit = 1)
|
|
88 if components.len == 1:
|
|
89 if not (components[0] in yielded):
|
|
90 yield (kind: pcFile, path: components[0])
|
|
91 else:
|
|
92 if not (components[0] in yielded):
|
|
93 yield (kind: pcDir, path: components[0])
|
|
94 yielded.incl components[0]
|
|
95 archive.close()
|
|
96
|
|
97 elif thebundletype == Exe:
|
|
98
|
|
99 const BUILD_RESOURCEROOT* {.strdefine.}: string = ""
|
|
100
|
|
101 proc loadResources(): Table[string, Table[string, string]] {.compileTime.} =
|
|
102 when BUILD_RESOURCEROOT == "":
|
|
103 {.warning: "BUILD_RESOURCEROOT is empty, no resources will be packaged".}
|
|
104 return
|
|
105 else:
|
|
106 for kind, packageDir in walkDir(BUILD_RESOURCEROOT):
|
|
107 if kind == pcDir:
|
|
108 let package = packageDir.splitPath.tail
|
|
109 result[package] = Table[string, string]()
|
|
110 for resourcefile in walkDirRec(packageDir, relative = true):
|
|
111 result[package][resourcefile.replace('\\', '/')] = staticRead(packageDir.joinPath(resourcefile))
|
|
112 const bundledResources = loadResources()
|
|
113
|
|
114 proc loadResource_intern(path: string, package: string): Stream =
|
|
115 if not (path in bundledResources[package]):
|
|
116 raise newException(Exception, &"Resource {path} not found")
|
|
117 newStringStream(bundledResources[package][path])
|
|
118
|
|
119 proc modList_intern(): seq[string] =
|
|
120 result = bundledResources.keys().toSeq()
|
|
121
|
|
122 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
|
|
123 for i in bundledResources[package].keys:
|
|
124 yield i
|
|
125
|
|
126 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
|
|
127 let normDir = dir.normalizeDir()
|
|
128 var yielded: HashSet[string]
|
|
129
|
|
130 for i in bundledResources[package].keys:
|
|
131 if i.startsWith(normDir):
|
|
132 let components = i[normDir.len .. ^1].split('/', maxsplit = 1)
|
|
133 if components.len == 1:
|
|
134 if not (components[0] in yielded):
|
|
135 yield (kind: pcFile, path: components[0])
|
|
136 else:
|
|
137 if not (components[0] in yielded):
|
|
138 yield (kind: pcDir, path: components[0])
|
|
139 yielded.incl components[0]
|
|
140
|
|
141 proc LoadResource*(path: string, package = DEFAULT_PACKAGE): Stream =
|
|
142 loadResource_intern(path, package = package)
|
|
143
|
|
144 #[
|
|
145 proc LoadImage*[T](path: string, package = DEFAULT_PACKAGE): Image[RGBAPixel] =
|
|
146 if path.splitFile().ext.toLowerAscii == ".bmp":
|
|
147 loadResource_intern(path, package = package).ReadBMP()
|
|
148 elif path.splitFile().ext.toLowerAscii == ".png":
|
|
149 loadResource_intern(path, package = package).ReadPNG()
|
|
150 else:
|
|
151 raise newException(Exception, "Unsupported image file type: " & path)
|
|
152
|
|
153 proc LoadJson*(path: string, package = DEFAULT_PACKAGE): JsonNode =
|
|
154 path.loadResource_intern(package = package).readAll().parseJson()
|
|
155
|
|
156 proc LoadConfig*(path: string, package = DEFAULT_PACKAGE): Config =
|
|
157 path.loadResource_intern(package = package).loadConfig(filename = path)
|
|
158
|
|
159 proc LoadMeshes*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): seq[MeshTree] =
|
|
160 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)
|
|
161
|
|
162 proc LoadFirstMesh*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): Mesh =
|
|
163 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)[0].toSeq[0]
|
|
164
|
|
165 ]#
|
|
166
|
|
167 proc Packages*(): seq[string] =
|
|
168 modList_intern()
|
|
169
|
|
170 proc WalkResources*(dir = "", package = DEFAULT_PACKAGE): seq[string] =
|
|
171 for i in walkResources_intern(dir, package = package):
|
|
172 if i.startsWith(dir):
|
|
173 result.add i
|
|
174 result.sort()
|
|
175
|
|
176 proc List*(dir: string, package = DEFAULT_PACKAGE): seq[tuple[kind: PathComponent, path: string]] =
|
|
177 for i in ls_intern(dir = dir, package = package):
|
|
178 result.add i
|
|
179 result.sort()
|