/// <summary> Sets the model associated with this entity. </summary> /// <param name="model"> Can be either 'name' or 'name'|'scale'. </param> public void SetModel(string model) { ModelScale = new Vector3(1.0f); int sep = model.IndexOf('|'); string scale = sep == -1 ? null : model.Substring(sep + 1); ModelName = sep == -1 ? model : model.Substring(0, sep); if (Utils.CaselessEquals(model, "giant")) { ModelName = "humanoid"; ModelScale *= 2; } ModelBlock = Block.Air; if (BlockID.TryParse(ModelName, out ModelBlock)) { ModelName = "block"; } Model = game.ModelCache.Get(ModelName); ParseScale(scale); MobTextureId = -1; Model.RecalcProperties(this); UpdateModelBounds(); }
bool ParseBlock(string[] args) { if (args.Length == 1) { return(true); } if (Utils.CaselessEquals(args[1], "yes")) { persist = true; return(true); } int temp = -1; BlockID block = 0; if ((temp = BlockInfo.FindID(args[1])) != -1) { block = (BlockID)temp; } else if (!BlockID.TryParse(args[1], out block)) { game.Chat.Add("&eCuboid: &c\"" + args[1] + "\" is not a valid block name or id."); return(false); } if (block >= Block.CpeCount && BlockInfo.Name[block] == "Invalid") { game.Chat.Add("&eCuboid: &cThere is no block with id \"" + args[1] + "\"."); return(false); } this.block = block; return(true); }
public static AABB CalcAABB(Entity entity) { string model = GetRawModel(entity.Model); AABB bb; BlockID raw; if (BlockID.TryParse(model, out raw) && raw <= Block.MaxRaw) { BlockID block = Block.FromRaw(raw); bb = Block.BlockAABB(block, entity.Level); bb = bb.Offset(-16, 0, -16); // centre around [-16, 16] instead of [0, 32] } else { bb = AABB.Make(new Vec3S32(0, 0, 0), Get(model).BaseSize); } bb = bb.Expand(-1); // adjust the model AABB inwards slightly Vec3F32 scale = CalcScale(entity); // always limit max scale for collisions performance float max = DefaultMaxScale(model); scale.X = Math.Min(scale.X, max); scale.Y = Math.Min(scale.Y, max); scale.Z = Math.Min(scale.Z, max); bb.Min.X = (int)(bb.Min.X * scale.X); bb.Max.X = (int)(bb.Max.X * scale.X); bb.Min.Y = (int)(bb.Min.Y * scale.Y); bb.Max.Y = (int)(bb.Max.Y * scale.Y); bb.Min.Z = (int)(bb.Min.Z * scale.Z); bb.Max.Z = (int)(bb.Max.Z * scale.Z); return(bb); }
public static BlockID Parse(Player p, string input) { BlockDefinition[] defs = p.IsSuper ? BlockDefinition.GlobalDefs : p.level.CustomBlockDefs; BlockID block; // raw ID is treated specially, before names if (BlockID.TryParse(input, out block)) { if (block < Block.CpeCount || (block <= Block.MaxRaw && defs[FromRaw(block)] != null)) { return(FromRaw(block)); } } block = GetBlockByName(input, defs); if (block != Block.Invalid) { return(block); } byte coreID; bool success = Aliases.TryGetValue(input.ToLower(), out coreID); return(success ? coreID : Invalid); }
/// <summary> Gives distance (in half-pixel world units) from feet to camera height </summary> public static int CalcEyeHeight(Entity entity) { Vec3F32 scale = CalcScale(entity); string model = GetRawModel(entity.Model); BlockID raw; if (BlockID.TryParse(model, out raw) && raw <= Block.MaxRaw) { return(16); //lazily return middle of full block if it thinks it's a block ID. } float eyeHeight = Get(model).EyeHeight; eyeHeight *= scale.Y; eyeHeight *= 2f; //multiply by two because world positions are measured in half-pixels return((int)eyeHeight); }
static void SendModel(Player pl, byte id, string model) { BlockID raw; if (BlockID.TryParse(model, out raw) && raw > pl.MaxRawBlock) { BlockID block = Block.FromRaw(raw); if (block >= Block.ExtendedCount) { model = "humanoid"; // invalid block ids } else { model = pl.ConvertBlock(block).ToString(); } } pl.Send(Packet.ChangeModel(id, model, pl.hasCP437)); }
public override void SendChangeModel(byte id, string model) { BlockID raw; if (BlockID.TryParse(model, out raw) && raw > MaxRawBlock) { BlockID block = Block.FromRaw(raw); if (block >= Block.SUPPORTED_COUNT) { model = "humanoid"; // invalid block ids } else { model = ConvertBlock(block).ToString(); } } Send(Packet.ChangeModel(id, model, player.hasCP437)); }
public static AABB ModelAABB(Entity entity, Level lvl) { string model = entity.Model; float scale = GetScaleFrom(ref model); AABB bb; BlockID raw; if (BlockID.TryParse(model, out raw) && raw <= Block.MaxRaw) { BlockID block = Block.FromRaw(raw); bb = Block.BlockAABB(block, lvl); bb = bb.Offset(-16, 0, -16); // centre around [-16, 16] instead of [0, 32] } else { bb = AABB.Make(new Vec3S32(0, 0, 0), BaseSize(model)); } bb = bb.Expand(-1); // adjust the model AABB inwards slightly float max = model.CaselessEq("chibi") ? 3 : 2; float scaleX = scale, scaleY = scale, scaleZ = scale; if (entity.ScaleX != 0) { scaleX = Math.Min(entity.ScaleX * scale, max); } if (entity.ScaleY != 0) { scaleY = Math.Min(entity.ScaleY * scale, max); } if (entity.ScaleZ != 0) { scaleZ = Math.Min(entity.ScaleZ * scale, max); } bb.Min.X = (int)(bb.Min.X * scaleX); bb.Max.X = (int)(bb.Max.X * scaleX); bb.Min.Y = (int)(bb.Min.Y * scaleY); bb.Max.Y = (int)(bb.Max.Y * scaleY); bb.Min.Z = (int)(bb.Min.Z * scaleZ); bb.Max.Z = (int)(bb.Max.Z * scaleZ); return(bb); }
static void ProcessLines(StreamReader r) { string[] args = new string[4]; string line; while ((line = r.ReadLine()) != null) { if (line.IsCommentLine()) { continue; } // Format - ID : Lowest : Disallow : Allow line.Replace(" ", "").FixedSplit(args, ':'); BlockID block; if (!BlockID.TryParse(args[0], out block)) { // Old format - Name : Lowest : Disallow : Allow block = Block.Parse(Player.Console, args[0]); } if (block == Block.Invalid) { continue; } try { LevelPermission min; List <LevelPermission> allowed, disallowed; Deserialise(args, 1, out min, out allowed, out disallowed); Set(block, min, allowed, disallowed); } catch { Logger.Log(LogType.Warning, "Hit an error on the block " + line); continue; } } }
public override void Execute(string[] args) { if (args.Length != 2 && args.Length != 3) { game.Chat.Add("Unspecified error"); } else { BlockID id = 5; EntityItem Item = new EntityItem(game); if (!BlockID.TryParse(args[1], out id)) { game.Chat.Add("Unspecified error"); return; } Item.ItemId = id; float x = game.LocalPlayer.Position.X; float z = game.LocalPlayer.Position.Z; Vector3 pos = Respawn.FindSpawnPosition(game, x, z, Item.Size); Item.SetLocation(LocationUpdate.MakePos(pos, false), false); game.Entities.List[200] = Item; } }
static void LoadCore(string path, BlockProps[] list, byte scope, bool mapOld) { string[] lines = File.ReadAllLines(path); for (int i = 0; i < lines.Length; i++) { string line = lines[i].Trim(); if (line.Length == 0 || line[0] == '#') { continue; } string[] parts = line.Split(':'); if (parts.Length < 10) { Logger.Log(LogType.Warning, "Invalid line \"{0}\" in {1}", line, path); continue; } BlockID b; if (!BlockID.TryParse(parts[0], out b)) { Logger.Log(LogType.Warning, "Invalid line \"{0}\" in {1}", line, path); continue; } if (mapOld) { b = Block.MapOldRaw(b); } if (b >= list.Length) { Logger.Log(LogType.Warning, "Invalid block ID: " + b); continue; } bool.TryParse(parts[1], out list[b].IsRails); bool.TryParse(parts[2], out list[b].IsTDoor); bool.TryParse(parts[3], out list[b].IsDoor); bool.TryParse(parts[4], out list[b].IsMessageBlock); bool.TryParse(parts[5], out list[b].IsPortal); bool.TryParse(parts[6], out list[b].WaterKills); bool.TryParse(parts[7], out list[b].LavaKills); bool.TryParse(parts[8], out list[b].KillerBlock); list[b].ChangedScope = scope; list[b].DeathMessage = parts[9].Replace("\\;", ":"); if (list[b].DeathMessage.Length == 0) { list[b].DeathMessage = null; } if (parts.Length > 10) { byte ai; byte.TryParse(parts[10], out ai); list[b].AnimalAI = (AnimalAI)ai; } if (parts.Length > 11) { BlockID.TryParse(parts[11], out list[b].StackBlock); list[b].StackBlock = Block.MapOldRaw(list[b].StackBlock); } if (parts.Length > 12) { bool.TryParse(parts[12], out list[b].OPBlock); } if (parts.Length > 13) { BlockID.TryParse(parts[13], out list[b].oDoorBlock); } if (parts.Length > 14) { bool.TryParse(parts[14], out list[b].Drownable); } if (parts.Length > 15) { BlockID.TryParse(parts[15], out list[b].GrassBlock); } if (parts.Length > 16) { BlockID.TryParse(parts[16], out list[b].DirtBlock); } } }
static bool Parse <TEnum> (Type enumType, string value, bool ignoreCase, out TEnum result) { result = default(TEnum); MonoEnumInfo info; MonoEnumInfo.GetInfo(enumType, out info); // is 'value' a named constant? int loc = FindName(info.name_hash, info.names, value, ignoreCase); if (loc >= 0) { result = (TEnum)info.values.GetValue(loc); return(true); } TypeCode typeCode = ((Enum)info.values.GetValue(0)).GetTypeCode(); // is 'value' a list of named constants? if (value.IndexOf(',') != -1) { string [] names = value.Split(split_char); ulong retVal = 0; for (int i = 0; i < names.Length; ++i) { loc = FindName(info.name_hash, info.names, names [i].Trim(), ignoreCase); if (loc < 0) { return(false); } retVal |= GetValue(info.values.GetValue(loc), typeCode); } result = (TEnum)ToObject(enumType, retVal); return(true); } // is 'value' a number? switch (typeCode) { case TypeCode.SByte: sbyte sb; if (!SByte.TryParse(value, out sb)) { return(false); } result = (TEnum)ToObject(enumType, sb); break; case TypeCode.Byte: byte b; if (!Byte.TryParse(value, out b)) { return(false); } result = (TEnum)ToObject(enumType, b); break; case TypeCode.Int16: short i16; if (!Int16.TryParse(value, out i16)) { return(false); } result = (TEnum)ToObject(enumType, i16); break; case TypeCode.UInt16: ushort u16; if (!UInt16.TryParse(value, out u16)) { return(false); } result = (TEnum)ToObject(enumType, u16); break; case TypeCode.Int32: int i32; if (!Int32.TryParse(value, out i32)) { return(false); } result = (TEnum)ToObject(enumType, i32); break; case TypeCode.UInt32: uint u32; if (!UInt32.TryParse(value, out u32)) { return(false); } result = (TEnum)ToObject(enumType, u32); break; case TypeCode.Int64: long i64; if (!Int64.TryParse(value, out i64)) { return(false); } result = (TEnum)ToObject(enumType, i64); break; case TypeCode.UInt64: ulong u64; if (!UInt64.TryParse(value, out u64)) { return(false); } result = (TEnum)ToObject(enumType, u64); break; default: break; } return(true); }
public static bool TryParseEnum <TEnum>(this string value, out TEnum result) where TEnum : struct { if (string.IsNullOrEmpty(value)) { result = default(TEnum); return(false); } var enumType = typeof(TEnum); if (!enumType.IsEnum) { throw new ArgumentException(string.Format("Type '{0}' is not an enum", enumType.FullName)); } result = default(TEnum); // Try to parse the value directly if (Enum.IsDefined(enumType, value)) { result = (TEnum)Enum.Parse(enumType, value); return(true); } // Get some info on enum var enumValues = Enum.GetValues(enumType); if (enumValues.Length == 0) { return(false); } // probably can't happen as you cant define empty enum? var enumTypeCode = Type.GetTypeCode(enumValues.GetValue(0).GetType()); // Try to parse it as a flag if (value.IndexOf(',') != -1) { if (!Attribute.IsDefined(enumType, typeof(FlagsAttribute))) { return(false); } // value has flags but enum is not flags // todo: cache this for efficiency var enumInfo = new Dictionary <string, object>(); var enumNames = Enum.GetNames(enumType); for (var i = 0; i < enumNames.Length; i++) { enumInfo.Add(enumNames[i], enumValues.GetValue(i)); } ulong retVal = 0; foreach (var name in value.Split(FlagDelimiter)) { var trimmedName = name.Trim(); if (!enumInfo.ContainsKey(trimmedName)) { return(false); } // Enum has no such flag var enumValueObject = enumInfo[trimmedName]; ulong enumValueLong; switch (enumTypeCode) { case TypeCode.Byte: enumValueLong = (byte)enumValueObject; break; case TypeCode.SByte: enumValueLong = (byte)((sbyte)enumValueObject); break; case TypeCode.Int16: enumValueLong = (ushort)((short)enumValueObject); break; case TypeCode.Int32: enumValueLong = (uint)((int)enumValueObject); break; case TypeCode.Int64: enumValueLong = (ulong)((long)enumValueObject); break; case TypeCode.UInt16: enumValueLong = (ushort)enumValueObject; break; case TypeCode.UInt32: enumValueLong = (uint)enumValueObject; break; case TypeCode.UInt64: enumValueLong = (ulong)enumValueObject; break; default: return(false); // should never happen } retVal |= enumValueLong; } result = (TEnum)Enum.ToObject(enumType, retVal); return(true); } // the value may be a number, so parse it directly switch (enumTypeCode) { case TypeCode.SByte: sbyte sb; if (!SByte.TryParse(value, out sb)) { return(false); } result = (TEnum)Enum.ToObject(enumType, sb); break; case TypeCode.Byte: byte b; if (!Byte.TryParse(value, out b)) { return(false); } result = (TEnum)Enum.ToObject(enumType, b); break; case TypeCode.Int16: short i16; if (!Int16.TryParse(value, out i16)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i16); break; case TypeCode.UInt16: ushort u16; if (!UInt16.TryParse(value, out u16)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u16); break; case TypeCode.Int32: int i32; if (!Int32.TryParse(value, out i32)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i32); break; case TypeCode.UInt32: uint u32; if (!UInt32.TryParse(value, out u32)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u32); break; case TypeCode.Int64: long i64; if (!Int64.TryParse(value, out i64)) { return(false); } result = (TEnum)Enum.ToObject(enumType, i64); break; case TypeCode.UInt64: ulong u64; if (!UInt64.TryParse(value, out u64)) { return(false); } result = (TEnum)Enum.ToObject(enumType, u64); break; default: return(false); // should never happen } return(true); }
private void UpdateMaxValue(string MaxValue) { String datatypeName = autosarApp.GetDataTypeName(simpleDataType.BaseDataTypeGUID); if (datatypeName.Equals("uint8")) { byte value; if (byte.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = byte.MaxValue.ToString(); } } else if (datatypeName.Equals("uint16")) { UInt16 value; if (UInt16.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = UInt16.MaxValue.ToString(); } } else if (datatypeName.Equals("uint32")) { UInt32 value; if (UInt32.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = UInt32.MaxValue.ToString(); } } else if (datatypeName.Equals("int8")) { sbyte value; if (sbyte.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = sbyte.MaxValue.ToString(); } } else if (datatypeName.Equals("int16")) { Int16 value; if (Int16.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = Int16.MaxValue.ToString(); } } else if (datatypeName.Equals("int32")) { Int32 value; if (Int32.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = Int32.MaxValue.ToString(); } } else if (datatypeName.Equals("float32")) { float value; MaxValue = MaxValue.Replace(".", ","); if (float.TryParse(MaxValue, out value)){ this.simpleDataType.MaxValue = value.ToString(); } else { this.simpleDataType.MaxValue = float.MaxValue.ToString(); } } }
public static object Parse(Type enumType, string value, bool ignoreCase) { if (enumType == null) { throw new ArgumentNullException("enumType"); } if (value == null) { throw new ArgumentNullException("value"); } if (!enumType.IsEnum) { throw new ArgumentException("enumType is not an Enum type.", "enumType"); } value = value.Trim(); if (value.Length == 0) { throw new ArgumentException("An empty string is not considered a valid value."); } MonoEnumInfo info; MonoEnumInfo.GetInfo(enumType, out info); // is 'value' a named constant? int loc = FindName(info.name_hash, info.names, value, ignoreCase); if (loc >= 0) { return(info.values.GetValue(loc)); } TypeCode typeCode = ((Enum)info.values.GetValue(0)).GetTypeCode(); // is 'value' a list of named constants? if (value.IndexOf(',') != -1) { string [] names = value.Split(split_char); ulong retVal = 0; for (int i = 0; i < names.Length; ++i) { loc = FindName(info.name_hash, info.names, names [i].Trim(), ignoreCase); if (loc < 0) { throw new ArgumentException("The requested value was not found."); } retVal |= GetValue(info.values.GetValue(loc), typeCode); } return(ToObject(enumType, retVal)); } // is 'value' a number? #if !NET_2_0 try { return(ToObject(enumType, Convert.ChangeType(value, typeCode))); } catch (FormatException) { throw new ArgumentException(String.Format("The requested value '{0}' was not found.", value)); } #else switch (typeCode) { case TypeCode.SByte: sbyte sb; if (SByte.TryParse(value, out sb)) { return(ToObject(enumType, sb)); } break; case TypeCode.Byte: byte b; if (Byte.TryParse(value, out b)) { return(ToObject(enumType, b)); } break; case TypeCode.Int16: short i16; if (Int16.TryParse(value, out i16)) { return(ToObject(enumType, i16)); } break; case TypeCode.UInt16: ushort u16; if (UInt16.TryParse(value, out u16)) { return(ToObject(enumType, u16)); } break; case TypeCode.Int32: int i32; if (Int32.TryParse(value, out i32)) { return(ToObject(enumType, i32)); } break; case TypeCode.UInt32: uint u32; if (UInt32.TryParse(value, out u32)) { return(ToObject(enumType, u32)); } break; case TypeCode.Int64: long i64; if (Int64.TryParse(value, out i64)) { return(ToObject(enumType, i64)); } break; case TypeCode.UInt64: ulong u64; if (UInt64.TryParse(value, out u64)) { return(ToObject(enumType, u64)); } break; default: break; } throw new ArgumentException(String.Format("The requested value '{0}' was not found.", value)); #endif }
/// <summary> /// String to UInt16(字符串 转换成 无符号、数值、整数) /// </summary> /// <remarks> /// 2014-06-23 16:31 Created By iceStone /// </remarks> /// <param name="s">String</param> /// <param name="def">Default</param> /// <returns>Byte</returns> public static ushort ToUInt16(this string s, ushort def = default(ushort)) { ushort result; return(UInt16.TryParse(s, out result) ? result : def); }