private void AllocateCustomData(int customDataCount) { if (CustomData.Count() == 0) { CustomData = new CustomDataInformation[customDataCount]; for (int j = 0; j < customDataCount; j++) { CustomData[j] = new CustomDataInformation(); } } }
public bool Load(string path) { if (string.IsNullOrEmpty(path)) { return(false); } System.IO.FileStream fs = null; if (!System.IO.File.Exists(path)) { return(false); } try { fs = System.IO.File.Open(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); } catch (System.IO.FileNotFoundException e) { return(false); } var br = new System.IO.BinaryReader(fs); var buf = new byte[1024]; if (br.Read(buf, 0, 16) != 16) { fs.Dispose(); br.Close(); return(false); } if (buf[0] != 'E' || buf[1] != 'F' || buf[2] != 'K' || buf[3] != 'M') { return(false); } int version = BitConverter.ToInt32(buf, 4); GUID = BitConverter.ToUInt64(buf, 8); while (true) { if (br.Read(buf, 0, 8) != 8) { fs.Dispose(); br.Close(); break; } if (buf[0] == 'D' && buf[1] == 'E' && buf[2] == 'S' && buf[3] == 'C') { var temp = new byte[BitConverter.ToInt32(buf, 4)]; if (br.Read(temp, 0, temp.Length) != temp.Length) { return(false); } var reader = new BinaryReader(temp); int count = 0; reader.Get(ref count); for (int i = 0; i < count; i++) { int lang = 0; string name = null; string desc = null; reader.Get(ref lang); reader.Get(ref name, Encoding.UTF8); reader.Get(ref desc, Encoding.UTF8); Names.Add((Language)lang, name); Descriptions.Add((Language)lang, desc); } } if (buf[0] == 'P' && buf[1] == 'R' && buf[2] == 'M' && buf[3] == '_') { var temp = new byte[BitConverter.ToInt32(buf, 4)]; if (br.Read(temp, 0, temp.Length) != temp.Length) { return(false); } var reader = new BinaryReader(temp); int shadingModel = 0; reader.Get(ref shadingModel); bool hasNormal = false; reader.Get(ref hasNormal); bool hasReflection = false; reader.Get(ref hasReflection); int customData1Count = 0; reader.Get(ref customData1Count); int customData2Count = 0; reader.Get(ref customData2Count); int textureCount = 0; reader.Get(ref textureCount); List <TextureInformation> textures = new List <TextureInformation>(); for (int i = 0; i < textureCount; i++) { TextureInformation info = new TextureInformation(); reader.Get(ref info.Name, Encoding.UTF8); // name is for human, uniformName is a variable name after 3 if (version >= 3) { reader.Get(ref info.UniformName, Encoding.UTF8); } else { info.UniformName = info.Name; } reader.Get(ref info.DefaultPath, Encoding.UTF8); reader.Get(ref info.Index); reader.Get(ref info.Priority); reader.Get(ref info.IsParam); int textureType = 0; reader.Get(ref textureType); info.Type = (TextureType)textureType; int sampler = 0; reader.Get(ref sampler); // convert a path into absolute if (string.IsNullOrEmpty(info.DefaultPath)) { info.DefaultPath = string.Empty; } else { info.DefaultPath = Utils.Misc.GetAbsolutePath(path, info.DefaultPath); } textures.Add(info); } Textures = textures.ToArray(); int uniformCount = 0; reader.Get(ref uniformCount); List <UniformInformation> uniforms = new List <UniformInformation>(); for (int i = 0; i < uniformCount; i++) { UniformInformation info = new UniformInformation(); reader.Get(ref info.Name, Encoding.UTF8); // name is for human, uniformName is a variable name after 3 if (version >= 3) { reader.Get(ref info.UniformName, Encoding.UTF8); } else { info.UniformName = info.Name; } reader.Get(ref info.Offset); reader.Get(ref info.Priority); reader.Get(ref info.Type); reader.Get(ref info.DefaultValues[0]); reader.Get(ref info.DefaultValues[1]); reader.Get(ref info.DefaultValues[2]); reader.Get(ref info.DefaultValues[3]); uniforms.Add(info); } Uniforms = uniforms.ToArray(); } if (buf[0] == 'P' && buf[1] == 'R' && buf[2] == 'M' && buf[3] == '2') { var temp = new byte[BitConverter.ToInt32(buf, 4)]; if (br.Read(temp, 0, temp.Length) != temp.Length) { return(false); } var reader = new BinaryReader(temp); if (version >= 2) { int customDataCount = 0; reader.Get(ref customDataCount); CustomData = new CustomDataInformation[customDataCount]; for (int j = 0; j < customDataCount; j++) { CustomData[j] = new CustomDataInformation(); int count = 0; reader.Get(ref count); for (int i = 0; i < count; i++) { int lang = 0; string name = null; string desc = null; reader.Get(ref lang); reader.Get(ref name, Encoding.UTF8); reader.Get(ref desc, Encoding.UTF8); CustomData[j].Summaries.Add((Language)lang, name); CustomData[j].Descriptions.Add((Language)lang, desc); } } } int textureCount = 0; reader.Get(ref textureCount); for (int j = 0; j < textureCount; j++) { int count = 0; reader.Get(ref count); for (int i = 0; i < count; i++) { int lang = 0; string name = null; string desc = null; reader.Get(ref lang); reader.Get(ref name, Encoding.UTF8); reader.Get(ref desc, Encoding.UTF8); Textures[j].Summaries.Add((Language)lang, name); Textures[j].Descriptions.Add((Language)lang, desc); } } int uniformCount = 0; reader.Get(ref uniformCount); for (int j = 0; j < uniformCount; j++) { int count = 0; reader.Get(ref count); for (int i = 0; i < count; i++) { int lang = 0; string name = null; string desc = null; reader.Get(ref lang); reader.Get(ref name, Encoding.UTF8); reader.Get(ref desc, Encoding.UTF8); Uniforms[j].Summaries.Add((Language)lang, name); Uniforms[j].Descriptions.Add((Language)lang, desc); } } } } return(true); }