SCJMapper-V2/SC/CryXMLlib/Conversions.cs
bm98 ac0ca1c208 V 2.23 - BETA Build 57
- update - Using .Net 4.5.2 Now (seems to handle some scaling issues WinForm apps)
- update - Try to find the SC path also as StarCitizen\Live (instead of Public) was mentioned for SC2.2.2 onwards ???
- fix - addbind UNDEF removed when assigned
- improvement - Issue a infobox if the Client folder cannot be found
               (please submit the complete folder structure of your installation as bug report ...)
- add - a table display for mappings
- some internal stuff (namespaces etc)
- update - doc SCJMapper_QGuide V2.23beta.pdf
2016-05-27 17:13:24 +02:00

104 lines
2.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace SCJMapper_V2.CryXMLlib
{
internal static class Extensions
{
/// <summary>
/// Get the array slice between the two indexes.
/// ... Inclusive for start index, exclusive end
/// </summary>
public static T[] SliceE<T>( this T[] source, UInt32 start, UInt32 end )
{
// Handles negative ends.
if ( end < 0 ) {
end = ( UInt32 )source.Length + end;
}
UInt32 len = end - start;
// Return new array.
T[] res = new T[len];
/*
for ( int i = 0; i < len; i++ ) {
resOLD[i] = source[i + start];
}
*/
Array.ConstrainedCopy( source, ( int )start, res, 0, ( int )len );
return res;
}
/// <summary>
/// Get the array slice between the two indexes.
/// ... Inclusive for start index, length.
/// </summary>
public static T[] SliceL<T>( this T[] source, UInt32 offset, UInt32 length )
{
UInt32 end = offset + length;
// Handles negative ends.
if ( end < 0 ) {
end = ( UInt32 )source.Length + end;
}
UInt32 len = end - offset;
// Return new array.
T[] res = new T[len];
/*
for ( int i = 0; i < len; i++ ) {
res[i] = source[i + offset];
}
*/
Array.ConstrainedCopy( source, ( int )offset, res, 0, ( int )len );
return res;
}
}
internal class Conversions
{
/// <summary>
/// Converts a number of ASCII chars into a String
/// </summary>
/// <param name="bPtr">Mem Location of the ASCII Chars</param>
/// <param name="size">Max number of ASCII chars to convert, stops at \0 however</param>
/// <returns>The converted string</returns>
static public string ToString( byte[] byteArr, uint size = 999 )
{
String s = "";
for ( uint i = 0; i < size; i++ ) {
if ( byteArr[i] != 0 )
s += Char.ConvertFromUtf32( byteArr[i] );
else
break; // stop at char 0
}
return s;
}
/// <summary>
/// Allocates and reads bytes of the size of one record
/// and returns the allocated bytes are structure - allowing structured access to binary data
/// Note: there is no error checking whatsoever here - so better make sure everything is OK
/// </summary>
/// <typeparam name="T">The record type to read</typeparam>
/// <param name="reader">A binary reader</param>
/// <returns>The read record</returns>
public static T ByteToType<T>( byte[] bytes, UInt32 offset = 0 )
{
byte[] _bytes = bytes.SliceL( offset, ( UInt32 )Marshal.SizeOf( typeof( T ) ) ); // lets see if this works with Alloc below..
GCHandle handle = GCHandle.Alloc( _bytes, GCHandleType.Pinned );
T theStructure = ( T )Marshal.PtrToStructure( handle.AddrOfPinnedObject( ), typeof( T ) );
handle.Free( );
return theStructure;
}
}
}