Example #1
0
        public void WhenCalculatingBundleHash_HashingBeginsAtFirstObject()
        {
            ArchiveAndCompressBundles.TaskInput input = GetDefaultInput();
            WriteResult result = AddSimpleBundle(input, "mybundle", "internalName");

            // Add a serialized. Say that the first object begins 100 bytes into the file
            var osi = new ObjectSerializedInfo();
            SerializedLocation header = new SerializedLocation();

            header.SetFileName(result.resourceFiles[0].fileAlias);
            header.SetOffset(100);
            osi.SetHeader(header);
            result.SetSerializedObjects(new ObjectSerializedInfo[] { osi });
            ResourceFile rf = result.resourceFiles[0];

            rf.SetSerializedFile(true);
            result.SetResourceFiles(new ResourceFile[] { rf });
            input.InternalFilenameToWriteResults["internalName"] = result;
            string srcFile = input.InternalFilenameToWriteResults["internalName"].resourceFiles[0].fileName;

            ArchiveAndCompressBundles.Run(input, out ArchiveAndCompressBundles.TaskOutput output);
            // Change the first 100 bytes. This is before the serialized object.
            WriteRandomData(srcFile, 100, 1);
            ArchiveAndCompressBundles.Run(input, out ArchiveAndCompressBundles.TaskOutput output2);

            // Change the first 104 bytes. This should affect the hash
            WriteRandomData(srcFile, 104, 2);
            ArchiveAndCompressBundles.Run(input, out ArchiveAndCompressBundles.TaskOutput output3);

            Assert.AreEqual(output.BundleDetails["mybundle"].Hash, output2.BundleDetails["mybundle"].Hash);
            Assert.AreNotEqual(output.BundleDetails["mybundle"].Hash, output3.BundleDetails["mybundle"].Hash);
        }
        // Create a populated 'SerializedLocation' instance.  Can set fields directly for 2019.4+ as internals are available but have to use reflection for 2018.4
        static SerializedLocation CreateSerializedLocation(string filename, uint offset, uint size)
        {
            SerializedLocation serializedLocation = new SerializedLocation();

#if UNITY_2019_4_OR_NEWER
            serializedLocation.m_FileName = filename;
            serializedLocation.m_Offset   = offset;
            serializedLocation.m_Size     = size;
#else
            SetFieldValue(serializedLocation, "m_FileName", filename);
            SetFieldValue(serializedLocation, "m_Offset", offset);
            SetFieldValue(serializedLocation, "m_Size", size);
#endif

            return(serializedLocation);
        }
        // Create a populated 'ObjectSerializedInfo' instance.  Can set fields directly for 2019.4+ as internals are available but have to use reflection for 2018.4
        static ObjectSerializedInfo CreateObjectSerializedInfo(ObjectIdentifier serializedObject, SerializedLocation header, SerializedLocation rawData)
        {
            ObjectSerializedInfo objectSerializedInfo = new ObjectSerializedInfo();

#if UNITY_2019_4_OR_NEWER
            objectSerializedInfo.m_SerializedObject = serializedObject;
            objectSerializedInfo.m_Header           = header;
            objectSerializedInfo.m_RawData          = rawData;
#else
            SetFieldValue(objectSerializedInfo, "m_SerializedObject", serializedObject);
            SetFieldValue(objectSerializedInfo, "m_Header", header);
            SetFieldValue(objectSerializedInfo, "m_RawData", rawData);
#endif

            return(objectSerializedInfo);
        }