-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmod.rs
More file actions
997 lines (885 loc) · 32.3 KB
/
mod.rs
File metadata and controls
997 lines (885 loc) · 32.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
//! The run module provides everything necessary for working with a `Run`, like parsing and saving or editing them.
//!
//! # Examples
//!
//! ```
//! use livesplit_core::run::{Run, Segment};
//!
//! let mut run = Run::new();
//!
//! run.set_game_name("Super Mario Odyssey");
//! run.set_category_name("Darker Side");
//!
//! run.push_segment(Segment::new("Cap Kingdom"));
//! run.push_segment(Segment::new("Cascade Kingdom"));
//! ```
mod attempt;
#[cfg(feature = "auto-splitting")]
mod auto_splitter_settings;
mod comparisons;
pub mod editor;
mod linked_layout;
pub mod parser;
mod run_metadata;
pub mod saver;
mod segment;
mod segment_history;
#[cfg(test)]
mod tests;
pub use attempt::Attempt;
pub use comparisons::Comparisons;
pub use editor::{Editor, RenameError};
pub use linked_layout::LinkedLayout;
pub use run_metadata::{CustomVariable, RunMetadata};
pub use segment::Segment;
pub use segment_history::SegmentHistory;
#[cfg(feature = "auto-splitting")]
use crate::run::auto_splitter_settings::AutoSplitterSettings;
use crate::{
AtomicDateTime, Time, TimeSpan, TimingMethod,
comparison::{ComparisonGenerator, RACE_COMPARISON_PREFIX, default_generators, personal_best},
platform::prelude::*,
settings::Image,
util::{PopulateString, caseless::matches_ascii_key},
};
use alloc::borrow::Cow;
use core::{cmp::max, fmt};
use hashbrown::HashSet;
/// A Run stores the split times for a specific game and category of a runner.
///
/// # Examples
///
/// ```
/// use livesplit_core::{Run, Segment};
///
/// let mut run = Run::new();
///
/// run.set_game_name("Super Mario Odyssey");
/// run.set_category_name("Darker Side");
///
/// run.push_segment(Segment::new("Cap Kingdom"));
/// run.push_segment(Segment::new("Cascade Kingdom"));
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct Run {
game_icon: Image,
game_name: String,
category_name: String,
offset: TimeSpan,
attempt_count: u32,
attempt_history: Vec<Attempt>,
metadata: RunMetadata,
has_been_modified: bool,
segments: Vec<Segment>,
custom_comparisons: Vec<String>,
comparison_generators: ComparisonGenerators,
auto_splitter_settings: String,
#[cfg(feature = "auto-splitting")]
parsed_auto_splitter_settings: Option<AutoSplitterSettings>,
linked_layout: Option<LinkedLayout>,
}
#[derive(Clone, Debug)]
struct ComparisonGenerators(Vec<Box<dyn ComparisonGenerator>>);
impl PartialEq for ComparisonGenerators {
fn eq(&self, other: &ComparisonGenerators) -> bool {
self.0
.iter()
.map(|c| c.name())
.eq(other.0.iter().map(|c| c.name()))
}
}
/// Error type for an invalid comparison name to be added.
#[derive(PartialEq, Eq, Debug, snafu::Snafu)]
pub enum AddComparisonError {
/// Comparison name starts with "\[Race\]".
NameStartsWithRace,
/// Comparison name is a duplicate.
DuplicateName,
}
/// Error type for copying a comparison.
#[derive(PartialEq, Eq, Debug, snafu::Snafu)]
pub enum CopyComparisonError {
/// There is no comparison with the provided name to copy.
NoSuchComparison,
/// The new comparison could not be added.
AddComparison {
/// The underlying error.
source: AddComparisonError,
},
}
impl Run {
/// Creates a new Run object with no segments.
#[inline]
pub fn new() -> Self {
Self {
game_icon: Image::default(),
game_name: String::new(),
category_name: String::new(),
offset: TimeSpan::zero(),
attempt_count: 0,
attempt_history: Vec::new(),
metadata: RunMetadata::new(),
has_been_modified: false,
segments: Vec::new(),
custom_comparisons: vec![personal_best::NAME.to_string()],
comparison_generators: ComparisonGenerators(default_generators()),
auto_splitter_settings: String::new(),
#[cfg(feature = "auto-splitting")]
parsed_auto_splitter_settings: None,
linked_layout: None,
}
}
/// Accesses the name of the game this Run is for.
#[inline]
pub fn game_name(&self) -> &str {
&self.game_name
}
/// Sets the name of the game this Run is for.
#[inline]
pub fn set_game_name<S>(&mut self, name: S)
where
S: PopulateString,
{
name.populate(&mut self.game_name);
}
/// Accesses the game's icon.
#[inline]
pub const fn game_icon(&self) -> &Image {
&self.game_icon
}
/// Sets the game's icon.
#[inline]
pub fn set_game_icon(&mut self, image: Image) {
self.game_icon = image;
}
/// Accesses the name of the category this Run is for.
#[inline]
pub fn category_name(&self) -> &str {
&self.category_name
}
/// Sets the name of the category this Run is for.
#[inline]
pub fn set_category_name<S>(&mut self, name: S)
where
S: PopulateString,
{
name.populate(&mut self.category_name);
}
/// Returns the amount of runs that have been attempted with these splits.
#[inline]
pub const fn attempt_count(&self) -> u32 {
self.attempt_count
}
/// Sets the amount of runs that have been attempted with these splits.
#[inline]
pub const fn set_attempt_count(&mut self, attempts: u32) {
self.attempt_count = attempts;
}
/// Accesses additional metadata of this Run, like the platform and region
/// of the game.
#[inline]
pub const fn metadata(&self) -> &RunMetadata {
&self.metadata
}
/// Grants mutable access to the additional metadata of this Run, like the
/// platform and region of the game.
#[inline]
pub const fn metadata_mut(&mut self) -> &mut RunMetadata {
&mut self.metadata
}
/// Sets the time an attempt of this Run should start at.
#[inline]
pub const fn set_offset(&mut self, offset: TimeSpan) {
self.offset = offset;
}
/// Accesses the time an attempt of this Run should start at.
#[inline]
pub const fn offset(&self) -> TimeSpan {
self.offset
}
/// Marks a Run that a new Attempt has started. If you use it with a Timer,
/// this is done automatically.
pub const fn start_next_run(&mut self) {
self.attempt_count += 1;
self.has_been_modified = true;
}
/// Accesses the Segments of this Run object.
#[inline]
pub fn segments(&self) -> &[Segment] {
&self.segments
}
/// Grants mutable access to the Segments of this Run object.
#[inline]
pub const fn segments_mut(&mut self) -> &mut Vec<Segment> {
&mut self.segments
}
/// Pushes the segment provided to the end of the list of segments of this Run.
#[inline]
pub fn push_segment(&mut self, segment: Segment) {
self.segments.push(segment);
}
/// Accesses a certain segment of this Run.
///
/// # Panics
///
/// Panics if the index is out of bounds.
#[inline]
pub fn segment(&self, index: usize) -> &Segment {
&self.segments[index]
}
/// Mutably accesses a certain segment of this Run.
///
/// # Panics
///
/// Panics if the index is out of bounds.
#[inline]
pub fn segment_mut(&mut self, index: usize) -> &mut Segment {
&mut self.segments[index]
}
/// Accesses the history of all the runs that have been attempted. This does
/// not store the actual segment times, just the overall attempt
/// information. Information about the individual segments is stored within
/// each segment.
#[inline]
pub fn attempt_history(&self) -> &[Attempt] {
&self.attempt_history
}
/// Accesses the custom comparisons that are stored in this Run. This
/// includes `Personal Best` but excludes all the other Comparison
/// Generators.
#[inline]
pub fn custom_comparisons(&self) -> &[String] {
&self.custom_comparisons
}
/// Grants mutable access to the custom comparisons that are stored in this
/// Run. This includes `Personal Best` but excludes all the other
/// Comparison Generators.
///
/// # Warning
///
/// You may not delete the `Personal Best` comparison.
#[inline]
pub const fn custom_comparisons_mut(&mut self) -> &mut Vec<String> {
&mut self.custom_comparisons
}
/// Accesses an iterator that iterates over all the comparisons. This
/// includes both the custom comparisons defined by the user and the
/// Comparison Generators.
#[inline]
pub fn comparisons(&self) -> ComparisonsIter<'_> {
ComparisonsIter {
custom: &self.custom_comparisons,
generators: &self.comparison_generators.0,
}
}
/// Accesses the Comparison Generators in use by this Run.
#[inline]
pub fn comparison_generators(&self) -> &[Box<dyn ComparisonGenerator>] {
&self.comparison_generators.0
}
/// Grants mutable access to the Comparison Generators in use by this Run.
#[inline]
pub fn comparison_generators_mut(&mut self) -> &mut Vec<Box<dyn ComparisonGenerator>> {
&mut self.comparison_generators.0
}
/// Accesses the Auto Splitter Settings that are encoded as XML.
#[inline]
pub fn auto_splitter_settings(&self) -> &str {
&self.auto_splitter_settings
}
/// Grants mutable access to the XML encoded Auto Splitter Settings.
///
/// # Warning
///
/// You need to ensure that the Auto Splitter Settings are encoded as data
/// that would be valid as an interior of an XML element.
#[inline]
pub const fn auto_splitter_settings_mut(&mut self) -> &mut String {
&mut self.auto_splitter_settings
}
/// Loads a copy of the Auto Splitter Settings as a settings map.
#[inline]
#[cfg(feature = "auto-splitting")]
pub fn auto_splitter_settings_map_load(
&self,
) -> Option<livesplit_auto_splitting::settings::Map> {
if let Some(p) = &self.parsed_auto_splitter_settings {
return Some(p.custom_settings.clone());
}
None
}
/// Stores a settings map into the parsed auto splitter settings.
#[cfg(feature = "auto-splitting")]
pub fn auto_splitter_settings_map_store(
&mut self,
settings_map: livesplit_auto_splitting::settings::Map,
) {
let p = &mut self.parsed_auto_splitter_settings;
match p {
None => {
if settings_map.is_empty() {
return;
}
let mut a = AutoSplitterSettings::default();
a.set_custom_settings(settings_map);
*p = Some(a);
}
Some(a) => {
a.set_custom_settings(settings_map);
}
}
}
/// Accesses the [`LinkedLayout`] of this `Run`. If a
/// [`Layout`](crate::Layout) is linked, it is supposed to be loaded to
/// visualize the `Run`.
#[inline]
pub const fn linked_layout(&self) -> Option<&LinkedLayout> {
self.linked_layout.as_ref()
}
/// Notifies the `Run` that the currently loaded [`Layout`](crate::Layout)
/// has changed. If the `Run` has a [`LinkedLayout`], it will be updated
/// accordingly. Specify [`None`] if the default [`Layout`](crate::Layout)
/// should be linked. Returns [`true`] if the [`LinkedLayout`] has changed.
#[must_use]
#[inline]
pub fn layout_path_changed<S>(&mut self, path: Option<S>) -> bool
where
S: PopulateString,
{
if let Some(layout) = &mut self.linked_layout {
match (&mut *layout, path) {
(LinkedLayout::Default, Some(path)) => {
*layout = LinkedLayout::Path(path.into_string())
}
(LinkedLayout::Path(_), None) => *layout = LinkedLayout::Default,
(LinkedLayout::Path(old_path), Some(new_path)) => {
if old_path == new_path.as_str() {
return false;
}
new_path.populate(old_path);
}
(LinkedLayout::Default, None) => return false,
}
return true;
}
false
}
/// Sets the [`LinkedLayout`] of this `Run`. If a [`Layout`](crate::Layout)
/// is linked, it is supposed to be loaded to visualize the `Run`.
#[inline]
pub fn set_linked_layout(&mut self, linked_layout: Option<LinkedLayout>) {
self.linked_layout = linked_layout;
}
/// Returns the amount of segments stored in this Run.
#[inline]
pub const fn len(&self) -> usize {
self.segments.len()
}
/// Returns `true` if there's no segments stored in this Run.
#[inline]
pub const fn is_empty(&self) -> bool {
self.segments.is_empty()
}
/// Marks the Run as modified, so that it is known that there are changes
/// that should be saved.
#[inline]
pub const fn mark_as_modified(&mut self) {
self.has_been_modified = true;
}
/// Marks the Run as unmodified, so that it is known that all the changes
/// have been saved.
#[inline]
pub const fn mark_as_unmodified(&mut self) {
self.has_been_modified = false;
}
/// Returns whether the Run has been modified and should be saved so that
/// the changes don't get lost.
#[inline]
pub const fn has_been_modified(&self) -> bool {
self.has_been_modified
}
/// Adds a new Attempt to the Run's Attempt History. This is automatically
/// done if the Run is used with a Timer.
pub fn add_attempt(
&mut self,
time: Time,
started: Option<AtomicDateTime>,
ended: Option<AtomicDateTime>,
pause_time: Option<TimeSpan>,
) {
let index = self
.attempt_history
.iter()
.map(Attempt::index)
.max()
.unwrap_or(0);
let index = max(0, index + 1);
self.add_attempt_with_index(time, index, started, ended, pause_time);
}
/// Adds a new Attempt to the Run's Attempt History with a predetermined
/// History Index.
///
/// # Warning
///
/// This index may not overlap with an index that is already in the Attempt
/// History.
pub fn add_attempt_with_index(
&mut self,
time: Time,
index: i32,
started: Option<AtomicDateTime>,
ended: Option<AtomicDateTime>,
pause_time: Option<TimeSpan>,
) {
let attempt = Attempt::new(index, time, started, ended, pause_time);
self.attempt_history.push(attempt);
}
/// Clears the speedrun.com Run ID of this Run, as the current Run does not
/// reflect the run on speedrun.com anymore. This may be the case if a new
/// Personal Best is achieved for example.
#[inline]
pub fn clear_run_id(&mut self) {
self.metadata.set_run_id("");
}
/// Adds a new custom comparison. If a custom comparison with that name
/// already exists, it is not added.
#[inline]
pub fn add_custom_comparison<S>(&mut self, comparison: S) -> Result<(), AddComparisonError>
where
S: PopulateString,
{
self.validate_comparison_name(comparison.as_str())?;
self.custom_comparisons.push(comparison.into_string());
Ok(())
}
/// Recalculates all the comparison times the Comparison Generators provide.
#[inline]
pub fn regenerate_comparisons(&mut self) {
for generator in &mut self.comparison_generators.0 {
generator.generate(&mut self.segments, &self.attempt_history);
}
}
/// Returns a file name (without the extension) suitable for this Run that
/// is built the following way:
///
/// Game Name - Category Name
///
/// If either is empty, the dash is omitted. Special characters that cause
/// problems in file names are also omitted. If an extended category name is
/// used, the variables of the category are appended in a parenthesis.
pub fn extended_file_name(&self, use_extended_category_name: bool) -> String {
let mut extended_name = self.extended_name(use_extended_category_name).into_owned();
extended_name
.retain(|c| !matches!(c, '\\' | '/' | ':' | '*' | '?' | '"' | '<' | '>' | '|'));
extended_name
}
/// Returns a name suitable for this Run that is built the following way:
///
/// Game Name - Category Name
///
/// If either is empty, the dash is omitted. If an extended category name is
/// used, the variables of the category are appended in a parenthesis.
pub fn extended_name(&self, use_extended_category_name: bool) -> Cow<'_, str> {
let mut name = Cow::Borrowed(self.game_name());
let category_name = if use_extended_category_name {
Cow::Owned(self.extended_category_name(false, false, true).to_string())
} else {
Cow::Borrowed(self.category_name())
};
if !category_name.is_empty() {
if !name.is_empty() {
let name = name.to_mut();
name.push_str(" - ");
name.push_str(&category_name);
} else {
name = category_name;
}
}
name
}
/// Returns an extended category name that possibly includes the region,
/// platform and variables, depending on the arguments provided. The
/// returned object implements `Display` where it lazily formats the
/// extended category name. An extended category name may look like this:
///
/// Any% (No Tuner, JPN, Wii Emulator)
pub const fn extended_category_name(
&self,
show_region: bool,
show_platform: bool,
show_variables: bool,
) -> ExtendedCategoryName<'_> {
ExtendedCategoryName {
run: self,
show_region,
show_platform,
show_variables,
}
}
/// Returns the maximum index currently in use by the Attempt History. This
/// mostly serves as a helper function for the Timer.
pub fn max_attempt_history_index(&self) -> Option<i32> {
self.attempt_history().iter().map(Attempt::index).max()
}
/// Applies some fixing algorithms on the Run. This includes fixing the
/// comparison times and history, removing duplicates in the segment
/// histories and removing empty times.
pub fn fix_splits(&mut self) {
for method in TimingMethod::all() {
self.fix_comparison_times_and_history(method);
}
self.remove_duplicates();
self.remove_none_values();
self.reattach_unattached_segment_history_elements();
}
/// Clears out the Attempt History and the Segment Histories of all the segments.
pub fn clear_history(&mut self) {
self.attempt_history.clear();
for segment in &mut self.segments {
segment.segment_history_mut().clear();
}
}
/// Clears out the Attempt History, the Segment Histories, all the times,
/// sets the Attempt Count to 0 and clears the speedrun.com run id
/// association. All Custom Comparisons other than `Personal Best` are
/// deleted as well.
pub fn clear_times(&mut self) {
self.clear_history();
self.custom_comparisons.retain(|c| c == personal_best::NAME);
for segment in &mut self.segments {
segment.comparisons_mut().clear();
segment.set_best_segment_time(Time::default());
}
self.attempt_count = 0;
self.clear_run_id();
}
fn fix_comparison_times_and_history(&mut self, method: TimingMethod) {
// Remove negative Best Segment Times
for segment in &mut self.segments {
if segment.best_segment_time_mut()[method].is_some_and(|t| t < TimeSpan::zero()) {
segment.best_segment_time_mut()[method] = None;
}
}
for segment in &mut self.segments {
fix_history_from_none_best_segments(segment, method);
}
for comparison in &self.custom_comparisons {
let mut previous_time = TimeSpan::zero();
for segment in &mut self.segments {
if let Some(mut time) = segment.comparison_mut(comparison)[method] {
// Prevent comparison times from decreasing from one split to the next
if time < previous_time {
time = previous_time;
segment.comparison_mut(comparison)[method] = Some(time);
}
// Fix Best Segment time if the PB segment is faster
if comparison == personal_best::NAME {
let current_segment = time - previous_time;
if segment.best_segment_time()[method].is_none_or(|t| t > current_segment) {
segment.best_segment_time_mut()[method] = Some(current_segment);
}
}
previous_time = time;
}
}
}
for segment in &mut self.segments {
fix_history_from_best_segment_times(segment, method);
}
}
fn remove_none_values(&mut self) {
let mut cache = Vec::new();
if let Some(min_index) = self.min_segment_history_index() {
let max_index = self.max_attempt_history_index().unwrap_or(0) + 1;
for run_index in min_index..max_index {
for index in 0..self.len() {
if let Some(element) = self.segments[index].segment_history().get(run_index) {
if element.real_time.is_none() && element.game_time.is_none() {
cache.push(run_index);
} else {
cache.clear();
}
} else {
// Remove None times in history that aren't followed by a non-None time
self.remove_items_from_cache(index, &mut cache);
}
}
let len = self.len();
self.remove_items_from_cache(len, &mut cache);
}
}
}
fn remove_duplicates(&mut self) {
let mut rta_set = HashSet::new();
let mut igt_set = HashSet::new();
for segment in self.segments_mut() {
let history = segment.segment_history_mut();
rta_set.clear();
igt_set.clear();
for &(_, time) in history.iter_actual_runs() {
if let Some(time) = time.real_time {
rta_set.insert(time);
}
if let Some(time) = time.game_time {
igt_set.insert(time);
}
}
history.retain(|&(index, time)| {
if index >= 1 {
return true;
}
let (mut is_none, mut is_unique) = (true, false);
if let Some(time) = time.real_time {
is_unique |= rta_set.insert(time);
is_none = false;
}
if let Some(time) = time.game_time {
is_unique |= igt_set.insert(time);
is_none = false;
}
is_none || is_unique
});
}
}
fn remove_items_from_cache(&mut self, index: usize, cache: &mut Vec<i32>) {
let ind = index - cache.len();
for (index, segment) in cache.drain(..).zip(self.segments_mut()[ind..].iter_mut()) {
segment.segment_history_mut().remove(index);
}
}
/// Returns the minimum index in use by all the Segment Histories. `None` is
/// returned if the Run has no segments.
pub fn min_segment_history_index(&self) -> Option<i32> {
self.segments
.iter()
.map(|s| s.segment_history().min_index())
.min()
}
/// Fixes the Segment History by calculating the segment times from the
/// Personal Best times and adding those to the Segment History.
pub fn import_pb_into_segment_history(&mut self) {
if let Some(mut index) = self.min_segment_history_index() {
for timing_method in TimingMethod::all() {
index -= 1;
let mut prev_time = TimeSpan::zero();
for segment in self.segments_mut() {
// Import the PB splits into the history
let pb_time = segment.personal_best_split_time()[timing_method];
let time = Time::new()
.with_timing_method(timing_method, pb_time.map(|p| p - prev_time));
segment.segment_history_mut().insert(index, time);
if let Some(time) = pb_time {
prev_time = time;
}
}
}
}
}
/// Fixes a segment's Segment History by adding its Best Segment Time to its
/// Segment History.
///
/// # Panics
///
/// This panics if the segment index provided is out of bounds.
pub fn import_best_segment(&mut self, segment_index: usize) {
let best_segment_time = self.segments[segment_index].best_segment_time();
if best_segment_time.real_time.is_some() || best_segment_time.game_time.is_some() {
// We can unwrap here because due to the fact that we can access the
// best_segment_time of some segment, at least one exists.
let index = self.min_segment_history_index().unwrap() - 1;
self.segments[segment_index]
.segment_history_mut()
.insert(index, best_segment_time);
}
}
/// Updates the Segment History by adding the split times of the most recent
/// attempt up to the provided current split index to the Segment History.
///
/// # Panics
///
/// This panics if there is no attempt in the Attempt History.
pub fn update_segment_history(&mut self, segments_count: usize) {
let mut previous_split_time = Time::zero();
let segments = &mut self.segments[..segments_count];
let index = self
.attempt_history
.last()
.expect("There is no attempt in the Attempt History.")
.index();
for segment in segments {
let split_time = segment.split_time();
let segment_time = split_time - previous_split_time;
segment.segment_history_mut().insert(index, segment_time);
if let Some(time) = split_time.real_time {
previous_split_time.real_time = Some(time);
}
if let Some(time) = split_time.game_time {
previous_split_time.game_time = Some(time);
}
}
}
/// Checks a given name against the current comparisons in the Run to
/// ensure that it is valid for use.
pub fn validate_comparison_name(&self, new: &str) -> Result<(), AddComparisonError> {
if new.starts_with(RACE_COMPARISON_PREFIX) {
Err(AddComparisonError::NameStartsWithRace)
} else if self.comparisons().any(|c| c == new) {
Err(AddComparisonError::DuplicateName)
} else {
Ok(())
}
}
fn reattach_unattached_segment_history_elements(&mut self) {
let max_id = self.max_attempt_history_index().unwrap_or_default();
let mut min_id = self.min_segment_history_index().unwrap_or_default();
while let Some(unattached_id) = self
.segments
.iter()
.filter_map(|s| s.segment_history().try_get_max_index())
.filter(|&i| i > max_id)
.max()
{
let reassign_id = min_id - 1;
for segment in self.segments_mut() {
let history = segment.segment_history_mut();
if let Some(time) = history.remove(unattached_id) {
history.insert(reassign_id, time);
}
}
min_id = reassign_id;
}
}
}
impl Default for Run {
fn default() -> Self {
Run::new()
}
}
fn fix_history_from_none_best_segments(segment: &mut Segment, method: TimingMethod) {
// Only do anything if the Best Segment Time is gone for the Segment in question
if segment.best_segment_time()[method].is_none() {
// Keep only the skipped segments
segment
.segment_history_mut()
.retain(|&(_, time)| time[method].is_none());
}
}
fn fix_history_from_best_segment_times(segment: &mut Segment, method: TimingMethod) {
if let Some(best_segment) = segment.best_segment_time()[method] {
for (_, time) in segment.segment_history_mut().iter_mut() {
// Make sure no times in the history are lower than the Best Segment
if let Some(time) = &mut time[method]
&& *time < best_segment
{
*time = best_segment;
}
}
}
}
/// Iterator that iterates over all the comparisons. This includes both the
/// custom comparisons defined by the user and the Comparison Generators.
pub struct ComparisonsIter<'a> {
custom: &'a [String],
generators: &'a [Box<dyn ComparisonGenerator>],
}
impl<'a> Iterator for ComparisonsIter<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<&'a str> {
if let Some((a, b)) = self.custom.split_first() {
self.custom = b;
Some(a)
} else if let Some((a, b)) = self.generators.split_first() {
self.generators = b;
Some(a.name())
} else {
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.custom.len() + self.generators.len();
(len, Some(len))
}
}
impl ExactSizeIterator for ComparisonsIter<'_> {}
/// Lazily formats an extended category name via the `Display` trait. It's a
/// category name that possibly includes the region, platform and variables,
/// depending on the arguments provided. An extended category name may look like
/// this:
///
/// Any% (No Tuner, JPN, Wii Emulator)
pub struct ExtendedCategoryName<'run> {
run: &'run Run,
show_region: bool,
show_platform: bool,
show_variables: bool,
}
impl fmt::Display for ExtendedCategoryName<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let category_name = self.run.category_name.as_str();
if category_name.is_empty() {
return Ok(());
}
let mut is_empty = true;
let mut has_pushed = false;
let (before, after_parenthesis) = if let Some((_, rem)) = category_name.split_once('(')
&& let Some((between, after)) = rem.split_once(')')
{
is_empty = between.is_empty();
// Split before the closing parenthesis so we can insert stuff
// before it.
category_name.split_at(category_name.len() - after.len() - 1)
} else {
(category_name, "")
};
f.write_str(before)?;
let mut push = |values: &[&str]| {
if is_empty {
if !has_pushed {
f.write_str(" (")?;
}
is_empty = false;
} else {
f.write_str(", ")?;
}
for value in values {
f.write_str(value)?;
}
has_pushed = true;
Ok(())
};
if self.show_variables {
for (name, value) in self.run.metadata.speedrun_com_variables() {
let name = name.trim_end_matches('?');
if matches_ascii_key("yes", value) {
push(&[name])?;
} else if matches_ascii_key("no", value) {
push(&["No ", value])?;
} else {
push(&[value])?;
}
}
}
if self.show_region {
let region = self.run.metadata.region_name();
if !region.is_empty() {
push(&[region])?;
}
}
if self.show_platform {
let platform = self.run.metadata.platform_name();
let uses_emulator = self.run.metadata.uses_emulator();
match (!platform.is_empty(), uses_emulator) {
(true, true) => push(&[platform, " Emulator"])?,
(true, false) => push(&[platform])?,
(false, true) => push(&["Emulator"])?,
_ => (),
}
}
if !after_parenthesis.is_empty() {
f.write_str(after_parenthesis)?;
} else if !is_empty {
f.write_str(")")?;
}
Ok(())
}
}