public static void DumpSpriteCollectionMetadata(tk2dSpriteCollectionData sprites) { string path = "DUMPsprites/" + sprites.spriteCollectionName; string diskPath = Path.Combine(ResourcesDirectory, path.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".json"); if (File.Exists(diskPath)) { return; } Directory.GetParent(diskPath).Create(); JSONHelper.WriteJSON(AssetSpriteData.FromTK2D(sprites), diskPath); for (int i = 0; i < sprites.spriteDefinitions.Length; i++) { tk2dSpriteDefinition frame = sprites.spriteDefinitions[i]; Texture2D texOrig = (Texture2D)frame.material.mainTexture; if (!frame.Valid || (frame.materialInst != null && TextureMap.ContainsValue((Texture2D)frame.materialInst.mainTexture))) { continue; } string pathFull = path + "/" + frame.name; diskPath = Path.Combine(ResourcesDirectory, pathFull.Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".json"); if (!File.Exists(diskPath)) { Directory.GetParent(diskPath).Create(); JSONHelper.WriteJSON(AssetSpriteData.FromTK2D(sprites, frame, true), diskPath); } } }
public static void ToTK2D(List <AssetSpriteData> list, out string[] names, out Rect[] regions, out Vector2[] anchors, out AttachPoint[][] attachPoints) { names = new string[list.Count]; regions = new Rect[list.Count]; anchors = new Vector2[list.Count]; attachPoints = new AttachPoint[list.Count][]; for (int i = 0; i < list.Count; i++) { // TODO AssetSpriteData item = list[i]; names[i] = item.name; regions[i] = new Rect(item.x, item.y, item.width, item.height); anchors[i] = new Vector2(item.width / 2f, item.height / 2f); attachPoints[i] = item.attachPoints; } }
public static void HandleSprites(tk2dSpriteCollectionData sprites) { if (sprites == null) { return; } string path = "sprites/" + sprites.spriteCollectionName; Texture2D replacement; AssetMetadata metadata; Texture mainTexture = sprites.materials?.Length != 0 ? sprites.materials[0]?.mainTexture : null; string atlasName = mainTexture?.name; if (mainTexture != null && (atlasName == null || atlasName.Length == 0 || atlasName[0] != '~')) { if (TextureMap.TryGetValue(path, out replacement)) { } else if (TryGetMapped(path, out metadata)) { TextureMap[path] = replacement = Resources.Load <Texture2D>(path); } else { foreach (KeyValuePair <string, AssetMetadata> mapping in Map) { if (!mapping.Value.HasData) { continue; } string resourcePath = mapping.Key; if (!resourcePath.StartsWithInvariant("sprites/@")) { continue; } string spriteName = resourcePath.Substring(9); if (sprites.spriteCollectionName.Contains(spriteName)) { string copyPath = Path.Combine(ResourcesDirectory, ("DUMP" + path).Replace('/', Path.DirectorySeparatorChar).Replace('\\', Path.DirectorySeparatorChar) + ".png"); if (mapping.Value.Container == AssetMetadata.ContainerType.Filesystem && !File.Exists(copyPath)) { Directory.GetParent(copyPath).Create(); File.Copy(mapping.Value.File, copyPath); } TextureMap[path] = replacement = Resources.Load <Texture2D>(resourcePath); break; } } } if (replacement != null) { // Full atlas texture replacement. replacement.name = '~' + atlasName; for (int i = 0; i < sprites.materials.Length; i++) { if (sprites.materials[i]?.mainTexture == null) { continue; } sprites.materials[i].mainTexture = replacement; } } } if (DumpSprites) { Dump.DumpSpriteCollection(sprites); } if (DumpSpritesMetadata) { Dump.DumpSpriteCollectionMetadata(sprites); } List <tk2dSpriteDefinition> list = null; foreach (KeyValuePair <string, AssetMetadata> mapping in Map) { string assetPath = mapping.Key; if (assetPath.Length <= path.Length + 1) { continue; } if (!assetPath.StartsWithInvariant(path) || mapping.Value.AssetType != t_Texture2D) { continue; } string name = assetPath.Substring(path.Length + 1); tk2dSpriteDefinition frame = sprites.GetSpriteDefinition(name); if (frame != null && frame.materialInst != null) { Texture2D origTex = (Texture2D)frame.materialInst.mainTexture; if (Packer.IsPageTexture(origTex)) { continue; } } if (!TextureMap.TryGetValue(assetPath, out replacement)) { replacement = TextureMap[assetPath] = Resources.Load <Texture2D>(assetPath); } if (replacement == null) { continue; } if (frame == null && name[0] == '@') { name = name.Substring(1); for (int i = 0; i < sprites.spriteDefinitions.Length; i++) { tk2dSpriteDefinition frame_ = sprites.spriteDefinitions[i]; if (frame_.Valid && frame_.name.Contains(name)) { frame = frame_; name = frame_.name; break; } } if (frame == null) { continue; } } if (frame != null) { // Replace old sprite. frame.ReplaceTexture(replacement); } else { // Add new sprite. if (list == null) { list = new List <tk2dSpriteDefinition>(sprites.spriteDefinitions?.Length ?? 32); if (sprites.spriteDefinitions != null) { list.AddRange(sprites.spriteDefinitions); } } frame = new tk2dSpriteDefinition(); frame.name = name; frame.material = sprites.materials[0]; frame.ReplaceTexture(replacement); AssetSpriteData frameData = new AssetSpriteData(); AssetMetadata jsonMetadata = GetMapped(assetPath + ".json"); if (jsonMetadata != null) { frameData = JSONHelper.ReadJSON <AssetSpriteData>(jsonMetadata.Stream); } frame.normals = new Vector3[0]; frame.tangents = new Vector4[0]; frame.indices = new int[] { 0, 3, 1, 2, 3, 0 }; // TODO figure out this black magic const float pixelScale = 0.0625f; float w = replacement.width * pixelScale; float h = replacement.height * pixelScale; frame.position0 = new Vector3(0f, 0f, 0f); frame.position1 = new Vector3(w, 0f, 0f); frame.position2 = new Vector3(0f, h, 0f); frame.position3 = new Vector3(w, h, 0f); frame.boundsDataCenter = frame.untrimmedBoundsDataCenter = new Vector3(w / 2f, h / 2f, 0f); frame.boundsDataExtents = frame.untrimmedBoundsDataExtents = new Vector3(w, h, 0f); sprites.SetAttachPoints(list.Count, frameData.attachPoints); list.Add(frame); } } if (list != null) { sprites.spriteDefinitions = list.ToArray(); ReflectionHelper.SetValue(f_tk2dSpriteCollectionData_spriteNameLookupDict, sprites, null); } if (sprites.hasPlatformData) { sprites.inst.Handle(); } }
public static UnityEngine.Object Load(string path, Type type) { if (path == "PlayerCoopCultist" && Player.CoopReplacement != null) { path = Player.CoopReplacement; } else if (path.StartsWithInvariant("Player") && Player.PlayerReplacement != null) { path = Player.PlayerReplacement; } UnityEngine.Object customobj = null; for (int i = 0; i < _Protocols.Length; i++) { var protocol = _Protocols[i]; customobj = protocol.Get(path); if (customobj != null) { return(customobj); } } #if DEBUG if (DumpResources) { Dump.DumpResource(path); } #endif AssetMetadata metadata; bool isJson = false; bool isPatch = false; if (TryGetMapped(path, out metadata, true)) { } else if (TryGetMapped(path + ".json", out metadata)) { isJson = true; } else if (TryGetMapped(path + ".patch.json", out metadata)) { isPatch = true; isJson = true; } if (metadata != null) { if (isJson) { if (isPatch) { UnityEngine.Object obj = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix); using (JsonHelperReader json = JSONHelper.OpenReadJSON(metadata.Stream)) { json.Read(); // Go to start; return((UnityEngine.Object)json.FillObject(obj)); } } return((UnityEngine.Object)JSONHelper.ReadJSON(metadata.Stream)); } if (t_tk2dSpriteCollectionData == type) { AssetMetadata json = GetMapped(path + ".json"); if (metadata.AssetType == t_Texture2D && json != null) { // Atlas string[] names; Rect[] regions; Vector2[] anchors; AttachPoint[][] attachPoints; AssetSpriteData.ToTK2D(JSONHelper.ReadJSON <List <AssetSpriteData> >(json.Stream), out names, out regions, out anchors, out attachPoints); tk2dSpriteCollectionData sprites = tk2dSpriteCollectionData.CreateFromTexture( Resources.Load <Texture2D>(path), tk2dSpriteCollectionSize.Default(), names, regions, anchors ); for (int i = 0; i < attachPoints.Length; i++) { sprites.SetAttachPoints(i, attachPoints[i]); } return(sprites); } if (metadata.AssetType == t_AssetDirectory) { // Separate textures // TODO create collection from "children" assets tk2dSpriteCollectionData data = new GameObject(path.StartsWithInvariant("sprites/") ? path.Substring(8) : path).AddComponent <tk2dSpriteCollectionData>(); tk2dSpriteCollectionSize size = tk2dSpriteCollectionSize.Default(); data.spriteCollectionName = data.name; data.Transient = true; data.version = 3; data.invOrthoSize = 1f / size.OrthoSize; data.halfTargetHeight = size.TargetHeight * 0.5f; data.premultipliedAlpha = false; data.material = new Material(DefaultSpriteShader); data.materials = new Material[] { data.material }; data.buildKey = UnityEngine.Random.Range(0, int.MaxValue); data.Handle(); data.textures = new Texture2D[data.spriteDefinitions.Length]; for (int i = 0; i < data.spriteDefinitions.Length; i++) { data.textures[i] = data.spriteDefinitions[i].materialInst.mainTexture; } return(data); } } if (t_Texture.IsAssignableFrom(type) || type == t_Texture2D || (type == t_Object && metadata.AssetType == t_Texture2D)) { Texture2D tex = new Texture2D(2, 2); tex.name = path; tex.LoadImage(metadata.Data); tex.filterMode = FilterMode.Point; return(tex); } } UnityEngine.Object orig = Resources.Load(path + ETGModUnityEngineHooks.SkipSuffix, type); if (orig is GameObject) { Objects.HandleGameObject((GameObject)orig); } return(orig); }