11package dev .koifysh .archipelago ;
22
3+ import com .google .gson .JsonObject ;
34import dev .koifysh .archipelago .events .RetrievedEvent ;
45import dev .koifysh .archipelago .flags .ItemsHandling ;
56import dev .koifysh .archipelago .network .server .ConnectUpdatePacket ;
1718import java .io .*;
1819import java .net .URI ;
1920import java .net .URISyntaxException ;
21+ import java .nio .CharBuffer ;
22+ import java .nio .charset .StandardCharsets ;
23+ import java .nio .file .Files ;
2024import java .nio .file .Path ;
2125import java .nio .file .Paths ;
2226import java .util .*;
27+ import java .util .logging .Level ;
2328import java .util .logging .Logger ;
2429
2530public abstract class Client {
2631
2732 private final static Logger LOGGER = Logger .getLogger (Client .class .getName ());
2833
29- private static String OS = System .getProperty ("os.name" ).toLowerCase ();
34+ private static final String OS = System .getProperty ("os.name" ).toLowerCase ();
35+
36+ private static final Path cachePath ;
37+ private static final Path datapackageCachePath ;
3038
31- private static final Path windowsDataPackageCache ;
32-
33- private static final Path otherDataPackageCache ;
34-
3539 static
3640 {
3741 String appData = System .getenv ("LOCALAPPDATA" );
3842 String winHome = System .getenv ("USERPROFILE" );
3943 String userHome = System .getProperty ("user.home" );
4044
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" );
45+ if (OS .startsWith ("windows" ))
46+ {
47+ if (appData == null || appData .isEmpty ()) {
48+ cachePath = Paths .get (winHome , "appdata" , "local" , "Archipelago" , "Cache" );
49+ } else {
50+ cachePath = Paths .get (appData , "Archipelago" , "Cache" );
51+ }
4552 }
46-
47- otherDataPackageCache = Paths .get (userHome , ".cache" , "Archipelago" , "datapackage" );
53+ else
54+ {
55+ cachePath = Paths .get (userHome , ".cache" , "Archipelago" );
56+ }
57+ datapackageCachePath = cachePath .resolve ("datapackage" );
58+
4859 }
4960
61+ private static String uuid = null ;
62+
5063 private static Path dataPackageLocation ;
5164
5265 protected Map <String ,String > versions ;
@@ -61,11 +74,9 @@ public abstract class Client {
6174
6275 private String password ;
6376
64- private final String UUID ;
65-
6677 private RoomInfoPacket roomInfo ;
6778
68- private DataPackage dataPackage ;
79+ private final DataPackage dataPackage ;
6980
7081 public static Client client ;
7182
@@ -85,19 +96,8 @@ public abstract class Client {
8596 private int itemsHandlingFlags = 0b000;
8697
8798 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-
99+ dataPackageLocation = datapackageCachePath ;
100+ dataPackage = new DataPackage ();
101101 eventManager = new EventManager ();
102102 locationManager = new LocationManager (this );
103103 itemManager = new ItemManager (this );
@@ -159,6 +159,57 @@ public void removeTag(String tag) {
159159 }
160160
161161
162+ /**
163+ * Gets the UUID of clients on this machine
164+ * @return UUID of the client, this should theoretically never change.
165+ */
166+ public static String getUUID () {
167+ if (uuid == null )
168+ {
169+ synchronized (DataPackage .class )
170+ {
171+ if (uuid != null ) {
172+ return uuid ;
173+ }
174+ String tmp = null ;
175+ File common = cachePath .resolve ("common.json" ).toFile ();
176+ JsonObject data = new JsonObject ();
177+ if (common .exists ())
178+ {
179+ try (BufferedReader reader = Files .newBufferedReader (common .toPath (), StandardCharsets .UTF_8 ))
180+ {
181+ data = gson .fromJson (reader , JsonObject .class );
182+ }
183+ catch (IOException ex )
184+ {
185+ LOGGER .log (Level .WARNING ,"Failed to load common uuid" , ex );
186+ // We probably will fail to write
187+ return uuid = UUID .randomUUID ().toString ();
188+ }
189+ }
190+
191+ tmp = data .get ("uuid" ).getAsString ();
192+ if (tmp != null )
193+ {
194+ return uuid = tmp ;
195+ }
196+
197+ tmp = UUID .randomUUID ().toString ();
198+ data .addProperty ("uuid" , uuid );
199+ try (BufferedWriter writer = Files .newBufferedWriter (common .toPath (),StandardCharsets .UTF_8 ))
200+ {
201+ writer .write (gson .toJson (data ));
202+ }
203+ catch (IOException ex )
204+ {
205+ LOGGER .log (Level .WARNING ,"Failed to save common uuid" , ex );
206+ }
207+ return uuid = tmp ;
208+ }
209+ }
210+ return uuid ;
211+ }
212+
162213 protected void loadDataPackage () {
163214 synchronized (Client .class ){
164215 File directoryPath = dataPackageLocation .toFile ();
@@ -475,14 +526,6 @@ public void reconnect() {
475526 webSocket .reconnect ();
476527 }
477528
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-
486529 /**
487530 * gets the alias of this slot.
488531 * @return Alias of the slot connected to.
0 commit comments