|
| 1 | +using Kermalis.EndianBinaryIO; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace Kermalis.FLP; |
| 8 | + |
| 9 | +public sealed class FLProjectWriter |
| 10 | +{ |
| 11 | + private static ReadOnlySpan<byte> MIDIInfo0 => new byte[20] |
| 12 | + { |
| 13 | + 0x01, 0x00, 0x00, 0x00, |
| 14 | + 0x00, 0x00, 0x00, 0x00, |
| 15 | + 0x01, |
| 16 | + 0x90, // 144 |
| 17 | + 0xFF, 0x0F, |
| 18 | + 0x04, 0x00, 0x00, 0x00, |
| 19 | + 0xD5, 0x01, // 469 |
| 20 | + 0x00, 0x00 |
| 21 | + }; |
| 22 | + private static ReadOnlySpan<byte> MIDIInfo1 => new byte[20] |
| 23 | + { |
| 24 | + 0xFD, 0x00, 0x00, 0x00, |
| 25 | + 0x00, 0x00, 0x00, 0x00, |
| 26 | + 0x80, // 128 |
| 27 | + 0x90, // 144 |
| 28 | + 0xFF, 0x0F, |
| 29 | + 0x04, 0x00, 0x00, 0x00, |
| 30 | + 0xD5, 0x01, // 469 |
| 31 | + 0x00, 0x00 |
| 32 | + }; |
| 33 | + private static ReadOnlySpan<byte> MIDIInfo2 => new byte[20] |
| 34 | + { |
| 35 | + 0xFF, 0x00, 0x00, 0x00, |
| 36 | + 0x00, 0x00, 0x00, 0x00, |
| 37 | + 0x04, // 4 |
| 38 | + 0x00, |
| 39 | + 0xFF, 0x0F, |
| 40 | + 0x04, 0x00, 0x00, 0x00, |
| 41 | + 0x00, 0xFE, |
| 42 | + 0xFF, 0xFF |
| 43 | + }; |
| 44 | + |
| 45 | + public readonly FLInsert[] Inserts; |
| 46 | + public readonly List<FLChannelFilter> ChannelFilters; |
| 47 | + public readonly List<FLChannel> Channels; |
| 48 | + public readonly List<FLAutomation> Automations; |
| 49 | + public readonly List<FLPattern> Patterns; |
| 50 | + public readonly List<FLArrangement> Arrangements; |
| 51 | + public FLVersionCompat VersionCompat; |
| 52 | + public ushort PPQN; |
| 53 | + public decimal CurrentTempo; |
| 54 | + public byte TimeSigNumerator; |
| 55 | + public byte TimeSigDenominator; |
| 56 | + public FLChannelFilter? SelectedChannelFilter; |
| 57 | + public FLArrangement? SelectedArrangement; |
| 58 | + |
| 59 | + public FLProjectWriter() |
| 60 | + { |
| 61 | + // FL Default |
| 62 | + PPQN = 96; |
| 63 | + CurrentTempo = 140; |
| 64 | + TimeSigNumerator = 4; |
| 65 | + TimeSigDenominator = 4; |
| 66 | + |
| 67 | + ChannelFilters = new List<FLChannelFilter>(); |
| 68 | + Channels = new List<FLChannel>(); |
| 69 | + Automations = new List<FLAutomation>(); |
| 70 | + Patterns = new List<FLPattern>(); |
| 71 | + Arrangements = new List<FLArrangement>(1) |
| 72 | + { |
| 73 | + new FLArrangement("Arrangement"), |
| 74 | + }; |
| 75 | + |
| 76 | + Inserts = new FLInsert[127]; |
| 77 | + for (byte i = 0; i < 127; i++) |
| 78 | + { |
| 79 | + Inserts[i] = new FLInsert(i); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public FLChannelFilter CreateUnsortedFilter() |
| 84 | + { |
| 85 | + return CreateChannelFilter("Unsorted"); |
| 86 | + } |
| 87 | + public FLChannelFilter CreateAutomationFilter() |
| 88 | + { |
| 89 | + return CreateChannelFilter("Automation"); |
| 90 | + } |
| 91 | + public FLChannelFilter CreateChannelFilter(string name) |
| 92 | + { |
| 93 | + var f = new FLChannelFilter(name); |
| 94 | + ChannelFilters.Add(f); |
| 95 | + return f; |
| 96 | + } |
| 97 | + |
| 98 | + public FLChannel CreateChannel(string name, byte midiChan, byte midiBank, byte midiProgram, FLChannelFilter filter) |
| 99 | + { |
| 100 | + var c = new FLChannel(name, midiChan, midiBank, midiProgram, filter); |
| 101 | + Channels.Add(c); |
| 102 | + return c; |
| 103 | + } |
| 104 | + public FLAutomation CreateTempoAutomation(string name, FLChannelFilter filter) |
| 105 | + { |
| 106 | + var a = new FLAutomation(name, FLAutomation.MyType.Tempo, null, filter); |
| 107 | + Automations.Add(a); |
| 108 | + return a; |
| 109 | + } |
| 110 | + public FLAutomation CreateAutomation(string name, FLAutomation.MyType type, List<FLChannel> targets, FLChannelFilter filter) |
| 111 | + { |
| 112 | + var a = new FLAutomation(name, type, targets, filter); |
| 113 | + Automations.Add(a); |
| 114 | + return a; |
| 115 | + } |
| 116 | + public FLAutomation CreateAutomation(string name, FLAutomation.MyType type, FLChannel target, FLChannelFilter filter) |
| 117 | + { |
| 118 | + var a = new FLAutomation(name, type, new List<FLChannel>(1) { target }, filter); |
| 119 | + Automations.Add(a); |
| 120 | + return a; |
| 121 | + } |
| 122 | + |
| 123 | + public FLPattern CreatePattern() |
| 124 | + { |
| 125 | + var p = new FLPattern(); |
| 126 | + Patterns.Add(p); |
| 127 | + return p; |
| 128 | + } |
| 129 | + |
| 130 | + public void Write(Stream s) |
| 131 | + { |
| 132 | + var w = new EndianBinaryWriter(s, ascii: true); |
| 133 | + |
| 134 | + FirstPassAssignIDs(); |
| 135 | + |
| 136 | + WriteHeaderChunk(w); |
| 137 | + WriteDataChunk(w); |
| 138 | + } |
| 139 | + private void FirstPassAssignIDs() |
| 140 | + { |
| 141 | + // Channel Filters must be alphabetical. Even if I don't sort them, they will be opened in the wrong order |
| 142 | + ChannelFilters.Sort((c1, c2) => c1.Name.CompareTo(c2.Name)); |
| 143 | + for (ushort i = 0; i < ChannelFilters.Count; i++) |
| 144 | + { |
| 145 | + ChannelFilters[i].Index = i; |
| 146 | + } |
| 147 | + ushort chanIndex = 0; |
| 148 | + foreach (FLChannel c in Channels) |
| 149 | + { |
| 150 | + c.Index = chanIndex++; |
| 151 | + } |
| 152 | + foreach (FLAutomation a in Automations) |
| 153 | + { |
| 154 | + a.Index = chanIndex++; |
| 155 | + } |
| 156 | + for (ushort i = 0; i < Patterns.Count; i++) |
| 157 | + { |
| 158 | + Patterns[i].Index = i; |
| 159 | + } |
| 160 | + for (ushort i = 0; i < Arrangements.Count; i++) |
| 161 | + { |
| 162 | + Arrangements[i].Index = i; |
| 163 | + } |
| 164 | + } |
| 165 | + private void WriteHeaderChunk(EndianBinaryWriter w) |
| 166 | + { |
| 167 | + w.WriteChars("FLhd"); |
| 168 | + w.WriteUInt32(6); // Length |
| 169 | + w.WriteUInt16(0); // Format |
| 170 | + w.WriteUInt16((ushort)(Channels.Count + Automations.Count)); |
| 171 | + w.WriteUInt16(PPQN); |
| 172 | + } |
| 173 | + private void WriteDataChunk(EndianBinaryWriter w) |
| 174 | + { |
| 175 | + w.WriteChars("FLdt"); |
| 176 | + |
| 177 | + long dataLenOffset = w.Stream.Position; |
| 178 | + w.WriteUInt32(0); // Write length later |
| 179 | + |
| 180 | + WriteProjectInfo(w); |
| 181 | + WriteChannels(w); |
| 182 | + foreach (FLArrangement a in Arrangements) |
| 183 | + { |
| 184 | + a.Write(w, VersionCompat); |
| 185 | + } |
| 186 | + WriteMoreStuffIDK(w); |
| 187 | + WriteMixer(w); |
| 188 | + |
| 189 | + // Write chunk length |
| 190 | + uint length = (uint)(w.Stream.Length - (dataLenOffset + 4)); |
| 191 | + w.Stream.Position = dataLenOffset; |
| 192 | + w.WriteUInt32(length); |
| 193 | + } |
| 194 | + |
| 195 | + internal static void Write8BitEvent(EndianBinaryWriter w, FLEvent ev, byte data) |
| 196 | + { |
| 197 | + w.WriteEnum(ev); |
| 198 | + w.WriteByte(data); |
| 199 | + } |
| 200 | + internal static void Write16BitEvent(EndianBinaryWriter w, FLEvent ev, ushort data) |
| 201 | + { |
| 202 | + w.WriteEnum(ev); |
| 203 | + w.WriteUInt16(data); |
| 204 | + } |
| 205 | + internal static void Write32BitEvent(EndianBinaryWriter w, FLEvent ev, uint data) |
| 206 | + { |
| 207 | + w.WriteEnum(ev); |
| 208 | + w.WriteUInt32(data); |
| 209 | + } |
| 210 | + internal static void WriteArrayEventLength(EndianBinaryWriter w, uint length) |
| 211 | + { |
| 212 | + // TODO: How many bytes can this len use? |
| 213 | + while (true) |
| 214 | + { |
| 215 | + if (length <= 0x7F) |
| 216 | + { |
| 217 | + w.WriteByte((byte)length); |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + w.WriteByte((byte)((length & 0x7Fu) | 0x80u)); |
| 222 | + length >>= 7; |
| 223 | + } |
| 224 | + } |
| 225 | + internal static void WriteUTF8EventWithLength(EndianBinaryWriter w, FLEvent ev, string str) |
| 226 | + { |
| 227 | + WriteArrayEventWithLength(w, ev, Encoding.UTF8.GetBytes(str)); |
| 228 | + } |
| 229 | + internal static void WriteUTF16EventWithLength(EndianBinaryWriter w, FLEvent ev, string str) |
| 230 | + { |
| 231 | + WriteArrayEventWithLength(w, ev, Encoding.Unicode.GetBytes(str)); |
| 232 | + } |
| 233 | + internal static void WriteArrayEventWithLength(EndianBinaryWriter w, FLEvent ev, ReadOnlySpan<byte> bytes) |
| 234 | + { |
| 235 | + w.WriteEnum(ev); |
| 236 | + WriteArrayEventLength(w, (uint)bytes.Length); |
| 237 | + w.WriteBytes(bytes); |
| 238 | + } |
| 239 | + |
| 240 | + private void WriteProjectInfo(EndianBinaryWriter w) |
| 241 | + { |
| 242 | + WriteVersion(w); |
| 243 | + WriteRegistration(w); |
| 244 | + |
| 245 | + Write32BitEvent(w, FLEvent.FineTempo, (uint)(CurrentTempo * 1_000)); |
| 246 | + Write16BitEvent(w, FLEvent.SelectedPatternNum, 1); |
| 247 | + Write8BitEvent(w, FLEvent.IsSongMode, 1); |
| 248 | + Write8BitEvent(w, FLEvent.Shuffle, 0); |
| 249 | + Write16BitEvent(w, FLEvent.MasterPitch, 0); |
| 250 | + Write8BitEvent(w, FLEvent.ProjectTimeSigNumerator, TimeSigNumerator); |
| 251 | + Write8BitEvent(w, FLEvent.ProjectTimeSigDenominator, TimeSigDenominator); |
| 252 | + Write8BitEvent(w, FLEvent.ProjectShouldUseTimeSignatures, 1); |
| 253 | + Write8BitEvent(w, FLEvent.PanningLaw, 0); // Circular |
| 254 | + if (VersionCompat == FLVersionCompat.V21_0_3__B3517) |
| 255 | + { |
| 256 | + Write8BitEvent(w, FLEvent.PlaylistShouldUseAutoCrossfades, 0); |
| 257 | + } |
| 258 | + Write8BitEvent(w, FLEvent.ShouldPlayTruncatedClipNotes, 1); |
| 259 | + Write8BitEvent(w, FLEvent.ShouldShowInfoOnOpen, 0); |
| 260 | + WriteUTF16EventWithLength(w, FLEvent.ProjectTitle, "\0"); |
| 261 | + WriteUTF16EventWithLength(w, FLEvent.ProjectGenre, "\0"); |
| 262 | + WriteUTF16EventWithLength(w, FLEvent.ProjectAuthor, "\0"); |
| 263 | + WriteUTF16EventWithLength(w, FLEvent.ProjectDataPath, "\0"); |
| 264 | + WriteUTF16EventWithLength(w, FLEvent.ProjectComment, "\0"); |
| 265 | + // ProjectURL would go here |
| 266 | + FLProjectTime.Write(w, DateTime.Now, TimeSpan.Zero); |
| 267 | + |
| 268 | + foreach (FLChannelFilter f in ChannelFilters) |
| 269 | + { |
| 270 | + f.Write(w); |
| 271 | + } |
| 272 | + Write32BitEvent(w, FLEvent.CurFilterNum, SelectedChannelFilter is null ? 0 : (uint)SelectedChannelFilter.Index); |
| 273 | + |
| 274 | + WriteArrayEventWithLength(w, FLEvent.CtrlRecChan, Array.Empty<byte>()); |
| 275 | + // TODO: Why did this CtrlRecChan only show up sometimes? 0x1900 = 6_400 |
| 276 | + // Bytes: CtrlRecChan - 12 = [0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00] |
| 277 | + foreach (FLPattern p in Patterns) |
| 278 | + { |
| 279 | + p.WritePatternNotes(w); |
| 280 | + } |
| 281 | + |
| 282 | + // No idea what these mean, but there are 3 and they're always the same in all my projects. |
| 283 | + // Dunno if it's MIDI keyboard related or something, because I only have 1 |
| 284 | + WriteArrayEventWithLength(w, FLEvent.MIDIInfo, MIDIInfo0); |
| 285 | + WriteArrayEventWithLength(w, FLEvent.MIDIInfo, MIDIInfo1); |
| 286 | + WriteArrayEventWithLength(w, FLEvent.MIDIInfo, MIDIInfo2); |
| 287 | + } |
| 288 | + private void WriteVersion(EndianBinaryWriter w) |
| 289 | + { |
| 290 | + string v; |
| 291 | + uint b; |
| 292 | + |
| 293 | + switch (VersionCompat) |
| 294 | + { |
| 295 | + case FLVersionCompat.V20_9_2__B2963: |
| 296 | + { |
| 297 | + v = "20.9.2.2963"; |
| 298 | + b = 2963; |
| 299 | + break; |
| 300 | + } |
| 301 | + case FLVersionCompat.V21_0_3__B3517: |
| 302 | + { |
| 303 | + v = "21.0.3.3517"; |
| 304 | + b = 3517; |
| 305 | + break; |
| 306 | + } |
| 307 | + default: throw new InvalidOperationException("Invalid FL Version compatibility: " + VersionCompat); |
| 308 | + } |
| 309 | + |
| 310 | + WriteUTF8EventWithLength(w, FLEvent.Version, v + '\0'); |
| 311 | + Write32BitEvent(w, FLEvent.VersionBuildNumber, b); |
| 312 | + } |
| 313 | + private static void WriteRegistration(EndianBinaryWriter w) |
| 314 | + { |
| 315 | + // When you save a project in FL Studio, the entire file becomes obfuscated if IsRegistered is 0 (trial mode). |
| 316 | + // Specifically, every 8bit/16bit/32bit event becomes obfuscated. The other array data is left alone (from the small glimpse I had), and it makes sense to leave it. |
| 317 | + // For example, the Unk_37 byte here became 76 instead of 1 in FL21. I didn't try different projects or FL versions, but if I had to guess, it is probably randomized and seeds the obfuscation. |
| 318 | + // Every other event also became obfuscated in some way that I couldn't quickly decipher with Windows calculator since it seems to mix the current position and eventID with the seed. |
| 319 | + // Examples: |
| 320 | + // ================ |
| 321 | + // Byte: IsSongMode = 1 | IsSongMode = 62 (0x3E) |
| 322 | + // Byte: Shuffle = 0 | Shuffle = 61 (0x3D) |
| 323 | + // Byte: ProjectShouldUseTimeSignatures = 1 | ProjectShouldUseTimeSignatures = 53 (0x35) |
| 324 | + // Byte: PanningLaw = 0 | PanningLaw = 50 (0x32) |
| 325 | + // ================ |
| 326 | + // Word: MasterPitch = 0 | MasterPitch = 15931 (0x3E3B) |
| 327 | + // Word: NewPattern = 1 | NewPattern = 8220 (0x201C) |
| 328 | + // Word: NewPattern = 2 | NewPattern = 6677 (0x1A15) |
| 329 | + // ================ |
| 330 | + // DWord: FineTempo = 185000 | FineTempo = 1347393775 (0x504F98EF) |
| 331 | + // DWord: CurFilterNum = 0 | CurFilterNum = 757737252 (0x2D2A2724) |
| 332 | + |
| 333 | + // I can only imagine that trying to decipher this is trouble waiting to happen, so I won't try to. |
| 334 | + // They clearly want to obfuscate trial projects in a proprietary way, in order to prevent people from using FL in a trial and then using the project files with other software. |
| 335 | + // Reverse-engineering is protected by law in the USA (where I live), but Image-Line can make it against their TOS to try to decipher trial mode projects. They probably have, but I didn't check. |
| 336 | + // If you ever manage to reverse-engineer their method, they will 100% just change it to a new one, then you can't support new versions lol |
| 337 | + |
| 338 | + // They probably don't obfuscate the registered projects since it'd defeat the purpose of buying FL if you were trying to do something outside of it (like this). |
| 339 | + // If paying wouldn't make the project easier to parse, and you could defeat the obfuscation algorithm, you'd remain on the trial version or pirate it which is against Image-Line's interests. |
| 340 | + |
| 341 | + // Anyway, FL seems to be fine when reading projects that are in trial mode, even if they have no obfuscation. I assume this is due to the Unk_37 byte here still being 1 instead of 76. |
| 342 | + // So I am writing unregistered projects with no obfuscation. Thanks Image-Line for making this possible :) |
| 343 | + |
| 344 | + // I put my registration ID there as a reference to what it might look like. |
| 345 | + // I assume this is nothing to worry about since that would be so easy to find if you open a project I share. I've also seen other parsing projects show theirs. |
| 346 | + |
| 347 | + // Hopefully you enjoyed reading this part |
| 348 | + |
| 349 | + Write8BitEvent(w, FLEvent.IsRegistered, 0); // 1 if registered |
| 350 | + Write8BitEvent(w, FLEvent.Unk_37, 1); // Obfuscation-related? |
| 351 | + WriteUTF16EventWithLength(w, FLEvent.RegistrationID, "\0"); // Mine is "d3@?4xufs49p1n?B>;?889\0" in FL20 and FL21 |
| 352 | + } |
| 353 | + private void WriteChannels(EndianBinaryWriter w) |
| 354 | + { |
| 355 | + foreach (FLAutomation a in Automations) |
| 356 | + { |
| 357 | + a.WriteAutomationConnection(w); |
| 358 | + } |
| 359 | + |
| 360 | + foreach (FLChannel c in Channels) |
| 361 | + { |
| 362 | + c.Write(w, VersionCompat); |
| 363 | + } |
| 364 | + // For some reason, pattern colors go between |
| 365 | + foreach (FLPattern p in Patterns) |
| 366 | + { |
| 367 | + p.WriteColorAndNameIfNecessary(w); |
| 368 | + } |
| 369 | + // |
| 370 | + foreach (FLAutomation a in Automations) |
| 371 | + { |
| 372 | + a.Write(w, VersionCompat, PPQN); |
| 373 | + } |
| 374 | + } |
| 375 | + private void WriteMoreStuffIDK(EndianBinaryWriter w) |
| 376 | + { |
| 377 | + Write16BitEvent(w, FLEvent.CurArrangementNum, SelectedArrangement is null ? (ushort)0 : SelectedArrangement.Index); |
| 378 | + Write8BitEvent(w, FLEvent.APDC, 1); |
| 379 | + Write8BitEvent(w, FLEvent.Unk_39, 1); |
| 380 | + Write8BitEvent(w, FLEvent.ShouldCutNotesFast, 0); |
| 381 | + Write8BitEvent(w, FLEvent.EEAutoMode, 0); |
| 382 | + Write8BitEvent(w, FLEvent.Unk_38, 1); |
| 383 | + } |
| 384 | + private void WriteMixer(EndianBinaryWriter w) |
| 385 | + { |
| 386 | + foreach (FLInsert i in Inserts) |
| 387 | + { |
| 388 | + i.Write(w, VersionCompat); |
| 389 | + } |
| 390 | + |
| 391 | + FLMixerParams.Write(w); |
| 392 | + |
| 393 | + Write32BitEvent(w, FLEvent.WindowH, 1286); // TODO: WindowH for what? Piano roll? Mixer? |
| 394 | + } |
| 395 | +} |
0 commit comments