1 /** 2 Inochi2D Unique IDs 3 4 Copyright: 5 Copyright © 2020-2026, Inochi2D Project 6 7 License: 8 $(LINK2 https://github.com/Inochi2D/inochi2d/blob/main/LICENSE, BSD 2-clause License) 9 10 Authors: 11 Luna Nielsen 12 seagetch 13 */ 14 module inochi2d.core.guid; 15 import inochi2d.core.serde; 16 import inochi2d.common; 17 import nulib.random; 18 import nulib.uuid; 19 20 /** 21 A globally unique ID. 22 */ 23 alias GUID = UUID; 24 25 /** 26 Creates a GUID from a legacy UUID. 27 28 Params: 29 legacyUUID = The legacy UUID to convert. 30 31 Returns: 32 A new GUID, with the time set to the legacy UUID, 33 and the last byte set to 255. 34 35 Notes: 36 You should ideally use GUIDs instead of UUIDs. 37 */ 38 GUID toGuid(uint legacyUUID) @nogc { 39 return GUID(legacyUUID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255); 40 } 41 42 /** 43 Creates a new GUID. 44 45 Returns: 46 A new random GUID based on system time. 47 */ 48 GUID inNewGUID() @nogc { 49 return GUID.createRandom(__i2d_uuid_random); 50 } 51 52 /** 53 Tries to get a GUID from a JSON Object. 54 55 Inochi2D has transitioned over to GUIDs, 56 as such we convert the old 32 bit UUIDs into 57 fake GUIDs if they're in use. 58 59 Params: 60 obj = The object to get the GUID from. 61 state = The state of the deserializer. 62 uuidKey = The legacy UUID key to check for. 63 guidKey = The GUID key to check for. 64 65 Returns: 66 A GUID. 67 */ 68 GUID tryGetGUID(ref DataNode obj, ref ModelState state, string uuidKey, string guidKey = "guid") @nogc { 69 if (uuidKey in obj && obj[uuidKey].isNumber) 70 return obj.tryGet!uint(state, uuidKey).toGuid; 71 else { 72 return GUID(obj.tryGet!string(state, guidKey)); 73 } 74 } 75 76 /** 77 Tries to get a GUID from a JSON Object. 78 79 Params: 80 obj = The object to get the GUID from. 81 state = The state of the deserializer. 82 */ 83 GUID tryGetGUID(ref DataNode obj, ref ModelState state) @nogc { 84 return obj.isNumber ? 85 obj.tryGet!uint(state, uint.max).toGuid : GUID(obj.text); 86 } 87 88 // 89 // IMPLEMENTATION DETAILS. 90 // 91 private: 92 93 __gshared Random __i2d_uuid_random; 94 pragma(crt_constructor) 95 export extern (C) void __i2d_init_random() { 96 import numem : nogc_new; 97 98 __i2d_uuid_random = nogc_new!Random(0); 99 }