public void TestLoadBeatmap() { using (Apk apk = new Apk(baseAPKPath)) { byte[] data = apk.ReadEntireEntry(Apk.MainAssetsFile); SerializedAssets assets = SerializedAssets.FromBytes(data); SerializedAssets.AssetObject obj = assets.objects[62]; MonoBehaviorAssetData monob = (MonoBehaviorAssetData)obj.data; BeatmapDataBehaviorData beatmap = (BeatmapDataBehaviorData)monob.data; using (Stream fileStream = new FileStream(repoPath("testoutput/beatmap_deflated.bin"), FileMode.Create)) { using (MemoryStream memoryStream = new MemoryStream(beatmap.projectedData)) { using (DeflateStream ds = new DeflateStream(memoryStream, CompressionMode.Decompress)) { ds.CopyTo(fileStream); } } } BeatmapSaveData saveData = BeatmapSaveData.DeserializeFromBinary(beatmap.projectedData); Assert.NotEmpty(saveData._notes); byte[] outData = saveData.SerializeToBinary(false); File.WriteAllBytes(repoPath("testoutput/beatmap_roundtrip.bin"), outData); BeatmapSaveData saveData2 = BeatmapSaveData.DeserializeFromBinary(outData, false); Assert.NotEmpty(saveData._notes); byte[] outData2 = saveData.SerializeToBinary(false); File.WriteAllBytes(repoPath("testoutput/beatmap_roundtrip2.bin"), outData); } }
private SerializedAssets TestRoundTrips(byte[] data, string name) { // File.WriteAllBytes($"../../../../testoutput/{name}.before.asset", data); SerializedAssets assets = SerializedAssets.FromBytes(data); Assert.NotEmpty(assets.types); Assert.NotEmpty(assets.objects); byte[] outData = assets.ToBytes(); // File.WriteAllBytes($"../../../../testoutput/{name}.after.asset", outData); Assert.True(System.Linq.Enumerable.SequenceEqual(data, outData)); return(assets); }
public void TestTextReplaceRoundTrip() { using (Apk apk = new Apk(baseAPKPath)) { byte[] data = apk.ReadEntireEntry(apk.TextFile()); SerializedAssets textAssets = SerializedAssets.FromBytes(data, apk.version); var aotext = textAssets.GetAssetAt(1); TextAssetData ta = aotext.data as TextAssetData; string oldScript = ta.script; var segments = ta.ReadLocaleText(); ta.WriteLocaleText(segments); Assert.Equal(oldScript, ta.script); } }
static void UpdateColors(Apk apk, CustomColors colors, InvocationResult res) { SerializedAssets colorAssets = SerializedAssets.FromBytes( apk.ReadEntireEntry(apk.ColorsFile()), apk.version); // There should only be one color manager var colorManager = colorAssets.FindScript <ColorManager>(cm => true); colorManager.UpdateColor(colorAssets, colors.colorA, ColorManager.ColorSide.A); colorManager.UpdateColor(colorAssets, colors.colorB, ColorManager.ColorSide.B); apk.ReplaceAssetsFile(apk.ColorsFile(), colorAssets.ToBytes()); res.newColors = new CustomColors() { colorA = colorManager.colorA.FollowToScript <SimpleColor>(colorAssets), colorB = colorManager.colorB.FollowToScript <SimpleColor>(colorAssets), }; }
static InvocationResult RunInvocation(Invocation inv) { InvocationResult res = new InvocationResult(); try { using (Apk apk = new Apk(inv.apkPath)) { if (inv.patchSignatureCheck) { apk.PatchSignatureCheck(); res.didSignatureCheckPatch = true; } SerializedAssets mainAssets = SerializedAssets.FromBytes( apk.ReadEntireEntry(apk.MainAssetsFile()), apk.version ); SyncLevels(apk, mainAssets, inv, res); apk.ReplaceAssetsFile(apk.MainAssetsFile(), mainAssets.ToBytes()); if (inv.colors != null) { UpdateColors(apk, inv.colors, res); } if (inv.replaceText != null) { UpdateText(apk, inv.replaceText, res); } apk.Save(); } if (inv.sign) { Signer.Sign(inv.apkPath); res.didSign = true; } } catch (Exception e) { res.error = e.ToString(); } return(res); }
static InvocationResult RunInvocation(Invocation inv) { InvocationResult res = new InvocationResult(); try { using (Apk apk = new Apk(inv.apkPath)) { if (inv.patchSignatureCheck) { apk.PatchSignatureCheck(); res.didSignatureCheckPatch = true; } byte[] data = apk.ReadEntireEntry(apk.MainAssetsFile()); SerializedAssets assets = SerializedAssets.FromBytes(data, apk.version); HashSet <string> existingLevels = assets.ExistingLevelIDs(); if (inv.ensureInstalled.Count > 0) { Program.EnsureInstalled(apk, assets, existingLevels, res, inv.ensureInstalled); byte[] outData = assets.ToBytes(); apk.ReplaceAssetsFile(apk.MainAssetsFile(), outData); } res.presentLevels = existingLevels.ToList(); apk.Save(); } if (inv.sign) { Signer.Sign(inv.apkPath); res.didSign = true; } } catch (Exception e) { res.error = e.ToString(); } return(res); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("arguments: pathToAPKFileToModify levelFolders..."); return; } string apkPath = args[0]; using (Apk apk = new Apk(apkPath)) { apk.PatchSignatureCheck(); byte[] data = apk.ReadEntireEntry(Apk.MainAssetsFile); SerializedAssets assets = SerializedAssets.FromBytes(data); HashSet <string> existingLevels = assets.ExistingLevelIDs(); LevelCollectionBehaviorData extrasCollection = assets.FindExtrasLevelCollection(); for (int i = 1; i < args.Length; i++) { Utils.FindLevels(args[i], levelFolder => { JsonLevel level = JsonLevel.LoadFromFolder(levelFolder); string levelID = level.LevelID(); if (existingLevels.Contains(levelID)) { Console.WriteLine($"Present: {level._songName}"); } else { Console.WriteLine($"Adding: {level._songName}"); AssetPtr levelPtr = level.AddToAssets(assets, apk); extrasCollection.levels.Add(levelPtr); existingLevels.Add(levelID); } }); } byte[] outData = assets.ToBytes(); apk.ReplaceAssetsFile(Apk.MainAssetsFile, outData); } }
static void UpdateText(Apk apk, Dictionary <string, string> replaceText, InvocationResult res) { SerializedAssets textAssets = SerializedAssets.FromBytes(apk.ReadEntireEntry(apk.TextFile()), apk.version); var aotext = textAssets.GetAssetAt(1); TextAssetData ta = aotext.data as TextAssetData; var segments = ta.ReadLocaleText(); TextAssetData.ApplyWatermark(segments); foreach (var entry in replaceText) { Dictionary <string, string> value; if (!segments.TryGetValue(entry.Key, out value)) { continue; } value["ENGLISH"] = entry.Value; } ta.WriteLocaleText(segments); apk.ReplaceAssetsFile(apk.TextFile(), textAssets.ToBytes()); res.didReplaceText = true; }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("arguments: pathToAPKFileToModify levelFolders..."); return; } string apkPath = args[0]; using (Apk apk = new Apk(apkPath)) { apk.PatchSignatureCheck(); byte[] data = apk.ReadEntireEntry(apk.MainAssetsFile()); SerializedAssets assets = SerializedAssets.FromBytes(data, apk.version); HashSet <string> existingLevels = assets.ExistingLevelIDs(); LevelCollectionBehaviorData extrasCollection = assets.FindExtrasLevelCollection(); for (int i = 1; i < args.Length; i++) { Utils.FindLevels(args[i], levelFolder => { try { JsonLevel level = JsonLevel.LoadFromFolder(levelFolder); string levelID = level.GenerateBasicLevelID(); if (existingLevels.Contains(levelID)) { Console.WriteLine($"Present: {level._songName}"); } else { Console.WriteLine($"Adding: {level._songName}"); // We use transactions here so if these throw // an exception, which happens when levels are // invalid, then it doesn't modify the APK in // any way that might screw things up later. var assetsTxn = new SerializedAssets.Transaction(assets); var apkTxn = new Apk.Transaction(); AssetPtr levelPtr = level.AddToAssets(assetsTxn, apkTxn, levelID); // Danger should be over, nothing here should fail assetsTxn.ApplyTo(assets); extrasCollection.levels.Add(levelPtr); existingLevels.Add(levelID); apkTxn.ApplyTo(apk); } } catch (FileNotFoundException e) { Console.WriteLine("[SKIPPING] Missing file referenced by level: {0}", e.FileName); } catch (JsonReaderException e) { Console.WriteLine("[SKIPPING] Invalid level JSON: {0}", e.Message); } }); } byte[] outData = assets.ToBytes(); apk.ReplaceAssetsFile(apk.MainAssetsFile(), outData); apk.Save(); } Console.WriteLine("Signing APK..."); Signer.Sign(apkPath); }
static void SyncLevels( Apk apk, SerializedAssets mainAssets, Invocation inv, InvocationResult res ) { if (inv.levels == null || inv.packs == null) { throw new ApplicationException("Either the 'levels' or 'packs' key is missing. Note the 'levels' key changed names from 'ensureInstalled' in the new version."); } Dictionary <string, ulong> existingLevels = mainAssets.FindLevels(); ulong maxBasePathID = mainAssets.MainAssetsMaxBaseGamePath(); // === Load root level pack SerializedAssets rootPackAssets = SerializedAssets.FromBytes(apk.ReadEntireEntry(apk.RootPackFile()), apk.version); string mainFileName = apk.MainAssetsFileName(); int mainFileI = rootPackAssets.externals.FindIndex(e => e.pathName == mainFileName) + 1; BeatmapLevelPackCollection rootLevelPack = rootPackAssets.FindMainLevelPackCollection(); // Might be null if we're on a version older than v1.1.0 AlwaysOwnedBehaviorData alwaysOwned = mainAssets.FindScript <AlwaysOwnedBehaviorData>(x => true); // === Remove existing custom packs rootLevelPack.beatmapLevelPacks.RemoveAll(ptr => ptr.fileID == mainFileI && ptr.pathID > maxBasePathID); if (alwaysOwned != null) { alwaysOwned.levelPacks.RemoveAll(ptr => ptr.fileID == 0 && ptr.pathID > maxBasePathID); } LevelPackBehaviorData.RemoveCustomPacksFromEnd(mainAssets); // === Remove old-school custom levels from Extras pack var extrasCollection = mainAssets.FindExtrasLevelCollection(); extrasCollection.levels.RemoveAll(ptr => ptr.pathID > maxBasePathID); // === Remove existing levels var toRemove = new HashSet <string>(); foreach (var entry in existingLevels) { if (inv.levels.ContainsKey(entry.Key)) { continue; // requested } if (entry.Value <= maxBasePathID) { continue; // base game level } toRemove.Add(entry.Key); } foreach (string levelID in toRemove) { var ao = mainAssets.GetAssetObjectFromScript <LevelBehaviorData>(p => p.levelID == levelID); var apkTxn = new Apk.Transaction(); Utils.RemoveLevel(mainAssets, ao, apkTxn); apkTxn.ApplyTo(apk); } res.removedLevels = toRemove.ToList(); // === Install new levels var toInstall = new HashSet <string>(); foreach (var entry in inv.levels) { if (existingLevels.ContainsKey(entry.Key)) { continue; // already installed } toInstall.Add(entry.Key); } Program.Install(apk, mainAssets, toInstall, res, inv.levels); // === Create new custom packs Dictionary <string, ulong> availableLevels = mainAssets.FindLevels(); foreach (LevelPack pack in inv.packs) { if (pack.name == null || pack.id == null || pack.levelIDs == null) { throw new ApplicationException("Packs require name, id and levelIDs list"); } var txn = new SerializedAssets.Transaction(mainAssets); CustomPackInfo info = LevelPackBehaviorData.CreateCustomPack( txn, pack.id, pack.name, pack.coverImagePath ); txn.ApplyTo(mainAssets); var customCollection = info.collection.FollowToScript <LevelCollectionBehaviorData>(mainAssets); foreach (string levelID in pack.levelIDs) { ulong levelPathID; if (!availableLevels.TryGetValue(levelID, out levelPathID)) { res.missingFromPacks.Add(levelID); continue; } customCollection.levels.Add(new AssetPtr(0, levelPathID)); } rootLevelPack.beatmapLevelPacks.Add(new AssetPtr(mainFileI, info.pack.pathID)); if (alwaysOwned != null) { alwaysOwned.levelPacks.Add(info.pack); } } res.presentLevels = availableLevels.Keys.ToList(); apk.ReplaceAssetsFile(apk.RootPackFile(), rootPackAssets.ToBytes()); }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("arguments: pathToAPKFileToModify [-r removeSongs] levelFolders..."); return; } bool removeSongs = false; if (args.Contains("-r") || args.Contains("removeSongs")) { removeSongs = true; } bool replaceExtras = false; if (args.Contains("-e")) { replaceExtras = true; } string apkPath = args[0]; using (Apk apk = new Apk(apkPath)) { apk.PatchSignatureCheck(); byte[] data = apk.ReadEntireEntry(Apk.MainAssetsFile); SerializedAssets assets = SerializedAssets.FromBytes(data); string colorPath = "assets/bin/Data/sharedassets1.assets"; SerializedAssets colorAssets = SerializedAssets.FromBytes(apk.ReadEntireEntry(colorPath)); //string textAssetsPath = "assets/bin/Data/c4dc0d059266d8d47862f46460cf8f31"; string textAssetsPath = "assets/bin/Data/231368cb9c1d5dd43988f2a85226e7d7"; SerializedAssets textAssets = SerializedAssets.FromBytes(apk.ReadEntireEntry(textAssetsPath)); HashSet <string> existingLevels = assets.ExistingLevelIDs(); LevelCollectionBehaviorData customCollection = assets.FindCustomLevelCollection(); LevelPackBehaviorData customPack = assets.FindCustomLevelPack(); ulong customPackPathID = assets.GetAssetObjectFromScript <LevelPackBehaviorData>(mob => mob.name == "CustomLevelPack", b => true).pathID; for (int i = 1; i < args.Length; i++) { if (args[i] == "-r" || args[i] == "removeSongs" || args[i] == "-e") { continue; } if (args[i] == "-t") { if (i + 2 >= args.Length) { // There is not enough data after the text // Reset it. //continue; } var ao = textAssets.GetAssetAt(1); TextAssetAssetData ta = ao.data as TextAssetAssetData; string key = args[i + 1].ToUpper(); var segments = Utils.ReadLocaleText(ta.script, new List <char>() { ',', ',', '\n' }); //segments.ToList().ForEach(a => Console.Write(a.Trim() + ",")); List <string> value; if (!segments.TryGetValue(key.Trim(), out value)) { Console.WriteLine($"[ERROR] Could not find key: {key} in text!"); } Console.WriteLine($"Found key at index: {key.Trim()} with value: {value[value.Count - 1]}"); segments[key.Trim()][value.Count - 1] = args[i + 2]; Console.WriteLine($"New value: {args[i + 2]}"); Utils.ApplyWatermark(segments); ta.script = Utils.WriteLocaleText(segments, new List <char>() { ',', ',', '\n' }); i += 2; apk.ReplaceAssetsFile(textAssetsPath, textAssets.ToBytes()); //Console.WriteLine((a.data as TextAsset).script); continue; } if (args[i] == "-c1" || args[i] == "-c2") { if (i + 1 >= args.Length) { // There is nothing after the color // Reset it. Utils.ResetColors(colorAssets); apk.ReplaceAssetsFile(colorPath, colorAssets.ToBytes()); continue; } if (!args[i + 1].StartsWith("(")) { // Reset it. Utils.ResetColors(colorAssets); apk.ReplaceAssetsFile(colorPath, colorAssets.ToBytes()); continue; } if (i + 4 >= args.Length) { Console.WriteLine($"[ERROR] Cannot parse color, not enough colors! Please copy-paste a series of floats"); i += 4; continue; } SimpleColor c = new SimpleColor { r = Convert.ToSingle(args[i + 1].Split(',')[0].Replace('(', '0')), g = Convert.ToSingle(args[i + 2].Split(',')[0].Replace('(', '0')), b = Convert.ToSingle(args[i + 3].Split(',')[0].Replace(')', '0')), a = Convert.ToSingle(args[i + 4].Split(',')[0].Replace(')', '.')) }; ColorManager dat = Utils.CreateColor(colorAssets, c); var ptr = colorAssets.AppendAsset(new MonoBehaviorAssetData() { data = c, name = "CustomColor" + args[i][args[i].Length - 1], script = new AssetPtr(1, SimpleColor.PathID), }); Console.WriteLine($"Created new CustomColor for colorA at PathID: {ptr.pathID}"); if (args[i] == "-c1") { dat.colorA = ptr; } else { dat.colorB = ptr; } apk.ReplaceAssetsFile(colorPath, colorAssets.ToBytes()); i += 4; continue; } if (args[i] == "-g") { string path = "assets/bin/Data/level11"; SerializedAssets a = SerializedAssets.FromBytes(apk.ReadEntireEntry(path)); var gameobject = a.FindGameObject("LeftSaber"); var script = gameobject.components[4].FollowToScript <Saber>(a); Console.WriteLine($"GameObject: {gameobject}"); foreach (AssetPtr p in gameobject.components) { Console.WriteLine($"Component: {p.pathID} followed: {p.Follow(a)}"); } Console.WriteLine($"Left saber script: {script}"); // Find all objects that have the GameObject: LeftSaber (pathID = 20, fileID = 0 (142)) continue; } if (args[i] == "-s") { string cusomCoverFile = args[i + 1]; try { Texture2DAssetData dat = assets.GetAssetAt(14).data as Texture2DAssetData; //assets.SetAssetAt(14, dat); var ptr = assets.AppendAsset(Texture2DAssetData.CoverFromImageFile(args[i + 1], "CustomSongs", true)); Console.WriteLine($"Added Texture at PathID: {ptr.pathID} with new Texture2D from file: {args[i + 1]}"); var sPtr = assets.AppendAsset(Utils.CreateSprite(assets, ptr)); Console.WriteLine($"Added Sprite at PathID: {sPtr.pathID}!"); customPack.coverImage = sPtr; } catch (FileNotFoundException) { Console.WriteLine($"[ERROR] Custom cover file does not exist: {args[i+1]}"); } i++; continue; } Utils.FindLevels(args[i], levelFolder => { try { JsonLevel level = JsonLevel.LoadFromFolder(levelFolder); string levelID = level.GenerateBasicLevelID(); var apkTxn = new Apk.Transaction(); if (existingLevels.Contains(levelID)) { if (removeSongs) { // Currently does not handle transactions Console.WriteLine($"Removing: {level._songName}"); existingLevels.Remove(levelID); var l = assets.GetLevelMatching(levelID); var ao = assets.GetAssetObjectFromScript <LevelBehaviorData>(p => p.levelID == l.levelID); ulong lastLegitPathID = 201; // Currently, this removes all songs matching the song // the very first time it runs customCollection.levels.RemoveAll(ptr => ptr.pathID > lastLegitPathID && ao.pathID == ptr.pathID); foreach (string s in l.OwnedFiles(assets)) { if (apk != null) { apk.RemoveFileAt($"assets/bin/Data/{s}"); } } Utils.RemoveLevel(assets, l); apkTxn.ApplyTo(apk); } else { Console.WriteLine($"Present: {level._songName}"); } } else { Console.WriteLine($"Adding: {level._songName}"); // We use transactions here so if these throw // an exception, which happens when levels are // invalid, then it doesn't modify the APK in // any way that might screw things up later. var assetsTxn = new SerializedAssets.Transaction(assets); AssetPtr levelPtr = level.AddToAssets(assetsTxn, apkTxn, levelID); // Danger should be over, nothing here should fail assetsTxn.ApplyTo(assets); customCollection.levels.Add(levelPtr); existingLevels.Add(levelID); apkTxn.ApplyTo(apk); } } catch (FileNotFoundException e) { Console.WriteLine("[SKIPPING] Missing file referenced by level: {0}", e.FileName); } catch (JsonReaderException e) { Console.WriteLine("[SKIPPING] Invalid level JSON: {0}", e.Message); } }); } byte[] outData = assets.ToBytes(); apk.ReplaceAssetsFile(Apk.MainAssetsFile, outData); string mainPackFile = "assets/bin/Data/sharedassets19.assets"; SerializedAssets mainPackAssets = SerializedAssets.FromBytes(apk.ReadEntireEntry(mainPackFile)); // Modify image to be CustomLevelPack image? //customPack.coverImage = new AssetPtr(assets.externals.FindIndex(e => e.pathName == "sharedassets19.assets")) // Adds custom pack to the set of all packs int fileI = mainPackAssets.externals.FindIndex(e => e.pathName == "sharedassets17.assets") + 1; Console.WriteLine($"Found sharedassets17.assets at FileID: {fileI}"); var mainLevelPack = mainPackAssets.FindMainLevelPackCollection(); var pointerPacks = mainLevelPack.beatmapLevelPacks[mainLevelPack.beatmapLevelPacks.Count - 1]; Console.WriteLine($"Original last pack FileID: {pointerPacks.fileID} PathID: {pointerPacks.pathID}"); if (!mainLevelPack.beatmapLevelPacks.Any(ptr => ptr.fileID == fileI && ptr.pathID == customPackPathID)) { Console.WriteLine($"Added CustomLevelPack to {mainPackFile}"); if (replaceExtras) { Console.WriteLine("Replacing ExtrasPack!"); mainLevelPack.beatmapLevelPacks[2] = new AssetPtr(fileI, customPackPathID); } else { Console.WriteLine("Adding as new Pack!"); mainLevelPack.beatmapLevelPacks.Add(new AssetPtr(fileI, customPackPathID)); } } pointerPacks = mainLevelPack.beatmapLevelPacks[mainLevelPack.beatmapLevelPacks.Count - 1]; Console.WriteLine($"New last pack FileID: {pointerPacks.fileID} PathID: {pointerPacks.pathID}"); apk.ReplaceAssetsFile(mainPackFile, mainPackAssets.ToBytes()); Console.WriteLine("Complete!"); } Console.WriteLine("Signing APK..."); Signer.Sign(apkPath); }