44import com .google .gson .JsonParser ;
55import net .fabricmc .loader .api .FabricLoader ;
66import net .i_no_am .viewmodel .Global ;
7- import net .i_no_am .viewmodel .ViewModel ;
87import net .i_no_am .viewmodel .config .Config ;
98import net .minecraft .client .gui .screen .ConfirmScreen ;
109import net .minecraft .text .Text ;
1110import net .minecraft .util .Formatting ;
1211import net .minecraft .util .Util ;
1312import org .jetbrains .annotations .NotNull ;
1413
14+ import java .io .File ;
15+ import java .io .FileWriter ;
16+ import java .io .IOException ;
1517import java .net .URI ;
1618import java .net .http .HttpClient ;
1719import java .net .http .HttpRequest ;
1820import java .net .http .HttpResponse ;
1921import java .time .Duration ;
22+ import java .time .LocalDateTime ;
23+ import java .time .format .DateTimeFormatter ;
2024import java .util .HashMap ;
2125import java .util .Map ;
2226
@@ -29,8 +33,8 @@ public class Version implements Global {
2933 private final double version ;
3034
3135 /**
32- * @param api The link to the GITHUB repo api.
33- * @param download The link to the download page.
36+ @param api The link to the GITHUB repo api.
37+ @param download The link to the download page.
3438 ***/
3539
3640 private Version (@ NotNull String api , @ NotNull String download ) throws Exception {
@@ -43,15 +47,14 @@ public static Version create(String apiLink, String downloadLink) {
4347 try {
4448 return new Version (apiLink , downloadLink );
4549 } catch (Exception e ) {
50+ Utils .log (e .getMessage ());
4651 throw new RuntimeException (e );
4752 }
4853 }
4954
5055 public void notifyUpdate (boolean printVersions ) {
5156 if (!bl && mc .currentScreen == null && mc .player != null && !isUpdated () && Config .shouldCheck ) {
52- if (printVersions ) {
53- ViewModel .Log ("Versions: \n Current Version: " + getSelf () + "\n " + "Online Version: " + getApi ());
54- }
57+ if (printVersions ) Utils .log ("Versions: \n Current Version: " + getSelf () + "\n " + "Online Version: " + getApi () + "\n " + "isUpdated() state: " + isUpdated ());
5558 mc .setScreen (new ConfirmScreen (confirmed -> {
5659 if (confirmed ) {
5760 Util .getOperatingSystem ().open (URI .create (download ));
@@ -76,18 +79,30 @@ private static double getSelf() {
7679 return parseVersion (versionString );
7780 }
7881
79- private static double parseVersion (String version ) {
82+ /**
83+ * Parses a version string into a composite integer.
84+ * For example, "1.0.1" becomes 10001 and "1.0.2" becomes 10002.
85+ *
86+ * @param version the version string that is taking from the mod version from gradle.properties
87+ * @return the composite version as an integer
88+ */
89+ private static int parseVersion (String version ) {
8090 String [] parts = version .split ("-" );
81-
8291 for (String part : parts ) {
8392 if (part .matches ("\\ d+\\ .\\ d+\\ .\\ d+" )) {
8493 String [] versionNumbers = part .split ("\\ ." );
85- double parsedVersion = Double .parseDouble (versionNumbers [0 ] + "." + versionNumbers [1 ]);
86- return parsedVersion * 10 ;
87- } else if (part .matches ("\\ d+\\ .\\ d+" )) return Double .parseDouble (part );
94+ int major = Integer .parseInt (versionNumbers [0 ]);
95+ int minor = Integer .parseInt (versionNumbers [1 ]);
96+ int patch = Integer .parseInt (versionNumbers [2 ]);
97+ return major * 10000 + minor * 100 + patch ;
98+ } else if (part .matches ("\\ d+\\ .\\ d+" )) {
99+ String [] versionNumbers = part .split ("\\ ." );
100+ int major = Integer .parseInt (versionNumbers [0 ]);
101+ int minor = Integer .parseInt (versionNumbers [1 ]);
102+ return major * 10000 + minor * 100 ;
103+ }
88104 }
89-
90- return 0.0 ;
105+ return 0 ;
91106 }
92107
93108 private double getVApi () throws Exception {
@@ -111,4 +126,41 @@ private double getVApi() throws Exception {
111126 versionCache .put (api , parsedVersion );
112127 return parsedVersion ;
113128 }
114- }
129+
130+ public static class Utils {
131+ private static final File logFile = new File (mc .runDirectory + "/logs" , "view-model.log" );
132+ private static boolean wroteLaunchHeader = false ;
133+ static {
134+ try {
135+ logFile .getParentFile ().mkdirs ();
136+ if (logFile .exists ()) {
137+ logFile .delete ();
138+ }
139+ } catch (Exception e ) {
140+ System .err .println ("[View-Model] Failed to prepare log file: " + e .getMessage ());
141+ }
142+ }
143+
144+ public static void log (String message ) {
145+ if (isDev ) {
146+ System .out .println ("[ViewModel] " + message );
147+ }
148+
149+ try (FileWriter writer = new FileWriter (logFile , true )) {
150+ if (!wroteLaunchHeader ) {
151+ writer .write ("=== Launch at " + getTimestamp (true ) + " ===\n " );
152+ wroteLaunchHeader = true ;
153+ }
154+ writer .write (getTimestamp (false ) + " " + message + "\n " );
155+ } catch (IOException e ) {
156+ System .err .println ("Failed to write to view-model.log: " + e .getMessage ());
157+ }
158+ }
159+
160+ private static String getTimestamp (boolean includeFullDate ) {
161+ DateTimeFormatter fullDate = DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss:SSS" );
162+ DateTimeFormatter timeOnly = DateTimeFormatter .ofPattern ("HH:mm:ss:SSS" );
163+ return LocalDateTime .now ().format (includeFullDate ? fullDate : timeOnly );
164+ }
165+ }
166+ }
0 commit comments