Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.42 KB | None | 0 0
  1. using BlamCore.Serialization;
  2. using System;
  3.  
  4. namespace BlamCore.Common
  5. {
  6.     [TagStructure(Size = 0x14)]
  7.     public class TagFunction
  8.     {
  9.         public byte[] Data;
  10.  
  11.         public enum TagFunctionType : byte
  12.         {
  13.             Identity,
  14.             Constant,
  15.             Transition,
  16.             Periodic,
  17.             Linear,
  18.             LinearKey,
  19.             MultiLinearKey,
  20.             Spline,
  21.             MultiSpline,
  22.             Exponent,
  23.             Spline2
  24.         }
  25.  
  26.         [Flags]
  27.         public enum TagFunctionTypeFlag : byte
  28.         {
  29.            SetToMin = 1,
  30.            IsBounded = 4,
  31.  
  32.         }
  33.  
  34.         public float ComputeFunction(float input, float scale)
  35.         {
  36.             if (Data == null || Data.Length <= 0)
  37.                 return 0.0f;
  38.  
  39.             else
  40.             {
  41.                 TagFunctionType opcode = (TagFunctionType)Data[0];
  42.                 TagFunctionTypeFlag flags = (TagFunctionTypeFlag)Data[1];
  43.  
  44.                 float result = ComputeSubFunction(0, input, scale);
  45.  
  46.                 if (Data[2] >= 1)
  47.                     return result;
  48.  
  49.                 //Weird
  50.                 return (Data[2] - Data[1]) * result + Data[1];
  51.             }
  52.            
  53.         }
  54.         private float ComputeSubFunction(int index, float input, float scale)
  55.         {
  56.             float result = 0.0f;
  57.  
  58.             TagFunctionType opcode = (TagFunctionType) Data[index];
  59.             TagFunctionTypeFlag flags = (TagFunctionTypeFlag) Data[index + 1];
  60.  
  61.             switch (opcode)
  62.             {
  63.                 case TagFunctionType.Identity:
  64.                     result = input;
  65.                     break;
  66.  
  67.                 case TagFunctionType.Constant:
  68.                     if (flags.HasFlag(TagFunctionTypeFlag.SetToMin))
  69.                         result = scale;
  70.                     else
  71.                         result = 0.0f;
  72.                     break;
  73.  
  74.                 case TagFunctionType.Transition:
  75.  
  76.                     float min = Transition(index + 8, input);
  77.  
  78.                     if (flags.HasFlag(TagFunctionTypeFlag.SetToMin))
  79.                     {
  80.                         result = min;                                 //Label24 is v6=v10 which is result = temp
  81.                         break;
  82.                     }
  83.  
  84.                     float max = Transition(index + 11, input);
  85.                     result = (max - min) * scale + min;
  86.                     //Label25 is making some variable 0, but it seems to be always 0
  87.                     break;
  88.  
  89.                 default:
  90.                     break;
  91.             }
  92.  
  93.             if (flags.HasFlag(TagFunctionTypeFlag.IsBounded))
  94.             {
  95.                 if (result < 0.0f)
  96.                     result = 0.0f;
  97.                 if (result - 1.0f >= 0.0f)
  98.                     result = 1.0f;
  99.             }
  100.  
  101.             return result;
  102.         }
  103.  
  104.         private float Transition(int index, float value)
  105.         {
  106.             float max = getFloatFromBytes(index + 8);
  107.             float min = getFloatFromBytes(index + 4);
  108.  
  109.             float scale = 0.0f; // another function call
  110.  
  111.             return (max - min) * scale + min;
  112.         }
  113.  
  114.         private float getFloatFromBytes(int index)
  115.         {
  116.             byte[] temp = new byte[4];
  117.             Array.Copy(Data, index, temp, 0, 4);
  118.             Array.Reverse(temp);
  119.             return BitConverter.ToSingle(temp, index);
  120.         }
  121.  
  122.     }
  123.    
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement