using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace SRMultiplayer
{
public static class Utils
{
///
/// Loads up any resources embedded in the mod
///
/// Name of resource
/// Contents of resource
public static byte[] ExtractResource(String filename)
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
using (Stream resFilestream = a.GetManifestResourceStream(filename))
{
if (resFilestream == null) return null;
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
return ba;
}
}
///
/// Sets what layer the given item is at
///
public static void SetLayer(GameObject obj, int layer)
{
obj.layer = layer;
foreach (Transform child in obj.transform)
{
SetLayer(child.gameObject, layer);
}
}
///
/// Check to see if the provided values are close enough to be the same
///
public static bool CloseEnoughForMe(double value1, double value2, double acceptableDifference)
{
return Math.Abs(value1 - value2) <= acceptableDifference;
}
///
/// Check to see if the provided values are close enough to be the same
///
public static bool CloseEnoughForMe(float value1, float value2, float acceptableDifference)
{
return Mathf.Abs(value1 - value2) <= acceptableDifference;
}
private static System.Random m_Random = new System.Random();
///
/// Gets a new random actor id for netowork actors
///
/// Random integer between in Min and int max that is not in the current sessions of NetworkActors
public static int GetRandomActorID()
{
int id = m_Random.Next(int.MinValue, int.MaxValue);
while (Globals.Actors.ContainsKey(id))
{
id = m_Random.Next(int.MinValue, int.MaxValue);
}
return id;
}
static readonly string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
const long byteConversion = 1000;
///
/// Takes a count of bytes and turns it into a human readable version
///
/// Count of Bytes as a long
/// String statement of the bytes
public static string GetHumanReadableFileSize(long value)
{
if (value < 0) { return "-" + GetHumanReadableFileSize(-value); }
if (value == 0) { return "0.0 bytes"; }
int mag = (int)Math.Log(value, byteConversion);
double adjustedSize = (value / Math.Pow(1000, mag));
return string.Format("{0:n2} {1}", adjustedSize, SizeSuffixes[mag]);
}
///
/// Gets a value that indicates whether
/// is a valid path.
///
/// Returns true if is a
/// valid path; false otherwise. Also returns false if
/// the caller does not have the required permissions to access
/// .
///
///
///
public static bool IsValidPath(string path)
{
string result;
return TryGetFullPath(path, out result);
}
///
/// Returns the absolute path for the specified path string. A return
/// value indicates whether the conversion succeeded.
///
/// The file or directory for which to obtain absolute
/// path information.
///
/// When this method returns, contains the absolute
/// path representation of , if the conversion
/// succeeded, or if the conversion failed.
/// The conversion fails if is null or
/// , or is not of the correct format. This
/// parameter is passed uninitialized; any value originally supplied
/// in will be overwritten.
///
/// true if was converted
/// to an absolute path successfully; otherwise, false.
///
///
///
public static bool TryGetFullPath(string path, out string result)
{
result = String.Empty;
if (String.IsNullOrWhiteSpace(path)) { return false; }
bool status = false;
try
{
result = Path.GetFullPath(path);
status = true;
}
catch (ArgumentException) { }
catch (SecurityException) { }
catch (NotSupportedException) { }
catch (PathTooLongException) { }
return status;
}
}
}