don't export invalid objects

This commit is contained in:
Ted John 2016-07-09 14:52:56 +01:00
parent a25d7aa535
commit 8c2393f5c5
4 changed files with 47 additions and 14 deletions

View File

@ -167,6 +167,30 @@ namespace ObjectFactory
return result;
}
Object * CreateObjectFromLegacyData(const rct_object_entry * entry, const void * data, size_t dataSize)
{
Guard::ArgumentNotNull(entry);
Guard::ArgumentNotNull(data);
Object * result = CreateObject(*entry);
if (result != nullptr)
{
utf8 objectName[9] = { 0 };
Memory::Copy(objectName, entry->name, 8);
auto readContext = ReadObjectContext(objectName);
auto chunkStream = MemoryStream(data, dataSize);
ReadObjectLegacy(result, &readContext, &chunkStream);
if (readContext.WasError())
{
delete result;
result = nullptr;
}
}
return result;
}
Object * CreateObject(const rct_object_entry &entry)
{
Object * result = nullptr;

View File

@ -23,5 +23,6 @@ class Object;
namespace ObjectFactory
{
Object * CreateObjectFromLegacyFile(const utf8 * path);
Object * CreateObjectFromLegacyData(const rct_object_entry * entry, const void * data, size_t dataSize);
Object * CreateObject(const rct_object_entry &entry);
}

View File

@ -438,7 +438,7 @@ private:
{
utf8 objName[9] = { 0 };
Memory::Copy(objName, entry->name, 8);
Console::Error::WriteFormat("[%s]: Object not found.", objName);
Console::Error::WriteFormat("[%s] Object not found.", objName);
Console::Error::WriteLine();
}
@ -446,7 +446,7 @@ private:
{
utf8 objName[9] = { 0 };
Memory::Copy(objName, entry->name, 8);
Console::Error::WriteFormat("[%s]: Object could not be loaded.", objName);
Console::Error::WriteFormat("[%s] Object could not be loaded.", objName);
Console::Error::WriteLine();
}

View File

@ -207,20 +207,28 @@ public:
// TODO append checksum match bytes
// TODO check object is loadable before writing it
utf8 path[MAX_PATH];
GetPathForNewObject(path, sizeof(path), objectName);
Console::WriteLine("Adding object: [%s]", objectName);
try
// Check that the object is loadable before writing it
Object * object = ObjectFactory::CreateObjectFromLegacyData(objectEntry, data, dataSize);
if (object == nullptr)
{
SaveObject(path, objectEntry, data, dataSize);
ScanObject(path);
Console::Error::WriteFormat("[%s] Unable to export object.", objectName);
Console::Error::WriteLine();
}
catch (Exception ex)
else
{
Console::WriteLine("Failed saving object: [%s] to '%s'.", objectName, path);
utf8 path[MAX_PATH];
GetPathForNewObject(path, sizeof(path), objectName);
Console::WriteLine("Adding object: [%s]", objectName);
try
{
SaveObject(path, objectEntry, data, dataSize);
ScanObject(path);
}
catch (Exception ex)
{
Console::WriteLine("Failed saving object: [%s] to '%s'.", objectName, path);
}
}
}
@ -895,6 +903,6 @@ static void ReportMissingObject(const rct_object_entry * entry)
{
utf8 objName[9] = { 0 };
Memory::Copy(objName, entry->name, 8);
Console::Error::WriteFormat("[%s]: Object not found.", objName);
Console::Error::WriteFormat("[%s] Object not found.", objName);
Console::Error::WriteLine();
}