-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from reflektone-games/feat/serializer
feat: serializer
- Loading branch information
Showing
15 changed files
with
300 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { EachStyle } from "../../structures/eachStyle"; | ||
import { MaiChart } from "../../structures/maiChart"; | ||
import { NoteCollection } from "../../structures/noteCollection"; | ||
|
||
/** | ||
* INTERNAL USAGE ONLY! DO NOT USE THIS PROPERTY DIRECTLY! | ||
*/ | ||
export class Serializer { | ||
private _currentTimingChange: number = 0; | ||
private _currentNoteCollection: number = 0; | ||
private _currentTime: number = 0; | ||
|
||
// there's no such thing as StringWriter in TypeScript | ||
public serialize(chart: MaiChart): string { | ||
let writer = ""; | ||
|
||
writer += `(${chart.timingChanges[this._currentTimingChange].tempo})`; | ||
writer += `{${chart.timingChanges[this._currentTimingChange].subdivisions}}`; | ||
|
||
while (this._currentTime <= (chart.finishTiming ? chart.finishTiming : 0)) { | ||
if ( | ||
this._currentTimingChange < chart.timingChanges.length - 1 && | ||
Math.abs(chart.timingChanges[this._currentTimingChange + 1].time - this._currentTime) < 1.401298E-45 | ||
) { | ||
this._currentTimingChange++; | ||
|
||
if ( | ||
Math.abs( | ||
chart.timingChanges[this._currentTimingChange].tempo - | ||
chart.timingChanges[this._currentTimingChange - 1].tempo | ||
) > 1.401298E-45 | ||
) { | ||
writer += `(${chart.timingChanges[this._currentTimingChange].tempo})`; | ||
} | ||
|
||
if ( | ||
Math.abs( | ||
chart.timingChanges[this._currentTimingChange].subdivisions - | ||
chart.timingChanges[this._currentTimingChange - 1].subdivisions | ||
) > 1.401298E-45 | ||
) { | ||
writer += `{${chart.timingChanges[this._currentTimingChange].subdivisions}}`; | ||
} | ||
} | ||
|
||
if ( | ||
this._currentNoteCollection < chart.noteCollections.length && | ||
Math.abs(chart.noteCollections[this._currentNoteCollection].time - this._currentTime) <= 1.401298E-45 | ||
) { | ||
writer += Serializer.serializeNoteCollection(chart.noteCollections[this._currentNoteCollection]); | ||
|
||
this._currentNoteCollection++; | ||
} | ||
|
||
const timingChange = chart.timingChanges[this._currentTimingChange]; | ||
this._currentTime += timingChange.secondsPerBeat; | ||
writer += ","; | ||
} | ||
writer += "E"; | ||
|
||
return writer; | ||
} | ||
|
||
private static serializeNoteCollection(notes: NoteCollection): string { | ||
let writer = ""; | ||
|
||
const seperator = notes.eachStyle === EachStyle.ForceBroken ? "`" : "/"; | ||
|
||
if (notes.eachStyle === EachStyle.ForceEach) writer += "0/"; | ||
|
||
for (let i = 0; i < notes.length; i++) { | ||
writer += notes[i].write(); | ||
|
||
if (i != notes.length - 1) writer += seperator; | ||
} | ||
|
||
return writer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { TimingChange } from "../internal/syntacticAnalysis/timingChange"; | ||
import { NoteCollection } from "./noteCollection"; | ||
|
||
export class MaiChart { | ||
public finishTiming: number | undefined; | ||
public noteCollections: NoteCollection[] = []; | ||
public timingChanges: TimingChange[] = []; | ||
} |
Oops, something went wrong.