Skip to content

Commit d9a5337

Browse files
committed
Support unload for a generic resource
1 parent 4347261 commit d9a5337

1 file changed

Lines changed: 47 additions & 28 deletions

File tree

libdeadcode/src/gui/resources/generic.d

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import std.path;
1212
import std.variant;
1313

1414
/** Persisting of objects
15-
15+
1616
Different fields needs to be persisted in different situations.
1717
A class implementing the IPersister interface is used to persist
1818
in one of these situations. A class or struct implementing the persist()
@@ -57,7 +57,7 @@ persister.get("bar", barGet);
5757
assert(bar.persistMe == barGet.persistMe);
5858
---
5959
*/
60-
//interface IPersister
60+
//interface IPersister
6161
//{
6262
// void set(T)(string name, T value);
6363
// void get(T)(string name, T value);
@@ -76,7 +76,7 @@ assert(bar.persistMe == barGet.persistMe);
7676
// {
7777
// data[name] = v;
7878
// }
79-
//
79+
//
8080
// void get(T)(string name, T value)
8181
// {
8282
// }
@@ -98,14 +98,14 @@ class GenericResource : Resource!GenericResource
9898
}
9999

100100
static class Helper(T) : IHelper
101-
{
101+
{
102102
static this()
103103
{
104104
string name = T.classinfo.name;
105105
ClassInfo ci = (Helper!T).classinfo;
106106
typeNameToHelperMap[name] = ci;
107107
}
108-
108+
109109
T data;
110110

111111
this ()
@@ -126,12 +126,12 @@ class GenericResource : Resource!GenericResource
126126
{
127127
return data.classinfo.name;
128128
}
129-
129+
130130
void serialize(Appender!string output)
131131
{
132132
output ~= jsonEncode(data);
133133
}
134-
134+
135135
void deserialize(string str)
136136
{
137137
data = jsonDecode!T(str);
@@ -152,7 +152,7 @@ class GenericResource : Resource!GenericResource
152152
auto idx = key in index;
153153
if (idx is null)
154154
return null;
155-
155+
156156
return get!T(*idx);
157157
}
158158

@@ -203,7 +203,7 @@ class GenericResource : Resource!GenericResource
203203
{
204204
import std.conv;
205205
import std.string;
206-
206+
207207
output ~= "Deadcode 1\n";
208208
output ~= "json 1\n";
209209

@@ -230,7 +230,7 @@ class GenericResource : Resource!GenericResource
230230
dataOutput ~= "\n";
231231

232232
helper.serialize(dataOutput);
233-
233+
234234
headerOutput ~= ",";
235235
headerOutput ~= text(dataOutput.data().length - offset);
236236
headerOutput ~= "\n";
@@ -244,28 +244,28 @@ class GenericResource : Resource!GenericResource
244244
}
245245
else
246246
{
247-
assert(0); // not supported right now
248-
foreach (helper; helpers)
249-
{
250-
helper.serialize(output);
251-
output ~= "\n";
252-
}
247+
throw new Exception("Not supported yet");
248+
//foreach (helper; helpers)
249+
//{
250+
// helper.serialize(output);
251+
// output ~= "\n";
252+
//}
253253
}
254254
}
255255

256256
void deserialize(string str)
257257
{
258-
258+
259259
if (includeHeader)
260260
{
261261
// First read headers to get a map of the file.
262-
262+
263263
}
264-
264+
265265
// First line describes the type to deserialize
266266
// This can be used to lookup type deserializer helper
267267
import std.algorithm;
268-
268+
269269
auto result = str.splitter("\r\n");
270270
if (result.empty)
271271
{
@@ -288,8 +288,8 @@ class GenericResource : Resource!GenericResource
288288
import std.format;
289289
int objectCount;
290290
int indexCount;
291-
292-
string data = result.front;
291+
292+
string data = result.front;
293293
if (std.format.formattedRead(data, "%s,%s", &objectCount, &indexCount) != 2)
294294
throw new Exception("Cannot read index entry " ~ data);
295295
result.popFront();
@@ -320,11 +320,19 @@ class GenericResource : Resource!GenericResource
320320
typeNames ~= typeName;
321321
}
322322

323+
// TODO: Fix or change
324+
//int j = 0;
325+
//while (objectCount--)
326+
//{
327+
// readObject(typeNames[j++], result.front);
328+
// result.popFront();
329+
//}
323330
int j = 0;
324331
while (objectCount--)
325332
{
326-
readObject(typeNames[j++], result.front);
327-
result.popFront();
333+
import std.array;
334+
readObject(typeNames[j++], join(result));
335+
break;
328336
}
329337
}
330338

@@ -361,19 +369,30 @@ class GenericResourceManager : ResourceManager!GenericResource
361369
return fm;
362370
}
363371

364-
GenericResource create(T)(string name)
372+
GenericResource create(T)(string name)
365373
{
366374
auto f = declare(name);
367375
return f;
368376
}
369-
370-
GenericResource create(T)(string name, T value)
377+
378+
GenericResource create(T)(string name, T value)
371379
{
372-
import jsonx;
380+
import util.jsonx;
373381
auto f = declare(name);
374382
f.data = value;
375383
return f;
376384
}
385+
386+
override bool unload(Handle h)
387+
{
388+
ResourceState* rs = h in _resourcesByHandle;
389+
if (rs !is null)
390+
{
391+
rs.resource.helpers = null;
392+
rs.resource.index = null;
393+
}
394+
return super.unload(h);
395+
}
377396
}
378397

379398
class GenericResourceJsonSerializer : ResourceSerializer!GenericResource

0 commit comments

Comments
 (0)