static void Install( Apk apk, SerializedAssets assets, HashSet <string> toInstall, InvocationResult res, Dictionary <string, string> levels ) { foreach (string levelID in toInstall) { string levelFolder = levels[levelID]; try { JsonLevel level = JsonLevel.LoadFromFolder(levelFolder); // 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); apkTxn.ApplyTo(apk); res.installedLevels.Add(levelID); } catch (FileNotFoundException e) { res.installSkipped.Add(levelID, $"Missing file referenced by level: {e.FileName}"); } catch (JsonReaderException e) { res.installSkipped.Add(levelID, $"Invalid level JSON: {e.Message}"); } } }
private AudioClipAssetData CreateAudioAsset(Apk.Transaction apk, string levelID) { string audioClipFile = Path.Combine(levelFolderPath, _songFilename); string sourceFileName = levelID + ".ogg"; apk.CopyFileInto(audioClipFile, $"assets/bin/Data/{sourceFileName}"); ulong fileSize = (ulong)new FileInfo(audioClipFile).Length; using (NVorbis.VorbisReader v = new NVorbis.VorbisReader(audioClipFile)) { return(new AudioClipAssetData() { name = levelID, loadType = 1, channels = v.Channels, frequency = v.SampleRate, bitsPerSample = 16, length = (Single)v.TotalTime.TotalSeconds, isTracker = false, subsoundIndex = 0, preloadAudio = false, backgroundLoad = true, legacy3D = true, compressionFormat = 1, // vorbis source = sourceFileName, offset = 0, size = fileSize, }); } }
public AssetPtr AddToAssets(SerializedAssets.Transaction assets, Apk.Transaction apk, string levelID) { // var watch = System.Diagnostics.Stopwatch.StartNew(); AudioClipAssetData audioClip = CreateAudioAsset(apk, levelID); AssetPtr audioClipPtr = assets.AppendAsset(audioClip); string coverPath = Path.Combine(levelFolderPath, _coverImageFilename); Texture2DAssetData cover = Texture2DAssetData.CoverFromImageFile(coverPath, levelID); AssetPtr coverPtr = assets.AppendAsset(cover); AssetPtr environment = new AssetPtr(20, 1); // default environment switch (_environmentName) { case "NiceEnvironment": environment = new AssetPtr(38, 3); break; case "TriangleEnvironment": environment = new AssetPtr(0, 252); break; case "BigMirrorEnvironment": environment = new AssetPtr(0, 249); break; } LevelBehaviorData level = new LevelBehaviorData() { levelID = levelID, songName = _songName, songSubName = _songSubName, songAuthorName = _songAuthorName, levelAuthorName = _levelAuthorName, beatsPerMinute = _beatsPerMinute, songTimeOffset = _songTimeOffset, shuffle = _shuffle, shufflePeriod = _shufflePeriod, previewStartTime = _previewStartTime, previewDuration = _previewDuration, audioClip = audioClipPtr, coverImage = coverPtr, environment = environment, difficultyBeatmapSets = _difficultyBeatmapSets.Select(s => s.ToAssets(assets, levelFolderPath, levelID)).Where(s => s != null).ToList(), }; MonoBehaviorAssetData monob = new MonoBehaviorAssetData() { script = new AssetPtr(1, LevelBehaviorData.PathID), name = level.levelID + "Level", data = level, }; // watch.Stop(); // Console.WriteLine("song: " + watch.ElapsedMilliseconds); return(assets.AppendAsset(monob)); }
static void EnsureInstalled( Apk apk, SerializedAssets assets, HashSet <string> existingLevels, InvocationResult res, Dictionary <string, string> ensureInstalled ) { LevelCollectionBehaviorData extrasCollection = assets.FindExtrasLevelCollection(); foreach (KeyValuePair <string, string> entry in ensureInstalled) { string levelID = entry.Key; string levelFolder = entry.Value; try { JsonLevel level = JsonLevel.LoadFromFolder(levelFolder); if (existingLevels.Contains(levelID)) { res.installSkipped.Add(levelID, "Present"); } else { // 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); apkTxn.ApplyTo(apk); existingLevels.Add(levelID); res.installedLevels.Add(levelID); } } catch (FileNotFoundException e) { res.installSkipped.Add(levelID, $"Missing file referenced by level: {e.FileName}"); } catch (JsonReaderException e) { res.installSkipped.Add(levelID, $"Invalid level JSON: {e.Message}"); } } }
public void TestBigFile() { using (Apk apk = new Apk(baseAPKPath)) { byte[] data = apk.ReadEntireEntry(Apk.MainAssetsFile); var assets = TestRoundTrips(data, "big"); var existing = assets.ExistingLevelIDs(); Assert.NotEmpty(existing); Assert.False(existing.Contains("BUBBLETEA"), "Run tests on a non-patched APK"); JsonLevel level = JsonLevel.LoadFromFolder(repoPath("testdata/bubble_tea_song/")); var assetsTxn = new SerializedAssets.Transaction(assets); var apkTxn = new Apk.Transaction(); AssetPtr levelPtr = level.AddToAssets(assetsTxn, apkTxn, level.GenerateBasicLevelID()); assetsTxn.ApplyTo(assets); // don't apply apkTxn so our tests don't modify the APK LevelCollectionBehaviorData extrasCollection = assets.FindExtrasLevelCollection(); extrasCollection.levels.Add(levelPtr); byte[] outData = assets.ToBytes(); File.WriteAllBytes($"../../../../testoutput/bubble_tea_mod.asset", outData); } }
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); }
// This method will fail if given an object that is not a LevelBehaviorData public static void RemoveLevel(SerializedAssets assets, SerializedAssets.AssetObject obj, Apk.Transaction apk) { LevelBehaviorData level = (obj.data as MonoBehaviorAssetData).data as LevelBehaviorData; // Remove audio file foreach (string s in level.OwnedFiles(assets)) { apk.RemoveFileAt($"assets/bin/Data/{s}"); } // Remove things from bottom up, so that pointers to other assets still are rooted // and so get fixed up by RemoveAssetAt foreach (BeatmapSet s in level.difficultyBeatmapSets) { foreach (BeatmapDifficulty d in s.difficultyBeatmaps) { assets.RemoveAssetAt(d.beatmapData.pathID); } } assets.RemoveAssetAt(level.coverImage.pathID); assets.RemoveAssetAt(level.audioClip.pathID); assets.RemoveAssetAt(obj.pathID); }
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); }