0
|
1 |
'From Smalltalk/X, Version:2.7.1 on 10-Jul-1993 at 19:03:09'!
|
|
2 |
|
|
3 |
Dictionary subclass:#ResourcePack
|
|
4 |
instanceVariableNames:'elements dependents fileName'
|
|
5 |
classVariableNames:'packs'
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'System-Support'
|
|
8 |
!
|
|
9 |
|
|
10 |
!ResourcePack class methodsFor:'initialization'!
|
|
11 |
|
|
12 |
initialize
|
|
13 |
packs isNil ifTrue:[
|
|
14 |
packs := ShadowArray new:30
|
|
15 |
].
|
|
16 |
|
|
17 |
"ResourcePack initialize"
|
|
18 |
! !
|
|
19 |
|
|
20 |
!ResourcePack class methodsFor:'instance creation'!
|
|
21 |
|
|
22 |
fromFile:aFileName
|
|
23 |
"get the resource definitions from a file"
|
|
24 |
|
|
25 |
|inStream newPack temp idx|
|
|
26 |
|
|
27 |
"first look, if not already here"
|
|
28 |
|
|
29 |
packs isNil ifTrue:[
|
|
30 |
self initialize
|
|
31 |
].
|
|
32 |
packs do:[:aPack |
|
|
33 |
aPack notNil ifTrue:[
|
|
34 |
aPack fileName = aFileName ifTrue:[
|
|
35 |
^ aPack
|
|
36 |
]
|
|
37 |
]
|
|
38 |
].
|
|
39 |
|
|
40 |
inStream := Smalltalk systemFileStreamFor:('resources/' , aFileName).
|
|
41 |
inStream isNil ifTrue:[^ self new].
|
|
42 |
newPack := self new readFromResourceStream:inStream.
|
|
43 |
newPack fileName:aFileName.
|
|
44 |
inStream close.
|
|
45 |
|
|
46 |
idx := packs identityIndexOf:nil.
|
|
47 |
idx notNil ifTrue:[
|
|
48 |
packs at:idx put:newPack
|
|
49 |
] ifFalse:[
|
|
50 |
temp := ShadowArray new:(packs size).
|
|
51 |
temp replaceFrom:1 with:packs.
|
|
52 |
packs := temp
|
|
53 |
].
|
|
54 |
|
|
55 |
^ newPack
|
|
56 |
|
|
57 |
"ResourcePack fromFile:'SBrowser.rs'"
|
|
58 |
"ResourcePack fromFile:'FBrowser.rs'"
|
|
59 |
"ResourcePack fromFile:'Smalltalk.rs'"
|
|
60 |
!
|
|
61 |
|
|
62 |
for:aClass
|
|
63 |
"get the resource definitions for aClass"
|
|
64 |
|
|
65 |
|superPack|
|
|
66 |
|
|
67 |
aClass == Object ifTrue:[
|
|
68 |
^ self fromFile:((Smalltalk fileNameForClass:aClass name) , '.rs')
|
|
69 |
].
|
|
70 |
superPack := self for:aClass superclass.
|
|
71 |
^ (self fromFile:((Smalltalk fileNameForClass:aClass name) , '.rs')) merge:superPack
|
|
72 |
! !
|
|
73 |
|
|
74 |
!ResourcePack methodsFor:'merging'!
|
|
75 |
|
|
76 |
merge:anotherPack
|
|
77 |
anotherPack associationsDo:[:assoc |
|
|
78 |
self at:(assoc key) put:(assoc value)
|
|
79 |
]
|
|
80 |
! !
|
|
81 |
|
|
82 |
!ResourcePack methodsFor:'accessing'!
|
|
83 |
|
|
84 |
dependents:aCollection
|
|
85 |
dependents := aCollection
|
|
86 |
!
|
|
87 |
|
|
88 |
at:aKey
|
|
89 |
^ self at:aKey ifAbsent:[aKey]
|
|
90 |
!
|
|
91 |
|
|
92 |
fileName
|
|
93 |
^ fileName
|
|
94 |
!
|
|
95 |
|
|
96 |
fileName:aString
|
|
97 |
fileName := aString
|
|
98 |
!
|
|
99 |
|
|
100 |
dependents
|
|
101 |
^ dependents
|
|
102 |
!
|
|
103 |
|
|
104 |
at:aKey default:default
|
|
105 |
^ self at:aKey ifAbsent:[default]
|
|
106 |
!
|
|
107 |
|
|
108 |
array:anArray
|
|
109 |
^ anArray collect:[:r | self at:r default:r]
|
|
110 |
!
|
|
111 |
|
|
112 |
string:s
|
|
113 |
^ self at:s ifAbsent:[s]
|
|
114 |
!
|
|
115 |
|
|
116 |
string:s with:arg
|
|
117 |
^ self string:s withArgs:(Array with:arg)
|
|
118 |
!
|
|
119 |
|
|
120 |
string:s with:arg1 with:arg2
|
|
121 |
^ self string:s withArgs:(Array with:arg1 with:arg2)
|
|
122 |
!
|
|
123 |
|
|
124 |
string:s withArgs:argArray
|
|
125 |
| template expandedString idx start stop next |
|
|
126 |
|
|
127 |
template := self at:s ifAbsent:[s].
|
|
128 |
expandedString := ''.
|
|
129 |
stop := template size.
|
|
130 |
start := 1.
|
|
131 |
[start < stop] whileTrue:[
|
|
132 |
idx := template indexOf:$% startingAt:start.
|
|
133 |
idx == 0 ifTrue:[
|
|
134 |
^ expandedString , (template copyFrom:start to:stop)
|
|
135 |
].
|
|
136 |
"found a %"
|
|
137 |
next := template at:(idx + 1).
|
|
138 |
(next == $%) ifTrue:[
|
|
139 |
expandedString := expandedString , '%'
|
|
140 |
] ifFalse:[
|
|
141 |
expandedString := expandedString , (argArray at:(next digitValue)) printString
|
|
142 |
].
|
|
143 |
start := idx + 2
|
|
144 |
]
|
|
145 |
! !
|
|
146 |
|
|
147 |
!ResourcePack methodsFor:'file reading'!
|
|
148 |
|
|
149 |
readFromResourceStream:inStream
|
|
150 |
|lineString rest value ifLevel skipping l name first|
|
|
151 |
|
|
152 |
ifLevel := 0.
|
|
153 |
skipping := false.
|
|
154 |
[inStream atEnd] whileFalse:[
|
|
155 |
lineString := inStream nextLine.
|
|
156 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[
|
|
157 |
first := lineString at:1.
|
|
158 |
first == $; ifFalse:[
|
|
159 |
first == $# ifTrue:[
|
|
160 |
(lineString startsWith:'#if') ifTrue:[
|
|
161 |
skipping ifFalse:[
|
|
162 |
rest := lineString copyFrom:4.
|
|
163 |
value := Compiler evaluate:rest.
|
|
164 |
(value == #Error) ifTrue:[
|
|
165 |
Transcript show:('error in resource:' , lineString).
|
|
166 |
].
|
|
167 |
(value == false) ifTrue:[
|
|
168 |
skipping := true
|
|
169 |
]
|
|
170 |
].
|
|
171 |
ifLevel := ifLevel + 1
|
|
172 |
] ifFalse:[
|
|
173 |
(lineString startsWith:'#endif') ifTrue:[
|
|
174 |
ifLevel := ifLevel - 1.
|
|
175 |
ifLevel == 0 ifTrue:[
|
|
176 |
skipping := false
|
|
177 |
]
|
|
178 |
].
|
|
179 |
].
|
|
180 |
] ifFalse:[
|
|
181 |
skipping ifFalse:[
|
|
182 |
lineString := lineString withoutSeparators.
|
|
183 |
name := nil.
|
|
184 |
(lineString at:1) == $' ifTrue:[
|
|
185 |
l := lineString indexOf:$' startingAt:2.
|
|
186 |
l ~~ 0 ifTrue:[
|
|
187 |
name := (lineString copyFrom:2 to:l-1).
|
|
188 |
l := l + 1
|
|
189 |
]
|
|
190 |
] ifFalse:[
|
|
191 |
l := lineString indexOfSeparatorStartingAt:1.
|
|
192 |
l ~~ 0 ifTrue:[
|
|
193 |
name := lineString copyFrom:1 to:l-1.
|
|
194 |
]
|
|
195 |
].
|
|
196 |
name notNil ifTrue:[
|
|
197 |
rest := lineString copyFrom:l.
|
|
198 |
value := Compiler evaluate:rest.
|
|
199 |
(value == #Error) ifTrue:[
|
|
200 |
Transcript show:('error in resource:' , name).
|
|
201 |
].
|
|
202 |
self at:name put:value.
|
|
203 |
]
|
|
204 |
]
|
|
205 |
]
|
|
206 |
]
|
|
207 |
]
|
|
208 |
].
|
|
209 |
! !
|
|
210 |
|
|
211 |
ResourcePack initialize!
|