11package dev .koifysh .archipelago ;
22
3+ import com .google .gson .JsonElement ;
4+ import com .google .gson .JsonObject ;
35import dev .koifysh .archipelago .events .RetrievedEvent ;
46import dev .koifysh .archipelago .flags .ItemsHandling ;
57import dev .koifysh .archipelago .network .server .ConnectUpdatePacket ;
1719import java .io .*;
1820import java .net .URI ;
1921import java .net .URISyntaxException ;
22+ import java .nio .CharBuffer ;
23+ import java .nio .charset .StandardCharsets ;
24+ import java .nio .file .Files ;
2025import java .nio .file .Path ;
2126import java .nio .file .Paths ;
2227import java .util .*;
28+ import java .util .logging .Level ;
2329import java .util .logging .Logger ;
2430
2531public abstract class Client {
2632
2733 private final static Logger LOGGER = Logger .getLogger (Client .class .getName ());
2834
29- private static String OS = System .getProperty ("os.name" ).toLowerCase ();
35+ private static final String OS = System .getProperty ("os.name" ).toLowerCase ();
36+
37+ private static final Path cachePath ;
38+ private static final Path datapackageCachePath ;
3039
31- private static final Path windowsDataPackageCache ;
32-
33- private static final Path otherDataPackageCache ;
34-
3540 static
3641 {
3742 String appData = System .getenv ("LOCALAPPDATA" );
3843 String winHome = System .getenv ("USERPROFILE" );
3944 String userHome = System .getProperty ("user.home" );
4045
41- if (appData == null || appData .isEmpty ()) {
42- windowsDataPackageCache = Paths .get (winHome , "appdata" ,"local" ,"Archipelago" ,"cache" ,"datapackage" );
43- } else {
44- windowsDataPackageCache = Paths .get (appData , "Archipelago" , "cache" , "datapackage" );
46+ if (OS .startsWith ("windows" ))
47+ {
48+ if (appData == null || appData .isEmpty ()) {
49+ cachePath = Paths .get (winHome , "appdata" , "local" , "Archipelago" , "Cache" );
50+ } else {
51+ cachePath = Paths .get (appData , "Archipelago" , "Cache" );
52+ }
4553 }
46-
47- otherDataPackageCache = Paths .get (userHome , ".cache" , "Archipelago" , "datapackage" );
54+ else
55+ {
56+ cachePath = Paths .get (userHome , ".cache" , "Archipelago" );
57+ }
58+ datapackageCachePath = cachePath .resolve ("datapackage" );
59+
4860 }
4961
62+ private static String uuid = null ;
63+
5064 private static Path dataPackageLocation ;
5165
5266 protected Map <String ,String > versions ;
@@ -61,11 +75,9 @@ public abstract class Client {
6175
6276 private String password ;
6377
64- private final String UUID ;
65-
6678 private RoomInfoPacket roomInfo ;
6779
68- private DataPackage dataPackage ;
80+ private final DataPackage dataPackage ;
6981
7082 public static Client client ;
7183
@@ -85,19 +97,8 @@ public abstract class Client {
8597 private int itemsHandlingFlags = 0b000;
8698
8799 public Client () {
88- //Determine what platform we are on
89- if (OS .startsWith ("windows" )){
90- dataPackageLocation = windowsDataPackageCache ;
91- } else {
92- dataPackageLocation = otherDataPackageCache ;
93- }
94-
95- if (dataPackage == null ){
96- dataPackage = new DataPackage ();
97- }
98-
99- UUID = dataPackage .getUUID ();
100-
100+ dataPackageLocation = datapackageCachePath ;
101+ dataPackage = new DataPackage ();
101102 eventManager = new EventManager ();
102103 locationManager = new LocationManager (this );
103104 itemManager = new ItemManager (this );
@@ -159,6 +160,60 @@ public void removeTag(String tag) {
159160 }
160161
161162
163+ /**
164+ * Gets the UUID of clients on this machine
165+ * @return UUID of the client, this should theoretically never change.
166+ */
167+ public static String getUUID () {
168+ if (uuid == null )
169+ {
170+ synchronized (DataPackage .class )
171+ {
172+ if (uuid != null ) {
173+ return uuid ;
174+ }
175+ String tmp = null ;
176+ File common = cachePath .resolve ("common.json" ).toFile ();
177+ JsonObject data = new JsonObject ();
178+ if (common .exists ())
179+ {
180+ try (BufferedReader reader = Files .newBufferedReader (common .toPath (), StandardCharsets .UTF_8 ))
181+ {
182+ data = gson .fromJson (reader , JsonObject .class );
183+ }
184+ catch (IOException ex )
185+ {
186+ LOGGER .log (Level .WARNING ,"Failed to load common uuid" , ex );
187+ // We probably will fail to write
188+ return uuid = UUID .randomUUID ().toString ();
189+ }
190+ }
191+ JsonElement uuidEle = data .get ("uuid" );
192+ if (uuidEle != null && !uuidEle .isJsonNull ())
193+ {
194+ tmp = uuidEle .getAsString ();
195+ }
196+ if (tmp != null )
197+ {
198+ return uuid = tmp ;
199+ }
200+
201+ tmp = UUID .randomUUID ().toString ();
202+ data .addProperty ("uuid" , tmp );
203+ try (BufferedWriter writer = Files .newBufferedWriter (common .toPath (),StandardCharsets .UTF_8 ))
204+ {
205+ writer .write (gson .toJson (data ));
206+ }
207+ catch (IOException ex )
208+ {
209+ LOGGER .log (Level .WARNING ,"Failed to save common uuid" , ex );
210+ }
211+ return uuid = tmp ;
212+ }
213+ }
214+ return uuid ;
215+ }
216+
162217 protected void loadDataPackage () {
163218 synchronized (Client .class ){
164219 File directoryPath = dataPackageLocation .toFile ();
@@ -475,14 +530,6 @@ public void reconnect() {
475530 webSocket .reconnect ();
476531 }
477532
478- /**
479- * Gets the UUID of this client.
480- * @return UUID of the client, this should theoretically never change.
481- */
482- public String getUUID () {
483- return UUID ;
484- }
485-
486533 /**
487534 * gets the alias of this slot.
488535 * @return Alias of the slot connected to.
0 commit comments