private byte[] CreateTk2DEmulatorMonoBehaviour(long goPid, Tk2dInfo tk2dInfo) { byte[] data; using (MemoryStream ms = new MemoryStream()) using (AssetsFileWriter w = new AssetsFileWriter(ms)) { w.bigEndian = false; w.Write(0); w.Write(goPid); w.Write(1); w.Write(3); w.Write((long)11500000); w.WriteCountStringInt32(""); w.Align(); w.Write(tk2dInfo.positions.Length); for (int i = 0; i < tk2dInfo.positions.Length; i++) { Vector3 position = tk2dInfo.positions[i]; w.Write(position.x); w.Write(position.y); w.Write(position.z); } w.Align(); w.Write(tk2dInfo.uvs.Length); for (int i = 0; i < tk2dInfo.uvs.Length; i++) { Vector3 uv = tk2dInfo.uvs[i]; w.Write(uv.x); w.Write(uv.y); } w.Align(); w.Write(tk2dInfo.indices.Length); for (int i = 0; i < tk2dInfo.indices.Length; i++) { w.Write(tk2dInfo.indices[i]); } w.Align(); data = ms.ToArray(); } return(data); }
private void ReplaceReferencesRecurse(AssetsFileInstance inst, AssetTypeValueField field, AssetFileInfoEx inf) { foreach (AssetTypeValueField child in field.children) { //not a value (ie int) if (!child.templateField.hasValue) { //not null if (child == null) { return; } //not array of values either if (child.templateField.isArray && child.templateField.children[1].valueType != EnumValueTypes.ValueType_None) { break; } string typeName = child.templateField.type; //is a pptr if (typeName.StartsWith("PPtr<") && typeName.EndsWith(">") && child.childrenCount == 2) { int fileId = child.Get("m_FileID").GetValue().AsInt(); long pathId = child.Get("m_PathID").GetValue().AsInt64(); //not a null pptr if (pathId == 0) { continue; } AssetID aid = ConvertToAssetID(inst, fileId, pathId); //not already visited if (references.ContainsKey(aid)) { AssetID id = references[aid]; //normally, I would've just checked if the path names //matched up, but this is faster than looking up names //I check type of this asset and compare with the name //of the assetid to see if it should be itself or if //it should be the dependency file bool isSelfAsset = IsAsset(inf); bool isDepAsset = id.fileName == ASSET_LEVEL_NAME; int newFileId = isDepAsset ^ isSelfAsset ? 1 : 0; child.Get("m_FileID").GetValue().Set(newFileId); child.Get("m_PathID").GetValue().Set(id.pathId); } else { /////////////// todo move to another method AssetsFileInstance depInst = ConvertToInstance(inst, fileId); AssetFileInfoEx depInf = depInst.table.GetAssetInfo(pathId); if (depInf.curFileType == 0x72) { ushort scriptIndex = depInst.file.typeTree.unity5Types[depInf.curFileTypeOrIndex].scriptIndex; if (tk2dSpriteScriptIndex == 0xffff) { AssetTypeValueField monoBase = am.GetATI(depInst.file, depInf).GetBaseField(); AssetExternal scriptBaseExt = am.GetExtAsset(depInst, monoBase.Get("m_Script")); if (scriptBaseExt.instance != null) { AssetTypeValueField scriptBase = scriptBaseExt.instance.GetBaseField(); string scriptName = scriptBase.Get("m_ClassName").GetValue().AsString(); if (scriptName == "tk2dSprite") { tk2dSpriteScriptIndex = scriptIndex; } } } if (tk2dSpriteScriptIndex == depInst.file.typeTree.unity5Types[depInf.curFileTypeOrIndex].scriptIndex) { string managedPath = Path.Combine(Path.GetDirectoryName(depInst.path), "Managed"); AssetTypeValueField spriteBase = am.GetMonoBaseFieldCached(depInst, depInf, managedPath); int spriteId = spriteBase.Get("_spriteId").GetValue().AsInt(); AssetExternal colBaseExt = am.GetExtAsset(depInst, spriteBase.Get("collection")); AssetsFileInstance colInst = colBaseExt.file; AssetTypeValueField colBase = am.GetMonoBaseFieldCached(colInst, colBaseExt.info, managedPath); AssetTypeValueField spriteDefinitions = colBase.Get("spriteDefinitions")[spriteId]; AssetTypeValueField positionsField = spriteDefinitions.Get("positions"); AssetTypeValueField uvsField = spriteDefinitions.Get("uvs"); AssetTypeValueField indicesField = spriteDefinitions.Get("indices"); Vector3[] positions = new Vector3[positionsField.GetChildrenCount()]; Vector2[] uvs = new Vector2[uvsField.GetChildrenCount()]; int[] indices = new int[indicesField.GetChildrenCount()]; for (int i = 0; i < positions.Length; i++) { AssetTypeValueField positionField = positionsField[i]; positions[i] = new Vector3() { x = positionField.Get("x").GetValue().AsFloat(), y = positionField.Get("y").GetValue().AsFloat(), z = positionField.Get("z").GetValue().AsFloat() }; } for (int i = 0; i < uvs.Length; i++) { AssetTypeValueField uvField = uvsField[i]; uvs[i] = new Vector2() { x = uvField.Get("x").GetValue().AsFloat(), y = uvField.Get("y").GetValue().AsFloat() }; } for (int i = 0; i < indices.Length; i++) { AssetTypeValueField indexField = indicesField[i]; indices[i] = indexField.GetValue().AsInt(); } AssetID thisAid = ConvertToAssetID(inst, 0, inf.index); tk2dFromGoLookup[thisAid] = new Tk2dInfo(positions, uvs, indices); } } /////////////// child.Get("m_FileID").GetValue().Set(0); child.Get("m_PathID").GetValue().Set(0); } } ReplaceReferencesRecurse(inst, child, inf); } } }