File Index Symbol Index

#include "GuiResourceManager.h"
#include "GuiParserManager.h"
namespace
vl
{
namespace
presentation
{
using
namespace
collections
;
using
namespace
stream
;
using
namespace
parsing
::
xml
;
using
namespace
reflection
::
description
;
using
namespace
controls
;
/*********************************************************************** Class Name Record (ClassNameRecord) ***********************************************************************/
class
GuiResourceClassNameRecordTypeResolver
:
public
Object
,
public
IGuiResourceTypeResolver
,
private
IGuiResourceTypeResolver_DirectLoadStream
{
public
:
WString
GetType
()
override
{
return
L"ClassNameRecord"
; }
bool
XmlSerializable
()
override
{
return
false
; }
bool
StreamSerializable
()
override
{
return
true
; }
IGuiResourceTypeResolver_DirectLoadStream
*
DirectLoadStream
()
override
{
return
this
; }
void
SerializePrecompiled
(
Ptr
<
GuiResourceItem
>
resource
,
Ptr
<
DescriptableObject
>
content
,
stream
::
IStream
&
binaryStream
)
override
{
if
(
auto
obj
=
content
.
Cast
<
GuiResourceClassNameRecord
>()) {
internal
::
ContextFreeWriter
writer
(
binaryStream
);
writer
<
<
obj
-
>
classNames
; } }
Ptr
<
DescriptableObject
>
ResolveResourcePrecompiled
(
Ptr
<
GuiResourceItem
>
resource
,
stream
::
IStream
&
binaryStream
,
GuiResourceError
::
List
&
errors
)
override
{
internal
::
ContextFreeReader
reader
(
binaryStream
);
auto
obj
=
MakePtr
<
GuiResourceClassNameRecord
>();
reader
<
<
obj
-
>
classNames
;
return
obj
; } };
/*********************************************************************** IGuiInstanceResourceManager ***********************************************************************/
IGuiResourceManager
*
resourceManager
=
nullptr
;
IGuiResourceManager
*
GetResourceManager
() {
return
resourceManager
; }
class
GuiResourceManager
:
public
Object
,
public
IGuiResourceManager
,
public
IGuiPlugin
{
protected
:
typedef
Dictionary
<
WString
,
Ptr
<
GuiResource
>>
ResourceMap
;
List
<
Ptr
<
GuiResource
>>
anonymousResources
;
ResourceMap
resources
;
ResourceMap
instanceResources
;
class
PendingResource
:
public
Object
{
public
:
Ptr
<
GuiResourceMetadata
>
metadata
;
GuiResourceUsage
usage
;
MemoryStream
memoryStream
;
SortedList
<
WString
>
dependencies
;
Ptr
<
GuiResource
>
LoadResource
() {
memoryStream
.
SeekFromBegin
(
0
);
List
<
GuiResourceError
>
errors
;
auto
resource
=
GuiResource
::
LoadPrecompiledBinary
(
memoryStream
,
errors
);
return
resource
; } };
Group
<
WString
,
Ptr
<
PendingResource
>>
depToPendings
;
SortedList
<
Ptr
<
PendingResource
>>
pendingResources
;
public
: { }
void
Load
()
override
{
resourceManager
=
this
;
IGuiResourceResolverManager
*
manager
=
GetResourceResolverManager
();
manager
->
SetTypeResolver
(
new
GuiResourceClassNameRecordTypeResolver
); }
void
Unload
()
override
{
resourceManager
=
nullptr
; }
void
SetResource
(
Ptr
<
GuiResource
>
resource
,
GuiResourceError
::
List
&
errors
,
GuiResourceUsage
usage
)
override
{
auto
metadata
=
resource
-
>
GetMetadata
();
if
(
metadata
-
>
name
==
L""
) {
if
(
anonymousResources
.
Contains
(
resource
.
Obj
()))
return
;
resource
-
>
Initialize
(
usage
,
errors
);
if
(
errors
.
Count
() >
0
) {
return
; }
anonymousResources
.
Add
(
resource
); }
else
{
resource
-
>
Initialize
(
usage
,
errors
);
if
(
errors
.
Count
() >
0
) {
return
; }
resources
.
Add
(
metadata
-
>
name
,
resource
); }
if
(
auto
record
=
resource
-
>
GetValueByPath
(
L"Precompiled/ClassNameRecord"
).Cast<
GuiResourceClassNameRecord
>()) { {
instanceResources
.
Add
(
className
,
resource
); } }
if
(
metadata
-
>
name
!=
L""
) {
vint
index
=
depToPendings
.
Keys
().
IndexOf
(
metadata
-
>
name
);
if
(
index
!= -
1
) {
List
<
Ptr
<
PendingResource
>>
prs
;
CopyFrom
(
prs
,
depToPendings
.
GetByIndex
(
index
));
depToPendings
.
Remove
(
metadata
-
>
name
); {
pr
-
>
dependencies
.
Remove
(
metadata
-
>
name
);
if
(
pr
-
>
dependencies
.
Count
() ==
0
) {
pendingResources
.
Remove
(
pr
.
Obj
());
SetResource
(
pr
-
>
LoadResource
(),
errors
,
pr
-
>
usage
); } } } } }
Ptr
<
GuiResource
>
GetResource
(
const
WString
&
name
)
override
{
vint
index
=
resources
.
Keys
().
IndexOf
(
name
);
return
index
== -
1
?
nullptr
:
resources
.
Values
()
[
index
]; }
Ptr
<
GuiResource
>
GetResourceFromClassName
(
const
WString
&
classFullName
)
override
{
vint
index
=
instanceResources
.
Keys
().
IndexOf
(
classFullName
);
if
(
index
== -
1
)
return
nullptr
;
return
instanceResources
.
Values
()
[
index
]; }
void
UnloadResource
(
const
WString
&
name
)
override
{
vint
index
=
resources
.
Keys
().
IndexOf
(
name
);
if
(
index
!= -
1
) {
auto
resource
=
resources
.
Values
()
[
index
];
resources
.
Remove
(
name
);
if
(
auto
record
=
resource
-
>
GetValueByPath
(
L"Precompiled/ClassNameRecord"
).Cast<
GuiResourceClassNameRecord
>()) { {
instanceResources
.
Remove
(
className
); } } } }
void
LoadResourceOrPending
(
stream
::
IStream
&
resourceStream
,
GuiResourceError
::
List
&
errors
,
GuiResourceUsage
usage
)
override
{
auto
pr
=
MakePtr
<
PendingResource
>();
pr
-
>
usage
=
usage
;
CopyStream
(
resourceStream
,
pr
-
>
memoryStream
);
pr
-
>
metadata
=
MakePtr
<
GuiResourceMetadata
>(); {
pr
-
>
memoryStream
.
SeekFromBegin
(
0
);
stream
::
internal
::
ContextFreeReader
reader
(
pr
-
>
memoryStream
);
WString
metadata
;
reader
<
<
metadata
;
List
<
GuiResourceError
>
errors
;
auto
parser
=
GetParserManager
()->
GetParser
<
XmlDocument
>(
L"XML"
);
auto
xmlMetadata
=
parser
->Parse({},
metadata
,
errors
);
pr
-
>
metadata
-
>
LoadFromXml
(
xmlMetadata
, {},
errors
); }
CopyFrom
(
pr
-
>
dependencies
,
From
(
pr
-
>
metadata
-
>
dependencies
).Except(
resources
.
Keys
()));
if
(
pr
-
>
dependencies
.
Count
() ==
0
) {
SetResource
(
pr
-
>
LoadResource
(),
errors
,
pr
-
>
usage
); }
else
{
pendingResources
.
Add
(
pr
); {
depToPendings
.
Add
(
dep
,
pr
); } } }
void
LoadResourceOrPending
(
stream
::
IStream
&
resourceStream
,
GuiResourceUsage
usage
)
override
{
GuiResourceError
::
List
errors
;
LoadResourceOrPending
(
resourceStream
,
errors
,
usage
); }
void
GetPendingResourceNames
(
collections
::
List
<
WString
>&
names
)
override
{
CopyFrom
(
names
,
From
(
pendingResources
).Select([](
Ptr
<
PendingResource
>
pr
) {
return
pr
-
>
metadata
-
>
name
; })); } }; } }