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 proc LoadJson*(path: string, package = DEFAULT_PACKAGE): JsonNode =
|
|
145 path.loadResource_intern(package = package).readAll().parseJson()
|
|
146
|
|
147 proc LoadConfig*(path: string, package = DEFAULT_PACKAGE): Config =
|
|
148 path.loadResource_intern(package = package).loadConfig(filename = path)
|
|
149
|
1242
|
150 # PNG-images
|
|
151
|
|
152 {.compile: currentSourcePath.parentDir() & "/contrib/lodepng.c".}
|
|
153 proc lodepng_decode_memory(out_data: ptr cstring, w: ptr cuint, h: ptr cuint, in_data: cstring, insize: csize_t, colorType: cint, bitdepth: cuint): cuint {.importc.}
|
|
154
|
|
155 proc lodepng_encode_memory(out_data: ptr cstring, outsize: ptr csize_t, image: cstring, w: cuint, h: cuint, colorType: cint, bitdepth: cuint): cuint {.importc.}
|
|
156
|
|
157 #[
|
1225
|
158 proc LoadMeshes*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): seq[MeshTree] =
|
|
159 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)
|
|
160
|
|
161 proc LoadFirstMesh*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): Mesh =
|
|
162 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)[0].toSeq[0]
|
|
163 ]#
|
|
164
|
|
165 proc Packages*(): seq[string] =
|
|
166 modList_intern()
|
|
167
|
|
168 proc WalkResources*(dir = "", package = DEFAULT_PACKAGE): seq[string] =
|
|
169 for i in walkResources_intern(dir, package = package):
|
|
170 if i.startsWith(dir):
|
|
171 result.add i
|
|
172 result.sort()
|
|
173
|
|
174 proc List*(dir: string, package = DEFAULT_PACKAGE): seq[tuple[kind: PathComponent, path: string]] =
|
|
175 for i in ls_intern(dir = dir, package = package):
|
|
176 result.add i
|
|
177 result.sort()
|