Inheritance: DisposableResource
Exemple #1
0
		private void init(string filename, Stream stream, string contentDirectory, Dictionary<string,Type> materialTypes, List<MaterialFieldBinder> value1BinderTypes, List<MaterialFieldBinder> value2BinderTypes, List<MaterialFieldBinder> value3BinderTypes, List<MaterialFieldBinder> value4BinderTypes, List<MaterialFieldBinder> textureBinderTypes, Dictionary<string,string> fileExtOverrides, int classicInstanceCount, Loader.LoadedCallbackMethod loadedCallback)
		{
			try
			{
				var reader = new BinaryReader(stream);

				// meta data
				if (reader.ReadInt32() != Streams.MakeFourCC('R', 'M', 'F', 'T')) Debug.ThrowError("Error", "Not a ReignModel file: " + filename);
				float version = reader.ReadSingle();
				if (version != 1.0f) Debug.ThrowError("Error", "Unsuported model version: " + version.ToString());
				bool compressed = reader.ReadBoolean();

				// frames
				FrameStart = reader.ReadSingle();
				FrameEnd = reader.ReadSingle();
				FrameCount = FrameEnd - FrameStart;
				FPS = reader.ReadSingle();

				// materials
				int materialCount = reader.ReadInt32();
				Materials = new MaterialI[materialCount];
				Textures = new List<ITexture2D>();
				for (int i = 0; i != materialCount; ++i)
				{
					string name = reader.ReadString();

					// create material
					bool pass = false;
					foreach (var materialType in (Dictionary<string,Type>)materialTypes)
					{
						if (materialType.Key == name)
						{
							Materials[i] = (MaterialI)Activator.CreateInstance(materialType.Value);
							Materials[i].Name = name;
							pass = true;
							break;
						}
					}
					if (!pass) Debug.ThrowError("Model", "Failed to find a valid material type for: " + name);
					var material = Materials[i];

					// values1
					var values1 = new Dictionary<string,float>();
					int valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values1.Add(reader.ReadString(), reader.ReadSingle());
					bindTypes(material, values1, value1BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values2
					var values2 = new Dictionary<string,Vector2>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values2.Add(reader.ReadString(), reader.ReadVector2());
					bindTypes(material, values2, value2BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values3
					var values3 = new Dictionary<string,Vector3>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values3.Add(reader.ReadString(), reader.ReadVector3());
					bindTypes(material, values3, value3BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// values4
					var values4 = new Dictionary<string,Vector4>();
					valueCount = reader.ReadInt32();
					for (int i2 = 0; i2 != valueCount; ++i2) values4.Add(reader.ReadString(), reader.ReadVector4());
					bindTypes(material, values4, value4BinderTypes, contentDirectory, fileExtOverrides, handleFoundValueBinder);

					// textures
					var textures = new Dictionary<string,string>();
					int textureCount = reader.ReadInt32();
					for (int i2 = 0; i2 != textureCount; ++i2) textures.Add(reader.ReadString(), reader.ReadString());
					bindTypes(material, textures, textureBinderTypes, contentDirectory, fileExtOverrides, handleFoundTextureBinder);
				}

				// meshes
				int meshCount = reader.ReadInt32();
				Meshes = new Mesh[meshCount];
				for (int i = 0; i != meshCount; ++i)
				{
					Meshes[i] = new Mesh(reader, this, classicInstanceCount);
				}

				// actions
				int actionCount = reader.ReadInt32();
				Actions = new Action[actionCount];
				for (int i = 0; i != actionCount; ++i)
				{
					Actions[i] = new Action(reader);
				}

				// armatures
				int armatureCount = reader.ReadInt32();
				Armatures = new Armature[armatureCount];
				for (int i = 0; i != armatureCount; ++i)
				{
					Armatures[i] = new Armature(reader);
				}

				// objects
				int objectCount = reader.ReadInt32();
				Objects = new Object[objectCount];
				for (int i = 0; i != objectCount; ++i)
				{
					string type = reader.ReadString();
					if (type == "MESH") Objects[i] = new ObjectMesh(reader, this);
					else if (type == "ARMATURE") Objects[i] = new ObjectArmature(reader, this);
					else Debug.ThrowError("Mesh", "Unsuported Object type: " + type);
				}

				// link objects
				foreach (var o in Objects)
				{
					o.linkObjects(Objects);
				}
			}
			catch (Exception e)
			{
				FailedToLoad = true;
				Loader.AddLoadableException(e);
				Dispose();
				if (loadedCallback != null) loadedCallback(this, false);
				return;
			}

			if (Textures.Count == 0)
			{
				Loaded = true;
				if (loadedCallback != null) loadedCallback(this, true);
			}
			else
			{
				new LoadWaiter(Textures.ToArray(),
				delegate(object sender, bool succeeded)
				{
					if (succeeded)
					{
						Loaded = true;
						if (loadedCallback != null) loadedCallback(this, true);
					}
					else
					{
						FailedToLoad = true;
						Dispose();
						if (loadedCallback != null) loadedCallback(this, false);
					}
				});
			}
		}