Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BlamCore.Serialization;
- using System;
- namespace BlamCore.Common
- {
- [TagStructure(Size = 0x14)]
- public class TagFunction
- {
- public byte[] Data;
- public enum TagFunctionType : byte
- {
- Identity,
- Constant,
- Transition,
- Periodic,
- Linear,
- LinearKey,
- MultiLinearKey,
- Spline,
- MultiSpline,
- Exponent,
- Spline2
- }
- [Flags]
- public enum TagFunctionTypeFlag : byte
- {
- SetToMin = 1,
- IsBounded = 4,
- }
- public float ComputeFunction(float input, float scale)
- {
- if (Data == null || Data.Length <= 0)
- return 0.0f;
- else
- {
- TagFunctionType opcode = (TagFunctionType)Data[0];
- TagFunctionTypeFlag flags = (TagFunctionTypeFlag)Data[1];
- float result = ComputeSubFunction(0, input, scale);
- if (Data[2] >= 1)
- return result;
- //Weird
- return (Data[2] - Data[1]) * result + Data[1];
- }
- }
- private float ComputeSubFunction(int index, float input, float scale)
- {
- float result = 0.0f;
- TagFunctionType opcode = (TagFunctionType) Data[index];
- TagFunctionTypeFlag flags = (TagFunctionTypeFlag) Data[index + 1];
- switch (opcode)
- {
- case TagFunctionType.Identity:
- result = input;
- break;
- case TagFunctionType.Constant:
- if (flags.HasFlag(TagFunctionTypeFlag.SetToMin))
- result = scale;
- else
- result = 0.0f;
- break;
- case TagFunctionType.Transition:
- float min = Transition(index + 8, input);
- if (flags.HasFlag(TagFunctionTypeFlag.SetToMin))
- {
- result = min; //Label24 is v6=v10 which is result = temp
- break;
- }
- float max = Transition(index + 11, input);
- result = (max - min) * scale + min;
- //Label25 is making some variable 0, but it seems to be always 0
- break;
- default:
- break;
- }
- if (flags.HasFlag(TagFunctionTypeFlag.IsBounded))
- {
- if (result < 0.0f)
- result = 0.0f;
- if (result - 1.0f >= 0.0f)
- result = 1.0f;
- }
- return result;
- }
- private float Transition(int index, float value)
- {
- float max = getFloatFromBytes(index + 8);
- float min = getFloatFromBytes(index + 4);
- float scale = 0.0f; // another function call
- return (max - min) * scale + min;
- }
- private float getFloatFromBytes(int index)
- {
- byte[] temp = new byte[4];
- Array.Copy(Data, index, temp, 0, 4);
- Array.Reverse(temp);
- return BitConverter.ToSingle(temp, index);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement