OpenClonk
ModXMLData Struct Reference

#include <C4StartupModsDlg.h>

Classes

struct  FileInfo
 

Public Types

enum class  Source { Unknown , Local , Overview , DetailView }
 

Public Member Functions

bool requiresUpdate () const
 
 ModXMLData (const TiXmlElement *xml, Source source=Source::Unknown)
 
 ~ModXMLData ()
 

Public Attributes

std::vector< FileInfofiles
 
std::vector< std::string > dependencies
 
std::vector< std::string > tags
 
std::string title
 
std::string id
 
std::string description
 
std::string longDescription
 
std::string slug
 
bool metadataMissing { false }
 
Source source { Source::Unknown }
 
TiXmlNode * originalXMLElement { nullptr }
 

Detailed Description

Definition at line 35 of file C4StartupModsDlg.h.


Class Documentation

◆ ModXMLData::FileInfo

struct ModXMLData::FileInfo

Definition at line 37 of file C4StartupModsDlg.h.

Class Members
string handle
string name
string sha1
size_t size

Member Enumeration Documentation

◆ Source

enum ModXMLData::Source
strong
Enumerator
Unknown 
Local 
Overview 
DetailView 

Definition at line 54 of file C4StartupModsDlg.h.

55  {
56  Unknown,
57  Local,
58  Overview,
59  DetailView
60  };

Constructor & Destructor Documentation

◆ ModXMLData()

ModXMLData::ModXMLData ( const TiXmlElement *  xml,
Source  source = Source::Unknown 
)

Definition at line 56 of file C4StartupModsDlg.cpp.

57 {
58 
59  // Remember whether the loaded the element from a local file / overview, because we
60  // then still have to do a query if we want to update.
61  this->source = source;
62  // It is possible to instantiate this without valid XML.
63  if (xml == nullptr)
64  {
65  metadataMissing = true;
66  return;
67  }
68  // Remember the XML element in case we need to pretty-print it later.
69  originalXMLElement = xml->Clone();
70  id = getSafeStringValue(xml, "id", "");
71  title = getSafeStringValue(xml, "title", "");
72  assert(IsValidUtf8(title.c_str()));
73  slug = getSafeStringValue(xml, "slug", title);
74  assert(IsValidUtf8(slug.c_str()));
75  description = getSafeStringValue(xml, "description");
76  assert(IsValidUtf8(description.c_str()));
77  longDescription = getSafeStringValue(xml, "long_description");
78  if (!description.empty())
79  {
80  const bool descriptionNeedsCut = description.size() > 150;
81  if (descriptionNeedsCut)
82  {
83  // Find a cutoff that is not inside a UTF-8 character.
84  std::string::size_type characterCount{ 0 };
85  for (const char* c = description.data(); *c != '\0';)
86  {
87  const uint8_t val = *c;
88  if (val <= 127)
89  {
90  c += 1;
91  }
92  else
93  {
95  }
96  characterCount += 1;
97 
98  const bool shouldCutHere = characterCount >= 150;
99  if (shouldCutHere)
100  {
101  uint32_t byteDifference = c - description.data();
102  description.resize(byteDifference);
103  break;
104  }
105  }
106  }
107  }
108  assert(IsValidUtf8(description.c_str()));
109  // Additional meta-information.
110 
111  const TiXmlElement* dependencies_node = xml->FirstChildElement("dependencies");
112  if (dependencies_node != nullptr)
113  for (const TiXmlElement *node = dependencies_node->FirstChildElement("item"); node != nullptr; node = node->NextSiblingElement("item"))
114  {
115  const char *depID = node->GetText();
116  if (depID != nullptr)
117  dependencies.push_back(depID);
118  }
119 
120  const TiXmlElement* tags_node = xml->FirstChildElement("tags");
121  if (tags_node != nullptr)
122  for (const TiXmlElement *node = tags_node->FirstChildElement("item"); node != nullptr; node = node->NextSiblingElement("item"))
123  {
124  const std::string tag = node->GetText();
125  if (!tag.empty())
126  tags.push_back(tag);
127  }
128 
129  const TiXmlElement* files_node = xml->FirstChildElement("files");
130  if (files_node != nullptr)
131  for (const TiXmlElement *filenode = files_node->FirstChildElement("item"); filenode != nullptr; filenode = filenode->NextSiblingElement("item"))
132  {
133  // We guarantee that we do not modify the handle below, thus the const_cast is safe.
134  const TiXmlHandle nodeHandle(const_cast<TiXmlNode*> (static_cast<const TiXmlNode*> (filenode)));
135 
136  const std::string handle = getSafeStringValue(filenode, "id", "");
137  const std::string name = getSafeStringValue(filenode, "filename", "");
138  const std::string lengthString = getSafeStringValue(filenode, "length", "");
139 
140  const std::string hashSHA1 = getSafeStringValue(filenode, "sha1", "");
141 
142  if (handle.empty() || name.empty() || lengthString.empty()) continue;
143  size_t length{ 0 };
144 
145  try
146  {
147  length = std::stoi(lengthString);
148  }
149  catch (...)
150  {
151  continue;
152  }
153 
154  files.emplace_back(FileInfo{ handle, length, name, hashSHA1 });
155  }
156 }
std::string getSafeStringValue(const TiXmlElement *xml, const char *childName, std::string fallback="", bool isAttribute=false)
bool IsValidUtf8(const char *text, int length)
Definition: Standard.cpp:702
uint32_t GetNextUTF8Character(const char **pszString)
Definition: Standard.cpp:755
std::string slug
std::string longDescription
bool metadataMissing
std::string description
std::string title
std::vector< std::string > dependencies
std::vector< FileInfo > files
TiXmlNode * originalXMLElement
std::vector< std::string > tags

References dependencies, description, files, GetNextUTF8Character(), getSafeStringValue(), IsValidUtf8(), longDescription, metadataMissing, originalXMLElement, slug, source, tags, and title.

Here is the call graph for this function:

◆ ~ModXMLData()

ModXMLData::~ModXMLData ( )

Definition at line 158 of file C4StartupModsDlg.cpp.

159 {
160  delete originalXMLElement;
161  originalXMLElement = nullptr;
162 }

References originalXMLElement.

Member Function Documentation

◆ requiresUpdate()

bool ModXMLData::requiresUpdate ( ) const
inline

Definition at line 62 of file C4StartupModsDlg.h.

References DetailView, and source.

Member Data Documentation

◆ dependencies

std::vector<std::string> ModXMLData::dependencies

Definition at line 45 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ description

std::string ModXMLData::description

Definition at line 49 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ files

std::vector<FileInfo> ModXMLData::files

Definition at line 44 of file C4StartupModsDlg.h.

Referenced by C4StartupModsListEntry::GetFileInfos(), and ModXMLData().

◆ id

std::string ModXMLData::id

Definition at line 48 of file C4StartupModsDlg.h.

Referenced by C4StartupModsListEntry::GetID().

◆ longDescription

std::string ModXMLData::longDescription

Definition at line 49 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ metadataMissing

bool ModXMLData::metadataMissing { false }

Definition at line 51 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ originalXMLElement

TiXmlNode* ModXMLData::originalXMLElement { nullptr }

Definition at line 68 of file C4StartupModsDlg.h.

Referenced by C4StartupModsListEntry::GetXMLNode(), ModXMLData(), and ~ModXMLData().

◆ slug

std::string ModXMLData::slug

Definition at line 50 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ source

Source ModXMLData::source { Source::Unknown }

◆ tags

std::vector<std::string> ModXMLData::tags

Definition at line 46 of file C4StartupModsDlg.h.

Referenced by ModXMLData().

◆ title

std::string ModXMLData::title

Definition at line 47 of file C4StartupModsDlg.h.

Referenced by C4StartupModsListEntry::GetTitle(), and ModXMLData().


The documentation for this struct was generated from the following files: