示例#1
0
        public static bool TryReadInt64(this CResGFF resGff, CResStruct resStruct, byte *fieldName, out long value)
        {
            int bSuccess;

            value = resGff.ReadFieldINT64(resStruct, fieldName, &bSuccess);
            return(bSuccess.ToBool());
        }
示例#2
0
        public static bool CreateFromResRef(ResRefType resRefType, string resRef, Action <CResGFF, CResStruct> deserializeAction)
        {
            if (string.IsNullOrEmpty(resRef))
            {
                return(false);
            }

            if (!ResourceManager.IsValidResource(resRef, resRefType))
            {
                return(false);
            }

            CResGFF resGff = new CResGFF((ushort)resRefType, $"{resRefType} ".GetNullTerminatedString(), resRef.ToResRef());

            if (!resGff.m_bLoaded.ToBool())
            {
                Log.Warn($"Unable to load ResRef: {resRef}");
                return(false);
            }

            CResStruct resStruct = new CResStruct();

            resGff.GetTopLevelStruct(resStruct).ToBool();
            deserializeAction(resGff, resStruct);

            resStruct.Dispose();
            resGff.Dispose();
            return(true);
        }
示例#3
0
        public static bool TryReadShort(this CResGFF resGff, CResStruct resStruct, byte *fieldName, out short value)
        {
            int bSuccess;

            value = resGff.ReadFieldSHORT(resStruct, fieldName, &bSuccess);
            return(bSuccess.ToBool());
        }
示例#4
0
        public static bool TryReadByte(this CResGFF resGff, CResStruct resStruct, byte *fieldName, out byte value)
        {
            int bSuccess;

            value = resGff.ReadFieldBYTE(resStruct, fieldName, &bSuccess);
            return(bSuccess.ToBool());
        }
示例#5
0
        public static bool TryReadDWord(this CResGFF resGff, CResStruct resStruct, byte *fieldName, out uint value)
        {
            int bSuccess;

            value = resGff.ReadFieldDWORD(resStruct, fieldName, &bSuccess);
            return(bSuccess.ToBool());
        }
示例#6
0
        public static bool TryReadCExoString(this CResGFF resGff, CResStruct resStruct, byte *fieldName, out CExoString value)
        {
            int bSuccess;

            value = resGff.ReadFieldCExoString(resStruct, fieldName, &bSuccess);
            return(bSuccess.ToBool());
        }
示例#7
0
        private static byte[]? SerializeGff(CExoString fileType, CExoString version, Func <CResGFF, CResStruct, bool> serializeAction)
        {
            void *pData;
            int   dataLength;

            using CResGFF resGff       = new CResGFF();
            using CResStruct resStruct = new CResStruct();

            if (!resGff.CreateGFFFile(resStruct, fileType, version).ToBool())
            {
                return(null);
            }

            if (!serializeAction(resGff, resStruct))
            {
                return(null);
            }

            resGff.WriteGFFToPointer(&pData, &dataLength);

            byte[] serialized = new byte[dataLength];

            Marshal.Copy((IntPtr)pData, serialized, 0, dataLength);
            Marshal.FreeHGlobal((IntPtr)pData);

            return(serialized);
        }
示例#8
0
        private void OnSaveToGff(void *pUUID, void *pRes, void *pStruct)
        {
            CNWSUUID   uuid      = CNWSUUID.FromPointer(pUUID);
            CResGFF    resGff    = CResGFF.FromPointer(pRes);
            CResStruct resStruct = CResStruct.FromPointer(pStruct);

            string?serialized = GetObjectStorage(uuid.m_parent).Serialize();

            resGff.WriteFieldCExoString(resStruct, serialized.ToExoString(), AnvilGffFieldNamePtr);

            saveToGffHook.CallOriginal(pUUID, pRes, pStruct);
        }
示例#9
0
 internal GffResourceFieldList(CResGFF resGff, CResList list, uint count) : base(resGff)
 {
     for (uint i = 0; i < count; i++)
     {
         CResStruct             resStruct  = new CResStruct();
         GffResourceFieldStruct?childField = ResGff.GetListElement(resStruct, list, i).ToBool() ? new GffResourceFieldStruct(ResGff, resStruct) : null;
         if (childField != null)
         {
             children.Add(childField);
         }
     }
 }
示例#10
0
        internal GffResource(string name, CResGFF resGff)
        {
            this.resGff = resGff;

            FileType = resGff.m_pFileType.ReadFixedLengthString().Trim();

            rootStruct = new CResStruct();
            if (!resGff.GetTopLevelStruct(rootStruct).ToBool())
            {
                throw new ArgumentException($"Failed to initialize top level structure in gff resource {name}", nameof(resGff));
            }
        }
示例#11
0
        public static bool IsValidGff(this CResGFF resGff, IEnumerable <string> expectedFileTypes, IEnumerable <string> expectedVersions)
        {
            CExoString sFileType    = new CExoString();
            CExoString sFileVersion = new CExoString();

            resGff.GetGFFFileInfo(sFileType, sFileVersion);

            string fileType    = sFileType.ToString();
            string fileVersion = sFileVersion.ToString();

            return(expectedVersions.Any(expectedVersion => expectedVersion == fileVersion) &&
                   expectedFileTypes.Any(expectedFileType => expectedFileType + " " == fileType));
        }
示例#12
0
        /// <summary>
        /// Gets the specified Gff resource.
        /// </summary>
        /// <param name="name">The resource name to fetch, without any filetype extensions.</param>
        /// <param name="type">The type of the file/resource.</param>
        /// <returns>A <see cref="GffResource"/> representation of the specified resource if it exists, otherwise null.</returns>
        public GffResource?GetGenericFile(string name, ResRefType type)
        {
            CResRef resRef = new CResRef(name);

            if (!ResMan.Exists(resRef, (ushort)type).ToBool())
            {
                return(null);
            }

            CResGFF gff = new CResGFF((ushort)type, $"{type.ToString()} ".GetNullTerminatedString(), resRef);

            return(new GffResource(name, gff));
        }
示例#13
0
        private int OnLoadFromGff(void *pUUID, void *pRes, void *pStruct)
        {
            CNWSUUID   uuid      = CNWSUUID.FromPointer(pUUID);
            CResGFF    resGff    = CResGFF.FromPointer(pRes);
            CResStruct resStruct = CResStruct.FromPointer(pStruct);

            bool hasAnvilPos = resGff.TryReadCExoString(resStruct, AnvilGffFieldNamePtr, out CExoString anvilSerialized);
            bool hasNwnxPos  = resGff.TryReadCExoString(resStruct, NWNXGffFieldNamePtr, out CExoString nwnxSerialized);

            if (!hasAnvilPos && !hasNwnxPos)
            {
                return(loadFromGffHook.CallOriginal(pUUID, pRes, pStruct));
            }

            ObjectStorage storage = GetObjectStorage(uuid.m_parent);

            storage.Clear();

            if (hasNwnxPos)
            {
                try
                {
                    storage.Deserialize(nwnxSerialized.ToString());
                }
                catch (Exception e)
                {
                    Log.Error(e, "Failed to import NWNX object storage");
                }
            }

            if (hasAnvilPos)
            {
                try
                {
                    storage.Deserialize(anvilSerialized.ToString());
                }
                catch (Exception e)
                {
                    Log.Error(e, "Failed to load Anvil object storage");
                }
            }

            return(loadFromGffHook.CallOriginal(pUUID, pRes, pStruct));
        }
示例#14
0
        public static bool DeserializeGff(byte[] serialized, Func <CResGFF, CResStruct, bool> deserializeAction)
        {
            // GFF header size
            if (serialized.Length < 14 * 4)
            {
                return(false);
            }

            CResGFF    resGff    = new CResGFF();
            CResStruct resStruct = new CResStruct();

            IntPtr dataPtr = Marshal.AllocHGlobal(serialized.Length);

            Marshal.Copy(serialized, 0, dataPtr, serialized.Length);

            void *data = (void *)dataPtr;

            if (!resGff.GetDataFromPointer(data, serialized.Length, true).ToBool())
            {
                Marshal.FreeHGlobal(dataPtr);
                return(false);
            }

            resGff.InitializeForWriting();
            if (!resGff.GetTopLevelStruct(resStruct).ToBool())
            {
                Marshal.FreeHGlobal(dataPtr);
                return(false);
            }

            if (deserializeAction(resGff, resStruct))
            {
                GC.SuppressFinalize(resGff);
                GC.SuppressFinalize(resStruct);
                return(true);
            }

            Marshal.FreeHGlobal(dataPtr);
            return(false);
        }
示例#15
0
        internal GffResourceFieldStruct(CResGFF resGff, CResStruct resStruct) : base(resGff)
        {
            int fieldCount = (int)resGff.GetFieldCount(resStruct);
            List <KeyValuePair <string, GffResourceField> > entrySet = new List <KeyValuePair <string, GffResourceField> >();

            for (uint i = 0; i < fieldCount; i++)
            {
                byte *           fieldIdPtr = ResGff.GetFieldLabel(resStruct, i);
                string           key        = StringHelper.ReadNullTerminatedString(fieldIdPtr);
                GffResourceField?value      = Create(resGff, resStruct, i, fieldIdPtr);

                if (value == null)
                {
                    continue;
                }

                keys.Add(key);
                values.Add(value);
                fieldLookup.Add(key, value);
                entrySet.Add(new KeyValuePair <string, GffResourceField>(key, value));
            }

            EntrySet = entrySet;
        }
示例#16
0
 internal GffResourceFieldValue(CResGFF resGff, CResStruct parentStruct, byte *fieldId) : base(resGff)
 {
     this.parentStruct = parentStruct;
     this.fieldId      = fieldId;
 }
示例#17
0
 protected GffResourceField(CResGFF resGff)
 {
     ResGff = resGff;
 }
示例#18
0
 public static bool IsValidGff(this CResGFF resGff, string expectedFileType, string expectedVersion = DefaultGffVersion)
 {
     return(IsValidGff(resGff, expectedFileType.Yield(), expectedVersion.Yield()));
 }