Skip to content

Commit bda683c

Browse files
committed
Fix the remaining bugs
1 parent 86d963a commit bda683c

File tree

5 files changed

+135
-95
lines changed

5 files changed

+135
-95
lines changed

shared/Animation/Track.h

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,42 +72,55 @@ struct TimeUnit {
7272
};
7373

7474
struct PropertyW {
75-
Tracks::ffi::ValueProperty const* property;
75+
Tracks::ffi::ValueProperty* property;
7676

7777
constexpr PropertyW() = default;
78-
constexpr PropertyW(Tracks::ffi::ValueProperty const* property) : property(property) {}
78+
constexpr PropertyW(Tracks::ffi::ValueProperty* property) : property(property) {}
7979

8080
operator Tracks::ffi::ValueProperty const*() const {
8181
return property;
8282
}
83+
operator Tracks::ffi::ValueProperty*() const {
84+
return property;
85+
}
8386
operator bool() const {
8487
return property != nullptr;
8588
}
8689

8790
[[nodiscard]] Tracks::ffi::WrapBaseValueType GetType() const {
91+
CRASH_UNLESS(property);
92+
8893
return Tracks::ffi::property_get_type(property);
8994
}
9095
[[nodiscard]] Tracks::ffi::CValueProperty GetValue() const {
96+
CRASH_UNLESS(property);
9197
return Tracks::ffi::property_get_value(property);
9298
}
9399

94100
[[nodiscard]] TimeUnit GetTime() const {
101+
CRASH_UNLESS(property);
102+
95103
return Tracks::ffi::property_get_last_updated(property);
96104
}
97105

106+
constexpr bool hasUpdated(Tracks::ffi::CValueProperty value, TimeUnit lastCheckedTime = {}) const {
107+
return lastCheckedTime == TimeUnit() || TimeUnit(value.last_updated) >= lastCheckedTime;
108+
}
109+
98110
[[nodiscard]] std::optional<NEVector::Quaternion> GetQuat(TimeUnit lastCheckedTime = {}) const {
99111
auto value = GetValue();
100-
if (!value.value.has_value) return std::nullopt;
101-
if (TimeUnit(value.last_updated) <= lastCheckedTime) return std::nullopt;
102-
if (value.value.value.ty != Tracks::ffi::WrapBaseValueType::Quat) return std::nullopt;
103-
112+
if (!value.value.has_value) {return std::nullopt;}
113+
if (!hasUpdated(value, lastCheckedTime)) {return std::nullopt;}
114+
if (value.value.value.ty != Tracks::ffi::WrapBaseValueType::Quat) {
115+
return std::nullopt;
116+
}
104117
auto v = value.value.value.value;
105118
return NEVector::Quaternion{ v.quat.x, v.quat.y, v.quat.z, v.quat.w };
106119
}
107120
[[nodiscard]] std::optional<NEVector::Vector3> GetVec3(TimeUnit lastCheckedTime = {}) const {
108121
auto value = GetValue();
109122
if (!value.value.has_value) return std::nullopt;
110-
if (TimeUnit(value.last_updated) <= lastCheckedTime) return std::nullopt;
123+
if (!hasUpdated(value, lastCheckedTime)) return std::nullopt;
111124
if (value.value.value.ty != Tracks::ffi::WrapBaseValueType::Vec3) return std::nullopt;
112125

113126
auto v = value.value.value.value;
@@ -116,7 +129,7 @@ struct PropertyW {
116129
[[nodiscard]] std::optional<NEVector::Vector4> GetVec4(TimeUnit lastCheckedTime = {}) const {
117130
auto value = GetValue();
118131
if (!value.value.has_value) return std::nullopt;
119-
if (TimeUnit(value.last_updated) <= lastCheckedTime) return std::nullopt;
132+
if (!hasUpdated(value, lastCheckedTime)) return std::nullopt;
120133
if (value.value.value.ty != Tracks::ffi::WrapBaseValueType::Vec4) return std::nullopt;
121134
auto v = value.value.value.value;
122135

@@ -125,7 +138,7 @@ struct PropertyW {
125138
[[nodiscard]] std::optional<float> GetFloat(TimeUnit lastCheckedTime = {}) const {
126139
auto value = GetValue();
127140
if (!value.value.has_value) return std::nullopt;
128-
if (TimeUnit(value.last_updated) <= lastCheckedTime) return std::nullopt;
141+
if (!hasUpdated(value, lastCheckedTime)) return std::nullopt;
129142
if (value.value.value.ty != Tracks::ffi::WrapBaseValueType::Float) return std::nullopt;
130143

131144
return value.value.value.value.float_v;
@@ -272,10 +285,24 @@ struct TrackW {
272285
return Tracks::ffi::track_get_path_properties_map(track);
273286
}
274287

275-
[[nodiscard]] std::string GetName() const {
288+
/**
289+
* @brief Get the Name object
290+
*
291+
* @return std::string_view Return a string view as the original string is leaked from the FFI.
292+
*/
293+
[[nodiscard]] std::string_view GetName() const {
276294
return Tracks::ffi::track_get_name(track);
277295
}
278296

297+
/**
298+
* @brief Set the Name object
299+
*
300+
* @param name The name to set
301+
*/
302+
void SetName(std::string_view name) const {
303+
Tracks::ffi::track_set_name(track, name.data());
304+
}
305+
279306
[[nodiscard]] std::span<UnityEngine::GameObject* const> GetGameObjects() const {
280307
static_assert(sizeof(UnityEngine::GameObject*) == sizeof(Tracks::ffi::GameObject),
281308
"Tracks wrapper and GameObject pointer do not match size!");

shared/AssociatedData.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ inline static constexpr std::string_view const LEFT_HANDED_ID = "leftHanded";
8080

8181
using TracksVector = sbo::small_vector<TrackW, 1>;
8282

83-
enum class EventType { animateTrack, assignPathAnimation, unknown };
83+
enum class EventType { unknown, animateTrack, assignPathAnimation };
8484

8585
class TracksContext {
8686
public:
@@ -192,6 +192,7 @@ class BeatmapAssociatedData {
192192

193193
auto freeTrack = Tracks::ffi::track_create();
194194
auto ownedTrack = internal_tracks_context->AddTrack(TrackW(freeTrack, v2));
195+
ownedTrack.SetName(name);
195196
tracks.emplace(name, ownedTrack);
196197

197198
return ownedTrack;

shared/bindings.h

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ typedef uint32_t PropertyNames;
9393
#endif // __cplusplus
9494

9595
typedef enum WrapBaseValueType {
96+
Unknown = -1,
9697
Vec3 = 0,
9798
Quat = 1,
9899
Vec4 = 2,
@@ -131,11 +132,11 @@ typedef union CEventTypeData {
131132
/**
132133
* AnimateTrack(ValueProperty)
133134
*/
134-
const struct ValueProperty *property;
135+
struct ValueProperty *property;
135136
/**
136137
* AssignPathAnimation(PathProperty)
137138
*/
138-
const PathProperty *path_property;
139+
PathProperty *path_property;
139140
} CEventTypeData;
140141

141142
typedef struct CEventType {
@@ -248,20 +249,20 @@ typedef struct GameObject {
248249
} GameObject;
249250

250251
typedef struct CPropertiesMap {
251-
const struct ValueProperty *position;
252-
const struct ValueProperty *rotation;
253-
const struct ValueProperty *scale;
254-
const struct ValueProperty *local_rotation;
255-
const struct ValueProperty *local_position;
256-
const struct ValueProperty *dissolve;
257-
const struct ValueProperty *dissolve_arrow;
258-
const struct ValueProperty *time;
259-
const struct ValueProperty *cuttable;
260-
const struct ValueProperty *color;
261-
const struct ValueProperty *attentuation;
262-
const struct ValueProperty *fog_offset;
263-
const struct ValueProperty *height_fog_start_y;
264-
const struct ValueProperty *height_fog_height;
252+
struct ValueProperty *position;
253+
struct ValueProperty *rotation;
254+
struct ValueProperty *scale;
255+
struct ValueProperty *local_rotation;
256+
struct ValueProperty *local_position;
257+
struct ValueProperty *dissolve;
258+
struct ValueProperty *dissolve_arrow;
259+
struct ValueProperty *time;
260+
struct ValueProperty *cuttable;
261+
struct ValueProperty *color;
262+
struct ValueProperty *attentuation;
263+
struct ValueProperty *fog_offset;
264+
struct ValueProperty *height_fog_start_y;
265+
struct ValueProperty *height_fog_height;
265266
} CPropertiesMap;
266267

267268
typedef struct CPathPropertiesMap {
@@ -507,6 +508,10 @@ void track_destroy(struct Track *track);
507508

508509
void track_set_name(struct Track *track, const char *name);
509510

511+
/**
512+
* Returns the name of the track as a C string.
513+
* This leaks memory
514+
*/
510515
const char *track_get_name(const struct Track *track);
511516

512517
void track_register_game_object(struct Track *track, struct GameObject game_object);
@@ -517,17 +522,17 @@ const struct GameObject *track_get_game_objects(const struct Track *track, uintp
517522

518523
void track_register_property(struct Track *track, const char *id, struct ValueProperty *property);
519524

520-
const struct ValueProperty *track_get_property(const struct Track *track, const char *id);
525+
struct ValueProperty *track_get_property(struct Track *track, const char *id);
521526

522-
const struct ValueProperty *track_get_property_by_name(const struct Track *track, PropertyNames id);
527+
struct ValueProperty *track_get_property_by_name(struct Track *track, PropertyNames id);
523528

524529
PathProperty *track_get_path_property_by_name(struct Track *track, PropertyNames id);
525530

526531
void track_register_path_property(struct Track *track, const char *id, PathProperty *property);
527532

528533
PathProperty *track_get_path_property(struct Track *track, const char *id);
529534

530-
struct CPropertiesMap track_get_properties_map(const struct Track *track);
535+
struct CPropertiesMap track_get_properties_map(struct Track *track);
531536

532537
struct CPathPropertiesMap track_get_path_properties_map(struct Track *track);
533538

src/Animation/Events.cpp

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ void Events::UpdateCoroutines(BeatmapCallbacksController* callbackController) {
5151
}
5252

5353
[[nodiscard]]
54-
Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData const& eventAD,
55-
TrackW track,
54+
Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData const& eventAD, TrackW track,
5655
PathPropertyW pathProperty, PointDefinitionW pointData) {
5756
auto eventType = Tracks::ffi::CEventType {
5857
.ty = Tracks::ffi::CEventTypeEnum::AssignPathAnimation,
@@ -75,8 +74,8 @@ Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData con
7574
return eventData;
7675
}
7776
[[nodiscard]]
78-
Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData const& eventAD,
79-
TrackW const& track, PropertyW const& property, PointDefinitionW const& pointData) {
77+
Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData const& eventAD, TrackW const& track,
78+
PropertyW const& property, PointDefinitionW const& pointData) {
8079
auto eventType = Tracks::ffi::CEventType {
8180
.ty = Tracks::ffi::CEventTypeEnum::AnimateTrack,
8281
.data = {
@@ -94,95 +93,103 @@ Tracks::ffi::EventData* makeEvent(float eventTime, CustomEventAssociatedData con
9493
.point_data_ptr = pointData,
9594
};
9695

96+
CRASH_UNLESS(pointData);
9797
auto eventData = Tracks::ffi::event_data_to_rust(&cEventData);
98+
CRASH_UNLESS(eventData);
9899

99100
return eventData;
100101
}
101102
void CustomEventCallback(BeatmapCallbacksController* callbackController,
102103
CustomJSONData::CustomEventData* customEventData) {
103-
PAPER_IL2CPP_CATCH_HANDLER(
104-
bool isType = false;
105104

106-
auto typeHash = customEventData->typeHash;
105+
bool isType = false;
106+
107+
auto typeHash = customEventData->typeHash;
107108

108109
#define TYPE_GET(jsonName, varName) \
109110
static auto jsonNameHash_##varName = std::hash<std::string_view>()(jsonName); \
110-
if (!isType && typeHash == (jsonNameHash_##varName)) isType = true;
111+
if (!isType && typeHash == (jsonNameHash_##varName)) isType = true
111112

112-
TYPE_GET("AnimateTrack", AnimateTrack) TYPE_GET("AssignPathAnimation", AssignPathAnimation)
113+
TYPE_GET("AnimateTrack", AnimateTrack);
114+
TYPE_GET("AssignPathAnimation", AssignPathAnimation);
113115

114-
if (!isType) { return; }
116+
if (!isType) {
117+
return;
118+
}
115119

116-
CustomEventAssociatedData const& eventAD = getEventAD(customEventData);
120+
CustomEventAssociatedData const& eventAD = getEventAD(customEventData);
117121

118-
auto* customBeatmapData = il2cpp_utils::cast<CustomJSONData::CustomBeatmapData>(callbackController->_beatmapData);
119-
TracksAD::BeatmapAssociatedData& beatmapAD = TracksAD::getBeatmapAD(customBeatmapData->customData);
122+
auto* customBeatmapData = il2cpp_utils::cast<CustomJSONData::CustomBeatmapData>(callbackController->_beatmapData);
123+
TracksAD::BeatmapAssociatedData& beatmapAD = TracksAD::getBeatmapAD(customBeatmapData->customData);
120124

121-
// fail safe, idek why this needs to be done smh
122-
// CJD you bugger
123-
if (!eventAD.parsed) {
124-
TLogger::Logger.debug("callbackController {}", fmt::ptr(callbackController));
125-
TLogger::Logger.debug("_beatmapData {}", fmt::ptr(callbackController->_beatmapData));
126-
TLogger::Logger.debug("customBeatmapData {}", fmt::ptr(customBeatmapData));
125+
// fail safe, idek why this needs to be done smh
126+
// CJD you bugger
127+
if (!eventAD.parsed) {
128+
TLogger::Logger.debug("callbackController {}", fmt::ptr(callbackController));
129+
TLogger::Logger.debug("_beatmapData {}", fmt::ptr(callbackController->_beatmapData));
130+
TLogger::Logger.debug("customBeatmapData {}", fmt::ptr(customBeatmapData));
127131

128-
if (!beatmapAD.valid) {
129-
TLogger::Logger.debug("Beatmap wasn't parsed when event is invoked, what?");
130-
TracksAD::readBeatmapDataAD(customBeatmapData);
131-
}
132+
if (!beatmapAD.valid) {
133+
TLogger::Logger.debug("Beatmap wasn't parsed when event is invoked, what?");
134+
TracksAD::readBeatmapDataAD(customBeatmapData);
135+
}
132136

133-
LoadTrackEvent(customEventData, beatmapAD, customBeatmapData->v2orEarlier);
134-
}
137+
LoadTrackEvent(customEventData, beatmapAD, customBeatmapData->v2orEarlier);
138+
}
135139

136-
auto duration = eventAD.duration;
140+
auto duration = eventAD.duration;
137141

138-
if (!TracksStatic::bpmController) {
139-
CJDLogger::Logger.fmtLog<Paper::LogLevel::ERR>("BPM CONTROLLER NOT INITIALIZED");
140-
}
142+
if (!TracksStatic::bpmController) {
143+
CJDLogger::Logger.fmtLog<Paper::LogLevel::ERR>("BPM CONTROLLER NOT INITIALIZED");
144+
}
141145

142-
auto bpm = TracksStatic::bpmController->currentBpm; // spawnController->get_currentBpm()
146+
auto bpm = TracksStatic::bpmController->currentBpm; // spawnController->get_currentBpm()
143147

144-
duration = 60.0f * duration / bpm;
148+
duration = 60.0f * duration / bpm;
145149

146-
auto easing = eventAD.easing; auto repeat = eventAD.repeat;
150+
auto easing = eventAD.easing;
151+
auto repeat = eventAD.repeat;
147152

148-
bool noDuration = duration == 0 || customEventData->time + (duration * (repeat + 1)) <
149-
TracksStatic::bpmController->_beatmapCallbacksController->songTime;
153+
bool noDuration = duration == 0 || customEventData->time + (duration * (repeat + 1)) <
154+
TracksStatic::bpmController->_beatmapCallbacksController->songTime;
150155

151-
auto tracksContext = beatmapAD.internal_tracks_context;
156+
auto tracksContext = beatmapAD.internal_tracks_context;
152157

153-
auto coroutineManager = tracksContext->GetCoroutineManager();
154-
auto baseManager = tracksContext->GetBaseProviderContext();
155-
auto eventTime = customEventData->time;
156-
auto songTime = callbackController->_songTime;
158+
auto coroutineManager = tracksContext->GetCoroutineManager();
159+
auto baseManager = tracksContext->GetBaseProviderContext();
160+
auto eventTime = customEventData->time;
161+
auto songTime = callbackController->_songTime;
157162

158-
for (auto const& track
159-
: eventAD.tracks) {
160-
switch (eventAD.type) {
161-
case EventType::animateTrack: {
162-
for (auto const& animateTrackData : eventAD.animateTrackData) {
163-
for (auto const& [property, pointData] : animateTrackData.properties) {
164163

165-
auto event = makeEvent(eventTime, eventAD, track, property, pointData);
166-
Tracks::ffi::start_event_coroutine(coroutineManager, bpm, songTime, baseManager, event);
167-
}
168-
}
169-
break;
170-
}
171-
case EventType::assignPathAnimation: {
172-
for (auto const& assignPathAnimationData : eventAD.assignPathAnimation) {
173-
for (auto const& [pathProperty, pointData] : assignPathAnimationData.pathProperties) {
174-
auto event = makeEvent(eventTime, eventAD, track, pathProperty, pointData);
175-
Tracks::ffi::start_event_coroutine(coroutineManager, bpm, songTime, baseManager, event);
176-
}
177-
}
178-
break;
164+
for (auto const& track : eventAD.tracks) {
165+
166+
switch (eventAD.type) {
167+
case EventType::animateTrack: {
168+
for (auto const& animateTrackData : eventAD.animateTrackData) {
169+
170+
TLogger::Logger.fmtLog<Paper::LogLevel::INF>("Processing event for track {} with duration {} at time {} properties {}",
171+
track.GetName(), duration, eventTime, animateTrackData.properties.size());
172+
173+
for (auto const& [property, pointData] : animateTrackData.properties) {
174+
auto event = makeEvent(eventTime, eventAD, track, property, pointData);
175+
Tracks::ffi::start_event_coroutine(coroutineManager, bpm, songTime, baseManager, event);
179176
}
180-
default:
181-
break;
177+
}
178+
break;
179+
}
180+
case EventType::assignPathAnimation: {
181+
for (auto const& assignPathAnimationData : eventAD.assignPathAnimation) {
182+
for (auto const& [pathProperty, pointData] : assignPathAnimationData.pathProperties) {
183+
auto event = makeEvent(eventTime, eventAD, track, pathProperty, pointData);
184+
Tracks::ffi::start_event_coroutine(coroutineManager, bpm, songTime, baseManager, event);
182185
}
183186
}
184-
185-
)
187+
break;
188+
}
189+
default:
190+
break;
191+
}
192+
}
186193
}
187194

188195
void Events::AddEventCallbacks() {

0 commit comments

Comments
 (0)