Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Weermann (TGE) committed Dec 5, 2018
0 parents commit 917413b
Show file tree
Hide file tree
Showing 36 changed files with 14,033 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"# 010-Editor-Templates"
2 changes: 2 additions & 0 deletions common/include.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#include "../common/types.h"
#include "../common/utils.h"
164 changes: 164 additions & 0 deletions common/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
typedef char bool;
typedef char s8;
typedef uchar u8;
typedef int16 s16;
typedef uint16 u16;
typedef int16 s16;
typedef int32 s32;
typedef uint32 u32;
typedef int64 s64;
typedef uint64 u64;
typedef hfloat f16;
typedef float f32;
typedef double f64;

typedef struct
{
f32 X;
f32 Y;
} Vector2 <read=Vector2ToString>;

string Vector2ToString( Vector2& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f]", value.X, value.Y );

return buffer;
}

typedef struct
{
f16 X;
f16 Y;
} Vector2Half <read=Vector2HalfToString>;

string Vector2HalfToString( Vector2Half& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f]", value.X, value.Y );

return buffer;
}

typedef struct
{
f32 X;
f32 Y;
f32 Z;
} Vector3 <read=Vector3ToString>;

string Vector3ToString( Vector3& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f, %.6f]", value.X, value.Y, value.Z );

return buffer;
}

typedef struct
{
f32 X;
f32 Y;
f32 Z;
f32 W;
} Vector4 <read=Vector4ToString>;

string Vector4ToString( Vector4& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f, %.6f, %.6f]", value.X, value.Y, value.Z, value.W );

return buffer;
}

typedef struct
{
Vector3 Min;
Vector3 Max;
} BoundingBox <read=BoundingBoxToString>;

string BoundingBoxToString( BoundingBox& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f, %.6f] [%.6f, %.6f, %.6f]", value.Min.X, value.Min.Y, value.Min.Z, value.Max.X, value.Max.Y, value.Max.Z );

return buffer;
}

typedef struct
{
Vector3 Center;
f32 Radius;
} BoundingSphere <read=BoundingSphereToString>;

string BoundingSphereToString( BoundingSphere& value )
{
local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f, %.6f] %.6f", value.Center.X, value.Center.Y, value.Center.Z, value.Radius );

return buffer;
}

typedef struct
{
u32 Value;
} Normal11_11_10ToString <read=ReadNormal11_11_10>;

string ReadNormal11_11_10( Normal11_11_10ToString& value )
{
const int FULL_WIDTH = 32;

const int X_POS = 0;
const int X_WIDTH = 11;
const int X_LSHIFT = FULL_WIDTH - X_POS - X_WIDTH;
const int X_RSHIFT = FULL_WIDTH - X_WIDTH;
const int X_DOT = (1 << (X_WIDTH - 1)) - 1;

const int Y_POS = X_POS + X_WIDTH;
const int Y_WIDTH = 11;
const int Y_LSHIFT = FULL_WIDTH - Y_POS - Y_WIDTH;
const int Y_RSHIFT = FULL_WIDTH - Y_WIDTH;
const int Y_DOT = (1 << (Y_WIDTH - 1)) - 1;

const int Z_POS = Y_POS + Y_WIDTH;
const int Z_WIDTH = 10;
const int Z_LSHIFT = FULL_WIDTH - Z_POS - Z_WIDTH;
const int Z_RSHIFT = FULL_WIDTH - Z_WIDTH;
const int Z_DOT = (1 << (Z_WIDTH - 1)) - 1;

u32 packed = value.Value;

int xInt = ((int)(packed << X_LSHIFT)) >> X_RSHIFT;
int yInt = ((int)(packed << Y_LSHIFT)) >> Y_RSHIFT;
int zInt = ((int)(packed << Z_LSHIFT)) >> Z_RSHIFT;

f32 x = (float)xInt / (float)X_DOT;
f32 y = (float)yInt / (float)Y_DOT;
f32 z = (float)zInt / (float)Z_DOT;

local char buffer[255];
SPrintf( buffer, "[%.6f, %.6f, %.6f]", x, y, z );

return buffer;
}

typedef struct( u32 base )
{
u32 Offset;

if ( Offset != 0 )
{
local u32 returnPos = FTell();
FSeek( base + Offset );
string Value;
FSeek( returnPos );
}
} StringOffset <read=StringOffsetToString>;

string StringOffsetToString( StringOffset& value )
{
if ( value.Offset == 0 ) return "";
return value.Value;
}


52 changes: 52 additions & 0 deletions common/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//---------------------------------------------
// Random color
//---------------------------------------------
local u32 __RandomSeed = 0xDEADBABE;
local u32 __RandomBit = 0;
local u32 __RandomCount = 0;

u32 MyRandom( u32 to )
{
++__RandomCount;
__RandomBit = ( (__RandomSeed >> 0 ) ^ ( __RandomSeed >> 2 ) ^ ( __RandomSeed >> 3 ) ^ ( __RandomSeed >> 5 ) ) & 1;
__RandomSeed = ( ( ( ( __RandomBit << 15 ) | ( __RandomSeed >> 1 ) ) + ( 0xBABE / __RandomCount ) ) % to );

while( __RandomSeed < 0 )
__RandomSeed += to;

return __RandomSeed;
}

u32 RandomColor()
{
return MyRandom( 0xFFFFFFFF );
}

void SetRandomBackColor()
{
SetBackColor( RandomColor() );
}

// Generate u32 from FourCC in string format
u32 FourCC( string str )
{
return (u32)str[0] << 24 | (u32)str[1] << 16 | (u32)str[2] << 8 | (u32)str[3];
}

u32 MakeFourCC( string str )
{
if ( IsLittleEndian() )
return (u32)str[0] | (u32)str[1] << 8 | (u32)str[2] << 16 | (u32)str[3] << 24;
else
return (u32)str[3] | (u32)str[2] << 8 | (u32)str[1] << 16 | (u32)str[0] << 24;
}

u32 Align( u32 value, u32 alignment )
{
return (value + (alignment - 1)) & ~(alignment - 1);
}

void FAlign( u32 alignment )
{
FSeek( Align( FTell(), alignment ) );
}
142 changes: 142 additions & 0 deletions scripts/analyzer.1sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//------------------------------------------------
//--- 010 Editor v8.0 Script File
//
// File: Analyzer.1sc
// Authors: TGE
// Version: 1.0.0
// Purpose: Analyzing binary data into value types.
// Category:
// History:
//------------------------------------------------

#include "../common/include.h"

enum FieldValueType
{
FieldValueType_Invalid = 0,
FieldValueType_Byte,
FieldValueType_Short,
FieldValueType_Int,
FieldValueType_Float
};

// Globals
u32 gInFile = GetFileNum();
u32 gOutFile = FileNew();
FileSelect( gInFile );
u32 gStartOffset = GetSelStart();
u32 gEndOffset = gStartOffset + GetSelSize();
FieldValueType gValueTypes[4096];

// Map FieldValueType to its byte size.
u32 GetValueTypeSize( FieldValueType type )
{
switch ( type )
{
case FieldValueType_Byte: return 1;
case FieldValueType_Short: return 2;
case FieldValueType_Int:
case FieldValueType_Float:
return 4;

default:
return 0;
}
}

// Match floats by checking if the high bits fall within a common range.
bool IsFloat( u32 value )
{
u32 highBits = ( value & 0xFF000000 );
return highBits >= 0x30000000 && highBits <= 0xD0000000;
}

// Match shorts whose value is less than 0x1000
bool IsShort( u32 value )
{
return ( ( value & 0xF0000000 ) == 0 && ( value & 0x0FFF0000 ) != 0 ) &&
( ( value & 0x0000F000 ) == 0 && ( value & 0x00000FFF ) != 0 );
}

// Match byte patterns where each byte is less than 0x10
bool IsBytes( u32 value )
{
u32 mask = 0xF0F0F0F0;
if ( ( value & mask ) == 0 )
{
u32 count = 0;
u32 i;
for ( i = 0; i < 4; ++i )
count += ( value & ( 0x0F << ( i * 8 ) ) ) != 0;

return count >= 3;
}

return false;
}

void AnalyzeRange( u32 startOffset, u32 endOffset )
{
int value;
int numRemainingBytes;
int i;
int fieldIndex = 0;
int offset = startOffset;

while ( offset < endOffset )
{
// Assume int by default
value = ReadInt( offset );
numRemainingBytes = endOffset - offset;

// Analyze type
if ( numRemainingBytes >= 4 && IsFloat( value ) )
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Float;
offset += 4;
}
else if ( IsBytes( value ) )
{
for ( i = 0; i < 4; ++i )
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Byte;
offset += 1;
}
}
else if ( numRemainingBytes > 2 && IsShort( value ) || numRemainingBytes == 2 )
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Short;
offset += 2;

if ( numRemainingBytes >= 4 )
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Short;
offset += 2;
}
}
else if ( numRemainingBytes >= 4 )
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Int;
offset += 4;
}
else
{
gValueTypes[ fieldIndex++ ] = FieldValueType_Byte;
offset += 1;
}
}
}

void AnalyzeSelection()
{
AnalyzeRange( gStartOffset, gEndOffset );
}

void FlushToClipboard()
{
FileSelect( gOutFile );
SetSelection( 0, FileSize() );
CutToClipboard();
FileClose();
FileSelect( gInFile );
}
Loading

0 comments on commit 917413b

Please sign in to comment.