Exemple #1
0
		void GuiResourceFolder::LoadResourceFolderFromXml(DelayLoadingList& delayLoadings, const WString& containingFolder, Ptr<parsing::xml::XmlElement> folderXml, collections::List<WString>& errors)
		{
			ClearItems();
			ClearFolders();
			FOREACH(Ptr<XmlElement>, element, XmlGetElements(folderXml))
			{
				WString name;
				if(Ptr<XmlAttribute> nameAtt=XmlGetAttribute(element, L"name"))
				{
					name=nameAtt->value.value;
				}
				if(element->name.value==L"Folder")
				{
					if (name == L"")
					{
						errors.Add(L"A resource folder should have a name.");
						errors.Add(
							L"Format: RESOURCE, Row: " + itow(element->codeRange.start.row + 1) +
							L", Column: " + itow(element->codeRange.start.column + 1) +
							L", Message: A resource folder should have a name.");
					}
					else
					{
						Ptr<GuiResourceFolder> folder=new GuiResourceFolder;
						if(AddFolder(name, folder))
						{
							WString newContainingFolder=containingFolder;
							Ptr<XmlElement> newFolderXml=element;
							if(Ptr<XmlAttribute> contentAtt=XmlGetAttribute(element, L"content"))
							{
								if(contentAtt->value.value==L"Link")
								{
									folder->SetFileContentPath(XmlGetValue(element));
									WString filePath = containingFolder + folder->GetFileContentPath();
									WString text;
									if(LoadTextFile(filePath, text))
									{
										if(auto parser=GetParserManager()->GetParser<XmlDocument>(L"XML"))
										{
											if(auto xml=parser->TypedParse(text, errors))
											{
												newContainingFolder=GetFolderPath(filePath);
												newFolderXml=xml->rootElement;
											}
										}
									}
									else
									{
										errors.Add(L"Failed to load file \"" + filePath + L"\".");
									}
								}
							}
							folder->LoadResourceFolderFromXml(delayLoadings, newContainingFolder, newFolderXml, errors);
						}
						else
						{
							errors.Add(L"Duplicated resource folder name \"" + name + L"\".");
						}
					}
				}
				else if(element->name.value.Length() <= 3 || element->name.value.Sub(0, 4) != L"ref.")
				{
					WString relativeFilePath;
					WString filePath;
					if(Ptr<XmlAttribute> contentAtt=XmlGetAttribute(element, L"content"))
					{
						if(contentAtt->value.value==L"File")
						{
							relativeFilePath = XmlGetValue(element);
							filePath = containingFolder + relativeFilePath;
							if(name==L"")
							{
								name=GetFileName(filePath);
							}
						}
					}

					Ptr<GuiResourceItem> item = new GuiResourceItem;
					if(AddItem(name, item))
					{
						WString type = element->name.value;
						IGuiResourceTypeResolver* typeResolver = GetResourceResolverManager()->GetTypeResolver(type);
						IGuiResourceTypeResolver* preloadResolver = typeResolver;

						if(typeResolver)
						{
							if (!typeResolver->DirectLoadXml())
							{
								WString preloadType = typeResolver->IndirectLoad()->GetPreloadType();
								if (preloadType != L"")
								{
									preloadResolver = GetResourceResolverManager()->GetTypeResolver(preloadType);
									if (!preloadResolver)
									{
										errors.Add(L"Unknown resource resolver \"" + preloadType + L"\" of resource type \"" + type + L"\".");
									}
								}
							}
						}
						else
						{
							errors.Add(L"Unknown resource type \"" + type + L"\".");
						}

						if(typeResolver && preloadResolver)
						{
							if (auto directLoad = preloadResolver->DirectLoadXml())
							{
								Ptr<DescriptableObject> resource;
								WString itemType = preloadResolver->GetType();
								if (filePath == L"")
								{
									resource = directLoad->ResolveResource(element, errors);
								}
								else
								{
									item->SetFileContentPath(relativeFilePath);
									resource = directLoad->ResolveResource(filePath, errors);
								}

								if (typeResolver != preloadResolver)
								{
									if (auto indirectLoad = typeResolver->IndirectLoad())
									{
										if(indirectLoad->IsDelayLoad())
										{
											DelayLoading delayLoading;
											delayLoading.type = type;
											delayLoading.workingDirectory = containingFolder;
											delayLoading.preloadResource = item;
											delayLoadings.Add(delayLoading);
										}
										else if(resource)
										{
											resource = indirectLoad->ResolveResource(resource, 0, errors);
											itemType = typeResolver->GetType();
										}
									}
									else
									{
										resource = 0;
										errors.Add(L"Resource type \"" + typeResolver->GetType() + L"\" is not a indirect load resource type.");
									}
								}
								item->SetContent(itemType, resource);
							}
							else
							{
								errors.Add(L"Resource type \"" + preloadResolver->GetType() + L"\" is not a direct load resource type.");
							}
						}

						if(!item->GetContent())
						{
							RemoveItem(name);
						}
					}
					else
					{
						errors.Add(L"Duplicated resource item name \"" + name + L"\".");
					}
				}
			}
				void Load()override
				{
					IGuiParserManager* manager=GetParserManager();
					manager->SetParser(L"SHORTCUT", new GuiToolstripCommandShortcutParser);
				}