annotate semiconginev2/old/resources.nim @ 1231:70f6c1cfe005

add: incomplete cube demo
author sam <sam@basx.dev>
date Thu, 18 Jul 2024 23:48:57 +0700
parents 56781cc0fc7c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 911
diff changeset
1 import std/parsecfg
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/streams
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 911
diff changeset
3 import std/algorithm
911
0a1e68f32fca add: shortcut for loading of json files
Sam <sam@basx.dev>
parents: 900
diff changeset
4 import std/json
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/strutils
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 840
diff changeset
6 import std/sequtils
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/strformat
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
8 import std/os
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
9 import std/sets
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
10 import std/unicode
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
11
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
12 import ./core
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
13 import ./resources/image
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
14 import ./resources/audio
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
15 import ./resources/mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
16 import ./resources/font
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
17 import ./mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
18 import ./material
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
19
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
20 export image
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
21 export audio
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
22 export mesh
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
23
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
24 type
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
25 ResourceBundlingType = enum
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
26 Dir # Directories
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
27 Zip # Zip files
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
28 Exe # Embeded in executable
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
29
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 840
diff changeset
30 const
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
31 thebundletype = parseEnum[ResourceBundlingType](PACKAGETYPE.toLowerAscii().capitalizeAscii())
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 840
diff changeset
32 ASCII_CHARSET = PrintableChars.toSeq.toRunes
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
33 DEFAULT_PACKAGE = "default"
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
34
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
35 # resource loading
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
36
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
37 func normalizeDir(dir: string): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
38 result = dir
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
39 if result.startsWith("./"):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
40 result = result[2 .. ^1]
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
41 if result.startsWith("/"):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
42 result = result[1 .. ^1]
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
43 result = dir.replace('\\', '/')
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
44 if not result.endsWith("/") and result != "":
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
45 result = result & "/"
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
46
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
47 when thebundletype == Dir:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
48
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
49 proc resourceRoot(): string =
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
50 getAppDir().absolutePath().joinPath(RESOURCEROOT)
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
51 proc packageRoot(package: string): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
52 resourceRoot().joinPath(package)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
53
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
54 proc loadResource_intern(path: string, package: string): Stream =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
55 let realpath = package.packageRoot().joinPath(path)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
56 if not realpath.fileExists():
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
57 raise newException(Exception, &"Resource {path} not found (checked {realpath})")
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
58 newFileStream(realpath, fmRead)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
59
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
60 proc modList_intern(): seq[string] =
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 872
diff changeset
61 for kind, file in walkDir(resourceRoot(), relative = true):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
62 if kind == pcDir:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
63 result.add file
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
64
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
65 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
66 for file in walkDirRec(package.packageRoot().joinPath(dir), relative = true):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
67 yield file
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
68
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
69 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
70 for i in walkDir(package.packageRoot().joinPath(dir), relative = true):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
71 yield i
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
72
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
73 elif thebundletype == Zip:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
74
1013
967b8fa81b6b add: thirdparty libraries into repo
sam <sam@basx.dev>
parents: 995
diff changeset
75 import ./thirdparty/zippy/zippy/ziparchives
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
76
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
77 proc resourceRoot(): string =
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
78 absolutePath(getAppDir()).joinPath(RESOURCEROOT)
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
79 proc packageRoot(package: string): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
80 resourceRoot().joinPath(package)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
81
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
82 proc loadResource_intern(path: string, package: string): Stream =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
83 let archive = openZipArchive(package.packageRoot() & ".zip")
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
84 try:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
85 result = newStringStream(archive.extractFile(path))
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
86 except ZippyError:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
87 raise newException(Exception, &"Resource {path} not found")
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
88 archive.close()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
89
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
90 proc modList_intern(): seq[string] =
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 872
diff changeset
91 for kind, file in walkDir(resourceRoot(), relative = true):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
92 if kind == pcFile and file.endsWith(".zip"):
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
93 result.add file[0 ..< ^4]
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
94
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
95 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
96 let archive = openZipArchive(package.packageRoot() & ".zip")
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
97 let normDir = dir.normalizeDir()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
98 for i in archive.walkFiles:
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
99 if i.startsWith(normDir):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
100 yield i
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
101 archive.close()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
102
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
103 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
104 let archive = openZipArchive(package.packageRoot() & ".zip")
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
105 let normDir = dir.normalizeDir()
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
106 var yielded: HashSet[string]
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
107
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
108 for i in archive.walkFiles:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
109 if i.startsWith(normDir):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
110 let components = i[normDir.len .. ^1].split('/', maxsplit = 1)
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
111 if components.len == 1:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
112 if not (components[0] in yielded):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
113 yield (kind: pcFile, path: components[0])
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
114 else:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
115 if not (components[0] in yielded):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
116 yield (kind: pcDir, path: components[0])
900
f14b955da09b fix: yielding duplicates
Sam <sam@basx.dev>
parents: 899
diff changeset
117 yielded.incl components[0]
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
118 archive.close()
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
119
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
120 elif thebundletype == Exe:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
121
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
122 import std/tables
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
123
898
6924c2cf94e5 fix: exe-bundling not working correctly
Sam <sam@basx.dev>
parents: 882
diff changeset
124 const BUILD_RESOURCEROOT* {.strdefine.}: string = ""
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
125
898
6924c2cf94e5 fix: exe-bundling not working correctly
Sam <sam@basx.dev>
parents: 882
diff changeset
126 proc loadResources(): Table[string, Table[string, string]] {.compileTime.} =
995
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
127 when BUILD_RESOURCEROOT == "":
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
128 {.warning: "BUILD_RESOURCEROOT is empty, no resources will be packaged".}
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
129 return
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
130 else:
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
131 for kind, packageDir in walkDir(BUILD_RESOURCEROOT):
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
132 if kind == pcDir:
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
133 let package = packageDir.splitPath.tail
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
134 result[package] = Table[string, string]()
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
135 for resourcefile in walkDirRec(packageDir, relative = true):
2a1de6cb5282 fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents: 983
diff changeset
136 result[package][resourcefile.replace('\\', '/')] = staticRead(packageDir.joinPath(resourcefile))
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
137 const bundledResources = loadResources()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
138
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
139 proc loadResource_intern(path: string, package: string): Stream =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
140 if not (path in bundledResources[package]):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
141 raise newException(Exception, &"Resource {path} not found")
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
142 newStringStream(bundledResources[package][path])
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
143
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
144 proc modList_intern(): seq[string] =
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
145 result = bundledResources.keys().toSeq()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
146
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
147 iterator walkResources_intern(dir: string, package = DEFAULT_PACKAGE): string =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
148 for i in bundledResources[package].keys:
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
149 yield i
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
150
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
151 iterator ls_intern(dir: string, package: string): tuple[kind: PathComponent, path: string] =
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
152 let normDir = dir.normalizeDir()
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
153 var yielded: HashSet[string]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
154
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
155 for i in bundledResources[package].keys:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
156 if i.startsWith(normDir):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
157 let components = i[normDir.len .. ^1].split('/', maxsplit = 1)
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
158 if components.len == 1:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
159 if not (components[0] in yielded):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
160 yield (kind: pcFile, path: components[0])
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
161 else:
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
162 if not (components[0] in yielded):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
163 yield (kind: pcDir, path: components[0])
900
f14b955da09b fix: yielding duplicates
Sam <sam@basx.dev>
parents: 899
diff changeset
164 yielded.incl components[0]
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
165
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
166 proc LoadResource*(path: string, package = DEFAULT_PACKAGE): Stream =
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
167 loadResource_intern(path, package = package)
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
168
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
169 proc LoadImage*[T](path: string, package = DEFAULT_PACKAGE): Image[RGBAPixel] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
170 if path.splitFile().ext.toLowerAscii == ".bmp":
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
171 loadResource_intern(path, package = package).ReadBMP()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
172 elif path.splitFile().ext.toLowerAscii == ".png":
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
173 loadResource_intern(path, package = package).ReadPNG()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
174 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
175 raise newException(Exception, "Unsupported image file type: " & path)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
176
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
177 proc LoadAudio*(path: string, package = DEFAULT_PACKAGE): Sound =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
178 if path.splitFile().ext.toLowerAscii == ".au":
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1013
diff changeset
179 loadResource_intern(path, package = package).ReadAU()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
180 elif path.splitFile().ext.toLowerAscii == ".ogg":
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1013
diff changeset
181 loadResource_intern(path, package = package).ReadVorbis()
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
182 else:
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
183 raise newException(Exception, "Unsupported audio file type: " & path)
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
184
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
185 proc LoadJson*(path: string, package = DEFAULT_PACKAGE): JsonNode =
911
0a1e68f32fca add: shortcut for loading of json files
Sam <sam@basx.dev>
parents: 900
diff changeset
186 path.loadResource_intern(package = package).readAll().parseJson()
0a1e68f32fca add: shortcut for loading of json files
Sam <sam@basx.dev>
parents: 900
diff changeset
187
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
188 proc LoadConfig*(path: string, package = DEFAULT_PACKAGE): Config =
912
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 911
diff changeset
189 path.loadResource_intern(package = package).loadConfig(filename = path)
c72b246d8413 add: config loader; fix: circle-mesh
Sam <sam@basx.dev>
parents: 911
diff changeset
190
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
191 proc LoadFont*(
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 840
diff changeset
192 path: string,
882
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 872
diff changeset
193 name = "",
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 872
diff changeset
194 lineHeightPixels = 80'f32,
5392cbd9db41 fix: material handlinge, did: formatting, add: support for multi-material texts
Sam <sam@basx.dev>
parents: 872
diff changeset
195 additional_codepoints: openArray[Rune] = [],
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
196 charset = ASCII_CHARSET,
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
197 package = DEFAULT_PACKAGE
872
1ee397815b0b did: image & font refactoring, add texture-atlas-packing
Sam <sam@basx.dev>
parents: 840
diff changeset
198 ): Font =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
199 var thename = name
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
200 if thename == "":
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
201 thename = path.splitFile().name
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1013
diff changeset
202 loadResource_intern(path, package = package).ReadTrueType(name, charset & additional_codepoints.toSeq, lineHeightPixels)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
203
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
204 proc LoadMeshes*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): seq[MeshTree] =
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1013
diff changeset
205 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
206
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
207 proc LoadFirstMesh*(path: string, defaultMaterial: MaterialType, package = DEFAULT_PACKAGE): Mesh =
1138
02e1d2658ff5 did: more renaming
sam <sam@basx.dev>
parents: 1013
diff changeset
208 loadResource_intern(path, package = package).ReadglTF(defaultMaterial)[0].toSeq[0]
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
209
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
210 proc Packages*(): seq[string] =
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
211 modList_intern()
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
212
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
213 proc WalkResources*(dir = "", package = DEFAULT_PACKAGE): seq[string] =
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
214 for i in walkResources_intern(dir, package = package):
840
44ec744fbedc did: package restructuring according to nimble recommendation for libraries
Sam <sam@basx.dev>
parents:
diff changeset
215 if i.startsWith(dir):
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
216 result.add i
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
217 result.sort()
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
218
1139
114f395b9144 did: finish refactoring and updated all tests accordingly
sam <sam@basx.dev>
parents: 1138
diff changeset
219 proc List*(dir: string, package = DEFAULT_PACKAGE): seq[tuple[kind: PathComponent, path: string]] =
899
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
220 for i in ls_intern(dir = dir, package = package):
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
221 result.add i
ad961543994b did: refactor and improve packaging API
Sam <sam@basx.dev>
parents: 898
diff changeset
222 result.sort()