blob: 4e0d2cb77a0f673a4cb11be2352d407566018223 (
plain)
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
|
namespace Pathfinding.Serialization {
/// <summary>
/// Helper struct for handling serialization backwards compatibility.
///
/// It stores which migrations have been completed as a bitfield.
/// </summary>
public struct Migrations {
/// <summary>Bitfield of all migrations that have been run</summary>
internal int finishedMigrations;
/// <summary>
/// Bitfield of all migrations that the component supports.
/// A newly created component will be initialized with this value.
/// </summary>
internal int allMigrations;
internal bool ignore;
/// <summary>A special migration flag which is used to mark that the version has been migrated to the bitfield format, from the legacy linear version format</summary>
const int MIGRATE_TO_BITFIELD = 1 << 30;
public bool IsLegacyFormat => (finishedMigrations & MIGRATE_TO_BITFIELD) == 0;
public int LegacyVersion => finishedMigrations;
public Migrations(int value) {
this.finishedMigrations = value;
allMigrations = MIGRATE_TO_BITFIELD;
ignore = false;
}
public bool TryMigrateFromLegacyFormat (out int legacyVersion) {
legacyVersion = finishedMigrations;
if (IsLegacyFormat) {
this = new Migrations(MIGRATE_TO_BITFIELD);
return true;
} else return false;
}
public void MarkMigrationFinished (int flag) {
if (IsLegacyFormat) throw new System.InvalidOperationException("Version must first be migrated to the bitfield format");
finishedMigrations |= flag;
}
public bool AddAndMaybeRunMigration (int flag, bool filter = true) {
if ((flag & MIGRATE_TO_BITFIELD) != 0) throw new System.ArgumentException("Cannot use the MIGRATE_TO_BITFIELD flag when adding a migration");
allMigrations |= flag;
if (filter) {
var res = (finishedMigrations & flag) != flag;
MarkMigrationFinished(flag);
return res;
} else return false;
}
public void IgnoreMigrationAttempt () {
ignore = true;
}
}
}
|