Skip to content

Commit

Permalink
Fix Cesium Native
Browse files Browse the repository at this point in the history
  • Loading branch information
j9liu committed Dec 8, 2023
1 parent 8eaff58 commit 4274222
Show file tree
Hide file tree
Showing 7 changed files with 621 additions and 285 deletions.
304 changes: 22 additions & 282 deletions Runtime/CesiumMetadataValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,57 +121,12 @@ public Boolean GetBoolean(Boolean defaultValue = false)
/// <returns>The property value as a SByte.</returns>
public SByte GetSByte(SByte defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToSByte(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToSByte(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToSByte(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -200,57 +155,12 @@ public SByte GetSByte(SByte defaultValue = 0)
/// <returns>The property value as a Byte.</returns>
public Byte GetByte(Byte defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToByte(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToByte(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToByte(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -279,57 +189,12 @@ public Byte GetByte(Byte defaultValue = 0)
/// <returns>The property value as a Int16.</returns>
public Int16 GetInt16(Int16 defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToInt16(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToInt16(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToInt16(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -359,57 +224,12 @@ public Int16 GetInt16(Int16 defaultValue = 0)
/// <returns>The property value as a UInt16.</returns>
public UInt16 GetUInt16(UInt16 defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToUInt16(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToUInt16(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToUInt16(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -440,57 +260,12 @@ public UInt16 GetUInt16(UInt16 defaultValue = 0)
/// <returns>The property value as a Int32.</returns>
public Int32 GetInt32(Int32 defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToInt32(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToInt32(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToInt32(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -519,57 +294,12 @@ public Int32 GetInt32(Int32 defaultValue = 0)
/// <returns>The property value as a UInt32.</returns>
public UInt32 GetUInt32(UInt32 defaultValue = 0)
{
if (this.isEmpty)
{
return defaultValue;
}

CesiumMetadataValueType valueType = this.valueType;
if (valueType.isArray)
if (this.isEmpty || this.valueType.isArray)
{
return defaultValue;
}

switch (valueType.type)
{
case CesiumMetadataType.Boolean:
case CesiumMetadataType.String:
try
{
return Convert.ToUInt32(this.valueImpl);
}
catch
{
// The above may throw if trying to convert an invalid string.
return defaultValue;
}
case CesiumMetadataType.Scalar:
// We need to explicitly truncate floating-point values. Otherwise,
// Convert will round to the nearest number.
System.Object value = this.valueImpl;
switch (valueType.componentType)
{
case CesiumMetadataComponentType.Float32:
value = Math.Truncate((value as float?).Value);
break;
case CesiumMetadataComponentType.Float64:
value = Math.Truncate((value as double?).Value);
break;
default:
break;
}
try
{
return Convert.ToUInt32(value);
}
catch
{
// The above may throw if trying to convert an out-of-range-number.
return defaultValue;
}
default:
return defaultValue;
}
return ConvertToUInt32(this, defaultValue);
}

/// <summary>
Expand Down Expand Up @@ -1194,6 +924,16 @@ internal static String GetObjectAsString(System.Object inObject)

#region Internal static partial methods
internal static partial bool ConvertToBoolean(CesiumMetadataValue value, bool defaultValue);
internal static partial SByte ConvertToSByte(CesiumMetadataValue value, SByte defaultValue);
internal static partial Byte ConvertToByte(CesiumMetadataValue value, Byte defaultValue);
internal static partial Int16 ConvertToInt16(CesiumMetadataValue value, Int16 defaultValue);
internal static partial UInt16 ConvertToUInt16(CesiumMetadataValue value, UInt16 defaultValue);
internal static partial Int32 ConvertToInt32(CesiumMetadataValue value, Int32 defaultValue);
internal static partial UInt32 ConvertToUInt32(CesiumMetadataValue value, UInt32 defaultValue);
internal static partial Int64 ConvertToInt64(CesiumMetadataValue value, Int64 defaultValue);
internal static partial UInt64 ConvertToUInt64(CesiumMetadataValue value, UInt64 defaultValue);
internal static partial String ConvertToString(CesiumMetadataValue value, String defaultValue);

#endregion
}
}
4 changes: 4 additions & 0 deletions Runtime/CesiumMetadataValueType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,10 @@ public static CesiumMetadataValueType GetValueType(System.Object inObject)
return new CesiumMetadataValueType(CesiumMetadataType.Scalar, CesiumMetadataComponentType.Int64, false);
case UInt64:
return new CesiumMetadataValueType(CesiumMetadataType.Scalar, CesiumMetadataComponentType.Uint64, false);
case float:
return new CesiumMetadataValueType(CesiumMetadataType.Scalar, CesiumMetadataComponentType.Float32, false);
case double:
return new CesiumMetadataValueType(CesiumMetadataType.Scalar, CesiumMetadataComponentType.Float64, false);
case int2:
return new CesiumMetadataValueType(CesiumMetadataType.Vec2, CesiumMetadataComponentType.Int32, false);
case int3:
Expand Down
2 changes: 1 addition & 1 deletion Runtime/ConfigureReinterop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ Cesium3DTilesetLoadFailureDetails tilesetDetails
valueType = myValue.valueType;

System.Object myObject = myValue.valueImpl;

CesiumMetadataValue.GetObjectAsBoolean(myObject);
CesiumMetadataValue.GetObjectAsSByte(myObject);
CesiumMetadataValue.GetObjectAsByte(myObject);
Expand Down
Loading

0 comments on commit 4274222

Please sign in to comment.